你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

Linux查看资源使用情况和性能调优常用的命令

[复制链接]
gaosmile 发布时间:2020-11-9 20:36
目录
  • 前言
  • top命令
  • htop命令
  • ps命令
  • free命令
  • vmstat命令
  • iosat命令
  • netstat命令
  • df命令
  • du命令

    6 W  T8 u. N8 y
前言

一般来说,Linux性能调优涉及CPU、内存和 IO,包括进程、线程、程序堆栈、内存、Swap、CPU调度、内存调度、网络连接和IO读写等。

本文介绍了Linux查看资源使用情况和性能调优常用的命令,包括top、htop、ps、free、vmstat、iostat、netstat、df和du命令。

top命令

top命令用来监控系统的资源使用情况,包括CPU、内存、Swap、进程和线程等。

命令:

# 运行top
1 n2 E! _; Q! Ptop
5 j) F: P6 j- g8 c! i; V  X. E" G0 X1 A( B6 v3 y4 [( t# Y
# 显示进程的完整命令, j2 I! U% N) C1 R% V; K6 F
top -c+ M, W/ ?# m; K. Z0 P& R

推荐使用下面的htop命令来代替top命令。

htop命令

htop命令用来监控系统的资源使用情况,包括CPU、内存、Swap、进程和线程等。相比top命令,htop命令更能更强大,显示也更加直观。

命令:

# 运行htop
, z4 y0 K6 U$ M8 i9 j! U; E: S* b$ Ahtop6 z9 H9 v& D) q- C& P9 K0 P! R

概要信息说明:

  • CPU使用率:数字1、2、...表示CPU有几个核,后面的进度条用不同颜色来表示不同维度的CPU使用率,最后是CPU使用率;
  • 内存使用率:Mem后面的进度条用不同颜色来表示不同维度的内存使用率(绿色:used,蓝色:buffers,橙色:cache),最后是内存使用率(used / total);
  • Swap使用率:Swap后面的进度条显示已用的Swap,最后是Swap使用率(used / total),如果系统关闭了Swap,则进度条为空,Swap使用率为0K/0K;
  • 进程和线程统计:Tasks:进程数,thr:线程数,running:正在运行的进程数;
  • 系统平均负载统计:Load average:前1分钟平均负载、前5分钟平均负载和前15分钟平均负载;负载越低说明CPU越空闲,负载越高说明CPU越忙:
    + [. q9 G8 V. B8 V4 x
    • 负载低于0.7 * CPU核数:CPU使用率正常;(道路通畅)
    • 负载高于0.7 * CPU核数:CPU使用率较高,需要关注;(道路堵车,需要交警到场疏导交通)
    • 负载高于1 * CPU核数:CPU使用率过高,需要关注;(道路严重堵车,可能发生了交通事故,需要交警马上到现场处理)
    • 负载高于5 * CPU核数:系统超负荷运转,无法正常响应;(交通瘫痪,可能道路已经被水淹无法通行,需要投入抢险力量恢复交通)
      / P  H$ I, V7 L8 i! g
  • 系统已连续运行时间:Uptime后面为系统从上次启动后,已连续运行时间,可以倒推系统上次启动时间;' Q: h! N, h( ]) h0 x+ _& e& W

详细信息说明:

  • PID:进程ID;
  • USER:进程所有者;
  • PRI:进程优先级;
  • NI:nice值,负值表示高优先级,正值表示低优先级;
  • VIRT:进程使用的虚拟内存(virtual memory);
  • RES:进程使用的物理内存(physical memory);
  • SHR:进程使用的共享内存(shared memory);
  • S:进程状态,R(Running)正在运行的进程、S(Sleeping)休眠的进程、T/S(Traced/Stopped) 已停止或中止的进程 或 Z(Zommbie)僵尸进程;
  • CPU%:进程使用的CPU时间百分比;
  • MEM%:进程使用的内存百分比;
  • TIME+:进程已连续运行时间;
  • Command:进程的执行命令;
    2 j: T. a! n8 ~( a$ H& ]. o

常用快捷键:

  • 上下箭头:上下滚动查看进程;
  • u:显示指定用户的进程;
  • P:按照进程使用的CPU时间百分比排序;
  • M:按照进程使用的内存百分比排序;
  • T:按照进程已连续运行时间排序;3 y1 Y) [: z* Q& f5 z7 R

参考:

  • https://linuxtogether.org/htop-command-explanation/
  • https://www.softprayog.in/tutorials/htop-command-in-linux
  • https://scoutapm.com/blog/understanding-load-averages
  • htop官网
    ) L+ E5 `  K, ?
ps命令# 查看java进程- c; o0 j: ^# @
# e(every) 所有进程6 {9 N3 C0 a5 J$ t5 w, e( E' t7 @
# f(full format) 完整输出格式0 t" e8 @. `6 [, w
ps -ef | grep nginx' S) ?$ V2 M0 L6 ^

: w0 a9 z- Y  j. k/ L. }! y. m# 不显示grep本身8 o7 K9 K3 P. N3 z. _. w! z
ps -ef | grep nginx | grep -v grep+ D- l3 Z$ ]! t- |7 u6 k3 I( n

4 \8 q4 X; m" z+ r+ ]/ z+ U# 获取指定进程的PID
- e& w% ^/ ?7 O% J# d2 G( ]( Hps -ef | grep java | grep jenkins | grep -v grep | awk '{print $2}'
/ m- }  u% [" a4 q
2 s9 `: Y: _( f% ^8 Z  z  d+ d9 N2 c. W& a# 根据进程的PID中止进程
) X( F! @% Q$ P2 e& [- wps -ef | grep java | grep jenkins | grep -v grep | awk '{print $2}' | xargs kill -9- ?: [8 D+ l# l- G

0 B5 ?: M0 w( k$ R8 p4 N& i4 N# 查看进程堆栈
" w/ E6 {; {1 C) g9 v* ^$ r# p(path) 显示完整路径0 @# \( @% f# C2 e  q( l& @3 x1 E
pmap <pid> -p
& k( s" ~: u, q' c& X
- N7 g2 r% Q$ v/ B4 d( J# 查看9091端口是哪个容器进程# j; ^3 Z3 U6 o* R
docker ps | grep 9091
7 ?) {  t- k4 X7 ]" _
7 X1 U5 S7 |; W# v3 X0 u

ps -ef格式:

  • UID:进程的UID(用户)
  • PID:进程ID
  • PPID:父进程ID
  • C:CPU使用时间百分比
  • STIME
  • TTY
  • TIME
  • CMD:启动进程的命令2 v: b. [, g. k" e3 g$ R/ Z- k
free命令

free命令用来查看内存和Swap的使用情况。

命令:

# 以适合阅读的形式输出(h: human); K: ?6 N3 L0 W: n
free -h
* @/ k* T. J3 }& H! S9 Q
/ \4 ]8 v' O/ S  W! ]# 以MB格式输出(m: MB)
. W1 u8 w1 l' G6 ]  ffree -m
2 O" [1 I# P" `$ Q4 Y0 ]; u9 Q2 q8 G% _! |, n, I3 B' S
# 合计Mem和Swap(t: total)
" k' B+ V" q- cfree -h -t' t& B; p7 E# s/ v5 h

$ l6 P7 t( e* k# 统计3次,每秒1次(c: count)
" h5 f) K* q6 V) S) E; p4 ifree -h -c 3
4 `7 u9 N; H0 K% m9 e0 l) Y: f7 \4 q( h7 {+ h: x
# 统计5次,每2秒统计1次(s: second)
0 I" e' T9 ]' y/ E) N6 B8 R; }free -h -t -c 5 -s 24 b% A: J1 r4 g2 c7 [, C2 t% z1 z' Q

说明:

  • Total: The amount of RAM installed in your system.
  • Used: Equal to Total-(Free+Buffers+Cache).
  • Free: The amount of memory completely unused by anything.
  • Shared: Memory taken by the tmpfs file systems.
  • Buffer: The data structures that are maintained to provide an index for everything stored in Cache.
  • Cache: Data read from the hard drive, modified data waiting to be written back to the hard drive, and other computed values.
  • Available: What’s really free. An estimate of the memory in Free, Buffer, and Cache that could be used to satisfy a memory request.
    ( Y. f# P7 p! x! K" W7 W* h

简单来说,total是系统总内存,used就是系统已用内存,total - used 就是系统可用内存。

参考:

  • https://www.howtogeek.com/456943/how-to-use-the-free-command-on-linux/
    6 x+ v8 S  N" s( _# f$ i7 C
vmstat命令

vmstat命令用来统计进程、内存、Swap、IO和CPU等信息。

命令:

# 运行vmstat$ R0 v7 s8 a8 ?- x' c) h/ }
vmstat
4 K% o$ a+ R+ ~3 l3 Z, a: b% @) t' L
# 每5秒统计1次* J9 b( n/ H- z  U* w7 ^4 ^9 W
vmstat 5
8 s/ ?3 Z: i- m- q/ x
9 e" ?8 l' f  A4 T2 ]. T# 每5秒统计1次,共统计4次
+ W9 ~5 @0 k. ~8 J( c# K% Pvmstat 5 4
; ^1 T& u/ y/ t' Y# M3 A5 i1 D+ A& q( L7 V4 g5 [
# 以MB格式输出内存使用情况
, b( v% k& F# nvmstat 5 4 -S M, k7 [& ~" a7 z0 B/ T, Y  N

6 X# f0 m$ m9 O' J$ Z& m4 ]& s# 显示统计概要信息$ N" m+ B9 l7 f6 L( ?
vmstat -s
5 z) [, F6 q/ l6 a& K3 ]& a+ E
! @1 a0 X/ ]( L3 n4 Q$ p5 v. m

说明:

Proc

  • r: The number of runnable processes. These are processes that have been launched and are either running or are waiting for their next time-sliced burst of CPU cycles.
  • b: The number of processes in uninterruptible sleep. The process isn’t sleeping, it is performing a blocking system call, and it cannot be interrupted until it has completed its current action. Typically the process is a device driver waiting for some resource to come free. Any queued interrupts for that process are handled when the process resumes its usual activity.) L3 C+ G  F- [8 ^1 \, L5 w

Memory

  • swpd: the amount of virtual memory used. In other words, how much memory has been swapped out.,
  • free: the amount of idle (currently unused) memory.
  • buff: the amount of memory used as buffers.
  • cache: the amount of memory used as cache.$ p: Y  Y$ |6 j( g  B0 G9 b/ X

Swap

  • si: Amount of virtual memory swapped in from swap space.
  • so: Amount of virtual memory swapped out to swap space.
    - r2 u8 a; s" r$ x; w# v0 i

IO

  • bi: Blocks received from a block device. The number of data blocks used to swap virtual memory back into RAM.
  • bo: Blocks sent to a block device. The number of data blocks used to swap virtual memory out of RAM and into swap space.
    + J" k7 Q  B8 ^/ _+ w* V2 t

System

  • in: The number of interrupts per second, including the clock.
  • cs: The number of context switches per second. A context switch is when the kernel swaps from system mode processing into user mode processing.
    ) s0 u1 d, a3 z; d. N- U

CPU

These values are all percentages of the total CPU time.

  • us: Time spent running non-kernel code. That is, how much time is spent in user time processing and in nice time processing.
  • sy: Time spent running kernel code.
  • id: Time spent idle.
  • wa: Time spent waiting for input or output.
  • st: Time stolen from a virtual machine. This is the time a virtual machine has to wait for the hypervisor to finish servicing other virtual machines before it can come back and attend to this virtual machine.
    6 F/ e  y7 q& w" W& T7 S) [6 P

参考:

  • https://www.howtogeek.com/424334/how-to-use-the-vmstat-command-on-linux/
  • https://www.tecmint.com/linux-performance-monitoring-with-vmstat-and-iostat-commands/
    1 o6 x( X9 y' C/ C
iosat命令

iostat命令用来统计磁盘IO和CPU等信息。

命令:

# 运行iostat* m* ~0 @, t1 {" n
iostat
% n+ p! f! O0 E+ A6 I5 i
- Z7 H$ N0 Q7 T, ?/ C  w7 z- P# c% j# 以MB格式输出
/ r# g5 _7 S. l7 b8 J: e8 n4 Ciostat -m
7 w7 n6 f# P0 t$ w5 V
9 o, ?7 Z% d& k! i+ t# 查看指定磁盘(分区)
, `3 v* P0 a5 i1 _iostat -m -p vda
, U9 T2 Q& r+ ?/ ?! h
' y. U0 N: B, ~0 D6 m- q# 每2秒统计一次,共统计3次' c) V, }2 v5 N6 M7 y
iostat -m 2 3
$ G$ O8 o; n  C) V. L

说明:

CPU信息:

  • %user : It shows the percentage of CPU being utilization that while executing at the user level.
  • %nice : It shows the percentage of CPU utilization that occurred while executing at the user level with a nice priority.
  • %system : It shows the percentage of CPU utilization that occurred while executing at the system (kernel) level.
  • %iowait : It shows the percentage of the time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.
  • %steal : It shows the percentage of time being spent in involuntary wait by the virtual CPU or CPUs while the hypervisor was servicing by another virtual processor.
  • %idle : It shows the percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.5 Y# m" j, h+ Z4 r

磁盘信息:

  • Device : The device/partition name is listed in /dev directory.
  • tps : The number of transfers per second that were issued to the device. Higher tps means the processor is busier.
  • Blk_read/s : It shows the amount of data read from the device expressed in a number of blocks (kilobytes, megabytes) per second.
  • Blk_wrtn/s : The amount of data written to the device expressed in a number of blocks (kilobytes, megabytes) per second.
  • Blk_read : It shows the total number of blocks read.
  • Blk_wrtn : It shows the total number of blocks written.
    5 c% i+ L9 Y8 x4 U2 s5 b) @

参考:

  • https://www.geeksforgeeks.org/iostat-command-in-linux-with-examples/
  • https://www.techrepublic.com/article/how-to-use-the-linux-iostat-command-to-check-on-your-storage-subsystem/; E* I4 R! b- u$ u% H, m
netstat命令

netstat命令用来统计网络连接和端口等信息。

命令:

# 显示在侦听的TCP/UDP网络连接,包括程序和端口信息: B# }' p, `- ?5 `5 U. N
# u(UDP)
+ J( D$ B1 Z: I. h" B# t(TCP)
! v3 S- J/ N2 m- c- E9 U' J# n(port)5 g9 n: \  W" K& j9 M% T! |6 R
# l(listen)+ {) ^1 S0 h) m- S* x: o1 L
# p(program)6 K  r4 }0 e* L! e9 z. E9 P
netstat -utnlp! l2 G. t0 W9 o: }4 B6 h

1 p5 _1 W* K  z% h+ b, P* m7 i( m# 只显示在侦听的TCP网络连接,包括程序和端口信息+ S4 n5 c; E2 Q2 b8 G, D
netstat -tnlp
0 \4 Y3 b) ~- M( @/ Q% O- n# I) q- |% t  G2 D' @+ @+ Z( Z
# 查看某个端口被哪个进程使用7 ~2 o4 k2 a' }2 }% u: h
# a(all)
3 x2 A9 S' ?; Pnetstat -anp | grep 6379
. O6 k+ Q3 N& o4 T* i8 ~7 ?$ f) h( e3 S; ~9 j
# 查看某个进程在哪个端口
2 a/ `( r" y/ Y: x5 }# anetstat -anp | grep redis, o& l6 q  D0 ?! ~2 h1 g3 P
, c" F: P( ~2 U8 D
# 查看docker启动的进程在哪个端口- ]2 \# _% B8 m4 j/ E
docker ps | grep jenkins: h- ]6 s* C3 P
df命令

df命令用来统计磁盘使用情况。

命令:

# 查看全部文件系统的磁盘使用情况: ~0 q5 f0 a& g1 ~3 v
df -h
3 C8 L% `( f" M: t7 V0 D+ V' I5 p6 b& d0 A- A
# 查看指定目录所在的文件系统的磁盘使用情况
% A1 k7 F* y2 O! S$ I, U  Sdf -h ~
! o* z' P! j, j8 a/ n
0 r) N. h6 g" {. Q: B+ J$ A# 不显示docker文件系统3 j' l& O. ]9 o# v* e8 r
df -h | grep -v docker
* b' R) j$ T" G0 r7 _4 z' k" a
du命令

du命令用来统计指定目录的大小。

命令:

# 统计某个目录大小" u# r# m1 P, J* e
du -sh ~* y# Z, ~) Y" W% r1 [
/ f5 q/ k0 l. s; q0 i2 }
# 统计目录下各个子目录和文件的大小
; t& b. C' _" {( a% Odu -h ~
. d+ o6 b- k. U9 `" w2 W5 a( w2 X# Q. m: ?0 h2 _; T( E" Z3 ?. P
# 统计目录下各个子目录和文件的大小,并显示合计大小9 @  `0 D9 `& m
du -ch ~  f) O* V' s3 U2 T8 k2 w
2 [1 i3 ~+ q2 n( J2 Z4 q2 `; c8 T
# 只统计下一级目录大小; i. p2 |4 n: i$ _  D
du -h --max-depth 11 X& @8 j6 U2 q9 q0 z% j

' q  D4 K: ?% Y- o5 L6 d, t# 只统计下一级目录大小,单位为MB,从大到小排序2 H2 V, d4 [( H: T
du -m --max-depth 1 | sort -rn( w) X' x0 G+ k; j. V

& K8 S8 v# W, G! k, ]# 只统计下一级目录大小,单位为MB,从大到小排序,返回最大的10个文件(目录)
& R4 s0 v7 d" Z9 c- odu -m --max-depth 1 | sort -rn | head -11
+ m. Z/ m% r2 c
赞 收藏 评论0 发布时间:2020-11-9 20:36

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版