如何将时钟滴答中的进程 cpu 使用率转换为百分比?

How to convert process cpu usage in clock ticks to percentage?

我在我的 Debian 6 虚拟机上安装了 collectd 以进行监控和性能分析。 Collectd 的 processes 插件提供有关进程的 cpu 使用情况的统计信息,但这些统计信息的单位并未在任何地方记录。这肯定不是 jiffies 或毫秒,因为在 4 核虚拟机上,几个进程的总 cpu 使用率可能高达每秒 400,000(某个未知单位)。

通过查看 collectd 的源代码 (https://github.com/collectd/collectd/blob/master/src/processes.c - in the ps_read_process function) , I figured out this data is read from the /proc/$pid/stat file of the process. The proc man page (link- http://man7.org/linux/man-pages/man5/proc.5.html) 说 cpu 用法以时钟滴答测量。

这很好,但是时钟滴答对于监控和性能分析来说有点武断。我想将时钟滴答值转换为更有意义的值,最好是总 cpu 时间的百分比。我怎样才能以便携的方式做到这一点,而不仅仅是假设我的处理器提供 3GHZ 的时钟滴答?

进一步检查 collectd 的代码后发现 cpu 用法已转换为微秒。

另外,类似的问题已经有人提出并得到回答 here