以百分比形式获取 Docker 容器 CPU 使用率 [英] Get Docker Container CPU Usage as Percentage

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

问题描述

Docker 提供了一个交互式统计命令,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/eb131c5383db8cac633919f82bad86c99bffbe命令/容器/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天全站免登陆