lib/App/OS/Detect/MachineCores.pm

filter: Statementstmt 77.9% 46 / 59 Branchbran 30.0% 6 / 20 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 94.1% 16 / 17 Podpod n/a totaltotal 66.9% 71 / 106 CC 13 SCAR 20.7
CC
13
Cov
65%
_build_cores
25.6
_build_SC_NPROCESSORS_ONLN
6.0
__ANON__
4.1
CRAP 7.9
↑ Summary
1 package App::OS::Detect::MachineCores;
2  
3 # PODNAME: App::OS::Detect::MachineCores
4 # ABSTRACT: Detect how many cores your machine has (OS-independently)
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use MooX::Options skip_options => [qw<os cores>];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use POSIX qw/ sysconf /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use IPC::Open2;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 has os => (
17 is => 'ro',
18 required => 1,
19 2 ▶ default => sub { $^O },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 1 );
21  
22 has cores => (
23 is => 'rw',
24 2 ▶ isa => sub { die "$_[0] is not a reasonable number of cores!" unless $_[0] > 0 and $_[0] < 100 },
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] > 0 and $_[0] < 100 0 2 2
Condition33%1/3
$_[0] > 0 and $_[0] < 100
!ll&&!rl&&r
002
MC/DC0%0/2
$_[0] > 0 and $_[0] < 100
$_[0] > 0 $_[0] < 100
Truth table33%1/3
A and B
A= $_[0] > 0B= $_[0] < 100
ABresult
0X0
100
111
25 1 lazy => 1,
26 builder => '_build_cores',
27 );
28  
29 option add_one => (
30 is => 'rw',
31 2 ▶ isa => sub { die "Invalid bool!" unless $_[0] == 0 or $_[0] == 1 },
Subroutine __ANON__: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] == 0 or $_[0] == 1 0 2 2
Condition66%2/3
$_[0] == 0 or $_[0] == 1
l!l&&r!l&&!r
110
MC/DC0%0/2
$_[0] == 0 or $_[0] == 1
$_[0] == 0 $_[0] == 1
Truth table66%2/3
A or B
A= $_[0] == 0B= $_[0] == 1
ABresult
1X1
011
000
32 1 ▶ default => sub { '0' },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1 short => 'i',
34 doc => q{add one to the number of cores (useful in scripts)},
35 );
36  
37 has SC_NPROCESSORS_ONLN => (
38 is => 'ro',
39 2 ▶ default => sub { 84 },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 );
41  
42 0 ▶sub _build_SC_NPROCESSORS_ONLN {
Subroutine _build_SC_NPROCESSORS_ONLN: not called CC=2 SCAR=17.9
43 0 my ($self) = @_;
44 0 ▶ return ($self->os eq 'aix') ? 73 : 84;
Branch: 1 branch
BranchTrueFalseTotal
$self->os eq 'aix' ? : 0 0 0
45 }
46  
47 2 ▶sub _build_cores {
Subroutine _build_cores: called CC=7 SCAR=32.4
48 2 my ($self) = @_;
49 2 my $cores;
50  
51 2 ▶ if ($self->os =~ /aix|bsdos|darwin|dynixptx|freebsd|linux|hpux|irix|openbsd|solaris|sunos/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os =~ /aix|bsdos|darwin|dynixptx|freebsd|linux|hpux|irix|openbsd|solaris|sunos/) 2 0 2
52 2 $cores = sysconf $self->SC_NPROCESSORS_ONLN;
53 }
54  
55 2 ▶ if (!$cores) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cores) 0 2 2
56 0 my $cmd;
57  
58 0 ▶ if ($self->os eq 'linux') { $cmd = q<grep -c processor /proc/cpuinfo> }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "linux") 0 0 0
59 0 ▶ if ($self->os eq 'darwin') { $cmd = q<sysctl hw.ncpu | awk '{print $2}'> }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "darwin") 0 0 0
60 0 ▶ if ($self->os eq 'MSWin32') { $cmd = q<echo %NUMBER_OF_PROCESSORS%> }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->os eq "MSWin32") 0 0 0
61  
62 0 ▶ if ($cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd) { } 0 0 0
63 0 waitpid( open2(my $out, my $in, $cmd), 0);
64 0 $cores = <$out> + 0;
65 }
66 else {
67 0 carp("Can't detect the cores for your system/OS, sorry.");
68 }
69 }
70  
71 2 return $cores;
72 }
73  
74 2 ▶around cores => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
75 2 my ($orig, $self) = (shift, shift);
76  
77 2 ▶ return $self->$orig() + 1 if $self->add_one;
Branch: 1 branch
BranchTrueFalseTotal
if $self->add_one 1 1 2
78 1 return $self->$orig();
79 1 };
80  
81 1 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 1 1;
84  
85 =for Pod::Coverage os cores _build_cores add_one
86  
87 =begin wikidoc
88  
89 = SYNOPSIS
90  
91 On different system, different approaches are needed to detect the number of cores for that machine.
92  
93 This Module is a wrapper around these different approaches.
94  
95 = USAGE
96  
97 This module will install one executable, {mcores}, in your bin.
98  
99 It is really simple and straightforward:
100  
101 usage: mcores [-?i] [long options...]
102 -h --help Prints this usage information.
103 -i --add_one add one to the number of cores (useful in scripts)
104  
105 = SUPPORTED SYSTEMS
106  
107 * Linux
108 * OSX
109 * Windows
110 * aix
111 * bsdos
112 * darwin
113 * dynixptx
114 * freebsd
115 * linux
116 * hpux
117 * irix
118 * openbsd
119 * solaris
120 * sunos
121  
122 = MOTIVATION
123  
124 During development of dotfiles for different platforms I was searching for some way to be able to
125 transparantly detect the number of available cores and couldn't find one.
126 Also it is quite handy to be able to increment the number by simply using a little switch {-i}.
127  
128 Example:
129  
130 export TEST_JOBS=`mcores -i`
131  
132 =end wikidoc
↑ Summary