What posix_fadvise() args for sequential file write?

前端 未结 3 1521
不知归路
不知归路 2021-01-30 14:55

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

相关标签:
3条回答
  • 2021-01-30 15:34

    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 out window N with write();
    • Request asynchronous write-out of window N with sync_file_range(..., SYNC_FILE_RANGE_WRITE)
    • Wait for the write-out of window N-1 to complete with sync_file_range(..., SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER)
    • Drop window N-1 from the pagecache with 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.

    0 讨论(0)
  • 2021-01-30 15:42

    It all depends on the temporal locality of your data. If your application won't need the data soon after it was written, then you can go with POSIX_FADV_NOREUSE to avoid writing to the buffer cache (in a similar way as the O_DIRECT flag from open()).

    0 讨论(0)
  • 2021-01-30 15:43

    As far as writes go I think that you can just rely on the OSes disk IO scheduler to do the right thing.

    You should keep in mind that while posix_fadvise is there specifically to give the kernel hints about future file usage patterns the kernel also has other data to help it out.

    If you don't open the file for reading then it would only need to read blocks in when they were partially written. If you were to truncate the file to 0 then it doesn't even have to do that (you said that you were overwriting).

    0 讨论(0)
提交回复
热议问题