From: Vincent Pit Date: Sun, 29 Jun 2008 16:41:13 +0000 (+0200) Subject: Importing Linux-SysInfo-0.01.tar.gz X-Git-Tag: v0.01 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=31d5c1d33efaa2d0b4736bad344a68c3d2c90aed;p=perl%2Fmodules%2FLinux-SysInfo.git Importing Linux-SysInfo-0.01.tar.gz --- 0010ec75da61adecfb655901837909d90404c94c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..818c7de --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +blib* +pm_to_blib* + +Makefile{,.old} +Build +_build* + +*.tar.gz +Linux-SysInfo-* + +core.* +*.{c,o,so,bs,out,def,exp} + +cover_db +*.{gcda,gcov,gcno} + diff --git a/Changes b/Changes new file mode 100644 index 0000000..8c5dd92 --- /dev/null +++ b/Changes @@ -0,0 +1,5 @@ +Revision history for Linux-SysInfo + +0.01 2007-04-11 12:45 UTC + First version, released on an unsuspecting world. + diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..359ca9e --- /dev/null +++ b/MANIFEST @@ -0,0 +1,13 @@ +Changes +MANIFEST +META.yml # Will be created by "make dist" +Makefile.PL +README +SysInfo.xs +lib/Linux/SysInfo.pm +t/00-load.t +t/10-standard.t +t/20-extended.t +t/boilerplate.t +t/pod-coverage.t +t/pod.t diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..708db53 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,16 @@ +use strict; +use warnings; +use ExtUtils::MakeMaker; + +WriteMakefile( + NAME => 'Linux::SysInfo', + AUTHOR => 'Vincent Pit ', + VERSION_FROM => 'lib/Linux/SysInfo.pm', + ABSTRACT_FROM => 'lib/Linux/SysInfo.pm', + PL_FILES => {}, + PREREQ_PM => { + 'Test::More' => 0, + }, + dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, + clean => { FILES => 'Linux-SysInfo-*' }, +); diff --git a/README b/README new file mode 100644 index 0000000..47fe5f4 --- /dev/null +++ b/README @@ -0,0 +1,105 @@ +NAME + Linux::SysInfo - Perl interface to the sysinfo(2) Linux system call. + +VERSION + Version 0.01 + +SYNOPSIS + use Linux::SysInfo qw/sysinfo/; + + my $si = sysinfo; + print "$_: $si->{$_}\n" for keys %$si; + +DESCRIPTION + This module is a wrapper around the sysinfo(2) Linux system call. It + gives information about the current uptime, load average, memory usage + and processes running. Other systems have also this system call (e.g. + Solaris), but in most cases the returned information is different. + +EXPORT + The only function of this module, "sysinfo", and the constant + "LS_HAS_EXTENDED" are only exported on request. + +FUNCTIONS + "sysinfo" + This function takes no argument. It returns undef on failure or a hash + reference whose keys are the members name of the struct sysinfo on + success : + + "uptime" + Seconds elapsed since the system booted. + + "load1", "load5", "load15" + 1, 5 and 15 minutes load average. + + "totalram" + Total usable main memory size. + + "freeram" + Available memory size. + + "sharedram" + Amount of shared memory. + + "bufferram" + Memory used by buffers. + + "totalswap" + Total swap space size. + + "freeswap" + Swap space stil available. + + "procs" + Number of current processes. + + Prior to Linux 2.3.23 on i386 and 2.3.48 on all other architectures, the + memory sizes were given in bytes. Since then, the following members are + also available and all the memory sizes are given as multiples of + mem_unit bytes : + + "totalhigh" + Total high memory size. + + "freehigh" + Available high memory size. + + "mem_unit" + Memory unit size in bytes. + +CONSTANTS + LS_HAS_EXTENDED + This constant is set to 1 if your kernel supports the three extended + fields "totalhigh", "freehigh" and "mem_unit" ; and to 0 otherwise. + +SEE ALSO + The sysinfo(2) man page. + + Sys::Info : Gather information about your system. + + Sys::CpuLoad : Try several different methods to retrieve the load + average. + + BSD::getloadavg : Wrapper to the getloadavg(3) BSD system call. + +AUTHOR + Vincent Pit, "" + +BUGS + Please report any bugs or feature requests to "bug-linux-sysinfo at + rt.cpan.org", or through the web interface at + . I will + be notified, and then you'll automatically be notified of progress on + your bug as I make changes. + +SUPPORT + You can find documentation for this module with the perldoc command. + + perldoc Linux::SysInfo + +COPYRIGHT & LICENSE + Copyright 2007 Vincent Pit, all rights reserved. + + This program is free software; you can redistribute it and/or modify it + under the same terms as Perl itself. + diff --git a/SysInfo.xs b/SysInfo.xs new file mode 100644 index 0000000..9874185 --- /dev/null +++ b/SysInfo.xs @@ -0,0 +1,103 @@ +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" + +#include +#include + +#if ((defined(__i386__) || defined(__x86_64__)) && (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 3, 23))) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 3, 48)) +# define SYSINFO_EXTENDED 1 +#else +# define SYSINFO_EXTENDED 0 +#endif + +typedef struct { + const char* key; + U32 klen; + U32 hash; +} sysinfo_key; + +#define SYSINFO_KEY_SET_HASH(S) PERL_HASH((S).hash, (S).key, (S).klen) +#define SYSINFO_KEY_STORE(H,S,V) hv_store((H), (S).key, (S).klen, (V), (S).hash) + +static sysinfo_key key_uptime = { "uptime", 6, 0 }; +static sysinfo_key key_load1 = { "load1", 5, 0 }; +static sysinfo_key key_load5 = { "load5", 5, 0 }; +static sysinfo_key key_load15 = { "load15", 6, 0 }; +static sysinfo_key key_totalram = { "totalram", 8, 0 }; +static sysinfo_key key_freeram = { "freeram", 7, 0 }; +static sysinfo_key key_sharedram = { "sharedram", 9, 0 }; +static sysinfo_key key_bufferram = { "bufferram", 9, 0 }; +static sysinfo_key key_totalswap = { "totalswap", 9, 0 }; +static sysinfo_key key_freeswap = { "freeswap", 8, 0 }; +static sysinfo_key key_procs = { "procs", 5, 0 }; +#if SYSINFO_EXTENDED +static sysinfo_key key_totalhigh = { "totalhigh", 9, 0 }; +static sysinfo_key key_freehigh = { "freehigh", 8, 0 }; +static sysinfo_key key_mem_unit = { "mem_unit", 8, 0 }; +#endif /* SYSINFO_EXTENDED */ + +MODULE = Linux::SysInfo PACKAGE = Linux::SysInfo + +PROTOTYPES: ENABLE + +BOOT: +{ + SYSINFO_KEY_SET_HASH(key_uptime); + SYSINFO_KEY_SET_HASH(key_load1); + SYSINFO_KEY_SET_HASH(key_load5); + SYSINFO_KEY_SET_HASH(key_load15); + SYSINFO_KEY_SET_HASH(key_totalram); + SYSINFO_KEY_SET_HASH(key_freeram); + SYSINFO_KEY_SET_HASH(key_sharedram); + SYSINFO_KEY_SET_HASH(key_bufferram); + SYSINFO_KEY_SET_HASH(key_totalswap); + SYSINFO_KEY_SET_HASH(key_freeswap); + SYSINFO_KEY_SET_HASH(key_procs); +#if SYSINFO_EXTENDED + SYSINFO_KEY_SET_HASH(key_totalhigh); + SYSINFO_KEY_SET_HASH(key_freehigh); + SYSINFO_KEY_SET_HASH(key_mem_unit); +#endif /* SYSINFO_EXTENDED */ +} + +SV * +sysinfo() +PREINIT: + struct sysinfo si; + HV* h; +CODE: + if (sysinfo(&si) == -1) { + XSRETURN_UNDEF; + } + + h = newHV(); +/* sv_2mortal((SV *) h); */ + + SYSINFO_KEY_STORE(h, key_uptime, newSViv(si.uptime)); + SYSINFO_KEY_STORE(h, key_load1, newSVuv(si.loads[0])); + SYSINFO_KEY_STORE(h, key_load5, newSVuv(si.loads[1])); + SYSINFO_KEY_STORE(h, key_load15, newSVuv(si.loads[2])); + SYSINFO_KEY_STORE(h, key_totalram, newSVuv(si.totalram)); + SYSINFO_KEY_STORE(h, key_freeram, newSVuv(si.freeram)); + SYSINFO_KEY_STORE(h, key_sharedram, newSVuv(si.sharedram)); + SYSINFO_KEY_STORE(h, key_bufferram, newSVuv(si.bufferram)); + SYSINFO_KEY_STORE(h, key_totalswap, newSVuv(si.totalswap)); + SYSINFO_KEY_STORE(h, key_freeswap, newSVuv(si.freeswap)); + SYSINFO_KEY_STORE(h, key_procs, newSVuv(si.procs)); +#if SYSINFO_EXTENDED + SYSINFO_KEY_STORE(h, key_totalhigh, newSVuv(si.totalhigh)); + SYSINFO_KEY_STORE(h, key_freehigh, newSVuv(si.freehigh)); + SYSINFO_KEY_STORE(h, key_mem_unit, newSVuv(si.mem_unit)); +#endif /* SYSINFO_EXTENDED */ + + RETVAL = newRV_noinc((SV *) h); +OUTPUT: + RETVAL + +SV * +LS_HAS_EXTENDED() +CODE: + RETVAL = newSViv(SYSINFO_EXTENDED); +OUTPUT: + RETVAL diff --git a/lib/Linux/SysInfo.pm b/lib/Linux/SysInfo.pm new file mode 100644 index 0000000..8b7eb0e --- /dev/null +++ b/lib/Linux/SysInfo.pm @@ -0,0 +1,152 @@ +package Linux::SysInfo; + +use warnings; +use strict; + +=head1 NAME + +Linux::SysInfo - Perl interface to the sysinfo(2) Linux system call. + +=head1 VERSION + +Version 0.01 + +=cut + +our $VERSION = '0.01'; + +=head1 SYNOPSIS + + use Linux::SysInfo qw/sysinfo/; + + my $si = sysinfo; + print "$_: $si->{$_}\n" for keys %$si; + +=head1 DESCRIPTION + +This module is a wrapper around the sysinfo(2) Linux system call. It gives information about the current uptime, load average, memory usage and processes running. Other systems have also this system call (e.g. Solaris), but in most cases the returned information is different. + +=head1 EXPORT + +The only function of this module, C, and the constant C are only exported on request. + +=cut + +use base qw/Exporter/; + +our @EXPORT_OK = qw/sysinfo LS_HAS_EXTENDED/; + +our %EXPORT_TAGS = ( 'all' => [ @EXPORT_OK ] ); + +require XSLoader; + +XSLoader::load('Linux::SysInfo', $VERSION); + +=head1 FUNCTIONS + +=head2 C + +This function takes no argument. It returns undef on failure or a hash reference whose keys are the members name of the struct sysinfo on success : + +=over 4 + +=item C + +Seconds elapsed since the system booted. + +=item C, C, C + +1, 5 and 15 minutes load average. + +=item C + +Total usable main memory size. + +=item C + +Available memory size. + +=item C + +Amount of shared memory. + +=item C + +Memory used by buffers. + +=item C + +Total swap space size. + +=item C + +Swap space stil available. + +=item C + +Number of current processes. + +=back + +Prior to Linux 2.3.23 on i386 and 2.3.48 on all other architectures, the memory sizes were given in bytes. Since then, the following members are also available and all the memory sizes are given as multiples of mem_unit bytes : + +=over 4 + +=item C + +Total high memory size. + +=item C + +Available high memory size. + +=item C + +Memory unit size in bytes. + +=back + +=head1 CONSTANTS + +=head2 LS_HAS_EXTENDED + +This constant is set to 1 if your kernel supports the three extended fields C, C and C ; and to 0 otherwise. + +=head1 SEE ALSO + +The C man page. + +L : Gather information about your system. + +L : Try several different methods to retrieve the load average. + +L : Wrapper to the C BSD system call. + +=head1 AUTHOR + +Vincent Pit, C<< >> + +=head1 BUGS + +Please report any bugs or feature requests to +C, or through the web interface at +L. +I will be notified, and then you'll automatically be notified of progress on +your bug as I make changes. + +=head1 SUPPORT + +You can find documentation for this module with the perldoc command. + + perldoc Linux::SysInfo + +=head1 COPYRIGHT & LICENSE + +Copyright 2007 Vincent Pit, all rights reserved. + +This program is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=cut + +1; # End of Linux::SysInfo diff --git a/t/00-load.t b/t/00-load.t new file mode 100644 index 0000000..3ba29b7 --- /dev/null +++ b/t/00-load.t @@ -0,0 +1,9 @@ +#!perl -T + +use Test::More tests => 1; + +BEGIN { + use_ok( 'Linux::SysInfo' ); +} + +diag( "Testing Linux::SysInfo $Linux::SysInfo::VERSION, Perl $], $^X" ); diff --git a/t/10-standard.t b/t/10-standard.t new file mode 100644 index 0000000..69a0d6e --- /dev/null +++ b/t/10-standard.t @@ -0,0 +1,10 @@ +#!perl -T + +use Test::More tests => 12; + +use Linux::SysInfo qw/sysinfo/; + +my $si = sysinfo; +ok(defined $si); + +ok(exists $si->{$_}) for qw/uptime load1 load5 load15 totalram freeram sharedram bufferram totalswap freeswap procs/; diff --git a/t/20-extended.t b/t/20-extended.t new file mode 100644 index 0000000..c54751e --- /dev/null +++ b/t/20-extended.t @@ -0,0 +1,15 @@ +#!perl -T + +use Test::More tests => 4; + +use Linux::SysInfo qw/sysinfo LS_HAS_EXTENDED/; + +SKIP: +{ + skip 'Your kernel does not support extended sysinfo fields', 4 unless LS_HAS_EXTENDED; + + my $si = sysinfo; + ok(defined $si); + + ok(exists $si->{$_}) for qw/totalhigh freehigh mem_unit/; +} diff --git a/t/boilerplate.t b/t/boilerplate.t new file mode 100644 index 0000000..b80a670 --- /dev/null +++ b/t/boilerplate.t @@ -0,0 +1,48 @@ +#!perl -T + +use strict; +use warnings; +use Test::More tests => 3; + +sub not_in_file_ok { + my ($filename, %regex) = @_; + open my $fh, "<", $filename + or die "couldn't open $filename for reading: $!"; + + my %violated; + + while (my $line = <$fh>) { + while (my ($desc, $regex) = each %regex) { + if ($line =~ $regex) { + push @{$violated{$desc}||=[]}, $.; + } + } + } + + if (%violated) { + fail("$filename contains boilerplate text"); + diag "$_ appears on lines @{$violated{$_}}" for keys %violated; + } else { + pass("$filename contains no boilerplate text"); + } +} + +not_in_file_ok(README => + "The README is used..." => qr/The README is used/, + "'version information here'" => qr/to provide version information/, +); + +not_in_file_ok(Changes => + "placeholder date/time" => qr(Date/time) +); + +sub module_boilerplate_ok { + my ($module) = @_; + not_in_file_ok($module => + 'the great new $MODULENAME' => qr/ - The great new /, + 'boilerplate description' => qr/Quick summary of what the module/, + 'stub function definition' => qr/function[12]/, + ); +} + +module_boilerplate_ok('lib/Linux/SysInfo.pm'); diff --git a/t/pod-coverage.t b/t/pod-coverage.t new file mode 100644 index 0000000..703f91d --- /dev/null +++ b/t/pod-coverage.t @@ -0,0 +1,6 @@ +#!perl -T + +use Test::More; +eval "use Test::Pod::Coverage 1.04"; +plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; +all_pod_coverage_ok(); diff --git a/t/pod.t b/t/pod.t new file mode 100644 index 0000000..976d7cd --- /dev/null +++ b/t/pod.t @@ -0,0 +1,6 @@ +#!perl -T + +use Test::More; +eval "use Test::Pod 1.14"; +plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; +all_pod_files_ok();