Skip to content

Instantly share code, notes, and snippets.

@asender
Last active May 26, 2019 04:43
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 asender/71b9746ae16602bfad0c268e5e6652fe to your computer and use it in GitHub Desktop.
Save asender/71b9746ae16602bfad0c268e5e6652fe to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
options = {
count: 10
}
lines = `grep '^Swap' /proc/*/smaps 2>/dev/null`.split("\n")
pid2swap = {}
puts "Swap space PID Process"
puts "========== ===== ======="
lines.each do |line|
if !line.match(/\/proc\/(\d+)\/smaps:Swap:\s*(\d+) kB/)
puts "Bad line: " + line
next
end
pid, kb = $1, $2
pid2swap[pid] = pid2swap[pid].to_i + kb.to_i
end
pid2swap.sort {|a,b| -a[1] <=> -b[1] }.slice(0...options[:count]).each do |pid, kb|
psout = `ps -p #{pid} -o args=`.strip
if psout.empty?
printf "%s kB (no longer running)\n", kb
else
printf "%s kB %s %s\n", kb, pid, psout
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment