Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Darwin support

  • Loading branch information...
commit de981bdc25c94d604d3dc321e8251a59aa33531e 1 parent 02f26e9
@itsderek23 itsderek23 authored
View
10 memory_profiler/fixtures/top_darwin.txt
@@ -0,0 +1,10 @@
+Processes: 89 total, 5 running, 84 sleeping, 480 threads
+2012/05/14 13:32:02
+Load Avg: 0.47, 0.72, 0.73
+CPU usage: 16.12% user, 48.38% sys, 35.48% idle
+SharedLibs: 2392K resident, 3920K data, 0B linkedit.
+MemRegions: 26179 total, 1411M resident, 26M private, 496M shared.
+PhysMem: 883M wired, 1650M active, 624M inactive, 3158M used, 936M free.
+VM: 183G vsize, 1034M framework vsize, 33138536(0) pageins, 6235(0) pageouts.
+Networks: packets: 17981863/16G in, 13457896/4107M out.
+Disks: 3911133/88G read, 7886166/144G written.
View
44 memory_profiler/memory_profiler.rb
@@ -1,7 +1,15 @@
class MemoryProfiler < Scout::Plugin
+ # reports darwin units as MB
+ DARWIN_UNITS = { "b" => 1/(1024*1024),
+ "k" => 1/1024,
+ "m" => 1,
+ "g" => 1024 }
+
def build_report
if solaris?
solaris_memory
+ elsif darwin?
+ darwin_memory
else
linux_memory
end
@@ -63,6 +71,27 @@ def linux_memory
end
end
+ # Parses top output. Does not report swap usage.
+ def darwin_memory
+ report_data = Hash.new
+ top_output = `top -l1 -n0 -u`
+ mem = top_output[/^(?:Phys)?Mem:.+/i]
+
+ mem.scan(/(\d+|\d+\.\d+)([bkmg])\s+(\w+)/i) do |amount, unit, label|
+ case label
+ when 'used'
+ report_data["Memory Used"] =
+ (amount.to_f * DARWIN_UNITS[unit.downcase]).round
+ when 'free'
+ report_data["Memory Available"] =
+ (amount.to_f * DARWIN_UNITS[unit.downcase]).round
+ end
+ end
+ report_data["Memory Total"] = report_data["Memory Used"]+report_data["Memory Available"]
+ report_data['% Memory Used'] = ((report_data["Memory Used"].to_f/report_data["Memory Total"])*100).to_i
+ report(report_data)
+ end
+
# Memory Used and Swap Used come from the prstat command.
# Memory Total comes from prtconf
# Swap Total comes from swap -s
@@ -110,6 +139,21 @@ def solaris?
return solaris
end
+ # True if on darwin. Only checked on the first run (assumes OS does not change).
+ def darwin?
+ darwin = if @memory.has_key?(:darwin)
+ memory(:darwin) || false
+ else
+ darwin = false
+ begin
+ darwin = true if `uname` =~ /darwin/i
+ rescue
+ end
+ end
+ remember(:darwin, darwin)
+ return darwin
+ end
+
# Ensures solaris memory metrics are in MB. Metrics that don't contain 'T,G,M,or K' are just
# turned into integers.
def clean_value(value)
View
74 memory_profiler/test.rb
@@ -7,8 +7,8 @@ class MemoryProfilerTest < Test::Unit::TestCase
# date hash hash
def test_success_linux
@plugin=MemoryProfiler.new(nil,{},{})
- @plugin.expects(:`).with("cat /proc/meminfo").returns(File.read(File.dirname(__FILE__)+'/fixtures/proc_meminfo.txt')).once
- @plugin.expects(:`).with("uname").returns('Linux').once
+ @plugin.stubs(:`).with("cat /proc/meminfo").returns(File.read(File.dirname(__FILE__)+'/fixtures/proc_meminfo.txt'))
+ @plugin.stubs(:`).with("uname").returns('Linux')
res = @plugin.run()
@@ -28,9 +28,8 @@ def test_success_linux
def test_success_linux_second_run
# shouldn't run uname again as it is stored in memory
- @plugin=MemoryProfiler.new(Time.now-60*10,{:solaris=>false},{})
- @plugin.expects(:`).with("cat /proc/meminfo").returns(File.read(File.dirname(__FILE__)+'/fixtures/proc_meminfo.txt')).once
- @plugin.expects(:`).with("uname").returns('Linux').never
+ @plugin=MemoryProfiler.new(Time.now-60*10,{:solaris=>false,:darwin=>false},{})
+ @plugin.stubs(:`).with("cat /proc/meminfo").returns(File.read(File.dirname(__FILE__)+'/fixtures/proc_meminfo.txt'))
res = @plugin.run()
assert_equal false,res[:memory][:solaris]
@@ -38,8 +37,8 @@ def test_success_linux_second_run
def test_success_with_no_buffers
@plugin=MemoryProfiler.new(nil,{},{})
- @plugin.expects(:`).with("cat /proc/meminfo").returns(File.read(File.dirname(__FILE__)+'/fixtures/no_buffers.txt')).once
- @plugin.expects(:`).with("uname").returns('Linux').once
+ @plugin.stubs(:`).with("cat /proc/meminfo").returns(File.read(File.dirname(__FILE__)+'/fixtures/no_buffers.txt'))
+ @plugin.stubs(:`).with("uname").returns('Linux')
res = @plugin.run()
@@ -50,10 +49,10 @@ def test_success_with_no_buffers
def test_success_solaris
@plugin=MemoryProfiler.new(nil,{},{})
- @plugin.expects(:`).with("prstat -c -Z 1 1").returns(File.read(File.dirname(__FILE__)+'/fixtures/prstat.txt')).once
- @plugin.expects(:`).with("/usr/sbin/prtconf | grep Memory").returns(File.read(File.dirname(__FILE__)+'/fixtures/prtconf.txt')).once
- @plugin.expects(:`).with("swap -s").returns(File.read(File.dirname(__FILE__)+'/fixtures/swap.txt')).once
- @plugin.expects(:`).with("uname").returns('SunOS').once
+ @plugin.stubs(:`).with("prstat -c -Z 1 1").returns(File.read(File.dirname(__FILE__)+'/fixtures/prstat.txt'))
+ @plugin.stubs(:`).with("/usr/sbin/prtconf | grep Memory").returns(File.read(File.dirname(__FILE__)+'/fixtures/prtconf.txt'))
+ @plugin.stubs(:`).with("swap -s").returns(File.read(File.dirname(__FILE__)+'/fixtures/swap.txt'))
+ @plugin.stubs(:`).with("uname").returns('SunOS')
res = @plugin.run()
@@ -73,10 +72,10 @@ def test_success_solaris
def test_success_solaris_second_run
@plugin=MemoryProfiler.new(Time.now-60*10,{:solaris=>true},{})
- @plugin.expects(:`).with("prstat -c -Z 1 1").returns(File.read(File.dirname(__FILE__)+'/fixtures/prstat.txt')).once
- @plugin.expects(:`).with("/usr/sbin/prtconf | grep Memory").returns(File.read(File.dirname(__FILE__)+'/fixtures/prtconf.txt')).once
- @plugin.expects(:`).with("swap -s").returns(File.read(File.dirname(__FILE__)+'/fixtures/swap.txt')).once
- @plugin.expects(:`).with("uname").returns('SunOS').never
+ @plugin.stubs(:`).with("prstat -c -Z 1 1").returns(File.read(File.dirname(__FILE__)+'/fixtures/prstat.txt'))
+ @plugin.stubs(:`).with("/usr/sbin/prtconf | grep Memory").returns(File.read(File.dirname(__FILE__)+'/fixtures/prtconf.txt'))
+ @plugin.stubs(:`).with("swap -s").returns(File.read(File.dirname(__FILE__)+'/fixtures/swap.txt'))
+ @plugin.stubs(:`).with("uname").returns('SunOS').never
res = @plugin.run()
assert_equal true,res[:memory][:solaris]
@@ -84,10 +83,10 @@ def test_success_solaris_second_run
def test_success_solaris_with_gb_swap_units
@plugin=MemoryProfiler.new(nil,{},{})
- @plugin.expects(:`).with("prstat -c -Z 1 1").returns(File.read(File.dirname(__FILE__)+'/fixtures/prstat.txt')).once
- @plugin.expects(:`).with("/usr/sbin/prtconf | grep Memory").returns(File.read(File.dirname(__FILE__)+'/fixtures/prtconf.txt')).once
- @plugin.expects(:`).with("swap -s").returns(File.read(File.dirname(__FILE__)+'/fixtures/swap_gb.txt')).once
- @plugin.expects(:`).with("uname").returns('SunOS').once
+ @plugin.stubs(:`).with("prstat -c -Z 1 1").returns(File.read(File.dirname(__FILE__)+'/fixtures/prstat.txt'))
+ @plugin.stubs(:`).with("/usr/sbin/prtconf | grep Memory").returns(File.read(File.dirname(__FILE__)+'/fixtures/prtconf.txt'))
+ @plugin.stubs(:`).with("swap -s").returns(File.read(File.dirname(__FILE__)+'/fixtures/swap_gb.txt'))
+ @plugin.stubs(:`).with("uname").returns('SunOS')
res = @plugin.run()
@@ -104,4 +103,41 @@ def test_success_solaris_with_gb_swap_units
assert_equal 872, r["Memory Used"]
assert_equal 32763, r["Memory Total"]
end
+
+ def test_success_darwin
+ @plugin=MemoryProfiler.new(nil,{},{})
+ @plugin.stubs(:`).with("top -l1 -n0 -u").returns(File.read(File.dirname(__FILE__)+'/fixtures/top_darwin.txt'))
+ @plugin.stubs(:`).with("uname").returns('Darwin')
+
+ res = @plugin.run()
+
+ assert res[:errors].empty?
+ assert !res[:memory][:solaris]
+ assert res[:memory][:darwin]
+
+ assert_equal 4, res[:reports].first.keys.size
+
+ r = res[:reports].first
+ assert_equal 77, r["% Memory Used"]
+ assert_equal 3158, r["Memory Used"]
+ assert_equal 4094, r["Memory Total"]
+ assert_equal 936, r["Memory Available"]
+ end
+
+ def test_success_darwin_second_run
+ @plugin=MemoryProfiler.new(Time.now-60*10,{:solaris=>false,:darwin=>true},{})
+ @plugin.stubs(:`).with("top -l1 -n0 -u").returns(File.read(File.dirname(__FILE__)+'/fixtures/top_darwin.txt'))
+ @plugin.stubs(:`).with("uname").returns('Darwin')
+
+ res = @plugin.run()
+
+ assert res[:errors].empty?
+ assert !res[:memory][:solaris]
+ assert res[:memory][:darwin]
+
+ assert_equal 4, res[:reports].first.keys.size
+
+ r = res[:reports].first
+ assert_equal 77, r["% Memory Used"]
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.