From 3b2bab6d50f69010004dfc8d2630e147d0e9602a Mon Sep 17 00:00:00 2001 From: Robert Guo Date: Tue, 13 Mar 2018 14:58:04 -0400 Subject: SERVER-33643 refactor processInfo to no longer depend on global initialization. (cherry picked from commit ebcfc62197744ffe0bb77a404a8e210c2100de31) --- src/mongo/util/clock_source_bm.cpp | 7 +---- src/mongo/util/processinfo.cpp | 20 +------------ src/mongo/util/processinfo.h | 50 +++++++++++++++++-------------- src/mongo/util/processinfo_freebsd.cpp | 2 +- src/mongo/util/processinfo_linux.cpp | 2 +- src/mongo/util/processinfo_openbsd.cpp | 2 +- src/mongo/util/processinfo_osx.cpp | 2 +- src/mongo/util/processinfo_solaris.cpp | 2 +- src/mongo/util/processinfo_test.cpp | 13 +++----- src/mongo/util/processinfo_windows.cpp | 4 ++- src/mongo/util/tcmalloc_set_parameter.cpp | 2 +- 11 files changed, 42 insertions(+), 64 deletions(-) (limited to 'src/mongo/util') diff --git a/src/mongo/util/clock_source_bm.cpp b/src/mongo/util/clock_source_bm.cpp index 719471e976e..c64811210f3 100644 --- a/src/mongo/util/clock_source_bm.cpp +++ b/src/mongo/util/clock_source_bm.cpp @@ -69,12 +69,7 @@ void BM_ClockNow(benchmark::State& state) { } BENCHMARK(BM_ClockNow) - ->ThreadRange(1, - [] { - ProcessInfo::initializeSystemInfo(); - ProcessInfo pi; - return static_cast(pi.getNumAvailableCores().value_or(pi.getNumCores())); - }()) + ->ThreadRange(1, ProcessInfo::getNumAvailableCores()) ->ArgName("poll period") ->Arg(0) ->Arg(1) diff --git a/src/mongo/util/processinfo.cpp b/src/mongo/util/processinfo.cpp index 987c9875406..fcd75b7daa3 100644 --- a/src/mongo/util/processinfo.cpp +++ b/src/mongo/util/processinfo.cpp @@ -92,22 +92,4 @@ private: bool writePidFile(const string& path) { return pidFileWiper.write(path); } - -ProcessInfo::SystemInfo* ProcessInfo::systemInfo = NULL; - -void ProcessInfo::initializeSystemInfo() { - if (systemInfo == NULL) { - systemInfo = new SystemInfo(); - } -} - -/** - * We need this get the system page size for the secure allocator, which the enterprise modules need - * for storage for command line parameters. - */ -MONGO_INITIALIZER_GENERAL(SystemInfo, MONGO_NO_PREREQUISITES, MONGO_NO_DEPENDENTS) -(InitializerContext* context) { - ProcessInfo::initializeSystemInfo(); - return Status::OK(); -} -} +} // namespace mongo diff --git a/src/mongo/util/processinfo.h b/src/mongo/util/processinfo.h index 0a2d2cc1d22..f02f1a78493 100644 --- a/src/mongo/util/processinfo.h +++ b/src/mongo/util/processinfo.h @@ -58,69 +58,71 @@ public: /** * Get the type of os (e.g. Windows, Linux, Mac OS) */ - const std::string& getOsType() const { + static const std::string& getOsType() { return sysInfo().osType; } /** * Get the os Name (e.g. Ubuntu, Gentoo, Windows Server 2008) */ - const std::string& getOsName() const { + static const std::string& getOsName() { return sysInfo().osName; } /** * Get the os version (e.g. 10.04, 11.3.0, 6.1 (build 7600)) */ - const std::string& getOsVersion() const { + static const std::string& getOsVersion() { return sysInfo().osVersion; } /** * Get the cpu address size (e.g. 32, 36, 64) */ - unsigned getAddrSize() const { + static unsigned getAddrSize() { return sysInfo().addrSize; } /** * Get the total amount of system memory in MB */ - unsigned long long getMemSizeMB() const { + static unsigned long long getMemSizeMB() { return sysInfo().memSize / (1024 * 1024); } /** - * Get the number of available CPUs. Depending on the OS, the number can be the - * number of available CPUs to the current process or scheduler. + * Get the number of CPUs */ - boost::optional getNumAvailableCores(); + static unsigned getNumCores() { + return sysInfo().numCores; + } /** - * Get the number of CPUs + * Get the number of cores available. Make a best effort to get the cores for this process. + * If that information is not available, get the total number of CPUs. */ - unsigned getNumCores() const { - return sysInfo().numCores; + static unsigned long getNumAvailableCores() { + return ProcessInfo::getNumCoresForProcess().value_or(ProcessInfo::getNumCores()); } /** * Get the system page size in bytes. */ static unsigned long long getPageSize() { - return systemInfo->pageSize; + return sysInfo().pageSize; } /** * Get the CPU architecture (e.g. x86, x86_64) */ - const std::string& getArch() const { + static const std::string& getArch() { return sysInfo().cpuArch; } /** * Determine if NUMA is enabled (interleaved) for this process */ - bool hasNumaEnabled() const { + static bool hasNumaEnabled() { return sysInfo().hasNuma; } @@ -128,14 +130,14 @@ public: * Determine if file zeroing is necessary for newly allocated data files. */ static bool isDataFileZeroingNeeded() { - return systemInfo->fileZeroNeeded; + return sysInfo().fileZeroNeeded; } /** * Determine if we need to workaround slow msync performance on Illumos/Solaris */ static bool preferMsyncOverFSync() { - return systemInfo->preferMsyncOverFSync; + return sysInfo().preferMsyncOverFSync; } /** @@ -236,18 +238,20 @@ private: }; ProcessId _pid; - static stdx::mutex _sysInfoLock; static bool checkNumaEnabled(); - static ProcessInfo::SystemInfo* systemInfo; - - inline const SystemInfo& sysInfo() const { - return *systemInfo; + inline static const SystemInfo& sysInfo() { + static ProcessInfo::SystemInfo systemInfo; + return systemInfo; } -public: - static void initializeSystemInfo(); +private: + /** + * Get the number of available CPUs. Depending on the OS, the number can be the + * number of available CPUs to the current process or scheduler. + */ + static boost::optional getNumCoresForProcess(); }; bool writePidFile(const std::string& path); diff --git a/src/mongo/util/processinfo_freebsd.cpp b/src/mongo/util/processinfo_freebsd.cpp index ce07c514753..3041d0f4387 100644 --- a/src/mongo/util/processinfo_freebsd.cpp +++ b/src/mongo/util/processinfo_freebsd.cpp @@ -195,7 +195,7 @@ bool ProcessInfo::pagesInMemory(const void* start, size_t numPages, vector } // get the number of CPUs available to the scheduler -boost::optional ProcessInfo::getNumAvailableCores() { +boost::optional ProcessInfo::getNumCoresForProcess() { long nprocs = sysconf(_SC_NPROCESSORS_ONLN); if (nprocs) return nprocs; diff --git a/src/mongo/util/processinfo_linux.cpp b/src/mongo/util/processinfo_linux.cpp index a3310e64e95..41411a0797d 100644 --- a/src/mongo/util/processinfo_linux.cpp +++ b/src/mongo/util/processinfo_linux.cpp @@ -422,7 +422,7 @@ bool ProcessInfo::supported() { } // get the number of CPUs available to the current process -boost::optional ProcessInfo::getNumAvailableCores() { +boost::optional ProcessInfo::getNumCoresForProcess() { cpu_set_t set; if (sched_getaffinity(0, sizeof(cpu_set_t), &set) == 0) { diff --git a/src/mongo/util/processinfo_openbsd.cpp b/src/mongo/util/processinfo_openbsd.cpp index a4c1d0f9ab2..7db5cb4e908 100644 --- a/src/mongo/util/processinfo_openbsd.cpp +++ b/src/mongo/util/processinfo_openbsd.cpp @@ -214,7 +214,7 @@ bool ProcessInfo::pagesInMemory(const void* start, size_t numPages, vector } // get the number of CPUs available to the scheduler -boost::optional ProcessInfo::getNumAvailableCores() { +boost::optional ProcessInfo::getNumCoresForProcess() { long nprocs = sysconf(_SC_NPROCESSORS_ONLN); if (nprocs) return nprocs; diff --git a/src/mongo/util/processinfo_osx.cpp b/src/mongo/util/processinfo_osx.cpp index 475016de313..f244bea4da9 100644 --- a/src/mongo/util/processinfo_osx.cpp +++ b/src/mongo/util/processinfo_osx.cpp @@ -64,7 +64,7 @@ bool ProcessInfo::supported() { } // get the number of CPUs available to the scheduler -boost::optional ProcessInfo::getNumAvailableCores() { +boost::optional ProcessInfo::getNumCoresForProcess() { long nprocs = sysconf(_SC_NPROCESSORS_ONLN); if (nprocs) return nprocs; diff --git a/src/mongo/util/processinfo_solaris.cpp b/src/mongo/util/processinfo_solaris.cpp index 3bd892de4e8..26ff218d90c 100644 --- a/src/mongo/util/processinfo_solaris.cpp +++ b/src/mongo/util/processinfo_solaris.cpp @@ -110,7 +110,7 @@ bool ProcessInfo::supported() { } // get the number of CPUs available to the scheduler -boost::optional ProcessInfo::getNumAvailableCores() { +boost::optional ProcessInfo::getNumCoresForProcess() { long nprocs = sysconf(_SC_NPROCESSORS_ONLN); if (nprocs) return nprocs; diff --git a/src/mongo/util/processinfo_test.cpp b/src/mongo/util/processinfo_test.cpp index 76f7e7ee551..53bf5305b51 100644 --- a/src/mongo/util/processinfo_test.cpp +++ b/src/mongo/util/processinfo_test.cpp @@ -55,18 +55,13 @@ TEST(ProcessInfo, NonZeroPageSize) { TEST(ProcessInfo, GetNumAvailableCores) { #if defined(__APPLE__) || defined(__linux__) || (defined(__sun) && defined(__SVR4)) || \ defined(_WIN32) - ProcessInfo processInfo; - ProcessInfo::initializeSystemInfo(); - optional numAvailCores = processInfo.getNumAvailableCores(); - ASSERT_TRUE(numAvailCores.is_initialized()); - ASSERT_GREATER_THAN(*numAvailCores, 0u); - ASSERT_LESS_THAN_OR_EQUALS(*numAvailCores, processInfo.getNumCores()); + unsigned long numAvailCores = ProcessInfo::getNumAvailableCores(); + ASSERT_GREATER_THAN(numAvailCores, 0u); + ASSERT_LESS_THAN_OR_EQUALS(numAvailCores, ProcessInfo::getNumCores()); #endif } TEST(ProcessInfo, GetNumCoresReturnsNonZeroNumberOfProcessors) { - ProcessInfo processInfo; - ProcessInfo::initializeSystemInfo(); - ASSERT_GREATER_THAN(processInfo.getNumCores(), 0u); + ASSERT_GREATER_THAN(ProcessInfo::getNumCores(), 0u); } } diff --git a/src/mongo/util/processinfo_windows.cpp b/src/mongo/util/processinfo_windows.cpp index 676204c9856..7aba688d2df 100644 --- a/src/mongo/util/processinfo_windows.cpp +++ b/src/mongo/util/processinfo_windows.cpp @@ -77,7 +77,7 @@ ProcessInfo::ProcessInfo(ProcessId pid) {} ProcessInfo::~ProcessInfo() {} // get the number of CPUs available to the current process -boost::optional ProcessInfo::getNumAvailableCores() { +boost::optional ProcessInfo::getNumCoresForProcess() { DWORD_PTR process_mask, system_mask; if (GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask)) { @@ -464,6 +464,8 @@ bool ProcessInfo::checkNumaEnabled() { } bool ProcessInfo::blockCheckSupported() { + sysInfo(); // Initialize SystemInfo, which calls collectSystemInfo(), which creates + // psapiGlobal. return psapiGlobal->supported; } diff --git a/src/mongo/util/tcmalloc_set_parameter.cpp b/src/mongo/util/tcmalloc_set_parameter.cpp index ca9cf7eb108..1ec424e1490 100644 --- a/src/mongo/util/tcmalloc_set_parameter.cpp +++ b/src/mongo/util/tcmalloc_set_parameter.cpp @@ -129,7 +129,7 @@ TcmallocNumericPropertyServerParameter tcmallocAggressiveMemoryDecommit( "tcmallocAggressiveMemoryDecommit", "tcmalloc.aggressive_memory_decommit"); MONGO_INITIALIZER_GENERAL(TcmallocConfigurationDefaults, - ("SystemInfo"), + MONGO_NO_PREREQUISITES, ("BeginStartupOptionHandling")) (InitializerContext*) { // Before processing the command line options, if the user has not specified a value in via -- cgit v1.2.1