()
| 58 | * working-set convention `docker stats` uses. |
| 59 | */ |
| 60 | export function cgroupMemoryAvailable(): number | null { |
| 61 | if (process.platform !== 'linux') return null; |
| 62 | // v2 unified hierarchy |
| 63 | const v2Max = readCgroupBytes('/sys/fs/cgroup/memory.max'); |
| 64 | if (v2Max !== null) { |
| 65 | const current = readCgroupBytes('/sys/fs/cgroup/memory.current') ?? 0; |
| 66 | const reclaimable = readInactiveFile('/sys/fs/cgroup/memory.stat'); |
| 67 | return Math.max(0, v2Max - Math.max(0, current - reclaimable)); |
| 68 | } |
| 69 | // v1 |
| 70 | const v1Limit = readCgroupBytes('/sys/fs/cgroup/memory/memory.limit_in_bytes'); |
| 71 | // v1 reports "no limit" as a huge sentinel (~PAGE_COUNTER_MAX); treat |
| 72 | // anything at or beyond half the address-space-ish range as uncontained. |
| 73 | if (v1Limit !== null && v1Limit < 2 ** 60) { |
| 74 | const usage = readCgroupBytes('/sys/fs/cgroup/memory/memory.usage_in_bytes') ?? 0; |
| 75 | const reclaimable = readInactiveFile('/sys/fs/cgroup/memory/memory.stat'); |
| 76 | return Math.max(0, v1Limit - Math.max(0, usage - reclaimable)); |
| 77 | } |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Reclaimable-inclusive available memory on macOS, or null elsewhere / on |
no test coverage detected