Skip to content

Instantly share code, notes, and snippets.

@ioquatix
Created December 29, 2019 20:52
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 ioquatix/a23576e31bd53828ee1f94dd221be4e2 to your computer and use it in GitHub Desktop.
Save ioquatix/a23576e31bd53828ee1f94dd221be4e2 to your computer and use it in GitHub Desktop.
smaps pss memory usage
#!/usr/bin/env ruby
require 'process/pipeline'
def process_group_pids(pids)
pids.flat_map do |pid|
buffer = Process::Pipeline.("pstree -p #{pid}").read
buffer.scan(/(?<=\()\d+(?=\))/)
end.sort.uniq
end
usage = Hash.new{|h,k| h[k] = 0}
pids = process_group_pids(ARGV.map(&:to_i))
pids.each do |pid|
smaps = File.readlines("/proc/#{pid}/smaps")
smaps.each do |line|
if /(?<key>.*?):\s+(?<value>\d+) kB/ =~ line
usage[key] += value.to_i
end
end
end
usage.each do |key, value|
next if value.zero?
puts "#{key}: #{(value / 1024.0).round(2)}Mb"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment