Super basic question about CPU utilization
I would like to understand more about how a CPU reaches a utilization level between 0% and 100%.
Let's say I have a super basic single threaded Python program: while True: time.sleep(5) This results in a continually running process that htop shows as 0% CPU utilization. num = 0 while True: num += 1 This, however, shows a CPU utilization of 100%. In my basic mindset, a program is either working or it isn't working. And along that train of thought, I'm confused as to why htop will show some processes as using like 17.0% CPU. What is a program doing that makes it use *less* than 100% but *more* than 0%? |
Quote:
|
A "running" program may be not using CPU time at some specific instant, either because it is waiting for something, or because all the CPU cores are in use by other processes at that instant.
So far as I understand, the OS frequently "samples" which processes are using each CPU core and accumulates statistics based on the approximation that the process using the core at that instant was using the core for the entire tiny interval containing that instant. That is often not true and will overcount the sampled process and fail to count whatever else the core did during that tiny interval. But for typical processes those two errors somewhat cancel each other over a larger number of samples. So your first example is using a tiny amount more than 0%, which you wouldn't see anyway because the % is not reported to enough significant digits. But even if the % were reported to enough digits, the sampling might have entirely missed the CPU use of that process. Your whole question implies you have some idea about what a "CPU" actually is, which differs significantly from reality. Since I have been a software engineer most of my life, it is hard for me to estimate what someone outside the field will or won't understand about computers. So I can answer the specific details of such a question. But if you still have a fundamental lack of understanding of what a CPU is, I don't think I can construct an understandable answer. |
So from these two replies I think I understand something new that I didn't know before: the CPU utilization of a program that is reported is an average over an interval of time. It's not instant. If CPU utilization were reported instantly, then we would see strictly 0% and 100% toggling between each other very rapidly.
For the sake of example let's just define an "instant" as occurring "every one second." If a program is in use on second 1, the CPU utilization at that instant will be 100%. If the program is waiting on I/O on second 2, CPU utilization at that instant will be 0%. Then again it's in use at second 3 for 100%. But let's say what top reports only refreshes every 3 seconds. Then CPU utilization will be the average of 100 + 0 + 100, or 66.6%. I realize this is not how top or CPU utilization reporting works, it's just an example to show why CPU utilization would be reported as anything other than 0% or 100%. This seems to make sense I think because in top, if I execute top, and just let it do it's thing, it seems to refresh about every 4-5 seconds. As a result I see a lot of processes listed as using a small amount of CPU. But if I hold space bar, it refreshes top insanely fast, and suddenly when I do that, the only processes listed as those using CPU are the programs which are *actually* using CPU in that instant. |
Quote:
But I failed to guess that. I just assumed that averaging was a basic fact of the tool displaying the information that would (somehow) be known to anyone using the tool. Then I didn't have any good guess about what was confusing you. I think you understand it now. Your description blurred what I would consider "instant" vs. "interval", so I'm not sure you get that (I think you do). But you correctly described the next level up (sampling interval vs. reporting interval) and the way that results other than 0% or 100% derive from the reporting interval being longer than the sampling interval. |
Another thing you miss is that:
Code:
while True: Code:
num = 0 |
All times are GMT -5. The time now is 05:54 AM. |