diff options
author | Claire McQuin <mcquin@users.noreply.github.com> | 2014-06-19 11:42:15 -0700 |
---|---|---|
committer | Claire McQuin <mcquin@users.noreply.github.com> | 2014-06-19 11:42:15 -0700 |
commit | 4a1a4ddcbfda1a8d768508441c0283dfd5a1c59d (patch) | |
tree | 75d3e29d43a2b5a2b70226d388cbd605e7dfe48d | |
parent | 0b4d31d214e8d15860229d70b3b3d289e71170be (diff) | |
parent | d668cc64953c4b72dd8976028e308ce14ba5b9c2 (diff) | |
download | ohai-4a1a4ddcbfda1a8d768508441c0283dfd5a1c59d.tar.gz |
Merge pull request #331 from patcoll/OHAI-431
OHAI-431: provide basic memory information for Mac OS X
-rw-r--r-- | lib/ohai/plugins/darwin/memory.rb | 63 | ||||
-rw-r--r-- | spec/unit/plugins/darwin/memory_spec.rb | 64 |
2 files changed, 127 insertions, 0 deletions
diff --git a/lib/ohai/plugins/darwin/memory.rb b/lib/ohai/plugins/darwin/memory.rb new file mode 100644 index 00000000..62237a7d --- /dev/null +++ b/lib/ohai/plugins/darwin/memory.rb @@ -0,0 +1,63 @@ +# +# 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. +# + +Ohai.plugin(:Memory) do + provides 'memory' + + collect_data(:darwin) do + memory Mash.new + + installed_memory = shell_out("sysctl -n hw.memsize").stdout.to_i / 1024 / 1024.0 + memory[:total] = "#{installed_memory.to_i}MB" + + total_consumed = 0 + active = 0 + inactive = 0 + vm_stat = shell_out("vm_stat").stdout + vm_stat_match = /page size of (\d+) bytes/.match(vm_stat) + page_size = if vm_stat_match and vm_stat_match[1] + vm_stat_match[1].to_i + else + 4096 + end + vm_stat.split("\n").each do |line| + ['wired down', 'active', 'inactive'].each do |match| + unless line.index("Pages #{match}:").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 + end +end + diff --git a/spec/unit/plugins/darwin/memory_spec.rb b/spec/unit/plugins/darwin/memory_spec.rb new file mode 100644 index 00000000..17595ae8 --- /dev/null +++ b/spec/unit/plugins/darwin/memory_spec.rb @@ -0,0 +1,64 @@ +# +# 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(:each) 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 + + @plugin = get_plugin("darwin/memory") + @plugin.stub(:collect_os).and_return(:darwin) + @plugin.stub(:shell_out).with("sysctl -n hw.memsize").and_return(mock_shell_out(0, darwin_memsize, "")) + @plugin.stub(:shell_out).with("vm_stat").and_return(mock_shell_out(0, darwin_vm_stat, "")) + @plugin.run + end + + it "should set memory[:total] to 16384MB" do + @plugin[:memory][:total].should == '16384MB' + end + + it "should set memory[:active] to 5140MB" do + @plugin[:memory][:active].should == '5140MB' + end + + it "should set memory[:inactive] to 738MB" do + @plugin[:memory][:inactive].should == '738MB' + end + + it "should set memory[:free] to 10504MB" do + @plugin[:memory][:free].should == '10504MB' + end +end |