服务器的性能监控与分析
[root@iz2zeh1vukq7bno84jwa0tz ~]# vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 95500 11552 169968 0 0 27 8 5 2 2 2 96 0 0
0 0 0 95500 11552 170000 0 0 0 12 1664 3683 2 2 96 0 0
0 0 0 95500 11552 170000 0 0 0 0 1690 3756 2 1 97 0 0
1 0 0 95500 11552 170000 0 0 0 0 1655 3710 2 1 97 0 0
0 0 0 95500 11552 170000 0 0 0 0 1671 3755 2 2 96 0 0
[root@iz2zeh1vukq7bno84jwa0tz ~]# mpstat 1 5
Linux 3.10.0-1127.19.1.el7.x86_64 (iz2zeh1vukq7bno84jwa0tz) 2023年07月26日 _x86_64_ (1 CPU)
21时03分42秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
21时03分43秒 all 1.01 0.00 2.02 0.00 0.00 0.00 0.00 0.00 0.00 96.97
21时03分44秒 all 2.02 0.00 1.01 0.00 0.00 0.00 0.00 0.00 0.00 96.97
21时03分45秒 all 2.02 0.00 2.02 0.00 0.00 0.00 0.00 0.00 0.00 95.96
21时03分46秒 all 2.02 0.00 2.02 0.00 0.00 0.00 0.00 0.00 0.00 95.96
21时03分47秒 all 2.02 0.00 1.01 0.00 0.00 0.00 0.00 0.00 0.00 96.97
平均时间: all 1.82 0.00 1.62 0.00 0.00 0.00 0.00 0.00 0.00 96.57
# 指定cpu,cpu核编号从零开始
[root@iz2zeh1vukq7bno84jwa0tz ~]# mpstat -P 0 1 5
Linux 3.10.0-1127.19.1.el7.x86_64 (iz2zeh1vukq7bno84jwa0tz) 2023年07月26日 _x86_64_ (1 CPU)
21时06分18秒 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
21时06分19秒 0 3.96 0.00 2.97 0.00 0.00 0.00 0.00 0.00 0.00 93.07
21时06分20秒 0 1.02 0.00 1.02 0.00 0.00 0.00 0.00 0.00 0.00 97.96
21时06分21秒 0 3.00 0.00 2.00 0.00 0.00 0.00 0.00 0.00 0.00 95.00
21时06分22秒 0 1.01 0.00 2.02 0.00 0.00 0.00 0.00 0.00 0.00 96.97
21时06分23秒 0 2.00 0.00 2.00 0.00 0.00 0.00 0.00 0.00 0.00 96.00
平均时间: 0 2.21 0.00 2.01 0.00 0.00 0.00 0.00 0.00 0.00 95.78
# 对每个进程进行资源监控
[root@iz2zeh1vukq7bno84jwa0tz ~]# pidstat
Linux 3.10.0-1127.19.1.el7.x86_64 (iz2zeh1vukq7bno84jwa0tz) 2023年07月26日 _x86_64_ (1 CPU)
21时07分47秒 UID PID %usr %system %guest %CPU CPU Command
21时07分47秒 0 1 0.00 0.00 0.00 0.00 0 systemd
21时07分47秒 0 2 0.00 0.00 0.00 0.00 0 kthreadd
21时07分47秒 0 6 0.00 0.01 0.00 0.01 0 ksoftirqd/0
21时07分47秒 0 9 0.00 0.02 0.00 0.02 0 rcu_sched
21时07分47秒 0 11 0.00 0.00 0.00 0.00 0 watchdog/0
21时07分47秒 0 15 0.00 0.00 0.00 0.00 0 khungtaskd
21时07分47秒 0 30 0.00 0.00 0.00 0.00 0 kswapd0
21时07分47秒 0 32 0.00 0.00 0.00 0.00 0 khugepaged
21时07分47秒 0 100 0.00 0.00 0.00 0.00 0 kauditd
21时07分47秒 0 245 0.00 0.00 0.00 0.00 0 kworker/0:1H
21时07分47秒 0 257 0.00 0.00 0.00 0.00 0 jbd2/vda1-8
21时07分47秒 0 336 0.00 0.00 0.00 0.00 0 systemd-journal
21时07分47秒 0 354 0.00 0.00 0.00 0.00 0 systemd-udevd
21时07分47秒 0 378 0.00 0.00 0.00 0.00 0 auditd
21时07分47秒 999 418 0.00 0.00 0.00 0.00 0 polkitd
21时07分47秒 0 419 0.00 0.00 0.00 0.00 0 CmsGoAgent.linu
21时07分47秒 0 428 0.00 0.00 0.00 0.00 0 systemd-logind
21时07分47秒 81 431 0.00 0.00 0.00 0.00 0 dbus-daemon
21时07分47秒 38 449 0.00 0.00 0.00 0.00 0 ntpd
# 轮询每隔1秒自动获取3个活动进程的cpu使用情况
[root@iz2zeh1vukq7bno84jwa0tz ~]# pidstat 1 3
Linux 3.10.0-1127.19.1.el7.x86_64 (iz2zeh1vukq7bno84jwa0tz) 2023年07月26日 _x86_64_ (1 CPU)
21时09分16秒 UID PID %usr %system %guest %CPU CPU Command
21时09分17秒 0 785 0.99 0.00 0.00 0.99 0 python3
21时09分17秒 0 7857 0.99 0.00 0.00 0.99 0 java
21时09分17秒 0 16937 0.99 0.99 0.00 1.98 0 AliYunDunMonito
21时09分17秒 UID PID %usr %system %guest %CPU CPU Command
21时09分18秒 0 651 1.03 1.03 0.00 2.06 0 python3
21时09分18秒 0 5801 0.00 1.03 0.00 1.03 0 pidstat
21时09分18秒 0 16927 0.00 1.03 0.00 1.03 0 AliYunDun
21时09分18秒 UID PID %usr %system %guest %CPU CPU Command
21时09分19秒 0 651 1.01 1.01 0.00 2.02 0 python3
21时09分19秒 0 7857 0.00 1.01 0.00 1.01 0 java
21时09分19秒 0 16927 1.01 0.00 0.00 1.01 0 AliYunDun
21时09分19秒 0 16937 1.01 1.01 0.00 2.02 0 AliYunDunMonito
平均时间: UID PID %usr %system %guest %CPU CPU Command
平均时间: 0 651 0.67 0.67 0.00 1.35 - python3
平均时间: 0 785 0.34 0.00 0.00 0.34 - python3
平均时间: 0 5801 0.00 0.34 0.00 0.34 - pidstat
平均时间: 0 7857 0.34 0.34 0.00 0.67 - java
平均时间: 0 16927 0.34 0.34 0.00 0.67 - AliYunDun
平均时间: 0 16937 0.67 0.67 0.00 1.35 - AliYunDunMonito
# 每个进程I/O使用情况
[root@iz2zeh1vukq7bno84jwa0tz ~]# pidstat -d
Linux 3.10.0-1127.19.1.el7.x86_64 (iz2zeh1vukq7bno84jwa0tz) 2023年07月26日 _x86_64_ (1 CPU)
21时11分49秒 UID PID kB_rd/s kB_wr/s kB_ccwr/s Command
21时11分49秒 0 1 11.46 1.71 0.13 systemd
21时11分49秒 0 30 0.00 0.06 0.00 kswapd0
21时11分49秒 0 257 0.00 1.30 0.00 jbd2/vda1-8
21时11分49秒 0 336 1.01 0.49 0.00 systemd-journal
21时11分49秒 0 354 0.01 0.00 0.00 systemd-udevd
21时11分49秒 0 378 0.00 0.02 0.00 auditd
21时11分49秒 999 418 0.04 0.00 0.00 polkitd
# pidstat 其他参数,可通过pidstat --help查看
pidstat -p 指定进程的资源使用情况
pidstat -r 进程内存使用情况
# 查看进程名gunicorn打开了哪些文件
[root@iz2zeh1vukq7bno84jwa0tz static]# lsof -c gunicorn
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
gunicorn 9501 root cwd DIR 0,49 4096 1322034 /code/donkey
gunicorn 9501 root rtd DIR 0,49 4096 1707072 /
356673 922239 /var/data/log/DailyLog
gunicorn 9547 root 14u REG 253,1 1
# 其他命令
lsof -p pid # 查看某个进程id打开了哪些文件
lsof -i:80 # 查看80端口下的网络连接通信情况
# 查看系统内存使用情况
[root@iz2zeh1vukq7bno84jwa0tz ~]# free
total used free shared buff/cache available
Mem: 1881768 1617444 86760 760 177564 112380
Swap: 0 0 0
# 定位服务器性能消耗情况:top
top - 13:38:29 up 90 days, 22:38, 1 user, load average: 0.02, 0.04, 0.09
Tasks: 101 total, 1 running, 100 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.3 us, 2.0 sy, 0.0 ni, 96.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1881768 total, 98880 free, 1618164 used, 164724 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 111484 avail Mem
Unknown command - try 'h' for help
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
10733 root 20 0 51800 36328 1284 S 1.7 1.9 13:32.77 python3
16937 root 10 -10 135656 9172 1760 S 0.7 0.5 274:39.84 AliYunDunMonito
7857 root 20 0 2720332 339764 0 S 0.3 18.1 169:34.69 java
10866 root 20 0 54644 39484 2788 S 0.3 2.1 1:41.08 python3
16927 root 10 -10 108928 13072 3368 S 0.3 0.7 76:34.72 AliYunDun
1 root 20 0 191064 2680 1184 S 0.0 0.1 3:58.49 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd
4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
6 root 20 0 0 0 0 S 0.0 0.0 16:15.93 ksoftirqd/0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
# 查看网络流量使用信息:iftop
12.5Kb 25.0Kb 37.5Kb 50.0Kb 62.5Kb└────────────────────────────────────────────────────┴────────────────────────────────────────────────────┴─────────────────────────────────────────────────────┴────────────────────────────────────────────────────┴─────────────────────────────────────────────────────iz2zeh1vukq7bno84jwa0tz => ecs-119-3-116-109.compute.hwclouds-dns.com 0b 3.22Kb 3.22Kb
<= 0b 10.3Kb 10.3Kb
iz2zeh1vukq7bno84jwa0tz => 100.100.33.28 0b 10.4Kb 10.4Kb
<= 0b 533b 533b
iz2zeh1vukq7bno84jwa0tz => 118.113.77.134 3.61Kb 4.34Kb 4.34Kb
<= 368b 470b 470b
iz2zeh1vukq7bno84jwa0tz => 100.100.30.25 0b 1.81Kb 1.81Kb
<= 0b 46b 46b
iz2zeh1vukq7bno84jwa0tz => 100.100.2.136 280b 406b 406b
<= 500b 894b 894b
iz2zeh1vukq7bno84jwa0tz => 100.100.2.138 0b 443b 443b
<= 0b 790b 790b
iz2zeh1vukq7bno84jwa0tz => 100.100.3.3 304b 76b 76b
<= 304b 76b 76b
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────TX: cum: 20.6KB peak: 53.6Kb rates: 4.18Kb 20.6Kb 20.6Kb
RX: 13.0KB 28.0Kb 1.14Kb 13.0Kb 13.0Kb
TOTAL: 33.7KB 81.5Kb 5.32Kb 33.7Kb 33.7Kb
nmon 是linux免费的性能监控工具
# linux 查看cpu的核数:lscpu
[root@iz2zeh1vukq7bno84jwa0tz ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 1
On-line CPU(s) list: 0
Thread(s) per core: 1
Core(s) per socket: 1
座: 1
NUMA 节点: 1
厂商 ID: GenuineIntel
CPU 系列: 6
型号: 85
型号名称: Intel(R) Xeon(R) Platinum 8269CY CPU @ 2.50GHz
步进: 7
CPU MHz: 2499.998
BogoMIPS: 4999.99
超管理器厂商: KVM
虚拟化类型: 完全
L1d 缓存: 32K
L1i 缓存: 32K
L2 缓存: 1024K
L3 缓存: 36608K
NUMA 节点0 CPU: 0
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 arat avx512_vnni
apache 工作模式
- prfork模式:默认工作模式,多进程,单进程单线程方式,不存在线程安全问题,但不能处理高并发请求的场景。
<IfModule mpm_prefork_module>StartServers 8MinSpareServers 8MaxSpareServers 10MaxRequestWorkers 512MaxConnectionsPerChild 1000</ IfModule>worker模式:多进程多线程的混合模式来处理请求,需考虑线程安全问题
<IfModule mpm_worker_module>StartServers 4ServerLimit 20MinSpareThreads 65MaxSpareThreads 256ThreadPerChild 30MaxRequestWorkers 410MaxConnectionsPerChild 1200</ IfModule>event模式:与worker模式类似,是对worler模式的升级改进。有专门的线程承担管理和分配线程的工作,比如对keep-live长连接工作线程的管理。
<IfModule mpm_event_module>StartServers 4ServerLimit 20MinSpareThreads 65MaxSpareThreads 256ThreadPerChild 30MaxRequestWorkers 410MaxConnectionsPerChild 1200</ IfModule>
apache 自带性能测试工具ab
如:ab -n 10000 -c 60 -k http://127.0.0.1:80/ ,表示总共发送10000次压测请求,并发连接数为60,并且在压测时客户端开启KeepAlive。
jvm 性能监控工具集
远程连接时,需开启jmx参考链接
nohup java -jar -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.rmi.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=123.57.138.224 case-server-1.0-SNAPSHOT.jar >/dev/null 2>&1 &
jconsole,jvm可视化图形监控工具
jvisualvm,jvm可视化图形监控工具,比jconsole可监控的信息更多
jmap,生成jvm堆转储的dump快照
jstat,jvm监视小工具,监视jvm运行情况、类加载情况、jvm内存使用、GC垃圾回收、JIT编译信息
jstack,可查看线程运行、调用情况
MAT(需要下载)
开启GC日志,启动程序时加入对应的参数
mysql数据库性能监控
查看数据库的连接数:show processlist,默认显示100条,超过100条,用show full processlist;
查看允许的最大连接数:show variables like 'max_connections';
查看已使用过的最大连接数:show global status like 'max_used_connections';
查看数据库打开了哪些表:show open tables; 过滤出当前已经被锁定的表:show open tables where In_use > 0;
查看数据库中表的状态,show status like '%table%',Table_locks_waited如果数值过大,可能存在锁争抢的情况。
查看数据库中索引的使用情况:show global status like 'handler_read%';
查看每秒事务提交数:show global status like 'com_commit';
查看每秒事务回滚数:show global status like 'com_rollback';
查看线程的运行情况:show global status like 'threads_%';
查看连接总数(包括连接中和已断开的连接):show global status like 'Connections';
查看innodb引擎缓存命中情况:show global status like 'innodb_buffer_pool_read%';
查看join操作没有使用索引的次数:show global status like 'select_full_join';
查看sql中排序使用情况:show global status like 'sort%';
查看sql缓存命中情况:show global status like 'qcache%';
查询更多的状态信息:show global status;
show status 查询当前的运行状态,show global status 查询全局的运行状态。
查看是否开启慢查询日志:show variables like 'slow_query%';
临时开启慢查询日志:set global slow_query_log = 1; 或者 set global slow_query_log = ON;
长期开启慢查询日志:编辑/etc/my.cnf,加入如下两项,修改后需要重启mysql:
slow_query_log=ON slow_query_log_file=/var/lib/mysql/localhost-slow.log
慢查询记录时间:show variables like 'long_query%';
修改慢查询记录时间:set long_query_time=xx;
查看慢查询发生的次数:show status like 'slow_queries';
查看sql的执行计划:explain sql语句,如:explain SELECT * FROM member_weekly_report WHERE member_id = '2206';