StatsRenderer::StatsRenderer(MPEVRCustomPresenter* presenter, IDirect3DDevice9* device): m_pPresenter(presenter), m_pD3DDev(device), m_pD3DXCreateLine(NULL), m_pD3DXCreateFont(NULL), m_pD3DXCreateSprite(NULL), m_hD3DX9Dll(NULL), m_pFont(NULL), m_pSprite(NULL), m_pLine(NULL) { HINSTANCE hDll; hDll = GetD3X9Dll(); if(hDll) { (FARPROC&)m_pD3DXCreateLine = GetProcAddress(hDll, "D3DXCreateLine"); (FARPROC&)m_pD3DXCreateFont = GetProcAddress(hDll, "D3DXCreateFontW"); (FARPROC&)m_pD3DXCreateSprite = GetProcAddress(hDll, "D3DXCreateSprite"); } if (m_pD3DXCreateSprite) { m_pD3DXCreateSprite( m_pD3DDev, &m_pSprite); } if (m_pD3DXCreateLine) { m_pD3DXCreateLine(m_pD3DDev, &m_pLine); } PDH_STATUS pdhStatus; PdhOpenQuery(NULL, NULL, &m_cpuQuery); pdhStatus = PdhAddEnglishCounter(m_cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &m_cpuTotal); if (pdhStatus == ERROR_SUCCESS) { PdhCollectQueryData(m_cpuQuery); } m_counter = 20; }
static int check_load(printInfoStruct& printInfo) { if (l_Debug) std::wcout << L"Creating query and adding counter" << '\n'; PDH_HQUERY phQuery; PDH_STATUS err = PdhOpenQuery(NULL, NULL, &phQuery); if (!SUCCEEDED(err)) goto die; PDH_HCOUNTER phCounter; err = PdhAddEnglishCounter(phQuery, L"\\Processor(_Total)\\% Idle Time", NULL, &phCounter); if (!SUCCEEDED(err)) goto die; if (l_Debug) std::wcout << L"Collecting first batch of query data" << '\n'; err = PdhCollectQueryData(phQuery); if (!SUCCEEDED(err)) goto die; if (l_Debug) std::wcout << L"Sleep for one second" << '\n'; Sleep(1000); if (l_Debug) std::wcout << L"Collecting second batch of query data" << '\n'; err = PdhCollectQueryData(phQuery); if (!SUCCEEDED(err)) goto die; if (l_Debug) std::wcout << L"Creating formatted counter array" << '\n'; DWORD CounterType; PDH_FMT_COUNTERVALUE DisplayValue; err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue); if (SUCCEEDED(err)) { if (DisplayValue.CStatus == PDH_CSTATUS_VALID_DATA) { if (l_Debug) std::wcout << L"Recieved Value of " << DisplayValue.doubleValue << L" (idle)" << '\n'; printInfo.load = 100.0 - DisplayValue.doubleValue; } else { if (l_Debug) std::wcout << L"Received data was not valid\n"; goto die; } if (l_Debug) std::wcout << L"Finished collection. Cleaning up and returning" << '\n'; PdhCloseQuery(phQuery); return -1; } die: printErrorInfo(); if (phQuery) PdhCloseQuery(phQuery); return 3; }