对于开源数据库核国产数据库来说,CPU的优化也是十分重要的。以前我们谈到的CPU优化都是如何监控CPU,找出CPU的问题,而很少谈到如何优化CPU。这实际上只是CPU优化的一个方面,我们只是从CPU的消耗者角度来谈CPU优化。CPU优化不仅仅可以从优化CPU的消耗的角度去考虑,也可以从CPU的调度与应用策略去考虑。根据不同的应用场景和需求,去优化CPU。今天我们从CPU的优化这个角度来讨论一下LINUX下的CPU优化。CPU优化分为直接优化与间接优化。CPU的直接优化是让物理CPU发挥出更强的能力,主要是针对CPU调度策略、CPU节能等方面进行调整。现在的服务器为了节约能源,可以开启节能模式,当系统负载不高的时候,让CPU以节能方式运行。这种节能模式可以有效的节约能源,不过在系统负载突然增加的时候,CPU的工作模式有一个切换的延时,会影响超高并发系统的QPS。现在很多企业都选择了关闭节能模式的方式来避免这种情况出现,甚至把关闭节能模式写入了系统上线的规范之中。实际上对于觉得大多数系统来说,节能模式带来的性能下降是很有限的,而节能模式能够带来的电能消耗的节约是十分明显的,将大多数服务器的节能模式打开有助于节约企业的电能开销。除了在bios中开启和关闭节能模式外,在Linux中也可以调整CPU的工作模式,这方面LINUX的不同版本存在较大的差异。在RHEL 6的时候,主要是用cpuspeed服务,这个服务针对主频超过1.5GHZ的CPU起作用,可以根据负载情况限制CPU的速度,从而达到节能的作用。这个服务对于某些高负载或者对响应时间要求比较高的应用会造成性能影响。如果遇到了类似的性能问题,可以通过关闭这个服务来解决这方面的性能问题。定位这个问题的办法也十分简单,那就是ps -ef或者top命令检查操作系统的时候,可以看到很多cpuspeed的进程。关闭这个服务的方法可以使用:systemctl cpuspeed.service stop还有一个会影响CPU的工作模式的服务是Irqbalance服务,可以使用:systemctl list-dependencies |grep irqbalance来查看你的操作系统是否支持安装或者启用这个服务。Irqbalance服务可以对系统终端进行优化,当系统处于性能模式的时候,自动将中断分散到多个CPU上,从而让CPU使用更为均衡。当系统处于节能模式下,irqbalance服务可以把所有中断都发送到某个CPU核上,从而让大多数CPU处于休眠状态,从而节约能源。因此你可以根据不同的应用场景来选择是否启用irqbalance。如果你发现irqbalance服务确实已经影响了PG数据库的性能,那么你可以选择关闭irqbalance。可以使用下列命令关闭irqbalance:systemctl cpuspeed.service stop针对CPU的直接优化方案其实并不多,作为现在的X86服务器来说,主要是针对运行模式进行调整。从REHL 6.3开始提供了一个CPU调优工具-tuned。tuned包含了一个服务和一个管理工具tuned-adm。通过tuned-adm可以调整CPU的应用策略。root@node123:/root||-->>tuned-adm list
Available profiles:
- balanced - General non-specialized tuned profile
- desktop - Optimize for the desktop use-case
- 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: throughput-performance
通过tuned-adm list命令来查看目前tuned支持的工作模式。从上面的命令可以看到当前的工作模式是throughput-performance,吞吐量优先的性能模式,这个模式时liunx 7的默认模式。其他模式包括:lBalanced:平衡模式,不做特殊的调整;它提供平衡的节能和性能。当前的LINUX内核中,启用了 CPU 和磁盘插件,并确保按需调节器处于活动状态(如果当前的 cpufreq 驱动程序支持)。该模式下, SATA适配器启用 ALPM 省电,并将链接电源管理策略设置为 medium_power。 CPU 的能源性能偏好设置为正常;lDesktop:桌面模式;基于平衡配置文件为桌面优化的配置文件。启用调度程序自动分组,以便更好地响应交互式应用程序;lLatency-performance:延时优先的性能模式,以增加功耗为代价提升性能;用于低延迟性能调整的配置文件。禁用节能机制。CPU 调控器设置为性能并锁定到低 C 状态(通过 PM QoS)。CPU 能源性能偏好设置为性能;lNetwork-latency:以网络延时优先的性能模式,以增加功耗为代价提升网络的性能;用于低延迟网络调整的配置文件。基于延迟性能配置文件。同时禁用透明大页、NUMA 平衡,并调整其他几个与网络相关的 sysctl 参数;lNetwork-throughput: 以网络吞吐量优先的优化模式,通常仅在较旧的CPU或40G+网络上使用;基于吞吐量性能配置文件,同时还增加了内核网络缓冲区;lPowersave:节模式;最大节能,此时启用 USB 自动挂起(如果环境变量 USB_AUTOSUSPEND 设置为 1),启用 SATA 主机适配器的 ALPM 节能并将链接电源管理策略设置为 min_power。本模式下还支持 WiFi 节能,为低唤醒系统启用多核节能调度程序,并确保按需调节器处于活动状态(如果当前 cpufreq 驱动程序支持)。将 CPU的 能源性能偏好设置为 powersave;lthroughput-performance:广泛适用的CPU优化模式,可跨各种常见服务器工作负载提供优异的性能;禁用节能机制并启用提高磁盘和网络 IO 吞吐量性能的 sysctl 设置。CPU 调控器设置为性能,CPU 能量性能偏好设置为性能。磁盘预读值增加;lvirtual-guest:虚拟机的优化模式;基于吞吐量性能配置文件为虚拟访客(客户机)优化的配置文件。减少了虚拟内存交换并增加了dirty_ratio 设置。lvirtual-host:KVM虚拟机的优化模式;基于吞吐量性能配置文件为虚拟主机优化的配置文件。配置中对脏页进行更积极的回写。root@node123:/root||-->>tuned-adm profile balanced
root@node123:/root||-->>tuned-adm active
Current active profile: balanced
root@node123:/root||-->>tuned-adm list
Available profiles:
- balanced - General non-specialized tuned profile
- desktop - Optimize for the desktop use-case
- 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
在balanced模式下,benchmark数据如下:
把模式设置为powersave,再来看看:
Benchmark数据降低到57万左右了。为什么会这样呢?我们看一下cpuinfo的信息,这个e8 6150的cpu的主频是2.7GHZ,此时的运行情况是,CPU0为2.7GHZ,CPU1为2.6GHZ。
而在性能模式时,每个核的主频都是2.7GHZ:
实际上tuned定义了一系列的调优参数,行程配置文件-profile,通过一条命令就可以把LINUX操作系统中的一系列十分复杂的CPU方面的优化操作完成了。我们在调整CPU的优化策略的时候,可以利用/usr/lib/tuned下的PROFILE作为基础,通过更为精细的参数控制来达到最佳的效果。
我们来看看刚才使用过的throughput-performance文件都调整了什么:
[main]
summary=Broadly applicable tuning that provides excellent performance across a variety of common server workloads
[cpu]
governor=performance
energy_perf_bias=performance
min_perf_pct=100
[disk]
readahead=>4096
[vm]
transparent_hugepages=never
[sysctl]
# ktune sysctl settings for rhel6 servers, maximizing i/o throughput
#
# Minimal preemption granularity for CPU-bound tasks:
# (default: 1 msec# (1 + ilog(ncpus)), units: nanoseconds)
kernel.sched_min_granularity_ns = 10000000
# SCHED_OTHER wake-up granularity.
# (default: 1 msec# (1 + ilog(ncpus)), units: nanoseconds)
#
# This option delays the preemption effects of decoupled workloads
# and reduces their over-scheduling. Synchronous workloads will still
# have immediate wakeup/sleep latencies.
kernel.sched_wakeup_granularity_ns = 15000000
# If a workload mostly uses anonymous memory and it hits this limit, the entire
# working set is buffered for I/O, and any more write buffering would require
# swapping, so it's time to throttle writes until I/O can catch up. Workloads
# that mostly use file mappings may be able to use even higher values.
#
# The generator of dirty data starts writeback at this percentage (system default
# is 20%)
vm.dirty_ratio = 40
# Start background writeback (via writeback threads) at this percentage (system
# default is 10%)
vm.dirty_background_ratio = 10
# PID allocation wrap value. When the kernel's next PID value
# reaches this value, it wraps back to a minimum PID value.
# PIDs of value pid_max or larger are not allocated.
#
# A suggested value for pid_max is 1024 * <# of="" cpu="" threads="" in="" system="">
# e.g., a box with 32 cpus, the default of 32768 is reasonable, for 64 cpus,
# 65536, for 4096 cpus, 4194304 (which is the upper limit possible).
#kernel.pid_max = 65536
# The swappiness parameter controls the tendency of the kernel to move
# processes out of physical memory and onto the swap disk.
# 0 tells the kernel to avoid swapping processes out of physical memory
# for as long as possible
# 100 tells the kernel to aggressively swap processes out of physical memory
# and move them to swap cache
vm.swappiness=10