File Coverage

blib/lib/System/Info/Darwin.pm
Criterion Covered Total %
statement 51 53 96.2
branch 10 16 62.5
condition 5 12 41.6
subroutine 6 6 100.0
pod 1 1 100.0
total 73 88 82.9


line stmt bran cond sub pod time code
1             package System::Info::Darwin;
2              
3 3     3   591 use strict;
  3         5  
  3         91  
4 3     3   19 use warnings;
  3         6  
  3         85  
5              
6 3     3   15 use base "System::Info::BSD";
  3         7  
  3         2357  
7              
8             our $VERSION = "0.055";
9              
10             =head1 NAME
11              
12             System::Info::Darwin - Object for specific Darwin info.
13              
14             =head1 DESCRIPTION
15              
16             =head2 $si->prepare_sysinfo
17              
18             Use os-specific tools to find out more about the system.
19              
20             =cut
21              
22             sub prepare_sysinfo {
23 3     3 1 6 my $self = shift;
24 3         18 $self->System::Info::Base::prepare_sysinfo ();
25              
26 3         7 $self->{__os} .= " (Mac OS X)";
27              
28 3         7 my $scl = __get_sysctl ();
29              
30 3 50       21 my $system_profiler = __get_system_profiler () or
31             return $self->SUPER::prepare_sysinfo ();
32              
33 3         26 $self->{__system_profiler} = $system_profiler;
34 3 50       12 if (my $kv = $system_profiler->{"system version"}) {
35 0         0 $self->{__os} =~ s{\)$}{ - $kv)};
36             }
37              
38             my $model = $system_profiler->{"machine name"} ||
39 3   33     10 $system_profiler->{"machine model"};
40              
41             my $ncpu = # $scl->{"hw.ncpu"} ||
42 3         8 $system_profiler->{"number of cpus"};
43 3 100       13 $system_profiler->{"total number of cores"} and
44             $ncpu .= " [$system_profiler->{'total number of cores'} cores]";
45              
46             $self->{__cpu_type} = $system_profiler->{"cpu type"}
47 3 50       20 if $system_profiler->{"cpu type"};
48             $self->{__cpu} = # $scl->{"machdep.cpu.brand_string"} ||
49 3         14 "$model ($system_profiler->{'cpu speed'})";
50 3         21 $self->{__cpu_count} = $ncpu;
51             $scl->{"machdep.cpu.core_count"} and
52 3 50       12 $self->{_ncore} = $scl->{"machdep.cpu.core_count"};
53              
54 3   50     5 my $osv = do {
55             local $^W = 0;
56             `sw_vers -productVersion 2>/dev/null`;
57             } || "";
58 3         10935 chomp ($self->{__osvers} = $osv);
59              
60 3         46 $self->{__memsize} = $scl->{"hw.memsize"};
61              
62 3         1058 return $self;
63             } # prepare_sysinfo
64              
65             # System::Info::BSD.pm only uses hw
66             sub __get_sysctl {
67 3 50   3   100 my $sysctl_cmd = -x "/sbin/sysctl" ? "/sbin/sysctl" : "sysctl";
68 3         8 chomp (my @sysctl = do {
69 3         20 local $^W = 0;
70 3         25 `$sysctl_cmd -a 2>/dev/null`;
71             });
72 3         161097 my %sysctl = map { split m/\s*[:=]\s*/, $_, 2 } grep m/[:=]/ => @sysctl;
  3003         9036  
73 3         366 return \%sysctl;
74             } # __get_sysctl
75              
76             sub __get_system_profiler {
77 3 50   3   10 my $system_profiler_output = do {
78 3         32 local $^W = 0;
79 3         35 `/usr/sbin/system_profiler -detailLevel mini SPHardwareDataType SPSoftwareDataType 2>&1`;
80             } or return;
81              
82             # From RT#97441
83             # In Yosemite the system_profiler started emitting these warnings:
84             # 2015-07-24 06:54:06.842 system_profiler[59780:1318389] platformPluginDictionary: Can\'t get X86PlatformPlugin, return value 0
85             # They seem to be harmless, but annoying.
86             # Clean them out, but then warn about others from system_profiler.
87 3         99 $system_profiler_output =~ s/^\d{4}-\d\d-\d\d .+ system_profiler\[.+?\] platformPluginDictionary: Can't get X86PlatformPlugin, return value 0$//mg;
88 3         30 warn "Unexpected warning from system_profiler:\n$1\n"
89             while $system_profiler_output =~ /^(.+system_profiler.+)/mg;
90              
91 3         7 my %system_profiler;
92 3         237 $system_profiler{lc $1} = $2
93             while $system_profiler_output =~ m/^\s*([\w ]+):\s+(.+)$/gm;
94              
95             # convert newer output from Intel core duo
96 3         25 my %keymap = (
97             "processor name" => "cpu type",
98             "processor speed" => "cpu speed",
99             "model name" => "machine name",
100             "model identifier" => "machine model",
101             "number of processors" => "number of cpus",
102             "number of processors" => "number of cpus",
103             "total number of cores" => "total number of cores",
104             );
105 3         16 for my $newkey (keys %keymap) {
106 18         37 my $oldkey = $keymap{$newkey};
107             exists $system_profiler{$newkey} and
108 18 100       64 $system_profiler{$oldkey} = delete $system_profiler{$newkey};
109             }
110              
111 3   33     16 chomp ($system_profiler{"cpu type"} ||= `uname -m`);
112 3   50     10 $system_profiler{"cpu type"} ||= "Unknown";
113 3         26 $system_profiler{"cpu type"} =~ s/PowerPC\s*(\w+).*/macppc$1/;
114 3   50     17 $system_profiler{"cpu speed"} ||= 0; # Mac M1 does not show CPU speed
115 3         17 $system_profiler{"cpu speed"} =~
116 0         0 s/(0(?:\.\d+)?)\s*GHz/sprintf "%d MHz", $1 * 1000/e;
117              
118 3         31 return \%system_profiler;
119             } # __get_system_profiler
120              
121             1;
122              
123             __END__