Skip to content

puzl-cloud/go-cgroup2

Repository files navigation

go-cgroup2

A Go library designed to configure various cgroup v2 parameters, including CPU, memory, and I/O limits.

Features

  • 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.

Installation

go get github.com/puzl-cloud/go-cgroup2

Quick Start

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")
}

Configuration Format

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"
      }
    ]
  }
}

Supported Value Formats

  • 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

Documentation

See the docs directory for detailed documentation.

About

Go library for configuring cgroup v2

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages