class PosixPsutil::Memory

Public Class Methods

swap_memory() click to toggle source
# File linux/system.rb, line 230
def self.swap_memory()
  meminfo = OpenStruct.new
  swaps = File.new('/proc/swaps')
  swaps.readline() # ignore column header
  _, _, total, used, _ = swaps.readline().split(" ")
  # values are expressed in 4 KB, we want bytes instead
  meminfo.total = total.to_i * 1024
  meminfo.used = used.to_i * 1024

  
  meminfo.free = meminfo.total - meminfo.used
  meminfo.percent = COMMON::usage_percent(meminfo.used, meminfo.total, 1)
  
  IO.readlines('/proc/vmstat').each do |line|
    # values are expressed in 4 KB, we want bytes instead
    if line.start_with?('pswpin')
      meminfo.sin = line.split(' ')[1].to_i * LibPosixPsutil::PAGE_SIZE
    elsif line.start_with?('pswpout')
      meminfo.sout = line.split(' ')[1].to_i * LibPosixPsutil::PAGE_SIZE
    end
  end

  meminfo
end
virtual_memory() click to toggle source
# File linux/system.rb, line 202
def self.virtual_memory()
  meminfo = OpenStruct.new
  IO.readlines('/proc/meminfo').each do |line|
    pair = line.split(':')
    case pair[0]
      when 'Cached'
        # values are expressed in KB, we want bytes instead
        meminfo.cached = pair[1].to_i * 1024
      when 'Active'
        meminfo.active = pair[1].to_i * 1024
      when 'Inactive'
        meminfo.inactive = pair[1].to_i * 1024
      when 'Buffers'
        meminfo.buffers = pair[1].to_i * 1024
      when 'MemFree'
        meminfo.free = pair[1].to_i * 1024
      when 'MemTotal'
        meminfo.total = pair[1].to_i * 1024
    end
  end

  meminfo.used = meminfo.total - meminfo.free
  meminfo.available = meminfo.free + meminfo.cached + meminfo.buffers
  meminfo.percent = COMMON::usage_percent((
    meminfo.total - meminfo.available) , meminfo.total, 1)
  meminfo
end