What are cgroups ?

  • cgroups (control groups, which can be hierarchical) is a Linux kernel feature that limits, account for, and isolate the resource usage (CPU, memory, disk, IO, network, etc) of a collection of processes. It provides features like resource limiting, prioritization, accounting and control. (cite from 2)

  • subsystems 3

    cat /proc/cgroups (or ls -l /sys/fs/cgroups) to see enabled cgroups

  • cpuset     - assigns individual processor(s) and memory nodes to task(s) in a group;
    cpu        - uses the scheduler to provide cgroup tasks access to the processor resources;
    cpuacct    - generates reports about processor usage by a group;
    io         - sets limit to read/write from/to block devices;
    memory     - sets limit on memory usage by a task(s) from a group;
    devices    - allows access to devices by a task(s) from a group;
    freezer    - allows to suspend/resume for a task(s) from a group;
    net_cls    - allows to mark network packets from task(s) from a group;
    net_prio   - provides a way to dynamically set the priority of network traffic per network interface for a group;
    perf_event - provides access to perf events) to a group;
    hugetlb    - activates support for huge pages for a group;
    pid        - sets limit to number of processes in a group
    

Usage

  • cpuset 2

  • net_cls ( or [6])

    # net_cls : allows to mark network packets from task(s) from a group;
    mkdir /sys/fs/cgroups/net_cls
    mount -t cgroup -onet_cls net_cls /sys/fs/cgroup/net_cls
    mkdir /sys/fs/cgroup/net_cls/test
      
    echo 0x10001 > /sys/fs/cgroup/net_cls/test/net_cls.classid # see [4] or [5]
      
    sudo tc qdisc add dev eth0 root handle 1:0 htb default 3
    sudo tc class add dev eth0 parent 1:0 classid 1:0 htb rate 700mbps ceil 800mbps prio 0
    sudo tc class add dev eth0 parent 1:0 classid 1:1 htb rate 100mbps ceil 200mbps prio 0
    sudo tc class add dev eth0 parent 1:0 classid 1:2 htb rate 200mbps ceil 300mbps prio 0
    sudo tc class add dev eth0 parent 1:0 classid 1:3 htb rate 300mbps ceil 400mbps prio 0
      
    sudo tc filter add dev eth0 protocol ip parent 1:0 prio 1 handle 1:1 cgroup
    sudo cgexec -g net_cls:test iperf3 -c 192.168.0.53 -i 1 -t 5
    
  • refer to https://github.com/yongchaoHe/scripts/tree/master/cgroup [private]

REF

  1. wiki
  2. cgroups documentation
  3. Linux inside: cgroups
  4. tc-cgroup
  5. cgroup子系统之net_cls和net_prio
  6. Intruduction to net_cls subsystem