Commit Graph

1 Commits

Author SHA1 Message Date
Matt Harbison 25bda7ceb5
process_collector: fill in virtual and resident memory values on macOS using optional cgo (#1616)
Unfortunately, these values aren't available from getrusage(2), or any other
builtin Go API.  Go itself doesn't provide a mechanism (like on Windows) to call
into system libraries.  Using a 3rd party package[1] to dynamically call system
libraries was proposed and rejected, to avoid adding to the number of
dependencies.  That leaves using cgo, which is used here when available.  When
not available (either because of cross compiling or explicitly disabling it), a
stub function is linked instead, and the metrics are not exported.  That way,
cross compiling of other platforms is unaffected (and can also still be done
with Darwin too, but at the cost of not exporting these metrics).

Note that building an amd64 image on an arm64 mac or vice-versa is cross
compiling, and will use the stub method by default.  This can be avoided by
setting `CGO_ENABLED=1` in the environment to force the use of cgo for both
architectures.

I'm unsure of the usefulness of the potential adjustment made to the virtual
memory value after calling `mach_vm_region()`.  I've not seen that code get run
with a native amd64 or arm64 image, or with an amd64 image running under
Rosetta.  But that's what the `ps(1)` command does, and I think we should report
what the system tools do.

When I was testing this on a beta of macOS 15 with Go 1.21.13 (the current
minimum support for this module), the amd64 image ran fine under Rosetta, but
the arm64 image immediately printed a message that it was killed, even prior to
the cgo call.  This seems to be a recurring issue on macOS[2][3], and passing
`-ldflags -s` to `go build` avoided the issue.  Go 1.23.1 worked out of the box,
without fiddling with linker flags, so I don't think this is an issue- Go 1.21
is simply too old to support macOS 15, but I thought it was worth noting.  I
supposed we could gate the cgo code with an additional build flag, if anyone is
concerned about this.

[1] https://github.com/ebitengine/purego
[2] https://github.com/golang/go/issues/19841#issuecomment-293334802
[3] https://github.com/golang/go/issues/11887#issuecomment-125694604

Signed-off-by: Matt Harbison <mharbison72@gmail.com>
2024-09-27 17:29:44 +01:00