Docker Community Forums

Share and learn in the Docker community.

How to calculate the CPU usage in percent

Hi everyone, I’m trying to make a graph with the CPU usage of each of my containers.
I have founded a StackOverflow question that is linked to my problem here but I don’t understand all of the answer.

So I think someone here could help me to do it.

According to the answer I made :
y = (metric.usage_in_kernelmode + metric.usage_in_usermode)/( metric.idle+ metric.usage_in_kernelmode + metric.usage_in_usermode)*100 but I don’t know what idle is and where to find it.

Then in the comments the guy say :

“total_usage” seems to be the computing time my application/container consumed. You get another property “precpu_stats”, which looks exactly the same like “cpu_stats”, but the metrics are from the previous tick. I can then calculate delta-total_usage and delta-system_usage and the final solution is percent = (delta-total_usage / delta-system_usage) * percpu_usage.length * 100.0

But I don’t know how to do the “delta”

And in my collection I have:

Finally I did it like this:

delta_total_usage = (metric.cpu_stats.cpu_usage.total_usage - metric.precpu_stats.cpu_usage.total_usage)/metric.precpu_stats.cpu_usage.total_usage,

delta_system_usage = (metric.cpu_stats.system_cpu_usage - metric.precpu_stats.system_cpu_usage)/metric.precpu_stats.system_cpu_usage,

y = (delta_total_usage / delta_system_usage) * metric.cpu_stats.cpu_usage.percpu_usage.length * 100.0;

Hello Jerome,

dont know the exact answer for youre question but maybe a tool can help you.

This tools are perfect for youre problem and really easy to use.

I love to use Grafana because of the style.

But when you only have terminal i prefer Glances because it will run on Web with the command “glances -w”

I hope i can help you with this answer.