142

运行时如何计算最佳块大小dd?我已经对其进行了一些研究,但没有发现任何暗示如何实现这一点的东西。

我的印象是更大的块大小会导致更快dd......这是真的吗?

我即将dd在一个运行 Intel Core i3 和 4GB DDR3 1333mhz RAM 的盒子上运行两个相同的 500gb Hitachi HDD,它们以 7200rpm 的速度运行,所以我试图弄清楚要使用的块大小。(我将从闪存驱动器启动 Ubuntu 10.10 x86,并从中运行它。)

4

5 回答 5

111

最佳块大小取决于各种因素,包括操作系统(及其版本)以及所涉及的各种硬件总线和磁盘。几个类 Unix 系统(包括 Linux 和至少一些 BSD 风格)st_blksize在 中定义了struct stat给出内核认为的最佳块大小的成员:

#include <sys/stat.h>
#include <stdio.h>

int main(void)
{
    struct stat stats;

    if (!stat("/", &stats))
    {
        printf("%u\n", stats.st_blksize);
    }
}

最好的方法可能是进行实验:复制具有各种块大小和时间的千兆字节。(记得在每次运行之前清除内核缓冲区缓存:)echo 3 > /proc/sys/vm/drop_caches

但是,根据经验,我发现足够大的块大小可以dd做得很好,与 4 KiB 和 64 KiB 相比,64 KiB 和 1 MiB 之间的差异很小。(虽然,不可否认,我已经有一段时间没有这样做了。我现在默认使用兆字节,或者只是dd选择大小。)

于 2011-05-28T13:21:15.033 回答
87

正如其他人所说,没有普遍正确的块大小。对于一种情况或一件硬件来说,什么是最佳的,对于另一种情况来说可能是非常低效的。此外,根据磁盘的健康状况,最好使用与“最佳”不同的块大小。

在现代硬件上相当可靠的一件事是,512 字节的默认块大小往往比更优化的替代方案慢一个数量级。如有疑问,我发现 64K 是一个相当可靠的现代默认设置。虽然 64K 通常不是最佳的块大小,但根据我的经验,它往往比默认的更有效。64K 在性能可靠方面也有相当可靠的历史:您可以在大约 2002 年的 Eug-Lug 邮件列表中找到一条消息,建议在此处使用 64K 的块大小:http: //www.mail-archive.com/eug- lug@efn.org/msg12073.html

为了确定最佳输出块大小,我编写了以下脚本,该脚本测试使用 dd 在一系列不同的块大小(从默认的 512 字节到最大 64M)写入 128M 测试文件。请注意,此脚本在内部使用 dd,因此请谨慎使用。

dd_obs_test.sh:

#!/bin/bash

# Since we're dealing with dd, abort if any errors occur
set -e

TEST_FILE=${1:-dd_obs_testfile}
TEST_FILE_EXISTS=0
if [ -e "$TEST_FILE" ]; then TEST_FILE_EXISTS=1; fi
TEST_FILE_SIZE=134217728

if [ $EUID -ne 0 ]; then
  echo "NOTE: Kernel cache will not be cleared between tests without sudo. This will likely cause inaccurate results." 1>&2
fi

# Header
PRINTF_FORMAT="%8s : %s\n"
printf "$PRINTF_FORMAT" 'block size' 'transfer rate'

# Block sizes of 512b 1K 2K 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M
for BLOCK_SIZE in 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864
do
  # Calculate number of segments required to copy
  COUNT=$(($TEST_FILE_SIZE / $BLOCK_SIZE))

  if [ $COUNT -le 0 ]; then
    echo "Block size of $BLOCK_SIZE estimated to require $COUNT blocks, aborting further tests."
    break
  fi

  # Clear kernel cache to ensure more accurate test
  [ $EUID -eq 0 ] && [ -e /proc/sys/vm/drop_caches ] && echo 3 > /proc/sys/vm/drop_caches

  # Create a test file with the specified block size
  DD_RESULT=$(dd if=/dev/zero of=$TEST_FILE bs=$BLOCK_SIZE count=$COUNT conv=fsync 2>&1 1>/dev/null)

  # Extract the transfer rate from dd's STDERR output
  TRANSFER_RATE=$(echo $DD_RESULT | \grep --only-matching -E '[0-9.]+ ([MGk]?B|bytes)/s(ec)?')

  # Clean up the test file if we created one
  if [ $TEST_FILE_EXISTS -ne 0 ]; then rm $TEST_FILE; fi

  # Output the result
  printf "$PRINTF_FORMAT" "$BLOCK_SIZE" "$TRANSFER_RATE"
done

在 GitHub 上查看

我只在 Debian (Ubuntu) 系统和 OSX Yosemite 上测试了这个脚本,所以它可能需要一些调整才能在其他 Unix 风格上工作。

默认情况下,该命令将在当前目录中创建一个名为dd_obs_testfile的测试文件。或者,您可以通过在脚本名称后提供路径来提供自定义测试文件的路径:

$ ./dd_obs_test.sh /path/to/disk/test_file

脚本的输出是测试块大小及其各自传输速率的列表,如下所示:

$ ./dd_obs_test.sh
block size : transfer rate
       512 : 11.3 MB/s
      1024 : 22.1 MB/s
      2048 : 42.3 MB/s
      4096 : 75.2 MB/s
      8192 : 90.7 MB/s
     16384 : 101 MB/s
     32768 : 104 MB/s
     65536 : 108 MB/s
    131072 : 113 MB/s
    262144 : 112 MB/s
    524288 : 133 MB/s
   1048576 : 125 MB/s
   2097152 : 113 MB/s
   4194304 : 106 MB/s
   8388608 : 107 MB/s
  16777216 : 110 MB/s
  33554432 : 119 MB/s
  67108864 : 134 MB/s

(注:传输率的单位因操作系统而异)

要测试最佳读取块大小,您可以使用或多或少相同的过程,但不是从 /dev/zero 读取并写入磁盘,而是从磁盘读取并写入 /dev/null。执行此操作的脚本可能如下所示:

dd_ibs_test.sh:

#!/bin/bash

# Since we're dealing with dd, abort if any errors occur
set -e

TEST_FILE=${1:-dd_ibs_testfile}
if [ -e "$TEST_FILE" ]; then TEST_FILE_EXISTS=$?; fi
TEST_FILE_SIZE=134217728

# Exit if file exists
if [ -e $TEST_FILE ]; then
  echo "Test file $TEST_FILE exists, aborting."
  exit 1
fi
TEST_FILE_EXISTS=1

if [ $EUID -ne 0 ]; then
  echo "NOTE: Kernel cache will not be cleared between tests without sudo. This will likely cause inaccurate results." 1>&2
fi

# Create test file
echo 'Generating test file...'
BLOCK_SIZE=65536
COUNT=$(($TEST_FILE_SIZE / $BLOCK_SIZE))
dd if=/dev/urandom of=$TEST_FILE bs=$BLOCK_SIZE count=$COUNT conv=fsync > /dev/null 2>&1

# Header
PRINTF_FORMAT="%8s : %s\n"
printf "$PRINTF_FORMAT" 'block size' 'transfer rate'

# Block sizes of 512b 1K 2K 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M
for BLOCK_SIZE in 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864
do
  # Clear kernel cache to ensure more accurate test
  [ $EUID -eq 0 ] && [ -e /proc/sys/vm/drop_caches ] && echo 3 > /proc/sys/vm/drop_caches

  # Read test file out to /dev/null with specified block size
  DD_RESULT=$(dd if=$TEST_FILE of=/dev/null bs=$BLOCK_SIZE 2>&1 1>/dev/null)

  # Extract transfer rate
  TRANSFER_RATE=$(echo $DD_RESULT | \grep --only-matching -E '[0-9.]+ ([MGk]?B|bytes)/s(ec)?')

  printf "$PRINTF_FORMAT" "$BLOCK_SIZE" "$TRANSFER_RATE"
done

# Clean up the test file if we created one
if [ $TEST_FILE_EXISTS -ne 0 ]; then rm $TEST_FILE; fi

在 GitHub 上查看

在这种情况下,一个重要的区别是测试文件是由脚本编写的文件。不要将此命令指向现有文件,否则现有文件将被零覆盖!

对于我的特定硬件,我发现 128K 是 HDD 上的最佳输入块大小,而 32K 是 SSD 上的最佳输入块大小。

虽然这个答案涵盖了我的大部分发现,但我已经多次遇到这种情况,我写了一篇关于它的博客文章:http: //blog.tdg5.com/tuning-dd-block-size/你可以找到更多细节在我在那里进行的测试中。

于 2015-01-05T02:01:42.953 回答
14

我发现我的最佳块大小为 8 MB(等于磁盘缓存?)我需要在创建磁盘的压缩映像之前擦除(有人说:清洗)磁盘上的空白空间。我用了:

cd /media/DiskToWash/
dd if=/dev/zero of=zero bs=8M; rm zero

我尝试了从 4K 到 100M 的值。

在让 dd 运行一段时间后,我将其杀死(Ctlr+C)并读取输出:

36+0 records in
36+0 records out
301989888 bytes (302 MB) copied, 15.8341 s, 19.1 MB/s

由于 dd 显示输入/输出速率(在这种情况下为 19.1MB/s),因此很容易看出您选择的值是比前一个值好还是更差。

我的分数:

bs=   I/O rate
---------------
4K    13.5 MB/s
64K   18.3 MB/s
8M    19.1 MB/s <--- winner!
10M   19.0 MB/s
20M   18.6 MB/s
100M  18.6 MB/s   

注意:要检查您的磁盘缓存/缓冲区大小,您可以使用sudo hdparm -i /dev/sda

于 2014-08-02T16:13:12.133 回答
5

这完全取决于系统。您应该尝试找到最佳解决方案。尝试从bs=8388608. (因为日立硬盘似乎有 8MB 缓存。)

于 2011-05-28T13:24:40.757 回答
1
  • 为了获得更好的性能,请使用 RAM 可以容纳的最大块大小(将向操作系统发送更少的 I/O 调用)
  • 为了获得更好的准确性和数据恢复,将块大小设置为输入的本机扇区大小

由于 dd 使用 conv=noerror,sync 选项复制数据,它遇到的任何错误都将导致块的其余部分被零字节替换。较大的块大小会更快地复制,但每次遇到错误时都会忽略块的其余部分。

资源

于 2013-11-03T13:12:47.497 回答