xref: /freebsd/lib/libsys/madvise.2 (revision 8269e7673cf033aba67dab8264fe719920c70f87)
1*8269e767SBrooks Davis.\" Copyright (c) 1991, 1993
2*8269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
3*8269e767SBrooks Davis.\"
4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
6*8269e767SBrooks Davis.\" are met:
7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
8*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
10*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
11*8269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
13*8269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
14*8269e767SBrooks Davis.\"    without specific prior written permission.
15*8269e767SBrooks Davis.\"
16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8269e767SBrooks Davis.\" SUCH DAMAGE.
27*8269e767SBrooks Davis.\"
28*8269e767SBrooks Davis.Dd July 12, 2015
29*8269e767SBrooks Davis.Dt MADVISE 2
30*8269e767SBrooks Davis.Os
31*8269e767SBrooks Davis.Sh NAME
32*8269e767SBrooks Davis.Nm madvise , posix_madvise
33*8269e767SBrooks Davis.Nd give advice about use of memory
34*8269e767SBrooks Davis.Sh LIBRARY
35*8269e767SBrooks Davis.Lb libc
36*8269e767SBrooks Davis.Sh SYNOPSIS
37*8269e767SBrooks Davis.In sys/mman.h
38*8269e767SBrooks Davis.Ft int
39*8269e767SBrooks Davis.Fn madvise "void *addr" "size_t len" "int behav"
40*8269e767SBrooks Davis.Ft int
41*8269e767SBrooks Davis.Fn posix_madvise "void *addr" "size_t len" "int behav"
42*8269e767SBrooks Davis.Sh DESCRIPTION
43*8269e767SBrooks DavisThe
44*8269e767SBrooks Davis.Fn madvise
45*8269e767SBrooks Davissystem call
46*8269e767SBrooks Davisallows a process that has knowledge of its memory behavior
47*8269e767SBrooks Davisto describe it to the system.
48*8269e767SBrooks DavisThe
49*8269e767SBrooks Davis.Fn posix_madvise
50*8269e767SBrooks Davisinterface is identical, except it returns an error number on error and does
51*8269e767SBrooks Davisnot modify
52*8269e767SBrooks Davis.Va errno ,
53*8269e767SBrooks Davisand is provided for standards conformance.
54*8269e767SBrooks Davis.Pp
55*8269e767SBrooks DavisThe known behaviors are:
56*8269e767SBrooks Davis.Bl -tag -width MADV_SEQUENTIAL
57*8269e767SBrooks Davis.It Dv MADV_NORMAL
58*8269e767SBrooks DavisTells the system to revert to the default paging
59*8269e767SBrooks Davisbehavior.
60*8269e767SBrooks Davis.It Dv MADV_RANDOM
61*8269e767SBrooks DavisIs a hint that pages will be accessed randomly, and prefetching
62*8269e767SBrooks Davisis likely not advantageous.
63*8269e767SBrooks Davis.It Dv MADV_SEQUENTIAL
64*8269e767SBrooks DavisCauses the VM system to depress the priority of
65*8269e767SBrooks Davispages immediately preceding a given page when it is faulted in.
66*8269e767SBrooks Davis.It Dv MADV_WILLNEED
67*8269e767SBrooks DavisCauses pages that are in a given virtual address range
68*8269e767SBrooks Davisto temporarily have higher priority, and if they are in
69*8269e767SBrooks Davismemory, decrease the likelihood of them being freed.
70*8269e767SBrooks DavisAdditionally,
71*8269e767SBrooks Davisthe pages that are already in memory will be immediately mapped into
72*8269e767SBrooks Davisthe process, thereby eliminating unnecessary overhead of going through
73*8269e767SBrooks Davisthe entire process of faulting the pages in.
74*8269e767SBrooks DavisThis WILL NOT fault
75*8269e767SBrooks Davispages in from backing store, but quickly map the pages already in memory
76*8269e767SBrooks Davisinto the calling process.
77*8269e767SBrooks Davis.It Dv MADV_DONTNEED
78*8269e767SBrooks DavisAllows the VM system to decrease the in-memory priority
79*8269e767SBrooks Davisof pages in the specified address range.
80*8269e767SBrooks DavisConsequently, future references to this address range are more likely
81*8269e767SBrooks Davisto incur a page fault.
82*8269e767SBrooks Davis.It Dv MADV_FREE
83*8269e767SBrooks DavisGives the VM system the freedom to free pages,
84*8269e767SBrooks Davisand tells the system that information in the specified page range
85*8269e767SBrooks Davisis no longer important.
86*8269e767SBrooks DavisThis is an efficient way of allowing
87*8269e767SBrooks Davis.Xr malloc 3
88*8269e767SBrooks Davisto free pages anywhere in the address space, while keeping the address space
89*8269e767SBrooks Davisvalid.
90*8269e767SBrooks DavisThe next time that the page is referenced, the page might be demand
91*8269e767SBrooks Daviszeroed, or might contain the data that was there before the
92*8269e767SBrooks Davis.Dv MADV_FREE
93*8269e767SBrooks Daviscall.
94*8269e767SBrooks DavisReferences made to that address space range will not make the VM system
95*8269e767SBrooks Davispage the information back in from backing store until the page is
96*8269e767SBrooks Davismodified again.
97*8269e767SBrooks Davis.It Dv MADV_NOSYNC
98*8269e767SBrooks DavisRequest that the system not flush the data associated with this map to
99*8269e767SBrooks Davisphysical backing store unless it needs to.
100*8269e767SBrooks DavisTypically this prevents the
101*8269e767SBrooks Davisfile system update daemon from gratuitously writing pages dirtied
102*8269e767SBrooks Davisby the VM system to physical disk.
103*8269e767SBrooks DavisNote that VM/file system coherency is
104*8269e767SBrooks Davisalways maintained, this feature simply ensures that the mapped data is
105*8269e767SBrooks Davisonly flush when it needs to be, usually by the system pager.
106*8269e767SBrooks Davis.Pp
107*8269e767SBrooks DavisThis feature is typically used when you want to use a file-backed shared
108*8269e767SBrooks Davismemory area to communicate between processes (IPC) and do not particularly
109*8269e767SBrooks Davisneed the data being stored in that area to be physically written to disk.
110*8269e767SBrooks DavisWith this feature you get the equivalent performance with mmap that you
111*8269e767SBrooks Daviswould expect to get with SysV shared memory calls, but in a more controllable
112*8269e767SBrooks Davisand less restrictive manner.
113*8269e767SBrooks DavisHowever, note that this feature is not portable
114*8269e767SBrooks Davisacross UNIX platforms (though some may do the right thing by default).
115*8269e767SBrooks DavisFor more information see the MAP_NOSYNC section of
116*8269e767SBrooks Davis.Xr mmap 2
117*8269e767SBrooks Davis.It Dv MADV_AUTOSYNC
118*8269e767SBrooks DavisUndoes the effects of MADV_NOSYNC for any future pages dirtied within the
119*8269e767SBrooks Davisaddress range.
120*8269e767SBrooks DavisThe effect on pages already dirtied is indeterminate - they
121*8269e767SBrooks Davismay or may not be reverted.
122*8269e767SBrooks DavisYou can guarantee reversion by using the
123*8269e767SBrooks Davis.Xr msync 2
124*8269e767SBrooks Davisor
125*8269e767SBrooks Davis.Xr fsync 2
126*8269e767SBrooks Davissystem calls.
127*8269e767SBrooks Davis.It Dv MADV_NOCORE
128*8269e767SBrooks DavisRegion is not included in a core file.
129*8269e767SBrooks Davis.It Dv MADV_CORE
130*8269e767SBrooks DavisInclude region in a core file.
131*8269e767SBrooks Davis.It Dv MADV_PROTECT
132*8269e767SBrooks DavisInforms the VM system this process should not be killed when the
133*8269e767SBrooks Davisswap space is exhausted.
134*8269e767SBrooks DavisThe process must have superuser privileges.
135*8269e767SBrooks DavisThis should be used judiciously in processes that must remain running
136*8269e767SBrooks Davisfor the system to properly function.
137*8269e767SBrooks Davis.El
138*8269e767SBrooks Davis.Pp
139*8269e767SBrooks DavisPortable programs that call the
140*8269e767SBrooks Davis.Fn posix_madvise
141*8269e767SBrooks Davisinterface should use the aliases
142*8269e767SBrooks Davis.Dv POSIX_MADV_NORMAL , POSIX_MADV_SEQUENTIAL ,
143*8269e767SBrooks Davis.Dv POSIX_MADV_RANDOM , POSIX_MADV_WILLNEED ,
144*8269e767SBrooks Davisand
145*8269e767SBrooks Davis.Dv POSIX_MADV_DONTNEED
146*8269e767SBrooks Davisrather than the flags described above.
147*8269e767SBrooks Davis.Sh RETURN VALUES
148*8269e767SBrooks Davis.Rv -std madvise
149*8269e767SBrooks Davis.Sh ERRORS
150*8269e767SBrooks DavisThe
151*8269e767SBrooks Davis.Fn madvise
152*8269e767SBrooks Davissystem call will fail if:
153*8269e767SBrooks Davis.Bl -tag -width Er
154*8269e767SBrooks Davis.It Bq Er EINVAL
155*8269e767SBrooks DavisThe
156*8269e767SBrooks Davis.Fa behav
157*8269e767SBrooks Davisargument is not valid.
158*8269e767SBrooks Davis.It Bq Er ENOMEM
159*8269e767SBrooks DavisThe virtual address range specified by the
160*8269e767SBrooks Davis.Fa addr
161*8269e767SBrooks Davisand
162*8269e767SBrooks Davis.Fa len
163*8269e767SBrooks Davisarguments is not valid.
164*8269e767SBrooks Davis.It Bq Er EPERM
165*8269e767SBrooks Davis.Dv MADV_PROTECT
166*8269e767SBrooks Daviswas specified and the process does not have superuser privileges.
167*8269e767SBrooks Davis.El
168*8269e767SBrooks Davis.Sh SEE ALSO
169*8269e767SBrooks Davis.Xr mincore 2 ,
170*8269e767SBrooks Davis.Xr mprotect 2 ,
171*8269e767SBrooks Davis.Xr msync 2 ,
172*8269e767SBrooks Davis.Xr munmap 2 ,
173*8269e767SBrooks Davis.Xr posix_fadvise 2
174*8269e767SBrooks Davis.Sh STANDARDS
175*8269e767SBrooks DavisThe
176*8269e767SBrooks Davis.Fn posix_madvise
177*8269e767SBrooks Davisinterface conforms to
178*8269e767SBrooks Davis.St -p1003.1-2001 .
179*8269e767SBrooks Davis.Sh HISTORY
180*8269e767SBrooks DavisThe
181*8269e767SBrooks Davis.Fn madvise
182*8269e767SBrooks Davissystem call first appeared in
183*8269e767SBrooks Davis.Bx 4.4 .
184