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?
-
1Do *not* set your process to realtime if you want anything else to run (like your mouse cursor). – Gabe Nov 06 '10 at 21:06
5 Answers
Probably procdump - but one of the sysinternals process tools should help

- 94,801
- 28
- 188
- 263
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.

- 100,020
- 15
- 103
- 173
-
C API is not accessible to me, but great suggestion on perfmon! I always forget to use that tool. This article was helpful: http://adminfoo.net/2007/04/windows-perfmon-top-ten-counters.html – Suraj Nov 08 '10 at 17:31
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.

- 2,132
- 15
- 14
-
What would explain taskmgr freezing up when my process is set to RealTime, but works perfectly fine when priority is set to High? My process is mostly CPU-bound (actually I'm just automating Excel and Excel is the real process in question here - its processing a heavy calculation) – Suraj Nov 08 '10 at 17:24
-
Processes that aren't designed to be run in real-time will freeze the rest of the system if they dont yield time. i.e they need to be designed to run with real time priority. Freezing behavior with real time priority could happen with any app not designed for it. – bot403 Nov 08 '10 at 17:55
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);
}

- 476
- 3
- 9
-
I'm sure there's probably a way to do this in .net. I haven't touched c++ in over a decade. Looking for an out-of-the-box tool. Will try other suggestions on this post like perfmon and sysinternals stuff. – Suraj Nov 08 '10 at 17:26
@martin Becket: procmon lets you see detailed cpu usage in the properties of a specific process

- 2,017
- 1
- 21
- 46