jck28-luco-linux常用命令之性能统计

为什么要学习性能统计

  • 性能测试
  • 性能分析

性能统计知识

  • 系统级性能数据分析
  • 进程级别性能数据分析

常用性能指标

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

net io 的不合理使用同样会在 cpu 和 mem 上体现出影响,所以我们今天重点介绍 3 个指标,cpu mem 与 net

统计方法

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

cpu使用统计

cpu 信息 /proc/cpuinfo --cpu基本配置

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

系统负载与进程 cpu 占用 top

[jck287213@shell.ceshiren.com feier]$ top
(系统cpu指标)
top - 21:48:54 up 1074 days, 10:30, 2 users, load average: 0.02, 0.04, 0.05 —负载情况
Tasks: 109 total(任务数), 1 running, 107 sleeping 0 stopped, 1 zombie(任务状态)
%Cpu(s): 0.3 us, 0.7 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 s (cpu使用率)
KiB Mem : 3881920 total, 125132 free, 904120 used, 2852668 buff/cache (内存信息)
KiB Swap: 0 total, 0 free, 0 used. 606464 avail Mem

(进程cpu指标)
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
23931 root 10 -10 151924 27608 4720 S 1.3 0.7 434:45.07 AliYunDun+
10318 root 20 0 0 0 0 S 0.3 0.0 0:00.04 kworker/1+
1 root 20 0 52052 3252 1516 S 0.0 0.1 746:37.36 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:14.78 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 332:41.01 ksoftirqd+
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0+
7 root rt 0 0 0 0 S 0.0 0.0 6:44.95 migration+
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 374:48.97 rcu_sched
12 root rt 0 0 0 0 S 0.0 0.0 6:48.02 migration+
13 root 20 0 0 0 0 S 0.0 0.0 334:37.75 ksoftirqd+
15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1+
17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
18 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
19 root 20 0 0 0 0 S 0.0 0.0 0:31.85 khungtaskd
20 root 0 -20 0 0 0 S 0.0 0.0 0:00.05 writeback
21 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrit+
22 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
23 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kblockd
24 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 md
31 root 20 0 0 0 0 S 0.0 0.0 147:36.92 kswapd0
32 root 25 5 0 0 0 S 0.0 0.0 0:00.00 ksmd
33 root 39 19 0 0 0 S 0.0 0.0 14:30.38 khugepaged
34 root 20 0 0 0 0 S 0.0 0.0 0:01.64 fsnotify_+
35 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 crypto
43 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kthrotld

块引用

cpu 的关键指标

  • cpu 利用率 进程的 cpu 利用情况
  • load average 系统负载情况

ps 命令的 cpu 是平均 cpu 利用率(不准确),因为cpu的累计数据是总体的cpu占用时间/程序的运行时间(算的是累计平均占用率),ps统计的cpu利用率通常是非常低的,这个性能指标没法用,程序运行有时频繁的需要cpu,有时候又不需要,所以更多的使用top命令,ps不适合做性能分析
[jck287213@shell.ceshiren.com feier]$ ps aux | less

一个进程cpu利用率跟内存利用率都存在进程的一个特定的数据文件中去,/proc存储一个进程所有需要的资源
[jck287213@shell.ceshiren.com feier]$ ls /proc/29924/
ls: cannot read symbolic link /proc/29924/cwd: Permission denied
ls: cannot read symbolic link /proc/29924/root: Permission denied
ls: cannot read symbolic link /proc/29924/exe: Permission denied
attr cpuset limits net projid_map stat
autogroup cwd loginuid ns root statm
auxv environ map_files numa_maps sched status
cgroup exe maps oom_adj schedstat syscall
clear_refs fd mem oom_score sessionid task
cmdline fdinfo mountinfo oom_score_adj setgroups timers
comm gid_map mounts pagemap smaps uid_map
coredump_filter io mountstats personality stack wchan

内存(MEM)占用统计

常用命令

  • free
    [jck287213@shell.ceshiren.com feier]$ free
    total used free shared buff/cache available
    Mem: 3881920 903272 137828 2131180 2840820 607488
    Swap: 0 0 0
    [jck287213@shell.ceshiren.com feier]$ free -g ()
    total used free shared buff/cache available
    Mem: 3 0 0 2 2 0
    Swap: 0 0 0
    [jck287213@shell.ceshiren.com feier]$ free -m ()
    total used free shared buff/cache available(开启新进程时使用的内存)
    Mem: 3790 884 132 2081 2774 591
    Swap: 0 0 0

free 输出

  • total Total installed memory (MemTotal and SwapTotal in /proc/meminfo)

  • used Used memory (calculated as total - free - buffers - cache)

  • free Unused memory (MemFree and SwapFree in /proc/meminfo)

  • shared Memory used (mostly) by tmpfs (Shmem in /proc/meminfo, available, on kernels 2.6.32, displayed as zero if not available)

  • buffers Memory used by kernel buffers (Buffers in /proc/meminfo)

  • cache Memory used by the page cache and slabs (Cached and Slab in /proc/meminfo)

  • buff/cache Sum of buffers and cache

  • available Estimation of how much memory is available for starting new applications, without swapping. not all reclaimable memory slabs will be reclaimed due to items being in use

  • ps

  • top
    VIRT:虚拟内存—包括进程使用的库、代码、数据,以及malloc、new分配的堆空间和分配的栈空间等; VIRT = SWAP + RES
    RES: 常驻内存实存—指的是进程当前使用的内存大小,包括使用中的malloc、new分配的堆空间和分配的栈空间,但不包括swap out量;
    SHR:共享内存—除了自身进程的共享内存,也包括其他进程的共享内存
    DATA:数据占用的内存。如果top没有显示,按f键可以显示出来。

meminfo

/proc/meminfo 保存了更多的内存使用数据
[jck287213@shell.ceshiren.com ~]$ cat /proc/meminfo
MemTotal: 3881920 kB
MemFree: 113860 kB
MemAvailable: 602008 kB
Buffers: 105656 kB
Cached: 2685032 kB
SwapCached: 0 kB
Active: 1212916 kB
Inactive: 2358016 kB
Active(anon): 865832 kB
Inactive(anon): 2045648 kB
Active(file): 347084 kB
Inactive(file): 312368 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 0 kB

进程级别的内存分析

ps统计的cpu占用率是不准的,算的是累计平均总用率,但是内存是很准确的,都是进程的快照,内存是实时的
[jck287213@shell.ceshiren.com ~]$ ps -e -o uid,pid,ppid,pcpu,pmem,rss,vsz,comm rt -%mem | head -10
UID PID PPID %CPU %MEM RSS VSZ COMMAND
999 8457 8436 0.0 9.2 360524 1585852 mysqld
9603 21864 1 0.0 3.7 144612 3675820 java
101 22255 22094 0.0 2.1 85148 698484 mysqld
0 20599 1 0.0 1.1 43772 1265196 dockerd
0 457 1 0.2 0.9 37692 1053844 CmsGoAgent.linu
0 23931 1 1.3 0.7 28188 151924 AliYunDunMonito
0 22363 22042 0.0 0.5 20504 373976 apache2
0 21297 1 0.0 0.4 18132 1102672 containerd
8490 12410 1 0.1 0.3 14912 718900 node_exporter

top 命令

了解总的内存大小,以及整个有没有可用内存,然后是进程申请内存有无受到限制,申请了多少内存,内存使用是否合理,有无内存泄漏

网络连接(Net)统计

查看网络连接

[jck287213@shell.ceshiren.com ~]$ netstat -tnp | head -10
(No info could be read for “-p”: geteuid()=10316 but you should be root.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 172.19.147.8:47094 100.100.109.104:80 TIME_WAIT -
tcp 0 52 172.19.147.8:22 211.161.248.214:52204 ESTABLISHED -
tcp 0 0 172.19.147.8:22 223.71.37.166:50508 ESTABLISHED -
tcp 0 0 127.0.0.1:25 127.0.0.1:60546 TIME_WAIT -
tcp 0 0 172.19.147.8:59962 100.100.30.25:80 ESTABLISHED -
tcp 0 0 172.19.147.8:47096 100.100.109.104:80 TIME_WAIT -
tcp6 0 0 172.19.147.8:9100 47.95.206.45:29654 ESTABLISHED -

[jck287213@shell.ceshiren.com ~]$ netstat -tn
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 52 172.19.147.8:22 211.161.248.214:52204 ESTABLISHED
tcp 0 0 172.19.147.8:22 223.71.37.166:50508 ESTABLISHED
tcp 0 0 172.19.147.8:59962 100.100.30.25:80 ESTABLISHED
tcp6 0 0 172.19.147.8:9100 47.95.206.45:29654 ESTABLISHED
tcp6 0 0 127.0.0.1:60580 127.0.0.1:25 TIME_WAIT

[jck287213@shell.ceshiren.com ~]$ netstat -tlnp
t:tcp协议
u:utp协议
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 connection

数据统计

[jck287213@shell.ceshiren.com ~]$ netstat -tn | awk ‘NR>2{print $NF}’| sort | uniq -ort -nr
4 ESTABLISHED
2 TIME_WAIT