WMI calls for getting cpu percentage of current process. This code was a biatch and a half.

git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@10179 e03df62e-2008-0410-955e-edbf42e46eb7
This commit is contained in:
brain 2008-08-20 09:56:16 +00:00
parent 8e83ee21a6
commit 1bdc61df77
3 changed files with 161 additions and 1 deletions

View File

@ -8,11 +8,12 @@
IPCThread::IPCThread(InspIRCd* Instance) : Thread(), ServerInstance(Instance)
{
initwmi();
}
IPCThread::~IPCThread()
{
donewmi();
}
void IPCThread::Run()
@ -84,6 +85,7 @@ void IPCThread::Run()
stat << "kbitspersecout " << kbitpersec_out << std::endl;
stat << "kbitspersecin " << kbitpersec_in << std::endl;
stat << "uptime " << ServerInstance->Time() - ServerInstance->startup_time << std::endl;
stat << "cpu " << getcpu() << std::endl;
if (HaveMemoryStats)
{
stat << "workingset " << MemCounters.WorkingSetSize << std::endl;

View File

@ -17,6 +17,12 @@
#include <string>
#include <errno.h>
#include <assert.h>
#define _WIN32_DCOM
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "comsuppwd.lib")
#pragma comment(lib, "winmm.lib")
using namespace std;
@ -26,6 +32,9 @@ using namespace std;
#include <mmsystem.h>
IWbemLocator *pLoc = NULL;
IWbemServices *pSvc = NULL;
/* This MUST remain static and delcared outside the class, so that WriteProcessMemory can reference it properly */
static DWORD owner_processid = 0;
@ -639,3 +648,147 @@ int gettimeofday(struct timeval * tv, void * tz)
tv->tv_usec = (mstime - (tv->tv_sec * 1000)) * 1000;
return 0;
}
bool initwmi()
{
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
return false;
}
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
CoUninitialize();
return false;
}
pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
CoUninitialize();
return false;
}
pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
pLoc->Release();
CoUninitialize();
return false;
}
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return false;
}
return true;
}
void donewmi()
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
}
int getcpu()
{
HRESULT hres;
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("Select PercentProcessorTime,IDProcess from Win32_PerfFormattedData_PerfProc_Process"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
return 0;
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
int slotpos = 0;
while (pEnumerator)
{
VARIANT vtProp;
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if (uReturn == 0)
break;
/* Enumerate processes */
hr = pclsObj->Get(L"IDProcess", 0, &vtProp, 0, 0);
if (!FAILED(hr))
{
if (vtProp.uintVal == GetCurrentProcessId())
{
VariantClear(&vtProp);
/* Get CPU percentage for this process */
hr = pclsObj->Get(L"PercentProcessorTime", 0, &vtProp, 0, 0);
if (!FAILED(hr))
{
VariantClear(&vtProp);
int cpu = 0;
std::wstringstream out(vtProp.bstrVal);
out >> cpu;
return cpu;
}
}
}
}
pEnumerator->Release();
pclsObj->Release();
return 0;
}

View File

@ -209,5 +209,10 @@ void ChangeWindowsSpecificPointers(InspIRCd* Instance);
bool ValidateWindowsDnsServer(ServerConfig* conf, const char* tag, const char* value, ValueItem &data);
bool initwmi();
void donewmi();
int getcpu();
#endif