Calculating CPU usage

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Thunder

Calculating CPU usage

Post 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
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Calculating CPU usage

Post 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
Therx

Re:Calculating CPU usage

Post 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
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Calculating CPU usage

Post 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...
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Calculating CPU usage

Post 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.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Calculating CPU usage

Post 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 :D - their idle thread _IS_ 99% active, so to speak :)
BI lazy

Re:Calculating CPU usage

Post by BI lazy »

What do you think where I've taken the example from? 8).

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

Re:Calculating CPU usage

Post 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;
Post Reply