Using top

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,525
Reaction score
3,287
Credits
31,524

Using top in Linux​

The top command in Linux is a powerful tool for monitoring system performance and managing processes. It provides a real-time view of the system's resource usage, including CPU, memory, and process information.

Starting and Stopping top​

To start top, simply open a terminal and type:
Code:
 top

To stop top, press:
Code:
 q

Understanding Process States​

  • Running: These processes are actively using the CPU.
  • Sleeping: These processes are not currently using the CPU but are waiting for an event (e.g., I/O operations).
  • Stopped: These processes have been stopped, usually by receiving a signal.
  • Zombie: These are processes that have completed execution but still have an entry in the process table. They occur when the parent process hasn't read the exit status of the terminated process.

User Processes vs. System Processes​

  • User Processes: These are processes initiated by users. They typically run in user space and have lower priority compared to system processes.
  • System Processes: These are processes initiated by the system, often running in kernel space. They handle core system functions and usually have higher priority.

Columns in top​

  • PID: Process ID, a unique identifier for each process.
  • USER: The user who owns the process.
  • PR: Priority of the process.
  • NI: Nice value, which affects the priority.
  • VIRT: Virtual memory used by the process.
  • RES: Resident memory (physical memory) used by the process.
  • SHR: Shared memory used by the process.
  • S: Process state (e.g., R for running, S for sleeping).
  • %CPU: Percentage of CPU usage.
  • %MEM: Percentage of memory usage.
  • TIME+: Total CPU time the process has used since it started.
  • COMMAND: The command that started the process.

How CPU Usage Can Exceed 100% on Multicore Systems​

On multicore CPUs, the top command can show a process exceeding 100% CPU usage because it reports CPU usage relative to the total number of cores available.

How CPU Usage is Calculated​

Each core in a multicore CPU can be considered as having 100% capacity. So, if you have a quad-core CPU, the total capacity is 400%. Here's how it works:

  • Single-Core CPU: The maximum CPU usage for any process is 100%.
  • Dual-Core CPU: The maximum CPU usage for any process is 200%.
  • Quad-Core CPU: The maximum CPU usage for any process is 400%.

Example​

If you see a process using 150% CPU on a quad-core system, it means the process is using 1.5 cores' worth of CPU time. This could be one core fully utilized and another core half utilized, or any combination that totals to 150%.

Why This Happens​

Modern operating systems and applications are designed to take advantage of multiple cores. When a process is multithreaded, it can run multiple threads in parallel across different cores. This parallel execution allows the process to use more than 100% of a single core's capacity, effectively distributing its workload across multiple cores.

Visualizing in top​

In top, the CPU usage column (%CPU) shows the percentage of total CPU capacity being used by each process. On a multicore system, this can exceed 100% because it's summing up the usage across all cores.

Summary​

  • Single-Core: Max 100% per process.
  • Dual-Core: Max 200% per process.
  • Quad-Core: Max 400% per process.

Conclusion​

The top command is an invaluable tool for system administrators and users who need to monitor and manage system performance. By understanding the various process states, the difference between user and system processes, and the meaning of each column in top, you can effectively use this command to keep your system running smoothly. Additionally, understanding how CPU usage can exceed 100% on multicore systems helps in better interpreting the data provided by top.
 


TOP is a great tool, I can't say anything else. It usually provides a good insight on things going wrong, in such cases.

By default, the processes are sorted on CPU usage (descending), but if you press

M

it will processes sorted on memory usage.
When I do that I see all Java processes of the server :)


Also VERY interesting is sorting the processes on CPU TIME collected by any process. It is NOT the same as sorted by current CPU usage, because this one will give you an idea which process is actually using CPU, but on a long term basis. You can find bad processes this way, the worst one you can compare the CPU time with the uptime of a server, and if that relation is high, you know something is wrong. Now, every server must have one process at the top, but the thing is : what is the number displayed with it ?
 
Some other alternatives similar to top.

atop
btop
htop
glances

Some of these are more colorful and more visually attractive, some of these are more configurable than top.
On our servers, we typically use glances.
 
Isn't GLANCE a tool by HP ?

Yes, I'm pretty sure that was the original, glances is just a copy.
I haven't met many HPUX guys on here.

My understanding is that glance was written in C, glances is python,
while the output looks similar, the development teams are not connected in any way.
 
Yes, I'm pretty sure that was the original, glances is just a copy.
I haven't met many HPUX guys on here.

My understanding is that glance was written in C, glances is python,
while the output looks similar, the development teams are not connected in any way.

I guess HP UX is dead by now, it's more than 1 decade ago I last used one, I think.
But I remember the tool looked very neat.
 
More alternatives

Bottom

ps and pstree

And for networking

Iftop

Iotop

Vektor
 
Using the 'f' command in top allows setting the content and ordering of each of one of four different possible windows being displayed while top is running. It is also possible to set the color scheme for top. Using the 'W' command will write your current configuration to a file named ~/.config/procps/toprc. You can tell top to use Irix or Solaris mode to solve that CPU usage issue. I personally use Solaris mode so when one CPU core is fully utilized it will show 25% when the program does not use multiple cores since I have a quad-core CPU. Irix mode would show 100% CPU usage when a program used all of the CPU resources for a single core and up to 400% if a program was capable of using all four cores at once. Use the 'I' command to toggle between Irix and Solaris modes in top. I have top running all the time on my system since I use multiple virtual desktops in KDE and I use two different windows. I sort based on CPU usage for the first window named Pri and I sort based on "USED" memory usage in the third window named Mem.

Signed,

Matthew Campbell
 
I'd forgotten I have this installed...nice Tutorial.
1735860344141.gif
 


Members online


Top