Calculate CPU Load

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.
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Calculate CPU Load

Post by tsdnz »

Hi, I have designed a single state task machine with no task scheduler.
The CPUs run their tasks without getting switched.
Is there a way to get the CPU load?
All tasks are random in time, some take over 1 second.

Any ideas?
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Calculate CPU Load

Post by thepowersgang »

What does CPU load mean in this case?

If a CPU runs a task to completion then starts on the next one, load is meaningless.

Usually, load means "how much time does the CPU spend not idle". For this to have meaning, there must be an idle state (which is usualy an idle task that puts the CPU into a low power mode).
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Calculate CPU Load

Post by Love4Boobies »

thepowersgang wrote:What does CPU load mean in this case?
When a male CPU and a female CPU love each other...
thepowersgang wrote:If a CPU runs a task to completion then starts on the next one, load is meaningless.

Usually, load means "how much time does the CPU spend not idle". For this to have meaning, there must be an idle state (which is usualy an idle task that puts the CPU into a low power mode).
Well, you might say that you have full load in that case, or full load minus the idle time spent on I/O, interrupt processing, etc.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: Calculate CPU Load

Post by tsdnz »

I would like to query each CPU and get the load (time executing, without idle state, using hlt for idle).
After some kind of counter for each CPU, I see many counters but nothing I can use for load etc.
My real time clock interrupt could either remember the counter or reset it.
Based on maximum cycles per interrupt I can calculate performance percentages.
My RTC runs 2 times per second.

Anyone know of any such counters?
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: Calculate CPU Load

Post by bwat »

CPU load = CPU utilisation. Basically what percentage of time the CPU was doing something useful. From your question it looks like you know this already. Maybe you're making things more complicated than they need to be if you want to include CPU counters. I'm 99% certain you know how to calculate percentages and if you just put pen to paper and tried to figure this out, then you would come up with a solution (*).
thepowersgang wrote:If a CPU runs a task to completion then starts on the next one, load is meaningless.
No! CPU utilisation has nothing to do with the scheduling algorithm.
Love4Boobies wrote:Well, you might say that you have full load in that case, or full load minus the idle time spent on I/O, interrupt processing, etc.
Well, I would count interrupt processing - it is useful work. Why wouldn't you include it?


(*) The only interesting thing about this problem would be figuring out how to calculate it smartly.
Every universe of discourse has its logical structure --- S. K. Langer.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Calculate CPU Load

Post by Love4Boobies »

bwat wrote:
Love4Boobies wrote:Well, you might say that you have full load in that case, or full load minus the idle time spent on I/O, interrupt processing, etc.
Well, I would count interrupt processing - it is useful work. Why wouldn't you include it?
Because the user cares about their workload, not the OS does under the hood. I think it's good that you've asked because the OP can see that different OSes can have different policies on how they do the bookkeeping. Implementing it in hardware could potentially make this more difficult.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: Calculate CPU Load

Post by tsdnz »

Calculating this load is not high on my list. Currently I use my own counters and check them against a maximum value, with a bit of add's and div's I get a good approx value.
I thought I might have missed something inside the CPU counters etc.
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: Calculate CPU Load

Post by bwat »

Love4Boobies wrote:
bwat wrote:
Love4Boobies wrote:Well, you might say that you have full load in that case, or full load minus the idle time spent on I/O, interrupt processing, etc.
Well, I would count interrupt processing - it is useful work. Why wouldn't you include it?
Because the user cares about their workload, not the OS does under the hood. I think it's good that you've asked because the OP can see that different OSes can have different policies on how they do the bookkeeping. Implementing it in hardware could potentially make this more difficult.

What OS doesn't include interrupt processing in the CPU load calculation?

Edit: added this:
CPU load is not a user issue, it's a system issue. Consider this: I take an machine then flood it with input which generates a continuous stream of interrupts - as soon as one interrupt was serviced a new one was generated. With my method of measuring CPU load, the administrator could determine that the CPU load was approaching 100% and this would explain an increase in response times experienced by application programs. Your method would inaccurately report that the CPU had spare capacity yet response times had increased.
Every universe of discourse has its logical structure --- S. K. Langer.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Calculate CPU Load

Post by Brendan »

Hi,

It depends which load you're measuring. There's "global CPU load" (time CPU spends not idle) that should include everything except idle time. There's also "per-process CPU load", which may or may not include CPU time consumed by other things on behalf of that process.

For performance monitoring counters; most 80x86 CPUs have a "time spent not halted" counter that you can use to work out "global CPU load". You can also use it (e.g. by doing a little accounting during task switches) to keep track of "per-process CPU load".

However; for 100% accurate results it gets excessively tricky once you start thinking about things like hyper-threading. For example, one thread pounding the daylights out of a logical CPU and using most of the core while another thread is constantly waiting for fetches from RAM and not using much of the core. The other thing that makes a mess of it is power management. For example, if a low priority thread uses 1 second of CPU time while the CPU is in a low power state (e.g. throttled to 50% of its normal clock speed), and another high priority thread uses 1 second of CPU time while getting the benefit of a 20% "turbo boost"; then surely the different threads caused very different CPU loads despite running for 1 second each.

To improve accuracy in these cases, you might want to consider using "instructions retired" (and normalising it against "max. instructions the CPU could've retired when running at its max. speed"). In this case, (for hyper-threading) you can use front-end tagging to determine how many instructions each logical CPU in a core executed separately.

Of course for most OS's, both "global CPU load" and "per process CPU load" is no better than a crude estimate, and is only ever accurate if you're lucky (in a "stopped clock is right twice a day" way).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Calculate CPU Load

Post by Love4Boobies »

bwat wrote:What OS doesn't include interrupt processing in the CPU load calculation?
Um, who the hell cares?
bwat wrote:CPU load is not a user issue, it's a system issue. Consider this: I take an machine then flood it with input which generates a continuous stream of interrupts - as soon as one interrupt was serviced a new one was generated. With my method of measuring CPU load, the administrator could determine that the CPU load was approaching 100% and this would explain an increase in response times experienced by application programs. Your method would inaccurately report that the CPU had spare capacity yet response times had increased.
Not sure which part of the word "policy" you don't understand. If I want to measure how much of the workload is being processed by the CPU, then I can do that. If none of the workload gets processed because of interrupts, then a load of 0% will indicate that. A load of 100% might indicate anything---who knows what is being processed? Policy.
Brendan wrote:It depends which load you're measuring. There's "global CPU load" (time CPU spends not idle) that should include everything except idle time. There's also "per-process CPU load", which may or may not include CPU time consumed by other things on behalf of that process.
Sure. But I can think of other potentially useful ways to measure the load, too.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: Calculate CPU Load

Post by bwat »

Love4Boobies wrote: Um, who the hell cares?
Those who seek a demonstration, i.e. proof, to justify your conclusion. That would be me.
Love4Boobies wrote: Not sure which part of the word "policy" you don't understand. If I want to measure how much of the workload is being processed by the CPU, then I can do that. If none of the workload gets processed because of interrupts, then a load of 0% will indicate that. A load of 100% might indicate anything---who knows what is being processed? Policy.
i) But a load of 0% will also indicate idleness which means your method will lead to a possible ambiguous report. My method does not.
ii) A load of 100% indicates lack of spare capacity and nothing else. What is being processed does not alter load, the fact it is being processed does.
iii) A given CPU load is the ratio of use time to idle time, that's it. It has nothing to do with policy. If you had no spare cycles and you altered policy then you would still have no spare cycles. In fact we can generalise, if you had X% of CPU cycles free then a change in policy wouldn't alter that X%. We can therefore draw the conclusion that policy is non-essential. It doesn't need to be understood, it can be safely ignored.
Every universe of discourse has its logical structure --- S. K. Langer.
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: Calculate CPU Load

Post by bwat »

Brendan wrote:Hi,

It depends which load you're measuring. There's "global CPU load" (time CPU spends not idle) that should include everything except idle time. There's also "per-process CPU load", which may or may not include CPU time consumed by other things on behalf of that process.
Yes, of course, it is a question of definition. You are quite correct to bring this up.
Brendan wrote: For performance monitoring counters; most 80x86 CPUs have a "time spent not halted" counter that you can use to work out "global CPU load". You can also use it (e.g. by doing a little accounting during task switches) to keep track of "per-process CPU load".
This would be ideal.
Brendan wrote: However; for 100% accurate results it gets excessively tricky once you start thinking about things like hyper-threading. For example, one thread pounding the daylights out of a logical CPU and using most of the core while another thread is constantly waiting for fetches from RAM and not using much of the core.
Yes, this level granularity is interesting for those who wish to improve performance.
Brendan wrote: The other thing that makes a mess of it is power management. For example, if a low priority thread uses 1 second of CPU time while the CPU is in a low power state (e.g. throttled to 50% of its normal clock speed), and another high priority thread uses 1 second of CPU time while getting the benefit of a 20% "turbo boost"; then surely the different threads caused very different CPU loads despite running for 1 second each.

To improve accuracy in these cases, you might want to consider using "instructions retired" (and normalising it against "max. instructions the CPU could've retired when running at its max. speed"). In this case, (for hyper-threading) you can use front-end tagging to determine how many instructions each logical CPU in a core executed separately.
Clockspeed determines throughput and your normalisation would make a good benchmark indicator. However, there are cases where CPU load can be interesting regardless of what power management policy is in place. This should indicate to us that we don't want to combine the two into one metric, let's provide both to the administrator.
Brendan wrote: Of course for most OS's, both "global CPU load" and "per process CPU load" is no better than a crude estimate, and is only ever accurate if you're lucky (in a "stopped clock is right twice a day" way).
Do you not think that with a timer with a period that was both relatively short and relatively prime to the system clock that the global CPU load result would be very accurate. On 32-bit machines with floats or with fixed-point reals, accuracy would be fairly good too.
Every universe of discourse has its logical structure --- S. K. Langer.
html2wordpresstheme
Posts: 2
Joined: Mon Oct 28, 2013 3:23 am
Location: USA
Contact:

Re: Calculate CPU Load

Post by html2wordpresstheme »

Cpu load is calculated by no. of task running by cpu in a time frame. You can write a simple program.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Calculate CPU Load

Post by Love4Boobies »

bwat wrote:Those who seek a demonstration, i.e. proof, to justify your conclusion. That would be me.
Definitions are defined, not proved.
bwat wrote:...the rest is utter nonsense...
You don't seem to understand what a policy is. It's not the algorithm by which you measure something. E.g., compare the foreign policy of your country with the foreign policy of another country. We aren't measuring the same thing so stop repeating yourself, stomping your foot saying "X is Y, that's it," and inventing crazy stories about how the second half of the method I mentioned viewed through the first half of yours leads to inconsistent results (i.e., pretending you're measuring something else and then being surprised at the result).
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: Calculate CPU Load

Post by bwat »

Love4Boobies wrote: Definitions are defined, not proved.
So we'll just take your word for it, take it on faith?
Love4Boobies wrote:
bwat wrote:...the rest is utter nonsense...
Please don't make up my words. I didn't write what you attribute to me. Stop it! It is disrespectful and childish. You were offered an argument as it was assumed you could handle reasoned discourse. What do you offer in return: "the rest is utter nonsense". When you are offered argument it is because you are respected: someone wants to demonstrate to you and at the same time they are offering you a chance to decide for yourself. Your definitions are a forceful and respectless power-play.
Love4Boobies wrote: You don't seem to understand what a policy is. It's not the algorithm by which you measure something. E.g., compare the foreign policy of your country with the foreign policy of another country. We aren't measuring the same thing so stop repeating yourself, stomping your foot saying "X is Y, that's it," and inventing crazy stories about how the second half of the method I mentioned viewed through the first half of yours leads to inconsistent results (i.e., pretending you're measuring something else and then being surprised at the result).
Instead of just brushing off my "crazy stories", how about evidence contradicting my claims? Instead of your self proclaimed axiomatic definitions, how about reasoned argument? Instead of specious analogical reasoning, how about logic?

I claimed policy was non-essential. Do you agree? If not prove me wrong. Can you tell us how policy is essential in the determination of idle CPU cycles?
Every universe of discourse has its logical structure --- S. K. Langer.
Post Reply