检查和配置操作系统优化参数
在生产系统的 TiDB 中,建议对操作系统进行如下的配置优化:
- 关闭透明大页(即 Transparent Huge Pages,缩写为 THP)。数据库的内存访问模式往往是稀疏的而非连续的。当高阶内存碎片化比较严重时,分配 THP 页面会出现较高的延迟。
- 将存储介质的 I/O 调度器设置为 noop。对于高速 SSD 存储介质,内核的 I/O 调度操作会导致性能损失。将调度器设置为 noop 后,内核不做任何操作,直接将 I/O 请求下发给硬件,以获取更好的性能。同时,noop 调度器也有较好的普适性。
- 为调整 CPU 频率的 cpufreq 模块选用 performance 模式。将 CPU 频率固定在其支持的最高运行频率上,不进行动态调节,可获取最佳的性能。
采用如下步骤检查操作系统的当前配置,并配置系统优化参数:
执行以下命令查看透明大页的开启状态。
cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
注意
[always] madvise never表示透明大页处于启用状态,需要关闭。执行以下命令查看数据目录所在磁盘的 I/O 调度器。假设在 sdb、sdc 两个磁盘上创建了数据目录。
cat /sys/block/sd[bc]/queue/scheduler
noop [deadline] cfq noop [deadline] cfq
注意
noop [deadline] cfq表示磁盘的 I/O 调度器使用deadline,需要进行修改。执行以下命令查看磁盘的唯一标识
ID_SERIAL。udevadm info --name=/dev/sdb | grep ID_SERIAL
E: ID_SERIAL=36d0946606d79f90025f3e09a0c1f9e81 E: ID_SERIAL_SHORT=6d0946606d79f90025f3e09a0c1f9e81
注意
如果多个磁盘都分配了数据目录,需要多次执行以上命令,记录所有磁盘各自的唯一标识。
执行以下命令查看 cpufreq 模块选用的节能策略。
cpupower frequency-info --policy
analyzing CPU 0: current policy: frequency should be within 1.20 GHz and 3.10 GHz. The governor "powersave" may decide which speed to use within this range.
注意
The governor "powersave"表示 cpufreq 的节能策略使用 powersave,需要调整为 performance 策略。如果是虚拟机或者云主机,则不需要调整,命令输出通常为Unable to determine current policy。配置系统优化参数
方法一:使用 tuned(推荐)
执行
tuned-adm list命令查看当前操作系统的 tuned 策略。tuned-adm list
Available profiles: - balanced - General non-specialized tuned profile - desktop - Optimize for the desktop use-case - hpc-compute - Optimize for HPC compute workloads - latency-performance - Optimize for deterministic performance at the cost of increased power consumption - network-latency - Optimize for deterministic performance at the cost of increased power consumption, focused on low latency network performance - network-throughput - Optimize for streaming network throughput, generally only necessary on older CPUs or 40G+ networks - powersave - Optimize for low power consumption - throughput-performance - Broadly applicable tuning that provides excellent performance across a variety of common server workloads - virtual-guest - Optimize for running inside a virtual guest - virtual-host - Optimize for running KVM guests Current active profile: balanced
Current active profile: balanced表示当前操作系统的 tuned 策略使用 balanced,建议在当前策略的基础上添加操作系统优化配置。创建新的 tuned 策略。
mkdir /etc/tuned/balanced-tidb-optimal/ vi /etc/tuned/balanced-tidb-optimal/tuned.conf
[main] include=balanced [cpu] governor=performance [vm] transparent_hugepages=never [disk] devices_udev_regex=(ID_SERIAL=36d0946606d79f90025f3e09a0c1fc035)|(ID_SERIAL=36d0946606d79f90025f3e09a0c1f9e81) elevator=noop
include=balanced表示在现有的 balanced 策略基础上添加操作系统优化配置。应用新的 tuned 策略。
tuned-adm profile balanced-tidb-optimal
方法二:使用脚本方式。如果已经使用 tuned 方法,请跳过本方法。
执行
grubby命令查看默认内核版本。注意
需安装
grubby软件包。grubby --default-kernel
/boot/vmlinuz-3.10.0-957.el7.x86_64
执行
grubby --update-kernel命令修改内核配置。grubby --args="transparent_hugepage=never" --update-kernel /boot/vmlinuz-3.10.0-957.el7.x86_64
注意
--update-kernel后需要使用实际的默认内核版本。执行
grubby --info命令查看修改后的默认内核配置。grubby --info /boot/vmlinuz-3.10.0-957.el7.x86_64
注意
--info后需要使用实际的默认内核版本。index=0 kernel=/boot/vmlinuz-3.10.0-957.el7.x86_64 args="ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=en_US.UTF-8 transparent_hugepage=never" root=/dev/mapper/centos-root initrd=/boot/initramfs-3.10.0-957.el7.x86_64.img title=CentOS Linux (3.10.0-957.el7.x86_64) 7 (Core)
修改当前的内核配置立即关闭透明大页。
echo never > /sys/kernel/mm/transparent_hugepage/enabled echo never > /sys/kernel/mm/transparent_hugepage/defrag
配置 udev 脚本应用 IO 调度器策略。
vi /etc/udev/rules.d/60-tidb-schedulers.rules
ACTION=="add|change", SUBSYSTEM=="block", ENV{ID_SERIAL}=="36d0946606d79f90025f3e09a0c1fc035", ATTR{queue/scheduler}="noop" ACTION=="add|change", SUBSYSTEM=="block", ENV{ID_SERIAL}=="36d0946606d79f90025f3e09a0c1f9e81", ATTR{queue/scheduler}="noop"
应用 udev 脚本。
udevadm control --reload-rules udevadm trigger --type=devices --action=change
创建 CPU 节能策略配置服务。
cat >> /etc/systemd/system/cpupower.service << EOF [Unit] Description=CPU performance [Service] Type=oneshot ExecStart=/usr/bin/cpupower frequency-set --governor performance [Install] WantedBy=multi-user.target EOF
应用 CPU 节能策略配置服务。
systemctl daemon-reload systemctl enable cpupower.service systemctl start cpupower.service
执行以下命令验证透明大页的状态。
cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
执行以下命令验证数据目录所在磁盘的 I/O 调度器。
cat /sys/block/sd[bc]/queue/scheduler
[noop] deadline cfq [noop] deadline cfq
执行以下命令查看 cpufreq 模块选用的节能策略。
cpupower frequency-info --policy
analyzing CPU 0: current policy: frequency should be within 1.20 GHz and 3.10 GHz. The governor "performance" may decide which speed to use within this range.
执行以下命令修改 sysctl 参数。
echo "fs.file-max = 1000000">> /etc/sysctl.conf echo "net.core.somaxconn = 32768">> /etc/sysctl.conf echo "net.ipv4.tcp_tw_recycle = 0">> /etc/sysctl.conf echo "net.ipv4.tcp_syncookies = 0">> /etc/sysctl.conf echo "vm.overcommit_memory = 1">> /etc/sysctl.conf sysctl -p
执行以下命令配置用户的 limits.conf 文件。
cat << EOF >>/etc/security/limits.conf tidb soft nofile 1000000 tidb hard nofile 1000000 tidb soft stack 32768 tidb hard stack 32768 EOF




