Commits

Burak Gürsoy committed ed46dd1

Implement swap detection

Comments (0)

Files changed (1)

lib/Sys/Info/Driver/OSX/OS.pm

         qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
 };
 
+my %UNIT_TO_BYTES = (
+    G => 1024**3,
+    M => 1024**2,
+    K => 1024,
+);
+
 my %OSVERSION;
 
 my %FILE = (
     my $arch      = ($cpu->identify)[0]->{architecture};
     my $physmem   = fsysctl('hw.memsize'); # physmem
     my $usermem   = fsysctl('hw.usermem');
-
-    # XXX
-    my %swap;
-    @swap{ qw/ path size used / } = qw( TODO 0 0);
-    $swap{path} = undef;
-
+    my %swap      = $self->_probe_swap;
     my %info;
 
     # http://jaharmi.com/2007/05/11/read_the_mac_os_x_edition_and_version_from_prope
 
     $info{physical_memory_total}     = $physmem / 1024;
     $info{physical_memory_available} = ( $physmem - $usermem ) / 1024;
-    $info{page_file_total}           = $swap{size};
-    $info{page_file_available}       = $swap{size} - $swap{used};
+    $info{page_file_total}           = $swap{total};
+    $info{page_file_available}       = $swap{free};
 
     # windows specific
     $info{windows_dir}               = undef;
 
 # ------------------------[ P R I V A T E ]------------------------ #
 
+sub _probe_swap {
+    my($self) = @_;
+    # `vm_stat` ?
+    my $swapusage = fsysctl 'vm.swapusage';
+    my @sparts    = split m<\s{2,}>xms, $swapusage;
+    my $swap_enc  = $sparts[-1] =~ m{encrypted}xms ? pop @sparts : undef;
+    my %sw        = map { split m{ \s+ = \s+ }xms, $_ } @sparts;
+    my $size      = sub {
+        my($unit, $orig) = @_;
+        return $UNIT_TO_BYTES{ $unit }
+                || croak "Unable to determine bytes from $unit unit ($orig)"
+    };
+
+    foreach my $prop ( qw( free used total ) ) {
+        my $value = $sw{ $prop } || next;
+        my $unit  = chop $value;
+        $value += 0;
+        $sw{ $prop } = $value ? $value * $size->( $unit, $sw{ $prop } ) : 0;
+    }
+
+    return
+        %sw,
+        encrypted => $swap_enc ? 1 : 0,
+        path      => -d '/private/var/vm' ? '/private/var/vm' : undef,
+    ;
+}
+
 sub _install_date {
     my $self = shift;
     # I have no /var/log/OSInstall.custom on my system, so I believe that
 
 L<Sys::Info>, L<Sys::Info::OS>,
 L<http://en.wikipedia.org/wiki/Mac_OS_X>,
-L<http://stackoverflow.com/questions/3610424/determine-kernel-bitness-in-mac-os-x-10-6>.
+L<http://stackoverflow.com/questions/3610424/determine-kernel-bitness-in-mac-os-x-10-6>,
+L<http://osxdaily.com/2010/10/08/mac-virtual-memory-swap/>.
 
 =cut