I/Oの監視とチューニング

はじめに

第14章はじめに

このビデオでは、この章で取り上げるトピックの概要を説明します。


学習目標
この章の終わりまでに、次のことができるようになります。

I/Oアクティビティを監視することの重要性と、それがいつシステム パフォーマンスのボトルネックを引き起こすかを理解できます。
iostatを使用して、システムI/Oデバイスのアクティビティを監視できます。
iotopを使用して、絶えず更新されている現在のI/O使用率のテーブルを表示できます。
ioniceを使用して、特定のプロセスのI/Oスケジューリング クラスと優先順位の両方を設定できます。


I/Oの監視とチューニング

I/Oの監視とディスクのボトルネック
ディスク パフォーマンスの問題は、メモリ不足や不十分なネットワーク ハードウェアやチューニングなど、他の要因と強く結びついている可能性があります。解決は難しい場合があります。

原則として、CPUがI/Oの完了を待っていてアイドル状態にあるか、ネットワークがバッファがクリアされるのを待っている場合、システムはI/Oバウンド（i/o-bound）と見なされます。

ただし、間違う場合もあります。メモリ不足と思われるのは、I/Oが遅すぎるためです。読み書きに使用されているメモリ バッファがいっぱいになった場合、実際の問題はバッファが十分に速く満たされなかったり空にならないことなのに、メモリが問題のように見えることがあります。同様に、ネットワーク転送では、I/Oの完了を待っているためにネットワーク スループットが低下している可能性があります。

リアルタイムの監視とトレースは、ディスクのボトルネックを特定して軽減するために必要なツールです。ただし、めったに起きない問題や繰り返し発生することがない問題は、解決することが困難になる場合があります。

多くの関連変数があり、I/Oチューニングは複雑です。I/Oスケジューリングについても、のちほど説明します。


iostat
iostat は、システム上のI/Oデバイスのアクティビティを監視するための基本的なツールです。詳細なコンテンツをオプションで制御して、多くの情報を含むレポートを作成できます。

$ iostat [OPTIONS] [devices] [interval] [count]

このスクリーンショットは、iostatコマンドで何が得られるかを示しています。

iostat

CPU使用率の簡単な説明の後、I/Oの統計情報を示します。tps（1秒あたりのI/Oトランザクション。いくつかの論理的なリクエストを1つのリクエストにまとめることができます）、ブロックの単位時間ごとの読み書き数、このブロックはほとんどの場合512バイトのセクタです。そして、読み取りと書き込みの合計ブロック数です。

情報はディスク パーティションごとに分類されます（LVMがdmやデバイス マッパーや論理パーティションで使用されている場合）。

-kオプションを指定すると、少し異なる表示が作成されます。ブロック数ではなくKBで結果を表示します。-mを使用して、MBで結果を表示することもできます。

$ iostat -k

iostatに-kオプションを付ければ結果をKBで表示できます

もう1つの便利なオプションは-Nです。これは、提供されているスクリーンショットのように、デバイス名で表示します（または、少し異なる情報を得る場合は-dを使用）。

$ iostat -N

iostatと-Nオプションを使用して、デバイス名ごとに結果を表示します。

-xオプション（拡張用）を使用すると、より詳細なレポートを作成できます。

$ iostat -xk 

iostatを使用してより詳細なレポートを作成します。

🚩
使用率が100に近づくと、システムは飽和状態もしくはI/Oの限界状態になることに注意してください。


iotop
もう1つの非常に便利なユーティリティはiotopです。これはroot権限で実行する必要があります。topのように、現在のI/O使用量と定期的に更新される表を表示します。以下は、オプションなしでsudo iotopと入力して得られた内容のスクリーンショットです。

PRIOフィールド（プライオリティ）のbeとrtエントリは、ioniceセクションで説明します。beはベスト エフォート、rtはリアルタイムを表しています。

iotopの使用例

使用可能なオプションは、--helpオプションを実行すれば見ることができます。

$ iotop --help

iotopオプション

-oオプションを使用すると、I/O処理を実施しているプロセス／スレッドのみが表示され、混乱を避けるのに役立ちます。


ionice
ioniceユーティリティを使用すると、特定のプロセスのI/Oスケジューリング クラスと優先順位の両方を設定できます。形式は次のとおりです。

$ ionice [-c class] [-n priority] [-p pid ] [COMMAND [ARGS] ]

-p引数でpidを指定すると、指定されたプロセスに適用されます。それ以外の場合は、指定された引数を持つCOMMANDを実行するプロセスが対象となります。引数が指定されていない場合、ioniceは、次のように、現在のシェル プロセスのスケジューリング クラスと優先度を返します。

$ ionice
idle: prio 7

-cパラメータは、次の表に示すように、I/Oスケジューリング クラスを指定します。

表：I/Oスケジューリング クラス

スケジューリング クラス  -c値	意味
なしまたは不明（None/Unknown）　0　　デフォルト値を採用します。
リアルタイム（Real Time）　　1　　ディスクに最初にアクセスでき、他のプロセスに優先して処理されます。優先順位は、各プロセスが取得できるタイム スライスの大きさを定義します。
ベスト エフォート（Best Effort）　　2　　すべてのプログラムは、優先順位の設定に従ってラウンドロビン方式で実行されます。これがデフォルトです。
アイドル（Idle）　　3　　定義された期間、他のプログラムが要求しない限り、ディスクI/Oにアクセスしません。
 

ベスト エフォートおよびリアルタイムのクラスは、優先度を与える-n引数を指定します。これは0〜7の範囲で、0が最高の優先度です。以下に例を示します。

$ ionice -c 2 -n 3 -p 30078

🚩
ioniceは、CFQ I/Oスケジューラを使用している場合にのみ使えます。ただし、CFQ I/Oスケジューラはカーネル バージョン 5.0で削除されていることに注意してください。


デモ：iotopとiostatの使用方法

このビデオでは、入出力操作を監視するためのiotopとiostatの使用方法を示します。


演習

課題 14.1: bonnie++

🚩
以下のPDFドキュメントに埋め込まれた外部URLにアクセスする場合は、常に右クリックして新しいタブまたはウィンドウで開いてください。直接クリックしてURLを開こうとすると、コース ウィンドウ／タブが閉じます。

Exercise 14.1: bonnie++

bonnie++ is a widely available benchmarking program that tests and measures the performance of drives and filesystems. Itis descended from bonnie, an earlier implementation.

Results  can  be  read  from  the  terminal  window  or  directed  to  a  file,  and  also  to  acsvformat  (commaseparatedvalue).Companion programs,boncsv2html and boncsv2txt, can be used convert to html and plain text output formats.

We recommend you read the man page for bonnie++ before using as it has quite a few options regarding which tests to perform and how exhaustive and stressful they should be. A quick synopsis is obtained with:

$ bonnie++ --help
bonnie++: invalid option --'h'
usage:
bonnie++ [-d scratch-dir] [-c concurrency] [-s size(MiB)[:chunk-size(b)]]
[-n number-to-stat[:max-size[:min-size][:num-directories[:chunk-size]]]]
[-m machine-name] [-r ram-size-in-MiB]
[-x number-of-tests] [-u uid-to-use:gid-to-use] [-g gid-to-use]
[-q] [-f] [-b] [-p processes | -y] [-z seed | -Z random-file]
[-D]
Version: 1.96

A quick test can be obtained with a command like:

$  time sudo bonnie++ -n 0 -u 0 -r 100 -f -b -d /mnt

構文の内容は以下のとおりです。
•-n 0 means don’t perform the file creation tests.
•-u 0 means run as root.
•-r 100 means pretend you have 100 MB of RAM.
•-f means skip per character I/O tests.
•-b means do a fsync after every write, which forces flushing to disk rather than just writing to cache.
•-d /mnt just specifies the directory to place the temporary file created; make sure it has enough space, in this case 300 MB, available.

If you don’t supply a figure for your memory size, the program will figure out how much the system has and will create a testing file 2-3 times as large. We are not doing that here because it takes much longer to get a feel for things.

On RedHat

On anRHEL/CentOSsystem:

c7:/tmp> time sudo bonnie++ -n 0 -u 0 -r 100 -f -b -d /mnt

Using uid:0, gid:0.
Writing intelligently...done
Rewriting...done
Reading intelligently...done
start'em...done...done...done...done...done...
Version  1.97       ------Sequential Output------ --Sequential Input- --Random-
Concurrency   1     -Per Chr- --Block-- -Rewrite- -Per Chr- --Block-- --Seeks--
Machine        Size K/sec %CP K/sec %CP K/sec %CP K/sec %CP K/sec %CP  /sec %CP
c7             300M           286399  15 293907  14           +++++ +++  3143  12
Latency                          14us      15us                18us   59226us

1.97,1.97,c7,1,1544116090,300M,,,,286399,15,293907,14,,,+++++,+++,3143,12,,,,,,,,,,,,,,,,,,,\14us,15us,,18us,59226us,,,,,,

On Ubuntu

On anUbuntusystem, running as a virtual machine under hypervisor on the same physical machine:

$ sudo bonnie++ -n 0 -u 0 -r 100 -f -b -d /mnt

student@ubuntu: ̃$ sudo bonnie++ -n 0 -u 0 -r 100 -f -b -d /mnt
Using uid:0, gid:0.
Writing intelligently...done
Rewriting...doneReading intelligently...done
start'em...done...done...done...done...done...
Version  1.97       ------Sequential Output------ --Sequential Input- --Random-
Concurrency   1     -Per Chr- --Block-- -Rewrite- -Per Chr- --Block-- --Seeks--
Machine        Size K/sec %CP K/sec %CP K/sec %CP K/sec %CP K/sec %CP  /sec %CP
ubuntu         300M           230788  94 +++++ +++           +++++ +++ +++++ +++
Latency                       11210us     606us               631us    4365us

1.97,1.97,ubuntu,1,1544120438,300M,,,,230788,94,+++++,+++,,,+++++,+++,+++++,+++,,,,,,,,,,,,,,,,,,,\11210us,606us,,631us,4365us,,,,,,

You can clearly see the drop in performance.
Assuming you have saved the previous outputs as a file calledbonnie++.out, you can convert the output to html:
$ bon_csv2html < bonnie++.out > bonnie++.html
or to plain text with:
$ bon_csv2txt < bonnie++.out > bonnie++.txt

After reading the documentation, try longer and larger, more ambitious tests.  Try some of the tests we turned off.  If yoursystem is behaving well, save the results for future benchmarking comparisons when the system is sick.


課題 14.2: fs_mark

🚩
以下のPDFドキュメントに埋め込まれた外部URLにアクセスする場合は、常に右クリックして新しいタブまたはウィンドウで開いてください。直接クリックしてURLを開こうとすると、コース ウィンドウ／タブが閉じます。

Exercise 14.2: fs_mark

The fs_mark benchmark gives a low level bashing to file systems, using heavily asynchronous I/O across multiple directoriesand drives.It’s a rather old program written by Rick Wheeler that has stood the test of time.

It can be downloaded from http://sourceforge.net/projects/fsmark/ Once you have obtained the tarball,  you can unpack it and compile it with:

$ tar zxvf fs_mark-3.3.tgz
$ cd fs_mark
$ make

Read the README file as we are only going to touch the surface.
If the compile fails with an error like:

$ make

....
/usr/bin/ld: cannot find -lc

it is because you haven’t installed the static version of glibc. You can do this on Red Hat-based systems by doing:

$ sudo yum install glibc-static

and on SUSE-related systems with:

$ sudo zypper install glibc-devel-static

On Debian-based systems the relevant static library is installed along with the shared one so no additional package needs tobe sought.

On RedHat, Centos, or Fedora

•  On RHEL 8 glibc-static is not available without some unsupported tricks. However, on CentOS 8 it is includedin the PowerTools repo.

•  If you already have/etc/yum.repos.d/CentOS-PowerTools.repo on your system, then just make sure it has enabled=1 set in the file. Otherwise we have made a copy of this file and placed it in the SOLUTIONS section forthis course:

This file looks like:

CentOS-PowerTools.repo

[PowerTools]
name=CentOS-$releasever - PowerTools
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=PowerTools&infra=$infra#baseurl=http://mirror.centos.org/$contentdir/$releasever/PowerTools/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

•  On RHEL 8you can get away with adding this file to the repository directory, but when you install you will probably have to do it as:

# dnf install --nogpgcheck glibc-static

or  go  to  mirror  sites  and  find  the  latest  copies  of  the PowerTools repository  and  download  and  install  bothglibc-staticandlibxcrypt-static

•RHEL 8 believes static linking to be a security risk and they discourage the use of any static packages.  So youmay want to uninstall when done.

For a test we are going to create 2500 files, each 10 KB in size, and after each write we’ll perform anfsyncto flush out todisk. This can be done in the/tmpdirectory with the command:

$ fs_mark -d /tmp -n 2500 -s 10240

While this is running, gather extended iostat statistics with:

$ iostat -x -d /dev/sda 2 10

in another terminal window.

The numbers you should surely note are the number of files per second reported by fs_mark and the bandwidth percentage utilized as reported byiostat. If this is approaching 100 percent, you are I/O-bound.

Depending on what kind of filesystem you are using you may be able to get improved results by changing the mount options.For example, for ext3 or ext4 you can try:

$ mount -o remount,barrier=1 /tmp

or for ext4 you can try:

$ mount -o remount,journal_async_commit /tmp

See how your results change.

Please Note
•  In the above mount command you should use whatever partition /tmp  belongs to, such as /.

Note that these options may cause problems if you have a power failure, or other ungraceful system shutdown; i.e., there islikely to be a trade-off between stability and speed.

Documentation about some of the mount options can be found with the kernel source under Documentation/filesystems and the man page for mount.

CentOS-PowerTools.repo


知識チェック

「第14章 - I/Oの監視とチューニング」を完遂しました。おめでとうございます。このクイズに答えて、これまでに学んだ概念の理解度をチェックしてください。

クイズ開始

問題 14.1
プロセスのI/O優先度を設定するために使用されるツールは次のうちどれですか？

A. iotop
B. iostat
C. ionice

問題 14.2
現在I/Oを実行しているすべてのタスクのアクティビティを、継続的に表示するコマンドはどれですか。

A. sudo iotop -o
B. sudo iotop -u
C. sudo iotop -p 30376
D. sudo iotop -h

