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

fix cpu_openbsd.go once and for all #1244

Merged
merged 6 commits into from Feb 25, 2022
Merged

fix cpu_openbsd.go once and for all #1244

merged 6 commits into from Feb 25, 2022

Conversation

omar-polo
Copy link
Contributor

Hello,

as reported in #1241 currently cpu_openbsd.go doesn't retrieve the correct values in all situations. It's completely my fault for not implementing it correctly the first time, I was only thinking about amd64.

This is PR has a number of improvements, I suggest reading the diffs per-commit rather than a whole.

First of all, it drops support for OpenBSD <6.3, which was EOL'd more than three years ago! This allows to semplify the code a bit and to "const-ify" some parameters.

The second commit is aestetic: it just fixes some typos.

The third commit changes the way we parse the data we get from common.CallSyscall. Should make this library work on big endians.

The fourth commit is the real fix: it changes the way we obtain statistics from the CPU. There were multiple things wrong there:

  • KERN_CPTIME returns an array of longs, while KERN_CPTIME2 an array of uint64_t. longs depends on the platform (on amd64 are 64bits, on i386 are 32bits).
  • Also, instead of trying to guess which CPUs are online, use KERN_CPUSTATS that gives us the statistics and an handy flags field from which we can observe is the cpu is online or not.

I've tested this on amd64 (4 real cores, works correctly with both hw.smt enabled and not) and i386 (2 real cores, no smt), both running OpenBSD-CURRENT.

6.3 was EOL'd more than three years ago!
We can't use unix.Sysctl* for some sysctls, so we're on our own with
converting data from C arrays.

Don't assume that the byte order is little endian but do the right
thing.  Moreover, there's a little distinction in the sizes reported
by KERN_CPTIME (long[cpustates]) and KERN_CPTIME2
(u_int64_t[cpustates]) so account for that too.
don't make assumptions on which CPUs are online and wich aren't based
on hw.smt and hw.ncpuonline.  Rather, use KERN_CPUSTATS to get the CPU
statistics, which includes a flag field that can tell us if that CPU
is online or not.
cpu/cpu_openbsd.go Outdated Show resolved Hide resolved
Even thought OpenBSD often breaks the ABI compatibility and doesn't make
*any* promise of "stability", this project aims to be "pure go" so avoid
doing inter-op at the cost of artificially reducing the number of
supported architectures down to amd64 and i386.

To add support for another architecture (e.g. arm), add another file
cpu_openbsd_${arch}.go like done for 386 and amd64.  The fields are
declared as `long' in C, so pick the appropriate size when declaring the
struct.
@omar-polo
Copy link
Contributor Author

OK, I've got rid of the cgo bits and polished it a bit. I've tested on amd64 -CURRENT (with both smt enabled and disabled) and on i386 -CURRENT.

@omar-polo
Copy link
Contributor Author

P.S.: I think I could have used uintptr instead of the two _386.go and _amd64.go files, but I'm not sure if sizeof(long) == size of uintptr for all architectures, and I don't have an arm / aarch64 board at hand to verify, so I went for the additional files.

Copy link
Owner

@shirou shirou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your great contribution! I am very appreciate your continuous work.

@shirou shirou merged commit d33b2df into shirou:master Feb 25, 2022
@omar-polo
Copy link
Contributor Author

happy to have been useful and apologize again for my initial mistake :)

Cheers!

@chrissnell-okta
Copy link

Can you cut a new tag on this repo so that I can try it with Telegraf?

@chrissnell-okta
Copy link

Can you cut a new tag on this repo so that I can try it with Telegraf?

Actually, I figured out how to build from this most recent commit and good news, telegraf works now! Thanks @omar-polo

@shirou still, if you can cut a release or a tag, I will pester the telegraf folks to update the dep. Thanks!

@shirou
Copy link
Owner

shirou commented Feb 26, 2022

gopsutil has a monthly release policy. plz wait a few days.

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.

None yet

3 participants