これは、なにをしたくて書いたもの?
Linuxのチューニングツールとして、tunedというものがあるというのを最近知り。
どうやらUbuntu Linuxでもインストールできそうだったので、どういったものなのか見てみることにしました。
tuned : Focal (20.04) : Ubuntu
tuned
tunedのサイトを見てみると、
Tunning Profile Delivery Mechanism for Linux
と書かれています。
udev
デバイスマネージャーを使ってデバイスを監視し、選択したプロファイルに従ってシステム設定を調整するものだとか。
プロファイルにはいくつかの種類があり、
- 高スループット
- 低レイテンシ
- 少電力
などがあります。
ユースケースに合わせてプロファイルを選択することで、システムの調整ができます、と。
プロファイルは、既存のプロファイルを拡張する形で作成することができ、また独自のプロファイルも作成することが
できます。
ドキュメントとしては、このあたりを見ると良さそうです。
https://github.com/redhat-performance/tuned/tree/master/doc/manual/
第2章 Tuned の使用 Red Hat Enterprise Linux 8 | Red Hat Customer Portal
その他、参考に。
Tuned: helper for system tuning
Linux Performance Optimization – Supercharged Computing
では、試してみるとしましょう。
環境
今回の環境ですが、Vagrantを使った仮想マシンを使って行いたいと思います。
$ vagrant -v Vagrant 2.2.10
Boxは、こちらを利用。Ubuntu Linux 20.04 LTSです。
Vagrant box generic/ubuntu2004 - Vagrant Cloud
仮想マシンの情報です。
$ uname -a Linux ubuntu2004.localdomain 5.4.0-52-generic #57-Ubuntu SMP Thu Oct 15 10:57:00 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.1 LTS Release: 20.04 Codename: focal $ grep -iE 'processor|mhz' /proc/cpuinfo processor : 0 model name : Intel Core Processor (Haswell, no TSX, IBRS) cpu MHz : 2494.222 processor : 1 model name : Intel Core Processor (Haswell, no TSX, IBRS) cpu MHz : 2494.222 $ free -h total used free shared buff/cache available Mem: 1.9Gi 109Mi 1.5Gi 2.0Mi 331Mi 1.7Gi Swap: 1.9Gi 0B 1.9Gi $ df -hT Filesystem Type Size Used Avail Use% Mounted on udev devtmpfs 951M 0 951M 0% /dev tmpfs tmpfs 199M 684K 199M 1% /run /dev/vda3 ext4 124G 2.7G 115G 3% / tmpfs tmpfs 994M 0 994M 0% /dev/shm tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/vda1 ext4 456M 196M 227M 47% /boot tmpfs tmpfs 199M 0 199M 0% /run/user/1000
今回は、この環境で試していこうと思います。
情報を見ていると、カーネルパラメーターが変更されそうな感じだったので、現在の情報を記録。
$ sudo sysctl -a > sysctl_default.txt
tunedをインストールする
こちらを参考に。
Ubuntu Linuxなので、apt
でインストールします。
$ sudo apt install tuned
tuned 2.10.0がインストールされました。
$ tuned -v tuned 2.10.0
この時点で、すでにdaemonが起動しています。
$ sudo systemctl status tuned ● tuned.service - Dynamic System Tuning Daemon Loaded: loaded (/lib/systemd/system/tuned.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-10-24 09:30:09 UTC; 28s ago Docs: man:tuned(8) man:tuned.conf(5) man:tuned-adm(8) Main PID: 1180 (tuned) Tasks: 4 (limit: 2281) Memory: 13.1M CGroup: /system.slice/tuned.service └─1180 /usr/bin/python3 -Es /usr/sbin/tuned -l -P Oct 24 09:30:08 ubuntu2004.localdomain systemd[1]: Starting Dynamic System Tuning Daemon... Oct 24 09:30:09 ubuntu2004.localdomain systemd[1]: Started Dynamic System Tuning Daemon.
ちょっと、カーネルパラメーターを保存して
$ sudo sysctl -a > sysctl_tuned_installed.txt
素の状態の時と比較するおt,この時点ですでに変わっています。
$ diff sysctl_default.txt sysctl_tuned_installed.txt 44c44 < fs.dentry-state = 45146 30194 45 0 1131 0 --- > fs.dentry-state = 51738 36463 45 0 2364 0 48,50c48,50 < fs.file-nr = 960 0 9223372036854775807 < fs.inode-nr = 44453 663 < fs.inode-state = 44453 663 0 0 0 0 0 --- > fs.file-nr = 1216 0 9223372036854775807 > fs.inode-nr = 49349 663 > fs.inode-state = 49349 663 0 0 0 0 0 127c127 < kernel.ns_last_pid = 975 --- > kernel.ns_last_pid = 2179 162c162 < kernel.random.entropy_avail = 1881 --- > kernel.random.entropy_avail = 1792 166c166 < kernel.random.uuid = 126553f1-f399-4311-83ab-1a60d93faa63 --- > kernel.random.uuid = dea4cf67-ec0b-4df0-915b-0ea02c7769f4 178c178 < kernel.sched_domain.cpu0.domain0.max_newidle_lb_cost = 8746 --- > kernel.sched_domain.cpu0.domain0.max_newidle_lb_cost = 13828 186c186 < kernel.sched_domain.cpu1.domain0.max_newidle_lb_cost = 32527 --- > kernel.sched_domain.cpu1.domain0.max_newidle_lb_cost = 12565 191c191 < kernel.sched_min_granularity_ns = 1500000 --- > kernel.sched_min_granularity_ns = 10000000 200c200 < kernel.sched_wakeup_granularity_ns = 2000000 --- > kernel.sched_wakeup_granularity_ns = 15000000 739c739 < net.ipv6.conf.eth0.disable_ipv6 = 0 --- > net.ipv6.conf.eth0.disable_ipv6 = 1 787c787 < net.ipv6.conf.eth1.disable_ipv6 = 0 --- > net.ipv6.conf.eth1.disable_ipv6 = 1 994c994 < vm.dirty_ratio = 20 --- > vm.dirty_ratio = 30 1025c1025 < vm.swappiness = 60 --- > vm.swappiness = 30
設定ファイルは、/etc/tuned
にあるようです。
$ ll /etc/tuned total 44 drwxr-xr-x 2 root root 4096 Oct 24 09:30 ./ drwxr-xr-x 100 root root 4096 Oct 24 09:30 ../ -rw-r--r-- 1 root root 14 Oct 24 09:30 active_profile -rw-r--r-- 1 root root 1111 Jul 4 2018 bootcmdline -rw-r--r-- 1 root root 148 Jul 4 2018 cpu-partitioning-variables.conf -rw-r--r-- 1 root root 5 Oct 24 09:30 profile_mode -rw-r--r-- 1 root root 59 Jul 4 2018 realtime-variables.conf -rw-r--r-- 1 root root 59 Jul 4 2018 realtime-virtual-guest-variables.conf -rw-r--r-- 1 root root 59 Jul 4 2018 realtime-virtual-host-variables.conf -rw-r--r-- 1 root root 111 Jul 4 2018 sap-hana-vmware-variables.conf -rw-r--r-- 1 root root 1197 Jul 4 2018 tuned-main.conf
メインの設定ファイル、/etc/tuned/tuned-main.conf
の中身。
$ grep -vE '^#|^$' /etc/tuned/tuned-main.conf daemon = 1 dynamic_tuning = 1 sleep_interval = 1 update_interval = 10 recommend_command = 1 reapply_sysctl = 1 default_instance_priority = 0 udev_buffer_size = 1MB
dynamic_tuning
を1
にすると、統計情報を元に自動的にチューニングをするようです。
2.4. Tuned の静的および動的のチューニング Red Hat Enterprise Linux 8 | Red Hat Customer Portal
Red Hat Enterprise LinuxやCentOSではデフォルトで無効になっているようですが、Ubuntu Linuxだと有効になっていますね。
現在のプロファイルを確認してみます。
$ tuned-adm active Current active profile: virtual-guest
virtual-guest
でした。仮想マシン(ゲスト側)に合わせているようです。
/etc/tuned/active_profile
にも、同じ内容が設定されています。
virtual-guest
インストールされているプロファイルを見てみる
どのようなプロファイルがあるのか、見てみましょう。
$ tuned-adm list Available profiles: - atomic-guest - Optimize virtual guests based on the Atomic variant - atomic-host - Optimize bare metal systems running the Atomic variant - balanced - General non-specialized tuned profile - cpu-partitioning - Optimize for CPU partitioning - default - Legacy default tuned profile - desktop - Optimize for the desktop use-case - desktop-powersave - Optmize for the desktop use-case with power saving - enterprise-storage - Legacy profile for RHEL6, for RHEL7, please use throughput-performance profile - laptop-ac-powersave - Optimize for laptop with power savings - laptop-battery-powersave - Optimize laptop profile with more aggressive power saving - latency-performance - Optimize for deterministic performance at the cost of increased power consumption - mssql - Optimize for MS SQL Server - 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 - oracle - Optimize for Oracle RDBMS - powersave - Optimize for low power consumption - realtime - Optimize for realtime workloads - realtime-virtual-guest - Optimize for realtime workloads running within a KVM guest - realtime-virtual-host - Optimize for KVM guests running realtime workloads - sap-hana - Optimize for SAP HANA - sap-hana-vmware - Optimize for SAP HANA running inside a VMware guest - sap-netweaver - Optimize for SAP NetWeaver - server-powersave - Optimize for server power savings - spindown-disk - Optimize for power saving by spinning-down rotational disks - 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: virtual-guest
これだけあるようです。oracleとか、mssqlとかありますね…。
よく使いそうなのは、throughput-performance
、desktop
、balanced
、powersave
などでしょうか。
サーバー用途だと、throughput-performance
がまずはベースでしょうね。
- throughput-performance - Broadly applicable tuning that provides excellent performance across a variety of common server workloads
ビルトインされているプロファイルが、どのような定義なのかは/usr/lib/tuned/[profile]
にあるファイルを見ればOKです。
現在適用されている、virtual-guest
プロファイルを見てみます。
$ grep -v '^[#$]' /usr/lib/tuned/virtual-guest/tuned.conf [main] summary=Optimize for running inside a virtual guest include=throughput-performance [sysctl] vm.dirty_ratio = 30 vm.swappiness = 30
include
があるので、throughput-performance
プロファイルをベースにしているようですね。
あと、カーネルパラメーターを2つ調整しています。
throughput-performance
プロファイルも見てみましょう。
$ grep -v '^[#$]' /usr/lib/tuned/throughput-performance/tuned.conf [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 [sysctl] kernel.sched_min_granularity_ns = 10000000 kernel.sched_wakeup_granularity_ns = 15000000 vm.dirty_ratio = 40 vm.dirty_background_ratio = 10 vm.swappiness=10
こちらでは、CPUやディスクに関する設定も入っています。
tunedをインストールした直後に取得したカーネルパラーメーターから、一部を見てみましょう。
kernel.sched_wakeup_granularity_ns
はthroughput-performance
の値が、vm.swappiness
はthroughput-performance
にも
$ grep -E 'vm.swappiness|kernel.sched_wakeup_granularity_ns' sysctl_tuned_installed.txt kernel.sched_wakeup_granularity_ns = 15000000 vm.swappiness = 30
こうやってセクション単位で設定していくようですが、sysctl
以外の部分がどう設定されているのか、今の知識だとわかりません…。
[cpu] [disk] [sysctl]
CPUだと、このあたりを追っていった方がいいんでしょうかねぇ。
CPU Performance Scaling — The Linux Kernel documentation
https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt
https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-system-cpu
プロファイルを変更する
プロファイルを変更してみましょう。tuned-adm
コマンドで変更します。今回は、throughput-performance
プロファイルを選択。
$ sudo tuned-adm profile throughput-performance
変わりました。
$ tuned-adm active Current active profile: throughput-performance
カーネルパラーメーターを取得して、インストール直後と変わったかどうか見てみましょう。
$ sudo sysctl -a > sysctl_tuned_throughput-performance.txt $ diff sysctl_tuned_installed.txt sysctl_tuned_throughput-performance.txt 44c44 < fs.dentry-state = 51738 36463 45 0 2364 0 --- > fs.dentry-state = 46389 31186 45 0 985 0 48,50c48,50 < fs.file-nr = 1216 0 9223372036854775807 < fs.inode-nr = 49349 663 < fs.inode-state = 49349 663 0 0 0 0 0 --- > fs.file-nr = 1024 0 9223372036854775807 > fs.inode-nr = 45398 690 > fs.inode-state = 45398 690 0 0 0 0 0 127c127 < kernel.ns_last_pid = 2179 --- > kernel.ns_last_pid = 1249 161,162c161,162 < kernel.random.boot_id = 94b7ed18-a5a1-44fd-9b70-6a38b44ee182 < kernel.random.entropy_avail = 1792 --- > kernel.random.boot_id = f8def128-d07a-48c3-b117-6fdba1eade41 > kernel.random.entropy_avail = 2218 166c166 < kernel.random.uuid = dea4cf67-ec0b-4df0-915b-0ea02c7769f4 --- > kernel.random.uuid = 98934bfd-49eb-4671-9a0a-7dd615818877 178c178 < kernel.sched_domain.cpu0.domain0.max_newidle_lb_cost = 13828 --- > kernel.sched_domain.cpu0.domain0.max_newidle_lb_cost = 8976 186c186 < kernel.sched_domain.cpu1.domain0.max_newidle_lb_cost = 12565 --- > kernel.sched_domain.cpu1.domain0.max_newidle_lb_cost = 40237 994c994 < vm.dirty_ratio = 30 --- > vm.dirty_ratio = 40 1025c1025 < vm.swappiness = 30 --- > vm.swappiness = 10
vm.swappiness
などが、throughput-performance
プロファイルのものに変わっています。OKですね。
ところで、この環境での推奨はrecommend
で確認できるのですが、当然ながらvirtual-guest
プロファイルです。
$ tuned-adm recommend virtual-guest
カスタムプロファイルを作ってみる
最後に、自分でプロファイルを作ってみます。カスタムプロファイルですね。
このあたりを参考に。
3.8. 新しい Tuned プロファイルの作成 Red Hat Enterprise Linux 8 | Red Hat Customer Portal
/etc/tuned
ディレクトリ配下に、これから作るプロファイル用のディレクトリを作成します。
$ sudo mkdir /etc/tuned/my-profile
このディレクトリ内に、tuned.conf
ファイルを作り、設定を記載します。
/etc/tuned/my-profile/tuned.conf
[main] summary=My tuned profile include=throughput-performance [sysctl] vm.swappiness = 20 net.ipv4.tcp_max_syn_backlog=256
今回はthroughput-performance
プロファイルをベースに、2つほどカーネルパラーメーターを変更してみます。
この時点で、tunedにプロファイルとして認識されます。
$ sudo tuned-adm list Available profiles: - atomic-guest - Optimize virtual guests based on the Atomic variant - atomic-host - Optimize bare metal systems running the Atomic variant - balanced - General non-specialized tuned profile - cpu-partitioning - Optimize for CPU partitioning - default - Legacy default tuned profile - desktop - Optimize for the desktop use-case - desktop-powersave - Optmize for the desktop use-case with power saving - enterprise-storage - Legacy profile for RHEL6, for RHEL7, please use throughput-performance profile - laptop-ac-powersave - Optimize for laptop with power savings - laptop-battery-powersave - Optimize laptop profile with more aggressive power saving - latency-performance - Optimize for deterministic performance at the cost of increased power consumption - mssql - Optimize for MS SQL Server - my-profile - My tuned profile - 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 - oracle - Optimize for Oracle RDBMS - powersave - Optimize for low power consumption - realtime - Optimize for realtime workloads - realtime-virtual-guest - Optimize for realtime workloads running within a KVM guest - realtime-virtual-host - Optimize for KVM guests running realtime workloads - sap-hana - Optimize for SAP HANA - sap-hana-vmware - Optimize for SAP HANA running inside a VMware guest - sap-netweaver - Optimize for SAP NetWeaver - server-powersave - Optimize for server power savings - spindown-disk - Optimize for power saving by spinning-down rotational disks - 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
ありました。プロファイル名はディレクトリ名、表示内容はsummary
の部分ですね。
- my-profile - My tuned profile
作成したプロファイルに切り替えます。
$ sudo tuned-adm profile my-profile
切り替わりました。
$ sudo tuned-adm active Current active profile: my-profile
確認。
$ sudo sysctl -a | grep -E 'vm.swappiness|syn_backlog' net.ipv4.tcp_max_syn_backlog = 256 vm.swappiness = 20
反映されていますね。
なお、今回のカスタムプロファイルを適用する前(throughput-performance
プロファイルの時)は、こんな感じでした。
$ sudo sysctl -a | grep -E 'vm.swappiness|syn_backlog' net.ipv4.tcp_max_syn_backlog = 128 vm.swappiness = 10
まとめ
tunedを試してみました。適用するだけなら、すごくお手軽に使えます。
一方で、OSにどう影響するかはとても気になるところなので、書かれている設定が、もう少しちゃんと読めるようには
なりたいものですねぇ。
いろいろ勉強になりそう。