How can I calculate CPU and disk utilization of another concurrent program? I.e. one program is running and other calculates the first's resource use.
I'm using C and C++ and running under Windows XP.
How can I calculate CPU and disk utilization of another concurrent program? I.e. one program is running and other calculates the first's resource use.
I'm using C and C++ and running under Windows XP.
It is possible, as Process Explorer can do it, but I think you will have to use some kind of undocumented Windows API. PSAPI comes somewhat close, but it gives you only the memory usage information, not CPU or disk utilization.
As for CPU utilization it is not difficult to do after taking a look at this link Windows C++ Get CPU and Memory Utilisation With Performance Counters. As far as I understand (but have not tested) it is also possible to find out disk utilization.
The idea is to use Performance Counters. In your situation you need to use the performance counter L"\\Process(program_you_are_interested_in_name)\\% Processor Time"
for CPU Utilization and possibly L"\\Process(program_you_are_interested_in_name)\\Data Bytes/sec"
for disk operations. Since I am not sure what parameters you need to know about disk operations exactly you can take a look yourself at the list of all available parameters: Process Object
If for example you have a concurrent program with the name a_program_name.exe
you can find its CPU utilization measuring at least twice the performance counter L"\\Process(a_program_name)\\% Processor Time"
. In the example it is done in a loop. By the way measuring with this test a multithreaded application running on a multicore processor might give CPU utilization which is more than 100%.
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <pdh.h>
#include <pdhmsg.h>
#include <string.h>
#include <string>
#include <iostream>
// Put name of your process here!!!!
CONST PWSTR COUNTER_PATH = L"\\Process(a_program_name)\\% Processor Time";
void main(int argc, char *argv[]){
PDH_HQUERY hquery;
PDH_HCOUNTER hcountercpu;
PDH_STATUS status;
LPSTR pMessage;
PDH_FMT_COUNTERVALUE countervalcpu;
if((status=PdhOpenQuery(NULL, 0, &hquery))!=ERROR_SUCCESS){
printf("PdhOpenQuery %lx\n", status);
goto END;
}
if((status=PdhAddCounter(hquery,COUNTER_PATH,0, &hcountercpu))!=ERROR_SUCCESS){
printf("PdhAddCounter (cpu) %lx\n", status);
goto END;
}
/*Start outside the loop as CPU requires difference
between two PdhCollectQueryData s*/
if((status=PdhCollectQueryData(hquery))!=ERROR_SUCCESS){
printf("PdhCollectQueryData %lx\n", status);
goto END;
}
while(true){
if((status=PdhCollectQueryData(hquery))!=ERROR_SUCCESS){
printf("PdhCollectQueryData %lx\n", status);
goto END;
}
if((status=PdhGetFormattedCounterValue(hcountercpu, PDH_FMT_LONG | PDH_FMT_NOCAP100, 0, &countervalcpu))!=ERROR_SUCCESS){
printf("PdhGetFormattedCounterValue(cpu) %lx\n", status);
goto END;
}
printf("cpu %3d%%\n", countervalcpu.longValue);
Sleep(1000);
}
END:
;
}
There is one more thing to mention. PdhExpandWildCardPath
lets you expand a string like this L"\\Process(*)\\% Processor Time"
in a list of processes running on a computer. And then you can query performance counters for each process.