How to use the command 'systemd-cgls' (with examples)
- Linux
- December 25, 2023
Systemd-cgls is a command used to show the contents of the selected Linux control group hierarchy in a tree. It provides an overview of the control groups and their relationships, helping to understand how resources are organized and allocated in the system.
Use case 1: Display the whole control group hierarchy on your system
Code:
systemd-cgls
Motivation: Understanding the control group hierarchy is essential for system administrators and developers who need to manage resource allocation and usage. By displaying the whole control group hierarchy, you can get a comprehensive view of how resources are organized and allocated in your system.
Explanation: The command systemd-cgls
without any arguments will display the entire control group hierarchy on your system. It shows the relationships between control groups and presents them in a tree-like structure.
Example output:
├─1 /init
├─system.slice
│ ├─cronie.service
│ ├─dbus.service
│ ├─docker.service
│ ├─NetworkManager.service
│ └─...
├─user.slice
├─user-1000.slice
│ ├─session-c2.scope
│ │ ├─9962 sshd: piotr [priv]
│ │ └─9968 sshd: piotr@notty
│ └─session-c1.scope
│ ├─1148 /usr/libexec/gdm-x-session --run-script env GNOME_SHELL_SESSION_MODE=gdm /usr/bin/gnome-session
│ └─1149 /usr/bin/gnome-session
└─session.slice
├─session-1.scope
├─session-3.scope
└─session-2.scope
Use case 2: Display a control group tree of a specific resource controller
Code:
systemd-cgls cpu
Motivation: When dealing with specific resource controllers, such as CPU, memory, or I/O, it can be useful to see the control group hierarchy related to that specific controller. By displaying a control group tree of a specific resource controller, you can get a focused view that helps you understand the resource allocation and usage of the selected controller.
Explanation: The command systemd-cgls
followed by a resource controller argument (e.g., cpu
, memory
, io
) will display the control group hierarchy for that specific resource controller. This allows you to focus on a particular aspect of resource allocation and usage.
Example output:
├─1 /init
├─system.slice
│ ├─docker.service
│ └─...
└─user.slice
├─user-1000.slice
│ ├─session-c2.scope
│ │ └─...
│ └─session-c1.scope
│ └─...
└─session.slice
├─session-1.scope
├─session-3.scope
└─session-2.scope
Use case 3: Display the control group hierarchy of one or more systemd units
Code:
systemd-cgls --unit unit1 unit2 ...
Motivation: Systemd units are used to manage various aspects of system services and processes. Displaying the control group hierarchy of one or more systemd units can help you understand their resource allocation and usage within the system. This can be useful for troubleshooting resource-related issues or optimizing resource allocation.
Explanation: The command systemd-cgls
followed by --unit
and the names of one or more systemd units will display the control group hierarchy of those units. It provides a focused view of the control group hierarchy related to the specified units and helps understand their resource allocation.
Example output:
├─1 /init
├─system.slice
│ └─docker.service
└─user.slice
├─user-1000.slice
│ ├─session-c2.scope
└─session.slice
└─session-3.scope
Conclusion:
The systemd-cgls command is a powerful tool for understanding and managing control groups in the Linux system. It allows you to visualize the control group hierarchy, both as a whole and for specific resource controllers or systemd units. By using systemd-cgls, system administrators and developers can gain insights into resource allocation and usage, troubleshoot issues, and optimize resource management strategies.