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-2001 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()); the difference between the two is that the for‐
121 mer system call assumes that the type of the len argument is size_t,
122 while the latter expects loff_t there.
123
124 Architecture-specific variants
125 Some architectures require 64-bit arguments to be aligned in a suitable
126 pair of registers (see syscall(2) for further detail). On such archi‐
127 tectures, the call signature of posix_fadvise() shown in the SYNOPSIS
128 would force a register to be wasted as padding between the fd and off‐
129 set arguments. Therefore, these architectures define a version of the
130 system call that orders the arguments suitably, but is otherwise
131 exactly the same as posix_fadvise().
132
133 For example, since Linux 2.6.14, ARM has the following system call:
134
135 long arm_fadvise64_64(int fd, int advice,
136 loff_t offset, loff_t len);
137
138 These architecture-specific details are generally hidden from applica‐
139 tions by the glibc posix_fadvise() wrapper function, which invokes the
140 appropriate architecture-specific system call.
141
143 In kernels before 2.6.6, if len was specified as 0, then this was
144 interpreted literally as "zero bytes", rather than as meaning "all
145 bytes through to the end of the file".
146
148 fincore(1), mincore(2), readahead(2), sync_file_range(2), posix_fallo‐
149 cate(3), posix_madvise(3)
150
152 This page is part of release 5.07 of the Linux man-pages project. A
153 description of the project, information about reporting bugs, and the
154 latest version of this page, can be found at
155 https://www.kernel.org/doc/man-pages/.
156
157
158
159Linux 2019-03-06 POSIX_FADVISE(2)