无需 TaskMgr 即可测量 CPU 使用情况?
我正在尝试测量长时间运行的进程中的 CPU 利用率水平。我怀疑每次运行任务管理器查看数据时,进程的 CPU 利用率都会下降,因为任务管理器具有更高的优先级。如果我给我的进程实时优先级,那么任务管理器将完全锁定,我无法使用它。我希望我的大部分 CPU 周期专用于这个过程,并且我想大致了解它的利用率。我不需要一秒一秒的监视器,只需要一些快照,让我知道发生了什么。我怎样才能做到这一点?
I'm trying to gauge the CPU utilization level during a long-running process. I suspect that everytime I run task-manager to view the data, the process' CPU utilization goes down because taskmanager has a higher priority. If I give my process RealTime priority, then task manager completely locks up and I cannot use it. I want most of my CPU cycles dedicated to this process, and I want to get a rough idea of how much it is utilizing. I don't need a second-by-second monitor, but just a few snapshots that let me know what's going on. How can I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过 C API 以编程方式使用性能计数器 API。 (CPU 使用率只是另一个计数器)。您可以使用低级注册表API来查询数据的性能计数器。或者您可以使用 PDH API (性能数据助手 API)- 这可能就是您想要的。我过去都用过,PDH api 很容易使用。
另一个帮助您枚举可用计数器名称的工具是 perfmon。 (只需运行 c:\windows\system32\perfmon.exe)。它也是任务管理器的有用替代品。它还可以进行日志记录和图表。您可以为多进程计算机上的每个逻辑处理器设置计数器。
Programatically with a C API, you can use the Performance Counter API. (CPU usage is just another counter). You can use the low-level registry API to query the performance counter for data. Or you can use the PDH API (Performance Data Helper API) - which is probably what you want. I've used both in the past and the PDH api is easy to use.
Another tool to help you enumerate the names of available counters is perfmon. (Just run c:\windows\system32\perfmon.exe). It is also a useful alternative to Task Manager. It also does logging and graphs. And you can setup counters for each logical processor on a multi-proc machine.
可能是 procdump - 但 sysinternals 进程工具之一应该有所帮助
Probably procdump - but one of the sysinternals process tools should help
我不确定您对任务管理器的担忧是否有效。任务管理器足够轻量,不会淹没您长时间运行的进程并窃取足够的 CPU 周期。如果您的进程确实需要 CPU,您会在任务管理器中看到它。如果您没有看到进程的 CPU 使用率与您预期的一样多,那么您认为进程需要一段时间是因为它使用了大量 CPU 的假设是错误的。也许您长时间运行的进程之所以长时间运行,是因为它的 IO 限制或等待事件或睡眠时间较长。即做除使用 CPU 之外的事情。如果它使用大量 CPU,但不是 100% 如您所愿,那么它的效率可能不高,原因与我上面列出的相同。
I'm not sure your concern about Task Manager is valid. Task manager is lightweight enough where it won't drown out your long running process and steal enough CPU cycles to matter. If your process really is that CPU hungry you'll see it in task manager. If you're not seeing as much CPU usage as you expect for your process maybe your assumptions that your process takes a while because it uses a lot of CPU are wrong. Perhaps your long running process is long running because its IO bound or waiting for events or sleeps a lot. i.e. doing things OTHER than using CPU. If it uses a lot of CPU but not 100% as you want perhaps its not as efficient as it can be for the same reasons I listed above.
您可以编写一个简单的工具,通过 GetProcessTimes
行中的内容:
you can write a trivial tool which queries that particular process (by id, for instance) via GetProcessTimes
something in the lines:
@martin Becket:procmon 可以让您在特定进程的属性中查看详细的 cpu 使用情况
@martin Becket: procmon lets you see detailed cpu usage in the properties of a specific process