How to check if a cgroup is configured in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if cgroups are configured on your Linux system. Cgroups are a powerful kernel feature for managing and controlling resource usage of processes.

You will achieve this by listing the available cgroup subsystems using cat /proc/cgroups, examining the cgroup mount points in /sys/fs/cgroup, and verifying the cgroup configuration file /etc/cgconfig.conf. These steps will provide a comprehensive overview of your system's cgroup setup.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") subgraph Lab Skills linux/ls -.-> lab-558700{{"How to check if a cgroup is configured in Linux"}} linux/cat -.-> lab-558700{{"How to check if a cgroup is configured in Linux"}} end

List cgroups with cat /proc/cgroups

In this step, you will learn how to list the available cgroup subsystems on your Linux system. Cgroups (control groups) are a Linux kernel feature that allows you to organize processes into hierarchical groups and control their resource usage (CPU, memory, disk I/O, network, etc.).

Think of cgroups as a way to put limits or allocate resources to specific applications or users. This is crucial for managing system resources effectively, especially in environments like containers or cloud computing.

The /proc filesystem is a virtual filesystem that provides information about processes and other system information. The /proc/cgroups file contains details about the cgroup subsystems supported by your kernel.

To view the available cgroup subsystems, you will use the cat command. The cat command is a fundamental Linux utility used to display the content of files.

Open your terminal if it's not already open. You can do this by clicking the Xfce Terminal icon on the left side of your desktop.

Now, type the following command into the terminal and press Enter:

cat /proc/cgroups

You should see output similar to this:

#subsys_name    hierarchy       num_cgroups     enabled
cpuset  0       1       1
cpu     0       1       1
cpuacct 0       1       1
blkio   0       1       1
memory  0       1       1
devices 0       1       1
freezer 0       1       1
net_cls 0       1       1
perf_event      0       1       1
net_prio        0       1       1
hugetlb 0       1       1
pids    0       1       1
rdma    0       1       1

Let's break down the output:

  • #subsys_name: The name of the cgroup subsystem (e.g., cpu, memory, blkio). Each subsystem controls a specific type of resource.
  • hierarchy: The ID of the hierarchy the subsystem is attached to. In this output, all subsystems are attached to hierarchy 0, which is the default unified hierarchy in newer Linux distributions.
  • num_cgroups: The number of cgroups currently created within this subsystem's hierarchy.
  • enabled: Indicates whether the subsystem is enabled (1) or disabled (0) in the kernel.

This output shows you which resource control features are available and active on your system. Understanding these subsystems is the first step in learning how to manage resources with cgroups.

Click Continue to proceed to the next step.

Check cgroup mounts in /sys/fs/cgroup

In the previous step, you saw the available cgroup subsystems. Now, let's explore where these cgroups are mounted in the filesystem. In Linux, cgroups are typically exposed through a virtual filesystem, usually mounted under /sys/fs/cgroup.

The /sys filesystem is another virtual filesystem that provides an interface to kernel data structures. It's often used to configure and monitor hardware and kernel features. The /sys/fs/cgroup directory is the standard mount point for the cgroup filesystem.

To see the contents of this directory and understand how the cgroup hierarchies are organized, you will use the ls command. The ls command lists the contents of a directory.

Open your terminal if it's not already open.

Type the following command into the terminal and press Enter:

ls /sys/fs/cgroup

You should see output similar to this:

cgroup.controllers  cgroup.max.depth  cgroup.max.descendants  cgroup.stat  cgroup.subtree_control  cgroup.threads  cpu  cpu.stat  cpu.pressure  cpuset  cpuset.cpus  cpuset.mems  cpuset.stat  io  io.stat  io.pressure  memory  memory.stat  memory.pressure  memory.swap.max  memory.high  memory.low  memory.min  memory.swap.current  memory.current  memory.events  memory.events.local  pids  pids.current  pids.max  systemd  user.slice

This output shows the directories and files within the /sys/fs/cgroup directory. In systems using the unified cgroup hierarchy (cgroup v2), you'll see files like cgroup.controllers, cgroup.stat, and directories corresponding to the mounted controllers (like cpu, memory, io, pids).

Each of these directories (like cpu, memory, etc.) represents a cgroup controller. Inside these directories, you'll find files that allow you to configure and monitor the resource limits for processes within that cgroup.

For example, if you were to navigate into the cpu directory, you would find files related to CPU resource control.

Let's quickly look inside the cpu directory using ls:

ls /sys/fs/cgroup/cpu

You might see output like:

cgroup.controllers  cgroup.events  cgroup.freeze  cgroup.max.depth  cgroup.max.descendants  cgroup.stat  cgroup.subtree_control  cgroup.threads  cpu.max  cpu.stat  cpu.weight  cpu.pressure

These files (like cpu.max, cpu.weight) are used to set CPU limits and priorities for cgroups.

Understanding the structure of /sys/fs/cgroup is key to working with cgroups, as this is where you interact with them directly.

Click Continue to move on to the next step.

Verify cgroup config with cat /etc/cgconfig.conf

In the previous steps, you learned about the available cgroup subsystems and where they are mounted in the filesystem. Now, let's look at a common configuration file for cgroups: /etc/cgconfig.conf.

While cgroups can be managed directly through the /sys/fs/cgroup filesystem, configuration files like /etc/cgconfig.conf are often used to define cgroup hierarchies and initial settings when the system starts. This file is part of the cgroup-tools package, which provides utilities for managing cgroups.

The /etc/cgconfig.conf file is read by the cgconfig service (if installed and enabled) to set up cgroup hierarchies and attach controllers at boot time. Examining this file can give you insight into how cgroups are configured on your system.

You will again use the cat command to display the content of this configuration file.

Open your terminal if it's not already open.

Type the following command into the terminal and press Enter:

cat /etc/cgconfig.conf

You should see output similar to this (the exact content may vary depending on the system's configuration):

#
## A sample configuration file for cgconfig.
#
## The file contains two sections:
##   mount: defines the hierarchy and where to mount it.
##   group: defines the cgroup and its parameters.
#
## Example:
#
#mount {
##       cpuset = /cgroup/cpuset;
##       cpu = /cgroup/cpu;
##       cpuacct = /cgroup/cpuacct;
##       memory = /cgroup/memory;
##       devices = /cgroup/devices;
##       freezer = /cgroup/freezer;
##       net_cls = /cgroup/net_cls;
##       blkio = /cgroup/blkio;
#}
#
#group staff {
##       perm {
##               task {
##                       uid = staff;
##                       gid = staff;
##               }
##               admin {
##                       uid = staff;
##                       gid = staff;
##               }
##       }
##       cpu {
##               cpu.shares = 1000;
##       }
##       memory {
##               memory.limit_in_bytes = 256M;
##       }
#}
#
#group / {
##       cpu {
##               cpu.shares = 1024;
##       }
#}

In this example output, you can see commented-out sections (#) that show how you might define mount points for different controllers and create cgroup groups (like staff) with specific resource limits (e.g., cpu.shares, memory.limit_in_bytes).

If the file is empty or contains only comments, it means that cgroup hierarchies and initial groups are not being configured through this specific file. In modern systems, systemd often handles cgroup management, using the unified hierarchy mounted at /sys/fs/cgroup.

Even if this file isn't actively used for configuration on your system, knowing about its existence and purpose is important for understanding how cgroups can be managed.

You have now explored the available cgroup subsystems, their mount points in the filesystem, and a common configuration file. This gives you a foundational understanding of how cgroups are structured and potentially configured on a Linux system.

Click Continue to complete this lab.

Summary

In this lab, you learned how to check if cgroups are configured on a Linux system. You started by using the cat /proc/cgroups command to list the available cgroup subsystems supported by the kernel, understanding the output columns like subsys_name, hierarchy, num_cgroups, and enabled. This step provided insight into which resource control mechanisms are active.

Next, you verified the cgroup mount points by examining the /sys/fs/cgroup directory, which is the standard location where cgroup hierarchies are mounted. Finally, you checked the /etc/cgconfig.conf file to see if any persistent cgroup configurations are defined, although this file might not exist or be used on all systems, especially those relying on systemd for cgroup management. These steps collectively demonstrate how to determine the presence and basic configuration of cgroups on a Linux system.