gopsutil icon indicating copy to clipboard operation
gopsutil copied to clipboard

start to using CGO on cpu/cpu_darwin

Open shirou opened this issue 10 years ago • 11 comments

I know there are many people who want to get CPUTimes on darwin using CGO. I had rejected because of easy cross-compiling. I had wrote using helper process. But yes, this is bad approach. And I think there is no way to get host_processer_info from pure Go because this is belongs to Mach part of darwin.

So, now, I have changed my mind.

Since 47f6760cf36f1ee42b4adf32a201835eb5c2ae57, cpu_darwin uses CGO. Current code is borrowed from influxdb/telegraf. Thank you using gopsutil!)

If someone want to remove CGO dependency, please write comments for this issue. I will remove quickly.

I always grateful to such a many people using, discussing, and contributing gopsutil.

reference

  • #31
  • #57

shirou avatar Aug 28 '15 08:08 shirou

:+1:

vrecan avatar Aug 31 '15 22:08 vrecan

I would prefer this removed because as you said it makes cross compiling difficult. Using tools like gox is much harder with this now.

dadgar avatar Oct 05 '15 19:10 dadgar

@dadgar thank you for the :-1:

I have an idea about to use a build tag to easy cross compiling. It means, make two kind of files about darwin.

  • cpu_darwin_cross.go
    • // +build darwin cross
    • no CGO, just return NotImplementedError on CPUTimes()
  • cpu_darwin.go
    • // +build darwin !cross
    • use CGO

If you want to cross compile (and can accept could not get CPUTimes on darwin), type go build -tags=cross. Then, cpu_darwin_cross.go is used for cross-compile.

How do you feel it? Thank you.

shirou avatar Oct 08 '15 14:10 shirou

Yeah I like your thinking. You could probably use the cgo tag right? `

dadgar avatar Oct 09 '15 17:10 dadgar

Ah! cgo build tag is the right way. Totaly agree. I made a PR #89. could you check it out?

shirou avatar Oct 10 '15 13:10 shirou

Let user to choose use cgo or not when compile the code is a good idea

Comdex avatar May 31 '16 07:05 Comdex

@Comdex You can change CGO_ENABLE environment variable.

shirou avatar May 31 '16 08:05 shirou

Quick tip for anyone wondering, it's CGO_ENABLED. See: https://golang.org/cmd/cgo/.

jesse-c avatar Jun 16 '17 09:06 jesse-c

Hi, today we wanted to upgrade to v4 but we don't allow CGO for the same reasons as described above (easy cross compilation and pure go codebase). Is it possible to opt-out? On v4 our binary won't compile.

StarpTech avatar Jan 12 '25 11:01 StarpTech

@StarpTech from #1700, which means release v4.24.9, we don't use CGO anymore on Darwin. If you can not compile with latest release, please open an issue.

shirou avatar Jan 12 '25 11:01 shirou

@shirou created https://github.com/shirou/gopsutil/issues/1774

StarpTech avatar Jan 12 '25 12:01 StarpTech