Linux I/O Monitoring and Analyze: Difference between revisions

From WikiMLT
mNo edit summary
Line 2: Line 2:
There is a couple of tools available that allows you to monitor and analyze the disk I/O performance of your Linux driven system. Here are listed few of them and also how to install and examples of their basic usage.
There is a couple of tools available that allows you to monitor and analyze the disk I/O performance of your Linux driven system. Here are listed few of them and also how to install and examples of their basic usage.


== The <code>'''htop'''</code> command ==
== The <code class="noTypo">'''htop'''</code> command ==
If a newer version of <code>htop</code> is available at your distribution, there is available an additional tab that shows the <code>I/O</code> metrics of the instance - {{Media-cite|sf|1}}. Here is how to check the available version and install <code>htop</code>.
If a newer version of <code class="noTypo">htop</code> is available at your distribution, there is available an additional tab that shows the <code class="noTypo">I/O</code> metrics of the instance - {{Media-cite|sf|1}}. Here is how to check the available version and install <code class="noTypo">htop</code>.
<syntaxhighlight lang="shell" line="1" class="mlw-continue mlw-shell-gray margin-top-gray">
<syntaxhighlight lang="shell" line="1" class="mlw-continue mlw-shell-gray margin-top-gray">
sudo apt show htop 2>/dev/null | grep '^Version'
sudo apt show htop 2>/dev/null | grep '^Version'
Line 9: Line 9:
<syntaxhighlight lang="shell" line="1">
<syntaxhighlight lang="shell" line="1">
sudo apt install htop
sudo apt install htop
</syntaxhighlight>Install the latest version of <u><code>htop</code></u> <u>3.2.1-1</u> on <u>Ubuntu</u> Server <u>22.04.1</u> from a [https://packages.debian.org/bookworm/htop .deb package].<syntaxhighlight lang="shell" line="1">
</syntaxhighlight>Install the latest version of <u><code class="noTypo">htop</code></u> <u>3.2.1-1</u> on <u>Ubuntu</u> Server <u>22.04.1</u> from a [https://packages.debian.org/bookworm/htop .deb package].<syntaxhighlight lang="shell" line="1">
cd /tmp
cd /tmp
wget --no-check-certificate https://http.us.debian.org/debian/pool/main/h/htop/htop_3.2.1-1_amd64.deb
wget --no-check-certificate https://http.us.debian.org/debian/pool/main/h/htop/htop_3.2.1-1_amd64.deb
Line 23: Line 23:
}}
}}


== The <code>'''iostat'''</code> command ==
== The <code class="noTypo">'''iostat'''</code> command ==
...
...


== The <code>'''iotop'''</code> command ==
== The <code class="noTypo">'''iotop'''</code> command ==
...
...


== The <code>'''dstat'''</code> command ==
== The <code class="noTypo">'''dstat'''</code> command ==
<code>[https://manpages.ubuntu.com/manpages/jammy/man1/dstat.1.html dstat]</code> is a versatile tool for generating system resource statistics, it is a versatile replacement for <code>[[I/O Monitoring and Analyze#The%20vmstat%20command|vmstat]]</code>, <code>[[I/O Monitoring and Analyze#The%20iostat%20command|iostat]]</code> and <code>[[Linux Network Basics#The ifstat command|ifstat]]</code>. Dstat is unique in letting you aggregate block device throughput for a certain diskset or network bandwidth for a group of interfaces, ie. you can see the throughput for all the block devices that make up a single filesystem or storage system.<syntaxhighlight lang="shell" line="1" class="">
<code class="noTypo">[https://manpages.ubuntu.com/manpages/jammy/man1/dstat.1.html dstat]</code> is a versatile tool for generating system resource statistics, it is a versatile replacement for <code class="noTypo">[[I/O Monitoring and Analyze#The%20vmstat%20command|vmstat]]</code>, <code class="noTypo">[[I/O Monitoring and Analyze#The%20iostat%20command|iostat]]</code> and <code class="noTypo">[[Linux Network Basics#The ifstat command|ifstat]]</code>. Dstat is unique in letting you aggregate block device throughput for a certain diskset or network bandwidth for a group of interfaces, ie. you can see the throughput for all the block devices that make up a single filesystem or storage system.<syntaxhighlight lang="shell" line="1" class="">
sudo apt install dstat
sudo apt install dstat
</syntaxhighlight>There is an uncountable multitude of options and plugins available for <code>dstat</code>. Here is one example of usage - {{Media-cite|sf|3}} - where are applied the following options.
</syntaxhighlight>There is an uncountable multitude of options and plugins available for <code class="noTypo">dstat</code>. Here is one example of usage - {{Media-cite|sf|3}} - where are applied the following options.
* <code>-D sdc</code> - adds column that reports the I/O rate of <code>/dev/sdc</code>.
* <code class="noTypo">-D sdc</code> - adds column that reports the I/O rate of <code class="noTypo">/dev/sdc</code>.
* <code>-t</code>, <code>--time</code> - enable time/date output.  
* <code class="noTypo">-t</code>, <code class="noTypo">--time</code> - enable time/date output.  
* <code>-a</code>, <code>--all</code> - equals to <code>-cdngy</code> (<code>-c</code> cpu, <code>-d</code> disk; <code>-n</code> enable network stats; <code>-g</code> enable page stats; <code>-y</code> enable system stats).  
* <code class="noTypo">-a</code>, <code class="noTypo">--all</code> - equals to <code class="noTypo">-cdngy</code> (<code class="noTypo">-c</code> cpu, <code class="noTypo">-d</code> disk; <code class="noTypo">-n</code> enable network stats; <code class="noTypo">-g</code> enable page stats; <code class="noTypo">-y</code> enable system stats).  
* <code>--top-io</code> - show most expensive I/O process.  
* <code class="noTypo">--top-io</code> - show most expensive I/O process.  
* <code>--top-bio</code> - show most expensive block I/O process.  
* <code class="noTypo">--top-bio</code> - show most expensive block I/O process.  
* <code>--top-mem</code> - show process using the most memory.  
* <code class="noTypo">--top-mem</code> - show process using the most memory.  
<syntaxhighlight lang="shell" line="1" class="">
<syntaxhighlight lang="shell" line="1" class="">
sudo dstat -D sda -ta --top-io --top-bio --top-mem
sudo dstat -D sda -ta --top-io --top-bio --top-mem
Line 62: Line 62:
</syntaxhighlight>
</syntaxhighlight>


== The <code>'''vmstat'''</code> command ==
== The <code class="noTypo">'''vmstat'''</code> command ==
<code>[https://manpages.ubuntu.com/manpages/jammy/man8/vmstat.8.html vmstat]</code> - virtual memory statistics - reports information about processes, memory, paging, block IO, traps, disks and cpu activity. The first report produced gives averages since the last reboot. Additional reports give information on a sampling period of length delay. The process and memory reports are instantaneous in either case.  
<code class="noTypo">[https://manpages.ubuntu.com/manpages/jammy/man8/vmstat.8.html vmstat]</code> - virtual memory statistics - reports information about processes, memory, paging, block IO, traps, disks and cpu activity. The first report produced gives averages since the last reboot. Additional reports give information on a sampling period of length delay. The process and memory reports are instantaneous in either case.  


Here is how to get statistics about the block devices - <code>-d</code>, in megabytes <code>-Sm</code> <code>(1000000)</code>, or <code>-SM</code> <code>(1048576)</code> bytes.
Here is how to get statistics about the block devices - <code class="noTypo">-d</code>, in megabytes <code class="noTypo">-Sm</code> <code class="noTypo">(1000000)</code>, or <code class="noTypo">-SM</code> <code class="noTypo">(1048576)</code> bytes.
<syntaxhighlight lang="shell" line="1" class="mlw-continue">
<syntaxhighlight lang="shell" line="1" class="mlw-continue">
sudo vmstat -d -Sm
sudo vmstat -d -Sm
Line 90: Line 90:
</syntaxhighlight>
</syntaxhighlight>


== The <code>'''sar'''</code> command ==
== The <code class="noTypo">'''sar'''</code> command ==
The <code>[https://man.archlinux.org/man/sar.1.en sar]</code> command is part of the package <code>[https://manpages.ubuntu.com/manpages/jammy/man5/sysstat.5.html sysstat]</code>. It outputs the contents of selected cumulative activity counters in the  operating  system. The activities are collected by the <code>sysstat.service</code>. After installing the package we need to enable the collector service and wait until some statistics are collected.
The <code class="noTypo">[https://man.archlinux.org/man/sar.1.en sar]</code> command is part of the package <code class="noTypo">[https://manpages.ubuntu.com/manpages/jammy/man5/sysstat.5.html sysstat]</code>. It outputs the contents of selected cumulative activity counters in the  operating  system. The activities are collected by the <code class="noTypo">sysstat.service</code>. After installing the package we need to enable the collector service and wait until some statistics are collected.
<syntaxhighlight lang="shell" line="1" class="mlw-continue">
<syntaxhighlight lang="shell" line="1" class="mlw-continue">
sudo apt install sysstat
sudo apt install sysstat
Line 131: Line 131:


== Monitor the Files Size Changes Recursively ==
== Monitor the Files Size Changes Recursively ==
By the following command we can monitor which are the most written files for the past 10 minutes, larger than 800 Kb. This is done recursively for the directories <code>/var/lib</code> and <code>/var/log</code>.  The output of the command is shown at {{Media-cite|sf|3}}.
By the following command we can monitor which are the most written files for the past 10 minutes, larger than 800 Kb. This is done recursively for the directories <code class="noTypo">/var/lib</code> and <code class="noTypo">/var/log</code>.  The output of the command is shown at {{Media-cite|sf|3}}.


<syntaxhighlight lang="shell" line="1" class="multi-line-cmd">
<syntaxhighlight lang="shell" line="1" class="multi-line-cmd">
Line 144: Line 144:
}}
}}


Here is an advanced version :) which outputs also an additional data generated by <code>iostat</code>:
Here is an advanced version :) which outputs also an additional data generated by <code class="noTypo">iostat</code>:
{{collapse/begin}}
{{collapse/begin}}
<syntaxhighlight lang="shell" line="1" class="multi-line-cmd">
<syntaxhighlight lang="shell" line="1" class="multi-line-cmd">
Line 187: Line 187:
== References ==
== References ==


* Htop: [https://github.com/htop-dev/htop GitHub] | [https://htop.dev/index.html Home page [<code>htop</code><nowiki>]</nowiki>]
* Htop: [https://github.com/htop-dev/htop GitHub] | [https://htop.dev/index.html Home page [<code class="noTypo">htop</code><nowiki>]</nowiki>]
* Unix and Linux: [https://unix.stackexchange.com/q/55212/201297 How can I monitor disk I/O?]  [<code>sar</code>, <code>iostat</code>, <code>iotop</code>]
* Unix and Linux: [https://unix.stackexchange.com/q/55212/201297 How can I monitor disk I/O?]  [<code class="noTypo">sar</code>, <code class="noTypo">iostat</code>, <code class="noTypo">iotop</code>]
* Unix and Linux: [https://unix.stackexchange.com/q/89460/201297 How do I measure total disk I/O per hour?]  [<code>dstat</code>, <code>iostat</code>]
* Unix and Linux: [https://unix.stackexchange.com/q/89460/201297 How do I measure total disk I/O per hour?]  [<code class="noTypo">dstat</code>, <code class="noTypo">iostat</code>]
* Proxmox Forum: [https://forum.proxmox.com/threads/disk-prevent-from-spinning-down-because-of-pvestatd.53237/ Disk prevent from spinning down because of pvestatd] [<code>dstat</code>]
* Proxmox Forum: [https://forum.proxmox.com/threads/disk-prevent-from-spinning-down-because-of-pvestatd.53237/ Disk prevent from spinning down because of pvestatd] [<code class="noTypo">dstat</code>]
* [[PVE HDD Sleep (Suspend)]]
* [[PVE HDD Sleep (Suspend)]]
* [[Preload Tool for Better System Performance]]
* [[Preload Tool for Better System Performance]]

Revision as of 07:56, 31 August 2022

There is a cou­ple of tools avail­able that al­lows you to mon­i­tor and an­a­lyze the disk I/O per­for­mance of your Lin­ux dri­ven sys­tem. Here are list­ed few of them and al­so how to in­stall and ex­am­ples of their ba­sic us­age.

The htop com­mand

If a new­er ver­sion of htop is avail­able at your dis­tri­b­u­tion, there is avail­able an ad­di­tion­al tab that shows the I/O met­rics of the in­stance – Screen 1. Here is how to check the avail­able ver­sion and in­stall htop.

sudo apt show htop 2>/dev/null | grep '^Version'
sudo apt install htop

In­stall the lat­est ver­sion of htop 3.2.1–1 on Ubun­tu Serv­er 22.04.1 from a .deb pack­age.

cd /tmp
wget --no-check-certificate https://http.us.debian.org/debian/pool/main/h/htop/htop_3.2.1-1_amd64.deb
sudo apt install ./htop_3.2.1-1_amd64.deb

To be able to see all da­ta in most cas­es you need to run the tool as root:

sudo htop
Screen 1. The new I/O Met­rics tab of htop (v 3.2+). Use Tab to switch to the I/O tab, then use F6 to open the Sort by menu, and sort by IO_WRITE_RATE. The screen­shot is tak­en on Kali Lin­ux 2022. Screen 1. The new I/O Metrics tab of htop (v 3.2+). Use Tab to switch to the I/O tab, then use F6 to open the Sort by menu, and sort by IO_WRITE_RATE. The screenshot is taken on Kali Linux 2022.

The iostat com­mand

The iotop com­mand

The dstat com­mand

dstat is a ver­sa­tile tool for gen­er­at­ing sys­tem re­source sta­tis­tics, it is a ver­sa­tile re­place­ment for vmstat, iostat and ifstat. Dstat is unique in let­ting you ag­gre­gate block de­vice through­put for a cer­tain diskset or net­work band­width for a group of in­ter­faces, ie. you can see the through­put for all the block de­vices that make up a sin­gle filesys­tem or stor­age sys­tem.

sudo apt install dstat

There is an un­count­able mul­ti­tude of op­tions and plu­g­ins avail­able for dstat. Here is one ex­am­ple of us­age – Screen 3 – where are ap­plied the fol­low­ing op­tions.

  • -D sdc – adds col­umn that re­ports the I/O rate of /dev/sdc.
  • -t, --time – en­able time/​​​date out­put.
  • -a, --all – equals to -cdngy (-c cpu, -d disk; -n en­able net­work stats; -g en­able page stats; -y en­able sys­tem stats).
  • --top-io – show most ex­pen­sive I/O process.
  • --top-bio – show most ex­pen­sive block I/O process.
  • --top-mem – show process us­ing the most mem­o­ry.
sudo dstat -D sda -ta --top-io --top-bio --top-mem
Screen 3. Example of usage of the dstat command.
Screen 3. Ex­am­ple of us­age of the dstat com­mand. Screen 3. Example of usage of the dstat command.

Here is an­oth­er ex­am­ple that will out­put the av­er­age I/O rate per minute.

dstat -tdD total 60
----system--------dsk/total--
     time      |  read  writ
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
29-08 08:40:13 | 1138M 1782M
29-08 08:41:13 |  234k  744k
29-08 08:42:13 |  293k  171k
29-08 08:43:13 |  268k  113k
29-08 08:44:13 | 1100k  129k

The vmstat com­mand

vmstat – vir­tu­al mem­o­ry sta­tis­tics – re­ports in­for­ma­tion about process­es, mem­o­ry, pag­ing, block IO, traps, disks and cpu ac­tiv­i­ty. The first re­port pro­duced gives av­er­ages since the last re­boot. Ad­di­tion­al re­ports give in­for­ma­tion on a sam­pling pe­ri­od of length de­lay. The process and mem­o­ry re­ports are in­stan­ta­neous in ei­ther case.

Here is how to get sta­tis­tics about the block de­vices – -d, in megabytes -Sm (1000000), or -SM (1048576) bytes.

sudo vmstat -d -Sm
disk-   ------------reads--------------- --------------writes--------------- ------IO-----
        total  merged   sec tors      ms    total  merged   sectors       ms    cur    sec
loop0      84       0      2554       81        0       0         0        0      0      0
loop1      84       0      2382       36        0       0         0        0      0      0
loop2      52       0       856       39        0       0         0        0      0      0
loop3      60       0       814       43        0       0         0        0      0      0
loop4      52       0       764       14        0       0         0        0      0      0
loop5     539       0     11200      240        0       0         0        0      0      0
loop6      87       0      2498      105        0       0         0        0      0      0
loop7     493       0     34854      228        0       0         0        0      0      1
sda   1284880  157343  65276608   752987  3936160 2077213 135566208  4487853      0   4123
sdc    437039  119521   4760810  2594193    96132  145500  54786232 10747453      0   3024
sdd   2614304  458294  24597746  6017154    63873 1360249  19094048  7394053      0   5798
sdb    136351    1445  34564266   329162    25383    2759  47585536  4447925      0    424
sr0       120       0      897        14        0       0         0        0      0      0
loop8      49       0      752        24        0       0         0        0      0      0
loop9      88       0     3334        42        0       0         0        0      0      0
loop10     11       0       28         0        0       0         0        0      0      0

The sar com­mand

The sar com­mand is part of the pack­age sysstat. It out­puts the con­tents of se­lect­ed cu­mu­la­tive ac­tiv­i­ty coun­ters in the  op­er­at­ing  sys­tem. The ac­tiv­i­ties are col­lect­ed by the sysstat.service. Af­ter in­stalling the pack­age we need to en­able the col­lec­tor ser­vice and wait un­til some sta­tis­tics are col­lect­ed.

sudo apt install sysstat
sudo sed -i 's/ENABLED="false"/ENABLED="true"/' /etc/default/sysstat
sudo systemctl enable --now sysstat.service
systemctl cat sysstat-collect.timer
# /lib/systemd/system/sysstat-collect.timer
# /lib/systemd/system/sysstat-collect.timer
# (C) 2014 Tomasz Torcz <tomek@pipebreaker.pl>
#
# sysstat-12.5.2 systemd unit file:
#        Activates activity collector every 10 minutes

[Unit]
Description=Run system activity accounting tool every 10 minutes

[Timer]
OnCalendar=*:00/10

[Install]
WantedBy=sysstat.service
sar
Linux 5.15.39-4-pve (ubuntu-lxc-pve) 	08/28/22 	_x86_64_	(24 CPU)

20:41:48     LINUX RESTART	(24 CPU)

20:50:05        CPU     %user     %nice   %system   %iowait    %steal     %idle
21:00:00        all      1.66      0.00      0.26      0.02      0.00     98.06
21:10:10        all      2.66      0.00      0.27      0.03      0.00     97.03
21:20:13        all      1.92      0.00      0.29      0.02      0.00     97.76
Average:        all      2.09      0.00      0.27      0.03      0.00     97.62

Mon­i­tor the Files Size Changes Re­cur­sive­ly

By the fol­low­ing com­mand we can mon­i­tor which are the most writ­ten files for the past 10 min­utes, larg­er than 800 Kb. This is done re­cur­sive­ly for the di­rec­to­ries /var/lib and /var/log. The out­put of the com­mand is shown at Screen 3.

sudo watch -n 3 -d \
"find /var/lib /var/log -type f -size +800k -mmin -10 -printf '%-30s \t %t %p\n' | grep -Pv '\.(gz|[0-9])$'"
Screen 3. Use watch and find to monitor file change in real time.
Screen 3. Use watch and find to mon­i­tor file change in re­al time. Screen 3. Use watch and find to monitor file change in real time.

Here is an ad­vanced ver­sion :) which out­puts al­so an ad­di­tion­al da­ta gen­er­at­ed by iostat:

sudo watch -n 3 -d \
"find /var/lib /var/log -type f -size +800k -mmin -10 -printf '%-30s \t %t %p\n' | grep -Pv '\.(gz|[0-9])$';
 echo;
 iostat /dev/sda2"
#Out­put
Every 3.0s: find /var/lib /var/log -type f -size +800k -mmin -10 -printf '%s \t %t %p\n'...; iostat /dev/sda2...

4362053          Mon Aug 29 08:25:15.1573410540 2022 /var/lib/redis/dump.rdb
67108864         Mon Aug 29 08:29:34.6284410810 2022 /var/lib/mysql/undo_001
3276800          Mon Aug 29 08:29:36.1604593970 2022 /var/lib/mysql/#innodb_redo/#ib_redo10127
83886080         Mon Aug 29 08:29:33.6604295080 2022 /var/lib/mysql/mysql.ibd
31459279         Mon Aug 29 08:29:34.2284362990 2022 /var/lib/mysql/binlog.005694
5242880          Mon Aug 29 08:29:33.6284291260 2022 /var/lib/mysql/SCloud/oc_authtoken.ibd
6291456          Mon Aug 29 08:29:34.6284410810 2022 /var/lib/mysql/SCloud/oc_jobs.ibd
50331648         Mon Aug 29 08:29:34.6284410810 2022 /var/lib/mysql/undo_002
79691776         Mon Aug 29 08:29:34.6284410810 2022 /var/lib/mysql/ibdata1
11873325         Mon Aug 29 08:29:01.5600457430 2022 /var/log/syslog
31441772         Mon Aug 29 08:29:08.0921238280 2022 /var/log/auth.log
1504046          Mon Aug 29 08:25:15.2373420090 2022 /var/log/redis/redis-server.log
8388608          Mon Aug 29 08:27:04.5226471030 2022 /var/log/journal/e8dsfe54457bd2f6a44344e1/user-1000.journal
33554432         Mon Aug 29 08:29:08.5241289930 2022 /var/log/journal/e8dsfe54457bd2f6a44344e1/system.journal
1183995          Mon Aug 29 08:25:17.2973666000 2022 /var/log/apache2/wiki.error.log
1170720          Mon Aug 29 08:25:17.3013666480 2022 /var/log/apache2/wiki.access.log
1166389          Mon Aug 29 08:25:14.0613279700 2022 /var/log/apache2/cloud.access.log
1243871          Mon Aug 29 08:25:13.9613267760 2022 /var/log/apache2/bg.mirror.access.log

Linux 5.15.0-46-generic (szs.space) 	08/29/22 	_x86_64_	(16 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           2.50    0.00    0.73    1.06    0.00   95.71

Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
sda2             55.71       454.89       741.44       421.11   32361849   52747168   29958424

Ref­er­ences