From dafb5c5bab913ae1b1ad773fe18372ec7cb87109 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 3 Oct 2016 15:11:56 -0700 Subject: [PATCH] Add processor information to launch user activity event --- interface/src/Application.cpp | 13 ++- libraries/shared/src/SharedUtil.cpp | 146 +++++++++++++++++++++++++++- libraries/shared/src/SharedUtil.h | 11 +++ 3 files changed, 165 insertions(+), 5 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index ec452cb260..c170e38a27 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -815,7 +815,8 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer) : { "gl_version", glContextData["version"] }, { "gl_vender", glContextData["vendor"] }, { "gl_sl_version", glContextData["slVersion"] }, - { "gl_renderer", glContextData["renderer"] } + { "gl_renderer", glContextData["renderer"] }, + { "ideal_thread_count", QThread::idealThreadCount() } }; auto macVersion = QSysInfo::macVersion(); if (macVersion != QSysInfo::MV_None) { @@ -825,6 +826,16 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer) : if (windowsVersion != QSysInfo::WV_None) { properties["os_win_version"] = QSysInfo::windowsVersion(); } + + ProcessorInfo procInfo; + if (getProcessorInfo(procInfo)) { + properties["processor_core_count"] = procInfo.numProcessorCores; + properties["logical_processor_count"] = procInfo.numLogicalProcessors; + properties["processor_l1_cache_count"] = procInfo.numProcessorCachesL1; + properties["processor_l2_cache_count"] = procInfo.numProcessorCachesL2; + properties["processor_l3_cache_count"] = procInfo.numProcessorCachesL3; + } + UserActivityLogger::getInstance().logAction("launch", properties); _connectionMonitor.init(); diff --git a/libraries/shared/src/SharedUtil.cpp b/libraries/shared/src/SharedUtil.cpp index bb5d326851..efb032ce75 100644 --- a/libraries/shared/src/SharedUtil.cpp +++ b/libraries/shared/src/SharedUtil.cpp @@ -22,11 +22,8 @@ #include -#ifdef _WIN32 -#include -#endif - #ifdef Q_OS_WIN +#include #include "CPUIdent.h" #include #endif @@ -877,3 +874,144 @@ bool getMemoryInfo(MemoryInfo& info) { return false; } + +// Largely taken from: https://msdn.microsoft.com/en-us/library/windows/desktop/ms683194(v=vs.85).aspx + +#ifdef Q_OS_WIN +using LPFN_GLPI = BOOL(WINAPI*)( + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, + PDWORD); + +DWORD CountSetBits(ULONG_PTR bitMask) +{ + DWORD LSHIFT = sizeof(ULONG_PTR) * 8 - 1; + DWORD bitSetCount = 0; + ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT; + DWORD i; + + for (i = 0; i <= LSHIFT; ++i) + { + bitSetCount += ((bitMask & bitTest) ? 1 : 0); + bitTest /= 2; + } + + return bitSetCount; +} +#endif + +bool getProcessorInfo(ProcessorInfo& info) { + +#ifdef Q_OS_WIN + LPFN_GLPI glpi; + bool done = false; + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; + PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL; + DWORD returnLength = 0; + DWORD logicalProcessorCount = 0; + DWORD numaNodeCount = 0; + DWORD processorCoreCount = 0; + DWORD processorL1CacheCount = 0; + DWORD processorL2CacheCount = 0; + DWORD processorL3CacheCount = 0; + DWORD processorPackageCount = 0; + DWORD byteOffset = 0; + PCACHE_DESCRIPTOR Cache; + + glpi = (LPFN_GLPI)GetProcAddress( + GetModuleHandle(TEXT("kernel32")), + "GetLogicalProcessorInformation"); + if (nullptr == glpi) { + qDebug() << "GetLogicalProcessorInformation is not supported."; + return false; + } + + while (!done) { + DWORD rc = glpi(buffer, &returnLength); + + if (FALSE == rc) { + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + if (buffer) { + free(buffer); + } + + buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc( + returnLength); + + if (NULL == buffer) { + qDebug() << "Error: Allocation failure"; + return (2); + } + } else { + qDebug() << "Error " << GetLastError(); + return (3); + } + } else { + done = true; + } + } + + ptr = buffer; + + while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) { + switch (ptr->Relationship) { + case RelationNumaNode: + // Non-NUMA systems report a single record of this type. + numaNodeCount++; + break; + + case RelationProcessorCore: + processorCoreCount++; + + // A hyperthreaded core supplies more than one logical processor. + logicalProcessorCount += CountSetBits(ptr->ProcessorMask); + break; + + case RelationCache: + // Cache data is in ptr->Cache, one CACHE_DESCRIPTOR structure for each cache. + Cache = &ptr->Cache; + if (Cache->Level == 1) { + processorL1CacheCount++; + } else if (Cache->Level == 2) { + processorL2CacheCount++; + } else if (Cache->Level == 3) { + processorL3CacheCount++; + } + break; + + case RelationProcessorPackage: + // Logical processors share a physical package. + processorPackageCount++; + break; + + default: + qDebug() << "\nError: Unsupported LOGICAL_PROCESSOR_RELATIONSHIP value.\n"; + break; + } + byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); + ptr++; + } + + qDebug() << "GetLogicalProcessorInformation results:"; + qDebug() << "Number of NUMA nodes:" << numaNodeCount; + qDebug() << "Number of physical processor packages:" << processorPackageCount; + qDebug() << "Number of processor cores:" << processorCoreCount; + qDebug() << "Number of logical processors:" << logicalProcessorCount; + qDebug() << "Number of processor L1/L2/L3 caches:" + << processorL1CacheCount + << "/" << processorL2CacheCount + << "/" << processorL3CacheCount; + + info.numPhysicalProcessorPackages = processorPackageCount; + info.numProcessorCores = processorCoreCount; + info.numLogicalProcessors = logicalProcessorCount; + info.numProcessorCachesL1 = processorL1CacheCount; + info.numProcessorCachesL2 = processorL2CacheCount; + info.numProcessorCachesL3 = processorL3CacheCount; + + free(buffer); + + return true; +#endif + + return false; +} \ No newline at end of file diff --git a/libraries/shared/src/SharedUtil.h b/libraries/shared/src/SharedUtil.h index f3e5625484..863c4d6dc5 100644 --- a/libraries/shared/src/SharedUtil.h +++ b/libraries/shared/src/SharedUtil.h @@ -214,4 +214,15 @@ struct MemoryInfo { bool getMemoryInfo(MemoryInfo& info); +struct ProcessorInfo { + int32_t numPhysicalProcessorPackages; + int32_t numProcessorCores; + int32_t numLogicalProcessors; + int32_t numProcessorCachesL1; + int32_t numProcessorCachesL2; + int32_t numProcessorCachesL3; +}; + +bool getProcessorInfo(ProcessorInfo& info); + #endif // hifi_SharedUtil_h