Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can the CPU usage (output of top) be more than 100% in sum?

I am writing a little python script to test some stuff. later I want to use it to create resource usage plots with gnuplot, but first a few tests.

The script looks like

import subprocess

result = subprocess.check_output("top -b -n 1 -c", shell=True).split("\n")

head = result[:5]
body = [x for x in result[7:] if x] #removes empty strings

for line in head:
    print line

csum = 0.0
for line in body:
    print line
    csum += float(line.split()[8])

print "CPU usage of all processes added up", csum, "%"

Running it multiple times almost always resulted in a shown CPU usage > 100%. Sometimes even > 200%. How can this be?

It runs in a virtual machine (virtualbox, ubuntu 14.04 64 bit) with two cores. The host also has two cores.

Shouldn't the sum of the usage values of all running processes be always lower than 100%? I am running htop at the same time and this shows me about 50% load on every core....

Could the problem be that maybe some processes started others and both are shown in the output of top while the parent process also shows the cpu usage of the child? ==> child gets counted twice?

like image 246
Simon Hessner Avatar asked Dec 04 '22 03:12

Simon Hessner


2 Answers

a 100% cpu is a full utilization of 1 CPU/CORE/Thread. If you have, 8 CPU, then the maximum will be 800%.

If you have thread, the story is a bit more complicated, since a thread is not a real CPU, but, on Linux it is counted as a CPU.

like image 144
Breno Leitão Avatar answered Dec 06 '22 17:12

Breno Leitão


In my experience I had an oversized SQL database that was causing problems. This was because it caused the mysqld (mysql daemon) to execute at over the capacity of the system memory causing the server to crash. When i tried the 'top' command in UNIX the system showed that the process was using over 100% of memory. Thus processes can utilise more than 100% of system memory.

like image 20
Freddie Barr-Smith Avatar answered Dec 06 '22 18:12

Freddie Barr-Smith