TomasTomecek / sen

Terminal User Interface for containers.
MIT License
976 stars 59 forks source link

CPU Utilisation Calculation #152

Closed rushi47 closed 4 years ago

rushi47 commented 4 years ago

Hey Tomas, 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. Specially interested in cpu utilisation part. 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:

Please let me know if am missing anything in the calculation, it will be great help.

Thanks a lot :)

I created same issue Here also. Code am trying to work on is also in python. It will be great if you can comment on this.

TomasTomecek commented 4 years ago

I'd say that /proc/stat and cgroups use different type of values so that's why they are not consistent. So I guess you shouldn't mix them.

This is the code which performs the calculation:

https://github.com/TomasTomecek/sen/blob/master/sen/util.py#L176

I took it directly from docker's go code without thinking too much about it.

That's all I can help.

rushi47 commented 4 years ago

oh ok Thanks for the answer @TomasTomecek

rushi47 commented 4 years ago

If someone get stucked with same problem & want something like this : https://pypi.org/project/cgrputil/ , this is the library i created, feel free to use it :)