diff options
author | Matt Lord <mattalord@gmail.com> | 2019-03-07 10:58:22 -0500 |
---|---|---|
committer | Matt Lord <mattalord@gmail.com> | 2019-04-03 10:16:37 -0400 |
commit | e5998f6c628adcb9b82fd70839ab892e1d01f265 (patch) | |
tree | f885c396bff2f6401b1a89eff6b3993ae771add0 | |
parent | 57edce7396271531dde4499458b22c9cde1f03d4 (diff) | |
download | mongo-e5998f6c628adcb9b82fd70839ab892e1d01f265.tar.gz |
SERVER-16571 Use Actual Memory Constraint vs. Total System Memory When They Differ
(cherry picked from commit d535bce1bb7df20158fad965142d6b802ea95c60)
-rw-r--r-- | src/mongo/db/SConscript | 3 | ||||
-rw-r--r-- | src/mongo/db/commands/generic_servers.cpp | 3 | ||||
-rw-r--r-- | src/mongo/db/log_process_details.cpp | 5 | ||||
-rw-r--r-- | src/mongo/util/processinfo.h | 11 | ||||
-rw-r--r-- | src/mongo/util/processinfo_freebsd.cpp | 1 | ||||
-rw-r--r-- | src/mongo/util/processinfo_linux.cpp | 18 | ||||
-rw-r--r-- | src/mongo/util/processinfo_openbsd.cpp | 1 | ||||
-rw-r--r-- | src/mongo/util/processinfo_osx.cpp | 1 | ||||
-rw-r--r-- | src/mongo/util/processinfo_solaris.cpp | 1 | ||||
-rw-r--r-- | src/mongo/util/processinfo_windows.cpp | 1 |
10 files changed, 43 insertions, 2 deletions
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript index e98200213d0..76c666b0f0d 100644 --- a/src/mongo/db/SConscript +++ b/src/mongo/db/SConscript @@ -1749,6 +1749,9 @@ env.Library( '$BUILD_DIR/mongo/base', '$BUILD_DIR/mongo/util/net/network', ], + LIBDEPS_PRIVATE=[ + '$BUILD_DIR/mongo/util/processinfo', + ], ) env.Library( diff --git a/src/mongo/db/commands/generic_servers.cpp b/src/mongo/db/commands/generic_servers.cpp index 93ad3389461..a4e65bdf38d 100644 --- a/src/mongo/db/commands/generic_servers.cpp +++ b/src/mongo/db/commands/generic_servers.cpp @@ -123,7 +123,8 @@ public: bSys.appendDate("currentTime", jsTime()); bSys.append("hostname", prettyHostName()); bSys.append("cpuAddrSize", p.getAddrSize()); - bSys.append("memSizeMB", static_cast<unsigned>(p.getMemSizeMB())); + bSys.append("memSizeMB", static_cast<unsigned>(p.getSystemMemSizeMB())); + bSys.append("memLimitMB", static_cast<unsigned>(p.getMemSizeMB())); bSys.append("numCores", p.getNumCores()); bSys.append("cpuArch", p.getArch()); bSys.append("numaEnabled", p.hasNumaEnabled()); diff --git a/src/mongo/db/log_process_details.cpp b/src/mongo/db/log_process_details.cpp index 22eaca0cd20..6460ab12324 100644 --- a/src/mongo/db/log_process_details.cpp +++ b/src/mongo/db/log_process_details.cpp @@ -56,6 +56,11 @@ void logProcessDetails() { log() << mongodVersion(vii); vii.logBuildInfo(); + if (ProcessInfo::getMemSizeMB() < ProcessInfo::getSystemMemSizeMB()) { + log() << ProcessInfo::getMemSizeMB() << " MB of memory available to the process out of " + << ProcessInfo::getSystemMemSizeMB() << " MB total system memory"; + } + printCommandLineOpts(); } diff --git a/src/mongo/util/processinfo.h b/src/mongo/util/processinfo.h index 3af9ebb0f5f..408e633bd9b 100644 --- a/src/mongo/util/processinfo.h +++ b/src/mongo/util/processinfo.h @@ -87,9 +87,16 @@ public: } /** - * Get the total amount of system memory in MB + * Get the size of total memory available to the process in MB */ static unsigned long long getMemSizeMB() { + return sysInfo().memLimit / (1024 * 1024); + } + + /** + * Get the total memory available on the machine in MB + */ + static unsigned long long getSystemMemSizeMB() { return sysInfo().memSize / (1024 * 1024); } @@ -199,6 +206,7 @@ private: std::string osVersion; unsigned addrSize; unsigned long long memSize; + unsigned long long memLimit; unsigned numCores; unsigned long long pageSize; std::string cpuArch; @@ -220,6 +228,7 @@ private: SystemInfo() : addrSize(0), memSize(0), + memLimit(0), numCores(0), pageSize(0), hasNuma(false), diff --git a/src/mongo/util/processinfo_freebsd.cpp b/src/mongo/util/processinfo_freebsd.cpp index 075fea72ffd..7657a85c590 100644 --- a/src/mongo/util/processinfo_freebsd.cpp +++ b/src/mongo/util/processinfo_freebsd.cpp @@ -146,6 +146,7 @@ void ProcessInfo::SystemInfo::collectSystemInfo() { uintptr_t defaultNum = 1; status = getSysctlByNameWithDefault("hw.physmem", defaultNum, &numBuffer); memSize = numBuffer; + memLimit = memSize; if (status != 0) log() << "Unable to collect Physical Memory. (errno: " << status << " msg: " << strerror(status) << ")"; diff --git a/src/mongo/util/processinfo_linux.cpp b/src/mongo/util/processinfo_linux.cpp index cb91e199d74..67cd7fea0ca 100644 --- a/src/mongo/util/processinfo_linux.cpp +++ b/src/mongo/util/processinfo_linux.cpp @@ -415,6 +415,23 @@ public: } return 0; } + + /** + * Get memory limit for the process. + * If memory is being limited by the applied control group and it's less + * than the OS system memory (default cgroup limit is ulonglong max) let's + * return the actual memory we'll have available to the process. + */ + static unsigned long long getMemorySizeLimit() { + unsigned long long systemMemBytes = getSystemMemorySize(); + unsigned long long cgroupMemBytes = 0; + std::string cgmemlimit = readLineFromFile("/sys/fs/cgroup/memory/memory.limit_in_bytes"); + if (!cgmemlimit.empty() && + mongo::parseNumberFromString(cgmemlimit, &cgroupMemBytes).isOK()) { + return std::min(systemMemBytes, cgroupMemBytes); + } + return systemMemBytes; + } }; @@ -490,6 +507,7 @@ void ProcessInfo::SystemInfo::collectSystemInfo() { osName = distroName; osVersion = distroVersion; memSize = LinuxSysHelper::getSystemMemorySize(); + memLimit = LinuxSysHelper::getMemorySizeLimit(); addrSize = sizeof(void*) * CHAR_BIT; numCores = cpuCount; pageSize = static_cast<unsigned long long>(sysconf(_SC_PAGESIZE)); diff --git a/src/mongo/util/processinfo_openbsd.cpp b/src/mongo/util/processinfo_openbsd.cpp index 7d3fa570992..4fc381405e9 100644 --- a/src/mongo/util/processinfo_openbsd.cpp +++ b/src/mongo/util/processinfo_openbsd.cpp @@ -163,6 +163,7 @@ void ProcessInfo::SystemInfo::collectSystemInfo() { mib[1] = HW_PHYSMEM; status = getSysctlByIDWithDefault(mib, 2, defaultNum, &numBuffer); memSize = numBuffer; + memLimit = memSize; if (status != 0) log() << "Unable to collect Physical Memory. (errno: " << status << " msg: " << strerror(status) << ")"; diff --git a/src/mongo/util/processinfo_osx.cpp b/src/mongo/util/processinfo_osx.cpp index daac6a766cf..4ab77968a8b 100644 --- a/src/mongo/util/processinfo_osx.cpp +++ b/src/mongo/util/processinfo_osx.cpp @@ -189,6 +189,7 @@ void ProcessInfo::SystemInfo::collectSystemInfo() { osVersion = getSysctlByName<string>("kern.osrelease"); addrSize = (getSysctlByName<NumberVal>("hw.cpu64bit_capable") ? 64 : 32); memSize = getSysctlByName<NumberVal>("hw.memsize"); + memLimit = memSize; numCores = getSysctlByName<NumberVal>("hw.ncpu"); // includes hyperthreading cores pageSize = static_cast<unsigned long long>(sysconf(_SC_PAGESIZE)); cpuArch = getSysctlByName<string>("hw.machine"); diff --git a/src/mongo/util/processinfo_solaris.cpp b/src/mongo/util/processinfo_solaris.cpp index b115a41a008..9b8cc0c8caf 100644 --- a/src/mongo/util/processinfo_solaris.cpp +++ b/src/mongo/util/processinfo_solaris.cpp @@ -162,6 +162,7 @@ void ProcessInfo::SystemInfo::collectSystemInfo() { osVersion = unameData.version; pageSize = static_cast<unsigned long long>(sysconf(_SC_PAGESIZE)); memSize = pageSize * static_cast<unsigned long long>(sysconf(_SC_PHYS_PAGES)); + memLimit = memSize; numCores = static_cast<unsigned>(sysconf(_SC_NPROCESSORS_CONF)); cpuArch = unameData.machine; hasNuma = checkNumaEnabled(); diff --git a/src/mongo/util/processinfo_windows.cpp b/src/mongo/util/processinfo_windows.cpp index 8685d466127..02dd7d4fa86 100644 --- a/src/mongo/util/processinfo_windows.cpp +++ b/src/mongo/util/processinfo_windows.cpp @@ -289,6 +289,7 @@ void ProcessInfo::SystemInfo::collectSystemInfo() { mse.dwLength = sizeof(mse); if (GlobalMemoryStatusEx(&mse)) { memSize = mse.ullTotalPhys; + memLimit = memSize; } // get OS version info |