I need to do it for more predictable benchmarking.
相关问题
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- how to get running process information in java?
- In what practical case bool(std::ifstream) != std:
- Error building gcc 4.8.3 from source: libstdc++.so
Short good enough answer: (copy paste friendly)
Explanation:
sync
: From the man page: flush file system buffers. Force changed blocks to disk, update the super block.echo 3 > /proc/sys/vm/drop_cache
: from the kernel docs this will cause the kernel to drop clean cachesblockdev --flushbufs /dev/sda
: from the man page: call block device ioctls [to] flush buffers.hdparm -F /dev/sda
: from the man page: Flush the on-drive write cache buffer (older drives may not implement this)Although the blockdev and hdparm commands look similar according to an answer above they issue different ioctls to the device.
Long probably better way:
(I'll assume that you have formatted the disk but you can adapt these commands if you want to write directly to the disk)
Run this only once before the 1st benchmark:
Run this every time you want to empty the caches:
Run this when you're done.
Explanation:
The HDD may have H/W caches that will not be cleared by the above commands. I'm writing and reading pseudo-random data hopping to fill them with garbage. How much data depends on how large the HDD cache may be. I'm using /dev/urandom because it's fast and we don't care about true randomness. I'm creating /mnt/test/temp-hddread.tmp from the start and use it every time I want to read enough random data. I'm creating and deleting /mnt/test/temp-hddwrite.tmp each time I want to write enough random data.
Credits
I've wrote this answer based on the best parts of the existing answers.
Unmounting and re-mounting the disk under test will reset all caches and buffers.
Sounds like you want the sync command, or the sync() function.
If you want disk cache flushing:
echo 3 | sudo tee /proc/sys/vm/drop_caches
Disk cache purging:
echo 3 | sudo tee /proc/sys/vm/drop_caches
Command documentation: https://www.kernel.org/doc/Documentation/sysctl/vm.txt
You can do it like this:
You may use strace to see that these are three different syscalls
Also, it may be desirable to turn off HDD cache using hdparm, not sure what thing you benchmarking.
In any way, you cannot prevent HDD to cache last 64/32/16 MB of recently used data. In order to kill that cache, just write some amount of zeroes (and flush) + read some unrelated place from HDD. This is required since cache may be divided to read-part and write-part. After that you can benchmark HDD.