Linux Tune Network Stack (Buffers Size) To Increase Networking Performance

本文介绍如何通过调整Linux内核参数来提高大型文件传输的网络性能,包括增大缓冲区大小、启用窗口缩放等功能。

Linux Tune Network Stack (Buffers Size) To Increase Networking Performance

I‘ve two servers located in two different data center. Both server deals with a lot of concurrent large file transfers. But network performance is very poor for large files and performance degradation take place with a large files. How do I tune TCP under Linux to solve this problem?

By default the Linux network stack is not configured for high speed large file transfer across WAN links. This is done to save memory resources. You can easily tune Linux network stack by increasing network buffers size for high-speed networks that connect server systems to handle more network packets.

The default maximum Linux TCP buffer sizes are way too small. TCP memory is calculated automatically based on system memory; you can find the actual values by typing the following commands:
$ cat /proc/sys/net/ipv4/tcp_mem
The default and maximum amount for the receive socket memory:
$ cat /proc/sys/net/core/rmem_default
$ cat /proc/sys/net/core/rmem_max

The default and maximum amount for the send socket memory:
$ cat /proc/sys/net/core/wmem_default
$ cat /proc/sys/net/core/wmem_max

The maximum amount of option memory buffers:
$ cat /proc/sys/net/core/optmem_max

Tune values

Set the max OS send buffer size (wmem) and receive buffer size (rmem) to 12 MB for queues on all protocols. In other words set the amount of memory that is allocated for each TCP socket when it is opened or created while transferring files:

WARNING! The default value of rmem_max and wmem_max is about 128 KB in most Linux distributions, which may be enough for a low-latency general purpose network environment or for apps such as DNS / Web server. However, if the latency is large, the default size might be too small. Please note that the following settings going to increase memory usage on your server.

# echo 'net.core.wmem_max=12582912' >> /etc/sysctl.conf
# echo 'net.core.rmem_max=12582912' >> /etc/sysctl.conf

You also need to set minimum size, initial size, and maximum size in bytes:
# echo 'net.ipv4.tcp_rmem= 10240 87380 12582912' >> /etc/sysctl.conf
# echo 'net.ipv4.tcp_wmem= 10240 87380 12582912' >> /etc/sysctl.conf

Turn on window scaling which can be an option to enlarge the transfer window:
# echo 'net.ipv4.tcp_window_scaling = 1' >> /etc/sysctl.conf
Enable timestamps as defined in RFC1323:
# echo 'net.ipv4.tcp_timestamps = 1' >> /etc/sysctl.conf
Enable select acknowledgments:
# echo 'net.ipv4.tcp_sack = 1' >> /etc/sysctl.conf
By default, TCP saves various connection metrics in the route cache when the connection closes, so that connections established in the near future can use these to set initial conditions. Usually, this increases overall performance, but may sometimes cause performance degradation. If set, TCP will not cache metrics on closing connections.
# echo 'net.ipv4.tcp_no_metrics_save = 1' >> /etc/sysctl.conf
Set maximum number of packets, queued on the INPUT side, when the interface receives packets faster than kernel can process them.
# echo 'net.core.netdev_max_backlog = 5000' >> /etc/sysctl.conf
Now reload the changes:
# sysctl -p
Use tcpdump to view changes for eth0:
# tcpdump -ni eth0

Recommend readings:

先展示下效果 https://pan.quark.cn/s/a4b39357ea24 遗传算法 - 简书 遗传算法的理论是根据达尔文进化论而设计出来的算法: 人类是朝着好的方向(最优解)进化,进化过程中,会自动选择优良基因,淘汰劣等基因。 遗传算法(英语:genetic algorithm (GA) )是计算数学中用于解决最佳化的搜索算法,是进化算法的一种。 进化算法最初是借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传、突变、自然选择、杂交等。 搜索算法的共同特征为: 首先组成一组候选解 依据某些适应性条件测算这些候选解的适应度 根据适应度保留某些候选解,放弃其他候选解 对保留的候选解进行某些操作,生成新的候选解 遗传算法流程 遗传算法的一般步骤 my_fitness函数 评估每条染色体所对应个体的适应度 升序排列适应度评估值,选出 前 parent_number 个 个体作为 待选 parent 种群(适应度函数的值越小越好) 从 待选 parent 种群 中随机选择 2 个个体作为父方和母方。 抽取父母双方的染色体,进行交叉,产生 2 个子代。 (交叉概率) 对子代(parent + 生成的 child)的染色体进行变异。 (变异概率) 重复3,4,5步骤,直到新种群(parentnumber + childnumber)的产生。 循环以上步骤直至找到满意的解。 名词解释 交叉概率:两个个体进行交配的概率。 例如,交配概率为0.8,则80%的“夫妻”会生育后代。 变异概率:所有的基因中发生变异的占总体的比例。 GA函数 适应度函数 适应度函数由解决的问题决定。 举一个平方和的例子。 简单的平方和问题 求函数的最小值,其中每个变量的取值区间都是 [-1, ...
<think>好的,用户想了解如何在Linux上修改ext4文件系统的块大小。首先,我需要确认用户的需求。块大小是文件系统中的一个重要参数,影响磁盘空间利用率和性能。用户可能遇到了需要优化性能或调整存储效率的情况。 接下来,我需要回忆一下ext4文件系统的相关知识。块大小通常在创建文件系统时设定,之后无法直接修改。因此,用户可能需要备份数据,重新格式化分区,并恢复数据。这一点很重要,必须明确告诉用户,否则可能会导致数据丢失。 然后,我应该分步骤解释整个过程。首先确认当前块大小,使用dumpe2fs命令。然后卸载分区,使用mkfs.ext4指定新的块大小重新格式化。最后挂载并验证。需要提醒用户备份数据,因为格式化会清除所有数据。 另外,用户可能不知道块大小的选择对性能的影响。比如,较大的块适合大文件,提升性能但可能浪费空间;较小的块适合小文件,节省空间但可能增加碎片。这部分需要解释清楚,帮助用户做出合适的选择。 还要考虑用户是否可能混淆块大小和集群大小,或者其他文件系统参数。需要确保用词准确,避免歧义。同时,检查是否有其他方法或工具可以在不格式化的情况下调整块大小,但据我所知,ext4不支持在线调整块大小,所以必须明确告知用户这一点。 最后,总结步骤,强调备份的重要性,并给出验证命令,如使用dumpe2fs或tune2fs查看块大小是否修改成功。可能还需要提醒用户注意挂载选项和文件系统特性是否与新的块大小兼容。</think>在Linux系统中修改ext4文件系统的块大小(block size)需要遵循以下步骤和注意事项: --- ### 一、基础概念 1. **块大小定义** 块是ext4文件系统最小的存储单位,决定了: - 单个文件最小占用空间 - 文件系统最大容量 - 磁盘I/O性能 2. **常见规格** | 块大小 | 最大文件系统 | 适用场景 | |--------|--------------|----------------| | 1KB | 16TB | 小文件存储 | | 4KB | 256TB | 通用场景 | | 8KB | 512TB | 数据库/大文件 | --- ### 二、操作步骤 #### 1. 查看当前块大小 ```bash sudo dumpe2fs /dev/sdX | grep "Block size" ``` 或 ```bash sudo blockdev --getbsz /dev/sdX ``` #### 2. 卸载文件系统 ```bash sudo umount /dev/sdX ``` #### 3. 重新格式化(❗会清除数据) ```bash sudo mkfs.ext4 -b 4096 /dev/sdX ``` - `-b`参数指定块大小(单位:bytes) - 支持值:1024, 2048, 4096(默认), 8192, 16384, 32768, 65536 #### 4. 挂载验证 ```bash sudo mount /dev/sdX /mnt sudo tune2fs -l /dev/sdX | grep "Block size" ``` --- ### 三、关键注意事项 1. **数据丢失风险** 必须提前备份数据,格式化操作会完全清除分区内容 2. **性能权衡** - 大块(8KB+):提升大文件读写性能,但浪费存储空间 - 小块(1KB-4KB):节省空间,但增加元数据开销 3. **系统限制** - 块大小必须与存储设备的物理扇区对齐 - 最大块大小受内核版本限制(推荐≤64KB) --- ### 四、替代方案(无需格式化) 对于已存在的文件系统,可以通过调整以下参数优化性能: ```bash # 设置预读大小 sudo blockdev --setra 4096 /dev/sdX # 调整日志模式 sudo tune2fs -o journal_data_writeback /dev/sdX # 禁用访问时间记录 sudo tune2fs -O ^has_journal /dev/sdX ``` 建议在调整块大小前,使用`iozone`或`fio`工具进行基准测试验证效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值