Standard way of Tracking/Calculating CPU and IO usage of process

帅比萌擦擦* 提交于 2019-12-12 02:42:44

问题


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.


回答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.



来源:https://stackoverflow.com/questions/5181628/standard-way-of-tracking-calculating-cpu-and-io-usage-of-process

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!