Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPU usage percentage #3

Open
bararchy opened this issue Dec 3, 2017 · 4 comments
Open

CPU usage percentage #3

bararchy opened this issue Dec 3, 2017 · 4 comments

Comments

@bararchy
Copy link

bararchy commented Dec 3, 2017

Would you say this should give me the best overall % of CPU usage?

(Psutil.cpu_times.first.user + Psutil.cpu_times.first.system) / 1000
@bararchy
Copy link
Author

bararchy commented Dec 3, 2017

So, following this guide: https://github.com/Leo-G/DevopsWiki/wiki/How-Linux-CPU-Usage-Time-and-Percentage-is-calculated#formula

The right way to calculate is this:

total_time_used = Psutil.cpu_times.first.user + Psutil.cpu_times.first.system + Psutil.cpu_times.first.nice + Psutil.cpu_times.first.nice + Psutil.cpu_times.first.softirq + Psutil.cpu_times.first.irq + Psutil.cpu_times.first.steal + Psutil.cpu_times.first.idle + Psutil.cpu_times.first.iowait
total_time_idle = Psutil.cpu_times.first.iowait + Psutil.cpu_times.first.idle
time_used = total_time_used - total_time_idle
time_used / total_time_used * 100 # this is the % of cpu used

@bararchy
Copy link
Author

bararchy commented Dec 3, 2017

@kodnaplakal Maybe it can be nice to add a small method for this?

@aablinov
Copy link
Owner

aablinov commented Dec 5, 2017

@bararchy For calculate CPU usage you need interval, take Psutil.cpu_times sleep N take Psutil.cpu_times again and see difference.
For example please see in gopsutil https://github.com/shirou/gopsutil/blob/master/cpu/cpu.go#L139

@aablinov
Copy link
Owner

aablinov commented Dec 5, 2017

@bararchy if you can do it yourself, please create a PR if not I will do it today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants