Operations 29 min read

Master Linux Process Load Balancing with cgroups and taskset: A Step‑by‑Step Guide

This article explains how to use Linux's built‑in cgroups and taskset tools to monitor, limit, and bind process workloads, providing detailed commands, subsystem explanations, collaborative usage strategies, real‑world case studies, and troubleshooting tips for improving system performance and stability.

Deepin Linux
Deepin Linux
Deepin Linux
Master Linux Process Load Balancing with cgroups and taskset: A Step‑by‑Step Guide

1. cgroups: The Core of Resource Management

cgroups (Control Groups) is a Linux kernel feature that isolates and limits the physical resources used by a group of processes. Originating from Google in 2006 and merged into the kernel in 2008, cgroups organize tasks into hierarchies of control groups, each governed by subsystems such as blkio , cpu , memory , cpuacct , cpuset , devices , freezer , and net_cls . These subsystems provide fine‑grained control over I/O, CPU time, memory usage, accounting, CPU affinity, device access, process suspension, and network traffic.

1.1 Key Subsystems

blkio : Limits read/write rates for block devices, useful for separating database I/O from file‑server I/O.

cpu : Controls CPU time slices via cpu.cfs_period_us and cpu.cfs_quota_us, or relative weight with cpu.shares.

memory : Sets a hard memory limit; exceeding it triggers OOM protection.

cpuacct : Generates detailed CPU usage reports for each cgroup.

cpuset : Binds tasks to specific CPU cores and memory nodes, improving cache locality.

devices : Allows or denies device access for security isolation.

freezer : Pauses or resumes a cgroup, handy for maintenance.

net_cls : Tags packets with a class ID so traffic control tools can shape network flow.

1.2 Using cgroups on Ubuntu

Below are the essential commands to create and configure a CPU‑limited cgroup named my_group:

Check kernel support: cat /proc/cgroups Mount the cgroup filesystem (if not already mounted):

sudo mount -t cgroup -o cpu cpu /sys/fs/cgroup/cpu

Create the control group: sudo mkdir /sys/fs/cgroup/cpu/my_group Set CPU limits (e.g., 50 ms of CPU time per 100 ms window):

sudo echo 100000 > /sys/fs/cgroup/cpu/my_group/cpu.cfs_period_us</code><code>sudo echo 50000 > /sys/fs/cgroup/cpu/my_group/cpu.cfs_quota_us

Optionally set a weight:

sudo echo 512 > /sys/fs/cgroup/cpu/my_group/cpu.shares

Add a process (PID 12345) to the group:

sudo echo 12345 > /sys/fs/cgroup/cpu/my_group/tasks

2. taskset: Precise CPU Affinity

taskset sets processor affinity, binding a process or thread to specific CPU cores. By fixing a process to a core, cache misses and context‑switch overhead are reduced, which is critical for high‑performance workloads such as databases, scientific simulations, or real‑time media processing.

2.1 Common Use Cases

Database services : Bind MySQL to cores 0‑1 to avoid cache thrashing.

pgrep -f mysql</code><code>sudo taskset -p -c 0,1 12345

Scientific computing : Run a molecular dynamics program on cores 4‑7.

taskset -c 4-7 /path/to/molecular_dynamics_program

Real‑time applications : Pin a video encoder to core 2.

taskset -p -c 2 /usr/bin/video_encoding_program

3. Combining cgroups and taskset

3.1 Collaborative Principle

cgroups provide holistic resource quotas (CPU, memory, I/O, network), while taskset ensures the processes run on the most suitable cores. Together they isolate services, guarantee fair resource distribution, and maximize cache efficiency.

3.2 Practical Steps (Web + MySQL example)

Mount required subsystems:

sudo mount -t cgroup -o cpu,cpuacct,memory /sys/fs/cgroup

Create control groups:

sudo mkdir /sys/fs/cgroup/cpu,cpuacct,memory/nginx_group</code><code>sudo mkdir /sys/fs/cgroup/cpu,cpuacct,memory/mysql_group

Set limits for Nginx (30 ms CPU per 100 ms, 512 MiB memory):

sudo echo 100000 > /sys/fs/cgroup/cpu,cpuacct,memory/nginx_group/cpu.cfs_period_us</code><code>sudo echo 30000 > /sys/fs/cgroup/cpu,cpuacct,memory/nginx_group/cpu.cfs_quota_us</code><code>sudo echo 536870912 > /sys/fs/cgroup/cpu,cpuacct,memory/nginx_group/memory.limit_in_bytes

Set limits for MySQL (60 ms CPU per 100 ms, 1 GiB memory):

sudo echo 100000 > /sys/fs/cgroup/cpu,cpuacct,memory/mysql_group/cpu.cfs_period_us</code><code>sudo echo 60000 > /sys/fs/cgroup/cpu,cpuacct,memory/mysql_group/cpu.cfs_quota_us</code><code>sudo echo 1073741824 > /sys/fs/cgroup/cpu,cpuacct,memory/mysql_group/memory.limit_in_bytes

Find process IDs: pgrep -f nginx</code><code>pgrep -f mysql Bind Nginx to cores 2‑3 and MySQL to cores 4‑5:

sudo taskset -p -c 2,3 12345</code><code>sudo taskset -p -c 4,5 67890

4. Real‑World Case Study: E‑commerce Platform

During a sales event, the platform suffered CPU usage >95 %, memory ~75 %, and response times >500 ms, causing user churn. By segmenting order processing, product display, and authentication into separate cgroups and binding each to dedicated cores, the team reduced average response time to <200 ms and increased QPS from 300 to over 600.

4.1 Optimization Steps

Create order_group and allocate 70 ms CPU per 100 ms, 1 GiB memory.

Create product_group with 20 ms CPU per 100 ms, 512 MiB memory.

Bind order processes to cores 0‑2 and product processes to cores 3‑5 using taskset.

The combined approach eliminated CPU contention, improved cache locality, and stabilized the service under peak load.

5. Pitfalls & Troubleshooting

5.1 Mis‑configured Limits

Too‑tight CPU or memory caps can cause slowdowns or OOM kills; overly broad core bindings waste resources. Perform load testing (e.g., with JMeter) to determine realistic limits before applying them.

5.2 Compatibility Issues

Older kernels may lack full cgroup features or have buggy subsystems. Verify kernel version (≥ 3.10 recommended) and consider upgrading if subsystems misbehave.

5.3 Process Management Problems

Processes may escape their cgroup after a restart or fail to bind due to insufficient privileges. Use sudo for all commands, monitor cgroup membership regularly, and re‑add stray processes as needed.

Performance optimizationProcess ManagementLinuxresource allocationcgroupstaskset
Deepin Linux
Written by

Deepin Linux

Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.