1POSIX_FADVISE(2) Linux Programmer's Manual POSIX_FADVISE(2)
2
3
4
6 posix_fadvise - predeclare an access pattern for file data
7
9 #include <fcntl.h>
10
11 int posix_fadvise(int fd, off_t offset, off_t len, int advice);
12
13 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15 posix_fadvise():
16 _POSIX_C_SOURCE >= 200112L
17
19 Programs can use posix_fadvise() to announce an intention to access
20 file data in a specific pattern in the future, thus allowing the kernel
21 to perform appropriate optimizations.
22
23 The advice applies to a (not necessarily existent) region starting at
24 offset and extending for len bytes (or until the end of the file if len
25 is 0) within the file referred to by fd. The advice is not binding; it
26 merely constitutes an expectation on behalf of the application.
27
28 Permissible values for advice include:
29
30 POSIX_FADV_NORMAL
31 Indicates that the application has no advice to give about its
32 access pattern for the specified data. If no advice is given
33 for an open file, this is the default assumption.
34
35 POSIX_FADV_SEQUENTIAL
36 The application expects to access the specified data sequen‐
37 tially (with lower offsets read before higher ones).
38
39 POSIX_FADV_RANDOM
40 The specified data will be accessed in random order.
41
42 POSIX_FADV_NOREUSE
43 The specified data will be accessed only once.
44
45 In kernels before 2.6.18, POSIX_FADV_NOREUSE had the same seman‐
46 tics as POSIX_FADV_WILLNEED. This was probably a bug; since
47 kernel 2.6.18, this flag is a no-op.
48
49 POSIX_FADV_WILLNEED
50 The specified data will be accessed in the near future.
51
52 POSIX_FADV_WILLNEED initiates a nonblocking read of the speci‐
53 fied region into the page cache. The amount of data read may be
54 decreased by the kernel depending on virtual memory load. (A
55 few megabytes will usually be fully satisfied, and more is
56 rarely useful.)
57
58 POSIX_FADV_DONTNEED
59 The specified data will not be accessed in the near future.
60
61 POSIX_FADV_DONTNEED attempts to free cached pages associated
62 with the specified region. This is useful, for example, while
63 streaming large files. A program may periodically request the
64 kernel to free cached data that has already been used, so that
65 more useful cached pages are not discarded instead.
66
67 Requests to discard partial pages are ignored. It is preferable
68 to preserve needed data than discard unneeded data. If the
69 application requires that data be considered for discarding,
70 then offset and len must be page-aligned.
71
72 The implementation may attempt to write back dirty pages in the
73 specified region, but this is not guaranteed. Any unwritten
74 dirty pages will not be freed. If the application wishes to
75 ensure that dirty pages will be released, it should call
76 fsync(2) or fdatasync(2) first.
77
79 On success, zero is returned. On error, an error number is returned.
80
82 EBADF The fd argument was not a valid file descriptor.
83
84 EINVAL An invalid value was specified for advice.
85
86 ESPIPE The specified file descriptor refers to a pipe or FIFO. (ESPIPE
87 is the error specified by POSIX, but before kernel version
88 2.6.16, Linux returned EINVAL in this case.)
89
91 Kernel support first appeared in Linux 2.5.60; the underlying system
92 call is called fadvise64(). Library support has been provided since
93 glibc version 2.2, via the wrapper function posix_fadvise().
94
95 Since Linux 3.18, support for the underlying system call is optional,
96 depending on the setting of the CONFIG_ADVISE_SYSCALLS configuration
97 option.
98
100 POSIX.1-2001, POSIX.1-2008. Note that the type of the len argument was
101 changed from size_t to off_t in POSIX.1-2003 TC1.
102
104 Under Linux, POSIX_FADV_NORMAL sets the readahead window to the default
105 size for the backing device; POSIX_FADV_SEQUENTIAL doubles this size,
106 and POSIX_FADV_RANDOM disables file readahead entirely. These changes
107 affect the entire file, not just the specified region (but other open
108 file handles to the same file are unaffected).
109
110 The contents of the kernel buffer cache can be cleared via the
111 /proc/sys/vm/drop_caches interface described in proc(5).
112
113 One can obtain a snapshot of which pages of a file are resident in the
114 buffer cache by opening a file, mapping it with mmap(2), and then
115 applying mincore(2) to the mapping.
116
117 C library/kernel differences
118 The name of the wrapper function in the C library is posix_fadvise().
119 The underlying system call is called fadvise64() (or, on some architec‐
120 tures, fadvise64_64()).
121
122 Architecture-specific variants
123 Some architectures require 64-bit arguments to be aligned in a suitable
124 pair of registers (see syscall(2) for further detail). On such archi‐
125 tectures, the call signature of posix_fadvise() shown in the SYNOPSIS
126 would force a register to be wasted as padding between the fd and off‐
127 set arguments. Therefore, these architectures define a version of the
128 system call that orders the arguments suitably, but is otherwise
129 exactly the same as posix_fadvise().
130
131 For example, since Linux 2.6.14, ARM has the following system call:
132
133 long arm_fadvise64_64(int fd, int advice,
134 loff_t offset, loff_t len);
135
136 These architecture-specific details are generally hidden from applica‐
137 tions by the glibc posix_fadvise() wrapper function, which invokes the
138 appropriate architecture-specific system call.
139
141 In kernels before 2.6.6, if len was specified as 0, then this was
142 interpreted literally as "zero bytes", rather than as meaning "all
143 bytes through to the end of the file".
144
146 fincore(1), mincore(2), readahead(2), sync_file_range(2), posix_fallo‐
147 cate(3), posix_madvise(3)
148
150 This page is part of release 4.15 of the Linux man-pages project. A
151 description of the project, information about reporting bugs, and the
152 latest version of this page, can be found at
153 https://www.kernel.org/doc/man-pages/.
154
155
156
157Linux 2017-09-15 POSIX_FADVISE(2)