Debian Codegraph @

server/pse/pse_freebsd_sysctl.go

raw
1 // Copyright 2015-2025 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 // There are two FreeBSD implementations; one which uses cgo and should build
15 // locally on any FreeBSD, and this one which uses sysctl but needs us to know
16 // the offset constants for the fields we care about.
17 //
18 // The advantage of this one is that without cgo, it is much easier to
19 // cross-compile to a target. The official releases are all built with
20 // cross-compilation.
21 //
22 // We've switched the other implementation to include '_cgo' in the filename,
23 // to show that it's not the default. This isn't an os or arch build tag,
24 // so we have to use explicit build-tags within.
25 // If lacking CGO support and targeting an unsupported arch, then before the
26 // change you would have a compile failure for not being able to cross-compile.
27 // After the change, you have a compile failure for not having the symbols
28 // because no source file satisfies them.
29 // Thus we are no worse off, and it's now much easier to extend support for
30 // non-CGO to new architectures, just by editing this file.
31 //
32 // To generate for other architectures:
33 // 1. Copy `freebsd.txt` to have a .c filename on a box running the target
34 // architecture, compile and run it.
35 // 2. Update the init() function below to include a case for this architecture
36 // 3. Update the build-tags in this file.
37
38 //go:build !cgo && freebsd && (amd64 || arm64)
39
40 package pse
41
42 import (
43 "encoding/binary"
44 "runtime"
45 "syscall"
46
47 "golang.org/x/sys/unix"
48 )
49
50 // On FreeBSD, to get proc information we read it out of the kernel using a
51 // binary sysctl. The endianness of integers is thus explicitly "host", rather
52 // than little or big endian.
53 var nativeEndian = binary.LittleEndian
54
55 var pageshift int // derived from getpagesize(3) in init() below
56
57 var (
58 // These are populated in the init function, based on the current architecture.
59 // (It's less file-count explosion than having one small file for each
60 // FreeBSD architecture).
61 KIP_OFF_size int
62 KIP_OFF_rssize int
63 KIP_OFF_pctcpu int
64 )
65
66 func init() {
67 switch runtime.GOARCH {
68 // These are the values which come from compiling and running
69 // freebsd.txt as a C program.
70 // Most recently validated: 2025-04 with FreeBSD 14.2R in AWS.
71 case "amd64":
72 KIP_OFF_size = 256
73 KIP_OFF_rssize = 264
74 KIP_OFF_pctcpu = 308
75 case "arm64":
76 KIP_OFF_size = 256
77 KIP_OFF_rssize = 264
78 KIP_OFF_pctcpu = 308
79 default:
80 panic("code bug: server/pse FreeBSD support missing case for '" + runtime.GOARCH + "' but build-tags allowed us to build anyway?")
81 }
82
83 // To get the physical page size, the C library checks two places:
84 // process ELF auxiliary info, AT_PAGESZ
85 // as a fallback, the hw.pagesize sysctl
86 // In looking closely, I found that the Go runtime support is handling
87 // this for us, and exposing that as syscall.Getpagesize, having checked
88 // both in the same ways, at process start, so a call to that should return
89 // a memory value without even a syscall bounce.
90 pagesize := syscall.Getpagesize()
91 pageshift = 0
92 for pagesize > 1 {
93 pageshift += 1
94 pagesize >>= 1
95 }
96 }
97
98 func ProcUsage(pcpu *float64, rss, vss *int64) error {
99 rawdata, err := unix.SysctlRaw("kern.proc.pid", unix.Getpid())
100 if err != nil {
101 return err
102 }
103
104 r_vss_bytes := nativeEndian.Uint32(rawdata[KIP_OFF_size:])
105 r_rss_pages := nativeEndian.Uint32(rawdata[KIP_OFF_rssize:])
106 rss_bytes := r_rss_pages << pageshift
107
108 // In C: fixpt_t ki_pctcpu
109 // Doc: %cpu for process during ki_swtime
110 // fixpt_t is __uint32_t
111 // usr.bin/top uses pctdouble to convert to a double (float64)
112 // define pctdouble(p) ((double)(p) / FIXED_PCTCPU)
113 // FIXED_PCTCPU is _usually_ FSCALE (some architectures are special)
114 // <sys/param.h> has:
115 // #define FSHIFT 11 /* bits to right of fixed binary point */
116 // #define FSCALE (1<<FSHIFT)
117 r_pcpu := nativeEndian.Uint32(rawdata[KIP_OFF_pctcpu:])
118 f_pcpu := float64(r_pcpu) / float64(2048)
119
120 *rss = int64(rss_bytes)
121 *vss = int64(r_vss_bytes)
122 *pcpu = f_pcpu
123
124 return nil
125 }