]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/posix_fadvise.2
Ready for 5.00
[thirdparty/man-pages.git] / man2 / posix_fadvise.2
1 .\" Copyright 2003 Abhijit Menon-Sen <ams@wiw.org>
2 .\" and Copyright (C) 2010, 2015, 2017 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" 2005-04-08 mtk, noted kernel version and added BUGS
27 .\" 2010-10-09, mtk, document arm_fadvise64_64()
28 .\"
29 .TH POSIX_FADVISE 2 2019-03-06 "Linux" "Linux Programmer's Manual"
30 .SH NAME
31 posix_fadvise \- predeclare an access pattern for file data
32 .SH SYNOPSIS
33 .nf
34 .B #include <fcntl.h>
35 .PP
36 .BI "int posix_fadvise(int " fd ", off_t " offset ", off_t " len \
37 ", int " advice ");"
38 .fi
39 .PP
40 .ad l
41 .in -4n
42 Feature Test Macro Requirements for glibc (see
43 .BR feature_test_macros (7)):
44 .in
45 .PP
46 .BR posix_fadvise ():
47 .RS 4
48 _POSIX_C_SOURCE\ >=\ 200112L
49 .RE
50 .ad
51 .SH DESCRIPTION
52 Programs can use
53 .BR posix_fadvise ()
54 to announce an intention to access
55 file data in a specific pattern in the future, thus allowing the kernel
56 to perform appropriate optimizations.
57 .PP
58 The \fIadvice\fP applies to a (not necessarily existent) region starting
59 at \fIoffset\fP and extending for \fIlen\fP bytes (or until the end of
60 the file if \fIlen\fP is 0) within the file referred to by \fIfd\fP.
61 The \fIadvice\fP is not binding;
62 it merely constitutes an expectation on behalf of
63 the application.
64 .PP
65 Permissible values for \fIadvice\fP include:
66 .TP
67 .B POSIX_FADV_NORMAL
68 Indicates that the application has no advice to give about its access
69 pattern for the specified data.
70 If no advice is given for an open file,
71 this is the default assumption.
72 .TP
73 .B POSIX_FADV_SEQUENTIAL
74 The application expects to access the specified data sequentially (with
75 lower offsets read before higher ones).
76 .TP
77 .B POSIX_FADV_RANDOM
78 The specified data will be accessed in random order.
79 .TP
80 .B POSIX_FADV_NOREUSE
81 The specified data will be accessed only once.
82 .IP
83 In kernels before 2.6.18, \fBPOSIX_FADV_NOREUSE\fP had the
84 same semantics as \fBPOSIX_FADV_WILLNEED\fP.
85 This was probably a bug; since kernel 2.6.18, this flag is a no-op.
86 .TP
87 .B POSIX_FADV_WILLNEED
88 The specified data will be accessed in the near future.
89 .IP
90 \fBPOSIX_FADV_WILLNEED\fP initiates a
91 nonblocking read of the specified region into the page cache.
92 The amount of data read may be decreased by the kernel depending
93 on virtual memory load.
94 (A few megabytes will usually be fully satisfied,
95 and more is rarely useful.)
96 .TP
97 .B POSIX_FADV_DONTNEED
98 The specified data will not be accessed in the near future.
99 .IP
100 \fBPOSIX_FADV_DONTNEED\fP attempts to free cached pages associated with
101 the specified region.
102 This is useful, for example, while streaming large
103 files.
104 A program may periodically request the kernel to free cached data
105 that has already been used, so that more useful cached pages are not
106 discarded instead.
107 .IP
108 Requests to discard partial pages are ignored.
109 It is preferable to preserve needed data than discard unneeded data.
110 If the application requires that data be considered for discarding, then
111 .I offset
112 and
113 .I len
114 must be page-aligned.
115 .IP
116 The implementation
117 .I may
118 attempt to write back dirty pages in the specified region,
119 but this is not guaranteed.
120 Any unwritten dirty pages will not be freed.
121 If the application wishes to ensure that dirty pages will be released,
122 it should call
123 .BR fsync (2)
124 or
125 .BR fdatasync (2)
126 first.
127 .SH RETURN VALUE
128 On success, zero is returned.
129 On error, an error number is returned.
130 .SH ERRORS
131 .TP
132 .B EBADF
133 The \fIfd\fP argument was not a valid file descriptor.
134 .TP
135 .B EINVAL
136 An invalid value was specified for \fIadvice\fP.
137 .TP
138 .B ESPIPE
139 The specified file descriptor refers to a pipe or FIFO.
140 .RB ( ESPIPE
141 is the error specified by POSIX,
142 but before kernel version 2.6.16,
143 .\" commit 87ba81dba431232548ce29d5d224115d0c2355ac
144 Linux returned
145 .B EINVAL
146 in this case.)
147 .SH VERSIONS
148 Kernel support first appeared in Linux 2.5.60;
149 the underlying system call is called
150 .BR fadvise64 ().
151 .\" of fadvise64_64()
152 Library support has been provided since glibc version 2.2,
153 via the wrapper function
154 .BR posix_fadvise ().
155 .PP
156 Since Linux 3.18,
157 .\" commit d3ac21cacc24790eb45d735769f35753f5b56ceb
158 support for the underlying system call is optional,
159 depending on the setting of the
160 .B CONFIG_ADVISE_SYSCALLS
161 configuration option.
162 .SH CONFORMING TO
163 POSIX.1-2001, POSIX.1-2008.
164 Note that the type of the
165 .I len
166 argument was changed from
167 .I size_t
168 to
169 .I off_t
170 in POSIX.1-2003 TC1.
171 .SH NOTES
172 Under Linux, \fBPOSIX_FADV_NORMAL\fP sets the readahead window to the
173 default size for the backing device; \fBPOSIX_FADV_SEQUENTIAL\fP doubles
174 this size, and \fBPOSIX_FADV_RANDOM\fP disables file readahead entirely.
175 These changes affect the entire file, not just the specified region
176 (but other open file handles to the same file are unaffected).
177 .PP
178 The contents of the kernel buffer cache can be cleared via the
179 .IR /proc/sys/vm/drop_caches
180 interface described in
181 .BR proc (5).
182 .PP
183 One can obtain a snapshot of which pages of a file are resident
184 in the buffer cache by opening a file, mapping it with
185 .BR mmap (2),
186 and then applying
187 .BR mincore (2)
188 to the mapping.
189 .SS C library/kernel differences
190 The name of the wrapper function in the C library is
191 .BR posix_fadvise ().
192 The underlying system call is called
193 .BR fadvise64 ()
194 (or, on some architectures,
195 .BR fadvise64_64 ());
196 the difference between the two is that the former system call
197 assumes that the type of the \fIlen\fP argument is \fIsize_t\fP,
198 while the latter expects \fIloff_t\fP there.
199 .SS Architecture-specific variants
200 Some architectures require
201 64-bit arguments to be aligned in a suitable pair of registers (see
202 .BR syscall (2)
203 for further detail).
204 On such architectures, the call signature of
205 .BR posix_fadvise ()
206 shown in the SYNOPSIS would force
207 a register to be wasted as padding between the
208 .I fd
209 and
210 .I offset
211 arguments.
212 Therefore, these architectures define a version of the
213 system call that orders the arguments suitably,
214 but is otherwise exactly the same as
215 .BR posix_fadvise ().
216 .PP
217 For example, since Linux 2.6.14, ARM has the following system call:
218 .PP
219 .in +4n
220 .EX
221 .BI "long arm_fadvise64_64(int " fd ", int " advice ,
222 .BI " loff_t " offset ", loff_t " len );
223 .EE
224 .in
225 .PP
226 These architecture-specific details are generally
227 hidden from applications by the glibc
228 .BR posix_fadvise ()
229 wrapper function,
230 which invokes the appropriate architecture-specific system call.
231 .SH BUGS
232 In kernels before 2.6.6, if
233 .I len
234 was specified as 0, then this was interpreted literally as "zero bytes",
235 rather than as meaning "all bytes through to the end of the file".
236 .SH SEE ALSO
237 .BR fincore (1),
238 .BR mincore (2),
239 .BR readahead (2),
240 .BR sync_file_range (2),
241 .BR posix_fallocate (3),
242 .BR posix_madvise (3)