Skip to content

Commit 5c13458

Browse files
committed
fixes #16 adds LinuxMemory with MemAvailable
1 parent 7f4780d commit 5c13458

8 files changed

Lines changed: 201 additions & 10 deletions

File tree

lib/vmstat.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Vmstat
1515
autoload :Disk, "vmstat/disk"
1616
autoload :LinuxDisk, "vmstat/linux_disk"
1717
autoload :Memory, "vmstat/memory"
18+
autoload :LinuxMemory, "vmstat/linux_memory"
1819
autoload :Task, "vmstat/task"
1920
autoload :LoadAverage, "vmstat/load_average"
2021
autoload :ProcFS, "vmstat/procfs"

lib/vmstat/linux_memory.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# @attr [Fixnum] available
2+
# The estimated available memory (linux)
3+
# See: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
4+
class Vmstat::LinuxMemory < Vmstat::Memory
5+
attr_accessor :available
6+
7+
# Calculate the available bytes based of the active pages.
8+
# @return [Fixnum] active bytes
9+
def available_bytes
10+
available * pagesize
11+
end
12+
end
13+

lib/vmstat/memory.rb

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Vmstat
1515
# @attr [Fixnum] pageouts
1616
# The number of pageouts.
1717
class Memory < Struct.new(:pagesize, :wired, :active, :inactive, :free,
18-
:pageins, :pageouts, :available)
18+
:pageins, :pageouts)
1919
# Calculate the wired bytes based of the wired pages.
2020
# @return [Fixnum] wired bytes
2121
def wired_bytes
@@ -40,16 +40,23 @@ 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-
4943
# Calculate the total bytes based of all pages
5044
# @return [Fixnum] total bytes
5145
def total_bytes
5246
(wired + active + inactive + free) * pagesize
5347
end
5448
end
49+
50+
# @attr [Fixnum] available
51+
# The estimated available memory (linux)
52+
# See: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
53+
class LinuxMemory < Memory
54+
attr_accessor :available
55+
56+
# Calculate the available bytes based of the active pages.
57+
# @return [Fixnum] active bytes
58+
def available_bytes
59+
available * pagesize
60+
end
61+
end
5562
end

lib/vmstat/procfs.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def cpu
4545
# Vmstat.memory # => #<struct Vmstat::Memory ...>
4646
def memory
4747
@pagesize ||= Vmstat.pagesize
48+
has_available = false
4849

4950
total = free = active = inactive = pageins = pageouts = available = 0
5051
procfs_file("meminfo") do |file|
@@ -56,7 +57,9 @@ def memory
5657
case name
5758
when "MemTotal" then total = pages
5859
when "MemFree" then free = pages
59-
when "MemAvailable" then available = pages
60+
when "MemAvailable"
61+
available = pages
62+
has_available = true
6063
when "Active" then active = pages
6164
when "Inactive" then inactive = pages
6265
end
@@ -75,7 +78,11 @@ def memory
7578
end
7679
end
7780

78-
Memory.new @pagesize, total-free-active-inactive, active, inactive, free, pageins, pageouts, available
81+
mem_klass = has_available ? LinuxMemory : Memory
82+
mem_klass.new(@pagesize, total-free-active-inactive, active,
83+
inactive, free, pageins, pageouts).tap do |mem|
84+
mem.available = available if has_available
85+
end
7986
end
8087

8188
# Fetches the information for all available network devices.

lib/vmstat/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Vmstat
2-
VERSION = "2.2.1"
2+
VERSION = "2.3.0"
33
end

spec/memavail_procfs/meminfo

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
MemTotal: 1884428 kB
2+
MemFree: 252452 kB
3+
MemAvailable: 458368 kB
4+
Buffers: 76 kB
5+
Cached: 321752 kB
6+
SwapCached: 4 kB
7+
Active: 1307912 kB
8+
Inactive: 177976 kB
9+
Active(anon): 1074332 kB
10+
Inactive(anon): 106548 kB
11+
Active(file): 233580 kB
12+
Inactive(file): 71428 kB
13+
Unevictable: 21100 kB
14+
Mlocked: 21100 kB
15+
SwapTotal: 2097148 kB
16+
SwapFree: 2097140 kB
17+
Dirty: 60 kB
18+
Writeback: 0 kB
19+
AnonPages: 1185204 kB
20+
Mapped: 101384 kB
21+
Shmem: 8760 kB
22+
Slab: 59380 kB
23+
SReclaimable: 27064 kB
24+
SUnreclaim: 32316 kB
25+
KernelStack: 4528 kB
26+
PageTables: 21592 kB
27+
NFS_Unstable: 0 kB
28+
Bounce: 0 kB
29+
WritebackTmp: 0 kB
30+
CommitLimit: 3039360 kB
31+
Committed_AS: 2906080 kB
32+
VmallocTotal: 34359738367 kB
33+
VmallocUsed: 269704 kB
34+
VmallocChunk: 34359466492 kB
35+
HardwareCorrupted: 0 kB
36+
AnonHugePages: 659456 kB
37+
HugePages_Total: 0
38+
HugePages_Free: 0
39+
HugePages_Rsvd: 0
40+
HugePages_Surp: 0
41+
Hugepagesize: 2048 kB
42+
DirectMap4k: 26624 kB
43+
DirectMap2M: 2070528 kB

spec/memavail_procfs/vmstat

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
nr_free_pages 107062
2+
nr_inactive_anon 60
3+
nr_active_anon 1557
4+
nr_inactive_file 8341
5+
nr_active_file 4939
6+
nr_unevictable 0
7+
nr_mlock 0
8+
nr_anon_pages 1579
9+
nr_mapped 1461
10+
nr_file_pages 13346
11+
nr_dirty 1
12+
nr_writeback 0
13+
nr_slab_reclaimable 1628
14+
nr_slab_unreclaimable 1885
15+
nr_page_table_pages 101
16+
nr_kernel_stack 88
17+
nr_unstable 0
18+
nr_bounce 0
19+
nr_vmscan_write 0
20+
nr_vmscan_immediate_reclaim 0
21+
nr_writeback_temp 0
22+
nr_isolated_anon 0
23+
nr_isolated_file 0
24+
nr_shmem 67
25+
nr_dirtied 185
26+
nr_written 182
27+
nr_anon_transparent_hugepages 0
28+
nr_dirty_threshold 24392
29+
nr_dirty_background_threshold 12196
30+
pgpgin 64599
31+
pgpgout 1104
32+
pswpin 0
33+
pswpout 0
34+
pgalloc_dma 2
35+
pgalloc_normal 190973
36+
pgalloc_high 0
37+
pgalloc_movable 0
38+
pgfree 298439
39+
pgactivate 5802
40+
pgdeactivate 0
41+
pgfault 581461
42+
pgmajfault 42
43+
pgrefill_dma 0
44+
pgrefill_normal 0
45+
pgrefill_high 0
46+
pgrefill_movable 0
47+
pgsteal_dma 0
48+
pgsteal_normal 0
49+
pgsteal_high 0
50+
pgsteal_movable 0
51+
pgscan_kswapd_dma 0
52+
pgscan_kswapd_normal 0
53+
pgscan_kswapd_high 0
54+
pgscan_kswapd_movable 0
55+
pgscan_direct_dma 0
56+
pgscan_direct_normal 0
57+
pgscan_direct_high 0
58+
pgscan_direct_movable 0
59+
pginodesteal 0
60+
slabs_scanned 0
61+
kswapd_steal 0
62+
kswapd_inodesteal 0
63+
kswapd_low_wmark_hit_quickly 0
64+
kswapd_high_wmark_hit_quickly 0
65+
kswapd_skip_congestion_wait 0
66+
pageoutrun 1
67+
allocstall 0
68+
pgrotated 0
69+
compact_blocks_moved 0
70+
compact_pages_moved 0
71+
compact_pagemigrate_failed 0
72+
compact_stall 0
73+
compact_fail 0
74+
compact_success 0
75+
htlb_buddy_alloc_success 0
76+
htlb_buddy_alloc_fail 0
77+
unevictable_pgs_culled 0
78+
unevictable_pgs_scanned 0
79+
unevictable_pgs_rescued 0
80+
unevictable_pgs_mlocked 0
81+
unevictable_pgs_munlocked 0
82+
unevictable_pgs_cleared 0
83+
unevictable_pgs_stranded 0
84+
unevictable_pgs_mlockfreed 0
85+
thp_fault_alloc 0
86+
thp_fault_fallback 0
87+
thp_collapse_alloc 0
88+
thp_collapse_alloc_failed 0
89+
thp_split 0

spec/vmstat/memavail_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'spec_helper'
2+
3+
describe Vmstat::LinuxMemory do
4+
let(:procfs) do
5+
Class.new do
6+
extend Vmstat::ProcFS
7+
8+
def self.procfs_path
9+
File.expand_path("../../memavail_procfs", __FILE__)
10+
end
11+
end
12+
end
13+
14+
context "#memory" do
15+
subject { procfs.memory }
16+
17+
it { should be_a(Vmstat::LinuxMemory) }
18+
if `getconf PAGESIZE`.chomp.to_i == 4096
19+
it do
20+
should == Vmstat::LinuxMemory.new(4096, 36522, 326978,
21+
44494, 63113, 64599, 1104)
22+
end
23+
24+
it "should have the right total" do
25+
(subject.wired_bytes + subject.active_bytes +
26+
subject.inactive_bytes + subject.free_bytes).should == 1929654272
27+
end
28+
end
29+
end
30+
end
31+

0 commit comments

Comments
 (0)