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

Correctly calculate CPU percentages on linux with guests #940

Closed
wants to merge 1 commit into from

Conversation

Foxlik
Copy link

@Foxlik Foxlik commented Nov 23, 2016

Since guest is already included in user and guest_nice is included in nice, it got included twice in totals.

http://unix.stackexchange.com/questions/178045/proc-stat-is-guest-counted-into-user-time

Please doublecheck this, as I can test just on recent linux.

Since guest is already included in user and guest_nice is included in nice, it got included twice in totals.

http://unix.stackexchange.com/questions/178045/proc-stat-is-guest-counted-into-user-time

Please doublecheck this, as I can test just on recent linux.
@giampaolo
Copy link
Owner

Uhm... good catch. I'm not completely certain the explanation given in that SO thread is correct though. The first message talks about times in /proc/stat (the system-wide CPU times) whereas who posted the first answer talks about /proc/[pid]/stat, which are process times.

@giampaolo
Copy link
Owner

I'm trying to find an official reference which states that guest and guest_nice in /proc/stats are already included in user time.

@Foxlik
Copy link
Author

Foxlik commented Nov 26, 2016

@giampaolo
Copy link
Owner

Thanks, that's enough. =)
After further investigation it seems we are also calculating the busy time wrong (and hence the percentage is wrong).
Right now it's done as busy = total - idle but according to htop (see here and here) it should be busy = total - idle - iowait.

To push this even further, I'm not sure whether we should adjust the guest time calculation only in cpu_percent() or also in cpu_times() (I mean subtracting the guest and guest_nice from user and nice).

@giampaolo giampaolo closed this in 2536070 Nov 28, 2016
@giampaolo
Copy link
Owner

This is now correctly fixed. Thanks for discovering the original issue. I had no idea the CPU percent related math was not 100% correct.

@Foxlik
Copy link
Author

Foxlik commented Nov 29, 2016

Great. Thanks!

@Foxlik Foxlik deleted the patch-1 branch November 29, 2016 06:28
sparrc pushed a commit to influxdata/telegraf that referenced this pull request Dec 5, 2016
On linux, the cpu timer counters of user and nice include the respective guest and guest_nice counters. This results in improper calculation of percentages.

Please see:
https://github.com/torvalds/linux/blob/447976e/kernel/sched/cputime.c#L169
https://lists.linuxfoundation.org/pipermail/virtualization/2009-August/013459.html
giampaolo/psutil#940
bcaudesaygues pushed a commit to viareport/telegraf that referenced this pull request Feb 6, 2017
On linux, the cpu timer counters of user and nice include the respective guest and guest_nice counters. This results in improper calculation of percentages.

Please see:
https://github.com/torvalds/linux/blob/447976e/kernel/sched/cputime.c#L169
https://lists.linuxfoundation.org/pipermail/virtualization/2009-August/013459.html
giampaolo/psutil#940
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants