Linux procfs Introspection
Linux procfs Introspection
Overview
On Linux, /proc exposes live kernel views of processes, FDs, and networking. Handy for debug CLIs and self-inspection—not a portable API (guard with build tags).
Useful paths
| Path | Info |
|---|---|
/proc/self/status |
Pid, VmRSS, threads, fds |
/proc/self/fd |
Open file descriptors |
/proc/self/limits |
rlimits |
/proc/self/net/tcp |
TCP sockets (parse carefully) |
/proc/meminfo |
Host memory |
Count FDs
//go:build linux
func fdCount() (int, error) {
ents, err := os.ReadDir("/proc/self/fd")
if err != nil {
return 0, err
}
return len(ents), nil
}Read VmRSS
b, err := os.ReadFile("/proc/self/status")
// parse line "VmRSS:\t12345 kB"Minimal tool: procself
systool procself
fds N
vmrss N kB
Cgroup memory (containers)
/sys/fs/cgroup/memory.max # v2
/sys/fs/cgroup/memory/memory.limit_in_bytes # v1
Parse when present; fall back if missing (not in cgroup).
Rules
| Do | Don’t |
|---|---|
| Build-tag Linux code | Assume /proc on macOS |
| Treat format as unstable | Parse without tests |
| Prefer metrics APIs for prod | Scrape proc in hot path every µs |
Try next
- Print fd count before/after intentional leak.
- Read cgroup memory limit inside Docker.
- Cross-compile with
//go:build linuxstubs elsewhere.