歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

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

針對運行於插入電源筆記本電腦應用,中等影響的節能策略,啟用SATA的ALPM省電策略,啟用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是一種非常簡單易用的調優工具。而且容易進行調優經驗的積累分享。

Copyright © Linux教程網 All Rights Reserved