Click here to Skip to main content
15,936,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Friends, how do I capture system resources usage ( start time,end time,%cpu,%mem usage of application(like firefox)) in windows through code in Qt, I mean which library or DLL to download from where? How I use that in Qt?

It will be good if someone give answer in detail with small example. I'm new to programming.

I'm able to capture total ram and free ram like this

MEMORYSTATUSEX memory_status;
ZeroMemory(&memory_status, sizeof(MEMORYSTATUSEX));
memory_status.dwLength = sizeof(MEMORYSTATUSEX);
if (GlobalMemoryStatusEx(&memory_status))
int q=memory_status.ullTotalPhys/(1024 * 1024);
int q1=memory_status.ullAvailPhys/(1024 * 1024);

1 solution

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;
::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.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900