I am working on an application which does sequentially write a large file (and does not read at all), and I would like to use posix_fadvise()
to optimize the filesy
Most of the posix_fadvise()
flags (eg POSIX_FADV_SEQUENTIAL
and POSIX_FADV_RANDOM
) are hints about readahead rather than writing.
There's some advice from Linus here and here about getting good sequential write performance. The idea is to break the file into large-ish (8MB) windows, then loop around doing:
write()
;sync_file_range(..., SYNC_FILE_RANGE_WRITE)
sync_file_range(..., SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER)
posix_fadvise(..., POSIX_FADV_DONTNEED)
This way you never have more than two windows worth of data in the page cache, but you still get the kernel writing out part of the pagecache to disk while you fill the next part.