Posix fadvise and madvise

From Helpful
Revision as of 13:42, 23 July 2023 by Helpful (talk | contribs) (Created page with " posix_fadvice() gives a non-binding hint about how we expect to access a (portion of a) file, in case that helps its 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: <syntaxhighlight lang="c"> int posix_fadvise(int fd, off_t offset, off_t len, int advice); </syntaxhighlight> : The advice applies to a (not necessarily existent) region :: starting at offset...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


posix_fadvice() gives a non-binding hint about how we expect to access a (portion of a) file, in case that helps its 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

. The advice is not binding; it merely constitutes an expectation on behalf of the application.


  • 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, doubles the regular readahead
  • POSIX_FADV_RANDOM - "we don't expect to read this sequentially"
  • 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.



See also: