【问题标题】:Writing a file quite quick, overwriting the file take much longer写入文件很快,覆盖文件需要更长的时间
【发布时间】:2011-01-12 02:35:48
【问题描述】:

我在 Linux Fedora Core 11 机器上看到了一些 PHP 脚本的性能问题,因此我运行了一些命令来寻找瓶颈。我注意到的一件事是编写文件非常快:

[root@localhost ~]# dd if=/dev/zero of=/root/myGfile bs=1024K count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 1.0817 s, 969 MB/s

但是覆盖它需要更长的时间;

[root@localhost ~]# dd if=/dev/zero of=/root/myGfile bs=1024K count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 23.0658 s, 45.5 MB/s

这是为什么呢? (我可以重复这些结果。)

【问题讨论】:

  • 你可以尝试在命令之间运行sync,看看有什么不同吗?

标签: linux performance io disk ext3


【解决方案1】:

第一次写入文件时,它会缓冲在系统内存中。

第二次写入文件时,文件被截断,由于某种原因,这会导致所有脏页都被写入磁盘。是的,这似乎很愚蠢:当文件被截断为零长度时,为什么还要写出文件数据?

您可以通过使第二个dd 仅写入(例如,4k 的数据)来证明这一点。它需要同样长的时间。

您还可以使用conv=notrunc 强制dd 不截断。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多