| 441 | #endif |
| 442 | |
| 443 | void getMachineRAMInfo(MachineRAMInfo& memInfo) { |
| 444 | #if defined(__linux__) |
| 445 | std::ifstream zoneInfoFileStream("/proc/zoneinfo", std::ifstream::in); |
| 446 | int64_t lowWatermark = 0; |
| 447 | if (!zoneInfoFileStream.good()) { |
| 448 | TraceEvent(SevWarnAlways, "GetMachineZoneInfo").GetLastError(); |
| 449 | } else { |
| 450 | std::stringstream zoneInfoStream; |
| 451 | zoneInfoStream << zoneInfoFileStream.rdbuf(); |
| 452 | lowWatermark = getLowWatermark(zoneInfoStream) * 4; // Convert from 4K pages to KB |
| 453 | } |
| 454 | |
| 455 | std::ifstream fileStream("/proc/meminfo", std::ifstream::in); |
| 456 | if (!fileStream.good()) { |
| 457 | TraceEvent(SevError, "GetMachineMemInfo").GetLastError(); |
| 458 | throw platform_error(); |
| 459 | } |
| 460 | |
| 461 | std::map<StringRef, int64_t> request = { |
| 462 | { LiteralStringRef("MemTotal:"), 0 }, { LiteralStringRef("MemFree:"), 0 }, |
| 463 | { LiteralStringRef("MemAvailable:"), -1 }, { LiteralStringRef("Active(file):"), 0 }, |
| 464 | { LiteralStringRef("Inactive(file):"), 0 }, { LiteralStringRef("SwapTotal:"), 0 }, |
| 465 | { LiteralStringRef("SwapFree:"), 0 }, { LiteralStringRef("SReclaimable:"), 0 }, |
| 466 | }; |
| 467 | |
| 468 | std::stringstream memInfoStream; |
| 469 | memInfoStream << fileStream.rdbuf(); |
| 470 | getMemoryInfo(request, memInfoStream); |
| 471 | |
| 472 | int64_t memFree = request[LiteralStringRef("MemFree:")]; |
| 473 | int64_t pageCache = request[LiteralStringRef("Active(file):")] + request[LiteralStringRef("Inactive(file):")]; |
| 474 | int64_t slabReclaimable = request[LiteralStringRef("SReclaimable:")]; |
| 475 | int64_t usedSwap = request[LiteralStringRef("SwapTotal:")] - request[LiteralStringRef("SwapFree:")]; |
| 476 | |
| 477 | memInfo.total = 1024 * request[LiteralStringRef("MemTotal:")]; |
| 478 | if (request[LiteralStringRef("MemAvailable:")] != -1) { |
| 479 | memInfo.available = 1024 * (request[LiteralStringRef("MemAvailable:")] - usedSwap); |
| 480 | } else { |
| 481 | memInfo.available = |
| 482 | 1024 * (std::max<int64_t>(0, |
| 483 | (memFree - lowWatermark) + std::max(pageCache - lowWatermark, pageCache / 2) + |
| 484 | std::max(slabReclaimable - lowWatermark, slabReclaimable / 2)) - |
| 485 | usedSwap); |
| 486 | } |
| 487 | |
| 488 | memInfo.committed = memInfo.total - memInfo.available; |
| 489 | #elif defined(__FreeBSD__) |
| 490 | int status; |
| 491 | |
| 492 | u_int page_size; |
| 493 | u_int free_count; |
| 494 | u_int active_count; |
| 495 | u_int inactive_count; |
| 496 | u_int wire_count; |
| 497 | |
| 498 | size_t uint_size; |
| 499 | |
| 500 | uint_size = sizeof(page_size); |
no test coverage detected