Documentation
¶
Overview ¶
Package cgroup reads CPU and memory resource limits from the Linux cgroup v2 unified hierarchy (/sys/fs/cgroup). cgroup v1 is not supported. On non-Linux platforms all functions return (0, nil).
All public functions return 0 when no limit is configured (unlimited).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CPULimitMilli ¶
CPULimitMilli returns the CPU limit of the current process's cgroup in millicores (mCPU). Reads cpu.max; "max <period>" means unlimited → returns 0.
Example ¶
ExampleCPULimitMilli shows how to read the CPU limit from cgroup v2 and use it to cap GOMAXPROCS so the Go scheduler does not create more OS threads than the container is allowed to run.
package main
import (
"fmt"
"log/slog"
"runtime"
"github.com/phcp-tech/common-library-golang/cgroup"
)
func main() {
milli, err := cgroup.CPULimitMilli()
if err != nil {
slog.Error("read cpu limit", "error", err)
return
}
if milli == 0 {
fmt.Println("no CPU limit configured (unlimited)")
return
}
// Convert millicores to whole CPUs, minimum 1.
cpus := milli / 1000
if cpus < 1 {
cpus = 1
}
runtime.GOMAXPROCS(cpus)
fmt.Printf("CPU limit: %d mCPU → GOMAXPROCS set to %d\n", milli, cpus)
}
Output:
func CPURequestMilli ¶
CPURequestMilli returns the CPU request of the current process's cgroup in millicores (mCPU). Reads cpu.weight and converts to millicores via the Kubernetes weight→shares→mCPU formula:
shares = (weight × 262144 + 5000) / 10000 [clamped to 2–262144] mCPU = (shares × 1000 + 512) / 1024
Example ¶
ExampleCPURequestMilli shows how to read the CPU request (guaranteed share) from cgroup v2. This is useful for logging resource allocation at startup.
package main
import (
"fmt"
"log/slog"
"github.com/phcp-tech/common-library-golang/cgroup"
)
func main() {
milli, err := cgroup.CPURequestMilli()
if err != nil {
slog.Error("read cpu request", "error", err)
return
}
if milli == 0 {
fmt.Println("no CPU request configured (BestEffort)")
return
}
fmt.Printf("CPU request: %d mCPU\n", milli)
}
Output:
func MemoryLimitBytes ¶
MemoryLimitBytes returns the memory limit of the current process's cgroup in bytes. Reads memory.max; "max" means unlimited → returns 0.
Example ¶
ExampleMemoryLimitBytes shows how to read the memory limit from cgroup v2 and use it to size an in-process cache as a fraction of the container limit.
package main
import (
"fmt"
"log/slog"
"github.com/phcp-tech/common-library-golang/cgroup"
)
func main() {
limitBytes, err := cgroup.MemoryLimitBytes()
if err != nil {
slog.Error("read memory limit", "error", err)
return
}
if limitBytes == 0 {
fmt.Println("no memory limit configured (unlimited)")
return
}
// Use at most 25 % of the container memory limit for an in-process cache.
cacheBytes := limitBytes / 4
fmt.Printf("memory limit: %d MiB → cache budget: %d MiB\n",
limitBytes>>20, cacheBytes>>20)
}
Output:
func MemoryRequestBytes ¶
MemoryRequestBytes returns the memory soft limit (request) of the current process's cgroup in bytes. Reads memory.low; "0" or "max" → returns 0.
Example ¶
ExampleMemoryRequestBytes shows how to read the memory soft limit (request) from cgroup v2. A value of 0 means no soft limit is set.
package main
import (
"fmt"
"log/slog"
"github.com/phcp-tech/common-library-golang/cgroup"
)
func main() {
requestBytes, err := cgroup.MemoryRequestBytes()
if err != nil {
slog.Error("read memory request", "error", err)
return
}
if requestBytes == 0 {
fmt.Println("no memory request configured")
return
}
fmt.Printf("memory request (soft limit): %d MiB\n", requestBytes>>20)
}
Output:
Types ¶
This section is empty.