1.TCP三次握手和四次揮手
2.優(yōu)化文件打開數(shù)
為了防止失控的進程破壞系統(tǒng)的性能,Linux會跟蹤進程使用的大部分資源,并允許用戶和系統(tǒng)管理員對進程的資源限制嫉你,例如控制某個進程打開的系統(tǒng)文件數(shù)、對某個用戶打開系統(tǒng)進程數(shù)進行限制叉讥,包括軟顯示和硬限制
軟限制
soft limit 內核實際執(zhí)行的限制础淤,任何進程都可以講軟限制設置為任意小于或等于對進程的硬限制的值资铡、最大線程數(shù)(noproc)和文件數(shù)(nofile)
硬限制
可以在任何時候任何進程中設置,但應限制只能由超級用戶修改
默認Linux對每個用戶打開文件最大數(shù)為1024边篮,對于高并發(fā)網(wǎng)站是遠遠不夠的
vim /etc/security/limits.conf
* soft nofile 65535
* hard nofile 66535
* soft noproc 65535
* hard noproc 65535
如果需要對Linux整個系統(tǒng)設置文件最大數(shù)限制己莺,需要修改
[root@nginx ~]# cat /proc/sys/fs/file-max
6815744
3.內核參數(shù)的優(yōu)化
Linux /proc/sys 目錄下存放著對數(shù)內核的參數(shù)
/etc/sysctl.conf 修改內核參數(shù)永久生效
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies =1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 10000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 87380 4194304
net.core.rmem_default = 8388608
net.core.wmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 65500
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 945000000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65535
Linux內核日志一般保存在messages日志中 /var/log/messages
4.影響服務器性能因素
cpu user%+sys% < 70%
內存 Swap In(si) = 0 Swap Out(so) = 0
磁盤 iowait% < 20%
常用的分析命令為:
vmstat sar iostat netstat free ps top iftop等
vmstat sar iostat CPU
free vmstat 內存
iostat 磁盤
netstat iftop 網(wǎng)絡寬帶
整體性能評估
uptime
[root@nginx ~]# uptime
23:36:00 up 3:18, 2 users, load average: 0.00, 0.01, 0.05
load average 三個值 分別表示 最近1、5戈轿、15分鐘系統(tǒng)的負載
三個值一般不能超過系統(tǒng)邏輯CPU核數(shù)的2倍
CPU性能評估
vmstat
[root@nginx ~]# vmstat 2 10
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 3594200 2076 148892 0 0 5 1 31 28 0 0 100 0 0
0 0 0 3594184 2076 148892 0 0 0 0 57 44 0 0 100 0 0
0 0 0 3594184 2076 148892 0 0 0 0 48 42 0 0 100 0 0
0 0 0 3594184 2076 148892 0 0 0 0 43 35 0 0 100 0 0
0 0 0 3594184 2076 148892 0 0 0 0 48 39 0 0 100 0 0
0 0 0 3594184 2076 148892 0 0 0 0 42 35 0 0 100 0 0
0 0 0 3594184 2076 148892 0 0 0 0 55 43 0 0 100 0 0
0 0 0 3594184 2076 148892 0 0 0 0 53 42 0 0 100 0 0
0 0 0 3594184 2076 148892 0 0 0 0 55 46 0 0 100 0 0
0 0 0 3594184 2076 148892 0 0 0 0 44 36 0 0 100 0 0
r :該列表示運行和等待CPU時間片的進程數(shù)凌受,如果該值長時間大于系統(tǒng)CPU的個數(shù),說明CPU不足思杯,需要增加CPU
b: 表示等待資源的進程數(shù)胜蛉,比如正在等待I/O或者內存交換等
us: 用戶進程消耗的CPU時間百分比,如果長期大于50%,就需要考慮優(yōu)化程序或算法
sy:內核進程消耗的CPU時間百分比,
us+sys: 參考值為80% 誊册,大于80%說明存在CPU資源不足
sar
功能強大领突,但消耗資源,不過不影響評估
yum -y install sysstat
[root@nginx ~]# sar -u 2 10
Linux 3.10.0-693.el7.x86_64 (nginx.example.com) 11/12/2019 _x86_64_ (2 CPU)
11:54:53 PM CPU %user %nice %system %iowait %steal %idle
11:54:55 PM all 0.00 0.00 0.00 0.00 0.00 100.00
11:54:57 PM all 0.00 0.00 0.00 0.00 0.00 100.00
11:54:59 PM all 0.00 0.00 0.00 0.00 0.00 100.00
11:55:01 PM all 0.00 0.00 0.25 0.00 0.00 99.75
11:55:03 PM all 0.00 0.00 0.25 0.00 0.00 99.75
11:55:05 PM all 0.00 0.00 0.00 0.00 0.00 100.00
11:55:07 PM all 0.00 0.00 0.00 0.00 0.00 100.00
11:55:09 PM all 0.00 0.00 0.25 0.00 0.00 99.75
11:55:11 PM all 0.00 0.00 0.00 0.00 0.00 100.00
11:55:13 PM all 0.00 0.00 0.00 0.00 0.00 100.00
Average: all 0.00 0.00 0.08 0.00 0.00 99.92
%user
%nice
%system
%iowait
%steal
%idle
內存性能評估
free
[root@nginx ~]# free -m
total used free shared buff/cache available
Mem: 3774 118 3350 8 305 3406
Swap: 5721 0 5721
應用程序可用內存/系統(tǒng)物理內存>70%解虱,表示內存充足
應用程序可用內存/系統(tǒng)物理內存<20%攘须,表示內存資源緊缺 需要增加內存
20%<應用程序可用內存/系統(tǒng)物理內存 <70% 基本夠用,不影響系統(tǒng)性能
磁盤性能評估
iostat
[root@nginx ~]# iostat -d 1 10
Linux 3.10.0-693.el7.x86_64 (nginx.example.com) 11/12/2019 _x86_64_ (2 CPU)
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
scd0 0.00 0.08 0.00 1028 0
sda 0.51 13.87 9.70 178010 124535
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
scd0 0.00 0.00 0.00 0 0
sda 0.00 0.00 0.00 0 0
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
scd0 0.00 0.00 0.00 0 0
sda 0.00 0.00 0.00 0 0
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
scd0 0.00 0.00 0.00 0 0
sda 0.00 0.00 0.00 0 0
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
scd0 0.00 0.00 0.00 0 0
sda 0.00 0.00 0.00 0 0
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
scd0 0.00 0.00 0.00 0 0
sda 0.00 0.00 0.00 0 0
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
scd0 0.00 0.00 0.00 0 0
sda 0.00 0.00 0.00 0 0
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
scd0 0.00 0.00 0.00 0 0
kB_read/s 每秒讀取的數(shù)據(jù)塊數(shù)
kB_wrtn/s 每秒寫入的數(shù)據(jù)塊數(shù)
kB_read 讀取的所有數(shù)據(jù)塊數(shù)
kB_wrtn 寫入的所有數(shù)據(jù)塊數(shù)
sar
[root@nginx ~]# sar -d 1 10
Linux 3.10.0-693.el7.x86_64 (nginx.example.com) 11/12/2019 _x86_64_ (2 CPU)
11:56:40 PM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
11:56:41 PM dev11-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
11:56:41 PM dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
11:56:41 PM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
11:56:42 PM dev11-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
11:56:42 PM dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
11:56:42 PM DEV tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util
11:56:43 PM dev11-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
await 平均每次IO操作的等待時間 單位ms
svctm 平均每次IO操作的服務時間 ms
%util 表示1S中有百分之幾的時間用于IO操作
評估標準svctm 應該小于await
svctm 的大小和磁盤性能有關 CPU殴泰,內存負荷也會對 該值有影響
過多的請求也會導致svctm 值的增加
如何svctm 和await的值接近于宙,表示幾乎沒有IO等待,性能很好
如何大于svctm 表示IO隊列等待很長
%util 如何接近100% 表示磁盤I/O請求過多悍汛,磁盤存在瓶頸