Page 1 of 1
Calculating CPU usage
Posted: Sat May 01, 2004 7:07 am
by Thunder
Hello,
my OS handles software multitasing with 5 different privilege levels. But I'm wondering, how to calculate CPU usage ???
Because as I know CPU runs at its speed maximum, then how to calculate this CPU usage?
Is it connected with proccess working time per second or something?
Thanks
Re:Calculating CPU usage
Posted: Sat May 01, 2004 7:53 am
by Candy
Thunder wrote:
Hello,
my OS handles software multitasing with 5 different privilege levels. But I'm wondering, how to calculate CPU usage ???
Because as I know CPU runs at its speed maximum, then how to calculate this CPU usage?
Is it connected with proccess working time per second or something?
Thanks
practical load: count the number of times you got a timer interrupt with a thread started, and compare that with the number of timer interrupts you got.
Theoretical load (you cannot get a practical load >nproc): count the number of available threads at each timer interrupt, and divide that through the number of interrupts you got
Re:Calculating CPU usage
Posted: Mon May 03, 2004 12:05 am
by Therx
Can't you just calculate the percentage of time slices that the idle kernel thread gets? and then take that away from 100
Pete
Re:Calculating CPU usage
Posted: Mon May 03, 2004 1:01 am
by Candy
Pete wrote:
Can't you just calculate the percentage of time slices that the idle kernel thread gets? and then take that away from 100
Pete
Of course, you could do that, but who's telling the idle thread he's active?
hmmm.... active idle threads sounds paradoxal...
Re:Calculating CPU usage
Posted: Mon May 03, 2004 1:37 am
by distantvoices
You'd need some instance which continuously watches all the processes and measures the time they spend with the cpu - in a time window - and then do the calculation. Like the task manager of windows or the top-command of linux.
Forget this idle thread stuff. The idle thread is no means of checking processor activity - especially if you say "hlt". It would appear to have 99 % of processor time but that's simply not true.
stay safe.
Re:Calculating CPU usage
Posted: Mon May 03, 2004 2:38 am
by Candy
beyond infinity wrote:
Forget this idle thread stuff. The idle thread is no means of checking processor activity - especially if you say "hlt". It would appear to have 99 % of processor time but that's simply not true.
Tell that to microsoft
- their idle thread _IS_ 99% active, so to speak
Re:Calculating CPU usage
Posted: Mon May 03, 2004 2:46 am
by BI lazy
What do you think where I've taken the example from?
.
I'd like to know what this special idle thread is doing... attempting to phone home in case nothing else is to do? *gg* Best to have a firewall to prevent such activities.
Re:Calculating CPU usage
Posted: Mon May 03, 2004 1:02 pm
by jbuba
The Windows idle thread does the following actions in a loop:
- Call HalProcessorIdle (which == HLT).
- Empty the DPC queue if it is not empty.
- Check to see if any other threads are available on the ready queues and switch if available.
The idle thread is only run when the ready queues are empty.
Because of the way Task Manager calculates each process CPU usage, the idle thread winds up showing 99% when no other thread is using the CPU.
You can see how individual process CPU usage is calculated by looking at the ReactOS Task Manager source:
Code: Select all
CurrentProcessTime = Process->KernelTime + Process->UserTime;
OldProcessTime = Process->OldKernelTime + Process->OldUserTime;
ProcessCpuUsage = (CurrentProcessTime - OldProcessTime)/(CurrentSystemTime - OldSystemTime);
ProcessCpuUsage = ProcessCpuUsage*100 / NumberOfProcessors;
and total CPU usage:
Code: Select all
CurrentIdleTime = SysInfo->IdleTime - SysInfo->OldIdleTime;
CurrentIdleTime = CurrentIdleTime/(CurrentSystemTime - OldSystemTime);
TotalCpuUsage = 100 - CurrentIdleTime*100 / NumberOfProcessors;