moncho/dry

CPU Utilisation Calculation

rushi47 opened this issue · 1 comments

Hey Moncho,
Hope you are doing great :)
I looked at your project and its awesome.
I have few doubts, it will be great favour if you can help me with it.

func calculateCPUPercentUnix(stats *types.StatsJSON) float64 {

Specially interested in this line.
Am trying to calculate the same, on my own. But it seems, whenever i calculate the value its not even close, can you verify approach once ?

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 :)

Got created twice i think, closing it keep only one.
Duplicate
#135