A Go library designed to configure various cgroup v2 parameters, including CPU, memory, and I/O limits.
- Memory Limiting: Set memory usage limits to prevent excessive resource consumption.
- I/O Throttling: Manage read and write speeds on block devices to regulate process input/output operations.
go get github.com/puzl-cloud/go-cgroup2
package main
import (
"fmt"
"log"
"os"
"github.com/puzl-cloud/go-cgroup2/pkg/cgroup2"
)
func main() {
// Check if the cgroup path is provided
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <cgroup-path>")
fmt.Println("Example: go run main.go /sys/fs/cgroup/system.slice/my-service.service")
os.Exit(1)
}
cgroupPath := os.Args[1]
// Example JSON configuration
jsonConfig := `{
"Classes": {
"quota": [
{
"Devices": ["/dev/sda"],
"ThrottleReadIOPS": "1000",
"ThrottleWriteIOPS": "800",
"ThrottleReadBps": "50M",
"ThrottleWriteBps": "30M"
}
]
}
}`
log.Printf("Sets IO limits to cgroup: %s\n", cgroupPath)
manager := cgroup2.NewManager(cgroupPath)
err := manager.SetIOLimitsFromJSONString(jsonConfig)
if err != nil {
log.Fatalf("Failed to apply IO limits: %v\n", err)
}
log.Println("Successfully applied IO limits")
}
The package uses JSON configuration with the following structure:
{
"Classes": {
"example": [
{
"Devices": ["/dev/sda", "/dev/sdb", "/dev/mapper/*"],
"ThrottleReadIOPS": "1000",
"ThrottleWriteIOPS": "800",
"ThrottleReadBps": "50M",
"ThrottleWriteBps": "30M"
}
]
}
}
- IOPS: Numeric values (e.g., "1000")
- Bandwidth: Values with k, M, G suffixes (e.g., "10M" for 10 megabytes per second)
- Special values: "max" or "unlimited" to set no limit
See the docs directory for detailed documentation.