Skip to content

Instantly share code, notes, and snippets.

@tuxdna
Created December 5, 2012 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxdna/4215536 to your computer and use it in GitHub Desktop.
Save tuxdna/4215536 to your computer and use it in GitHub Desktop.
Script find usage of shared libraries of a running process
#!/usr/bin/env ruby
# filename="/proc/5952/maps"
def usage
puts "USAGE: #{$0} /proc/<pid>/maps"
exit
end
filename=ARGV[0]
usage unless filename
libs = {}
File.open(filename).readlines.each do |line|
entries = line.split(/\s+/)
# p [ entries, entries.size]
if entries.size == 6 then
libname = entries[-1]
if libname =~ /\.so/ then
start_mem,to_mem = entries[0].split("-")
bytes = to_mem.hex - start_mem.hex
# puts "#{libname}: #{bytes/1024.0} KBs"
if libs[libname] then
old_val = libs[libname]
new_val = (old_val || 0) + bytes
libs[libname] = new_val
else
libs[libname] = bytes
end
end
end
end
total = 0
libs.keys.sort.each do | k |
libname = k
bytes = libs[k]
puts "#{libname}: #{bytes/1024.0} KBs"
total += bytes
end
puts "total memory consumption by shared libraries: #{total/1024.0} KBs ( #{total/1024.0/1024} MBs )"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment