将Docker容器CPU使用率作为百分比获取 [英] Get Docker Container CPU Usage as Percentage

查看:2701
本文介绍了将Docker容器CPU使用率作为百分比获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Docker提供了一个交互式stats命令, docker stats [cid] ,它提供了有关CPU使用情况的最新信息,如下所示:

Docker provides an interactive stats command, docker stats [cid] which gives up to date information on the CPU usage, like so:

CONTAINER      CPU %          MEM USAGE/LIMIT       MEM %       NET I/O
36e8a65d       0.03%          4.086 MiB/7.798 GiB   0.05%       281.3 MiB/288.3 MiB

我试图以可消化的格式获得CPU使用率,以进行一些分析。

I'm trying to get the CPU usage as a percentage in a digestible format to do some analysis.

我看到/ sys / fs中的统计信息似乎提供了与 Docker Remote API ,它给了我这个JSON blob:

I've seen the stats in /sys/fs which seem to provide similar values as the Docker Remote API which gives me this JSON blob:

{
    "cpu_usage": {
        "usage_in_usermode": 345230000000, 
        "total_usage": 430576697133, 
        "percpu_usage": [
            112999686856, 
            106377031910, 
            113291361597, 
            97908616770
        ], 
        "usage_in_kernelmode": 80670000000
    }, 
    "system_cpu_usage": 440576670000000, 
    "throttling_data": {
        "throttled_time": 0, 
        "periods": 0, 
        "throttled_periods": 0
    }
}

但是我不知道如何从中获得正确的CPU使用率。

But I'm unsure how to get an exact CPU Usage as a percentage from that.

任何想法?

推荐答案

如果你要去我们在Stats API调用中,您可以查看docker客户端的操作: https://github.com/docker/docker/blob/eb131c5383db8cac633919f82abad86c99bffbe5/cli/command/container/stats_helpers.go#L175-L188

If you are going to use the Stats API call - you can take a look at how the docker client does it: https://github.com/docker/docker/blob/eb131c5383db8cac633919f82abad86c99bffbe5/cli/command/container/stats_helpers.go#L175-L188

func calculateCPUPercent(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)
    )

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

基本上,你参考一下,然后看10分钟的差异,然后你可以告诉多少容器使用时间。说起来,我们从容器的0 SystemCPUUsage和0 CPUUsage开始。如果10秒钟后,我们有10个SystemCPUUsage和1个CPUUsage,那么我们有10%的使用。您只需要在API中以纳秒(而不是秒)为单位给出结果。实际时间并不重要,SystemCPUUsage的总体更改是重要的,然后比较CPUUSage。

Basically, you take a point of reference, then see the difference in say 10 secs, you can then tell how much of the time was used by the container. Say, we start with 0 SystemCPUUsage and 0 CPUUsage for the container. If after 10 secs, we have 10 SystemCPUUsage and 1 CPUUsage, then we have 10% usage. You are just given the results in nanoseconds, not seconds, in the API. The actual time does not matter, the total SystemCPUUsage change is what matters, then compare CPUUSage to that.

这篇关于将Docker容器CPU使用率作为百分比获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆