process_xxx_memory statistics for macOS (without cgo)#1629
Closed
mharbison72 wants to merge 2 commits intoprometheus:mainfrom
Closed
process_xxx_memory statistics for macOS (without cgo)#1629mharbison72 wants to merge 2 commits intoprometheus:mainfrom
mharbison72 wants to merge 2 commits intoprometheus:mainfrom
Conversation
prometheus#1600) Unfortunately, these values aren't available from getrusage(2), or any other builtin Go API. Using cgo is one alternative. It's possible to conditionalize everything such that cgo can remain disabled on non-Darwin platforms, or even when cross-compiling Darwin executables on a non-Darwin platform (and stub in code that causes the metrics to not be exported). `CGO_ENABLED=1` is set by default on macOS, but unfortunately is off for the non-host architecture, even when gcc supports cross-compiling. (e.g. building with GOARCH=amd on an M2 mac skipped the cgo code.) I think that's too subtle of a distinction to rely on cgo. There's no builtin equivalent of `syscall.NewLazyDLL()` and `.NewProc()` on macOS that Go provides for Windows, so we're stuck with a 3rd party dependency. But it seems stable, maintained, ang getting a fair amount of usage. I'm avoiding their struct deserialization because these native structs are packed differently than the equivalent Go structs, which was causing bad values to be returned. The code is heavy with inline comments, and I tried keeping the type names the same as the C code to make it easier to search for them. I'm not sure that we need to do the `mach_vm_region()` call to adjust the `task_info()` values, because I've never seen that conditional evaluate to True on either amd64, arm64, or when amd64 is run under Rosetta. But this is what `ps(1)` does, and I think it's reasonable to try to match that unless somebody knows it's dead code. Signed-off-by: Matt Harbison <mharbison72@gmail.com>
Here we have to use cgo, but since it's conditionalized to 1) tests, 2) only on Darwin, 3) when `CGO_ENABLED=1`, and 4) gracefully skips when it is disabled, I think it's OK. There were many layers to the C types that are defined, and subtle sizing and offset issues that the C compiler would handle that need to be correct here, so I think it's worth having around. I don't expect any of the sizes or offsets to change until a new architecture is supported for macOS, but these are definitely not the more documented APIs. Signed-off-by: Matt Harbison <mharbison72@gmail.com>
Contributor
Author
|
The cgo version was landed instead of this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is the alternative to PR 1616, but keeping the cgo usage isolated to tests so that users don't need to fiddle with enabling cgo, or deal with losing cross platform support.
If this is viable, we can probably finish off the rest of #1600 with this technique. (Currently, RSS, VSIZE, and the network traffic stats are not reported on macOS- this handles the first two items.)
@bwplotka