Created
December 29, 2019 20:52
-
-
Save ioquatix/a23576e31bd53828ee1f94dd221be4e2 to your computer and use it in GitHub Desktop.
smaps pss memory usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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