简体   繁体   English

以百分比形式获取 Docker 容器 CPU 使用率

[英]Get Docker Container CPU Usage as Percentage

Docker provides an interactive stats command, docker stats [cid] which gives up to date information on the CPU usage, like so: Docker 提供了一个交互式 stats 命令, docker stats [cid] ,它提供有关 CPU 使用率的最新信息,如下所示:

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

I'm trying to get the CPU usage as a percentage in a digestible format to do some analysis.我正在尝试以可消化的格式获取 CPU 使用率的百分比以进行一些分析。

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:我已经看到 /sys/fs 中的统计信息似乎提供了与Docker Remote API类似的值,它给了我这个 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
    }
}

But I'm unsure how to get an exact CPU Usage as a percentage from that.但我不确定如何从中获得准确的 CPU 使用率百分比。

Any ideas?有任何想法吗?

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如果你打算使用 Stats API 调用 - 你可以看看 docker 客户端是如何做到的: 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
}

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.基本上,您获取一个参考点,然后查看 10 秒内的差异,然后您就可以知道容器使用了多少时间。 Say, we start with 0 SystemCPUUsage and 0 CPUUsage for the container.假设我们从容器的 0 SystemCPUUsage 和 0 CPUUsage 开始。 If after 10 secs, we have 10 SystemCPUUsage and 1 CPUUsage, then we have 10% usage.如果 10 秒后,我们有 10 SystemCPUUsage 和 1 CPUUsage,那么我们有 10% 的使用率。 You are just given the results in nanoseconds, not seconds, in the API.在 API 中,您只获得以纳秒为单位的结果,而不是以秒为单位。 The actual time does not matter, the total SystemCPUUsage change is what matters, then compare CPUUSage to that.实际时间并不重要,重要的是 SystemCPUUsage 的总变化,然后将 CPUUSage 与它进行比较。

After we consume the remote api we get these fields: precpu_stats/cpu_stats在我们使用远程 api 后,我们得到这些字段: precpu_stats/cpu_stats

Then, basically here is the code: (javascript example)然后,基本上这里是代码:(javascript示例)

var res <---- remote api response

var cpuDelta = res.cpu_stats.cpu_usage.total_usage -  res.precpu_stats.cpu_usage.total_usage;
var systemDelta = res.cpu_stats.system_cpu_usage - res.precpu_stats.system_cpu_usage;
var RESULT_CPU_USAGE = cpuDelta / systemDelta * 100;

Just to clarify the RESULT_CPU_USAGE ... it's the amount of resource consumed from your physical hardware, so supposing you are getting RESULT_CPU_USAGE as 50% , it means that 50% of all your PC power is being used by container X只是为了澄清RESULT_CPU_USAGE ...它是您的物理硬件消耗的资源量,因此假设您将RESULT_CPU_USAGE设为50% ,这意味着容器 X 使用了所有 PC 电源的50%

So I need this also, and the following gives me the correct CPU usage, factoring in number of cores.所以我也需要这个,下面给出了正确的 CPU 使用率,包括内核数。

var cpuDelta = metric.cpu_stats.cpu_usage.total_usage -  metric.precpu_stats.cpu_usage.total_usage;
var systemDelta = metric.cpu_stats.system_cpu_usage - metric.precpu_stats.system_cpu_usage;
var RESULT_CPU_USAGE = cpuDelta / systemDelta * metric.cpu_stats.cpu_usage.percpu_usage.length * 100;

console.log(RESULT_CPU_USAGE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM