Linux Cgroup from First Principles
a year ago
- #cgroups
- #Linux
- #system-resources
- Linux cgroups (control groups) are used to limit resource allocations such as memory, CPU, and network bandwidth for processes.
- Cgroup v2, introduced in Linux kernel 4.5, offers a simpler design compared to v1.
- Linux follows the Unix philosophy 'everything is a file,' allowing interaction with kernel subsystems via file operations.
- Cgroups are organized under `/sys/fs/cgroup`, with each cgroup containing virtual files for setting limits and monitoring usage.
- Controllers like `memory.max` and `cpu.max` can be enabled in cgroups to enforce resource constraints.
- Processes can be moved to a cgroup by writing their PID to `cgroup.procs` or using tools like `cgexec`.
- Memory limits can be set (e.g., 5MiB) to prevent processes from exceeding allocated resources, triggering OOM (Out of Memory) kills.
- CPU throttling can be applied (e.g., 1% CPU allocation) to simulate over-subscribed machines.
- Cgroups provide isolation and are useful for performance benchmarking under constrained conditions.
- Error messages for invalid cgroup operations (e.g., 'no internal process' constraint) could be more descriptive.