0

I have been looking for standard way of calculating CPU and I/O usage of running a process. I was looking into following options.

Running a Windows program and detect when it ends with C++

How does this code calculate the number of CPU cycles elapsed?

http://www.codeproject.com/KB/threads/Get_CPU_Usage.aspx

I am not able to get the last one working. I need these figures.

usr = userTime - last_userTime;
ker = kernelTime - last_kernelTime;
idl = idleTime - last_idleTime;

I am primarily looking for Windows GCC based solutions. I am using Dev C++ 4 for testing these. I am also interested in similar solution on unix (solaris/Linux with GCC).

Also is there a way for getting cpu, io used by a process when the process is in running state (Like tracking in a graph) along with limiting CPU usage of a process. Note: I am not looking for tools. I am looking for standard C code. If you want to share any standards from other languages like C++, Python you are welcome.

Community
  • 1
  • 1
Enjoy coding
  • 4,268
  • 12
  • 40
  • 50

1 Answers1

1

The standard way would be to use Windows' performance counters. These come in both "raw" and "cooked" forms, and can be accessed in at least three different ways:

  1. direct: a special key in the Windows registry
  2. Performance Data Helper (PDH) component
  3. WMI

I should warn you that all of these have a fairly steep initial learning curve. Once you've gotten to the point that you can collect and use data from one counter reasonably well, adding more is usually pretty easy though.

In your question, you also mention a completely different way to collect CPU usage data. The idea here is to use GetProcessTimes on a regular basis. For example, after starting up the child process, you can set a timer and call it once a second to get the ongoing CPU usage of the child process.

If you're going to get CPU usage that way, you'd probably want to use GetProcessMemoryInfo to collect memory usage statistics in pretty much the same way.

For I/O statistics, however, I don't know of much in the way of real alternatives to the performance counters.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111