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

Windows setting CPU Percents on cpu.TimeStat object #420

Closed
dadgar opened this issue Sep 6, 2017 · 5 comments
Closed

Windows setting CPU Percents on cpu.TimeStat object #420

dadgar opened this issue Sep 6, 2017 · 5 comments

Comments

@dadgar
Copy link

dadgar commented Sep 6, 2017

This should probably be a different struct as the other methods will accept it and behave incorrectly. Also it causes upstream bugs on code that relies on it not being a percent.

https://github.com/shirou/gopsutil/blob/master/cpu/cpu_windows.go#L140

dadgar added a commit to hashicorp/nomad that referenced this issue Sep 6, 2017
This PR fixes an issue introduced in Nomad 0.6.0 due to
shirou/gopsutil#420. The issue arised from the
fact that the Windows stats from gopsutil reports CPUs in
percentages where we expected ticks.
@shirou
Copy link
Owner

shirou commented Sep 8, 2017

Sorry, currently I don't have time to investigate this. What struct should we use? Could you send a PR?

@dadgar
Copy link
Author

dadgar commented Sep 8, 2017

The CPU package doesn't really have an appropriate struct. I would at the very minimum add documentation stating the different behavior on the Windows platform.

@dadgar
Copy link
Author

dadgar commented Sep 8, 2017

What makes it even more confusing is that cpu.Times(false) is in ticks but cpu.Times(true) is in percents.

Ticks: https://github.com/shirou/gopsutil/blob/master/cpu/cpu_windows.go#L52-L76
Percents: https://github.com/shirou/gopsutil/blob/master/cpu/cpu_windows.go#L131

dadgar added a commit to hashicorp/nomad that referenced this issue Sep 10, 2017
This PR fixes an issue introduced in Nomad 0.6.0 due to
shirou/gopsutil#420. The issue arised from the
fact that the Windows stats from gopsutil reports CPUs in
percentages where we expected ticks.
dadgar added a commit to hashicorp/nomad that referenced this issue Sep 10, 2017
This PR fixes an issue introduced in Nomad 0.6.0 due to
shirou/gopsutil#420. The issue arised from the
fact that the Windows stats from gopsutil reports CPUs in
percentages where we expected ticks.
@marcospedreiro
Copy link
Contributor

marcospedreiro commented Nov 19, 2018

I was taking a look at this today and was doing some experimentation here

I started using the WMI Code generator tool to see what we could get, and saw that the current class we're querying (win32_perfformatteddata_counters_processorinformation) does return percentage values.

I used the WMI code generator tool to see if there were any other classes that might get us the CPU time values and I saw win32_perfrawdata_counters_processorinformation. From the linked documentation, they are stated to return identical values, except the latter has a few extra fields.

However querying my windows computer for win32_perfrawdata_counters_processorinformation returned different values that were not percentages. I did a quick swap out to use this new class and got the following results utilizing the following code:

package main

import (
	"fmt"

	"github.com/marcospedreiro/gopsutil/cpu"
)

func main() {
	fmt.Println("[windows] cpu.Times(false)")
	t, err := cpu.Times(false)
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, ct := range t {
		fmt.Println(ct)
	}

	fmt.Println("[windows] cpu.Times(true)")
	t, err = cpu.Times(true)
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, ctf := range t {
		fmt.Println(ctf)
	}

	return
}

Currently when I run this on master I get the following on my windows computer:

[windows] cpu.Times(false)
{"cpu":"cpu-total","user":68496.5,"system":12999.8,"idle":126170.3,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}
[windows] cpu.Times(true)
{"cpu":"0,0","user":0.0,"system":0.0,"idle":100.0,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}
{"cpu":"0,1","user":0.0,"system":12.0,"idle":92.0,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}
{"cpu":"0,2","user":0.0,"system":0.0,"idle":100.0,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}
{"cpu":"0,3","user":0.0,"system":6.0,"idle":99.0,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}
{"cpu":"0,4","user":6.0,"system":0.0,"idle":99.0,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}
{"cpu":"0,5","user":0.0,"system":6.0,"idle":99.0,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}

With my commit that I linked above I get the following:

[windows] cpu.Times(false)
{"cpu":"cpu-total","user":69030.2,"system":13576.2,"idle":155644.0,"nice":0.0,"iowait":0.0,"irq":0.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}
[windows] cpu.Times(true)
{"cpu":"0,5","user":111033750000.0,"system":19630000000.0,"idle":266419843750.0,"nice":0.0,"iowait":0.0,"irq":196250000.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}
{"cpu":"0,4","user":112372968750.0,"system":21042968750.0,"idle":263667656250.0,"nice":0.0,"iowait":0.0,"irq":167031250.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}
{"cpu":"0,3","user":111817812500.0,"system":21075781250.0,"idle":264189843750.0,"nice":0.0,"iowait":0.0,"irq":182343750.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}
{"cpu":"0,2","user":121851093750.0,"system":16515312500.0,"idle":258717187500.0,"nice":0.0,"iowait":0.0,"irq":147968750.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}
{"cpu":"0,1","user":125105781250.0,"system":22633437500.0,"idle":249344375000.0,"nice":0.0,"iowait":0.0,"irq":199062500.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}
{"cpu":"0,0","user":108120156250.0,"system":34864062500.0,"idle":254101875000.0,"nice":0.0,"iowait":0.0,"irq":4299218750.0,"softirq":0.0,"steal":0.0,"guest":0.0,"guestNice":0.0,"stolen":0.0}

It appears that the win32_perfrawdata_counters_processorinformation may be giving us cpu time data as opposed to percentages, but what I've found in terms of documentation so far has not been helpful, not to mention that we are off by a couple orders of magnitude from the cpu-total values.

If we sum the user time values for each core:

111033750000.0 + 112372968750.0 + 111817812500.0 + 121851093750.0 + 125105781250.0 + 108120156250.0

we get

690301562500 which is the same value as the cpu total 69030.2 except larger by a factor of 10,000,000, which is the number of clock ticks per second on windows

I'm happy to clean up my commit and submit a PR if we feel this is the correct approach to go with.

@Lomanic
Copy link
Collaborator

Lomanic commented Dec 9, 2018

Fixed by #611.

@Lomanic Lomanic closed this as completed Dec 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants