LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-05-2014, 09:15 AM   #1
CptSupermrkt
LQ Newbie
 
Registered: Dec 2014
Posts: 5

Rep: Reputation: Disabled
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%?
 
Old 12-05-2014, 09:46 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,511

Rep: Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541
Quote:
Originally Posted by CptSupermrkt View Post
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%?
Waiting for I/O, most likely. The CPU is available for other processes during that time. The reported CPU utilization is an average over some time interval, and a program that is doing I/O will sometimes be running, sometimes waiting.

Last edited by rknichols; 12-05-2014 at 09:48 AM.
 
Old 12-05-2014, 10:15 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
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.

Last edited by johnsfine; 12-05-2014 at 10:25 AM.
 
Old 12-05-2014, 11:07 AM   #4
CptSupermrkt
LQ Newbie
 
Registered: Dec 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-05-2014, 01:07 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
Quote:
Originally Posted by CptSupermrkt View Post
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.
In hindsight that is what I should have guessed from your first post was the key missing piece in your understanding.

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.
 
Old 12-05-2014, 05:02 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,704

Rep: Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270
Another thing you miss is that:
Code:
while True:
time.sleep(5)
Will use the CPU for a VERY short time - then it sleeps for 5 seconds, which means it is no longer using the CPU. And that results in a CPU time of near 0%.

Code:
num = 0
while True:
num += 1
Will stay on the CPU forever adding one to num - thus the CPU usage will be 100%... minus that small fraction given to htop (unless it is running on a different core).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
High Run queue utilization although CPU utilization is low.. rajeprag Linux - Server 0 08-18-2013 09:44 AM
how to find all processes cpu utilization & the cpu processor in linux? vasanth.v999 Linux - Server 7 01-10-2012 08:43 AM
High CPU utilization on only one CPU out of 4 CPUs makam.sreekanth Linux - Kernel 2 09-03-2009 04:12 AM
cpu utilization and cpu load soltanloo Linux - General 1 06-07-2009 02:19 AM
Linux desktop GUI tool for CPU utilization and bandwidth utilization karimasif Linux - Software 4 03-03-2008 01:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration