Skip to content

Commit c5d1b87

Browse files
committed
Expose total system memory.
1 parent 36725a3 commit c5d1b87

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

lib/process/metrics/command/summary.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,7 @@ def call
109109
format_memory_usage = self.method(:format_memory_usage).curry
110110
shared_memory_usage = 0
111111
private_memory_usage = 0
112-
total_memory_usage = 0
113-
114-
summary.each do |pid, general|
115-
if memory = general.memory
116-
total_memory_usage += memory.proportional_size + memory.unique_size
117-
else
118-
total_memory_usage += general.resident_size
119-
end
120-
end
112+
total_memory_usage = Process::Metrics::Memory.total_size
121113

122114
proportional = true
123115

lib/process/metrics/memory/darwin.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ def self.supported?
1313
File.executable?(VMMAP)
1414
end
1515

16+
# @returns [Numeric] Total memory size in kilobytes.
17+
def self.total_size
18+
# sysctl hw.memsize
19+
IO.popen(["sysctl", "hw.memsize"], "r") do |io|
20+
io.each_line do |line|
21+
if line =~ /hw.memsize: (\d+)/
22+
return $1.to_i / 1024
23+
end
24+
end
25+
end
26+
end
27+
1628
# Parse a size string into kilobytes.
1729
def self.parse_size(string)
1830
return 0 unless string
@@ -91,6 +103,10 @@ def supported?
91103
return true
92104
end
93105

106+
def total_size
107+
return Memory::Darwin.total_size
108+
end
109+
94110
def capture(pids)
95111
return Memory::Darwin.capture(pids)
96112
end

lib/process/metrics/memory/linux.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
module Process
77
module Metrics
88
class Memory::Linux
9+
# @returns [Numeric] Total memory size in kilobytes.
10+
def self.total_size
11+
File.read("/proc/meminfo").each_line do |line|
12+
if /MemTotal:\s+(?<total>\d+) kB/ =~ line
13+
return total.to_i
14+
end
15+
end
16+
end
17+
918
# The fields that will be extracted from the `smaps` data.
1019
SMAP = {
1120
"Rss" => :resident_size,
@@ -89,6 +98,10 @@ def supported?
8998
return true
9099
end
91100

101+
def total_size
102+
return Memory::Linux.total_size
103+
end
104+
92105
def capture(pids)
93106
return Memory::Linux.capture(pids)
94107
end

test/process/memory.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
require "process/metrics"
77

88
describe Process::Metrics::Memory do
9+
with ".total_size" do
10+
it "can get the total available memory" do
11+
expect(Process::Metrics::Memory.total_size).to be > 0
12+
end
13+
end
14+
915
with ".capture" do
1016
let(:pid) {Process.pid}
1117
let(:capture) {Process::Metrics::General.capture(pid: pid)}

0 commit comments

Comments
 (0)