How to use Linux commands to check CPU usage

Time: 2017-05-04Source: Huajun InformationAuthor: monster

In Linux system operation, sometimes you need to check the CPU usage, which requires the use of Linux commands. There are four main commands to check the CPU usage in Linux, namely top, vmstat, mpstat and prstat commands. The top command is under Linux. Commonly used performance analysis tools can display the resource usage of each process in the system in real time, similar to the Windows Task Manager. Let’s learn about it together.

53-1412051I602C8.jpg

1.top

Access: All users

Usage: top [-] [d delay] [q] [c] [S] [s] [i] [n] [b]

Description: Real-time display of process dynamics

d: Change the display update speed, or press s in the interactive command bar

q: Display speed without any delay. If the user has superuser permissions, top will be executed with the highest priority.

c: Switch the display mode. There are two modes. One is to display only the name of the executable file, and the other is to display the complete path and name. S: Accumulation mode, which will display the completed or disappeared child process (dead child process). CPU time accumulates

s: Safe mode, cancel conversational commands to avoid potential crises

i: Do not display any idle or zombie trips

n: The number of updates, top will exit after completion

b: Batch file mode, used with the "n" parameter, can be used to output the top results to the file

example:

Exit after displaying updates ten times;

top -n 10

Users will no longer be able to use conversational commands to order trips:

top -s

Enter the results of updating and displaying twice into the file named top.log:

top -n 2 -b " top.log

Attached is a command introduction: linux traceroutewindows tracert. The two commands are equivalent to trace network routes.

2.vmstat

As we discussed before any system performance comparison is based on baseline and monitoring CPU performance is the above 3 points, run queue, CPU usage and context switching. The following are some common performance requirements for CPUs:

1. For each CPU, the run queue should not exceed 3. For example, if it is a dual-core CPU, it should not exceed 6;

2. If the CPU is running at full load, it should comply with the following distribution,

a) User Time: 65%~70%

b) System Time: 30%~35%

c) Idle: 0%~5%

3.mpstat

Context switching should be considered in conjunction with CPU usage. If the CPU usage meets the above distribution, a large number of context switches are also acceptable.

Commonly used monitoring tools include: vmstat, top, dstat and mpstat.

# vmstat 1

procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu- ---

r b swpd free buff cache si so bi bo in cs us sy id wa

0 0 104300 16800 95328 72200 0 0 5 26 7 14 4 1 95 0

0 0 104300 16800 95328 72200 0 0 0 24 1021 64 1 1 98 0

0 0 104300 16800 95328 72200 0 0 0 0 1009 59 1 1 98 0

r represents the size of the run queue,

b represents the number of threads blocked due to IO waiting,

in represents the number of interrupts,

cs represents the number of context switches,

us represents user CPU time,

sys represents system CPU time,

wa represents the time when the CPU is in idle state due to IO waiting.

id represents the total time the CPU is in idle state.

dstat can give the number of interrupts generated by each device:

# dstat -cip 1

----total-cpu-usage---- ----interrupts--- ---procs---

usr sys idl wai hiq siq| 15 169 185 |run blk new

6 1 91 2 0 0 | 12 0 13 | 0 0 0

1 0 99 0 0 0 | 0 0 6 | 0 0 0

0 0 100 0 0 0 | 18 0 2 | 0 0 0

0 0 100 0 0 0 | 0 0 3 | 0 0 0

We can see that there are three device numbers 15, 169 and 185. We can refer to the file /proc/interrupts for the relationship between device names and device numbers. Here 185 represents the network card eth1.

The above is the method summarized by the editor of Huajun. I hope it can help everyone. Learn more at Huajun Software Park!

Related articles更多>>

Popular recommendations