-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

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

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:

  1. cpu

    • Controls CPU scheduling and allocation.

    • Example path: /sys/fs/cgroup/cpu/

  2. memory

    • Controls memory usage for processes in a cgroup.

    • Example path: /sys/fs/cgroup/memory/

  3. blkio

    • Controls block I/O operations like disk read/write.

    • Example path: /sys/fs/cgroup/blkio/

  4. cpuacct

    • Provides accounting for CPU usage.

    • Example path: /sys/fs/cgroup/cpuacct/

  5. cpuset

    • Controls CPU core and memory node allocation.

    • Example path: /sys/fs/cgroup/cpuset/

  6. devices

    • Controls access to devices (e.g., block devices, character devices).

    • Example path: /sys/fs/cgroup/devices/

  7. freezer

    • Allows freezing and thawing of cgroups.

    • Example path: /sys/fs/cgroup/freezer/

  8. net_cls

    • Allows classification of network traffic for cgroups.

    • Example path: /sys/fs/cgroup/net_cls/

  9. 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:

/sys/fs/cgroup
├── cpu
│   ├── cpu.cfs_period_us
│   ├── cpu.cfs_quota_us
│   └── tasks
├── memory
│   ├── memory.limit_in_bytes
│   ├── memory.usage_in_bytes
│   └── tasks
├── blkio
│   ├── blkio.throttle.read_bps_device
│   ├── blkio.throttle.write_bps_device
│   └── tasks
└── cpuset
    ├── cpuset.cpus
    ├── cpuset.mems
    └── tasks

Key Files and Directories

Here are some of the important files and directories you will encounter within the /sys/fs/cgroup filesystem:

  1. 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

  2. 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

  3. 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

  4. cpuacct.usage

    • Provides the total CPU time used by all processes in the cgroup.

    • Example path: /sys/fs/cgroup/cpuacct/cpuacct.usage

  5. 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:

  1. 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 the cpu cgroup:

    echo 1234 > /sys/fs/cgroup/cpu/tasks
  2. Set Resource Limits: Modify files like memory.limit_in_bytes or cpu.cfs_quota_us to set resource limits for the cgroup:

    echo 1G > /sys/fs/cgroup/memory/memory.limit_in_bytes
  3. Monitor Resource Usage: To check the resource usage of a cgroup, you can read from files like memory.usage_in_bytes or cpuacct.usage:

    cat /sys/fs/cgroup/memory/memory.usage_in_bytes

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