0%

liunx 性能分析

uptime

1
2
$uptime
07:13:53up8days,19min, 1user, load average: 1.98,2.15,2.21

load average: 1.98,2.15,2.21 一分钟内的负载,5分钟内的负载,15分钟内的负载。

为了进一步理解系统负载,需要做一些假设。假设系统负载如下:
23:16:49 up 10:49, 5 user, load average: 1.00, 0.40, 3.35
在单核系统中意味着:

  • CPU 被充分利用(100%);最近的 1 分钟有 1 个进程在运行。
  • CPU 有 60% 处于空闲状态;在最近的 5 分钟没有进程等待 CPU 时间。
  • CPU 平均过载了 235%;最近的 15 分钟平均有 2.35 个进程在等待 CPU 时间。
    在双核系统中意味着:
  • 有一个 CPU 处于完全空闲状态,另一个 CPU 被使用;最近的 1 分钟没有进程等待 CPU 时间。
  • CPU 平均 160% 处于空闲状态;最近的 5 分钟没有进程等待 CPU 时间。
  • CPU 平均过载了 135%;最近的 15 分钟有 1.35 个进程等待 CPU 时间。

top -H -p pid

cpu 负载监控,实时查看占用cpu高的线程

vmstat

系统负载监控

iostat

磁盘利用率监控

netstat -anp | grep ‘ip/prot’

查看网络连接信息

lsof -p pid

  • 使用lsof -p pid 命令查看进程打开的文件,如果大部分文件都是同一类型的文件,说明可能未关闭文件流。找到打开文件的代码,关闭文件流即可。
  • 如果不存在未关闭文件流的问题,且业务本身就需要处理大量文件,则修改 /etc/security/limits.conf 文件如下内容;
    1
    2
    * hard nproc 10240
    * soft nproc 10240

压测分析步骤

  1. 优化服务器本省的TCP信息
  • tcp 端口的释放时间
  1. 查看内存,cpu,硬盘的IO
  • 内存 Free -h 查看 buffers/cache 的值,buffers/cache 表示实际占用的值
  • iostat
  • top
  1. 分析服务器当前的并发数,文件打开数

netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’

Liunx 内核优化
vim /etc/security/limits.conf

  •           soft    nproc           10240
    
  •           hard    nproc           16384
    
  •           soft    nofile          60240
    
  •           hard    nofile          65536
    
  •           hard    core           unlimited
    

vim /etc/security/limits.d/90-nproc.conf

  •      soft    nproc     50240
    

root soft nproc unlimited

vim /etc/sysctl.conf

net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 60

net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestsmps = 0

net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768

net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_wmem = 8192 436600 873200
net.ipv4.tcp_rmem = 32768 436600 873200
net.ipv4.tcp_mem = 94500000 91500000 92700000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_fin_timeout = 30

Liunx 最大文件数

命令:ulimit -a
open files (-n) 1024
“open files” 参数选项后面的数值就是当前系统的支持的最大打开文件数