|
// CpuLoad.cpp : Implementation of CCpuLoad
#include "stdafx.h"
#include "CpuLoad.h"
#include "Tools\Exception.h"
#include <stdexcept>
CCpuLoad::CCpuLoad() :
m_started(false),
m_timerThread(1000, this),
m_idleThread(),
m_handle()
{
}
// CCpuLoad
HRESULT CCpuLoad::FinalConstruct()
{
return S_OK;
}
void CCpuLoad::FinalRelease()
{
m_timerThread.Stop();
m_idleThread.Stop();
}
STDMETHODIMP CCpuLoad::InterfaceSupportsErrorInfo(REFIID riid)
{
static const IID* arr[] =
{
&IID_ICpuLoad
};
for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
if (InlineIsEqualGUID(*arr[i],riid))
return S_OK;
}
return S_FALSE;
}
STDMETHODIMP CCpuLoad::Start(void)
{
try
{
Windows::AutoCriticalSection lock(m_lock);
if (m_started) return S_FALSE;
HANDLE handle = ::OpenEvent(EVENT_ALL_ACCESS , FALSE, L"CpuLoadEvent");
if (handle != NULL)
{
Tools::Exception exception(CLSID_CpuLoad, IDS_ALREADY_STARTED_IN_OTHER_INSTANCE);
return exception.GetException();
}
m_handle = ::CreateEvent(NULL, FALSE, FALSE, L"CpuLoadEvent");;
m_idleThread.Start();
m_timerThread.Start();
m_started = true;
} catch (std::exception& err)
{
const char* msg = err.what();
Tools::Exception exception(CLSID_CpuLoad, IDS_FAILED_TO_START);
return exception.GetException();
}
return S_OK;
}
STDMETHODIMP CCpuLoad::Stop(void)
{
try
{
Windows::AutoCriticalSection lock(m_lock);
if (!m_started) return S_FALSE;
m_idleThread.Stop();
m_timerThread.Stop();
m_started = false;
} catch (std::exception& err)
{
const char* msg = err.what();
Tools::Exception exception(CLSID_CpuLoad, IDS_FAILED_TO_STOP);
return exception.GetException();
}
return S_OK;
}
void CCpuLoad::OnTimer()
{
static LARGE_INTEGER previousTime;
LARGE_INTEGER currentTime;
LARGE_INTEGER frequency;
static LARGE_INTEGER previousThreadTime;
LARGE_INTEGER currentThreadTime;
LARGE_INTEGER currentKernelTime;
LARGE_INTEGER currentUserTime;
FILETIME creationTime;
FILETIME exitTime;
FILETIME kernelTime;
FILETIME userTime;
LARGE_INTEGER cpuLoadIdleThread;
QueryPerformanceCounter(¤tTime);
QueryPerformanceFrequency(&frequency);
BOOL ret = GetThreadTimes(m_idleThread.GetThreadHandle(), &creationTime, &exitTime, &kernelTime, &userTime);
// Convert all to LARGE_INTEGER
currentKernelTime.HighPart = kernelTime.dwHighDateTime;
currentKernelTime.LowPart = kernelTime.dwLowDateTime;
currentUserTime.HighPart = userTime.dwHighDateTime;
currentUserTime.LowPart = userTime.dwLowDateTime;
currentThreadTime.QuadPart = currentKernelTime.QuadPart + currentUserTime.QuadPart;
cpuLoadIdleThread.QuadPart = ((currentThreadTime.QuadPart - previousThreadTime.QuadPart) / 10000 * 100) /
((currentTime.QuadPart - previousTime.QuadPart) * 1000 / frequency.QuadPart);
previousTime = currentTime;
previousThreadTime = currentThreadTime;
// Fire COM event that will tell the CpuLoad of the last m_time
Measurement(100 - cpuLoadIdleThread.LowPart/* % */);
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.