summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobert Guo <robert.guo@10gen.com>2018-03-13 14:58:04 -0400
committerRobert Guo <robert.guo@10gen.com>2018-03-15 10:14:34 -0400
commitebcfc62197744ffe0bb77a404a8e210c2100de31 (patch)
tree0f9ce5d4a564abb207045d0765a7b77281de0cad /src
parent137bae8a3739545564cef631e33680108e767330 (diff)
downloadmongo-ebcfc62197744ffe0bb77a404a8e210c2100de31.tar.gz
SERVER-33643 refactor processInfo to no longer depend on global
initialization.
Diffstat (limited to 'src')
-rw-r--r--src/mongo/base/secure_allocator.cpp2
-rw-r--r--src/mongo/client/examples/mongoperf.cpp2
-rw-r--r--src/mongo/db/storage/mmap_v1/record_access_tracker.cpp2
-rw-r--r--src/mongo/transport/service_executor_adaptive.cpp3
-rw-r--r--src/mongo/transport/service_executor_synchronous.cpp8
-rw-r--r--src/mongo/unittest/system_resource_canary_bm.cpp6
-rw-r--r--src/mongo/util/clock_source_bm.cpp7
-rw-r--r--src/mongo/util/processinfo.cpp20
-rw-r--r--src/mongo/util/processinfo.h50
-rw-r--r--src/mongo/util/processinfo_freebsd.cpp2
-rw-r--r--src/mongo/util/processinfo_linux.cpp2
-rw-r--r--src/mongo/util/processinfo_openbsd.cpp2
-rw-r--r--src/mongo/util/processinfo_osx.cpp2
-rw-r--r--src/mongo/util/processinfo_solaris.cpp2
-rw-r--r--src/mongo/util/processinfo_test.cpp13
-rw-r--r--src/mongo/util/processinfo_windows.cpp4
-rw-r--r--src/mongo/util/tcmalloc_set_parameter.cpp2
17 files changed, 47 insertions, 82 deletions
diff --git a/src/mongo/base/secure_allocator.cpp b/src/mongo/base/secure_allocator.cpp
index 68f8c190238..62d9256d05d 100644
--- a/src/mongo/base/secure_allocator.cpp
+++ b/src/mongo/base/secure_allocator.cpp
@@ -328,7 +328,7 @@ std::shared_ptr<Allocation> lastAllocation = nullptr;
} // namespace
-MONGO_INITIALIZER_GENERAL(SecureAllocator, ("SystemInfo"), MONGO_NO_DEPENDENTS)
+MONGO_INITIALIZER_GENERAL(SecureAllocator, MONGO_NO_PREREQUISITES, MONGO_NO_DEPENDENTS)
(InitializerContext* context) {
#if _WIN32
// Enable the increase working set size privilege in our access token.
diff --git a/src/mongo/client/examples/mongoperf.cpp b/src/mongo/client/examples/mongoperf.cpp
index 80f43ae815d..e5f5b2686ec 100644
--- a/src/mongo/client/examples/mongoperf.cpp
+++ b/src/mongo/client/examples/mongoperf.cpp
@@ -51,7 +51,6 @@
#include "mongo/stdx/thread.h"
#include "mongo/util/allocator.h"
#include "mongo/util/mongoutils/str.h"
-#include "mongo/util/processinfo.h"
#include "mongo/util/scopeguard.h"
#include "mongo/util/time_support.h"
#include "mongo/util/timer.h"
@@ -326,7 +325,6 @@ int main(int argc, char* argv[]) {
return EXIT_FAILURE;
}
cout << "parsed options:\n" << options.toString() << endl;
- ProcessInfo::initializeSystemInfo();
go();
} catch (DBException& e) {
diff --git a/src/mongo/db/storage/mmap_v1/record_access_tracker.cpp b/src/mongo/db/storage/mmap_v1/record_access_tracker.cpp
index 0bdd9237c52..67e0f1fd79a 100644
--- a/src/mongo/db/storage/mmap_v1/record_access_tracker.cpp
+++ b/src/mongo/db/storage/mmap_v1/record_access_tracker.cpp
@@ -48,7 +48,7 @@ namespace {
static bool blockSupported = false;
-MONGO_INITIALIZER_WITH_PREREQUISITES(RecordBlockSupported, ("SystemInfo"))(InitializerContext* cx) {
+MONGO_INITIALIZER(RecordBlockSupported)(InitializerContext* cx) {
blockSupported = ProcessInfo::blockCheckSupported();
return Status::OK();
}
diff --git a/src/mongo/transport/service_executor_adaptive.cpp b/src/mongo/transport/service_executor_adaptive.cpp
index 2fef111bb9f..f038d62c57a 100644
--- a/src/mongo/transport/service_executor_adaptive.cpp
+++ b/src/mongo/transport/service_executor_adaptive.cpp
@@ -105,8 +105,7 @@ struct ServerParameterOptions : public ServiceExecutorAdaptive::Options {
int reservedThreads() const final {
int value = adaptiveServiceExecutorReservedThreads.load();
if (value == -1) {
- ProcessInfo pi;
- value = pi.getNumAvailableCores().value_or(pi.getNumCores()) / 2;
+ value = ProcessInfo::getNumAvailableCores() / 2;
value = std::max(value, 2);
adaptiveServiceExecutorReservedThreads.store(value);
log() << "No thread count configured for executor. Using number of cores / 2: "
diff --git a/src/mongo/transport/service_executor_synchronous.cpp b/src/mongo/transport/service_executor_synchronous.cpp
index 5027fc4273d..a3bd83a0f06 100644
--- a/src/mongo/transport/service_executor_synchronous.cpp
+++ b/src/mongo/transport/service_executor_synchronous.cpp
@@ -60,13 +60,7 @@ thread_local int64_t ServiceExecutorSynchronous::_localThreadIdleCounter = 0;
ServiceExecutorSynchronous::ServiceExecutorSynchronous(ServiceContext* ctx) {}
Status ServiceExecutorSynchronous::start() {
- _numHardwareCores = [] {
- ProcessInfo p;
- if (auto availCores = p.getNumAvailableCores()) {
- return static_cast<size_t>(*availCores);
- }
- return static_cast<size_t>(p.getNumCores());
- }();
+ _numHardwareCores = static_cast<size_t>(ProcessInfo::getNumAvailableCores());
_stillRunning.store(true);
diff --git a/src/mongo/unittest/system_resource_canary_bm.cpp b/src/mongo/unittest/system_resource_canary_bm.cpp
index 7d15a8263d4..e7953fc08ff 100644
--- a/src/mongo/unittest/system_resource_canary_bm.cpp
+++ b/src/mongo/unittest/system_resource_canary_bm.cpp
@@ -154,11 +154,7 @@ BENCHMARK_REGISTER_F(CacheLatencyTest, BM_CacheLatency)
->RangeMultiplier(2 * 1024)
// Loop over arrays of different sizes to test the L2, L3, and RAM latency.
->Range(256 * 1024, 4096 * 1024)
- ->ThreadRange(1, [] {
- ProcessInfo::initializeSystemInfo();
- ProcessInfo pi;
- return static_cast<int>(pi.getNumAvailableCores().value_or(pi.getNumCores()));
- }());
+ ->ThreadRange(1, ProcessInfo::getNumAvailableCores());
} // namespace
} // namespace mongo
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<int>(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<unsigned long> 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<unsigned long> 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<char>
}
// get the number of CPUs available to the scheduler
-boost::optional<unsigned long> ProcessInfo::getNumAvailableCores() {
+boost::optional<unsigned long> 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 0c014a5e9a4..5db07c94661 100644
--- a/src/mongo/util/processinfo_linux.cpp
+++ b/src/mongo/util/processinfo_linux.cpp
@@ -424,7 +424,7 @@ bool ProcessInfo::supported() {
}
// get the number of CPUs available to the current process
-boost::optional<unsigned long> ProcessInfo::getNumAvailableCores() {
+boost::optional<unsigned long> 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<char>
}
// get the number of CPUs available to the scheduler
-boost::optional<unsigned long> ProcessInfo::getNumAvailableCores() {
+boost::optional<unsigned long> 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<unsigned long> ProcessInfo::getNumAvailableCores() {
+boost::optional<unsigned long> 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<unsigned long> ProcessInfo::getNumAvailableCores() {
+boost::optional<unsigned long> 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<unsigned long> 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<unsigned long> ProcessInfo::getNumAvailableCores() {
+boost::optional<unsigned long> 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