From 288a377e710e8a40731ba3eb75f2d723c7438b38 Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Sun, 26 Apr 2015 09:50:50 -0700 Subject: [PATCH] Collect RAM and swap usage on macos --- proc/macos-lib.pl | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/proc/macos-lib.pl b/proc/macos-lib.pl index 333f5e4c3..dcef3886a 100755 --- a/proc/macos-lib.pl +++ b/proc/macos-lib.pl @@ -90,5 +90,45 @@ foreach $pty (@ptys) { return (); } +# get_memory_info() +# Returns a list containing the real mem, free real mem, swap and free swap, +# and possibly cached memory and the burstable limit. All of these are in Kb. +sub get_memory_info +{ +my @rv; + +# Get total memory +my $out = &backquote_command("sysctl -a hw.physmem 2>/dev/null"); +if ($out =~ /:\s*(\d+)/) { + $rv[0] = $1 / 1024; + } + +# Get memory usage +$out = &backquote_command("vm_stat 2>/dev/null"); +my %stat; +foreach my $l (split(/\r?\n/, $out)) { + if ($l =~ /^(.*):\s*(\d+)/) { + $stat{lc($1)} = $2; + } + } +my $usage = ($stat{'pages active'} + $stat{'pages wired down'}) * 4; +$rv[1] = $rv[0] - $usage; + +# Get swap usage +$out = &backquote_command("sysctl -a vm.swapusage 2>/dev/null"); +if ($out =~ /total\s*=\s*([0-9\.]+)([KMGT]).*free\s*=\s*([0-9\.]+)([KMGT])/) { + $rv[2] = $1*($2 eq "K" ? 1 : + $2 eq "M" ? 1024 : + $2 eq "G" ? 1024*1024 : + $2 eq "T" ? 1024*1024*1024 : 0); + $rv[3] = $3*($4 eq "K" ? 1 : + $4 eq "M" ? 1024 : + $4 eq "G" ? 1024*1024 : + $4 eq "T" ? 1024*1024*1024 : 0); + } + +return @rv; +} + 1;