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][mac] change not return error even if cpu.frequency errors #1192

Merged
merged 1 commit into from
Dec 18, 2021

Conversation

shirou
Copy link
Owner

@shirou shirou commented Dec 4, 2021

This PR partially fixes #1000

Still we can not get concrete cpu frequency on m1 mac. This PR changes does not return error even we can not get a value from hw.cpufrequency.

@shirou shirou changed the title [cpu][mac] doesn't return error when cpu.frequency [cpu][mac] change not return error even if cpu.frequency errors Dec 4, 2021
@shirou shirou merged commit c6bccaf into master Dec 18, 2021
@shirou shirou deleted the feature/m1_not_return_when_cpu_freq branch December 18, 2021 02:35
KSerrania added a commit to DataDog/gopsutil that referenced this pull request Aug 13, 2022
Backport of shirou#1192.

sysctl hw.cpufrequency does not return anything on macOS M1. This causes cpu.Info()
to return an error every time it is called on macOS M1.

A partial fix of this issue is to ignore errors, and leave the frequency field with
its default value.
KSerrania added a commit to DataDog/gopsutil that referenced this pull request Sep 1, 2022
[macOS M1] Backports from upstream to fix cpu.Info() crashes

Since #40, we can build projects containing this gopsutil fork on macOS M1.

However, using cpu.Info() causes a crash: sysctl hw.cpufrequency doesn't return anything on M1, which makes the following snippet crash:

out, err = invoke.Command(sysctl, "hw.cpufrequency")
	if err != nil {
		return ret, err
	}

	values := strings.Fields(string(out))
	hz, err := strconv.ParseFloat(values[1], 64)
with an index out-of-range error.

This PR:
- backports Lomanic/gopsutil@2ec3560, which uses golang.org/x/sys/unix.Sysctl instead of manual calls to the sysctl binary, preventing the crash
- backports shirou#1192 to not make cpu.Info() return an error when the CPU frequency cannot be found, to prevent the method from erroring on M1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cpu.Info() errors on Apple Silicon M1 (darwin/arm64)
1 participant