-sys-fs-cgroups
The /sys/fs/cgroup
directory in Linux is a virtual filesystem that provides a mechanism for managing control groups (cgroups). Cgroups are a feature in the Linux kernel that allow processes to be grouped together and managed in terms of resources, such as CPU, memory, and I/O, allowing administrators to allocate and limit resources to specific processes or sets of processes.
Purpose of /sys/fs/cgroup
/sys/fs/cgroup
The /sys/fs/cgroup
directory provides information and control over various resource management parameters for processes. It is part of the cgroup virtual filesystem that allows for runtime configuration and monitoring of control groups. The cgroup filesystem is used to assign processes to control groups, monitor resource usage, and configure resource limits for groups of processes.
Structure of /sys/fs/cgroup
/sys/fs/cgroup
Within /sys/fs/cgroup
, there are subdirectories corresponding to various controllers (or resource subsystems) that manage specific resources. These controllers manage resource limits, monitoring, and prioritization for cgroups. Some of the most common controllers are:
cpu
Controls CPU scheduling and allocation.
Example path:
/sys/fs/cgroup/cpu/
memory
Controls memory usage for processes in a cgroup.
Example path:
/sys/fs/cgroup/memory/
blkio
Controls block I/O operations like disk read/write.
Example path:
/sys/fs/cgroup/blkio/
cpuacct
Provides accounting for CPU usage.
Example path:
/sys/fs/cgroup/cpuacct/
cpuset
Controls CPU core and memory node allocation.
Example path:
/sys/fs/cgroup/cpuset/
devices
Controls access to devices (e.g., block devices, character devices).
Example path:
/sys/fs/cgroup/devices/
freezer
Allows freezing and thawing of cgroups.
Example path:
/sys/fs/cgroup/freezer/
net_cls
Allows classification of network traffic for cgroups.
Example path:
/sys/fs/cgroup/net_cls/
pids
Controls the number of processes in a cgroup.
Example path:
/sys/fs/cgroup/pids/
Example Directory Structure
When you navigate to /sys/fs/cgroup
, you may see directories corresponding to different controllers. Each controller directory will contain files that allow you to manage and monitor the resources for the cgroup associated with that controller.
For example:
Key Files and Directories
Here are some of the important files and directories you will encounter within the /sys/fs/cgroup
filesystem:
tasks
Contains the list of PIDs (process IDs) of the processes that are part of the cgroup. For example,
/sys/fs/cgroup/cpu/tasks
lists the PIDs of processes in the CPU control group.Example path:
/sys/fs/cgroup/cpu/tasks
memory.limit_in_bytes
Specifies the memory limit for the cgroup. If a process exceeds this limit, it can be killed or throttled, depending on the configuration.
Example path:
/sys/fs/cgroup/memory/memory.limit_in_bytes
cpu.cfs_period_us and cpu.cfs_quota_us
These files are used to configure the CPU time period and quota for processes in the cgroup.
Example path:
/sys/fs/cgroup/cpu/cpu.cfs_period_us
cpuacct.usage
Provides the total CPU time used by all processes in the cgroup.
Example path:
/sys/fs/cgroup/cpuacct/cpuacct.usage
cpuset.cpus and cpuset.mems
Specifies which CPU cores and memory nodes the processes in the cgroup can use.
Example path:
/sys/fs/cgroup/cpuset/cpuset.cpus
Use Cases
Resource Limiting: You can use the
/sys/fs/cgroup
directory to limit the amount of CPU, memory, or disk I/O a process or group of processes can use, improving system performance and preventing resource starvation.Monitoring Resource Usage: The files in the cgroup filesystem allow you to track the resource usage of processes in real-time, such as CPU time, memory consumption, and block I/O operations.
Process Management: Cgroups can be used to isolate processes, for example, to control how many processes run in a container or virtual machine. This helps ensure that system resources are allocated appropriately and not overused by any single process or group of processes.
Managing Cgroups
To manipulate cgroups directly through the /sys/fs/cgroup
directory, you can:
Add a Process to a Cgroup: Write the PID of a process to the
tasks
file in the appropriate cgroup directory. For example, to add a process with PID 1234 to thecpu
cgroup:Set Resource Limits: Modify files like
memory.limit_in_bytes
orcpu.cfs_quota_us
to set resource limits for the cgroup:Monitor Resource Usage: To check the resource usage of a cgroup, you can read from files like
memory.usage_in_bytes
orcpuacct.usage
:
Conclusion
The /sys/fs/cgroup
directory is a critical component for managing system resources in Linux. By using cgroups, system administrators can effectively isolate, monitor, and limit the resources that processes use, which is particularly useful in environments like containers and virtualized systems.
Last updated