cgroups

  • what are cgroups: A control group is a linux mechanism for grouping processes so that their hardware resource (cpu time, memory, IO, etc) can be measured and controlled collectively. Eg put a program and its child processes in a cgroup and limit their combined CPU usage to 2 cores, or memory usage to 1GB, etc.
  • How to create cgroups: sudo mkdir /sys/fs/cgroup/<name>.
  • How to create temporary cgroup: systemd-run --user --unit=banana --scope -p CPUQuota=200% -p MemoryMax=2G -- ./a.out
  • How to run a process in a specific cgroup: bash -c 'echo $$ | sudo tee /sys/fs/cgroup/<cgroup_name>/cgroup.procs >/dev/null && exec ./a.out arg1 arg2'
  • How to check statistics of the cgroup: cat /sys/fs/cgroup/<name>/cpu.stat
  • Given a process, how to tell its cgroup: cat /proc/<pid>/cgroup
  • How to kill all process in a cgroup: echo 1 | sudo tee /sys/fs/cgroup/<name>/cgroup.kill
    • kill, freeze
  • pid in a cgroup: cat /sys/fs/cgroup/banana/cgroup.procs