| 376 | uint64_t memory_limit_bytes() { return g_memory_limit_bytes.load(std::memory_order_relaxed); } |
| 377 | |
| 378 | std::optional<HostMemoryReading> sample_host_memory(const MemoryProbePaths& paths, uint64_t explicit_limit) { |
| 379 | std::ifstream avail_file(paths.meminfo); |
| 380 | if (!avail_file) return {}; |
| 381 | std::optional<uint64_t> available = conversation_mem_available(avail_file); |
| 382 | if (!available) return {}; |
| 383 | if (const uint64_t arc = zfs_arc_reclaimable_bytes(paths.zfs_arcstats); arc > 0) // #5519: the ZFS ARC gives its share back |
| 384 | available = *available > std::numeric_limits<uint64_t>::max() - arc ? std::numeric_limits<uint64_t>::max() : *available + arc; |
| 385 | std::ifstream total_file(paths.meminfo); |
| 386 | std::optional<uint64_t> total; |
| 387 | if (total_file) total = meminfo_kb_bytes(total_file, "MemTotal:"); |
| 388 | CgroupScan cg; |
| 389 | if (!scan_cgroups(paths, cg)) { |
| 390 | // Without an operator cap the cgroup files are only an extra: the guard falls back to MemAvailable alone (what |
| 391 | // 0.1.41 used), once-warned. With a cap the usage cannot be told, so the sample stays unknown. |
| 392 | if (explicit_limit != 0) return {}; |
| 393 | static std::atomic<bool> warned{false}; |
| 394 | if (!warned.exchange(true)) |
| 395 | std::fprintf(stderr, "[strata] memory guard: the cgroup memory files could not be read or parsed, using " |
| 396 | "MemAvailable; set --memory-limit-mib to the container's limit\n"); |
| 397 | cg = CgroupScan{}; |
| 398 | } |
| 399 | std::optional<uint64_t> own_mapped; |
| 400 | if (!cg.limits.empty()) { // only a limit can use it |
| 401 | std::ifstream status(paths.self_status); |
| 402 | if (status) { |
| 403 | std::ostringstream text; |
| 404 | text << status.rdbuf(); |
| 405 | own_mapped = own_evictable_mapped_bytes(text.str(), locked_own_file_bytes()); |
| 406 | } |
| 407 | } |
| 408 | return combine(available, total, cg, explicit_limit, mem_credit_inactive_only(std::getenv("STRATA_MEM_CREDIT_INACTIVE")), own_mapped); |
| 409 | } |
| 410 | |
| 411 | std::optional<HostMemoryReading> sample_host_memory() { |
| 412 | #if defined(_WIN32) |