Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit d0e1dff8361a6b88913aa65fe9a2d63d9fce8b36 Alan Haggai Alavi committed Aug 28, 2011
Showing with 7,563 additions and 0 deletions.
  1. +107 −0 Cpuinfo.xs
  2. +9 −0 MANIFEST
  3. +33 −0 Makefile.PL
  4. +92 −0 README
  5. +128 −0 lib/Linux/Proc/Cpuinfo.pm
  6. +7,063 −0 ppport.h
  7. +30 −0 t/Linux-Proc-Cpuinfo.t
  8. +100 −0 t/cpuinfo
  9. +1 −0 typemap
@@ -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
@@ -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
@@ -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.
+
@@ -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
Oops, something went wrong.

0 comments on commit d0e1dff

Please sign in to comment.