|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2025-2026, by Samuel Williams. |
| 5 | + |
| 6 | +module Process |
| 7 | + module Metrics |
| 8 | + module Host |
| 9 | + # Linux implementation of host memory metrics. |
| 10 | + # Uses cgroups v2 (memory.max, memory.current) or cgroups v1 (memory.limit_in_bytes, memory.usage_in_bytes) when in a container; |
| 11 | + # otherwise reads /proc/meminfo (MemTotal, MemAvailable/MemFree, SwapTotal/SwapFree). Parses meminfo once per capture and reuses it. |
| 12 | + class Memory::Linux |
| 13 | + # Threshold for distinguishing actual memory limits from "unlimited" sentinel values in cgroups v1. |
| 14 | + # In cgroups v1, when memory.limit_in_bytes is set to unlimited (by writing -1), the kernel stores a very large sentinel near 2^63. |
| 15 | + # Any value >= 2^60 (1 exabyte) is treated as unlimited and we fall back to /proc/meminfo. |
| 16 | + # Reference: https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt |
| 17 | + CGROUP_V1_UNLIMITED_THRESHOLD = 2**60 |
| 18 | + |
| 19 | + def initialize |
| 20 | + @meminfo = false |
| 21 | + end |
| 22 | + |
| 23 | + # Capture current host memory. Reads total and used (from cgroup or meminfo), computes free, and parses swap from meminfo. |
| 24 | + # @returns [Host::Memory | Nil] |
| 25 | + def capture |
| 26 | + total = capture_total |
| 27 | + return nil unless total && total.positive? |
| 28 | + |
| 29 | + used = capture_used(total) |
| 30 | + used = 0 if used.nil? || used.negative? |
| 31 | + used = [used, total].min |
| 32 | + free = total - used |
| 33 | + |
| 34 | + swap_total, swap_used = capture_swap |
| 35 | + |
| 36 | + return Host::Memory.new(total, used, free, swap_total, swap_used) |
| 37 | + end |
| 38 | + |
| 39 | + private |
| 40 | + |
| 41 | + # Memoized /proc/meminfo contents. Used for total (MemTotal), used (via MemAvailable), and swap when not in a cgroup. |
| 42 | + # @returns [String | Nil] |
| 43 | + def meminfo |
| 44 | + if @meminfo == false |
| 45 | + @meminfo = File.read("/proc/meminfo") rescue nil |
| 46 | + end |
| 47 | + |
| 48 | + return @meminfo |
| 49 | + end |
| 50 | + |
| 51 | + # Total memory in bytes: cgroups v2 memory.max, cgroups v1 memory.limit_in_bytes (if < threshold), else MemTotal from meminfo. |
| 52 | + # @returns [Integer | Nil] |
| 53 | + def capture_total |
| 54 | + if File.exist?("/sys/fs/cgroup/memory.max") |
| 55 | + limit = File.read("/sys/fs/cgroup/memory.max").strip |
| 56 | + return limit.to_i if limit != "max" |
| 57 | + end |
| 58 | + |
| 59 | + if File.exist?("/sys/fs/cgroup/memory/memory.limit_in_bytes") |
| 60 | + limit = File.read("/sys/fs/cgroup/memory/memory.limit_in_bytes").strip.to_i |
| 61 | + return limit if limit > 0 && limit < CGROUP_V1_UNLIMITED_THRESHOLD |
| 62 | + end |
| 63 | + |
| 64 | + unless meminfo = self.meminfo |
| 65 | + return nil |
| 66 | + end |
| 67 | + |
| 68 | + meminfo.each_line do |line| |
| 69 | + if /MemTotal:\s*(?<total>\d+)\s*kB/ =~ line |
| 70 | + return $~[:total].to_i * 1024 |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + return nil |
| 75 | + end |
| 76 | + |
| 77 | + # Current memory usage in bytes: cgroups v2 memory.current, cgroups v1 memory.usage_in_bytes, or total - MemAvailable from meminfo. |
| 78 | + # @parameter total [Integer] Total memory (used to compute used from MemAvailable when not in cgroup). |
| 79 | + # @returns [Integer | Nil] |
| 80 | + def capture_used(total) |
| 81 | + if File.exist?("/sys/fs/cgroup/memory.current") |
| 82 | + current = File.read("/sys/fs/cgroup/memory.current").strip.to_i |
| 83 | + return current |
| 84 | + end |
| 85 | + |
| 86 | + if File.exist?("/sys/fs/cgroup/memory/memory.usage_in_bytes") |
| 87 | + limit = File.read("/sys/fs/cgroup/memory/memory.limit_in_bytes").strip.to_i |
| 88 | + if limit > 0 && limit < CGROUP_V1_UNLIMITED_THRESHOLD |
| 89 | + return File.read("/sys/fs/cgroup/memory/memory.usage_in_bytes").strip.to_i |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + unless meminfo = self.meminfo |
| 94 | + return nil |
| 95 | + end |
| 96 | + |
| 97 | + available_kb = meminfo[/MemAvailable:\s*(\d+)\s*kB/, 1]&.to_i |
| 98 | + available_kb ||= meminfo[/MemFree:\s*(\d+)\s*kB/, 1]&.to_i |
| 99 | + return nil unless available_kb |
| 100 | + |
| 101 | + return [total - (available_kb * 1024), 0].max |
| 102 | + end |
| 103 | + |
| 104 | + # Swap total and used in bytes from meminfo (SwapTotal, SwapFree). |
| 105 | + # @returns [Array(Integer, Integer)] [swap_total_bytes, swap_used_bytes], or [nil, nil] if no swap. |
| 106 | + def capture_swap |
| 107 | + return [nil, nil] unless meminfo |
| 108 | + swap_total_kb = meminfo[/SwapTotal:\s*(\d+)\s*kB/, 1]&.to_i |
| 109 | + swap_free_kb = meminfo[/SwapFree:\s*(\d+)\s*kB/, 1]&.to_i |
| 110 | + |
| 111 | + return [nil, nil] unless swap_total_kb |
| 112 | + |
| 113 | + swap_total_bytes = swap_total_kb * 1024 |
| 114 | + swap_used_bytes = (swap_total_kb - (swap_free_kb || 0)) * 1024 |
| 115 | + |
| 116 | + return swap_total_bytes, swap_used_bytes |
| 117 | + end |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | +end |
| 122 | + |
| 123 | +# Wire Host::Memory to this implementation on Linux. |
| 124 | +class << Process::Metrics::Host::Memory |
| 125 | + def capture |
| 126 | + Process::Metrics::Host::Memory::Linux.new.capture |
| 127 | + end |
| 128 | + |
| 129 | + def supported? |
| 130 | + File.exist?("/proc/meminfo") |
| 131 | + end |
| 132 | +end |
0 commit comments