Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CPULimitMilli ¶
CPULimitMilli 返回当前进程 cgroup 的 CPU 限制(毫核,mCPU)。 不限时返回 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 返回当前进程 cgroup 的 CPU 请求(毫核,mCPU)。 未显式请求时返回 0。
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 返回当前进程 cgroup 的内存限制(字节)。 不限时返回 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 返回当前进程 cgroup 的内存请求(字节)。 未显式请求时返回 0。
注意:在 Kubernetes 中,容器的 memory request 默认不会写入 cgroup,只用于调度和 OOM 打分。 本函数读取的 memory.low(v2)和 memory.soft_limit_in_bytes(v1),K8s 都不设置。 只有开启 MemoryQoS(cgroup v2)时,kubelet 才会把 request 写入 memory.min,而这里没有读取 memory.min。 所以 0 不表示"没有设置 request",而表示"无法获取"。已在 UAT 容器内验证:Octopus 设置了 request, 但 memory.min 和 memory.low 都是 0。metrics.GetMetrics 因此不再输出 memoryRequest,不要用本函数的结果判断 request。
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 ¶
type CPUThrottle ¶ added in v0.1.45
type CPUThrottle struct {
// Periods is nr_periods: the number of CFS periods that have elapsed.
Periods uint64
// Throttled is nr_throttled: the number of periods in which the cgroup used all its quota and was throttled.
Throttled uint64
// ThrottledNs is the total time the cgroup was throttled, in nanoseconds.
ThrottledNs uint64
}
CPUThrottle is the cumulative CPU throttle counters of a cgroup (from cpu.stat). The counters only increase, so the caller must take the difference of two reads. All fields are 0 when the cgroup has no CPU limit, or the counters can not be read.
func CPUThrottleStat ¶ added in v0.1.45
func CPUThrottleStat() (CPUThrottle, error)
CPUThrottleStat 返回当前进程 cgroup 累计的 CPU 节流计数(cpu.stat)。 计数只增不减,调用方需要取两次读数的差值。 不限 CPU 时各字段为 0。读取失败时返回错误。