class UnicornWrangler::RssReader

Constants

LINUX
PS_CMD
UNITS
VM_RSS

Public Class Methods

new(logger:) click to toggle source
# File lib/unicorn_wrangler/rss_reader.rb, line 16
def initialize(logger:)
  @logger = logger
end

Public Instance Methods

rss(pid: Process.pid) click to toggle source

Returns RSS in megabytes; should work on Linux and Mac OS X

# File lib/unicorn_wrangler/rss_reader.rb, line 21
def rss(pid: Process.pid)
  LINUX ? rss_linux(pid) : rss_posix(pid)
end

Private Instance Methods

rss_linux(pid) click to toggle source

Read from /proc/$pid/status. Linux only. ~100x faster and doesn't incur significant memory cost. file returns variable units, we want mb

# File lib/unicorn_wrangler/rss_reader.rb, line 38
def rss_linux(pid)
  File.read("/proc/#{pid}/status").match(VM_RSS) do |match|
    value, magnitude = match[1].to_i, UNITS.fetch(match[2].downcase.to_sym)

    value * magnitude / UNITS.fetch(:mb)
  end
rescue
  # If the given pid is dead, file will not be found
  @logger.warn 'Failed to read RSS from /proc, falling back to exec+ps' if @logger
  rss_posix(pid)
end
rss_posix(pid) click to toggle source

Fork/exec ps and parse result. Should work on any system with POSIX ps. ~4ms returns kb but we want mb

# File lib/unicorn_wrangler/rss_reader.rb, line 31
def rss_posix(pid)
  `#{PS_CMD % [pid]}`.to_i / 1024
end