Measuring CPU load

Tomas Lindquist Olsen tomas at famolsen.dk
Fri Dec 14 03:10:31 PST 2007


Mike wrote:
> Hi!
> 
> It's not really D related, I know, but I don't know where else to ask.
> 
> So: I've got this little class that measures in/out time of threads with 
> the rdtsc assembler opcode. The relevant thread gets started 
> periodically, takes time of start in[t] at the entry point and time of 
> exit out [t] with "scope (exit) ...".
> 
> This gives me the following measurments:
> 
> in[t]   // thread entered (current iteration)
> out[t]  // thread exited (current iteration)
> in[t-1] // thread entered (last iteration)
> 
> And the following formula:
> 
> cpuload = 100 * (out[t] - in[t]) / (in[t] - in[t-1]);
> 
> to get the cpu load in %.
> 
> Now I've tested this and I'm not quite sure. The results are at least 
> somewhat  reasonable, although they seem to be double the real value 
> (I've got a dual core system) so that might explain that.
> 
> But before guessing around I thought I'll ask here if this way of 
> measuring is completely wrong or if there's a better way to do that.
> 
> -Mike
> 

RDTSC is no longer safe on multicore systems. Wikipedia has some notes on it:
http://en.wikipedia.org/wiki/RDTSC



More information about the Digitalmars-d mailing list