The room the page cache has for the experts: in a memory-limited cgroup (v2; the tightest limit of the group and its ancestors) the limit less what is not page cache (the group's memory.current - its "file" pages); otherwise MemAvailable (free + reclaimable cache). 0 when unknown.
| 2293 | // ancestors) the limit less what is not page cache (the group's memory.current - its "file" pages); otherwise |
| 2294 | // MemAvailable (free + reclaimable cache). 0 when unknown. |
| 2295 | uint64_t gate_cache_room() { |
| 2296 | #if defined(__linux__) |
| 2297 | auto read_u64 = [](const std::string& path, uint64_t& v) { |
| 2298 | std::ifstream f(path); |
| 2299 | std::string s; |
| 2300 | if (!(f >> s) || s == "max") return false; |
| 2301 | v = std::strtoull(s.c_str(), nullptr, 10); |
| 2302 | return v > 0; |
| 2303 | }; |
| 2304 | std::string rel; |
| 2305 | { |
| 2306 | std::ifstream g("/proc/self/cgroup"); |
| 2307 | std::string line; |
| 2308 | while (std::getline(g, line)) |
| 2309 | if (line.rfind("0::", 0) == 0) { rel = line.substr(3); break; } |
| 2310 | } |
| 2311 | uint64_t best = 0; |
| 2312 | if (!rel.empty()) { |
| 2313 | const std::string base = "/sys/fs/cgroup"; |
| 2314 | for (std::string at = rel;; ) { |
| 2315 | const std::string dir = base + (at == "/" ? std::string() : at); |
| 2316 | uint64_t mx = 0, cur = 0, file = 0; |
| 2317 | if (read_u64(dir + "/memory.max", mx) && read_u64(dir + "/memory.current", cur)) { |
| 2318 | std::ifstream st(dir + "/memory.stat"); |
| 2319 | std::string k; |
| 2320 | uint64_t v = 0; |
| 2321 | while (st >> k >> v) |
| 2322 | if (k == "file") { file = v; break; } |
| 2323 | const uint64_t used = cur > file ? cur - file : 0; |
| 2324 | const uint64_t room = mx > used ? mx - used : 0; |
| 2325 | if (best == 0 || room < best) best = room; |
| 2326 | } |
| 2327 | if (at.empty() || at == "/") break; |
| 2328 | const size_t cut = at.find_last_of('/'); |
| 2329 | at = cut == 0 || cut == std::string::npos ? std::string("/") : at.substr(0, cut); |
| 2330 | } |
| 2331 | } |
| 2332 | if (best > 0) return best; |
| 2333 | std::ifstream info("/proc/meminfo"); |
| 2334 | std::stringstream text; |
| 2335 | text << info.rdbuf(); |
| 2336 | return detail::meminfo_bytes(text.str(), "MemAvailable"); |
| 2337 | #else |
| 2338 | return 0; |
| 2339 | #endif |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | double FileExpertSource::cache_room_ratio() const { |
no test coverage detected