| 1 | package System::CPU; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use List::Util qw(sum); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||
| 9 | 2 | our $VERSION = '1.03'; | |||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | System::CPU - Cross-platform CPU information / topology | ||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||
| 17 | use System::CPU; | ||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | # Number of logical cores. E.g. on SMT systems these will be Hyper-Threads | ||||||||||||||||||||||||||||||||||||||
| 20 | my $logical_cpu = System::CPU::get_ncpu(); | ||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||
| 22 | # On some platforms you can also get the number of processors and physical cores | ||||||||||||||||||||||||||||||||||||||
| 23 | my ($phys_processors, $phys_cpu, $logical_cpu) = System::CPU::get_cpu(); | ||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | # Model name of the CPU | ||||||||||||||||||||||||||||||||||||||
| 26 | my $name = System::CPU::get_name(); | ||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||
| 28 | # CPU Architecture | ||||||||||||||||||||||||||||||||||||||
| 29 | my $arch = System::CPU::get_arch(); | ||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||
| 31 | # Get all the above in a hash | ||||||||||||||||||||||||||||||||||||||
| 32 | my $hash = System::CPU::get_hash(); | ||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | A pure Perl module with no dependencies to get basic CPU information on any platform. | ||||||||||||||||||||||||||||||||||||||
| 37 | The data you can get differs depending on platform, but for many systems running | ||||||||||||||||||||||||||||||||||||||
| 38 | Linux/BSD/MacOS you can get extra nuance like number of threads vs cores etc. | ||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||
| 40 | It was created for L<Benchmark::DKbench> with the C<get_ncpu> function modeled | ||||||||||||||||||||||||||||||||||||||
| 41 | after the one on L<MCE::Util>. In fact, some code was copied from that function as | ||||||||||||||||||||||||||||||||||||||
| 42 | it had the most reliable way to consistently get the logical cpus of the system. | ||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||
| 44 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||
| 46 | =head2 get_cpu | ||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||
| 48 | Returns as detailed CPU topology as the platform allows. A list of three values | ||||||||||||||||||||||||||||||||||||||
| 49 | will be returned, with the first and the second possibly C<undef>: | ||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | my ($phys_processors, $phys_cpu, $logical_cpu) = System::CPU::get_cpu(); | ||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||
| 53 | For many Linux, MacOS, BSD systems the number of physical processors (sockets), | ||||||||||||||||||||||||||||||||||||||
| 54 | as well as the number of physical CPU cores and logical CPUs (CPU threads) will | ||||||||||||||||||||||||||||||||||||||
| 55 | be returned. | ||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | For the systems where the extra information is not available (i.e. all other OSes | ||||||||||||||||||||||||||||||||||||||
| 58 | and some Linux/MacOS/BSD setups), the first two values will be C<undef>. | ||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | =head2 get_ncpu | ||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||
| 62 | my $logical_cpus = System::CPU::get_ncpu(); | ||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||
| 64 | This function behaves very similar to C<MCE::Util::get_ncpu> - in fact code is borrowed | ||||||||||||||||||||||||||||||||||||||
| 65 | from it. The number of logical CPUs will be returned, this is the number of hyper-threads | ||||||||||||||||||||||||||||||||||||||
| 66 | for SMT systems and the number of cores for most others. | ||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||
| 68 | =head2 get_name | ||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||
| 70 | my $cpu_name = System::CPU::get_name(raw => $raw?); | ||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||
| 72 | Returns the CPU model name. By default it will remove some extra spaces and Intel's | ||||||||||||||||||||||||||||||||||||||
| 73 | (TM) and (R), but you can pass in the C<raw> argument to avoid this cleanup. | ||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||
| 75 | =head2 get_arch | ||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | my $arch = System::CPU::get_arch(); | ||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | Will return the CPU architecture as reported by the system. There is no standarized | ||||||||||||||||||||||||||||||||||||||
| 80 | form, e.g. Linux will report aarch64 on a system where Darwin would report arm64 | ||||||||||||||||||||||||||||||||||||||
| 81 | etc. | ||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||
| 83 | =head2 get_hash | ||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | my $hash = System::CPU::get_hash(%opt?); | ||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||
| 87 | Will return all the information the module can access in a hash. Accepts the options | ||||||||||||||||||||||||||||||||||||||
| 88 | of the other functions. Example hash output: | ||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||||||||||||
| 91 | arch => 'arm64', | ||||||||||||||||||||||||||||||||||||||
| 92 | logical_cores => 10, | ||||||||||||||||||||||||||||||||||||||
| 93 | name => 'Apple M2 Pro', | ||||||||||||||||||||||||||||||||||||||
| 94 | physical_cores => 10, | ||||||||||||||||||||||||||||||||||||||
| 95 | processors => 1 | ||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||
| 98 | =head1 CAVEATS | ||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | Since text output from user commands is parsed for most platforms, only the English | ||||||||||||||||||||||||||||||||||||||
| 101 | language locales are supported. | ||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | =head1 NOTES | ||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | I did try to use existing solutions before writing my own. L<Sys::Info> has issues | ||||||||||||||||||||||||||||||||||||||
| 106 | installing on modern Linux systems (I tried submitting a PR, but the author seems | ||||||||||||||||||||||||||||||||||||||
| 107 | unresponsive). | ||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | L<System::Info> is the most promising, however, it returns a simple "core" count which | ||||||||||||||||||||||||||||||||||||||
| 110 | seems to inconsistently be either physical cores or threads depending on the platform. | ||||||||||||||||||||||||||||||||||||||
| 111 | The author got back to me, so I will try to sort that out, as that module is more | ||||||||||||||||||||||||||||||||||||||
| 112 | generic than System::CPU. | ||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||
| 114 | There are also several platform-specific modules, most requiring a compiler too | ||||||||||||||||||||||||||||||||||||||
| 115 | (e.g. L<Unix::Processors>, L<Sys::Info>, various C<*::Sysinfo>). | ||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | In the end, I wanted to get the CPU topology where possible - number of processors/sockets, | ||||||||||||||||||||||||||||||||||||||
| 118 | cores, threads separately, something that wasn't readily available. | ||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||
| 120 | I intend to support all systems possible with this simple pure Perl module. If you | ||||||||||||||||||||||||||||||||||||||
| 121 | have access to a system that is not supported or where the module cannot currently | ||||||||||||||||||||||||||||||||||||||
| 122 | give you the correct output, feel free to contact me about extending support. | ||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||
| 124 | Currently supported systems: | ||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | Linux/Android, BSD/MacOS, Win32/Cygwin, AIX, Solaris, IRIX, HP-UX, Haiku, GNU | ||||||||||||||||||||||||||||||||||||||
| 127 | and variants of those. | ||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||
| 131 | 1 | ▶ | sub get_hash { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 132 | 1 | my %opt = @_; | |||||||||||||||||||||||||||||||||||||
| 133 | 1 | my ($proc, $phys, $log) = get_cpu(%opt); | |||||||||||||||||||||||||||||||||||||
| 134 | 1 | my $name = get_name(%opt); | |||||||||||||||||||||||||||||||||||||
| 135 | 1 | my $arch = get_arch(%opt); | |||||||||||||||||||||||||||||||||||||
| 136 | return { | ||||||||||||||||||||||||||||||||||||||
| 137 | 1 | processors => $proc, | |||||||||||||||||||||||||||||||||||||
| 138 | logical_cores => $log, | ||||||||||||||||||||||||||||||||||||||
| 139 | physical_cores => $phys, | ||||||||||||||||||||||||||||||||||||||
| 140 | name => $name, | ||||||||||||||||||||||||||||||||||||||
| 141 | arch => $arch, | ||||||||||||||||||||||||||||||||||||||
| 142 | }; | ||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||
| 145 | 53 | ▶ | sub get_cpu { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_cpu: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 146 | 53 | ▶ | return _linux_cpu() if $^O =~ /linux|android/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 147 | 44 | ▶ | return _bsd_cpu() if $^O =~ /bsd|darwin|dragonfly/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 148 | 37 | ▶ | return _solaris_cpu() if $^O =~ /osf|solaris|sunos|svr5|sco/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 149 | 31 | ▶ | return _aix_cpu() if $^O =~ /aix/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 150 | 21 | ▶ | return _gnu_cpu() if $^O =~ /gnu/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 151 | 17 | ▶ | return _haiku_cpu() if $^O =~ /haiku/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 152 | 13 | ▶ | return _hpux_cpu() if $^O =~ /hp-?ux/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 153 | 9 | ▶ | return _irix_cpu() if $^O =~ /irix/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 154 | return (undef, undef, $ENV{NUMBER_OF_PROCESSORS}) | ||||||||||||||||||||||||||||||||||||||
| 155 | 5 | ▶ | if $^O =~ /mswin|mingw|msys|cygwin/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||
| 157 | 1 | die "OS identifier '$^O' not recognized. Contact dkechag\@cpan.org to add support."; | |||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||
| 160 | 25 | ▶ | sub get_ncpu { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_ncpu: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 161 | 25 | my $ncpu = get_cpu(); | |||||||||||||||||||||||||||||||||||||
| 162 | 25 | return $ncpu; | |||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||
| 165 | 19 | ▶ | sub get_name { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_name: called CC=21 SCAR=30.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 166 | 19 | my %opt = @_; | |||||||||||||||||||||||||||||||||||||
| 167 | 19 | my $name; | |||||||||||||||||||||||||||||||||||||
| 168 | 19 | ▶ | if ($^O =~ /linux|android/i) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||
| 169 | 4 | ($name) = _proc_cpuinfo(); | |||||||||||||||||||||||||||||||||||||
| 170 | } elsif ($^O =~ /bsd|darwin|dragonfly/i) { | ||||||||||||||||||||||||||||||||||||||
| 171 | 4 | chomp($name = `sysctl -n machdep.cpu.brand_string 2>/dev/null`); | |||||||||||||||||||||||||||||||||||||
| 172 | 4 | ▶ | chomp($name = `sysctl -n hw.model 2>/dev/null`) unless $name; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 173 | } elsif ($^O =~ /mswin|mingw|msys|cygwin/i) { | ||||||||||||||||||||||||||||||||||||||
| 174 | 1 | $name = $ENV{PROCESSOR_IDENTIFIER}; | |||||||||||||||||||||||||||||||||||||
| 175 | } elsif ($^O =~ /aix/i) { | ||||||||||||||||||||||||||||||||||||||
| 176 | 2 | chomp(my $out = `prtconf | grep -i "Processor Type" 2>/dev/null`); | |||||||||||||||||||||||||||||||||||||
| 177 | 2 | ▶ | $name = $1 if $out =~ /:\s*(.*)/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 178 | } elsif ($^O =~ /irix/i) { | ||||||||||||||||||||||||||||||||||||||
| 179 | 2 | my @out = grep {/CPU:/i} `hinv 2>/dev/null`; | |||||||||||||||||||||||||||||||||||||
| 180 | 2 | ▶ | $name = $1 if @out && $out[0] =~ /CPU:\s*(.*)/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@out and $out[0] =~ /CPU:\s*(.*)/i
MC/DC50%1/2
@out and $out[0] =~ /CPU:\s*(.*)/i
@out
$out[0] =~ /CPU:\s*(.*)/i
Truth table66%2/3
A and B
A= @outB= $out[0] =~ /CPU:\s*(.*)/i
| |||||||||||||||||||||||||||||||||||||||
| 181 | } elsif ($^O =~ /haiku/i) { | ||||||||||||||||||||||||||||||||||||||
| 182 | 2 | my $out = `sysinfo -cpu 2>/dev/null | grep "^CPU #"`; | |||||||||||||||||||||||||||||||||||||
| 183 | 2 | ▶ | $name = $1 if $out =~ /:\s*(?:")?(.*?)(?:")?\s*$/m; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 184 | } elsif ($^O =~ /hp-?ux/i) { | ||||||||||||||||||||||||||||||||||||||
| 185 | 2 | my $out = `machinfo`; | |||||||||||||||||||||||||||||||||||||
| 186 | 2 | ▶ | if ($out =~ /processor model:\s*\d*\s*(.+?)$/im) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 187 | 1 | $name = $1; | |||||||||||||||||||||||||||||||||||||
| 188 | } elsif ($out =~ /\s*\d*\s*(.+(?:MHz|GHz).+)$/m) { | ||||||||||||||||||||||||||||||||||||||
| 189 | 1 | $name = $1; | |||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||
| 191 | } elsif ($^O =~ /osf|solaris|sunos|svr5|sco/i) { | ||||||||||||||||||||||||||||||||||||||
| 192 | 1 | my $out = `kstat -p cpu_info`; | |||||||||||||||||||||||||||||||||||||
| 193 | 1 | ▶ | $name = $1 if $out =~ /:brand\s*(.*)$/m; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 194 | } else { | ||||||||||||||||||||||||||||||||||||||
| 195 | 1 | die "OS identifier '$^O' not recognized. Contact dkechag\@cpan.org to add support."; | |||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||
| 198 | 18 | ▶ | unless ($opt{raw}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 199 | 17 | ▶ | $name =~ s/\s+/ /g if $name; # I don't like some systems giving excess whitespace. | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 200 | 17 | ▶ | $name =~ s/\((?:R|TM)\)//g if $name; # I don't like Intel's (R)s and (TM)s | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||
| 202 | 18 | ▶ | return $name || ""; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$name || ''
MC/DC100%1/1
$name || ''
$name
Truth table100%2/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||
| 206 | 7 | ▶ | sub get_arch { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get_arch: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 207 | 7 | ▶ | return _uname_m() if $^O =~ /linux|android|bsd|darwin|dragonfly|gnu|osf|solaris|sunos|svr5|sco|hp-?ux/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 208 | 4 | ▶ | return _uname_p() if $^O =~ /aix|irix/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 209 | 3 | ▶ | return _getarch() if $^O =~ /haiku/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 210 | 2 | ▶ | return $ENV{PROCESSOR_ARCHITECTURE} if $^O =~ /mswin|mingw|msys|cygwin/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||
| 212 | 1 | die "OS identifier '$^O' not recognized. Contact dkechag\@cpan.org to add support."; | |||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||
| 215 | 6 | ▶ | sub _solaris_cpu { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_solaris_cpu: called CC=4 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||
| 216 | 6 | my $ncpu; | |||||||||||||||||||||||||||||||||||||
| 217 | 6 | ▶ | if (-x '/usr/sbin/psrinfo') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 218 | 0 | my $count = grep {/on-?line/} `psrinfo 2>/dev/null`; | |||||||||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | $ncpu = $count if $count; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 220 | } else { | ||||||||||||||||||||||||||||||||||||||
| 221 | 6 | my @output = grep {/^NumCPU = \d+/} `uname -X 2>/dev/null`; | |||||||||||||||||||||||||||||||||||||
| 222 | 6 | ▶ | $ncpu = (split ' ', $output[0])[2] if @output; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||
| 224 | 6 | return (undef, undef, $ncpu); | |||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||
| 227 | 7 | ▶ | sub _bsd_cpu { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_bsd_cpu: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||
| 228 | 7 | my $prof = `system_profiler -detailLevel mini SPHardwareDataType SPSoftwareDataType 2>/dev/null`; | |||||||||||||||||||||||||||||||||||||
| 229 | 7 | ▶ | my $proc = $prof ? 1 : undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 230 | 7 | ▶ | $proc = $1 if $prof && $prof =~ /Number of (?:Processors|CPUs): (\d+)/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$prof and $prof =~ /Number of (?:Processors|CPUs): (\d+)/i
MC/DC100%2/2
$prof and $prof =~ /Number of (?:Processors|CPUs): (\d+)/i
$prof
$prof =~ /Number of (?:Processors|CPUs): (\d+)/i
Truth table100%3/3
A and B
A= $profB= $prof =~ /Number of (?:Processors|CPUs): (\d+)/i
| |||||||||||||||||||||||||||||||||||||||
| 231 | 7 | chomp(my $cpus = `sysctl -n hw.logicalcpu 2>/dev/null`); | |||||||||||||||||||||||||||||||||||||
| 232 | 7 | ▶ | chomp($cpus = `sysctl -n hw.ncpu 2>/dev/null`) unless $cpus; # Old system fallback | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 233 | 7 | ▶ | return ($proc, undef, undef) unless $cpus; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 234 | 5 | chomp(my $cores = `sysctl -n hw.physicalcpu 2>/dev/null`); | |||||||||||||||||||||||||||||||||||||
| 235 | 5 | ▶ | $cores ||= $cpus; | ||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$cores ||= $cpus
MC/DC0%0/2
$cores ||= $cpus
$cores
$cpus
Truth table66%2/3
A ||= B
A= $coresB= $cpus
| |||||||||||||||||||||||||||||||||||||||
| 236 | 5 | return ($proc, $cores, $cpus); | |||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||
| 239 | 9 | ▶ | sub _linux_cpu { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_linux_cpu: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 240 | 9 | my ($name, $phys, $cores, $cpus) = _proc_cpuinfo(); | |||||||||||||||||||||||||||||||||||||
| 241 | 9 | return $phys, $cores, $cpus; | |||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||
| 244 | 10 | ▶ | sub _aix_cpu { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_aix_cpu: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||
| 245 | 10 | my $ncpu; | |||||||||||||||||||||||||||||||||||||
| 246 | 10 | my @output = `lparstat -i 2>/dev/null | grep "^Online Virtual CPUs"`; | |||||||||||||||||||||||||||||||||||||
| 247 | 10 | ▶ | if (@output) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 248 | 4 | $output[0] =~ /(\d+)\n$/; | |||||||||||||||||||||||||||||||||||||
| 249 | 4 | ▶ | $ncpu = $1 if $1; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||
| 251 | 10 | ▶ | if (!$ncpu) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 252 | 8 | @output = `pmcycles -m 2>/dev/null`; | |||||||||||||||||||||||||||||||||||||
| 253 | 8 | ▶ | if (@output) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 254 | 4 | $ncpu = scalar @output; | |||||||||||||||||||||||||||||||||||||
| 255 | } else { | ||||||||||||||||||||||||||||||||||||||
| 256 | 4 | @output = `lsdev -Cc processor -S Available 2>/dev/null`; | |||||||||||||||||||||||||||||||||||||
| 257 | 4 | ▶ | $ncpu = scalar @output if @output; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||
| 260 | 10 | return (undef, undef, $ncpu); | |||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||
| 263 | 4 | ▶ | sub _haiku_cpu { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_haiku_cpu: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 264 | 4 | my $ncpu; | |||||||||||||||||||||||||||||||||||||
| 265 | 4 | my @output = `sysinfo -cpu 2>/dev/null | grep "^CPU #"`; | |||||||||||||||||||||||||||||||||||||
| 266 | 4 | ▶ | $ncpu = scalar @output if @output; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 267 | 4 | return (undef, undef, $ncpu);; | |||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||
| 270 | 4 | ▶ | sub _hpux_cpu { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_hpux_cpu: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 271 | 4 | my $ncpu = grep { /^processor/ } `ioscan -fkC processor 2>/dev/null`; | |||||||||||||||||||||||||||||||||||||
| 272 | 4 | ▶ | return (undef, undef, $ncpu || undef); | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ncpu || undef
MC/DC100%1/1
$ncpu || undef
$ncpu
Truth table100%2/2
A
A= $ncpu
| |||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||
| 275 | 4 | ▶ | sub _irix_cpu { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_irix_cpu: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 276 | 4 | my $ncpu; | |||||||||||||||||||||||||||||||||||||
| 277 | 4 | my @out = grep {/\s+processors?$/i} `hinv -c processor 2>/dev/null`; | |||||||||||||||||||||||||||||||||||||
| 278 | 4 | ▶ | $ncpu = (split ' ', $out[0])[0] if @out; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 279 | 4 | return (undef, undef, $ncpu);; | |||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||
| 282 | 4 | ▶ | sub _gnu_cpu { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_gnu_cpu: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 283 | 4 | my $ncpu; | |||||||||||||||||||||||||||||||||||||
| 284 | 4 | chomp(my @output = `nproc --all 2>/dev/null`); | |||||||||||||||||||||||||||||||||||||
| 285 | 4 | ▶ | $ncpu = $output[0] if @output; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 286 | 4 | return (undef, undef, $ncpu);; | |||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||
| 289 | 13 | ▶ | sub _proc_cpuinfo { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_proc_cpuinfo: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||||||||||||
| 290 | 13 | my (@physical, @cores, $phys, $cpus, $name); | |||||||||||||||||||||||||||||||||||||
| 291 | 13 | ▶ | if (-f '/proc/cpuinfo' && open my $fh, '<', '/proc/cpuinfo') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-f "/proc/cpuinfo" and open my $fh, "<", "/proc/cpuinfo"
MC/DC50%1/2
-f "/proc/cpuinfo" and open my $fh, "<", "/proc/cpuinfo"
-f "/proc/cpuinfo"
open my $fh, "<", "/proc/cpuinfo"
Truth table66%2/3
A and B
A= -f "/proc/cpuinfo"B= open my $fh, "<", "/proc/cpuinfo"
| |||||||||||||||||||||||||||||||||||||||
| 292 | 10 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 293 | 811 | ▶ | $cpus++ if /^processor\s*:/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 294 | 811 | ▶ | push @physical, $1 if /^physical id\s*:\s*(\d+)/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 295 | 811 | ▶ | push @cores, $1 if /^cpu cores\s*:\s*(\d+)/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 296 | 811 | ▶ | $name = $1 if /^model name\s*:\s*(.*)/i; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||
| 298 | 10 | ▶ | return $name, undef, $cores[0], $cpus if !@physical && @cores; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not @physical and @cores
MC/DC100%2/2
not @physical and @cores
not @physical
@cores
Truth table100%3/3
not(A) and B
A= not @physicalB= @cores
| |||||||||||||||||||||||||||||||||||||||
| 299 | 8 | ▶ | @cores = (0) unless @cores; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 300 | 8 | my %hash; | |||||||||||||||||||||||||||||||||||||
| 301 | $hash{$physical[$_]} = $_ < scalar(@cores) ? $cores[$_] : $cores[0] | ||||||||||||||||||||||||||||||||||||||
| 302 | 8 | ▶ | for 0 .. $#physical; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 303 | 8 | ▶ | my $phys = keys %hash || undef; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
keys %hash || undef
MC/DC100%1/1
keys %hash || undef
keys %hash
Truth table100%2/2
A
A= keys %hash
| |||||||||||||||||||||||||||||||||||||||
| 304 | 8 | ▶ | my $cores = sum(values %hash) || $cpus; | ||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
&sum(values %hash) || $cpus
MC/DC0%0/2
&sum(values %hash) || $cpus
&sum(values %hash)
$cpus
Truth table66%2/3
A || B
A= &sum(values %hash)B= $cpus
| |||||||||||||||||||||||||||||||||||||||
| 305 | 8 | return $name, $phys, $cores, $cpus; | |||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||
| 307 | 3 | return; | |||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||
| 310 | 5 | ▶ | sub _uname_m { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_uname_m: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 311 | 5 | chomp( my $arch = `uname -m 2>/dev/null` ); | |||||||||||||||||||||||||||||||||||||
| 312 | 5 | ▶ | return $arch || _uname_p(); | ||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$arch || _uname_p()
MC/DC100%2/2
$arch || _uname_p()
$arch
_uname_p()
Truth table100%3/3
A || B
A= $archB= _uname_p()
| |||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||
| 315 | 3 | ▶ | sub _uname_p { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_uname_p: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 316 | 3 | chomp( my $arch = `uname -p 2>/dev/null` ); | |||||||||||||||||||||||||||||||||||||
| 317 | 3 | return $arch; | |||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||
| 320 | 1 | ▶ | sub _getarch { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_getarch: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 321 | 1 | chomp( my $arch = `getarch 2>/dev/null` ); | |||||||||||||||||||||||||||||||||||||
| 322 | 1 | return $arch; | |||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||
| 325 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||
| 327 | Dimitrios Kechagias, C<< <dkechag at cpan.org> >> | ||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||
| 329 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||
| 331 | Please report any bugs or feature requests to L<https://github.com/dkechag/System-CPU/issues>. | ||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||
| 333 | You can also submit PRs with fixes/enhancements directly. | ||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||
| 335 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||
| 337 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||
| 339 | perldoc System::CPU | ||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||
| 341 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||
| 343 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||
| 345 | =item * GitHub | ||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||
| 347 | L<https://github.com/dkechag/System-CPU> | ||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||
| 349 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||
| 351 | L<https://metacpan.org/release/System-CPU> | ||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||
| 353 | =back | ||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||
| 355 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||
| 357 | Some code borrowed from L<MCE>. | ||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||
| 359 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||
| 361 | This software is copyright (c) 2023 by Dimitrios Kechagias. | ||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||
| 363 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||
| 364 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||
| 366 | =cut | ||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||
| 368 | 2 | 1; # End of System::CPU | |||||||||||||||||||||||||||||||||||||