Posix fadvise and madvise: Difference between revisions

From Helpful
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{#addbodyclass:tag_tech}}


==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  
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  
Line 23: Line 25:


* POSIX_FADV_SEQUENTIAL - "we expect to read this sequentially, if that helps"
* POSIX_FADV_SEQUENTIAL - "we expect to read this sequentially, if that helps"
: on linux, doubles the regular readahead
: 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"
* 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"
* POSIX_FADV_NOREUSE - "we expect to read this just once"
Line 37: Line 42:




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:
See also:
* http://linux.die.net/man/2/posix_fadvise
* http://linux.die.net/man/2/posix_fadvise
==madvise==
<!--
<syntaxhighlight lang="c">
int madvise(void addr[.length], size_t length, int advice);
int posix_madvise(void addr[.length], size_t length, int advice);
</syntaxhighlight>
Where posix_fadvise is advice about how we access a file (which includes page cache details), madvise is advice about the page cache.
https://stackoverflow.com/questions/20147881/fadvise-vs-madvise-can-i-use-both-together
https://man7.org/linux/man-pages/man2/madvise.2.html
-->

Latest revision as of 16:37, 20 April 2024

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