Linux性能优化特性Tuned和ktune

性能优化是任何系统架构师和运维人员头疼且痴迷的问题。从数据库、服务器、存储、网络、应用系统等诸多系统层面,都存在进行优化的空间和探讨的余地。各种前辈也从系统开发过程流程、方法论、部署原则、监控测试和后期运维等诸多方面,进行了若干年的探讨。

在各种优化流派中,起码有一条是得到了广泛共识:系统优化工作绝对不存在“金手指”和“万灵药”。按照现在比较官方的说法:优化是一项涉及多领域、多学科,需要各方面人才合作才能做好的一项系统工程。

落实在数据库和Linux操作系统方面,这个问题就变的比较复杂。首先,无论是Linux还是运行其上的应用程序,CPU、网络、存储IO等多个方面共同配合。一个方面的短板就会造成整体的“木桶效应”。其次,根据系统用途的差异,每个应用系统优化的方面是不同的。比如:数据库服务器优化的重点就是IO读写速度;一些Web应用服务器优化的重点是提升吞吐量。最后,系统的各种优化方面是相互制约、相互协助的关系。单靠一方面强大的系统,整体是不会有很好的表现的。

1、Tuned优化

对一般新手而言,Linux应用环境优化是很困难的。领域多,范围广:CPU、存储、缓存策略、内存管理等涉及的参数。Linux内部虽然有默认设置值,可以应对大多数的情况场景,但是针对一些特殊场景,例如高性能、高并发和高可用的系统,就需要我们进行调整。这种工作对于初学者而言,是比较困难的。

红帽Linux针对这种情况,综合多年的性能优化分析经验,将若干典型的优化场景转化为优化Profile。普通用户借助tuned和ktune特性工具,可以自由切换系统到不同的Profile模式,应用优化参数模式。同时,一些高级用户可以以这些Profile作为一个工作起始点,修改或者添加一些优化功能点,构建适合自己特性的优化Profile。

另外,Profile的组织方式非常简单,以一个目录的形式进行组织。如果需要进行迁移,只要将目录转移到一个新服务器目录,就可以实现优化策略迁移。

目前,Tuned优化Profile提供了如下几种策略:

Profile名称

应用场景

default

默认的节能(power-saving)配置。对系统影响最小的power-saving配置,只启动了CPU和磁盘的tuned机制;

desktop-powersave

针对桌面系统设计的节能Profile,对SATA适配器、CPU、网络和磁盘插件应用ALPM策略

server-powersave

针对服务器设计的节能Profile,对SATA适配器、禁用CD-ROM轮询、启动CPU和磁盘插件应用ALPM策略;

laptop-ac-powersave

针对运行于插入电源笔记本电脑应用,中等影响的节能策略,启用SATAALPM省电策略,启用WIFI省电策略,CPU、网络和磁盘插件收到节能影响;

laptop-battery-powersave

针对运行于插入电池笔记本电脑应用。激活应用所有的节能配置。对CPU、网络和IO方面,有延时缺点。

throughput-performance

禁用节能配置,启用sysctl模块设置来提升性能,改善网络和IO配置,切换到deadline switch

latency-performance

禁用节能配置,启用sysctl模块设置来提升性能,主要在网络IO方面提升;

在/etc/tune-profiles目录中,我们可以找到对应的配置文件夹。

[root@SICS-MIGPC-DB tune-profiles]# ls -l
total 68
-rw-r--r-- 1 root root  25 Apr  7 16:37 active-profile
drwxr-xr-x 2 root root 4096 Apr  3 13:11 default
drwxr-xr-x 2 root root 4096 Apr  3 13:11 desktop-powersave
drwxr-xr-x 2 root root 4096 Apr  3 13:11 enterprise-storage
drwxr-xr-x 2 root root 4096 Apr  3 13:42 enterprise-storage-no-thp
-rw-r--r-- 1 root root 9779 Jul 24  2013 functions
drwxr-xr-x 2 root root 4096 Apr  3 13:11 laptop-ac-powersave
drwxr-xr-x 2 root root 4096 Apr  3 13:11 laptop-battery-powersave
drwxr-xr-x 2 root root 4096 Apr  3 13:11 latency-performance
drwxr-xr-x 2 root root 4096 Apr  3 13:11 sap
drwxr-xr-x 2 root root 4096 Apr  3 13:11 server-powersave
drwxr-xr-x 2 root root 4096 Apr  3 13:11 spindown-disk
drwxr-xr-x 2 root root 4096 Apr  3 13:11 throughput-performance
drwxr-xr-x 2 root root 4096 Apr  3 13:11 virtual-guest
drwxr-xr-x 2 root root 4096 Apr  3 13:11 virtual-host2、Tuned和Ktune服务结构

Tuned本质上是一个Linux环境中的后台进程,在运行过程中依据配置内容监控调整系统。监控程序会根据监控的配置内容,监控进程会持续监控结果反馈,调整优化策略。Ktune进程是在Red Hat 5.3引入的一项架构服务内容,为一些特定的用例场景来优化进程。

[root@SICS-MIGPC-DB ~]# ps -ef | grep tune

root    18878 18856  0 13:36 pts/0    00:00:00 grep tune

root    31656    1  0 Apr07 ?        00:00:00 /usr/bin/python /usr/sbin/tuned -d -c /etc/tuned.conf

tuned程序是后台运行的守护进程,-c参数用于指定当前的监控参数。

[root@SICS-MIGPC-DB tune-profiles]# cat /etc/tuned.conf
# disable tuned
[main]
[DiskMonitor]
enabled=False
[DiskTuning]
enabled=False
[NetMonitor]
enabled=False
[NetTuning]
enabled=False
[CPUMonitor]
enabled=False
[CPUTuning]
enabled=False

作为后台的进程tuned命令,是可以通过chkconfig和service命令进行配置启动的。

[root@SICS-MIGPC-DB ~]# service tuned status

tuned (pid  31656) is running...

tuned-adm命令是管理tuned组件的主要接口。

[root@SICS-MIGPC-DB ~]# tuned-adm help

Usage: tuned-adm <command>

commands:

  help                          show this help message and exit

  list                          list all available and active profiles

  active                        show current active profile

  off                            switch off all tuning

  profile <profile-name>        switch to given profile

通过list命令,可以查看当前应用和备选的所有Profile内容。

[root@SICS-MIGPC-DB ~]# tuned-adm list
Available profiles:
- laptop-battery-powersave
- virtual-guest
- enterprise-storage
- desktop-powersave
- default
- throughput-performance
- spindown-disk
- sap
- enterprise-storage-no-thp
- laptop-ac-powersave
- virtual-host
- server-powersave
- latency-performance
Current active profile: enterprise-storage-no-thp

通过tuned-adm命令的profile参数,可以保证系统动态的进行调整参数。

[root@SICS-MIGPC-DB tune-profiles]# tuned-adm profile desktop-powersave
Reverting to saved sysctl settings: [  OK  ]
Calling '/etc/ktune.d/tunedadm.sh stop': [  OK  ]
Reverting to cfq elevator: dm-0 dm-1 dm-2 sda [  OK  ]
Stopping tuned: [  OK  ]
Switching to profile 'desktop-powersave'
Applying ktune sysctl settings:
/etc/ktune.d/tunedadm.conf: [  OK  ]
Calling '/etc/ktune.d/tunedadm.sh start': [  OK  ]
Applying sysctl settings from /etc/sysctl.conf
Starting tuned: [  OK  ]

从执行过程,可以看到系统不断的进行参数sysctl.conf调整应用。注意:/etc/tuned.conf内容也发生了变化。表明Profile的修改变化和Tuned行为变化。

[root@SICS-MIGPC-DB tune-profiles]# cat /etc/tuned.conf
#
# tuned configuration main service
#
[main]
# Interval for monitoring and tuning. Default is 10s.
# interval=10
#
# Disk monitoring section
#
[DiskMonitor]
# Enabled or disable the plugin. Default is True. Any other value
# disables it.
# enabled=False
#
# Disk tuning section
#
[DiskTuning]
# Enabled or disable the plugin. Default is True. Any other value
# disables it.
# enabled=False
# hdparm=False
# alpm=False
#
# Net monitoring section
#
[NetMonitor]
# Enabled or disable the plugin. Default is True. Any other value
# disables it.
# enabled=False
#
# Net tuning section
#
[NetTuning]
# Enabled or disable the plugin. Default is True. Any other value
# disables it.
# enabled=False
#
# CPU monitoring section
#
[CPUMonitor]
# Enabled or disable the plugin. Default is True. Any other value
# disables it.
# enabled=False
#
# CPU tuning section
#
[CPUTuning]
# Enabled or disable the plugin. Default is True. Any other value
# disables it.
# enabled=False

3、Profile内部信息

下面我们深入单个Profile的内部,探索一下内部的工作结构。在/etc/tune-profiles目录中,每个目录对应一个Profile内容。

[root@SICS-MIGPC-DB tune-profiles]# ls -l
total 68
-rw-r--r-- 1 root root  17 Apr  7 16:39 active-profile
drwxr-xr-x 2 root root 4096 Apr  3 13:11 default
drwxr-xr-x 2 root root 4096 Apr  3 13:11 desktop-powersave
drwxr-xr-x 2 root root 4096 Apr  3 13:11 enterprise-storage
drwxr-xr-x 2 root root 4096 Apr  3 13:42 enterprise-storage-no-thp
-rw-r--r-- 1 root root 9779 Jul 24  2013 functions
drwxr-xr-x 2 root root 4096 Apr  3 13:11 laptop-ac-powersave
drwxr-xr-x 2 root root 4096 Apr  3 13:11 laptop-battery-powersave
drwxr-xr-x 2 root root 4096 Apr  3 13:11 latency-performance
drwxr-xr-x 2 root root 4096 Apr  3 13:11 sap
drwxr-xr-x 2 root root 4096 Apr  3 13:11 server-powersave
drwxr-xr-x 2 root root 4096 Apr  3 13:11 spindown-disk
drwxr-xr-x 2 root root 4096 Apr  3 13:11 throughput-performance
drwxr-xr-x 2 root root 4096 Apr  3 13:11 virtual-guest
drwxr-xr-x 2 root root 4096 Apr  3 13:11 virtual-host

首先我们查看两个文件内容。Active-profile内容表明了当前正在使用的Profile名称。

[root@SICS-MIGPC-DB tune-profiles]# cat active-profile 

enterprise-storage-no-thp

而functions文件中定义了一些脚本中使用的函数内容。

[root@SICS-MIGPC-DB tune-profiles]# cat functions 

# vim: set syn=sh :

#

# This script contains functions which can be useful in ktune profile scripts.

#

#

# DISK tuning

#

DISKS_DEV="$(command ls -d1 /dev/[shv]d*[a-z] 2>/dev/null)"

DISKS_SYS="$(command ls -d1 /sys/block/{sd,cciss,dm-,vd}* 2>/dev/null)"

# SATA Aggressive Link Power Management

# usage: set_disk_alpm policy

set_disk_alpm() {

        policy=$1

        for host in /sys/class/scsi_host/*; do

                if [ -f $host/ahci_port_cmd ]; then

                        port_cmd=`cat $host/ahci_port_cmd`;

                        if [ $((0x$port_cmd & 0x240000)) = 0 -a -f $host/link_power_management_policy ]; then

                                echo $policy >$host/link_power_management_policy;

                        else

                                echo "max_performance" >$host/link_power_management_policy;

                        fi

                fi

        done

}

进入目录,每个Profile目录中的内容都近似。

[root@SICS-MIGPC-DB tune-profiles]# cd enterprise-storage-no-thp/

[root@SICS-MIGPC-DB enterprise-storage-no-thp]# ls -l

total 20

-rwxr-xr-x 1 root root  313 Apr  3 13:42 ktune.sh

-rw-r--r-- 1 root root 1239 Apr  3 13:37 ktune.sysconfig

-rw-r--r-- 1 root root 1562 Apr  3 13:37 sysctl.ktune

-rw-r--r-- 1 root root  191 Apr  3 13:37 tuned.conf

Ktune.sh文件是一个可执行脚本,是在ktune服务在启动过程中自动执行,来进行系统调整。

#!/bin/sh

. /etc/tune-profiles/functions –引用的functions文件

start() {

      set_cpu_governor performance

      set_transparent_hugepages never

      disable_disk_barriers

      multiply_disk_readahead 4

      return 0

}

stop() {

      restore_cpu_governor

      restore_transparent_hugepages

      enable_disk_barriers

      restore_disk_readahead

      return 0

}

process $@

ktune.sysconfig文件是作为ktune运行的配置参数。

# ktune service configuration
# This is the ktune sysctl file.  You can comment this out to prevent ktune
# from applying its sysctl settings.
#SYSCTL="/etc/sysctl.ktune"
# Use *.conf files in the ktune configuration directory /etc/ktune.d.
#  Value: yes|no,  default: yes
# It is useful if you want to load settings from additional files. Set this to
# no if you to prevent ktune from using these additional files.
USE_KTUNE_D="yes"
# This is the custom sysctl configuration file.  Any settings in this file will
# be applied after the ktune settings, overriding them.  Comment this out to
# use only the ktune settings.
SYSCTL_POST="/etc/sysctl.d/* /etc/sysctl.conf"
# This is the I/O scheduler ktune will use.  This will *not* override anything
# explicitly set on the kernel command line, nor will it change the scheduler
# for any block device that is using a non-default scheduler when ktune starts.
# You should probably leave this on "deadline", but "as", "cfq", and "noop" are
# also legal values.  Comment this out to prevent ktune from changing I/O
# scheduler settings.
ELEVATOR="deadline"
# These are the devices, that should be tuned with the ELEVATOR
ELEVATOR_TUNE_DEVS="/sys/block/{sd,cciss,dm-,vd}*/queue/scheduler"
Sysctl.ktune是指点sysctl设置,用来修改sysctl.conf文件。
# ktune sysctl settings for rhel6 servers with enterprise-class
# storage, 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

4、结论

从使用者的角度看,tuned是一种非常简单易用的调优工具。而且容易进行调优经验的积累分享。

相关推荐