guard against permission denied errors

reading from /proc/<pid>/smaps is not available inside of Docker container
This commit is contained in:
Gerhard Schlager 2014-08-03 11:40:31 +02:00
parent 04682335d4
commit abe657dfce
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