MemoryPressureMonitor.cpp [plain text]
#include "config.h"
#include "MemoryPressureMonitor.h"
#if OS(LINUX)
#include "WebProcessPool.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wtf/Threading.h>
#include <wtf/UniStdExtras.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringToIntegerConversion.h>
namespace WebKit {
static const size_t notSet = static_cast<size_t>(-1);
static const Seconds s_minPollingInterval { 1_s };
static const Seconds s_maxPollingInterval { 5_s };
static const double s_minUsedMemoryPercentageForPolling = 50;
static const double s_maxUsedMemoryPercentageForPolling = 85;
static const int s_memoryPresurePercentageThreshold = 90;
static const int s_memoryPresurePercentageThresholdCritical = 95;
static const char* s_cgroupMemoryPath = "/sys/fs/cgroup/%s/%s/%s";
static const char* s_procMeminfo = "/proc/meminfo";
static const char* s_procZoneinfo = "/proc/zoneinfo";
static const char* s_procSelfCgroup = "/proc/self/cgroup";
static const unsigned maxCgroupPath = 4096;
#define CGROUP_V2_HIERARCHY 0
#define CGROUP_NAME_BUFFER_SIZE 40
#define MEMINFO_TOKEN_BUFFER_SIZE 50
#define STRINGIFY_EXPANDED(val) #val
#define STRINGIFY(val) STRINGIFY_EXPANDED(val)
#define ZONEINFO_TOKEN_BUFFER_SIZE 128
static size_t lowWatermarkPages(FILE* zoneInfoFile)
{
size_t low = 0;
size_t sumLow = 0;
char buffer[ZONEINFO_TOKEN_BUFFER_SIZE + 1];
bool inNormalZone = false;
if (!zoneInfoFile || fseek(zoneInfoFile, 0, SEEK_SET))
return notSet;
while (!feof(zoneInfoFile)) {
int r;
r = fscanf(zoneInfoFile, " Node %*u, zone %" STRINGIFY(ZONEINFO_TOKEN_BUFFER_SIZE) "[^\n]\n", buffer);
if (r == 2 && !strcmp(buffer, "Normal"))
inNormalZone = true;
r = fscanf(zoneInfoFile, "%" STRINGIFY(ZONEINFO_TOKEN_BUFFER_SIZE) "s", buffer);
if (r == 1 && inNormalZone && !strcmp(buffer, "low")) {
r = fscanf(zoneInfoFile, "%zu", &low);
if (r == 1) {
sumLow += low;
continue;
}
}
}
return sumLow;
}
static inline size_t systemPageSize()
{
static size_t pageSize = 0;
if (!pageSize)
pageSize = sysconf(_SC_PAGE_SIZE);
return pageSize;
}
static size_t calculateMemoryAvailable(size_t memoryFree, size_t activeFile, size_t inactiveFile, size_t slabReclaimable, FILE* zoneInfoFile)
{
if (memoryFree == notSet || activeFile == notSet || inactiveFile == notSet || slabReclaimable == notSet)
return notSet;
size_t lowWatermark = lowWatermarkPages(zoneInfoFile);
if (lowWatermark == notSet)
return notSet;
lowWatermark *= systemPageSize() / KB;
lowWatermark *= systemPageSize() / KB;
size_t memoryAvailable = memoryFree - lowWatermark;
size_t pageCache = activeFile + inactiveFile;
pageCache -= std::min(pageCache / 2, lowWatermark);
memoryAvailable += pageCache;
memoryAvailable += slabReclaimable - std::min(slabReclaimable / 2, lowWatermark);
return memoryAvailable;
}
FILE* getCgroupFile(CString cgroupControllerName, CString cgroupControllerPath, CString cgroupFileName)
{
char cgroupPath[maxCgroupPath];
snprintf(cgroupPath, maxCgroupPath, s_cgroupMemoryPath, cgroupControllerName.data(), cgroupControllerPath.data(), cgroupFileName.data());
LOG_VERBOSE(MemoryPressure, "Open: %s", cgroupPath);
FILE* file = fopen(cgroupPath, "r");
if (file)
setbuf(file, nullptr);
return file;
}
static CString getCgroupControllerPath(FILE* cgroupControllerFile, const char* controllerName)
{
if (!cgroupControllerFile || fseek(cgroupControllerFile, 0, SEEK_SET))
return CString();
CString cgroupMemoryControllerPath;
while (!feof(cgroupControllerFile)) {
unsigned hierarchyId;
char name[CGROUP_NAME_BUFFER_SIZE + 1];
char path[maxCgroupPath + 1];
name[0] = path[0] = '\0';
int scanResult = fscanf(cgroupControllerFile, "%u:", &hierarchyId);
if (scanResult != 1)
return CString();
if (hierarchyId == CGROUP_V2_HIERARCHY) {
scanResult = fscanf(cgroupControllerFile, ":%" STRINGIFY(PATH_MAX) "[^\n]", path);
if (scanResult != 1)
return CString();
} else {
scanResult = fscanf(cgroupControllerFile, "%" STRINGIFY(CGROUP_NAME_BUFFER_SIZE) "[^:]:%" STRINGIFY(PATH_MAX) "[^\n]", name, path);
if (scanResult != 2)
return CString();
}
if (!strcmp(name, controllerName)) {
cgroupMemoryControllerPath = CString(path);
LOG_VERBOSE(MemoryPressure, "memoryControllerName - %s namespace (hierarchy: %d): %s", controllerName, hierarchyId, cgroupMemoryControllerPath.data());
return cgroupMemoryControllerPath;
}
if (!strcmp(name, "name=systemd")) {
cgroupMemoryControllerPath = CString(path);
LOG_VERBOSE(MemoryPressure, "memoryControllerName - systemd namespace (hierarchy: %d): %s", hierarchyId, cgroupMemoryControllerPath.data());
return cgroupMemoryControllerPath;
}
if (!strcmp(name, "")) {
cgroupMemoryControllerPath = CString(path);
LOG_VERBOSE(MemoryPressure, "memoryControllerName - empty namespace (hierarchy: %d): %s", hierarchyId, cgroupMemoryControllerPath.data());
return cgroupMemoryControllerPath;
}
}
return CString();
}
static int systemMemoryUsedAsPercentage(FILE* memInfoFile, FILE* zoneInfoFile, CGroupMemoryController* memoryController)
{
if (!memInfoFile || fseek(memInfoFile, 0, SEEK_SET))
return -1;
size_t memoryAvailable, memoryTotal, memoryFree, activeFile, inactiveFile, slabReclaimable;
memoryAvailable = memoryTotal = memoryFree = activeFile = inactiveFile = slabReclaimable = notSet;
while (!feof(memInfoFile)) {
char token[MEMINFO_TOKEN_BUFFER_SIZE + 1] = { 0 };
size_t amount;
if (fscanf(memInfoFile, "%" STRINGIFY(MEMINFO_TOKEN_BUFFER_SIZE) "s%zukB", token, &amount) != 2)
continue;
if (!strcmp(token, "MemTotal:"))
memoryTotal = amount;
else if (!strcmp(token, "MemFree:"))
memoryFree = amount;
else if (!strcmp(token, "MemAvailable:"))
memoryAvailable = amount;
else if (!strcmp(token, "Active(file):"))
activeFile = amount;
else if (!strcmp(token, "Inactive(file):"))
inactiveFile = amount;
else if (!strcmp(token, "SReclaimable:"))
slabReclaimable = amount;
if (memoryTotal != notSet && memoryFree != notSet && activeFile != notSet && inactiveFile != notSet && slabReclaimable != notSet)
break;
}
if (!memoryTotal || memoryTotal == notSet)
return -1;
if (memoryAvailable == notSet) {
memoryAvailable = calculateMemoryAvailable(memoryFree, activeFile, inactiveFile, slabReclaimable, zoneInfoFile);
if (memoryAvailable == notSet)
return -1;
}
if (memoryAvailable > memoryTotal)
return -1;
int memoryUsagePercentage = ((memoryTotal - memoryAvailable) * 100) / memoryTotal;
LOG_VERBOSE(MemoryPressure, "MemoryPressureMonitor::memory: real (memory total=%zu MB) (memory available=%zu MB) (memory usage percentage=%d MB)", memoryTotal, memoryAvailable, memoryUsagePercentage);
if (memoryController->isActive()) {
memoryTotal = memoryController->getMemoryTotalWithCgroup();
size_t memoryUsage = memoryController->getMemoryUsageWithCgroup();
if (memoryTotal != notSet && memoryUsage != notSet) {
int memoryUsagePercentageWithCgroup = 100 * ((float) memoryUsage / (float) memoryTotal);
LOG_VERBOSE(MemoryPressure, "MemoryPressureMonitor::memory: cgroup (memory total=%zu bytes) (memory usage=%zu bytes) (memory usage percentage=%d bytes)", memoryTotal, memoryUsage, memoryUsagePercentageWithCgroup);
if (memoryUsagePercentageWithCgroup > memoryUsagePercentage)
memoryUsagePercentage = memoryUsagePercentageWithCgroup;
}
}
LOG_VERBOSE(MemoryPressure, "MemoryPressureMonitor::memory: memoryUsagePercentage (%d)", memoryUsagePercentage);
return memoryUsagePercentage;
}
static inline Seconds pollIntervalForUsedMemoryPercentage(int usedPercentage)
{
if (usedPercentage < s_minUsedMemoryPercentageForPolling)
return s_maxPollingInterval;
if (usedPercentage >= s_maxUsedMemoryPercentageForPolling)
return s_minPollingInterval;
return s_minPollingInterval + (s_maxPollingInterval - s_minPollingInterval) *
((s_maxUsedMemoryPercentageForPolling - usedPercentage) / (s_maxUsedMemoryPercentageForPolling - s_minUsedMemoryPercentageForPolling));
}
MemoryPressureMonitor& MemoryPressureMonitor::singleton()
{
static NeverDestroyed<MemoryPressureMonitor> memoryMonitor;
return memoryMonitor;
}
struct FileHandleDeleter {
void operator()(FILE* f) { fclose(f); }
};
using FileHandle = std::unique_ptr<FILE, FileHandleDeleter>;
static bool tryOpeningForUnbufferedReading(FileHandle& handle, const char* filePath)
{
if (handle)
return true;
if (auto* f = fopen(filePath, "r")) {
setbuf(f, nullptr);
handle.reset(f);
return true;
}
return false;
}
void MemoryPressureMonitor::start()
{
if (m_started)
return;
m_started = true;
Thread::create("MemoryPressureMonitor", [] {
FileHandle memInfoFile, zoneInfoFile, cgroupControllerFile;
CGroupMemoryController memoryController = CGroupMemoryController();
Seconds pollInterval = s_maxPollingInterval;
while (true) {
sleep(pollInterval);
if (!tryOpeningForUnbufferedReading(memInfoFile, s_procMeminfo))
continue;
tryOpeningForUnbufferedReading(zoneInfoFile, s_procZoneinfo);
tryOpeningForUnbufferedReading(cgroupControllerFile, s_procSelfCgroup);
CString cgroupMemoryControllerPath = getCgroupControllerPath(cgroupControllerFile.get(), "memory");
memoryController.setMemoryControllerPath(cgroupMemoryControllerPath);
int usedPercentage = systemMemoryUsedAsPercentage(memInfoFile.get(), zoneInfoFile.get(), &memoryController);
if (usedPercentage == -1) {
WTFLogAlways("Failed to get the memory usage");
pollInterval = s_maxPollingInterval;
continue;
}
if (usedPercentage >= s_memoryPresurePercentageThreshold) {
bool isCritical = (usedPercentage >= s_memoryPresurePercentageThresholdCritical);
for (auto* processPool : WebProcessPool::allProcessPools())
processPool->sendMemoryPressureEvent(isCritical);
}
pollInterval = pollIntervalForUsedMemoryPercentage(usedPercentage);
}
})->detach();
}
void CGroupMemoryController::setMemoryControllerPath(CString memoryControllerPath)
{
if (memoryControllerPath == m_cgroupMemoryControllerPath)
return;
m_cgroupMemoryControllerPath = memoryControllerPath;
disposeMemoryController();
m_cgroupV2MemoryCurrentFile = getCgroupFile("/", memoryControllerPath, CString("memory.current"));
m_cgroupV2MemoryMemswMaxFile = getCgroupFile("/", memoryControllerPath, CString("memory.memsw.max"));
m_cgroupV2MemoryMaxFile = getCgroupFile("/", memoryControllerPath, CString("memory.max"));
m_cgroupV2MemoryHighFile = getCgroupFile("/", memoryControllerPath, CString("memory.high"));
m_cgroupMemoryMemswLimitInBytesFile = getCgroupFile("memory", memoryControllerPath, CString("memory.memsw.limit_in_bytes"));
m_cgroupMemoryLimitInBytesFile = getCgroupFile("memory", memoryControllerPath, CString("memory.limit_in_bytes"));
m_cgroupMemoryUsageInBytesFile = getCgroupFile("memory", memoryControllerPath, CString("memory.usage_in_bytes"));
}
void CGroupMemoryController::disposeMemoryController()
{
if (m_cgroupMemoryMemswLimitInBytesFile)
fclose(m_cgroupMemoryMemswLimitInBytesFile);
if (m_cgroupMemoryLimitInBytesFile)
fclose(m_cgroupMemoryLimitInBytesFile);
if (m_cgroupMemoryUsageInBytesFile)
fclose(m_cgroupMemoryUsageInBytesFile);
if (m_cgroupV2MemoryMemswMaxFile)
fclose(m_cgroupV2MemoryMemswMaxFile);
if (m_cgroupV2MemoryMaxFile)
fclose(m_cgroupV2MemoryMaxFile);
if (m_cgroupV2MemoryHighFile)
fclose(m_cgroupV2MemoryHighFile);
}
size_t CGroupMemoryController::getCgroupFileValue(FILE *file)
{
if (!file || fseek(file, 0, SEEK_SET))
return notSet;
size_t value;
return (fscanf(file, "%zu", &value) == 1) ? value : notSet;
}
size_t CGroupMemoryController::getMemoryTotalWithCgroup()
{
size_t value = notSet;
value = getCgroupFileValue(m_cgroupV2MemoryMemswMaxFile);
if (value != notSet)
return value;
value = getCgroupFileValue(m_cgroupV2MemoryMaxFile);
size_t valueHigh = getCgroupFileValue(m_cgroupV2MemoryHighFile);
if (value != notSet && valueHigh != notSet) {
value = std::min(value, valueHigh);
return value;
}
if (valueHigh != notSet)
return valueHigh;
if (value != notSet)
return value;
value = getCgroupFileValue(m_cgroupMemoryMemswLimitInBytesFile);
if (value != notSet)
return value;
value = getCgroupFileValue(m_cgroupMemoryLimitInBytesFile);
if (value != notSet)
return value;
return value;
}
size_t CGroupMemoryController::getMemoryUsageWithCgroup()
{
size_t value = notSet;
value = getCgroupFileValue(m_cgroupV2MemoryCurrentFile);
if (value != notSet)
return value;
value = getCgroupFileValue(m_cgroupMemoryUsageInBytesFile);
if (value != notSet)
return value;
return notSet;
}
}
#endif // OS(LINUX)