The Linux kernel's CPU Throttling mechanism is primarily designed to regulate the CPU usage of a process or a group of processes to prevent them from consuming too much CPU resources, thus ensuring fairness and preventing system instability. This mechanism is implemented using the Completely Fair Scheduler (CFS) in the Linux kernel version 3.13 and later.
When a CFS group, which can contain multiple threads, exceeds its CPU share, it may get throttled. The throttled_usec accounts for the time a group is throttled for, measured in microseconds. The throttling period is not fixed and can vary based on the system's workload and the CFS parameters.
Here's a breakdown of the throttling mechanism:
-
CFS Scheduler: The CFS scheduler is responsible for scheduling tasks (threads) in a fair manner. It maintains a run queue for each CPU and a global run queue. Each task is assigned a runtime budget, and the scheduler tries to ensure that each task gets its fair share of CPU time.
-
CFS Group: A CFS group is a collection of tasks that share the same runtime budget. A process can have multiple threads, and each thread can belong to a different CFS group or the same group.
-
CPU Shares: CPU shares represent the proportion of the CPU's total capacity that a CFS group can consume. A higher CPU share means a higher priority for the group.
-
Throttling: When a CFS group exceeds its CPU share, the CFS scheduler may throttle it, reducing its CPU usage. The
throttled_usecis incremented to keep track of the time the group is throttled. -
Throttling Period: The throttling period is not fixed and can vary based on the system's workload and the CFS parameters. When the system is under heavy load, the throttling period may be longer, allowing other tasks to run. Conversely, when the system is less loaded, the throttling period may be shorter.
Here's an example of a code snippet showing the throttled_usec for a CFS group:
# cat /sys/fs/cgroup/cpu/<cgroup_id>/cfs.throttled_usec
1234567
In this example, 1234567 represents the number of microseconds the CFS group has been throttled.
References