Docker CPU percentage

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

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?

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?

Hi

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.

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.

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.