Posix fadvise and madvise

From Helpful
Jump to navigation Jump to search

posix_fadvice

posix_fadvice() gives a non-binding hint about how the calling applications expect to access a (portion of a) file, in case that helps the OS's planning, mostly in terms of

  • how much readahead to do
  • whether to keep the results in the page cache


The below is mostly selections from the man page:

int posix_fadvise(int fd, off_t offset, off_t len, int advice);
  • The advice applies to a (not necessarily existent) region
    • starting at offset and extending
    • for len bytes (or until the end of the file if len is 0)
  • within the file referred to by fd



  • POSIX_FADV_NORMAL - application has no advice (assumed default)
on linux, allows readahead
  • POSIX_FADV_SEQUENTIAL - "we expect to read this sequentially, if that helps"
on linux, amy double the regular readahead(verify)
may be swapped out after use(verify)
  • POSIX_FADV_RANDOM - "we don't expect to read this sequentially"
may disable readahead(verify)
may not swap out as enthusiastically(verify)
  • POSIX_FADV_NOREUSE - "we expect to read this just once"
"In kernels before 2.6.18, POSIX_FADV_NOREUSE had the same semantics as POSIX_FADV_WILLNEED. This was probably a bug; since kernel 2.6.18, this flag is a no-op."
  • POSIX_FADV_WILLNEED - "we expect to read the same data again soon"
  • POSIX_FADV_DONTNEED - "we expect to not read this data again soon"
should be called after you are done with the data, not before you read it
the point would often be not pushing more useful pages out of the page cache -- for larger files you probably want to do this regularly while reading it.


Notes:

  • linux has its own paging daemon which may be clever enough in general, so not all advice is equally useful.
  • in theory, opening a file with O_DIRECT acts similarly to POSIX_FADV_DONTNEED

See also:


madvise