聊聊Linux效能優化特性Tuned和ktune

realkid4發表於2015-04-08

 

效能優化是任何系統架構師和運維人員頭疼且痴迷的問題。從資料庫、伺服器、儲存、網路、應用系統等諸多系統層面,都存在進行優化的空間和探討的餘地。各種前輩也從系統開發過程流程、方法論、部署原則、監控測試和後期運維等諸多方面,進行了若干年的探討。

在各種優化流派中,起碼有一條是得到了廣泛共識:系統優化工作絕對不存在“金手指”和“萬靈藥”。按照現在比較官方的說法:優化是一項涉及多領域、多學科,需要各方面人才合作才能做好的一項系統工程。

落實在資料庫和Linux作業系統方面,這個問題就變的比較複雜。首先,無論是Linux還是執行其上的應用程式,CPU、網路、儲存IO等多個方面共同配合。一個方面的短板就會造成整體的“木桶效應”。其次,根據系統用途的差異,每個應用系統優化的方面是不同的。比如:資料庫伺服器優化的重點就是IO讀寫速度;一些Web應用伺服器優化的重點是提升吞吐量。最後,系統的各種優化方面是相互制約、相互協助的關係。單靠一方面強大的系統,整體是不會有很好的表現的。

 

1Tuned優化

 

對一般新手而言,Linux應用環境優化是很困難的。領域多,範圍廣:CPU、儲存、快取策略、記憶體管理等涉及的引數。Linux內部雖然有預設設定值,可以應對大多數的情況場景,但是針對一些特殊場景,例如高效能、高併發和高可用的系統,就需要我們進行調整。這種工作對於初學者而言,是比較困難的。

紅帽Linux針對這種情況,綜合多年的效能優化分析經驗,將若干典型的優化場景轉化為優化Profile。普通使用者藉助tunedktune特性工具,可以自由切換系統到不同的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-host

 

 

2TunedKtune服務結構

 

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命令,是可以通過chkconfigservice命令進行配置啟動的。

 

 

[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

 

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          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

 

 

3Profile內部資訊

 

下面我們深入單個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是一種非常簡單易用的調優工具。而且容易進行調優經驗的積累分享。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17203031/viewspace-1546834/,如需轉載,請註明出處,否則將追究法律責任。

相關文章