简体   繁体   English

写入后从磁盘读取而不是缓存

[英]Read from disk after write instead of cache

Here's the task I'm trying to perform on a linux host, with a C program: 这是我试图在Linux主机上执行的任务,使用C程序:

Write random data to the disk, call fysnc() to flush data to disk, then read back what was written from the disk to ensure the disk controller wrote the data correctly. 将随机数据写入磁盘,调用fysnc()将数据刷新到磁盘,然后读回from the disk写入from the disk以确保磁盘控制器正确写入数据。 The problem I am running into is that reads appear to be answered by server-side caching rather than from the device itself. 我遇到的问题是读取似乎是由服务器端缓存而不是设备本身来回答的。 Here's what I've already tried: 这是我已经尝试过的:

 1. O_DIRECT (a gigantic pain in the butt, abandoned)
 2. posix_fadvise(fd,0,0,POSIX_FADV_DONTNEED)
 3. posix_fadvise(fd,0,0,POSIX_FADV_NOREUSE)
 4. O_SYNC
 5. O_ASYNC

In every case, iostat shows 0 rrqm/s and thousands of write requests. 在每种情况下, iostat显示0 rrqm/s和数千个写请求。 I could be a woefully uninformed linux user, but it is my belief that if no IOs are shown in rrqm/s then reads are being answered by the OS cache instead of the device itself. 我可能是一个非常不知情的Linux用户,但我相信如果rrqm / s中没有显示IO,则操作系统缓存而不是设备本身会回答读取。

"Why not use iozone or iometer, or any of the billions of other tools that already stress disks?" “为什么不使用iozone或iometer,或任何数十亿已经给磁盘带来压力的工具呢?” Well, to be honest, if HP-UX's HAZARD worked on anything except HP-UX, I would, but nothing else comes close to what hazard can do, so I'm making my own. 嗯,说实话,如果HP-UX的HAZARD可以处理除HP-UX之外的任何事情,我会这么做,但没有其他任何事情可以做到什么危险,所以我自己做了。

You need to do the equivalent of the following shell commands: 您需要执行以下shell命令的等效操作:

sync                               # Instruct all data to get flushed to disk
echo 3 > /proc/sys/vm/drop_caches  # Instruct VM system to clear caches

and then try reading the file again. 然后再次尝试读取该文件。

One way to do it from C would be something approximating: 从C中做到这一点的一种方法是近似的:

sync();
int fd = open("/proc/sys/vm/drop_caches", O_WRONLY|O_TRUNC)
write(fd, "3\n");
close(fd);

You should not go thru the file system to test a disk. 您不应该通过文件系统来测试磁盘。 You should read and write the raw partitions (eg /dev/sdc5 ) 您应该读取和写入原始分区(例如/dev/sdc5

On most current Linux systems and hardware, disks have a SMART interface. 在大多数当前的Linux系统和硬件上,磁盘具有SMART接口。 You should use it, see smartmontools and study its source code. 您应该使用它,查看smartmontools并研究其源代码。 (I guess that there are some ioctl(2) related to that.) (我猜有一些与之相关的ioctl(2) 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM