简体   繁体   English

跟踪/计算进程的CPU和IO使用情况的标准方法

[英]Standard way of Tracking/Calculating CPU and IO usage of process

I have been looking for standard way of calculating CPU and I/O usage of running a process. 我一直在寻找计算运行进程的CPU和I / O使用率的标准方法。 I was looking into following options. 我一直在研究以下选项。

Running a Windows program and detect when it ends with C++ 运行Windows程序并检测它何时以C ++结尾

How does this code calculate the number of CPU cycles elapsed? 此代码如何计算经过的CPU周期数?

http://www.codeproject.com/KB/threads/Get_CPU_Usage.aspx 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. 我主要是在寻找基于Windows GCC的解决方案。 I am using Dev C++ 4 for testing these. 我正在使用Dev C ++ 4进行测试。 I am also interested in similar solution on unix (solaris/Linux with GCC). 我也对UNIX上的类似解决方案(带有GCC的solaris / Linux)感兴趣。

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. 当进程处于运行状态(如图形中的跟踪)时,还有一种获取进程使用的cpu,io以及限制进程的CPU使用率的方法。 Note: I am not looking for tools. 注意:我不是在寻找工具。 I am looking for standard C code. 我在寻找标准的C代码。 If you want to share any standards from other languages like C++, Python you are welcome. 如果您想共享其他语言(如C ++,Python)的任何标准,欢迎您。

The standard way would be to use Windows' performance counters. 标准方法是使用Windows的性能计数器。 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 直接:Windows 注册表中的特殊键
  2. Performance Data Helper ( PDH ) component 性能数据助手( PDH )组件
  3. WMI 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. 在您的问题中,您还提到了一种收集CPU使用情况数据的完全不同的方法。 The idea here is to use GetProcessTimes on a regular basis. 这里的想法是定期使用GetProcessTimes 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. 例如,在启动子进程之后,您可以设置计时器并每秒调用一次,以获取子进程的持续CPU使用率。

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. 如果要以这种方式获取CPU使用率,则可能要使用GetProcessMemoryInfo以几乎相同的方式收集内存使用情况统计信息。

For I/O statistics, however, I don't know of much in the way of real alternatives to the performance counters. 但是,对于I / O统计信息,我对性能计数器的真正替代品知之甚少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM