Gauge/measure CPU usage without TaskMgr?

倖福魔咒の 提交于 2019-12-24 10:57:56

问题


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?


回答1:


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.




回答2:


Probably procdump - but one of the sysinternals process tools should help




回答3:


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.




回答4:


you can write a trivial tool which queries that particular process (by id, for instance) via GetProcessTimes

something in the lines:

main()
  HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, id);
  for(;;) { 
    GetProcessTimes(h, /*..times.. */);
    printf("time taken %d\n", (kerneltime+usertime));
    Sleep(1000);
  }



回答5:


@martin Becket: procmon lets you see detailed cpu usage in the properties of a specific process



来源:https://stackoverflow.com/questions/4109345/gauge-measure-cpu-usage-without-taskmgr

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