Calculating CPU Utilisation of specific function in Container without using docker api?
I looked at various link on stack over flow and others also & i found one pattern or generic way to calculate the cpu utilisation. One of the link is below
https://github.com/moncho/dry/blob/07244d36010d1029ee03bda29fa5aec6449ebf61/docker/stats.go#L168
Am trying to calculate the same, on my own. But it seems, whenever i calculate the value its not even close, can anyone verify approach once ? & If not can anyone help me with approach ?
I want to calculate cpu for specific time interval, just to give example. def something_get_execute() i want to calculate cpu usage for just this specific function.
For the same am doing following this, Getting values for user & system from here :
/sys/fs/cgroup/cpu,cpuacct/cpuacct.stat
Before function gets execute & after the function gets execute. After that i get delta for container by using below formula :
container_cpu_delta = (cpuacct.stat_final_user - cpuacct.stat_initial_user) / (cpuacct.stat_final_system - cpuacct_stat_initial_system)
Meanwhile i do the same, to get the value for host Before and After the function gets execute i get the value for User and system from :
/proc/stat
grep the first line and i get 1st & 3rd Param
cpu 205434 341 56826 3122952 48119 0 25640 0 0 0 i.e 205434 & 56826
And perform the same calculation
host_cpu_delta = (proc_stat_final_user - proc_stat_init_user)/(proc_stat_final_system-proc_stat_init_user)
& Then finally i do
cpu_percentage = (container_cpu_delta/host_cpu_delta) * no_of_cores (am bit confused about this cores allocated to container or cores on the host)
However there are some observations:
So many times i have seen the difference of
(cpuacct.stat_final_user - cpuacct.stat_initial_user)
i.e container is Greater than
(proc_stat_final_user - proc_stat_init_user)
of Host which i will feel little weird. As Host values contains the cycle of container as well Am confused about no of cores which to take while calculation, cpus allocated to container or all the cpus on host ? All things am doing from inside container, like reading all the values. Please let me know if am missing anything in the calculation, it will be great help.
Thanks a lot 🙂