Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions lib/ohai/plugins/darwin/memory.rb
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

67 changes: 67 additions & 0 deletions spec/unit/plugins/darwin/memory_spec.rb
Original file line number Diff line number Diff line change
@@ -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