module Host
Per-host (system-wide) memory metrics. Use Host::Memory for total/used/free and swap; use Process::Metrics::Memory for per-process metrics.
Definitions
Memory
Struct for host memory snapshot. All sizes in bytes. Stored: total_size, used_size, swap_*, reclaimable_size. free_size and available_size are derived.
Implementation
Memory = Struct.new(:total_size, :used_size, :swap_total_size, :swap_used_size, :reclaimable_size) do
def to_h
super.merge(free_size: free_size, available_size: available_size)
end
alias as_json to_h
def to_json(*arguments)
as_json.to_json(*arguments)
end
# Complement of used: total_size - used_size. Same meaning on all platforms.
def free_size
total_size - used_size
end
# Memory that could be used: free_size plus reclaimable. Use this for "available" when reclaimable_size is set; otherwise equals free_size.
def available_size
free_size + (reclaimable_size || 0)
end
# Create a zero-initialized Host::Memory instance.
# @returns [Memory]
def self.zero
self.new(0, 0, nil, nil, nil)
end
# Whether host memory capture is supported on this platform.
# @returns [Boolean]
def self.supported?
false
end
# Capture current host memory. Implemented by Host::Memory::Linux or Host::Memory::Darwin (in host/memory/linux.rb, host/memory/darwin.rb).
# @returns [Memory | Nil] A Host::Memory instance, or nil if not supported or capture failed.
def self.capture
return nil
end
end
def self.name
System name from uname -a (kernel name, nodename, release, etc.). Returns nil if uname is not available.
Signature
-
returns
String, nil
Implementation
def self.name
IO.popen(["uname", "-a"], "r", &:read)&.strip
rescue Errno::ENOENT
nil
end