MCPcopy Create free account
hub / github.com/Niko1221/Strata / host_available_memory

Function host_available_memory

engine/src/core/expert_source.cpp:432–575  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

430namespace detail {
431
432bool host_available_memory(HostMemory& m, const std::string& meminfo, const std::string& self_cgroup,
433 const std::string& cgroup_root, const std::string& zfs_arcstats) {
434 m = HostMemory{};
435#if defined(_WIN32)
436 (void) meminfo; (void) self_cgroup; (void) cgroup_root; (void) zfs_arcstats;
437 MEMORYSTATUSEX status{};
438 status.dwLength = sizeof(status);
439 if (!GlobalMemoryStatusEx(&status)) return false;
440 m.available = (uint64_t) status.ullAvailPhys;
441 // #5742: with the room the page files may still grow by (the DeepSeek engine's rule, ds4_os.hpp); the clamp of a RAM
442 // budget and the arena read this
443 m.commit = strata::platform::commit_available((uint64_t) status.ullAvailPageFile,
444 strata::platform::commit_growth((uint64_t) status.ullTotalPhys));
445 return m.available > 0;
446#elif defined(__linux__)
447 // MemAvailable includes reclaimable page cache, unlike _SC_AVPHYS_PAGES.
448 std::ifstream info(meminfo);
449 std::string line;
450 uint64_t bytes = 0;
451 std::optional<uint64_t> mem_total; // a limit below it is a container's (its /proc/meminfo is the host's)
452 while (std::getline(info, line)) {
453 std::istringstream fields(line);
454 std::string key, unit;
455 uint64_t value = 0;
456 if (fields >> key >> value >> unit && key == "MemAvailable:" && unit == "kB" &&
457 value <= std::numeric_limits<uint64_t>::max() / 1024) bytes = value * 1024;
458 else if (key == "MemTotal:" && unit == "kB" && value <= std::numeric_limits<uint64_t>::max() / 1024) mem_total = value * 1024;
459 }
460 if (bytes == 0) { m.why = "MemAvailable is not in /proc/meminfo"; return false; }
461 // #5519: the ZFS ARC's share above its floor, which MemAvailable leaves out (the cgroup room below still bounds a container)
462 if (const uint64_t arc = strata::core::zfs_arc_reclaimable_bytes(zfs_arcstats); arc > 0)
463 bytes = bytes > std::numeric_limits<uint64_t>::max() - arc ? std::numeric_limits<uint64_t>::max() : bytes + arc;
464 const bool inactive_only = strata::core::mem_credit_inactive_only(std::getenv("STRATA_MEM_CREDIT_INACTIVE"));
465 // Account for the tightest cgroup ancestor limit when its normal mount is visible.
466 // This is a point-in-time guard, not a reservation against concurrent allocations.
467 std::ifstream groups(self_cgroup);
468 if (!groups) { m.why = "/proc/self/cgroup cannot be read"; return false; }
469 const std::filesystem::path root(cgroup_root);
470 bool v2 = false;
471 std::string v1_path; // the memory controller's group (cgroup v1), when there is no v2 line
472 while (std::getline(groups, line)) {
473 if (line.rfind("0::/", 0) != 0) {
474 // v1: "N:controller[,controller]:/path"
475 const size_t c1 = line.find(':'), c2 = c1 == std::string::npos ? c1 : line.find(':', c1 + 1);
476 if (c2 == std::string::npos) continue;
477 std::istringstream ctl(line.substr(c1 + 1, c2 - c1 - 1));
478 for (std::string c; std::getline(ctl, c, ',');)
479 if (c == "memory") v1_path = line.substr(c2 + 1);
480 continue;
481 }
482 // A hybrid host (cgroup v1 controllers plus a "0::/..." line for the unified hierarchy; Docker on a v1 host, e.g.
483 // Vast's containers): the root is the v1 tree, it has no cgroup.controllers, and the "0::" path is not in it.
484 // That line is no v2 limit then; the v1 branch below reads the memory controller. (Before, this returned false
485 // and --resident-budget-gib was ignored: "cannot determine available memory".)
486 if (!std::filesystem::exists(root / "cgroup.controllers")) continue;
487 auto path = (root / line.substr(4)).lexically_normal();
488 if (path.string().rfind(root.string(), 0) != 0) { m.why = "the process's cgroup path leaves the cgroup mount"; return false; }
489 // #5196: a process whose "0::" path is not in this mount (a container without a cgroup namespace of its own: its

Callers 4

available_memory_bytesFunction · 0.70
openMethod · 0.70
test_host_memoryFunction · 0.50
test_host_memoryFunction · 0.50

Calls 15

commit_availableFunction · 0.85
commit_growthFunction · 0.85
mem_credit_inactive_onlyFunction · 0.85
arena_cgroup_roomFunction · 0.85
arena_cgroup_room_v1Function · 0.85
read_cgroup_memory_statFunction · 0.70
cgroup_available_bytesFunction · 0.70
maxFunction · 0.50
existsFunction · 0.50
minFunction · 0.50
findMethod · 0.45

Tested by 2

test_host_memoryFunction · 0.40
test_host_memoryFunction · 0.40