Linux学习整理-系统资源监视

1. top

top命令给一个正在运行的系统提供一个动态实时的视图,它会显示系统的进程和线程的摘要信息。主要用来实时监视CPU,内存。
Usage:
top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]

1-1. top命令

top
top - 23:11:31 up 13:22,  1 user,  load average: 0.00, 0.01, 0.05
Tasks: 109 total,   1 running, 108 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.2 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :   995692 total,   262992 free,   280276 used,   452424 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   563732 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                     
     9 root      20   0       0      0      0 S   0.3  0.0   0:01.50 rcu_sch

1-2. top视图的阅读方法

1-2-1. summary area

第一行
显示系统的时间,用户数,平均负载

top - 23:11:31 up 13:22,  1 user,  load average: 0.00, 0.01, 0.05
系统时间和系统启动多长时间    用户数    平均负载       1分,  5分,  15

第二行
显示系统的进程数

Tasks: 109 total,   1 running, 108 sleeping,   0 stopped,   0 zombie
总数             , 运行中进程数, 睡眠进程数   ,  停止进程数 , 僵尸进程数

第三行
显示CPU使用量

%Cpu(s):  0.2 us,  0.2 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

下面是每个列的详细解释,其中有个niced和un-niced,都属于用户层的进程。
简单来说un-niced 就是咱们正常运行命令行,产生的进程。
niced是用nice或者renice命令来运行命令行,产生的进程。
niced有个值的区间,从-20 到 19, -20 是优先值最高的。

us, user    : time running un-niced user processes
sy, system  : time running kernel processes
ni, nice    : time running niced user processes
id, idle    : time spent in the kernel idle handler
wa, IO-wait : time waiting for I/O completion
hi : time spent servicing hardware interrupts
si : time spent servicing software interrupts
st : time stolen from this vm by the hypervisor

第4,5行
显示内存和swap的使用量

KiB Mem :   995692 total,   262992 free,   280276 used,   452424 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   563732 avail Mem 

1-2-2. message area

这个只有在top界面,执行特定命令才显示,比如:g

Choose field group (1 - 4)

1-2-3. column heads area

这里显示每个进程任务的详细内容

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                     
     9 root      20   0       0      0      0 S   0.3  0.0   0:01.50 rcu_sch

1-3. top视图的操作方法

特指top视图界面的操作方法

1-3-1. h

按下h或者?显示帮助top视图的各种交互命令的帮助
按下q可以退出帮助界面

Help for Interactive Commands - procps-ng version 3.3.10
Window 1:Def: Cumulative mode Off.  System: Delay 3.0 secs; Secure mode Off.

  Z,B,E,e   Global: 'Z' colors; 'B' bold; 'E'/'e' summary/task memory scale
  l,t,m     Toggle Summary: 'l' load avg; 't' task/cpu stats; 'm' memory info
  0,1,2,3,I Toggle: '0' zeros; '1/2/3' cpus or numa node views; 'I' Irix mode
  f,F,X     Fields: 'f'/'F' add/remove/order/sort; 'X' increase fixed-width

  L,&,<,> . Locate: 'L'/'&' find/again; Move sort column: '<'/'>' left/right
  R,H,V,J . Toggle: 'R' Sort; 'H' Threads; 'V' Forest view; 'J' Num justify
  c,i,S,j . Toggle: 'c' Cmd name/line; 'i' Idle; 'S' Time; 'j' Str justify
  x,y     . Toggle highlights: 'x' sort field; 'y' running tasks
  z,b     . Toggle: 'z' color/mono; 'b' bold/reverse (only if 'x' or 'y')
  u,U,o,O . Filter by: 'u'/'U' effective/any user; 'o'/'O' other criteria
  n,#,^O  . Set: 'n'/'#' max tasks displayed; Show: Ctrl+'O' other filter(s)
  C,...   . Toggle scroll coordinates msg for: up,down,left,right,home,end

  k,r       Manipulate tasks: 'k' kill; 'r' renice
  d or s    Set update interval
  W,Y       Write configuration file 'W'; Inspect other output 'Y'
  q         Quit
          ( commands shown with '.' require a visible task display window ) 
Press 'h' or '?' for help with Windows,
Type 'q' or <Esc> to continue 

1-3-2. q

按下q退出top界面,当然也可以用Ctrl+c来退出。

1-3-3. l ,t,m

  • l

控制summary area的第一行显示状态,可以循环按下,可以看到下面这一行的显示和不显示

top - 23:11:31 up 13:22,  1 user,  load average: 0.00, 0.01, 0.05
  • t

控制summary area的第二,三行显示状态,可以循环按下,可以看到显示内容的变化

# 初始显示状态
Tasks: 110 total,   1 running, 109 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.0 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

# 显示线状
Tasks: 110 total,   2 running, 108 sleeping,   0 stopped,   0 zombie
%Cpu(s):   0.2/0.0     0[|||||||||||||||||||||||||||||||||||||||||||||                                                                ]

# 显示柱状(copy出来看不到效果。。)
Tasks: 110 total,   2 running, 108 sleeping,   0 stopped,   0 zombie
%Cpu(s):   0.0/0.9     1[                                                                                                                     ]

#不显示
  • m

控制summary area的第四,五行显示状态,可以循环按下,可以看到显示内容的变化

# 初始显示状态
KiB Mem :   995692 total,   249900 free,   283656 used,   462136 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   553764 avail Mem 

# 显示线状
KiB Mem : 44.4/995692   [||||||||||||||||||||||||||||||||||||||||||||                                                        ]
KiB Swap:  0.0/2097148  [                                                                                                    ]

# 显示柱状(copy出来看不到效果。。)
KiB Mem : 44.4/995692   [                                                                                                    ]
KiB Swap:  0.0/2097148  [                                                                                                    ]

#不显示

1-3-4. x,y

  • x

把排序列变成高亮

排序的列高亮

  • y

把运行中的任务变成高亮
运行中的任务高亮

1-3-5. E,e

  • E

summary area的内存显示单位变化。可以循环按下

KiB Mem :   995692 total,   249636 free,   283908 used,   462148 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.   553512 avail Mem 

MiB Mem :    972.4 total,    243.8 free,    277.3 used,    451.3 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.    540.5 avail Mem 

GiB Mem :      0.9 total,      0.2 free,      0.3 used,      0.4 buff/cache
GiB Swap:      2.0 total,      2.0 free,      0.0 used.      0.5 avail Mem 

TiB Mem :      0.0 total,      0.0 free,      0.0 used,      0.0 buff/cache
TiB Swap:      0.0 total,      0.0 free,      0.0 used.      0.0 avail Mem 

PiB Mem :      0.0 total,      0.0 free,      0.0 used,      0.0 buff/cache
PiB Swap:      0.0 total,      0.0 free,      0.0 used.      0.0 avail Mem

EiB Mem :      0.0 total,      0.0 free,      0.0 used,      0.0 buff/cache
EiB Swap:      0.0 total,      0.0 free,      0.0 used.      0.0 avail Mem 

  • e

columns area的内存显示单位变化。可以循环按下

1-3-6. z,b

  • z

颜色显示变化
在这里插入图片描述

  • b

x或者是y按下以后,高亮的部分变粗
如果不是高亮状态,按下b键,没啥反应
在这里插入图片描述

1-3-7. f

追加或者删除columns area显示列,更改排序列
用上下左右选择列,然后用空格键来控制列的表示非表示
用s键来控制排序列

Fields Management for window 1:Def, whose current sort field is PID
   Navigate with Up/Dn, Right selects for move then <Enter> or Left commits,
   'd' or <Space> toggles display, 's' sets sort.  Use 'q' or <Esc> to end!

* PID     = Process Id             Flags   = Task Flags <sched.h>
* USER    = Effective User Name    CGROUPS = Control Groups      
* PR      = Priority               SUPGIDS = Supp Groups IDs     
* NI      = Nice Value             SUPGRPS = Supp Groups Names   
* VIRT    = Virtual Image (KiB)    TGID    = Thread Group Id     
* RES     = Resident Size (KiB)    ENVIRON = Environment vars    
* SHR     = Shared Memory (KiB)    vMj     = Major Faults delta  
* S       = Process Status         vMn     = Minor Faults delta  
* %CPU    = CPU Usage              USED    = Res+Swap Size (KiB) 
* %MEM    = Memory Usage (RES)     nsIPC   = IPC namespace Inode 
* TIME+   = CPU Time, hundredths   nsMNT   = MNT namespace Inode 
* COMMAND = Command Name/Line      nsNET   = NET namespace Inode 
  PPID    = Parent Process pid     nsPID   = PID namespace Inode 
  UID     = Effective User Id      nsUSER  = USER namespace Inode
  RUID    = Real User Id           nsUTS   = UTS namespace Inode 
  RUSER   = Real User Name      
  SUID    = Saved User Id       
  SUSER   = Saved User Name     
  GID     = Group Id            
  GROUP   = Group Name          
  PGRP    = Process Group Id    
  TTY     = Controlling Tty     
  TPGID   = Tty Process Grp Id  
  SID     = Session Id          
  nTH     = Number of Threads   
  P       = Last Used Cpu (SMP) 
  TIME    = CPU Time            
  SWAP    = Swapped Size (KiB)  
  CODE    = Code Size (KiB)     
  DATA    = Data+Stack (KiB)    
  nMaj    = Major Page Faults   
  nMin    = Minor Page Faults   
  nDRT    = Dirty Pages Count   
  WCHAN   = Sleeping in Function

1-3-8. Z(shift+z)

控制各个区域的颜色设置

Help for color mapping - procps-ng version 3.3.10
current window: 1:Def

   color - 04:25:44 up 8 days, 50 min,  7 users,  load average:
   Tasks:  64 total,   2 running,  62 sleeping,   0 stopped,
   %Cpu(s):  76.5 user,  11.2 system,   0.0 nice,  12.3 idle
    Nasty Message!   -or-  Input Prompt
     PID TTY     PR  NI %CPU    TIME+   VIRT SWAP S COMMAND    
   17284 pts/2    8   0  0.0   0:00.75  1380    0 S /bin/bash   
    8601 pts/1    7 -10  0.4   0:00.03   916    0 R color -b -z
   11005 ?        9   0  0.0   0:02.50  2852 1008 S amor -sessi
   available toggles: B =disable bold globally (Off),
       z =color/mono (On), b =tasks "bold"/reverse (On)

1) Select a target as an upper case letter, current target is  T :
   S = Summary Data,  M = Messages/Prompts,
   H = Column Heads,  T = Task Information
2) Select a color as a number, current color is  1 :
   0 = black,  1 = red,      2 = green,  3 = yellow,
   4 = blue,   5 = magenta,  6 = cyan,   7 = white

3) Then use these keys when finished:
   'q' to abort changes to window '1:Def'
   'a' or 'w' to commit & change another, <Enter> to commit and end 

1 先用S,H,M,T来选定要改变颜色的区域
2 再用0-7,来改变颜色
3 按下回车键反映到top界面

在这里插入图片描述

1-3-9. W(shift+w)

把上述的各种设置写入配置文件,这样下次打开top会按照配置文件的设置,来显示top界面

2. sar

收集,报告,保存系统资源的使用信息。
可以看过去的资源使用信息

2-1. 用法

sar <属性> [间隔时间] [表示回数]

2-2. 属性

属性名说明
-A表示全部的情报
-B表示分页的统计情报
-b表示I/O和传输速率的统计情报
-d表示每个块设备的统计情报
-n表示网络的统计情报
-u表示CPU的统计情报
-r表示内存的统计情报
-S表示swap分区的统计情报
-q表示系统负载的统计情报
-s [ hh:mm[:ss] ]开始时间
-e [ hh:mm[:ss] ]结束时间

2-3. 使用例

#CPU使用量
[root@centos ~]# sar -P ALL
Linux 3.10.0-1160.49.1.el7.x86_64 (centos) 	04/19/2022 	_x86_64_	(2 CPU)

12:00:01 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
12:10:01 AM     all      0.01      0.00      0.05      0.00      0.00     99.94
12:10:01 AM       0      0.01      0.00      0.06      0.00      0.00     99.93
12:10:01 AM       1      0.02      0.00      0.03      0.00      0.00     99.95

# 查看负载
[root@centos ~]# sar -q
Linux 3.10.0-1160.49.1.el7.x86_64 (centos) 	04/19/2022 	_x86_64_	(2 CPU)

12:00:01 AM   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
12:10:01 AM         0       145      0.00      0.01      0.05         0
12:20:01 AM         0       145      0.00      0.01      0.05         0
12:30:01 AM         0       144      0.00      0.01      0.05         0
12:40:01 AM         0       144      0.02      0.02      0.05         0
12:50:01 AM         0       144      0.07      0.05      0.05         0
01:00:01 AM         0       145      0.00      0.01      0.05         0
01:10:01 AM         1       144      0.00      0.01      0.05         0
01:20:01 AM         0       144      0.03      0.04      0.05         0
01:30:01 AM         0       144      0.00      0.01      0.05         0
01:40:01 AM         0       144      0.02      0.03      0.05         0
01:50:01 AM         0       142      0.11      0.04      0.05         0
02:00:01 AM         0       143      0.00      0.01      0.05         0
02:10:01 AM         0       143      0.00      0.01      0.05         0
02:20:01 AM         0       141      0.00      0.01      0.05         0
02:30:01 AM         0       141      0.00      0.02      0.05         0
Average:            0       144      0.02      0.02      0.05         0

http://sebastien.godard.pagesperso-orange.fr/man_sar.html

3. vmstat

打印虚拟内存的统计信息

3-1. 用法

vmstat <属性> [间隔时间] [表示回数]

3-2. 使用例

# 每隔两秒打印一次,总共打印5次。
[root@centos ~]# vmstat 2 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 252644   2108 460044    0    0     1     0   10   16  0  0 100  0  0
 0  0      0 252652   2108 460044    0    0     0     0   24   34  0  0 100  0  0
 0  0      0 252652   2108 460044    0    0     0     0   27   38  0  0 100  0  0
 1  0      0 252652   2108 460044    0    0     0     0   21   31  0  0 100  0  0
 0  0      0 252652   2108 460044    0    0     0     0   22   35  0  0 100  0  0

在这里插入图片描述

# 显示硬盘的统计信息
[root@centos ~]# vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
sda     6175      3  829857   48853  14898   1316  235128   18630      0     30
sr0        0      0       0       0      0      0       0       0      0      0
dm-0    4202      0  805760   50110  16210      0  231032   20147      0     30
dm-1      88      0    4408     113      0      0       0       0      0      0

4. free

打印内存的统计信息
常用命令free -h

[root@centos ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           972M        274M        246M         13M        451M        543M
Swap:          2.0G          0B        2.0G

5. dstat

强大的资源监视工具,可以监视内存,cpu,硬盘,IO,网络等等。
需要安装
yum install -y dstat

[root@centos ~]# dstat 2 5
You did not select any stats, using -cdngy by default.
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read  writ| recv  send|  in   out | int   csw 
  0   0 100   0   0   0|2486B 1159B|   0     0 |   0     0 |  20    33 
  0   0 100   0   0   0|   0     0 |  60B  578B|   0     0 |  33    38 
  0   0 100   0   0   0|   0     0 |  60B  330B|   0     0 |  26    34 
  0   0  99   0   0   0|   0     0 |  60B  330B|   0     0 |  26    36 
  0   0 100   0   0   0|   0     0 |  60B  330B|   0     0 |  21    27 
  0   0 100   0   0   0|   0     0 |  60B  330B|   0     0 |  21    27 

6. iostat

打印设备、分区和网络文件系统的 CPU 统计信息和输入/输出统计信息

6-1. 用法

iostat <属性> [间隔时间] [表示回数]

7. 参考文档

资源管理SYSSTAT

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值