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

Function sample_host_memory

engine/src/core/conversation_memory.cpp:378–409  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

376uint64_t memory_limit_bytes() { return g_memory_limit_bytes.load(std::memory_order_relaxed); }
377
378std::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
411std::optional<HostMemoryReading> sample_host_memory() {
412#if defined(_WIN32)

Callers 4

file_tier_unbufferedFunction · 0.85
available_host_bytesFunction · 0.85
sampleMethod · 0.85
mainFunction · 0.85

Calls 15

meminfo_kb_bytesFunction · 0.85
scan_cgroupsFunction · 0.85
locked_own_file_bytesFunction · 0.85
mem_credit_inactive_onlyFunction · 0.85
commit_availableFunction · 0.85
commit_growthFunction · 0.85
memory_limit_bytesFunction · 0.85
combineFunction · 0.70
maxFunction · 0.50

Tested by 2

sampleMethod · 0.68
mainFunction · 0.68