Skip to content

Commit

Permalink
Adapt vitals (mem and cpu) for Puma
Browse files Browse the repository at this point in the history
Return the summed up memory bytes and cpu for the Puma process and its
worker subprocesses.
  • Loading branch information
philippthun committed Nov 22, 2023
1 parent b467d90 commit a105b64
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/vcap/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ module VCAP
class Stats
class << self
def process_memory_bytes_and_cpu
rss, pcpu = `ps -o rss=,pcpu= -p #{Process.pid}`.split.map(&:to_i)
rss_bytes = rss * 1024
[rss_bytes, pcpu]
rss = []
pcpu = []

ps_out = ps_pid
ps_out += ps_ppid if VCAP::CloudController::Config.config.get(:webserver) == 'puma'
ps_out.split.each_with_index { |e, i| i.even? ? rss << e : pcpu << e }

[rss.map(&:to_i).sum * 1024, pcpu.map(&:to_f).sum.round]
end

def memory_used_bytes
Expand All @@ -23,6 +28,16 @@ def memory_free_bytes
def cpu_load_average
Vmstat.load_average.one_minute
end

private

def ps_pid
`ps -o rss=,pcpu= --pid #{Process.pid}`
end

def ps_ppid
`ps -o rss=,pcpu= --ppid #{Process.pid}`
end
end
end
end
29 changes: 29 additions & 0 deletions spec/unit/lib/vcap/stats_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'spec_helper'

RSpec.describe VCAP::Stats do
describe '#process_memory_bytes_and_cpu' do
before do
allow(VCAP::Stats).to receive_messages(ps_pid: "123456 7.8\n", ps_ppid: "121212 3.4\n343434 5.6\n")
end

it 'returns the memory bytes and cpu for the process' do
rss_bytes, pcpu = VCAP::Stats.process_memory_bytes_and_cpu

expect(rss_bytes).to eq(126_418_944)
expect(pcpu).to eq(8)
end

context 'when Puma is configured as webserver' do
before do
TestConfig.override(webserver: 'puma')
end

it 'returns the summed up memory bytes and cpu for the process and its subprocesses' do
rss_bytes, pcpu = VCAP::Stats.process_memory_bytes_and_cpu

expect(rss_bytes).to eq(602_216_448)
expect(pcpu).to eq(17)
end
end
end
end

0 comments on commit a105b64

Please sign in to comment.