diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2020-11-09 12:09:22 -0500 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-11-13 17:58:17 +0000 |
commit | 3251ffe481e2c981e16e9bbf166cb2de7a493e0b (patch) | |
tree | f28b88b70a2946c80c9a6e2df29e79b91a49c4ee | |
parent | 20be11fce5f70d4187e4d1223edb9dc7c0ea7eab (diff) | |
download | mongo-3251ffe481e2c981e16e9bbf166cb2de7a493e0b.tar.gz |
SERVER-27354 Use Language neutral API to resolve performance counters
-rw-r--r-- | src/mongo/util/perfctr_collect.cpp | 52 | ||||
-rw-r--r-- | src/mongo/util/perfctr_collect.h | 6 |
2 files changed, 46 insertions, 12 deletions
diff --git a/src/mongo/util/perfctr_collect.cpp b/src/mongo/util/perfctr_collect.cpp index 9663fcafaf8..8ee4067b146 100644 --- a/src/mongo/util/perfctr_collect.cpp +++ b/src/mongo/util/perfctr_collect.cpp @@ -225,17 +225,17 @@ Status PerfCounterCollector::open() { return Status::OK(); } -StatusWith<PerfCounterCollector::CounterInfo> PerfCounterCollector::addCounter(StringData path) { - +StatusWith<std::tuple<PDH_HCOUNTER, std::unique_ptr<PDH_COUNTER_INFO>>> +PerfCounterCollector::addAndGetCounter(StringData path) { PDH_HCOUNTER counter{0}; - PDH_STATUS status = - PdhAddCounterW(_query, toNativeString(path.toString().c_str()).c_str(), NULL, &counter); + PDH_STATUS status = PdhAddEnglishCounterW( + _query, toNativeString(path.toString().c_str()).c_str(), NULL, &counter); if (status != ERROR_SUCCESS) { - return {ErrorCodes::WindowsPdhError, formatFunctionCallError("PdhAddCounterW", status)}; + return {ErrorCodes::WindowsPdhError, + formatFunctionCallError("PdhAddEnglishCounterW", status)}; } - DWORD bufferSize = 0; status = PdhGetCounterInfoW(counter, false, &bufferSize, nullptr); @@ -245,14 +245,26 @@ StatusWith<PerfCounterCollector::CounterInfo> PerfCounterCollector::addCounter(S } auto buf = std::make_unique<char[]>(bufferSize); - auto counterInfo = reinterpret_cast<PPDH_COUNTER_INFO>(buf.get()); - - status = PdhGetCounterInfoW(counter, false, &bufferSize, counterInfo); + std::unique_ptr<PDH_COUNTER_INFO> counterInfo( + reinterpret_cast<PPDH_COUNTER_INFO>(buf.release())); + status = PdhGetCounterInfoW(counter, false, &bufferSize, counterInfo.get()); if (status != ERROR_SUCCESS) { return {ErrorCodes::WindowsPdhError, formatFunctionCallError("PdhGetCounterInfoW", status)}; } + return std::tuple<PDH_HCOUNTER, std::unique_ptr<PDH_COUNTER_INFO>>{counter, + std::move(counterInfo)}; +} + +StatusWith<PerfCounterCollector::CounterInfo> PerfCounterCollector::addCounter(StringData path) { + auto swCounterInfo = addAndGetCounter(path); + if (!swCounterInfo.isOK()) { + return swCounterInfo.getStatus(); + } + + auto [counter, counterInfo] = std::move(swCounterInfo.getValue()); + // A full qualified path is as such: // "\\MYMACHINE\\Processor(0)\\% Idle Time" // MachineName \\ Object Name (Instance Name) \\ CounterName @@ -289,9 +301,25 @@ StatusWith<PerfCounterCollector::CounterInfo> PerfCounterCollector::addCounter(S StatusWith<std::vector<PerfCounterCollector::CounterInfo>> PerfCounterCollector::addCounters( StringData path) { - std::wstring pathWide = toNativeString(path.toString().c_str()); + + auto swCounterInfo = addAndGetCounter(path); + if (!swCounterInfo.isOK()) { + return swCounterInfo.getStatus(); + } + + auto [unexpandedCounter, counterInfo] = std::move(swCounterInfo.getValue()); + + PDH_STATUS status = PdhRemoveCounter(unexpandedCounter); + if (status != ERROR_SUCCESS) { + return {ErrorCodes::WindowsPdhError, + str::stream() << formatFunctionCallError("PdhRemoveCounter", status) + << " for counter '" << path << "'"}; + } + + auto localizedPath = counterInfo->szFullPath; + DWORD pathListLength = 0; - PDH_STATUS status = PdhExpandCounterPathW(pathWide.c_str(), nullptr, &pathListLength); + status = PdhExpandCounterPathW(localizedPath, nullptr, &pathListLength); if (status != PDH_MORE_DATA) { return {ErrorCodes::WindowsPdhError, @@ -301,7 +329,7 @@ StatusWith<std::vector<PerfCounterCollector::CounterInfo>> PerfCounterCollector: auto buf = std::make_unique<wchar_t[]>(pathListLength); - status = PdhExpandCounterPathW(pathWide.c_str(), buf.get(), &pathListLength); + status = PdhExpandCounterPathW(localizedPath, buf.get(), &pathListLength); if (status != ERROR_SUCCESS) { return {ErrorCodes::WindowsPdhError, diff --git a/src/mongo/util/perfctr_collect.h b/src/mongo/util/perfctr_collect.h index 3d1b45b0f81..15968503ce5 100644 --- a/src/mongo/util/perfctr_collect.h +++ b/src/mongo/util/perfctr_collect.h @@ -270,6 +270,12 @@ private: */ void checkForTicksTimeBase(); + /** + * Add and get a counter by an English name in a language independent way. + */ + StatusWith<std::tuple<PDH_HCOUNTER, std::unique_ptr<PDH_COUNTER_INFO>>> addAndGetCounter( + StringData path); + private: // PDH Query HQUERY _query{INVALID_HANDLE_VALUE}; |