Module: Sys::Memory

Extended by:
FFI::Library
Defined in:
lib/sys/bsd/memory.rb,
lib/sys/version.rb,
lib/sys/osx/memory.rb,
lib/sys/linux/memory.rb,
lib/sys/windows/memory.rb

Overview

The Memory module provides various functions that return information regarding the memory on your system.

Constant Summary collapse

VERSION =

The version of the sys-memory library.

'0.2.1'

Class Method Summary collapse

Class Method Details

.free(extended: false) ⇒ Object

The physical memory currently available, in bytes. This is the amount of physical memory that can be immediately reused without having to write its contents to disk first.

If the extended option is set to true then available swap (pagefile) memory is included as part of the total.



81
82
83
84
# File 'lib/sys/bsd/memory.rb', line 81

def free(extended: false)
  hash = memory
  extended ? hash[:free] + hash[:swap_free] : hash[:free]
end

.get_by_name(mib) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sys/bsd/memory.rb', line 106

def get_by_name(mib)
  value = nil

  begin
    optr = FFI::MemoryPointer.new(:uint64_t)
    size = FFI::MemoryPointer.new(:size_t)
    size.write_int(optr.size)

    if sysctlbyname(mib, optr, size, nil, 0) < 0
      raise SystemCallError.new("sysctlbyname: #{mib}", FFI.errno)
    end

    value = optr.read_uint64
  ensure
    optr.free if optr && !optr.null?
    size.free if size && !size.null?
  end

  value
end

.get_freebsd_swap_used(page_size) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/sys/bsd/memory.rb', line 127

def get_freebsd_swap_used(page_size)
  kd = nil

  begin
    error = FFI::MemoryPointer.new(:char, 2048)
    kd = kvm_openfiles(nil, File::NULL, nil, 0, error)

    if kd.null?
      message = error.read_string
      raise SystemCallError, "kvm_openfiles: #{message.empty? ? 'unknown error' : message}"
    end

    swap = KvmSwap.new

    if kvm_getswapinfo(kd, swap.pointer, 1, 0) < 0
      raise SystemCallError, "kvm_getswapinfo: #{kvm_geterr(kd)}"
    end

    swap[:ksw_used] * page_size
  ensure
    kvm_close(kd) if kd && !kd.null?
    error.free if error && !error.null?
  end
end

.load(_extended: false) ⇒ Object

A number between 0 and 100 that specifies the approximate percentage of physical memory that is in use.

On MS Windows the extended option is ignored, but present for interface compatibility with other platforms.



98
99
100
# File 'lib/sys/bsd/memory.rb', line 98

def load(extended: false)
  (used(extended: extended) / total(extended: extended).to_f).round(2) * 100
end

.memoryObject

Obtain detailed memory information about your host in the form of a hash. Note that the exact nature of this hash is largely dependent on your operating system.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sys/bsd/memory.rb', line 42

def memory
  page_size = get_by_name('hw.pagesize')

  hash = {}
  hash[:total] = get_by_name('hw.physmem')
  hash[:active] = get_by_name('vm.stats.vm.v_active_count') * page_size
  hash[:all] = get_by_name('vm.stats.vm.v_page_count') * page_size
  hash[:cache] = get_by_name('vm.stats.vm.v_cache_count') * page_size
  hash[:free] = get_by_name('vm.stats.vm.v_free_count') * page_size
  hash[:inactive] = get_by_name('vm.stats.vm.v_inactive_count') * page_size
  hash[:wire] = get_by_name('vm.stats.vm.v_wire_count') * page_size

  if RbConfig::CONFIG['host_os'] =~ /dragonfly/i
    hash[:swap_size] = get_by_name('vm.swap_size')
    hash[:swap_free] = get_by_name('vm.swap_free')
  elsif RbConfig::CONFIG['host_os'] =~ /freebsd/i
    hash[:swap_size] = get_by_name('vm.swap_total')
    hash[:swap_free] = hash[:swap_size] - get_freebsd_swap_used(page_size)
  else
    hash[:swap_size] = get_by_name('vm.swap_total')
    hash[:swap_free] = hash[:swap_size] - get_by_name('vm.swap_reserved') # Best guess
  end

  hash
end

.total(extended: false) ⇒ Object

Total memory in bytes. By default this is only physical memory, but if the extended option is set to true then swap (pagefile) memory is included as part of the total.



72
73
74
75
# File 'lib/sys/bsd/memory.rb', line 72

def total(extended: false)
  hash = memory
  extended ? hash[:total] + hash[:swap_size] : hash[:total]
end

.used(extended: false) ⇒ Object

The memory, in bytes, currently in use. By default this is only physical memory, but if the extended option is set to true then swap (pagefile) is included in the calculation.



90
91
92
# File 'lib/sys/bsd/memory.rb', line 90

def used(extended: false)
  total(extended: extended) - free(extended: extended)
end