开发者

Gauge/measure CPU usage without TaskMgr?

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?


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.


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


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.


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);
  }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜