views:

175

answers:

3

Is there a way to fetch the system cpu load in windows xp via a DLL call or other means. I would like to embed the call in my program and when the CPU load hits 100% I would like to capture the time. My program is maxes out cpu load every couple of days, and I would like to figure out precisely when so I can do a data dump and analyze the data.

Thanks!

A: 

NtQuerySystemInformation() looks relevant, but seems to be (semi-)deprecated/internal, and not recommended for use. It does list suggestions of other functions to use. Of those, it seems GetSystemTimes() should be useful, if you run it twice with a known delay between.

unwind
+1  A: 

You can use performance counters.

See "Using the PDH Functions to Consume Counter Data"

The specific counter you should use is Processor Time:

Processor Time is the percentage of time that the processor is executing a non-Idle thread. This counter was designed as a primary indicator of processor activity. It is calculated by measuring the time that the processor spends executing the thread of The Idle process- in each sample interval, and subtracting that value from 100%. (Each processor has an Idle thread which consumes cycles when no other threads are ready to run). It can be viewed as the percentage of the sample interval spent doing useful work. This counter displays the average percentage of busy time observed during the sample interval. It is calculated by monitoring the time the service was inactive, and then subtracting that value from 100%.

#define _PROCESSOR_COUNTER _T("\\Processor(0)\\% Processor Time")
Lior Kogan
A: 

Apparently you only care when/if your program is using 100% of the CPU. To make this work as part of the same process, you'll want to run the moinitoring code in a separate thread with elevated priority (so it'll run even if the rest of the program is consuming 100% of the CPU). It'll spend most of its time sleeping, but periodically call GetProcessTimes(), to find out how much CPU time its process is consuming.

Jerry Coffin