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