-
-
Save k0kubun/6abfa1f3f7bdd56ac03e800706bd92ce to your computer and use it in GitHub Desktop.
This file contains 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 | |
output = `pgrep -f 'unicorn worker'` | |
pids = output.split("\n") | |
processes = {} | |
pids.each do |pid| | |
begin | |
process_name = File.read("/proc/#{pid}/cmdline").strip.gsub("\0", ' ') | |
smaps_rollup = File.read("/proc/#{pid}/smaps_rollup") | |
pss = smaps_rollup.match(/Pss:\s+(\d+)/)[1].to_i | |
processes[pid] = { pss: pss, name: process_name } | |
rescue Errno::ENOENT | |
next | |
end | |
end | |
sorted_processes = processes.sort_by { |pid, info| -info[:pss] } | |
# Calculate the total PSS | |
total_pss = sorted_processes.reduce(0) { |sum, (pid, info)| sum + info[:pss] } | |
# Determine the width of the PSS column | |
pss_column_width = total_pss.to_s.length | |
sorted_processes.each do |pid, info| | |
pss_mib = (info[:pss] / 1024.0).round | |
# Calculate the percentage of the total PSS consumed by this process | |
pss_percent = ((info[:pss].to_f / total_pss) * 100).round(2) | |
# Format the percentage to always show two decimal places | |
formatted_pss_percent = format("%.2f", pss_percent) | |
# Use rjust to add padding to the left of the PSS value | |
puts "#{pss_mib.to_s.rjust(pss_column_width)} MiB (#{formatted_pss_percent}%): #{info[:name]}" | |
end | |
# Print the total PSS in MiB | |
total_pss_mib = (total_pss / 1024.0).round | |
puts "Total PSS: #{total_pss_mib} MiB" | |
# Calculate and print the average PSS in MiB | |
average_pss_mib = (total_pss / pids.size / 1024.0).round | |
puts "Average PSS: #{average_pss_mib} MiB" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment