Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial import

  • Loading branch information...
commit d0e1dff8361a6b88913aa65fe9a2d63d9fce8b36 0 parents
@alanhaggai authored
107 Cpuinfo.xs
@@ -0,0 +1,107 @@
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+
+#include "ppport.h"
+#include <proccpuinfo.h>
+
+typedef proccpuinfo* Linux__Proc__Cpuinfo;
+
+MODULE = Linux::Proc::Cpuinfo PACKAGE = Linux::Proc::Cpuinfo
+
+Linux::Proc::Cpuinfo
+new(package, ...)
+ char *package
+ PREINIT:
+ char *filename = NULL;
+ PPCODE:
+ if (items > 1) {
+ filename = (char *)SvPV_nolen(ST(1));
+ proccpuinfo_set_filename(filename);
+ }
+
+ proccpuinfo *info = proccpuinfo_read();
+ if (!info) {
+ return XSRETURN_UNDEF;
+ }
+
+ /*
+ * libproccpuinfo does not return NULL for files that do not exist.
+ * Check if we have read access to the file. Else return undef.
+ */
+ if (filename && access(filename, R_OK) == -1) {
+ return XSRETURN_UNDEF;
+ }
+
+ ST(0) = newSV(0);
+ sv_setref_pv(ST(0), "Linux::Proc::Cpuinfo", (void *)info);
+ XSRETURN(1);
+
+void
+destroy(self)
+ Linux::Proc::Cpuinfo self
+ CODE:
+ proccpuinfo_free(self);
+
+SV *
+architecture(self)
+ Linux::Proc::Cpuinfo self
+ CODE:
+ RETVAL = newSVpv(self->architecture, 0);
+ OUTPUT:
+ RETVAL
+
+SV *
+hardware_platform(self)
+ Linux::Proc::Cpuinfo self
+ CODE:
+ RETVAL = newSVpv(self->hardware_platform, 0);
+ OUTPUT:
+ RETVAL
+
+SV *
+frequency(self)
+ Linux::Proc::Cpuinfo self
+ CODE:
+ if (self->frequency == 0) {
+ RETVAL = &PL_sv_undef;
+ }
+ else {
+ RETVAL = newSVnv(self->frequency);
+ }
+ OUTPUT:
+ RETVAL
+
+SV *
+bogomips(self)
+ Linux::Proc::Cpuinfo self
+ CODE:
+ if (self->bogomips == 0) {
+ RETVAL = &PL_sv_undef;
+ }
+ else {
+ RETVAL = newSVnv(self->bogomips);
+ }
+ OUTPUT:
+ RETVAL
+
+SV *
+cache(self)
+ Linux::Proc::Cpuinfo self
+ CODE:
+ if (self->cache == 0) {
+ RETVAL = &PL_sv_undef;
+ }
+ else {
+ RETVAL = newSVuv(self->cache);
+ }
+ OUTPUT:
+ RETVAL
+
+SV *
+cpus(self)
+ Linux::Proc::Cpuinfo self
+ CODE:
+ RETVAL = newSVuv(self->cpus);
+ OUTPUT:
+ RETVAL
9 MANIFEST
@@ -0,0 +1,9 @@
+Cpuinfo.xs
+lib/Linux/Proc/Cpuinfo.pm
+Makefile.PL
+MANIFEST
+ppport.h
+README
+t/cpuinfo
+t/Linux-Proc-Cpuinfo.t
+typemap
33 Makefile.PL
@@ -0,0 +1,33 @@
+use 5.006000;
+
+use Devel::CheckLib;
+use ExtUtils::MakeMaker;
+
+check_lib_or_exit( 'lib' => 'proccpuinfo', 'header' => 'proccpuinfo.h' );
+
+WriteMakefile(
+ 'NAME' => 'Linux::Proc::Cpuinfo',
+ 'VERSION_FROM' => 'lib/Linux/Proc/Cpuinfo.pm',
+ 'PREREQ_PM' => { 'Test::More' => 0 },
+ (
+ $] >= 5.005
+ ? (
+ 'ABSTRACT_FROM' => 'lib/Linux/Proc/Cpuinfo.pm',
+ 'AUTHOR' => 'Alan Haggai Alavi <alanhaggai@alanhaggai.org>'
+ )
+ : ()
+ ),
+ 'LIBS' => ['-lproccpuinfo '],
+ 'DEFINE' => '',
+ 'INC' => '-I.',
+ 'LICENSE' => 'perl',
+ 'META_ADD' => {
+ 'resources' => {
+ 'repository' => {
+ 'type' => 'git',
+ 'url' => 'git://github.com/alanhaggai/Linux-Proc-Cpuinfo.git',
+ 'web' => 'http://github.com/alanhaggai/Linux-Proc-Cpuinfo',
+ }
+ },
+ },
+);
92 README
@@ -0,0 +1,92 @@
+NAME
+ Linux::Proc::Cpuinfo - XS wrapper for libproccpuinfo - a generic parser
+ for /proc/cpuinfo
+
+SYNOPSIS
+ use Linux::Proc::Cpuinfo;
+
+ my $info = Linux::Proc::Cpuinfo->new;
+ if ( defined $info ) {
+ print "Architecture:\t\t", $info->architecture, "\n";
+ print "Hardware Platform:\t", $info->hardware_platform, "\n";
+ print "Frequency:\t\t", $info->frequency, "\n";
+ print "Bogomips:\t\t", $info->bogomips, "\n";
+ print "Cache:\t\t\t", $info->cache, "\n";
+ print "CPUs:\t\t\t", $info->cpus, "\n";
+ }
+
+DESCRIPTION
+ Linux::Proc::Cpuinfo is a XS wrapper for "libproccpuinfo"
+ (<https://savannah.nongnu.org/projects/proccpuinfo/>). It provides a
+ generic interface to access "/proc/cpuinfo".
+
+METHODS
+ "new" or "new('filename')"
+ Returns a new Linux::Proc::Cpuinfo object. Without any argument, parses
+ "/proc/cpuinfo". If "filename" is passed, then the file with that name
+ is parsed.
+
+ On error, returns "undef".
+
+ "architecture"
+ Returns CPU architecture. If "/proc/cpuinfo" file does not list these
+ values or if the library fails to recognise the architecture, then the
+ value will be set to "undef".
+
+ "hardware_platform"
+ Returns hardware platform. If "/proc/cpuinfo" file does not list these
+ values or if the library fails to recognise the hardware platform, then
+ the value will be set to "undef".
+
+ "frequency"
+ Returns the CPU clock speed in MHz. If the "/proc/cpuinfo" file does not
+ list the clock speed or if the library fails to recognise the clock
+ speed, then this value defaults to 0.
+
+ "bogomips"
+ Returns the BogoMips as calculated by the kernel. BogoMips defaults to 0
+ if the "/proc/cpuinfo" file does not list the BogoMips or if the library
+ fails to recognise the BogoMips.
+
+ "cache"
+ Returns the amount of L2 cache in kilobytes. If the "/proc/cpuinfo" file
+ does not list the amount of L2 cache or if the library fails to
+ recognise the amount of L2 cache, then the value defaults to 0.
+
+ "cpus"
+ Returns the total number of processors detected. On systems that list
+ the number of detected processors and the number of active/enabled
+ processors, the number of detected processors is used. If the
+ "/proc/cpuinfo" file does not list the number of processors or if the
+ library fails to determine the number of processors, then the value
+ defaults to 1 since all running computers have at least 1 processor.
+
+INSTALLING "libproccpuinfo"
+ * Gentoo Linux
+
+ # emerge libproccpuinfo
+
+ * Generic Linux
+
+ Download the latest archive (named similar to
+ "libproccpuinfo-x.x.x.tar.bz2") from
+ <http://download.savannah.gnu.org/releases/proccpuinfo/> and extract
+ it.
+
+ $ cmake -D CMAKE_INSTALL_PREFIX=/usr .
+ $ make
+ $ make test
+ # make install
+
+ Please send me information on how to install it on other systems and
+ I will update it here.
+
+AUTHOR
+ Alan Haggai Alavi <alanhaggai@alanhaggai.org>
+
+COPYRIGHT AND LICENSE
+ This software is copyright (c) 2011 by Alan Haggai Alavi.
+
+ This is free software; you can redistribute it and/or modify it under
+ the same terms as the Perl 5 programming language system itself.
+
128 lib/Linux/Proc/Cpuinfo.pm
@@ -0,0 +1,128 @@
+package Linux::Proc::Cpuinfo;
+
+use 5.006000;
+use strict;
+use warnings;
+
+our $VERSION = '0.01';
+
+require XSLoader;
+XSLoader::load( 'Linux::Proc::Cpuinfo', $VERSION );
+
+sub DESTROY {
+ my $self = shift;
+ $self->destroy;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Linux::Proc::Cpuinfo - XS wrapper for libproccpuinfo - a generic parser for /proc/cpuinfo
+
+=head1 SYNOPSIS
+
+ use Linux::Proc::Cpuinfo;
+
+ my $info = Linux::Proc::Cpuinfo->new;
+ if ( defined $info ) {
+ print "Architecture:\t\t", $info->architecture, "\n";
+ print "Hardware Platform:\t", $info->hardware_platform, "\n";
+ print "Frequency:\t\t", $info->frequency, "\n";
+ print "Bogomips:\t\t", $info->bogomips, "\n";
+ print "Cache:\t\t\t", $info->cache, "\n";
+ print "CPUs:\t\t\t", $info->cpus, "\n";
+ }
+
+=head1 DESCRIPTION
+
+L<Linux::Proc::Cpuinfo> is a XS wrapper for C<libproccpuinfo>
+(L<https://savannah.nongnu.org/projects/proccpuinfo/>). It provides a generic
+interface to access C</proc/cpuinfo>.
+
+=head1 METHODS
+
+=head2 C<new> or C<new('filename')>
+
+Returns a new L<Linux::Proc::Cpuinfo> object. Without any argument, parses
+C</proc/cpuinfo>. If C<filename> is passed, then the file with that name is
+parsed.
+
+On error, returns C<undef>.
+
+=head2 C<architecture>
+
+Returns CPU architecture. If C</proc/cpuinfo> file does not list these values or
+if the library fails to recognise the architecture, then the value will be set
+to C<undef>.
+
+=head2 C<hardware_platform>
+
+Returns hardware platform. If C</proc/cpuinfo> file does not list these values
+or if the library fails to recognise the hardware platform, then the value will
+be set to C<undef>.
+
+=head2 C<frequency>
+
+Returns the CPU clock speed in MHz. If the C</proc/cpuinfo> file does not list
+the clock speed or if the library fails to recognise the clock speed, then this
+value defaults to C<0>.
+
+=head2 C<bogomips>
+
+Returns the BogoMips as calculated by the kernel. BogoMips defaults to C<0> if
+the C</proc/cpuinfo> file does not list the BogoMips or if the library fails to
+recognise the BogoMips.
+
+=head2 C<cache>
+
+Returns the amount of L2 cache in kilobytes. If the C</proc/cpuinfo> file does
+not list the amount of L2 cache or if the library fails to recognise the amount
+of L2 cache, then the value defaults to C<0>.
+
+=head2 C<cpus>
+
+Returns the total number of processors detected. On systems that list the number
+of detected processors and the number of active/enabled processors, the number
+of detected processors is used. If the C</proc/cpuinfo> file does not list the
+number of processors or if the library fails to determine the number of
+processors, then the value defaults to C<1> since all running computers have at
+least 1 processor.
+
+=head1 INSTALLING C<libproccpuinfo>
+
+=over 4
+
+=item * Gentoo Linux
+
+ # emerge libproccpuinfo
+
+=item * Generic Linux
+
+Download the latest archive (named similar to C<libproccpuinfo-x.x.x.tar.bz2>)
+from L<http://download.savannah.gnu.org/releases/proccpuinfo/> and extract it.
+
+ $ cmake -D CMAKE_INSTALL_PREFIX=/usr .
+ $ make
+ $ make test
+ # make install
+
+Please send me information on how to install it on other systems and I will
+update it here.
+
+=back
+
+=head1 AUTHOR
+
+Alan Haggai Alavi <alanhaggai@alanhaggai.org>
+
+=head1 COPYRIGHT AND LICENSE
+
+This software is copyright (c) 2011 by Alan Haggai Alavi.
+
+This is free software; you can redistribute it and/or modify it under the same
+terms as the Perl 5 programming language system itself.
+
+=cut
7,063 ppport.h
7,063 additions, 0 deletions not shown
30 t/Linux-Proc-Cpuinfo.t
@@ -0,0 +1,30 @@
+use strict;
+use warnings;
+
+use File::Spec;
+use FindBin;
+use Test::More 'tests' => 15;
+
+use_ok('Linux::Proc::Cpuinfo');
+
+my $info = Linux::Proc::Cpuinfo->new;
+isa_ok( $info, 'Linux::Proc::Cpuinfo' );
+
+can_ok( $info, 'architecture' );
+can_ok( $info, 'hardware_platform' );
+can_ok( $info, 'frequency' );
+can_ok( $info, 'bogomips' );
+can_ok( $info, 'cache' );
+can_ok( $info, 'cpus' );
+
+my $filename = File::Spec->catfile( $FindBin::RealBin, 'cpuinfo' );
+$info = Linux::Proc::Cpuinfo->new($filename);
+isa_ok( $info, 'Linux::Proc::Cpuinfo' );
+
+is( $info->architecture, 'Intel(R) Core(TM) i5 CPU M 480 @ 2.67GHz',
+ 'architecture' );
+is( $info->hardware_platform, 'GenuineIntel', 'hardware_platform' );
+is( $info->frequency, 1197, 'frequency' );
+is( $info->bogomips, 5319.63, 'bogomips' );
+is( $info->cache, 3072, 'cache' );
+is( $info->cpus, 4, 'cpus' );
100 t/cpuinfo
@@ -0,0 +1,100 @@
+processor : 0
+vendor_id : GenuineIntel
+cpu family : 6
+model : 37
+model name : Intel(R) Core(TM) i5 CPU M 480 @ 2.67GHz
+stepping : 5
+cpu MHz : 1197.000
+cache size : 3072 KB
+physical id : 0
+siblings : 4
+core id : 0
+cpu cores : 2
+apicid : 0
+initial apicid : 0
+fpu : yes
+fpu_exception : yes
+cpuid level : 11
+wp : yes
+flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 popcnt lahf_lm ida arat dts tpr_shadow vnmi flexpriority ept vpid
+bogomips : 5319.63
+clflush size : 64
+cache_alignment : 64
+address sizes : 36 bits physical, 48 bits virtual
+power management:
+
+processor : 1
+vendor_id : GenuineIntel
+cpu family : 6
+model : 37
+model name : Intel(R) Core(TM) i5 CPU M 480 @ 2.67GHz
+stepping : 5
+cpu MHz : 1197.000
+cache size : 3072 KB
+physical id : 0
+siblings : 4
+core id : 2
+cpu cores : 2
+apicid : 4
+initial apicid : 4
+fpu : yes
+fpu_exception : yes
+cpuid level : 11
+wp : yes
+flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 popcnt lahf_lm ida arat dts tpr_shadow vnmi flexpriority ept vpid
+bogomips : 5319.96
+clflush size : 64
+cache_alignment : 64
+address sizes : 36 bits physical, 48 bits virtual
+power management:
+
+processor : 2
+vendor_id : GenuineIntel
+cpu family : 6
+model : 37
+model name : Intel(R) Core(TM) i5 CPU M 480 @ 2.67GHz
+stepping : 5
+cpu MHz : 1197.000
+cache size : 3072 KB
+physical id : 0
+siblings : 4
+core id : 0
+cpu cores : 2
+apicid : 1
+initial apicid : 1
+fpu : yes
+fpu_exception : yes
+cpuid level : 11
+wp : yes
+flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 popcnt lahf_lm ida arat dts tpr_shadow vnmi flexpriority ept vpid
+bogomips : 5319.96
+clflush size : 64
+cache_alignment : 64
+address sizes : 36 bits physical, 48 bits virtual
+power management:
+
+processor : 3
+vendor_id : GenuineIntel
+cpu family : 6
+model : 37
+model name : Intel(R) Core(TM) i5 CPU M 480 @ 2.67GHz
+stepping : 5
+cpu MHz : 1197.000
+cache size : 3072 KB
+physical id : 0
+siblings : 4
+core id : 2
+cpu cores : 2
+apicid : 5
+initial apicid : 5
+fpu : yes
+fpu_exception : yes
+cpuid level : 11
+wp : yes
+flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 popcnt lahf_lm ida arat dts tpr_shadow vnmi flexpriority ept vpid
+bogomips : 5319.97
+clflush size : 64
+cache_alignment : 64
+address sizes : 36 bits physical, 48 bits virtual
+power management:
+
1  typemap
@@ -0,0 +1 @@
+Linux::Proc::Cpuinfo T_PTROBJ
Please sign in to comment.
Something went wrong with that request. Please try again.