Resource Limits
Runix can enforce CPU and memory limits on processes using Linux cgroups v2.
Implementation
Located at internal/cgroups/:
| File | Responsibility |
|---|---|
manager.go | Manager interface and helper functions |
manager_linux.go | Linux cgroups v2 implementation |
manager_other.go | No-op implementation for non-Linux platforms |
Configuration
processes:
api:
entrypoint: ./cmd/api
resources:
cpu_quota: "50%"
memory_limit: "512MB"
heavy-worker:
entrypoint: worker.py
resources:
cpu_quota: "200%"
memory_limit: "2GB"
Fields
| Field | Type | Description |
|---|---|---|
cpu_quota | string | CPU limit as percentage (e.g., "50%", "200%") |
memory_limit | string | Memory limit with unit (e.g., "512MB", "1GB", "1024KB") |
Memory Units
| Unit | Multiplier |
|---|---|
KB | 1024 |
MB | 1,048,576 |
GB | 1,073,741,824 |
Platform Support
| Platform | Support |
|---|---|
| Linux (cgroups v2) | Full — CPU and memory limits enforced |
| macOS | Not supported — returns error if limits are configured |
| Windows | Not supported — returns error if limits are configured |
How It Works (Linux)
cgroup Structure
/sys/fs/cgroup/runix/<pid>/
├── cgroup.procs # Process membership
├── cpu.max # CPU quota and period
└── memory.max # Memory limit in bytes
CPU Quota
CPU quota is converted from percentage to microseconds per 100ms period:
| Config | cpu.max |
|---|---|
"50%" | 50000 100000 |
"100%" | 100000 100000 |
"200%" | 200000 100000 |
Memory Limit
Memory limit is converted to bytes and written to memory.max:
| Config | Bytes |
|---|---|
"512MB" | 536870912 |
"1GB" | 1073741824 |
Lifecycle
- On start: Create cgroup at
/sys/fs/cgroup/runix/<pid>/, add PID, set limits - On stop: Remove the cgroup directory
- Stats: Read
memory.currentfor current memory usage
No-op Implementation
On non-Linux platforms, the noopManager returns an error if limits are configured:
func (m *noopManager) Apply(pid int, limits Limits) error {
if limits.CPUQuota != "" || limits.MemoryLimit != "" {
return fmt.Errorf("resource limits require cgroups v2 (Linux only)")
}
return nil
}
What's Next
- Configuration Reference — Resource config fields
- Metrics — Per-process resource monitoring