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
Calculating CPU usage
Re:Calculating CPU usage
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.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
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
Can't you just calculate the percentage of time slices that the idle kernel thread gets? and then take that away from 100
Pete
Pete
Re:Calculating CPU usage
Of course, you could do that, but who's telling the idle thread he's active?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
hmmm.... active idle threads sounds paradoxal...
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Calculating CPU usage
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.
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.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image
Re:Calculating CPU usage
Tell that to microsoft - their idle thread _IS_ 99% active, so to speakbeyond 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.
Re:Calculating CPU usage
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.
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
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:
and total CPU usage:
- 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;
Code: Select all
CurrentIdleTime = SysInfo->IdleTime - SysInfo->OldIdleTime;
CurrentIdleTime = CurrentIdleTime/(CurrentSystemTime - OldSystemTime);
TotalCpuUsage = 100 - CurrentIdleTime*100 / NumberOfProcessors;