## digitalmars.D - Measuring CPU load

- Mike (25/25) Dec 13 2007 Hi!
- Tomas Lindquist Olsen (3/33) Dec 14 2007 RDTSC is no longer safe on multicore systems. Wikipedia has some notes o...
- BCS (3/21) Dec 14 2007 this is only valid if the thread is not context switched.

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. -MikeRDTSC 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