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