Merge pull request #2622 from gschlager/fix_memstats

handle permission denied errors
This commit is contained in:
Sam 2014-08-06 13:58:45 +10:00
commit bc48094d9a
1 changed files with 25 additions and 15 deletions

View File

@ -86,31 +86,41 @@ def consume_mapping( map_lines, totals )
return m
end
def create_memstats_not_available( totals )
Mapping::FIELDS.each do |field|
totals[field] += Float::NAN
end
end
abort 'usage: memstats [pid]' unless ARGV.first
pid = ARGV.shift.to_i
totals = Hash.new(0)
mappings = []
File.open( "/proc/#{pid}/smaps" ) do |smaps|
begin
File.open( "/proc/#{pid}/smaps" ) do |smaps|
map_lines = []
map_lines = []
loop do
break if smaps.eof?
line = smaps.readline.strip
case line
when /\w+:\s+/
map_lines << line
when /[0-9a-f]+:[0-9a-f]+\s+/
if map_lines.size > 0 then
mappings << consume_mapping( map_lines, totals )
loop do
break if smaps.eof?
line = smaps.readline.strip
case line
when /\w+:\s+/
map_lines << line
when /[0-9a-f]+:[0-9a-f]+\s+/
if map_lines.size > 0 then
mappings << consume_mapping( map_lines, totals )
end
map_lines.clear
map_lines << line
else
break
end
map_lines.clear
map_lines << line
else
break
end
end
rescue
create_memstats_not_available( totals )
end
# http://rubyforge.org/snippet/download.php?type=snippet&id=511