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 0% #1759

Open
1 of 5 tasks
Zoobdude opened this issue Dec 20, 2024 · 3 comments
Open
1 of 5 tasks

CPU usage 0% #1759

Zoobdude opened this issue Dec 20, 2024 · 3 comments

Comments

@Zoobdude
Copy link

Zoobdude commented Dec 20, 2024

Describe the bug
The output of the below code snippet is

[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
<nil>

To Reproduce

cpuPercentProcess, err := cpu.Percent(0, true)
fmt.Println(cpuPercentProcess)
fmt.Println(err)

Expected behavior
Actual cpu usage is displayed

Environment

  • Windows: Version 10.0.22631 Build 22631
  • Linux: [paste contents of /etc/os-release and the result of uname -a]
  • Mac OS: [paste the result of sw_vers and uname -a
  • FreeBSD: [paste the result of freebsd-version -k -r -u and uname -a]
  • OpenBSD: [paste the result of uname -a]
@Zoobdude Zoobdude changed the title CPU usage 0 CPU usage 0% Dec 20, 2024
@shirou
Copy link
Owner

shirou commented Dec 21, 2024

Could you show us the ignored error?

@Zoobdude
Copy link
Author

The error was <nil>

@xmusphlkg
Copy link

xmusphlkg commented Jan 1, 2025

I encounter a similar problem.

This code created different outcomes in Windows Server 2021.

package main

import (
	"fmt"
	"time"

	"github.com/shirou/gopsutil/v4/cpu"
)

func main() {
	cpuPercentProcess, err := cpu.Percent(0, true)
	fmt.Println(cpuPercentProcess)
	fmt.Println(err)

		totalPercent, err := cpu.Percent(time.Second, false)
		if err != nil {
			fmt.Println("Error fetching total CPU usage:", err)
			return
		}
		fmt.Printf("Total CPU Usage: %.2f%%\n", totalPercent[0])
	
		perCPU, err := cpu.Percent(time.Second, true)
		if err != nil {
			fmt.Println("Error fetching per-CPU usage:", err)
			return
		}
	
		for idx, cp := range perCPU {
			fmt.Printf("CPU %d Usage: %.2f%%\n", idx, cp)
		}
}

The outcome:

PS C:\Users\abc\Downloads> ./testgopsutil.exe
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
<nil>
Total CPU Usage: 0.11%
Error fetching per-CPU usage: received two CPU counts: 24 != 64
PS C:\Users\abc\Downloads> ./testgopsutil.exe
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
<nil>
Total CPU Usage: 0.00%
CPU 0 Usage: 100.00%
CPU 1 Usage: 0.00%
CPU 2 Usage: 100.00%
CPU 3 Usage: 0.00%
CPU 4 Usage: 100.00%
CPU 5 Usage: 0.00%
CPU 6 Usage: 0.00%
CPU 7 Usage: 0.00%
CPU 8 Usage: 100.00%
CPU 9 Usage: 0.00%
CPU 10 Usage: 100.00%
........................
CPU 60 Usage: 0.00%
CPU 61 Usage: 0.00%
CPU 62 Usage: 0.00%
CPU 63 Usage: 0.00%

Total CPU Usage also inconsistent with task manager:

image

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

3 participants