Centos 檢查磁盤讀寫性能


啟動Tomcat發現deploy war的速度明顯變慢, 懷疑磁盤出問題

測試寫入

[tomcat@localhost ~]$ dd if=/dev/zero of=kwxgd bs=64k count=4k oflag=dsync
4096+0 records in
4096+0 records out
268435456 bytes (268 MB) copied, 127.514 s, 2.1 MB/s

測試讀取

dd if=kwxgd of=/dev/zero bs=64k count=4k iflag=direct

io狀態

[tomcat@localhost ~]$ iostat -x 1
Linux 2.6.32-358.el6.x86_64 (localhost.localdomain)     2015年07月02日  _x86_64_(24 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.77    0.00    0.12    0.00    0.00   99.11

Device:         rrqm/s   wrqm/s     r/s     w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.01    55.48    0.03    2.42     3.20   463.18   190.22     0.04   14.74   0.69   0.17

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.04    0.04    0.00   99.92

Device:         rrqm/s   wrqm/s     r/s     w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.00     0.00    1.00    0.00    16.00     0.00    16.00     0.01   11.00  11.00   1.10
Linux 2.6.32-358.el6.x86_64 (localhost.localdomain)     2015年07月02日  _x86_64_(24 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.77    0.00    0.12    0.00    0.00   99.11

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               2.45         3.20       463.21    6662512  964056792

rrqm/s: 每秒進行 merge 的讀操作數目。即 delta(rmerge)/s
wrqm/s:   每秒進行 merge 的寫操作數目。即 delta(wmerge)/s
r/s:           每秒完成的讀 I/O 設備次數。即 delta(rio)/s
w/s:       每秒完成的寫 I/O 設備次數。即 delta(wio)/s
rsec/s: 每秒讀扇區數。即 delta(rsect)/s
wsec/s:  每秒寫扇區數。即 delta(wsect)/s
rkB/s:   每秒讀K字節數。是 rsect/s 的一半,因為每扇區大小為512字節。(需要計算)
wkB/s: 每秒寫K字節數。是 wsect/s 的一半。(需要計算)
avgrq-sz: 平均每次設備I/O操作的數據大小 (扇區)。delta(rsect+wsect)/delta(rio+wio)
avgqu-sz: 平均I/O隊列長度。即 delta(aveq)/s/1000 (因為aveq的單位為毫秒)。
await: 平均每次設備I/O操作的等待時間 (毫秒)。即 delta(ruse+wuse)/delta(rio+wio)
svctm: 平均每次設備I/O操作的服務時間 (毫秒)。即 delta(use)/delta(rio+wio)
%util:    一秒中有百分之多少的時間用於 I/O 操作,或者說一秒中有多少時間 I/O 隊列是非空的。即 delta(use)/s/1000 (因為use的單位為毫秒)

 

硬盤性能測試
一、安裝hdparm

yum install hdparm -y

二、評估讀取
SSD 硬盤,請使用hdparm命令進行讀取測試。

hdparm -t /dev/xvda

SSH執行以上命令,可使用hdparm評估SSD的讀取速率。
“/dev/xvda”指的是對應磁盤的驅動號,請執行“fdisk -l”查看

 

 

# 測試隨機寫IOPS,運行以下命令:
fio -direct=1 -iodepth=128 -rw=randwrite -ioengine=libaio -bs=4k -size=1G -numjobs=1 -runtime=1000 -group_reporting -filename=iotest -name=Rand_Write_Testing

# 測試隨機讀IOPS,運行以下命令:
fio -direct=1 -iodepth=128 -rw=randread -ioengine=libaio -bs=4k -size=1G -numjobs=1 -runtime=1000 -group_reporting -filename=iotest -name=Rand_Read_Testing

# 測試順序寫吞吐量,運行以下命令:
fio -direct=1 -iodepth=64 -rw=write -ioengine=libaio -bs=1024k -size=1G -numjobs=1 -runtime=1000 -group_reporting -filename=iotest -name=Write_PPS_Testing

# 測試順序讀吞吐量,運行以下命令:
fio -direct=1 -iodepth=64 -rw=read -ioengine=libaio -bs=1024k -size=1G -numjobs=1 -runtime=1000 -group_reporting -filename=iotest -name=Read_PPS_Testing

 

下表以測試隨機寫IOPS的命令為例,說明命令中各種參數的含義。

-direct=1 表示測試時忽略I/O緩存,數據直寫。
-iodepth=128 表示使用AIO時,同時發出I/O數的上限為128。
-rw=randwrite 表示測試時的讀寫策略為隨機寫(random writes)。作其它測試時可以設置為:

randread(隨機讀random reads)
read(順序讀sequential reads)
write(順序寫sequential writes)
randrw(混合隨機讀寫mixed random reads and writes)

-ioengine=libaio 表示測試方式為libaio(Linux AIO,異步I/O)。應用程序使用I/O通常有兩種方式:

同步

同步的I/O一次只能發出一個I/O請求,等待內核完成才返回。這樣對於單個線程iodepth總是小於1,但是可以透過多個線程並發執行來解決。通常會用16−32根線程同時工作將iodepth塞滿。
異步

異步的I/O通常使用libaio這樣的方式一次提交一批I/O請求,然后等待一批的完成,減少交互的次數,會更有效率。

-bs=4k

表示單次I/O的塊文件大小為4 KB。未指定該參數時的默認大小也是4 KB。

測試IOPS時,建議將bs設置為一個比較小的值,如本示例中的4k。

測試吞吐量時,建議將bs設置為一個較大的值,如本示例中的1024k。
-size=1G 表示測試文件大小為1 GiB。
-numjobs=1 表示測試線程數為1。
-runtime=1000 表示測試時間為1000秒。如果未配置,則持續將前述-size指定大小的文件,以每次-bs值為分塊大小寫完。
-group_reporting 表示測試結果里匯總每個進程的統計信息,而非以不同job匯總展示信息。
-filename=iotest 指定測試文件的名稱,比如iotest。測試裸盤可以獲得真實的硬盤性能,但直接測試裸盤會破壞文件系統結構,請在測試前提前做好數據備份。
-name=Rand_Write_Testing 表示測試任務名稱為Rand_Write_Testing,可以隨意設定。

關於fdisk磁盤信息

Command (m for help): p
Disk /dev/sdc: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 33553920 bytes
Disklabel type: gpt
Disk identifier: 249AFED8-1A62-461E-9A07-23B686A34DF4

Command (m for help): n
Partition number (1-128, default 1): 
First sector (34-976773134, default 2048):

關於logical sector size 和 physical sector size的說明:  

  • The physical_block_size is the minimal size of a block the drive is able to write in an atomic operation.
  • The logical_block_size is the smallest size the drive is able to write (cf. the linux kernel documentation).

The logical sector size being smaller than the physical sector size is normal for most modern disks. This is simply how Advanced Format disks are most often implemented. Some external disks use the same (4096-byte) sector size for both physical and logical sectors, and I've heard that some high-end internal disks now do the same, but most disks these days are Advanced Format models with 512-byte logical sectors and 4096-byte physical sectors. There's nothing you can (or should try to) do about this.

關於First sector default 2048的說明:  

Most modern disk drives need a partition to be aligned on sector 2048 to avoid writes overlapping two sectors, but for a long time the sector 63 was used by the fdisk utility and distro's installers by default. This can cause severe performance issues on modern disks. Often they will try to cover up for it in firmware, which means the issue will be still there, but the pain will be just low enough to not really find out whats wrong. 

The best solution is to use either GPT/EFI partitions or switch to using LVM since partitions have been an outdated concept for many years now.
On modern distros like Ubuntu the fdisk utility is patched to default to 2048 sectors. You need to use sector 63? In fact it does not even allowed doing it wrong anymore


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM