Please forgive my ignorance on such topics, but I was wondering a CPU has instruction pointer (IP) and so can either be using that IP or not use it (IDLE)..
So CPU utilization can be either 0(assuming idle task is doing nothing or just while(1)) or it can be 100...
What do they mean when CPU utilization is 30 or 40 percent.
How is this calculated.
It's for a given time period. For example, in the last second, what percentage of time was spent doing something, and what percentage of time was idle. You are correct that at any given instant the CPU can be thought of as either doing something or not.
Although not focusing on a simple percentage, the Wikipedia article on load gives a simple overview of what goes into calculating how much work a computer is doing.
On most machines, the CPU is always running. The idle% is the time it's just waiting for useful work to do.
Think of it like the person at the register at Burger Barn. They're standing there the whole time, but if there is no customer ordering then they are idle. If in their 100 minute shift, if somebody is in front of them 73 of those minutes, then the cpu utilization is 73%.
Related
I'm having difficulties pinning down where our application is spending its time.
Looking at the flame graphs of an ETW trace from the sampled and the precise CPU Usage, they contradict each other. Below are the graphs for a 1 second duration
According to the "CPU Usage (Sampled)" graph
vbscript.dll!COleScript::ParseScriptText is a big contributor in the total performance.
ws2_32.dll!recv is a small contributor.
According to the "CPU Usage (Precise)" graph
Essentially, this shows it's the other way around?
vbscript.dll!COleScript::ParseScriptText is a small contributor, only taking up 3.95 ms of CPU.
ws2_32.dll!recv is a big contributor, taking up 915,09 ms of CPU.
What am I missing or misinterpreting?
CPU Usage (Sampled)
CPU Usage (Precise)
There is a simple explanation:
CPU Usage (Precise) is based on context switch data and it can therefore give an extremely accurate measure of how much time a thread spends on-CPU - how much time it is running. However because it is based on context switch data it only knows what the call stack is when a thread goes on/off CPU. It has no idea what it is doing in-between. Therefore, CPU Usage (Precise) data knows how much time a thread is using but it has no ideas where that time is spent.
CPU Usage (Sampled) is less accurate in regards to how much CPU time a thread consumes, but it is quite good (statistically speaking, absent systemic bias) at telling you where time is spent.
With CPU Usage (Sampled) you still need to be careful about inclusive versus exclusive time (time spent in a function versus spent in its descendants) in order to interpret data correctly but it sounds like that data is what you want.
For more details see https://randomascii.wordpress.com/2015/09/24/etw-central/ which has documentation of all of the columns for these two tables, as well as case studies of using them both (one to investigate CPU usage scenarios, the other to investigate CPU idle scenarios)
On Windows, GetProcessTimes() and QueryProcessCycleTime() can be used to get totals for all threads of an app. I expected (apparently naively) to find a proportional relationship between the total number of cycles and the total processor time (user + kernel). When converted to the same units (seconds) and expressed at a percent of the app's running time, they're not even close; and the ratio between them varies greatly.
Right after an app starts, they're fairly close.
3.6353% CPU cycles
5.2000% CPU time
0.79 Ratio
But this ratio increases as an app remains idle (below, after 11 hours, mostly idle).
0.0474% CPU cycles
0.0039% CPU time
12.16 Ratio
Apparently, cycles are counted that don't contribute to user or kernel time. I'm curious about how it works. Please enlighten me.
Thanks.
Vince
The GetProcessTimes and the QueryProcessCycleTime values are calculated in different ways. GetProcesTimes/GetThreadTimes are updated in response to timer interrupts, while QueryProcessCycleTime values are based on the tracking of actual thread execution times. These different ways of measuring may cause vastly different timing results when both API results are used and compared. Especially since the GetThreadTimes includes only fully completed time-slot values for its thread counters (see http://blog.kalmbachnet.de/?postid=28), which usually results in incorrect timings.
Since GetProcessTimes will in general report less time than actually spent (due to not always completing its time-slice) it makes sense
that its CPU time percentage will decrease over time compared to the cycle measurement percentage.
I know this question has been asked many times in many different manners, but it's still not clear for me what the CPU load % means.
I'll start explaining how I perceive the concepts now (of course, I might, and sure will, be wrong):
A CPU core can only execute one instruction at a time. It will not execute the next instruction until it finishes executing the current one.
Suppose your box has one single CPU with one single core. Parallel computing is hence not possible. Your OS's scheduler will pick up a process, set the IP to the entry point, and send that instruction to the CPU. It won't move to the next instruction until the CPU finishes executing the current instruction. After a certain amount of time it will switch to another process, and so on. But it will never switch to another process if the CPU is currently executing an instruction. It will wait until the CPU becomes free to switch to another process. Since you only have one single core, you can't have two processes executing simultaneously.
I/O is expensive. Whenever a process wants to read a file from the disk, it has to wait until the disk accomplishes its task, and the current process can't execute its next instruction until then. The CPU is not doing anything while the disk is working, and so our OS will switch to another process until the disk finishes its job in order not to waste time.
Following these principles, I've come myself to the conclusion that CPU load at a given time can only be one of the following two values:
0% - Idle. CPU is doing nothing at all.
100% - Busy. CPU is currently executing an instruction.
This is obviously false as taskmgr reports %1, 12%, 15%, 50%, etc. CPU usage values.
What does it mean that a given process, at a given time, is utilizing 1% of a given CPU core (as reported by taskmgr)? While that given process is executing, what happens with the 99%?
What does it mean that the overall CPU usage is 19% (as reported by Rainmeter at the moment)?
If you look into the task manager on Windows there is Idle process, that does exactly that, it just shows amount of cycles not doing anything useful. Yes, CPU is always busy, but it might be just running in a loop waiting for useful things to come.
Since you only have one single core, you can't have two processes
executing simultaneously.
This is not really true. Yes, true parallelism is not possible with single core, but you can create illusion of one with preemptive multitasking. Yes, it is impossible to interrupt instruction, but it is not a problem because most of the instructions require tiny amount of time to finish. OS shares time with time slices, which are significantly longer than execution time of single instruction.
What does it mean that a given process, at a given time, is utilizing 1% of a given CPU core
Most of the time applications are not doing anything useful. Think of application that waits for user to click a button to start processing something. This app doesn't need CPU, so it sleeps most of the time, or every time it gets time slice it just goes into sleep (see event loop in Windows). GetMessage is blocking, so it means that thread will sleep until message arrives. So what CPU load really means? So imagine the app receives some events or data to do things, it will do operations instead of sleeping. So if it utilizes X% of CPU means that over sampling period of time that app used X% of CPU time. CPU time usage is average metric.
PS: To summarize concept of CPU load, think of speed (in terms of physics). There are instantaneous and average speeds, so speaking of CPU load, there also are instantaneous and average measurements. Instantaneous is always equal to either 0% or 100%, because at some point of time process either uses CPU or not. If process used 100% of CPU in the course of 250ms and didn't use for next 750ms then we can say that process loaded CPU for 25% with sampling period of 1 second (average measurement can only be applied with certain sampling period).
http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages
A single-core CPU is like a single lane of traffic. Imagine you are a bridge operator ... sometimes your bridge is so busy there are cars lined up to cross. You want to let folks know how traffic is moving on your bridge. A decent metric would be how many cars are waiting at a particular time. If no cars are waiting, incoming drivers know they can drive across right away. If cars are backed up, drivers know they're in for delays.
This is basically what CPU load is. "Cars" are processes using a slice of CPU time ("crossing the bridge") or queued up to use the CPU. Unix refers to this as the run-queue length: the sum of the number of processes that are currently running plus the number that are waiting (queued) to run.
Also see: http://en.wikipedia.org/wiki/Load_(computing)
I'm wondering I always see my load average on my computer, 1.76,1.31,1.08 at the moment. What does it mean ?
If your CPU were a hot dog stand, the load averages would tell you the average number of people standing in line to get served. A load of less than 1.0 means that the hot dog vendor has some spare time between customers -- 1.0 means that, while a line never piles up, some customer is always talking to the vendor (who knows where all the hot dogs are going...). Having a "low" load average doesn't mean that your computer isn't doing anything though, there could be customers who take a really long time to eat their hot dog before getting in line again (that is to say some process might be waiting on the disk or network to wake them up with some data when it arrives), having a faster disk or net connection could improves your total dog sales.
Enjoy.
The load average tries to measure the number of active processes at any time. As a measure of CPU utilization, the load average is simplistic, poorly defined, but far from useless. High load averages usually mean that the system is being used heavily and the response time is correspondingly slow. What's high? ... Ideally, you'd like a load average under, say, 3, ... Ultimately, 'high' means high enough so that you don't need uptime to tell you that the system is overloaded.
When seeing the results of the load averages, they are for the past 1, 5, and 15 minute
The load indicates how man processes are waiting in the queue to be executed. You would have a load of 1 if your have that much processes that at least one process has always to queue up before it gets executed (is not executed immediately).
The numbers in uptime are average over some period.
The Windows Task Manager shows CPU usage in percentage. What's the formula behind this? Is it this:
% CPU usage for process A = (Sum of
all time slices given to A till now)/
Total time since the machine booted
Or is it something else?
I am not 100% sure what is uses, but I think you are a bit off on the CPU calculation.
I believe they are doing something like.
Process A CPU Usage = (Cycles for A over last X seconds)/(Total cycles for last X seconds)
I believe it is tied to the "update interval" set in task manager.
While doing a bit of research for you though I found this MSDN article that shows a microsoft recommended way of calculating the CPU time of a set of instructions, this might point you a bit towards their calculation as well.
No, it's not "since boot time" - it's far more time-sensitive than that.
It's "proportion of time during which a CPU was actively running a thread in that process since the last refresh". (Where the refresh rate is typically about a second.) In task manager I believe it's then divided by the number of CPUs, so the total ends up being 100% (i.e. on a dual core machine, a single-threaded CPU hog will show as 50%). Other similar programs sometimes don't do this, giving a total of 100% * cores.
You may also want to check this article as the way CPU cycles are handled with regards to scheduling was changed as part of Vista. I presume that this also applies to Win7.
See the source code of Task Manager