/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include"ProfilerCPUFreq.h" #include"nsString.h" #include"nsThreadUtils.h" #ifdef DEBUG # include "nsPrintfCString.h" #endif
ProfilerCPUFreq::ProfilerCPUFreq() { // Query the size of the text data so you can allocate the buffer.
DWORD dwBufferSize = 0; LONG status = RegQueryValueEx(HKEY_PERFORMANCE_DATA, L"Counter 9", NULL, NULL,
NULL, &dwBufferSize); if (ERROR_SUCCESS != status) {
NS_WARNING(nsPrintfCString("RegQueryValueEx failed getting required buffer " "size. Error is 0x%lx.\n",
status)
.get()); return;
}
// Allocate the text buffer and query the text.
LPWSTR pBuffer = (LPWSTR)malloc(dwBufferSize); if (!pBuffer) {
NS_WARNING("failed to allocate buffer"); return;
}
status = RegQueryValueEx(HKEY_PERFORMANCE_DATA, L"Counter 9", NULL, NULL,
(LPBYTE)pBuffer, &dwBufferSize); if (ERROR_SUCCESS != status) {
NS_WARNING(
nsPrintfCString("RegQueryValueEx failed with 0x%lx.\n", status).get());
free(pBuffer); return;
}
LPWSTR pwszCounterText = pBuffer; // Used to cycle through the Counter text // Ignore first pair.
pwszCounterText += (wcslen(pwszCounterText) + 1);
pwszCounterText += (wcslen(pwszCounterText) + 1);
for (; *pwszCounterText; pwszCounterText += (wcslen(pwszCounterText) + 1)) { // Keep a pointer to the counter index, to read the index later if the name // is the one we are looking for.
LPWSTR counterIndex = pwszCounterText;
pwszCounterText += (wcslen(pwszCounterText) + 1); // Skip past index value
if (!wcscmp(L"Processor Information", pwszCounterText)) {
mBlockIndex = _wcsdup(counterIndex);
} elseif (!wcscmp(L"% Processor Performance", pwszCounterText)) {
mCounterNameIndex = _wtoi(counterIndex); if (mBlockIndex) { // We have found all the indexes we were looking for. break;
}
}
}
free(pBuffer);
if (!mBlockIndex) {
NS_WARNING("index of the performance counter block not found"); return;
}
mBuffer = (LPBYTE)malloc(mBufferSize); if (!mBuffer) {
NS_WARNING("failed to allocate initial buffer"); return;
}
dwBufferSize = mBufferSize;
// Typically RegQueryValueEx will set the size variable to the required size. // But this does not work when querying object index values, and the buffer // size has to be increased in a loop until RegQueryValueEx no longer returns // ERROR_MORE_DATA. while (ERROR_MORE_DATA ==
(status = RegQueryValueEx(HKEY_PERFORMANCE_DATA, mBlockIndex, NULL,
NULL, mBuffer, &dwBufferSize))) {
mBufferSize *= 2; auto* oldBuffer = mBuffer;
mBuffer = (LPBYTE)realloc(mBuffer, mBufferSize); if (!mBuffer) {
NS_WARNING("failed to reallocate buffer");
free(oldBuffer); return;
}
dwBufferSize = mBufferSize;
}
// Now get the nominal core frequency.
HKEY key;
nsAutoString keyName(
L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\");
keyName.AppendInt(coreId);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName.get(), 0, KEY_QUERY_VALUE,
&key) == ERROR_SUCCESS) {
DWORD data, len;
len = sizeof(data);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.