Linux常用命令之性能统计

1、常用性能指标

  • cpu 代表算法的高效性
  • mem 代表数据结构的使用合理性
  • net io 等更多指标

2、 统计方法

  • 临时性分析 命令交互
  • 系统性分析 prometheus grafana

3、CPU使用统计

(1) cpu 的关键指标

  • cpu 利用率 进程的 cpu 利用情况
  • load average 系统负载情况
  • ps 命令的 cpu 是平均 cpu 利用率,不适合做性能分析

(2)cpu 信息

  • 命令:cat /proc/cpuinfo
cat /proc/cpuinfo
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 79
model name  : Intel(R) Xeon(R) CPU E5-2682 v4 @ 2.50GHz
microcode   : 0x1
cpu MHz     : 2499.994
cache size  : 40960 KB
physical id : 0
siblings    : 2
core id     : 0
cpu cores   : 1
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical, 48 bits virtual
power management:
processor   : 1
vendor_id   : GenuineIntel

(3) 系统负载与进程 cpu 占用 top

  • 命令:top
top - 17:27:57 up 76 days, 18:17,  1 user,  load average: 1.03, 1.11, 1.15
Tasks: 215 total,   2 running, 131 sleeping,   0 stopped,   0 zombie
%Cpu(s): 28.7 us,  0.4 sy,  0.0 ni, 70.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16122020 total,  1343832 free,  4715324 used, 10062864 buff/cache
KiB Swap:        0 total,        0 free,        0 used. 11060040 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
23508 yangche+  20   0  668504 114388  30628 R 100.0  0.7   2872:02 /usr/local+
11120 root      10 -10  152468  34444   9120 S  10.0  0.2   2648:51 /usr/local+
11634 yangtie+  20   0 1101696 123748  12648 S   2.0  0.8  53:58.88 /home/yang+
30939 yangtie+  20   0 1525284  95928  17472 S   1.3  0.6  27:44.85 /home/yang+
  658 root      20   0  306560 170916 170332 S   0.7  1.1 116:29.63 /usr/lib/s+
 5541 yangtie+  20   0  447548  42472  12632 S   0.7  0.3   8:15.91 /home/yang+
 5713 quanjin+  20   0 7850436   2.2g   8648 S   0.7 14.0 583:19.28 java -Duse+
 7427 yangche+  20   0 7796228 225700   9420 S   0.7  1.4 537:38.47 java -jar +

4、内存(MEM)占用统计

(1) 常用命令

  • 命令:free
free -m
              total        used        free      shared  buff/cache   available
Mem:           3790         367         136        2077        3287        1042
Swap:             0           0           0
  • 命令:man free
1、 total:机器上的内存总量(/proc/meminfo中的MemTotal和SwapTotal)

2、 used:已用内存(以总可用缓冲区缓存计算)

3、 free:未使用的内存(/proc/meminfo中的MemFree和SwapFree)

4、 shared:共享内存,tmpfs(主要)使用的内存(/proc/meminfo中的Shmem,可用,在内核2.6.32上,如果不可用,则显示为零)

5、 buffers:内核缓冲区使用的内存(/proc/meminfo中的缓冲区)

6、 cache:页面缓存和Slab使用的内存(/proc/meminfo中的Cached和Slab)

7、 buff/cache:缓冲区和缓存的总和

8、 available:估计有多少内存可用于启动新应用程序,而无需交换。由于项目正在使用,并非所有可回收的内存板都将被回收
  • 命令:cat /proc/meminfo:/proc/meminfo 保存了更多的内存使用数据
cat /proc/meminfo
MemTotal:        3881920 kB
MemFree:          132204 kB
MemAvailable:    1073044 kB
Buffers:          561564 kB
Cached:          2307572 kB
SwapCached:            0 kB
Active:           795200 kB
Inactive:        2358704 kB
Active(anon):     376576 kB
Inactive(anon):  2035324 kB
Active(file):     418624 kB
Inactive(file):   323380 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
...
  • 命令:ps
  • 进程级别的内存分析,可以筛选想要了解的字段信息
    ps -e -o uid,pid,ppid,pcpu,pmem,rss,vsz,comm --sort -%mem | head -10
UID   PID  PPID %CPU %MEM   RSS    VSZ COMMAND
 6597 12059     1  0.0  1.9 77036 3584588 java
 5729 32380     1  0.0  1.9 74608 3518808 java
    0 29821     1  1.5  0.9 38768 161384 AliYunDun
    0   457     1  0.2  0.9 37816 1053844 CmsGoAgent.linu
    0   340     1  0.0  0.7 27628  62708 systemd-journal
    0 12047     1  0.0  0.4 19264 396524 rsyslogd
    0 28845     1  0.5  0.4 16092 719100 node_exporter
    0 10454     1  0.0  0.2 11408 564716 tuned
  998 10439     1  0.0  0.2  8788 710032 polkitd
  • 命令:top
top - 17:17:49 up 325 days,  5:59,  5 users,  load average: 0.00, 0.01, 0.05
Tasks:  92 total,   1 running,  91 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  3881920 total,   134192 free,   372640 used,  3375088 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  1071332 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
12059 dx42310+  20   0 3584588  77036      0 S   0.3  2.0  35:01.64 java
32380 dx53889   20   0 3518808  74608   1056 S   0.0  1.9  19:20.34 java
29821 root      10 -10  161384  38768   5788 S   0.3  1.0 758:25.24 AliYunDun
  457 root      20   0 1053844  37680   5208 S   0.0  1.0 993:28.66 CmsGoAgent+
  340 root      20   0   62708  27876  27244 S   0.0  0.7 193:01.35 systemd-jo+
12047 root      20   0  396524  19408  15780 S   0.0  0.5  54:24.50 rsyslogd
28845 root      20   0  719100  16160   4376 S   0.0  0.4   1263:32 node_expor+
10454 root      20   0  564716  11408    712 S   0.0  0.3  18:45.31 tuned
10439 polkitd   20   0  710032   8788   1172 S   0.0  0.2   8:42.55 polkitd
17217 root      20   0  155268   5924   4476 S   0.0  0.2   0:00.03 sshd

5、网络连接(Net)统计

  • 查看网络连接
  • 命令:netstat -tlnp:可以打印当前机器上正在被监听的进程信息等,t 参数关注TCP协议,l 参数代表listen状态的,表示监听,n 不解析域名,p 打印出来进程
  • 网络状态
ESTABLISHED:成功连接 The socket has an established connection,已经被建立链接了
SYN_SENT:The socket is actively attempting to establish a connection
SYN_RECV:A connection request has been received from the network.
FIN_WAIT1:The socket is closed, and the connection is shutting down.
FIN_WAIT2:Connection is closed, and the socket is waiting for a shutdown from the remote end
TIME_WAIT:主动关闭 The socket is waiting after close to handle packets still in the network
CLOSE:The socket is not being used
CLOSE_WAIT:被动关闭 The remote end has shut down, waiting for the socket to close.
LISTEN:The socket is listening for incoming connections