8.9 Use Cases for cgroups

8.9.1 Pinning Processes to CPU Cores
8.9.2 Controlling CPU and Memory Usage
8.9.3 Restricting Access to Devices
8.9.4 Throttling I/O Bandwidth

The following sections describe sample /etc/cgconfig.conf entries for cgroups that can control the access that processes have to system resources.

8.9.1 Pinning Processes to CPU Cores

Define two cgroups that can be used to assign tasks to run on different sets of CPU cores.

mount {
    cpuset = /cgroup/coregrp;
}

group locores {
    cpuset {
        cpuset.mems="0";
#       Run tasks on cores 0 through 3
        cpuset.cpus="0-3"; 
    }
}

group hicores {
    cpuset {
        cpuset.mems="0";
#       Run tasks on cores 4 through 7
        cpuset.cpus="4-7"; 
    }
}

8.9.2 Controlling CPU and Memory Usage

Define two cgroups with different allocations of available CPU time and memory resources.

mount {
    cpu = /cgroup/cpumem;
    cpuset = /cgroup/cpumem;
    memory = /cgroup/cpumem;
}

# High priority group
group hipri { 
    cpu {
#       Set the relative share of CPU resources equal to 75%
        cpu.shares="750"; 
    }
    cpuset {
#       No alternate memory nodes if the system is not NUMA
        cpuset.mems="0"; 
#       Make all CPU cores available to tasks
        cpuset.cpus="0-7"; 
    }
    memory {
#       Allocate at most 2 GB of memory to tasks
        memory.limit_in_bytes="2G"; 
#       Allocate at most 4 GB of memory+swap to tasks
        memory.memsw.limit_in_bytes="4G"; 
#       Apply a soft limit of 1 GB to tasks
        memory.soft_limit_in_bytes="1G"; 
    }
}

# Low priority group
group lopri { 
    cpu {
#       Set the relative share of CPU resources equal to 25%
        cpu.shares="250"; 
    }
    cpuset {
#       No alternate memory nodes if the system is not NUMA
        cpuset.mems="0"; 
#       Make only cores 0 and 1 available to tasks
        cpuset.cpus="0,1"; 
    }
    memory {
#       Allocate at most 1 GB of memory to tasks
        memory.limit_in_bytes="1G"; 
#       Allocate at most 2 GB of memory+swap to tasks
        memory.memsw.limit_in_bytes="2G"; 
#       Apply a soft limit of 512 MB to tasks
        memory.soft_limit_in_bytes="512M"; 
    }
}

8.9.3 Restricting Access to Devices

Define a cgroup that denies access to the disk devices /dev/sd[bcd].

mount {
    devices = /cgroup/devlist;
}

group blkdev {
    devices {
#       Deny access to /dev/sdb
        devices.deny="b 8:16 mrw"; 
#       Deny access to /dev/sdc
        devices.deny="b 8:32 mrw"; 
#       Deny access to /dev/sdd
        devices.deny="b 8:48 mrw"; 
    }
}

8.9.4 Throttling I/O Bandwidth

Define a cgroup that limits the I/O bandwidth to 50MB/s when reading from /dev/sda1.

mount {
    blkio = /cgroup/iolimit;
}

group iocap1 {
    blkio  {
#       Limit reads from /dev/sda1 to 50 MB/s
        blkio.throttle.read_bps_device="8:1 52428800"; 
    }
}

Define a cgroup that limits the number of read transactions to 100 per second when reading from /dev/sdd.

mount {
    blkio = /cgroup/iolimit;
}

group iocap2 {
    blkio  {
#       Limit read tps from /dev/sdd to 100 per second
        blkio.throttle.read_iops_device="8:48 100";
    }
}

Define two cgroups with different shares of I/O access to /dev/sdb .

mount {
    blkio = /cgroup/iolimit;
}

# Low access share group
group iolo {
    blkio  {
#       Set the share of I/O access by /dev/sdb to 25%
        blkio.weight_device="8:16 250"; 
    }
}

# High access share group
group iohi {
    blkio  {
#       Set the share of I/O access by /dev/sdb to 75%
        blkio.weight_device="8:16 750"; 
    }
}