3

Is there any way that I can get the cpu percentage inside docker container and not outside of it?! docker stats DOCKER_ID shows the percentage which is exactly what I need but I need it as variable. I need to get cpu percentage inside the container itself and do some operation with it. I have looked into different stuff such as cgroup and docker rest API, but they do not provide cpu percentage. If there is a way to get the cpu percentage inside the container and not outside of it will be perfect. I found one solution provided by someone in below link, which is still outside the container by the rest api, however I did not really get it how to calculate the percentage.

Get Docker Container CPU Usage as Percentage

Community
  • 1
  • 1
secret
  • 706
  • 1
  • 10
  • 25

2 Answers2

0

You can install Google cAdvisor with Axibase Time-Series Database storage driver. It will collect and store CPU utilization measured both in core units as well as in percentages.

Screenshots with examples of how CPU is reported are located at the bottom of the page: https://axibase.com/products/axibase-time-series-database/writing-data/docker-cadvisor/

In a centralized configuration, the ATSD container itself can ingest metrics from multiple cAdvisor instances installed on multiple docker hosts.

EDIT 1: One liner to compute total CPU usage of all processes running inside the container. Adjust -d parameter to change the interval between samples to smooth spikes out:

top -b -d 5 -n 2 | awk '$1 == "PID" {block_num++; next} block_num == 2 {sum += $9;} END {print sum}'
Sergei Rodionov
  • 3,078
  • 5
  • 21
  • 40
  • Thanks for your reply, however this didn't work as I expected. It just complicates things to get single cpu percentage, and it observes container from outside. It saves the usage somewhere in the axibase container that I have to go through the container and extract the cpu. Isn't just a simple way to get the cpu percentage in the container itself, like what docker stats does? – secret Jul 22 '15 at 12:10
  • To be fair, docker stats console collects CPU usage for the selected container at the host level, correct? It's just not storing it in any database, but still these statistics are not available from within container. Are you open to the idea of measuring container CPU usage as the total CPU usage of all processes running inside the container? – Sergei Rodionov Jul 23 '15 at 06:52
  • Yes I want total cpu usage of the container not process by process. – secret Jul 23 '15 at 08:21
  • Here's one liner that prints out total CPU usage of all processes running inside the container: top -b -n 1 | tail -n +8 | awk '{s+=$9} END {print s}' I only tested it on ubuntu 14, so be prepared to make changes to fit your distribution. – Sergei Rodionov Jul 23 '15 at 09:58
  • Thanks this might help. – secret Jul 23 '15 at 10:50
0

I have used ctop which gives a more graphical way than docker_stats But I found that it was showing CPU percentage way higher than what Top was showing for the system. Basically it is showing relative to the root process. Docker containers run as child process

To illustrate with an example

First find the root process under which all the containers run

docker-containerd-shim - ..the Docker architecture is broken into four components: Docker engine, containerd, containerd-shm and runC. The binaries are respectively called docker, docker-containerd, docker-containerd-shim, and docker-runc. - https://hackernoon.com/docker-containerd-standalone-runtimes-heres-what-you-should-know-b834ef155426

root 1843 1918 0 Aug31 ? 00:00:00 docker-containerd-shim 611bd9... /var/run/docker/libcontainerd/611bd92.... docker-runc

You can see all the containers that are running using the command

pstree -p 1918

Now say that we are interested in seeing the CPU consumption of fluentdb.

Easy way to get the child pid of this is

pstree -p 1918 |grep fluentd

Which gives 21670

Now you can run top -p 21670 to see the CPU share of this child process also top -p 1918 to see the overall CPU of the parent process.

With cadvisor collecting to Promethus and view in Grafana, this was the closest and most accurate representation of the actual CPU percentage used by the container; in relation to the host machine. This diagram illustrates this. cTop and docker stats give 23% as the CPU percentage. Actual CPU percentage of the docker parent process is around 2% and cAdvisor output from Grafana shows the most 'accurate' value of the container CPU percentage related to host.

graphana-cadvisor-top-ptree-docker

Alex Punnen
  • 3,420
  • 36
  • 52