File Coverage

blib/lib/Ixchel/functions/cpu_count.pm
Criterion Covered Total %
statement 14 27 51.8
branch 0 4 0.0
condition n/a
subroutine 5 6 83.3
pod 1 1 100.0
total 20 38 52.6


line stmt bran cond sub pod time code
1             package Ixchel::functions::cpu_count;
2              
3 1     1   102666 use 5.006;
  1         3  
4 1     1   5 use strict;
  1         2  
  1         31  
5 1     1   6 use warnings;
  1         1  
  1         54  
6 1     1   625 use File::Slurp;
  1         46826  
  1         96  
7 1     1   8 use Exporter 'import';
  1         2  
  1         284  
8             our @EXPORT = qw(cpu_count);
9              
10             =head1 NAME
11              
12             Ixchel::functions::cpu_count - Gets a count of processors
13              
14             =head1 VERSION
15              
16             Version 0.0.1
17              
18             =cut
19              
20             our $VERSION = '0.0.1';
21              
22             =head1 SYNOPSIS
23              
24             use Ixchel::functions::cpu_count;
25              
26             print 'CPU count: '.cpu_count."\n";
27              
28             =head1 Functions
29              
30             =head2 cpu_count
31              
32             Returns CPU count starting from 1.
33              
34             Supported OSes...
35              
36             FreeBSD
37             Linux
38              
39             =cut
40              
41             sub cpu_count {
42 0     0 1   my $count;
43              
44 0 0         if ( $^O eq 'freebsd' ) {
    0          
45 0           my $output=`/sbin/sysctl -n kern.smp.cpus`;
46 0           chomp($output);
47 0           $count=$output;
48             } elsif ( $^O eq 'linux' ) {
49 0           eval{
50 0           my $proc_info=read_file('/proc/cpuinfo');
51 0           my @proc_split=split(/\n/, $proc_info);
52 0           my @procs=grep(/^processor.*\:.*\d/, @proc_split);
53 0           $count=$#procs;
54              
55             # arrays index from zero, so add one to it
56 0           $count++;
57             };
58             }else {
59 0           die('"'.$^O.'" is not a supported OS as of currently');
60             }
61              
62 0           return $count;
63             }
64              
65             1; # End of Ixchel::functions::cpu_count