Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

Linux 效能調優利器:Tuned 與 ktune 深入解析

效能調優,是每一個系統架構師與維運人員既頭疼又著迷的題目。從資料庫、伺服器、儲存、網路到應用系統,幾乎每一層都留有優化的空間與討論的餘地。這些年來,前輩們從開發流程、方法論、部署原則、監控測試到後期維運,做了不少累積。

在各種流派之中,至少有一點取得了廣泛共識:調優不存在「金手指」,也沒有「萬靈藥」。用今天比較官方的說法,優化是一項跨領域、跨學科,需要各方人才協作才能做好的系統工程。

落到資料庫與 Linux 作業系統上,事情就更複雜了。首先,不論是 Linux 本身還是跑在其上的應用,CPU、網路、儲存 IO 得一起配合,任何一塊短板都會觸發整體的「木桶效應」。其次,用途不同,每個系統的調優重點也不同:資料庫伺服器看重 IO 讀寫速度,某些 Web 服務則把吞吐量擺在第一。最後,各個優化面向彼此既制約又協作,單項再強,整體也未必好看。

1、Tuned 優化

對新手來說,Linux 應用環境調優門檻不低:牽涉的面向太多,CPU、儲存、快取策略、記憶體管理,樣樣都是參數。Linux 內建的預設值足以應付多數情境,但碰上高效能、高併發、高可用的特殊場景,就必須動手調整——而這對初學者並不友善。

Red Hat 針對這個痛點,把多年積累的調優經驗沉澱成一系列典型的 Profile。普通使用者借助 tuned 與 ktune,可以自由切換系統到不同的 Profile,套用對應的參數組合;進階使用者則能拿這些 Profile 當起點,增刪項目,打造屬於自己的調優方案。

另外,Profile 的組織方式非常樸素:一個目錄就是一個 Profile。要搬遷時,把目錄複製到新伺服器即可,策略跟著一起走。

目前 Tuned 提供的 Profile 大致如下:

Profile 名稱

應用場景

default

預設的節能(power-saving)配置,對系統影響最小,只啟用 CPU 與磁碟的 tuned 機制;

desktop-powersave

為桌面系統設計的節能 Profile,對 SATA 介面卡、CPU、網路與磁碟外掛套用 ALPM 策略;

server-powersave

為伺服器設計的節能 Profile,對 SATA 介面卡套用 ALPM、關閉 CD-ROM 輪詢,並啟用 CPU 與磁碟外掛;

laptop-ac-powersave

給插著電的筆記型電腦使用,中等強度節能,啟用 SATA 的 ALPM 與 WIFI 省電,CPU、網路與磁碟外掛都受影響;

laptop-battery-powersave

給靠電池供電的筆記型電腦使用,把所有節能配置全部打開,代價是 CPU、網路與 IO 上會有延遲;

throughput-performance

關閉節能,透過 sysctl 模組提升效能,改善網路與 IO 配置,切換到 deadline 排程器;

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

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

搭配 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.
# enabled=False
#
# Disk tuning section
#
[DiskTuning]
# enabled=False
# hdparm=False
# alpm=False
#
# Net monitoring section
#
[NetMonitor]
# enabled=False
#
# Net tuning section
#
[NetTuning]
# enabled=False
#
# CPU monitoring section
#
[CPUMonitor]
# enabled=False
#
# CPU tuning section
#
[CPUTuning]
# 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 目錄裡,各個 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

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

#SYSCTL="/etc/sysctl.ktune"

# Use *.conf files in the ktune configuration directory /etc/ktune.d.

USE_KTUNE_D="yes"

# This is the custom sysctl configuration file.

SYSCTL_POST="/etc/sysctl.d/* /etc/sysctl.conf"

# This is the I/O scheduler ktune will use.

ELEVATOR="deadline"

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

kernel.sched_min_granularity_ns = 10000000

kernel.sched_wakeup_granularity_ns = 15000000

vm.dirty_ratio = 40

4、小結

從使用者角度看,tuned 是一款門檻極低的調優工具,而且調優經驗容易沉澱、複用與分享——這或許才是它最大的價值。

Copyright © Linux教程網 All Rights Reserved