-
Notifications
You must be signed in to change notification settings - Fork 452
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
abbe888
provide basic memory information for Mac OS X
a53edb3
fix in case the unit is not uppercase (kB for kilobytes is an example)
023d9e3
Merge branch 'master' of git://github.com/opscode/ohai into OHAI-431
31ed7f3
use sysctl and vm_stat to get up-to-date mem stats. also lump in wire…
9cc8421
include test. use from helper.
5e1cd48
Merge branch 'master' of git://github.com/opscode/ohai into OHAI-431
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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: