Files
nats-server/server/pse/freebsd.txt
Phil Pennock bc6c433142 FreeBSD fixes: %cpu scale; no-cgo for amd64
It would be convenient if we did not need to use cgo to compile for
FreeBSD for common architectures, allowing builds to be generated in
Linux CI flows.

The sysctl interface uses offsets which can vary by build architecture,
so doing this in the general case without cgo is not realistic.  But we
can front-load the C work to get the offsets for a given architecture,
then use encoding/binary at run-time.

While doing this, I saw that the existing FreeBSD code was
mis-calculating `%cpu` by converting the `fixpt_t` scaled int straight
to a double without dividing by the scaling factor, so we also fix for
all other architectures by introducing a division by `FSCALE`.

The offsets-emitting code is in `freebsd.txt`, with the filename chosen
to keep the Go toolchain from picking it up and trying to compile.

The result is unsafe-free cgo-free builds for FreeBSD/amd64.
2020-11-24 23:04:50 -05:00

31 lines
984 B
Plaintext

/*
* Compile and run this as a C program to get the kinfo_proc offsets
* for your architecture.
* While FreeBSD works hard at binary-compatibility within an ABI, various
* we can't say for sure that these are right for _all_ use on a hardware
* platform. The LP64 ifdef affects the offsets considerably.
*
* We use these offsets in hardware-specific files for FreeBSD, to avoid a cgo
* compilation-time dependency, allowing us to cross-compile for FreeBSD from
* other hardware platforms, letting us distribute binaries for FreeBSD.
*/
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/user.h>
#define SHOW_OFFSET(FIELD) printf(" KIP_OFF_%s = %zu\n", #FIELD, offsetof(struct kinfo_proc, ki_ ## FIELD))
int main(int argc, char *argv[]) {
/* Uncomment these if you want some extra debugging aids:
SHOW_OFFSET(pid);
SHOW_OFFSET(ppid);
SHOW_OFFSET(uid);
*/
SHOW_OFFSET(size);
SHOW_OFFSET(rssize);
SHOW_OFFSET(pctcpu);
}