Introduction
In this lab, we will explore the Linux top command, a powerful tool for monitoring system performance and resource utilization. We will start by understanding the basic functionality of the top command, including its ability to display real-time information about running processes, CPU, memory, and other system resources. We will then dive into the various options and customizations available to tailor the top command's output to our specific needs, enabling us to effectively analyze system performance and identify potential bottlenecks.
Understand the top Command
In this step, we will learn about the top command, which is a powerful tool for monitoring system performance and resource utilization in Linux. The top command provides real-time information about running processes, CPU, memory, and other system resources.
Let's start by running the top command:
$ top
Example output:
top - 14:22:37 up 1 day, 12:34, 0 users, load average: 0.00, 0.01, 0.05
Tasks: 101 total, 1 running, 99 sleeping, 1 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1992.0 total, 1537.0 free, 235.0 used, 220.0 buff/cache
MiB Swap: 2047.9 total, 2047.9 free, 0.0 used. 1555.3 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 8788 5688 3828 S 0.0 0.3 0:02.14 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp
5 root 20 0 0 0 0 I 0.0 0.0 0:00.00 slub_flushwq
The top command displays various system information, including:
- Uptime: The time since the system was last booted.
- Tasks: The total number of running, sleeping, stopped, and zombie processes.
- CPU utilization: The percentage of CPU time spent in user mode, system mode, nice mode, idle, waiting, hardware interrupts, and software interrupts.
- Memory usage: The total, free, used, and buffered/cached memory.
- Swap usage: The total, free, and used swap space.
- Process list: A list of the most resource-intensive processes, sorted by CPU or memory usage.
You can customize the top command's output by pressing various keys, such as:
h: Display the help menu1: Toggle between per-CPU and aggregate CPU utilizationf: Manage the columns displayedo: Customize the sort orderu: Filter processes by a specific user
To exit the top command, press q.
Explore top Command Options and Customizations
In this step, we will explore the various options and customizations available in the top command to tailor the output to your specific needs.
Let's start by displaying the help menu:
$ top -h
This will show you all the available options and their descriptions. Some of the commonly used options include:
-d: Set the delay between updates (default is 3 seconds)-n: Specify the number of iterations (default is unlimited)-p: Monitor specific process IDs-u: Display processes owned by a specific user-o: Sort the process list by a specific column
For example, to display the top 5 processes sorted by memory usage, you can run:
$ top -o %MEM -n 5
Example output:
top - 14:30:37 up 1 day, 12:42, 0 users, load average: 0.00, 0.00, 0.00
Tasks: 101 total, 1 running, 99 sleeping, 1 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1992.0 total, 1537.0 free, 235.0 used, 220.0 buff/cache
MiB Swap: 2047.9 total, 2047.9 free, 0.0 used. 1555.3 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 8788 5688 3828 S 0.0 0.3 0:02.14 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp
5 root 20 0 0 0 0 I 0.0 0.0 0:00.00 slub_flushwq
You can also customize the columns displayed by pressing the f key while in the top command. This will allow you to add, remove, or rearrange the columns to suit your needs.
To exit the top command, press q.
Analyze System Performance Using top
In this final step, we will use the top command to analyze the system performance and identify any potential bottlenecks or issues.
Let's start by monitoring the system's overall performance:
$ top
Pay attention to the following key metrics:
- CPU utilization: The percentage of CPU time spent in user mode, system mode, nice mode, idle, waiting, hardware interrupts, and software interrupts. High CPU utilization may indicate a CPU-bound process or application.
- Memory usage: The total, free, used, and buffered/cached memory. High memory usage may indicate a memory-intensive process or a potential memory leak.
- Swap usage: The total, free, and used swap space. Excessive swap usage may indicate a lack of physical memory.
- Process list: The list of the most resource-intensive processes, sorted by CPU or memory usage. Identify any processes that are consuming a significant amount of system resources.
To further investigate a specific process, you can use the following options:
k: Kill a processr: Renice a process (change its priority)f: Manage the columns displayedo: Customize the sort order
For example, to view the top 5 processes sorted by CPU usage:
$ top -o %CPU -n 5
Example output:
top - 14:36:37 up 1 day, 12:48, 0 users, load average: 0.00, 0.00, 0.00
Tasks: 101 total, 1 running, 99 sleeping, 1 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1992.0 total, 1537.0 free, 235.0 used, 220.0 buff/cache
MiB Swap: 2047.9 total, 2047.9 free, 0.0 used. 1555.3 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 8788 5688 3828 S 0.0 0.3 0:02.14 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
3 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp
5 root 20 0 0 0 0 I 0.0 0.0 0:00.00 slub_flushwq
This will help you identify any processes that are consuming a significant amount of CPU resources.
To exit the top command, press q.
Summary
In this lab, we first learned about the top command, a powerful tool for monitoring system performance and resource utilization in Linux. We explored the various system information displayed by the top command, including uptime, task statistics, CPU and memory usage, and a list of the most resource-intensive processes. We also discovered how to customize the top command's output by using different keys, such as toggling between per-CPU and aggregate CPU utilization, managing the displayed columns, and customizing the sort order of the process list.



