Skip to content

Commit 7f4780d

Browse files
committed
Added support for MemAvailable, as this is the key metric when using Linux
1 parent da2db81 commit 7f4780d

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

lib/vmstat/memory.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ module Vmstat
1515
# @attr [Fixnum] pageouts
1616
# The number of pageouts.
1717
class Memory < Struct.new(:pagesize, :wired, :active, :inactive, :free,
18-
:pageins, :pageouts)
18+
:pageins, :pageouts, :available)
1919
# Calculate the wired bytes based of the wired pages.
2020
# @return [Fixnum] wired bytes
2121
def wired_bytes
22-
wired * pagesize
22+
wired * pagesize
2323
end
2424

2525
# Calculate the active bytes based of the active pages.
@@ -40,10 +40,16 @@ def free_bytes
4040
free * pagesize
4141
end
4242

43+
# Calculate the available bytes based of the active pages.
44+
# @return [Fixnum] active bytes
45+
def available_bytes
46+
available * pagesize
47+
end
48+
4349
# Calculate the total bytes based of all pages
4450
# @return [Fixnum] total bytes
4551
def total_bytes
46-
(wired + active + inactive + free) * pagesize
52+
(wired + active + inactive + free) * pagesize
4753
end
4854
end
4955
end

lib/vmstat/procfs.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,17 @@ def cpu
4646
def memory
4747
@pagesize ||= Vmstat.pagesize
4848

49-
total = free = active = inactive = pageins = pageouts = 0
49+
total = free = active = inactive = pageins = pageouts = available = 0
5050
procfs_file("meminfo") do |file|
5151
content = file.read(2048) # the requested information is in the first bytes
5252

53-
content.scan(/(\w+):\s+(\d+) kB/) do |name, kbytes|
53+
content.scan(/(\w+):\s+(\d+) kB/) do |name, kbytes|
5454
pages = (kbytes.to_i * 1024) / @pagesize
5555

5656
case name
5757
when "MemTotal" then total = pages
5858
when "MemFree" then free = pages
59+
when "MemAvailable" then available = pages
5960
when "Active" then active = pages
6061
when "Inactive" then inactive = pages
6162
end
@@ -74,8 +75,7 @@ def memory
7475
end
7576
end
7677

77-
Memory.new @pagesize, total-free-active-inactive, active, inactive, free,
78-
pageins, pageouts
78+
Memory.new @pagesize, total-free-active-inactive, active, inactive, free, pageins, pageouts, available
7979
end
8080

8181
# Fetches the information for all available network devices.
@@ -91,8 +91,8 @@ def network_interfaces
9191
when /^lo/ then NetworkInterface::LOOPBACK_TYPE
9292
end
9393

94-
netifcs << NetworkInterface.new(columns[0].to_sym, columns[1].to_i,
95-
columns[3].to_i, columns[4].to_i,
94+
netifcs << NetworkInterface.new(columns[0].to_sym, columns[1].to_i,
95+
columns[3].to_i, columns[4].to_i,
9696
columns[9].to_i, columns[11].to_i,
9797
type)
9898
end
@@ -107,7 +107,7 @@ def task
107107

108108
procfs_file("self", "stat") do |file|
109109
data = file.read.split(/ /)
110-
Task.new(data[22].to_i / @pagesize, data[23].to_i,
110+
Task.new(data[22].to_i / @pagesize, data[23].to_i,
111111
data[13].to_i * 1000, data[14].to_i * 1000)
112112
end
113113
end

0 commit comments

Comments
 (0)