diff options
author | Doug Rabson <dfr@rabson.org> | 2022-09-02 14:05:37 +0100 |
---|---|---|
committer | Doug Rabson <dfr@rabson.org> | 2022-09-14 08:29:26 +0100 |
commit | 1e6da111557c20d285087933e870833064f92984 (patch) | |
tree | 554171856005032d8d225d6665d837aab5262573 /pkg/rctl/rctl.go | |
parent | c64388728f5efec2658cf9097dbe15126f30753e (diff) | |
download | podman-1e6da111557c20d285087933e870833064f92984.tar.gz podman-1e6da111557c20d285087933e870833064f92984.tar.bz2 podman-1e6da111557c20d285087933e870833064f92984.zip |
pkg/rctl: Add a simple FreeBSD RCTL library
Initially just supporting just rctl_get_racct for
(*Container).GetContainerStats.
[NO NEW TESTS NEEDED] we are not running any FreeBSD tests in CI
Signed-off-by: Doug Rabson <dfr@rabson.org>
Diffstat (limited to 'pkg/rctl/rctl.go')
-rw-r--r-- | pkg/rctl/rctl.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/pkg/rctl/rctl.go b/pkg/rctl/rctl.go new file mode 100644 index 000000000..135cc60cb --- /dev/null +++ b/pkg/rctl/rctl.go @@ -0,0 +1,47 @@ +//go:build freebsd +// +build freebsd + +package rctl + +// #include <sys/rctl.h> +import "C" + +import ( + "bytes" + "fmt" + "strconv" + "strings" + "syscall" + "unsafe" + + "github.com/sirupsen/logrus" +) + +func GetRacct(filter string) (map[string]uint64, error) { + bp, err := syscall.ByteSliceFromString(filter) + if err != nil { + return nil, err + } + var buf [1024]byte + _, _, errno := syscall.Syscall6(syscall.SYS_RCTL_GET_RACCT, + uintptr(unsafe.Pointer(&bp[0])), + uintptr(len(bp)), + uintptr(unsafe.Pointer(&buf[0])), + uintptr(len(buf)), 0, 0) + if errno != 0 { + return nil, fmt.Errorf("error calling rctl_get_racct with filter %s: %v", errno) + } + len := bytes.IndexByte(buf[:], byte(0)) + entries := strings.Split(string(buf[:len]), ",") + res := make(map[string]uint64) + for _, entry := range entries { + kv := strings.SplitN(entry, "=", 2) + key := kv[0] + val, err := strconv.ParseUint(kv[1], 10, 0) + if err != nil { + logrus.Warnf("unexpected rctl entry, ignoring: %s", entry) + } + res[key] = val + } + return res, nil +} |