line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
1
|
|
17922
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
58
|
|
7
|
1
|
|
|
1
|
|
19
|
use 5.010; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
8
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
37
|
|
9
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
86
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
664
|
use Moo; |
|
1
|
|
|
|
|
14750
|
|
|
1
|
|
|
|
|
7
|
|
12
|
1
|
|
|
1
|
|
2249
|
use MooX::Options skip_options => [qw]; |
|
1
|
|
|
|
|
1200
|
|
|
1
|
|
|
|
|
6
|
|
13
|
1
|
|
|
1
|
|
222670
|
use POSIX qw/ sysconf /; |
|
1
|
|
|
|
|
3465
|
|
|
1
|
|
|
|
|
5
|
|
14
|
1
|
|
|
1
|
|
1238
|
use IPC::Open2; |
|
1
|
|
|
|
|
2774
|
|
|
1
|
|
|
|
|
384
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has os => ( |
17
|
|
|
|
|
|
|
is => 'ro', |
18
|
|
|
|
|
|
|
required => 1, |
19
|
|
|
|
|
|
|
default => sub { $^O }, |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has cores => ( |
23
|
|
|
|
|
|
|
is => 'rw', |
24
|
|
|
|
|
|
|
isa => sub { die "$_[0] is not a reasonable number of cores!" unless $_[0] > 0 and $_[0] < 100 }, |
25
|
|
|
|
|
|
|
lazy => 1, |
26
|
|
|
|
|
|
|
builder => '_build_cores', |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
option add_one => ( |
30
|
|
|
|
|
|
|
is => 'rw', |
31
|
|
|
|
|
|
|
isa => sub { die "Invalid bool!" unless $_[0] == 0 or $_[0] == 1 }, |
32
|
|
|
|
|
|
|
default => sub { '0' }, |
33
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
default => sub { 84 }, |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub _build_SC_NPROCESSORS_ONLN { |
43
|
0
|
|
|
0
|
|
0
|
my ($self) = @_; |
44
|
0
|
0
|
|
|
|
0
|
return ($self->os eq 'aix') ? 73 : 84; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _build_cores { |
48
|
2
|
|
|
2
|
|
30
|
my ($self) = @_; |
49
|
2
|
|
|
|
|
3
|
my $cores; |
50
|
|
|
|
|
|
|
|
51
|
2
|
50
|
|
|
|
16
|
if ($self->os =~ /aix|bsdos|darwin|dynixptx|freebsd|linux|hpux|irix|openbsd|solaris|sunos/) { |
52
|
2
|
|
|
|
|
48
|
$cores = sysconf $self->SC_NPROCESSORS_ONLN; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
2
|
50
|
|
|
|
5
|
if (!$cores) { |
56
|
0
|
|
|
|
|
0
|
my $cmd; |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
0
|
if ($self->os eq 'linux') { $cmd = q } |
|
0
|
|
|
|
|
0
|
|
59
|
0
|
0
|
|
|
|
0
|
if ($self->os eq 'darwin') { $cmd = q } |
|
0
|
|
|
|
|
0
|
|
60
|
0
|
0
|
|
|
|
0
|
if ($self->os eq 'MSWin32') { $cmd = q } |
|
0
|
|
|
|
|
0
|
|
61
|
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
0
|
if ($cmd) { |
63
|
0
|
|
|
|
|
0
|
waitpid( open2(my $out, my $in, $cmd), 0); |
64
|
0
|
|
|
|
|
0
|
$cores = <$out> + 0; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
else { |
67
|
0
|
|
|
|
|
0
|
carp("Can't detect the cores for your system/OS, sorry."); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
2
|
|
|
|
|
36
|
return $cores; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
around cores => sub { |
75
|
|
|
|
|
|
|
my ($orig, $self) = (shift, shift); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
return $self->$orig() + 1 if $self->add_one; |
78
|
|
|
|
|
|
|
return $self->$orig(); |
79
|
|
|
|
|
|
|
}; |
80
|
|
|
|
|
|
|
|
81
|
1
|
|
|
1
|
|
6
|
no Moo; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
6
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
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 |