根据process.cpu.time计算CPU使用率

问题描述 投票:0回答:2

https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/receiver/hostmetricsreceiver/internal/scraper/processscraper/documentation.md

我一直在使用这个库,它为我一个进程提供了 3 个值:

用户时间、系统时间和等待时间

一个示例值为:0.05、0.01、0.00

如何计算特定进程的 CPU 百分比?

cpu cpu-usage open-telemetry cpu-time
2个回答
2
投票

要计算系统的总 CPU 负载/利用率百分比,我们需要计算

"total system cpu time (during the period)" + "total user cpu time (during the period)" / "period"

在您的情况下,假设您每 2 秒采样一次,那么对于每个样本,您需要计算:

= ( (process.cpu.time.sys - previous_process.cpu.time.sys) + (process.cpu.time.user - previous_process.cpu.time.user) ) / 2

0
投票

如果您使用的是 hostmetrics 接收器CPU 文档 指出有许多默认情况下未启用的可选指标,包括

system.cpu.utilization
。要使用此指标,只需在配置文件中显式启用它即可:

metrics:
  system.cpu.utilization:
    enabled: true

⚠️ 如果您的收集器没有在您希望监控的主机上运行(即作为代理),则这将不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.