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.3.0'
Class Method Summary collapse
-
.available(extended: false) ⇒ Object
The physical memory currently available, in bytes.
-
.free(extended: false) ⇒ Object
The physical memory currently available, in bytes.
- .get_by_name(mib) ⇒ Object
- .get_freebsd_swap_used(page_size) ⇒ Object
-
.load(_extended: false) ⇒ Object
A number between 0 and 100 that specifies the approximate percentage of physical memory that is in use.
-
.memory ⇒ Object
Obtain detailed memory information about your host in the form of a hash.
-
.total(extended: false) ⇒ Object
Total memory in bytes.
-
.used(extended: false) ⇒ Object
The memory, in bytes, currently in use.
Class Method Details
.available(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.
93 94 95 96 97 98 |
# File 'lib/sys/bsd/memory.rb', line 93 def available(extended: false) hash = memory available = hash[:available] extended ? available + hash[:swap_free] : available end |
.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.
83 84 85 86 |
# File 'lib/sys/bsd/memory.rb', line 83 def free(extended: false) hash = memory extended ? hash[:free] + hash[:swap_free] : hash[:free] end |
.get_by_name(mib) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/sys/bsd/memory.rb', line 120 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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/sys/bsd/memory.rb', line 141 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? = error.read_string raise SystemCallError, "kvm_openfiles: #{.empty? ? 'unknown error' : }" 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.
112 113 114 |
# File 'lib/sys/bsd/memory.rb', line 112 def load(extended: false) (used(extended: extended) / total(extended: extended).to_f).round(2) * 100 end |
.memory ⇒ Object
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 67 68 |
# 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[:available] = hash[:free] + hash[:inactive] + hash[:cache] 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.
74 75 76 77 |
# File 'lib/sys/bsd/memory.rb', line 74 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.
104 105 106 |
# File 'lib/sys/bsd/memory.rb', line 104 def used(extended: false) total(extended: extended) - free(extended: extended) end |