## digitalmars.D - Measuring CPU load

- Mike <vertex gmx.at> Dec 13 2007
- Tomas Lindquist Olsen <tomas famolsen.dk> Dec 14 2007
- BCS <ao pathlink.com> Dec 14 2007

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 periodicall= y, = takes time of start in[t] at the entry point and time of exit out [t] wi= th = "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 =3D 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 -- = Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Dec 13 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

Dec 14 2007

Reply to mike,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]);

this is only valid if the thread is not context switched. the ony reliable way to get CPU loads I can think of would be to ask the OS.

Dec 14 2007