为什么缓存读取比 hdparm --direct 中的磁盘读取慢?

为什么缓存读取比 hdparm --direct 中的磁盘读取慢?

我试图解释 hdparm 的结果:

janus@behemoth ~ $ sudo hdparm  -Tt --direct /dev/nvme0n1

/dev/nvme0n1:
 Timing O_DIRECT cached reads:   2548 MB in  2.00 seconds = 1273.69 MB/sec
 Timing O_DIRECT disk reads: 4188 MB in  3.00 seconds = 1395.36 MB/sec

我不明白缓存读取如何比直接磁盘读取慢。如果我删除 --direct,我会得到我所期望的结果:磁盘读取速度比缓存的读取速度慢:

janus@behemoth ~ $ sudo hdparm  -Tt /dev/nvme0n1

/dev/nvme0n1:
 Timing cached reads:   22064 MB in  2.00 seconds = 11042.86 MB/sec
 Timing buffered disk reads: 2330 MB in  3.00 seconds = 776.06 MB/sec

(尽管它现在说“缓冲磁盘读取”)。

有人可以向我解释一下发生了什么事吗?

答案1

每个hdparm手册页:

--direct

Use the kernel´s "O_DIRECT" flag when  performing  a  -t  timing
test.   This  bypasses  the  page cache, causing the reads to go
directly from the drive into hdparm's buffers,  using  so-called
"raw"  I/O.  In many cases, this can produce results that appear
much faster than the usual page cache method,  giving  a  better
indication of raw device and driver performance.

这就解释了为什么hdparm -t --direct可能比hdparm -t.它还说--direct仅适用于-t测试,不适用于-T不涉及磁盘的测试(见下文)。

-T 

Perform timings of cache reads for benchmark and comparison pur‐
poses.   For  meaningful  results,  this  operation  should   be  
repeated  2-3  times  on  an otherwise inactive system (no other
active processes) with at least a couple of  megabytes  of  free
memory.   This  displays  the speed of reading directly from the 
Linux buffer cache without disk  access.   This  measurement  is  
essentially  an  indication  of the throughput of the processor,
cache, and memory of the system under test.

我想-T通过读取磁盘的相同缓存部分来工作。但你--direct阻止了这一点。因此,从逻辑上讲,您应该得到-t --direct与相同的结果-T --direct

答案2

根据我的研究,--direct通过硬盘驱动器获取的任何数据至少发送一次。这意味着,如果系统缓存以 10GB/秒的速度获取数据,但较差的硬盘控制器以 400MB/秒的速度陷入困境,那么您将获得 400MB/秒的速度。

作为复习

  • -Tt --direct运行两次:

      1. 读取系统缓存,然后将其发送到硬盘驱动器控制器并返回。这对于测试 SATA 连接和最大缓冲区速度可能很有用。
      1. 读取没有缓冲区的磁盘。
  • -T直接读取系统缓存,测试处理器和raid驱动程序开销

  • -t在启用缓冲区的情况下读取磁盘

相关内容