Qt is an UI (User Interface) framework and does not contain any functions to get OS or system specific resource usage.
The functions you may use are part of the Windows kernel and did not require an additional library.
The function
GetProcessTimes()[
^] will give you the process start and end time, and the CPU times. Pass
GetCurrentProcess()
as first parameter to get the times for the running application. The CPU times can be used to calculate the CPU usage:
CPU_usage = 100 * (KernelTime + UserTime) / (Now - StartTime)
This is for all CPUs and may be divided by the number of CPUs in the system (see
GetSystemInfo()[
^]). The current time can be retrieved by calling
GetSystemTimeAsFileTime()
. Note that the
FILETIME
used by these functions is a structure and performing arithmetic operations requires special handling.
An untested example:
unsigned long long nCreate, nUser, nKernel, nNow;
FILETIME ftCreate, ftExit, ftKernel, ftUser, ftNow;
SYSTEM_INFO si;
::GetSystemInfo(&si);
::GetSystemTimeAsFileTime(&ftNow);
::GetProcessTimes(::GetCurrentProcess(), &ftCreate, &ftExit, &ftKernel, &ftUser);
nUser = (ftUser.dwHighDateTime << 32ULL) + ftUser.dwLowDateTime;
nKernel = (ftKernel.dwHighDateTime << 32ULL) + ftKernel.dwLowDateTime;
nNow = (ftNow.dwHighDateTime << 32ULL) + ftNow.dwLowDateTime;
nCreate = (ftCreate.dwHighDateTime << 32ULL) + ftCreate.dwLowDateTime;
double fCpuUsage = 100.0 * (nKernel + nUser) / (nNow - nCreate);
double fSingleCpuUsage = fCpuUsage / si.dwNumberOfProcessors;
To get the same information for single threads use the
GetThreadTimes()
function.
I did not have a solution for the memory usage. But hopefully someone else will answer this.