Class: ProcParser::MemInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/proc_parser/mem_info.rb

Constant Summary collapse

@@attributes =
{
  memtotal: 'MemTotal',
  memfree: 'MemFree',
  buffers: 'Buffers',
  cached: 'Cached',
  swapcached: 'SwapCached',
  active: 'Active',
  inactive: 'Inactive',
  active_anon: 'Active\(anon\)',
  inactive_anon: 'Inactive\(anon\)',
  active_file: 'Active\(file\)',
  inactive_file: 'Inactive\(file\)',
  unevictable: 'Unevictable',
  mlocked: 'Mlocked',
  swaptotal: 'SwapTotal',
  swapfree: 'SwapFree',
  dirty: 'Dirty',
  writeback: 'Writeback',
  anonpages: 'AnonPages',
  mapped: 'Mapped',
  slab: 'Slab',
  sreclaimable: 'SReclaimable',
  sunreclaim: 'SUnreclaim',
  pagetables: 'PageTables',
  nfs_unstable: 'NFS_Unstable',
  bounce: 'Bounce',
  writebacktmp: 'WritebackTmp',
  commitlimit: 'CommitLimit',
  committed_as: 'Committed_AS',
  vmalloctotal: 'VmallocTotal',
  vmallocused: 'VmallocUsed',
  vmallocchunk: 'VmallocChunk',
  directmap4k: 'DirectMap4k',
  directmap2m: 'DirectMap2M',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meminfo_file = '/proc/meminfo') ⇒ MemInfo

Returns a new instance of MemInfo.

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/proc_parser/mem_info.rb', line 53

def initialize(meminfo_file = '/proc/meminfo')
  raise NoProcData, "This system doesn't have /proc/meminfo data." if !File.exist?(meminfo_file)

  File.open(meminfo_file, 'r') do |file|
    data = file.read
    @@attributes.each_key do |attribute|
      value, unit = regex_match(attribute, data)
      if unit != 'kB'
        raise NoProcData, 'Unsupported unit stored in meminfo.'
      end
      instance_variable_set("@#{attribute}", value.to_i)
    end
  end
end

Instance Attribute Details

#activeObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def active
  @active
end

#active_anonObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def active_anon
  @active_anon
end

#active_fileObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def active_file
  @active_file
end

#anonpagesObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def anonpages
  @anonpages
end

#bounceObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def bounce
  @bounce
end

#buffersObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def buffers
  @buffers
end

#cachedObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def cached
  @cached
end

#commitlimitObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def commitlimit
  @commitlimit
end

#committed_asObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def committed_as
  @committed_as
end

#directmap2mObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def directmap2m
  @directmap2m
end

#directmap4kObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def directmap4k
  @directmap4k
end

#dirtyObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def dirty
  @dirty
end

#inactiveObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def inactive
  @inactive
end

#inactive_anonObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def inactive_anon
  @inactive_anon
end

#inactive_fileObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def inactive_file
  @inactive_file
end

#mappedObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def mapped
  @mapped
end

#memfreeObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def memfree
  @memfree
end

#memtotalObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def memtotal
  @memtotal
end

#mlockedObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def mlocked
  @mlocked
end

#nfs_unstableObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def nfs_unstable
  @nfs_unstable
end

#pagetablesObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def pagetables
  @pagetables
end

#slabObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def slab
  @slab
end

#sreclaimableObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def sreclaimable
  @sreclaimable
end

#sunreclaimObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def sunreclaim
  @sunreclaim
end

#swapcachedObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def swapcached
  @swapcached
end

#swapfreeObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def swapfree
  @swapfree
end

#swaptotalObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def swaptotal
  @swaptotal
end

#unevictableObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def unevictable
  @unevictable
end

#vmallochunkObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def vmallochunk
  @vmallochunk
end

#vmalloctotalObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def vmalloctotal
  @vmalloctotal
end

#vmallocusedObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def vmallocused
  @vmallocused
end

#writebackObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def writeback
  @writeback
end

#writebacktmpObject

Contains the data in kilobytes



10
11
12
# File 'lib/proc_parser/mem_info.rb', line 10

def writebacktmp
  @writebacktmp
end

Instance Method Details

#free_buffersObject

Memory available in the system. It is not just the free memory.

The available memory is actually what the ‘free` command line tool calls `-/+ buffers/cache`. It uses information from /proc/meminfo: it sums the MemFree, the Buffers and the Cached.

cf. ‘free` source code: github.com/mmalecki/procps/blob/fe4c4a7314f32907b9f558ad0d8b8d0ff1cc76be/free.c#L97 cf. man 5 proc



90
91
92
# File 'lib/proc_parser/mem_info.rb', line 90

def free_buffers
  @memfree + @buffers + @cached
end

#memusedObject

Memory currently in use.

We substract the free amount of memory to the total.



71
72
73
# File 'lib/proc_parser/mem_info.rb', line 71

def memused
  @memtotal - @memfree
end

#swapusedObject

Swap currently in use.

We substract the free amount of swap to the total.



78
79
80
# File 'lib/proc_parser/mem_info.rb', line 78

def swapused
  @swaptotal - @swapfree
end