OHAI-431: provide basic memory information for Mac OS X #119

Closed
wants to merge 6 commits into
from
@@ -0,0 +1,54 @@
+#
+# Author:: Patrick Collins (<pat@burned.com>)
+# Copyright:: Copyright (c) 2013 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+provides 'memory'
+
+memory Mash.new
+
+installed_memory = from("sysctl -n hw.memsize").to_i / 1024 / 1024.0
+memory[:total] = "#{installed_memory.to_i}MB"
+
+total_consumed = 0
+active = 0
+inactive = 0
+vm_stat = from("vm_stat")
+page_size = begin; /page size of (\d+) bytes/.match(vm_stat)[1].to_i; rescue; 4096; end
@btm

btm Jun 19, 2013

Owner

What's the rescue block here for? When would you hit it? Why would we still default to 4096?

@patcoll

patcoll Jun 19, 2013

Contributor

Relying on parsing stdout is inherently risky, so it's parsing the page size if it can find it, otherwise relying on a reasonable default. A lot of non-ruby implementations I've seen simply hard-code the page size, but that's not very future-proof.

This page unequivocally says the page size in iOS and OS X is 4 kilobytes, so it is a reasonable default:

In both OS X and iOS, the size of a page is 4 kilobytes.
+vm_stat.split("\n").each do |line|
+ ['wired down', 'active', 'inactive'].each do |match|
+ unless line.index("Pages #{match}:").nil? or page_size.nil?
+ pages = line.split.last.to_i
+ megabyte_val = (pages * page_size) / 1024 / 1024.0
+ total_consumed += megabyte_val
+ case match
+ when 'wired down'
+ active += megabyte_val.to_i
+ when 'active'
+ active += megabyte_val.to_i
+ when 'inactive'
+ inactive += megabyte_val.to_i
+ end
+ end
+ end
+end
+
+memory[:active] = "#{active}MB" if active > 0
+memory[:inactive] = "#{inactive}MB" if inactive > 0
+
+free_memory = installed_memory - total_consumed
+memory[:free] = "#{free_memory.to_i}MB" if total_consumed > 0
+
@@ -0,0 +1,67 @@
+#
+# Author:: Patrick Collins (<pat@burned.com>)
+# Copyright:: Copyright (c) 2013 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper.rb')
+
+describe Ohai::System, "Darwin Memory Plugin" do
+ before do
+ darwin_memsize = <<-DARWIN_MEMSIZE
+17179869184
+ DARWIN_MEMSIZE
+ darwin_vm_stat = <<-DARWIN_VM_STAT
+Mach Virtual Memory Statistics: (page size of 4096 bytes)
+Pages free: 2155305.
+Pages active: 924164.
+Pages inactive: 189127.
+Pages speculative: 531321.
+Pages wired down: 391749.
+"Translation faults": 14107520.
+Pages copy-on-write: 810071.
+Pages zero filled: 6981505.
+Pages reactivated: 1397.
+Pageins: 630064.
+Pageouts: 0.
+Object cache: 12 hits of 139872 lookups (0% hit rate)
+ DARWIN_VM_STAT
+
+ @ohai = Ohai::System.new
+ @ohai.stub!(:require_plugin).and_return(true)
+
+ @ohai.stub(:from).with("sysctl -n hw.memsize").and_return(darwin_memsize)
+ @ohai.stub(:from).with("vm_stat").and_return(darwin_vm_stat)
+
+ @ohai._require_plugin("memory")
+ end
+
+ describe "gathering memory info" do
+ before do
+ @ohai._require_plugin("darwin::memory")
+ end
+
+ it "completes the run" do
+ @ohai['memory'].should_not be_nil
+ end
+
+ it "detects the correct memory levels" do
+ @ohai['memory']['total'].should == '16384MB'
+ @ohai['memory']['active'].should == '5140MB'
+ @ohai['memory']['inactive'].should == '738MB'
+ @ohai['memory']['free'].should == '10504MB'
+ end
+ end
+end