cgroup-stats
is a Go library designed for retrieving CPU and Memory limits information from Linux control groups (cgroups).
NOTE: cgroups v1 is not supported at the moment.
To start using cgroup-stats
, install Go and run go get
:
go get -u github.com/mr-karan/cgroup-stats
First, build the Docker image using the following command:
docker buildx build -t mrkaran/cgroups -f Dockerfile .
Execute the container without any specific CPU or memory limits:
docker run --rm -it mrkaran/cgroups
You should see an output similar to:
Number of CPUs on host: 10
Number of operating system threads: 10
CPU quota is not set
Memory limit is not set
Now, let's run the container with specific CPU and memory limits. In this case, we're limiting the container to use only half a CPU core (--cpus=0.5
) and 1000 MiB of memory (--memory=1000m
):
docker run --rm -it --cpus=0.5 --memory=1000m mrkaran/cgroups
You should see an output reflecting the imposed limits:
Number of CPUs on host: 10
Number of operating system threads: 10
CPU quota in the container: 0.500000
Memory limit in the container: 1000.000000 MiB
cgroup-stats
provides easy-to-use functions to retrieve CPU and Memory quota information from cgroups.
GetCPULimits
function returns the CPU quota assigned to the cgroup.
quota, _ := cgroupstats.GetCPULimits()
fmt.Println("CPU quota in the container:", quota)
GetMemoryLimits
function returns the memory limit assigned to the cgroup.
memQuota, _ := cgroupstats.GetMemoryLimits()
fmt.Println("Memory limit in the container:", memQuota/1048576)
See examples/main.go for a complete example.