Docker Community Forums

Share and learn in the Docker community.

Docker CPU percentage


(Emad88) #1

Any way to get the cpu percentage through the API? Or any easy calculations to get the percentage?

(Timgriffiths) #2

Sounds like this is what you want

GET /containers/(id)/stats

lifted straight from the docks

So then you need to do basically take two measurements subtract the current from the previous then divide by the time delta, think that’s what you will need to do.

Any reason your not using something like cadvisor to do all this hardwork for you?

(Getvivekv) #3

I am attempting to get the cpu percentage. I have cadvisor saving metrics to influxdb and I am reading the db to get the cpu percentage for the last 1 minute

[us-uat-sl.2.jfe330rvo4ond3ja0jal5rpys] => Array
            [cpu_usage_system] => Array
                    [current] => 2221163589743.6
                    [previous] => 2220552307692.3

            [cpu_usage_total] => Array
                    [current] => 2931552316127.5
                    [previous] => 2929715662161.9


I have 4 core cpu. How do I know from the above metrics the cpu percentage?

(Emad88) #4


here is the formula to get the cpu percentage:

cpuPercentage = (cpuDelta/systemDelta) x 100

Here cpuDelta is the current total CPU usage minus previousTotalCPUusage, depending how regularly we are checking the CPU. And systemDelta is the current system CPU usage minus previousSystemCPUusage.

(Getvivekv) #5

Would this still be accurate if I take MEAN() [average] of the values and calculate?

I am sure I tested this formula yesterday with an average cpu values for over a minute, and I haven’t got the correct output. I’ll test it again.

(Getvivekv) #6

I am having trouble with this formula. Here are my values

cpu usage total current second = 59670000000
cpu usage total previous second = 59360000000

cpu system usage current second = 280519631913
cpu system usage previous second = 279406375144

cpu delta = 59670000000 - 59360000000 = 310000000
cpu system detal = 280519631913 - 279406375144 = 1113256769

percentage = ( 310000000 / 1113256769 ) * 100
= 27.84622637223776

However, docker stats, shows only less than 0.9 percentage cpu use. It doesn’t make any sense.