How to use Linux commands to view CPU usage

  

In Linux system operation, sometimes you need to check the usage of cpu, which requires the use of Linux commands. There are four commands for viewing cpu usage in Linux, namely top. Vmstat, mpstat and prstat commands, let's get to know it.

1.top

Access: All users

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

Description: Instantly display the dynamics of the process

d : Change the update speed of the display, or in the interactive command line (interactive command) Press s

q : There is no delay in display speed. If the user has superuser permission, then top will execute in the highest priority order.

c : Switch display mode, there are two One mode 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 accumulate the CPU time of the dead child process that has been completed or disappeared

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

i : Do not display any idle or zombie trips

n : number of updates After completion, it will exit top

b : batch file mode, with “n” Used together, can be used to output the results of top to the file

Example:

Exit after 10 updates are displayed;

top -n 10

Users will not be able to use the chat command to command the trip:

top -s

Enter the result of the update display twice into the file named top.log:

top -n 2 -b " top.log

Attach a command to introduce linux traceroutewindows tracert two commands are equivalent, tracking network routing

2.vmstat

As we have discussed before, the performance comparison of any system is based on the baseline, and monitoring the performance of the CPU is the above three points, run queue, CPU usage and context switching. The following are some of the most 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, do not exceed 6;

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

a) User Time: 65%~70%

b) System Time: 30%~35%

c) Idle: 0% to 5%

3. mpstat

For context switching to be combined with CPU usage, if the CPU usage satisfies the above distribution, a large number of context switches are acceptable. .

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

# vmstat 1

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

rb 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 indicates the size of the run queue,

b indicates that due to IO wait The number of threads in the block,

in indicates the number of interrupts,

cs indicates the number of context switches,

us indicates the user CPU time,

sys System CPU time,

wa indicates the time when the CPU is in the idle state due to IO wait,

id indicates the total time the CPU is in the 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 3 device numbers 15,169 and 185. The relationship between device name and device number can refer to the file /proc/interrupts, where 185 stands for network card eth1.
One page 12 next page total 2 pages

Copyright © Windows knowledge All Rights Reserved