Skip to content

Instantly share code, notes, and snippets.

@apeiros
Created July 2, 2012 18:45
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 apeiros/3034844 to your computer and use it in GitHub Desktop.
Save apeiros/3034844 to your computer and use it in GitHub Desktop.
Prints memory and cpu footprint of the server (uses ps in a subshell)
module Kernel
# Prints memory and cpu footprint of the server (uses ps in a subshell,
# portability is therefore limited)
def print_resource_usage
ps_out = `ps -o vsz,rss,%cpu,%mem -p #{$$}`
vsz, rss, cpu, pmem = ps_out.scan(/\d+(?:[.,]\d+)?/).map { |e| e.gsub(/,/,'.').to_f } # ps on 10.5.1 outputs ',' instead of '.' for MEM%
virtual, real = (vsz-rss).div(1024), rss.div(1024)
$stdout.printf "%dMB real, %dMB virtual, %.1f%% CPU, %.1f%% MEM\n", real, virtual, cpu, pmem
end
module_function :print_resource_usage
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment