Linux TCP参数——tcp_adv_win_scale

文章目录

tcp_adv_win_scale

adv -advise;win -window;

用于指示TCP中接收缓存比例的值。

c 复制代码
static inline int tcp_win_from_space(int space)
{
	return sysctl_tcp_adv_win_scale<=0 ?
		(space>>(-sysctl_tcp_adv_win_scale)) :
		space - (space>>sysctl_tcp_adv_win_scale);
}

ip-sysctl.txt解释

txt 复制代码
tcp_adv_win_scale - INTEGER
	Count buffering overhead as bytes/2^tcp_adv_win_scale
	(if tcp_adv_win_scale > 0) or bytes-bytes/2^(-tcp_adv_win_scale),
	if it is <= 0.
	Possible values are [-31, 31], inclusive.
	Default: 1

指定计算缓冲 Overhead 的方式:

如果 tcp_adv_win_scale > 0 则为 bytes/2^tcp_adv_win_scale 否则为bytes - bytes/2^(-tcp_adv_win_scale)。

默认值:1(低版本默认值是 2)

可选值:-31, 31

buffering overhead

buffering overhead即缓冲开销,即除去数据部分的其他开销,包括头字段开销以及内核结构体(例:sk_buffer)设置部分的上下文参数的开销。
也就是接收缓存区其实是由数据信息和辅助信息组成!!!
所以"Count buffering overhead as"说明这是计算辅助信息(即非数据信息)的所占空间大小,而该参数就是比例因子。

内核缓存和应用缓存

tcp手册描述,"application" buffer,以及延伸出内核缓存概念(形容可能不是那么恰当准确,所以不用太过于陷入这个命名之中,否则会不利于你的理解)

txt 复制代码
The socket receive buffer space is shared between the
application and kernel.  TCP maintains part of the buffer
as the TCP window, this is the size of the receive window
advertised to the other end.  The rest of the space is
used as the "application" buffer, used to isolate the
network from scheduling and application latencies.  The
tcp_adv_win_scale default value of 2 implies that the
space used for the application buffer is one fourth that
of the total.

注:应用部分的作用是"used to isolate the network from scheduling and application latencies",具体指的应该是内核的 skb_shared_info 结构,对 TCP 本身没什么用,但对其它模块有用。

所以需要理解的其实就是:

  1. 内核缓存,表示的就是TCP载荷(数据),也就是TCP滑动窗口机制中的通告窗口大小
  2. 应用缓存,表示的就是内核中的辅助标识数据的元数据信息

    REF:关于Linux TCP接收缓存以及接收窗口的一个细节解析

示例计算

txt 复制代码
tcp_adv_win_scale > 0
	bytes/2^tcp_adv_win_scale
tcp_adv_win_scale <= 0
	bytes - bytes/2^(-tcp_adv_win_scale)

1:1/2 即非数据使用1/2区域,数据使用1/2区域
2:1/4 即非数据使用1/4区域,数据使用3/4区域

你会发现:在Linux内核版本优化中为什么该值会从2到1呢?(我的第一反应是增大非数据区的比例不是会减少载荷导致效率反而下降吗?)

深入理解从2到1(tcp_adv_win_scale的值)

比较Linux 2.6.32和Linux 3.10版本的sk_buffer

之所以要比较sk_buffer,是因为struct sk_buffer是网络协议栈中整个网络数据包存储的地方。(这个数据结构会被网络协议栈中的各层用来储存它们的协议头、用户数据和其他它们完成工作需要的数据)

shell 复制代码
# 以下命令可以查看到内核部分结构大小
sudo cat /proc/slabinfo
sudo cat slabtop

对比结果是sk_buff变打了,因为功能扩展,除了skb的膨胀之外,系统中还有别的膨胀,比如为了效率的"对齐开销",但更大的开销增加是skb_shared_info结构体的计入。

虽然这种开销的膨胀在TCP层面几乎看不到什么收益(反而付出了代价,你不得不配置更大的rcvbuf...),然而skb等并不单单服务于TCP,这种膨胀的收益可能被调度,中断,IP路由,负载均衡等机制获取了。所以这种变化对于整个系统来说,是好的变化,好的发展。
记住两点即可:首先,Linux内核各个子系统是一个整体,其次,内存越来越便宜而时间一去不复返,空间换时间,划得来!

总结

tcp_adv_win_scale计算出非载荷的占比,也被称之为application" buffer的占比。

相关推荐
壹方秘境27 分钟前
ChatTCP是怎么像Wireshark那样识别TCP重传、乱序和心跳保活的
网络协议·tcp/ip·wireshark
Moshow郑锴30 分钟前
Ubuntu 26.04 中文输入法 : fcitx5+Rime中州韵引擎
linux·运维·ubuntu
祺风挽楠1 小时前
ansible编辑
网络·ansible
莫名的好感°1 小时前
手机RAR解压怎么选?2026年二季度四款产品问答
服务器·网络·智能手机
qq_163135752 小时前
Linux 【04-more命令超详细教程】
linux
sevencheng7983 小时前
【ADB】adb命令行常用按键模拟代码
linux·adb·模拟按键,返回键,音量键
暗影天帝3 小时前
BPI-R3 Mini 刷 Yuzhii DHCPD U-Boot 教程
linux
小赖同学啊3 小时前
智能连接器集群化高可用生产方案
linux·运维·人工智能
Cinema KI3 小时前
Linux第一个系统程序-进度条
linux·服务器
Moshow郑锴4 小时前
Ubuntu 26.04 更换阿里云源镜像
linux·运维·ubuntu