How to use the command `systemd-cgtop` (with examples)

How to use the command `systemd-cgtop` (with examples)

The systemd-cgtop command is used to display the top control groups of the local Linux control group hierarchy, ordered by their CPU, memory, or disk I/O load. It provides an interactive view similar to the top command, but specific to control groups. By default, it shows the CPU usage percentage, memory usage in bytes, and I/O operations per second for each control group.

Use case 1: Start an interactive view

Code:

systemd-cgtop

Motivation: Starting an interactive view is the simplest and most common use case of the systemd-cgtop command. It allows you to monitor the CPU, memory, and disk I/O load of control groups in real-time, helping you identify any resource-intensive processes or groups of processes.

Explanation: Running the systemd-cgtop command without any additional arguments starts the interactive view of the control groups. It continuously updates the display based on the current resource usage of the control groups.

Example output:

Path         Tasks   Δ     CPU                 Memory              Input/s              Output/s             
/             463     +2    0.8%                4.9G                0B                   0B                   
/system       225     +1    0.6%                4.1G                0B                   0B                   
/systemd      226            0.5%                116.0M              0B                   0B                   
/docker       210            0.3%                3.3G                0B                   0B                   

The output displays the path of each control group, number of tasks running within it, the change in the number of tasks since the last update, CPU usage percentage, memory usage in bytes, input and output operations per second.

Use case 2: Change the sort order

Code:

systemd-cgtop --order=cpu

Motivation: Changing the sort order allows you to prioritize control groups based on their CPU usage. This can be useful when you want to identify the most resource-intensive control groups and how they are affecting the system.

Explanation: The --order option is used to specify the sort order of the control groups. In this example, we have set it to cpu to order the control groups based on their CPU usage. Other available options for --order are memory, path, tasks, and io.

Example output:

Path         Tasks   Δ     CPU                 Memory              Input/s              Output/s             
/system       225     +1    0.6%                4.1G                0B                   0B                   
/docker       210            0.3%                3.3G                0B                   0B                   
/systemd      226            0.5%                116.0M              0B                   0B                   
/             463     +2    0.8%                4.9G                0B                   0B                   

The output now shows the control groups ordered by their CPU usage in descending order.

Use case 3: Show the CPU usage by time instead of percentage

Code:

systemd-cgtop --cpu=percentage

Motivation: Displaying the CPU usage by time instead of percentage allows you to see the actual CPU usage duration for each control group, giving you more insight into their resource consumption.

Explanation: The --cpu option is used to specify the mode of displaying CPU usage. By default, systemd-cgtop displays the CPU usage as a percentage. In this example, we have set it to percentage to show the CPU usage by time in milliseconds.

Example output:

Path         Tasks   Δ     CPU (ms)            Memory              Input/s              Output/s             
/             463     +2    190.2ms             4.9G                0B                   0B                   
/system       225     +1    168.7ms             4.1G                0B                   0B                   
/systemd      226            145.4ms             116.0M              0B                   0B                   
/docker       210            102.3ms             3.3G                0B                   0B                   

The output now includes the CPU usage in milliseconds for each control group.

Use case 4: Change the update interval

Code:

systemd-cgtop --delay=2s

Motivation: Changing the update interval allows you to control the frequency at which systemd-cgtop updates the display. Increasing the interval can reduce the system load caused by the command.

Explanation: The --delay option is used to specify the update interval of systemd-cgtop. In this example, we have set it to 2 seconds using the s suffix. You can also use other time units such as ms (milliseconds), us (microseconds), and min (minutes).

Example output (after waiting for 2 seconds):

Path         Tasks   Δ     CPU                 Memory              Input/s              Output/s             
/             620     +3    1.2%                4.9G                0B                   0B                   
/system       292            0.8%                4.1G                0B                   0B                   
/systemd      292            0.6%                116.0M              0B                   0B                   
/docker       260            0.4%                3.3G                0B                   0B                   

The output now reflects the updated resource usage after the specified delay of 2 seconds.

Use case 5: Only count userspace processes

Code:

systemd-cgtop -P

Motivation: By default, systemd-cgtop counts both userspace processes and kernel threads in the control groups. In some cases, you may only want to monitor userspace processes, ignoring the kernel threads that don’t directly impact the resource usage.

Explanation: The -P option is a shorthand for --peers. It tells systemd-cgtop to only count userspace processes (tasks) and exclude kernel threads from the displayed statistics.

Example output:

Path         Tasks   Δ     CPU                 Memory              Input/s              Output/s             
/             449     +2    0.5%                5.0G                0B                   0B                   
/system       218     +1    0.4%                3.5G                0B                   0B                   
/systemd      221            0.3%                87.5M               0B                   0B                   
/docker       207            0.2%                4.6G                0B                   0B                   

The output now excludes kernel threads, showing only userspace processes for each control group.

Conclusion:

The systemd-cgtop command is a powerful tool for monitoring and analyzing the resource usage of control groups in real-time. By using different options and arguments, you can customize the display to fit your specific needs, such as changing the sort order, displaying CPU usage by time, adjusting update intervals, and excluding kernel threads. These use cases can help you identify and address performance bottlenecks in your system.

Related Posts

How to use the command "PSVersionTable" (with examples)

How to use the command "PSVersionTable" (with examples)

The PSVersionTable command is a read-only variable in PowerShell that allows users to retrieve information about the current PowerShell version.

Read More
How to use the command 'notifyd' (with examples)

How to use the command 'notifyd' (with examples)

The ’notifyd’ command is a notification server that should not be invoked manually.

Read More
Using AWS CLI to Generate Pre-signed URLs for Amazon S3 Objects (with examples)

Using AWS CLI to Generate Pre-signed URLs for Amazon S3 Objects (with examples)

When working with Amazon S3, there may be cases where you want to share access to specific objects stored within your buckets, without making them publicly accessible.

Read More