Skip to content

Commit afd4117

Browse files
author
241200050
committed
docs(linux): 添加 Linux网络性能调优文档
- 新增 linux 目录及其相关文档 - 添加网络性能调优相关章节 - 增补 TCP/IP 相关内容 - 插入云计算相关内容 -补充 pprof 工具使用说明
1 parent b599b7a commit afd4117

File tree

3 files changed

+224
-1
lines changed

3 files changed

+224
-1
lines changed

go/高并发.adoc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,53 @@ IO多路复用 + 单进(线)程有个好处,就是不会有并发编程的
109109

110110
==== pprof工具的简单使用
111111

112+
- 分析cpu
113+
114+
[source, go]
115+
----
116+
func main() {
117+
f, _ := os.OpenFile("cpu.profile", os.O_CREATE|os.O_RDWR, 0644)
118+
defer f.Close()
119+
pprof.StartCPUProfile(f)
120+
defer pprof.StopCPUProfile()
121+
122+
n := 10
123+
for i := 1; i <= 5; i++ {
124+
fmt.Printf("fib(%d)=%d\n", n, fib(n))
125+
n += 3 * i
126+
}
127+
}
128+
----
129+
130+
- 分析内存比较简单,随时可以进行采样
131+
132+
[source, go]
133+
----
134+
func main() {
135+
f, _ := os.OpenFile("mem.profile", os.O_CREATE|os.O_RDWR, 0644)
136+
defer f.Close()
137+
for i := 0; i < 100; i++ {
138+
repeat(generate(100), 100)
139+
}
140+
141+
pprof.Lookup("heap").WriteTo(f, 0)
142+
}
143+
----
144+
145+
当让在profile中对上述操作进行了简化,可以采用如下一行实现pprof采样
146+
147+
[source, go]
148+
----
149+
# 默认CPU采样
150+
defer profile.Start().Stop()
151+
# 如果需要对内存进行采样
152+
defer profile.Start(profile.MemProfile).Stop()
153+
# 使用浏览器查看
154+
go tool pprof -http :8080 cpu.profile
155+
----
156+
157+
158+
112159

113160

114161
https://darjun.github.io/2021/06/09/youdontknowgo/pprof/[pprof 性能分析工具]

linux/linux.adoc

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
:toc:
2+
3+
// 保证所有的目录层级都可以正常显示图片
4+
:path: linux/
5+
:imagesdir: ../image/
6+
7+
// 只有book调用的时候才会走到这里
8+
ifdef::rootpath[]
9+
:imagesdir: {rootpath}{path}{imagesdir}
10+
endif::rootpath[]
11+
12+
== linux
13+
14+
15+
16+
17+
18+
19+
=== 工具
20+
21+
==== sysctl
22+
23+
[source,bash]
24+
----
25+
# 列出所有sysctl可以设置的参数
26+
sysctl -a
27+
----
28+
29+
===== 套接字和tcp缓冲
30+
31+
所有协议类型的读(rmem_max)和写(wmem_max)的最大套接字缓冲区大小可以这样进行设置
32+
33+
[source,bash]
34+
----
35+
# 列出所有套接字缓冲区
36+
net.core.rmem_max
37+
net.core.wmem_max
38+
# 最小
39+
net.core.rmem_min
40+
net.core.wmem_min
41+
----
42+
43+
[source,bash]
44+
----
45+
# 为TCP的读和写缓冲设置自动调优参数
46+
# 最小, 默认, 最大字节数,长度从默认值自动调整,要提高吞吐量可以增加最大值
47+
# 增加最小值和默认值会使每个连接消耗更多不必要的内存
48+
sysctl -w net.ipv4.tcp_wmem="4096 87380 87380"
49+
sysctl -w net.ipv4.tcp_rmem="4096 87380 87380"
50+
----
51+
52+
===== TCP加压队列
53+
54+
首个积压队列,用于半开连接
55+
[source,bash]
56+
----
57+
net.ipv4.tcp_max_syn_backlog = 4096
58+
----
59+
60+
第二个积压队列,将连接传递给accept的监听积压队列
61+
[source,bash]
62+
----
63+
net.core.somaxconn = 1024
64+
----
65+
66+
为了应对突发的负载,这两个值也许都需要进行提高,比如设置成1024或者4096
67+
68+
===== 设备积压队列
69+
70+
增加每个CPU的网络设备积压队列长度
71+
72+
[source,bash]
73+
----
74+
# 如果是10GbE的网卡,这可能需要增加到10000
75+
net.core.netdev_max_backlog = 10000
76+
----
77+
78+
===== TCP拥塞控制算法
79+
80+
Linux支持可插入的拥塞控制算法
81+
82+
[source,bash]
83+
----
84+
# 列出所有支持的算法
85+
sysctl net.ipv4.tcp_available_congestion_control
86+
net.ipv4.tcp_available_congestion_control = reno cubic
87+
# 一些支持但是未加载,例如添加htcp
88+
modprobe tcp_htcp
89+
sysctl net.ipv4.tcp_available_congestion_control
90+
net.ipv4.tcp_available_congestion_control = reno cubic htcp
91+
----
92+
93+
===== TCP选项
94+
95+
一些TCP参数包括SACK和FACK扩展,他们能以一定的CPU负载为代价在高延时的网络中提高性能吞吐性
96+
97+
[source,bash]
98+
----
99+
net.ipv4.tcp_sack = 1
100+
net.ipv4.tcp_fack = 1
101+
# 可以重用一个TIME_WAIT会话
102+
net.ipv4.tcp_tw_reuse = 1
103+
# 可以重用一个TIME_WAIT会话,但是没有tcp_tw_reuse安全
104+
net.ipv4.tcp_tw_recycle = 1
105+
----
106+
107+
108+
109+
110+
111+

linux/performance-tools.adoc

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,12 @@ ethtool可以使用-i和-k选项来检查网络接口静态配置,也可以使
12951295

12961296
微基准测试工具,可以用来测试请求/响应的性能
12971297

1298+
[source, bash]
1299+
----
1300+
netserver -D -p 7001
1301+
netperf -v 100 -H 100.66.63.99 -t TCP_RR -p 7001
1302+
----
1303+
12981304
===== tc
12991305

13001306
流量控制工具tc,允许选择各种排队规则(qdiscs)来改善或管理性能
@@ -1305,9 +1311,17 @@ ethtool可以使用-i和-k选项来检查网络接口静态配置,也可以使
13051311
tc qdisc show dev eth0
13061312
# 使用netem将丢包率设置为1%
13071313
tc qdisc add dev eth0 root netem loss 1%
1308-
tc qdisc show dev eth0
1314+
# 使用-s显示网卡丢包的统计数据
1315+
tc -s qdisc show dev eth0
13091316
----
13101317

1318+
1319+
1320+
1321+
1322+
1323+
1324+
13111325
==== 调优
13121326

13131327
可通过配置来对网络性能进行调优,但是在试图调整之前最好先理解网络的使用情况,这样能发现可避免的不需要的操作,以提高更高的性能收益
@@ -1406,6 +1420,29 @@ cmdline_network_latency=skew_tick=1 tsc=reliable rcupdate.rcu_normal_after_boot=
14061420
|TCP_QUICKACK |立即发送 ACK(可以增加发送带宽)
14071421
|TCP_CONGESTION |套接字的一种拥塞控制算法
14081422
|===
1423+
1424+
1425+
==== 命令工具
1426+
1427+
1428+
1429+
1430+
1431+
1432+
1433+
1434+
1435+
1436+
1437+
1438+
1439+
1440+
1441+
1442+
1443+
1444+
1445+
14091446

14101447
=== 云计算
14111448

@@ -1416,6 +1453,34 @@ cmdline_network_latency=skew_tick=1 tsc=reliable rcupdate.rcu_normal_after_boot=
14161453

14171454

14181455

1456+
1457+
1458+
1459+
1460+
1461+
1462+
1463+
1464+
1465+
1466+
1467+
1468+
1469+
1470+
1471+
1472+
1473+
1474+
1475+
1476+
1477+
1478+
1479+
1480+
1481+
1482+
1483+
14191484
https://github.com/deepflowio/deepflow/blob/main/README-CN.md
14201485

14211486
https://deepflow.io/zh/ebpf-the-key-technology-to-observability/

0 commit comments

Comments
 (0)