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

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

[复制链接]
gaosmile 发布时间:2020-11-9 20:36
目录
  • 前言
  • top命令
  • htop命令
  • ps命令
  • free命令
  • vmstat命令
  • iosat命令
  • netstat命令
  • df命令
  • du命令
    2 J  H& R6 M- Y' C5 o& t7 B' g
前言

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

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

top命令

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

命令:

# 运行top" X' ^0 t) }) o( D: y
top1 w) ]1 E! i+ w% c; J; p. @

; Q2 E; d9 [9 j: k' l5 c# 显示进程的完整命令3 m4 f3 k2 I- q  U4 k. C
top -c
) Y& j1 u+ I( x$ h7 d- M

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

htop命令

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

命令:

# 运行htop; q( g0 g+ g9 s0 \! E3 m7 |# Y
htop
) x# S- K1 Z" O* L  m

概要信息说明:

  • 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越忙:% ^/ g' T- R, d" T) d7 m
    • 负载低于0.7 * CPU核数:CPU使用率正常;(道路通畅)
    • 负载高于0.7 * CPU核数:CPU使用率较高,需要关注;(道路堵车,需要交警到场疏导交通)
    • 负载高于1 * CPU核数:CPU使用率过高,需要关注;(道路严重堵车,可能发生了交通事故,需要交警马上到现场处理)
    • 负载高于5 * CPU核数:系统超负荷运转,无法正常响应;(交通瘫痪,可能道路已经被水淹无法通行,需要投入抢险力量恢复交通)2 S! L, R7 i* y
  • 系统已连续运行时间:Uptime后面为系统从上次启动后,已连续运行时间,可以倒推系统上次启动时间;
    9 S1 u( Y. T8 ~. I1 H

详细信息说明:

  • 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:进程的执行命令;
    6 R% e( m7 b/ X, x% W. }

常用快捷键:

  • 上下箭头:上下滚动查看进程;
  • u:显示指定用户的进程;
  • P:按照进程使用的CPU时间百分比排序;
  • M:按照进程使用的内存百分比排序;
  • T:按照进程已连续运行时间排序;
    0 b& U$ Q3 g6 N1 T$ _: r. g# n3 G

参考:

  • https://linuxtogether.org/htop-command-explanation/
  • https://www.softprayog.in/tutorials/htop-command-in-linux
  • https://scoutapm.com/blog/understanding-load-averages
  • htop官网$ F4 }3 `+ S& l1 f  F
ps命令# 查看java进程
6 w5 l7 A. E5 w# e(every) 所有进程
! ~+ B4 j1 z3 O$ U% Z% F  z- A; Q# f(full format) 完整输出格式. y+ Q; i: u7 c9 }# }' ]/ Y
ps -ef | grep nginx+ }9 d( T9 P* h9 o

7 }4 y- a& M6 G7 W. h# 不显示grep本身
1 i& B( u3 m/ D8 O& c. Aps -ef | grep nginx | grep -v grep& }1 N: a/ C3 V

/ j0 t1 t% e) v# 获取指定进程的PID) l6 ~4 ^) L3 r
ps -ef | grep java | grep jenkins | grep -v grep | awk '{print $2}'
' U* @8 n0 Z; |# H# t8 |6 t9 e5 B2 ?4 Q
# 根据进程的PID中止进程
" s' A9 y6 y, G+ q- r1 v" Cps -ef | grep java | grep jenkins | grep -v grep | awk '{print $2}' | xargs kill -94 P" h2 [7 A8 V1 O3 U) u  w1 G
( ]& [* J2 f1 v6 M+ l# |
# 查看进程堆栈
" l* w, Y  j- w* w# p(path) 显示完整路径
) p8 w/ M. S4 j. Q- ^- }; D! C" [. {pmap <pid> -p: g3 T4 ^; e* X4 E

1 N" [  \; ]3 y+ f4 a  H3 k# 查看9091端口是哪个容器进程  P& e; w# k( ^+ G! m* x0 w: r
docker ps | grep 90910 ~: r$ d, n8 L
& u( C  _! `2 \! I. u9 w

ps -ef格式:

  • UID:进程的UID(用户)
  • PID:进程ID
  • PPID:父进程ID
  • C:CPU使用时间百分比
  • STIME
  • TTY
  • TIME
  • CMD:启动进程的命令
    ' r  G$ [) f9 _2 V% e" F2 {
free命令

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

命令:

# 以适合阅读的形式输出(h: human)
! C3 t& W& j+ t1 y0 lfree -h
9 J7 N; Q% S4 W
" S$ Z& Z9 ]4 o# V# 以MB格式输出(m: MB)
* }. {, ~  f5 nfree -m8 P5 T; x$ `8 [" V  L: I& x: T- ]. O
" R4 J9 x/ r, S% R
# 合计Mem和Swap(t: total)5 Z, U* M* o) C1 r- Q% f
free -h -t) Z: v" m% [/ b5 z0 O2 E& ]; y

2 J1 J  w) D0 e  n" `# 统计3次,每秒1次(c: count)
  k' h+ ]' u6 n$ q% Ufree -h -c 3) ~0 `! N* n) s8 o
" A$ Z- L( O& b! _" K( J( c
# 统计5次,每2秒统计1次(s: second)+ Q# f. \: o% g& r& ~2 Y& S
free -h -t -c 5 -s 2
3 P) e& I/ d6 x# w" a* X: J

说明:

  • 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.
    % G( ~7 F  d! E8 H) u

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

参考:

  • https://www.howtogeek.com/456943/how-to-use-the-free-command-on-linux/
    & K+ n. p2 @% Q1 g4 M2 u
vmstat命令

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

命令:

# 运行vmstat6 q  S& G$ n9 e" D/ N( S3 X
vmstat
' d$ O  P9 \: M4 Q% G3 k  H( a! d
* y; n' s! H& K9 J3 h! I8 |8 {6 r* H# 每5秒统计1次
. o# n" q( G  b: N- U5 Pvmstat 52 t* m5 V; U5 v' H
4 C% t! Q! f: T$ w" J0 S
# 每5秒统计1次,共统计4次2 F1 D/ N9 G  J( q5 |
vmstat 5 4) s) I+ {7 l( G

, Z- L. c5 s9 Z  w) \: f# 以MB格式输出内存使用情况
) v, }2 L3 x+ D0 F$ x1 W$ t" xvmstat 5 4 -S M( d* Y4 @6 |5 J% J) t5 t
; C5 @% j0 D& b$ c
# 显示统计概要信息
. ?6 K. o7 V, j8 Z0 |  g& S1 A' ]3 e! wvmstat -s- N4 S, X6 W& G& Z9 v

( w! i5 @" O% M3 M" A, \

说明:

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.
    # e; u0 ^0 ~; q0 v0 w+ X& q

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.
    . B2 C( [# s7 u

Swap

  • si: Amount of virtual memory swapped in from swap space.
  • so: Amount of virtual memory swapped out to swap space.
    0 g3 Q5 I! A0 X8 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.. I1 {: ^0 J5 }# \" s* s

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.
    ) c# B- h& l# O9 L$ \

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.1 t# v. O6 b: H2 O5 Z( q

参考:

  • 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/
    2 Y* ^* a3 d6 q! F
iosat命令

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

命令:

# 运行iostat& N3 |" B# a  `3 }
iostat
5 E8 r  |9 h/ O- n
$ f+ l# C& r! w+ y" K8 R3 K5 @# 以MB格式输出" s9 i- ?6 s0 f2 G3 {- M
iostat -m1 r$ v' B, G/ R" k
* C; D; S3 K4 N
# 查看指定磁盘(分区); V3 \1 B0 k3 Y  o& W3 O
iostat -m -p vda
6 R3 g* N( Z* `7 F" N8 x1 `1 H9 X
# 每2秒统计一次,共统计3次
+ {; `. U2 d4 X' yiostat -m 2 33 Q. q0 u- R: W0 i, v0 ~, h( p( E

说明:

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./ f# c7 W8 O; n8 t8 d0 n, b

磁盘信息:

  • 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./ k- F# e; L. ]% ^

参考:

  • 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/
    + f6 w+ V% G& B  V0 c% d
netstat命令

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

命令:

# 显示在侦听的TCP/UDP网络连接,包括程序和端口信息; M3 H( |( c6 i4 b7 {8 u
# u(UDP)
( q$ A! G' y; F- z8 K- i/ U# t(TCP)$ j4 m; w( F0 a7 k" o& O! A, L) }
# n(port)
, u+ P3 I9 C7 I' h# l(listen)" T) X5 |8 |- `! J4 n8 v
# p(program)
* C$ B; z. N6 N1 g& knetstat -utnlp8 T; T- L" Y( p2 E8 `
# Z, Y) o+ @( u8 q' H2 N6 ^
# 只显示在侦听的TCP网络连接,包括程序和端口信息
# d- M1 W, J0 ?3 Y& u- N$ d1 wnetstat -tnlp
6 W' \( b/ w& A) c4 O
# N4 R" U% w% N0 M: ?& t( @1 b3 p# 查看某个端口被哪个进程使用9 s! B# g9 J7 B3 x" A# x
# a(all): p4 a$ J: J1 r0 e* n
netstat -anp | grep 6379
# A5 h4 j8 P! |* D' S3 I
" ~0 y. e. ?! s# 查看某个进程在哪个端口' A+ ~8 Y* K8 S: m3 M% B6 ]
netstat -anp | grep redis7 ~3 A  E  |- G1 Q4 c+ F4 P  n
1 X6 c9 v& U. M/ H. T$ u
# 查看docker启动的进程在哪个端口: q6 t4 F: i- t( C0 g3 c
docker ps | grep jenkins; G( P5 o, T7 I" B1 Q- W! B
df命令

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

命令:

# 查看全部文件系统的磁盘使用情况
; Y5 B5 s# |  H6 X1 ?+ H' Bdf -h, F' b' k9 M( O4 u" B

! d( V+ I) b* [; ^- U" F# 查看指定目录所在的文件系统的磁盘使用情况  [( F7 k+ x8 Q( `
df -h ~
! C% ~- M) e* ~  M, f0 Y5 x4 W! g2 _  Y! P5 \  ^
# 不显示docker文件系统
. ?- p: I+ I0 i0 Zdf -h | grep -v docker* E4 G1 n* g: B3 t) S1 y/ b2 E
du命令

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

命令:

# 统计某个目录大小
) I, e) r$ z* `* E0 W3 U+ |du -sh ~
( H4 z( M* L" a, F( O! [; d
) y& }) T6 e. i1 b# 统计目录下各个子目录和文件的大小7 h" D$ Y1 }. w) M
du -h ~0 r! F/ ^1 l9 R

- b+ D7 d6 H' ^' J+ ~# 统计目录下各个子目录和文件的大小,并显示合计大小- H& `. J1 F7 d
du -ch ~8 B( l, f& h; |& K$ z, W2 f
+ B3 G' o1 }2 M/ p
# 只统计下一级目录大小9 O0 }6 j) g8 R  G4 W  u! c
du -h --max-depth 1
; U1 L$ m8 N5 w: K" v. v5 A8 x
3 o6 }* s0 ?" a3 L$ W/ G1 O# 只统计下一级目录大小,单位为MB,从大到小排序
( c& N3 T; P1 c6 m* Wdu -m --max-depth 1 | sort -rn
* D! Y- N5 O( V7 ^7 y4 g8 C9 a! Z0 w# T1 F. @/ d
# 只统计下一级目录大小,单位为MB,从大到小排序,返回最大的10个文件(目录)
2 W. O# A& s/ m7 i0 odu -m --max-depth 1 | sort -rn | head -11
7 @" }; ?8 c' C) P4 H
赞 收藏 评论0 发布时间:2020-11-9 20:36

举报

0个回答

所属标签

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