Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container CPU usage exceeds 100% sometimes when streaming from "docker stats"

Tags:

docker

Docker container statistics exceeds 100% when using docker stats how is that possible? Shouldn't the maximum CPU be <= 100?

Similar output:

$ docker stats

CONTAINER ID        NAME                                    CPU %               
b95a83497c91        awesome_brattain                        152.28%                              
67b2525d8ad1        foobar                                  0.00%                           
e5c383697914        test-1951.1.kay7x1lh1twk9c0oig50sd5tr   0.00%               
4bda148efbc0        random.1.vnc8on831idyr42slu578u3cr      0.00% 

My use case is that I am trying to find the maximum usage that a container can acquire out of the current bare-metal host's CPU.

If the reporting result is on the count of the available cores. Would the maximum be 400% if I have 4-thread CPU? or 200% because I only have 2 cores?

like image 287
ndrwnaguib Avatar asked Sep 02 '25 13:09

ndrwnaguib


1 Answers

It's because you can have more than one CPU core, which is likely the case. 100% CPU means one core is totally occupied.

Edit: while I didn't find a good reference for this, I went digging into the source code:

func calculateCPUPercentUnix(previousCPU, previousSystem uint64, v *types.StatsJSON) float64 {
    var (
        cpuPercent = 0.0
        // calculate the change for the cpu usage of the container in between readings
        cpuDelta = float64(v.CPUStats.CPUUsage.TotalUsage) - float64(previousCPU)
        // calculate the change for the entire system between readings
        systemDelta = float64(v.CPUStats.SystemUsage) - float64(previousSystem)
        onlineCPUs  = float64(v.CPUStats.OnlineCPUs)
    )

    if onlineCPUs == 0.0 {
        onlineCPUs = float64(len(v.CPUStats.CPUUsage.PercpuUsage))
    }
    if systemDelta > 0.0 && cpuDelta > 0.0 {
        cpuPercent = (cpuDelta / systemDelta) * onlineCPUs * 100.0
    }
    return cpuPercent
}

https://github.com/docker/docker-ce/blob/master/components/cli/cli/command/container/stats_helpers.go#L181

This is the code used by the CLI stats tool that you used in your example.

like image 194
Uku Loskit Avatar answered Sep 05 '25 07:09

Uku Loskit