Reading /proc and getrusage
Read your resource limits, your own /proc/self/status, and CPU/memory stats from getrusage. The /proc part is Linux-only; on macOS it prints a note and the rest still runs.
package main
import (
"fmt"
"os"
"runtime"
"strings"
"syscall"
)
func main() {
// How many open files this process is allowed.
var lim syscall.Rlimit
syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim)
fmt.Printf("open-file limit: soft=%d hard=%d\n", lim.Cur, lim.Max)
// /proc/self is a symlink to this process's own /proc entry (Linux).
if status, err := os.ReadFile("/proc/self/status"); err == nil {
for _, line := range strings.Split(string(status), "\n") {
if strings.HasPrefix(line, "VmRSS") || strings.HasPrefix(line, "Threads") {
fmt.Println(strings.Join(strings.Fields(line), " "))
}
}
} else {
fmt.Println("/proc not available (not Linux)")
}
// getrusage: CPU time split into user vs kernel, plus peak resident memory.
var ru syscall.Rusage
syscall.Getrusage(syscall.RUSAGE_SELF, &ru)
fmt.Println("user CPU (s):", ru.Utime.Sec)
fmt.Println("sys CPU (s):", ru.Stime.Sec)
fmt.Println("peak RSS:", ru.Maxrss) // KB on Linux, bytes on macOS
fmt.Println("NumCPU:", runtime.NumCPU(), "GOMAXPROCS:", runtime.GOMAXPROCS(0))
}Save the code as main.go and run it:
go run main.goExpected output on Linux:
open-file limit: soft=1024 hard=1048576
VmRSS: 6784 kB
Threads: 6
user CPU (s): 0
sys CPU (s): 0
peak RSS: 6784
NumCPU: 8 GOMAXPROCS: 8Memory, thread, and CPU numbers vary by machine. On macOS there's no /proc, so the VmRSS/Threads lines are replaced by /proc not available (not Linux), and peak RSS is reported in bytes instead of kilobytes.