xref: /illumos-gate/usr/src/man/man3c/madvise.3c (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
1.\"
2.\" Copyright 2022 Oxide Computer Company
3.\" Copyright (c) 2005, Sun Microsystems, Inc. All Right Reserved.
4.\"
5.\" The contents of this file are subject to the terms of the
6.\" Common Development and Distribution License (the "License").
7.\" You may not use this file except in compliance with the License.
8.\"
9.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10.\" or http://www.opensolaris.org/os/licensing.
11.\" See the License for the specific language governing permissions
12.\" and limitations under the License.
13.\"
14.\" When distributing Covered Code, include this CDDL HEADER in each
15.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16.\" If applicable, add the following below this CDDL HEADER, with the
17.\" fields enclosed by brackets "[]" replaced with your own identifying
18.\" information: Portions Copyright [yyyy] [name of copyright owner]
19.\"
20.Dd February 17, 2023
21.Dt MADVISE 3C
22.Os
23.Sh NAME
24.Nm madvise
25.Nd provide advice to VM system
26.Sh SYNOPSIS
27.In sys/types.h
28.In sys/mman.h
29.Ft int
30.Fo madvise
31.Fa "void *addr"
32.Fa "size_t len"
33.Fa "int advice"
34.Fc
35.Sh DESCRIPTION
36The
37.Fn madvise
38function advises the kernel that a region of user mapped memory in the range
39.Pf [ Fa addr ,
40.Fa addr
41+
42.Fa len Ns
43) will be accessed following a type of pattern.
44The kernel uses this information to optimize the procedure for manipulating and
45maintaining the resources associated with the specified mapping range.
46In general
47.Pq and true to the name of the function ,
48the advice is merely advisory, and the only user-visible ramifications are in
49terms of performance, not semantics.
50Note that
51.Dv MADV_PURGE
52is an exception to this; see below for details.
53.Pp
54Values for
55.Fa advice
56are defined in
57.In sys/mman.h
58as:
59.Bd -literal -offset indent
60#define MADV_NORMAL           0x0  /* No further special treatment */
61#define MADV_RANDOM           0x1  /* Expect random page references */
62#define MADV_SEQUENTIAL       0x2  /* Expect sequential page references */
63#define MADV_WILLNEED         0x3  /* Will need these pages */
64#define MADV_DONTNEED         0x4  /* Don't need these pages */
65#define MADV_FREE             0x5  /* Contents can be freed */
66#define MADV_ACCESS_DEFAULT   0x6  /* default access */
67#define MADV_ACCESS_LWP       0x7  /* next LWP to access heavily */
68#define MADV_ACCESS_MANY      0x8  /* many processes to access heavily */
69#define MADV_PURGE            0x9  /* contents will be purged */
70.Ed
71.Bl -tag -width Ds
72.It Dv MADV_NORMAL
73This is the default system characteristic where accessing memory within the
74address range causes the system to read data from the mapped file.
75The kernel reads all data from files into pages which are retained for a period
76of time as a
77.Dq cache .
78System pages can be a scarce resource, so the kernel steals pages from other
79mappings when needed.
80This is a likely occurrence, but adversely affects system performance only if a
81large amount of memory is accessed.
82.It Dv MADV_RANDOM
83Tell the kernel to read in a minimum amount of data from a mapped file on any
84single particular access.
85If
86.Dv MADV_NORMAL
87is in effect when an address of a mapped file is accessed, the system tries to
88read in as much data from the file as reasonable, in anticipation of other
89accesses within a certain locality.
90.It Dv MADV_SEQUENTIAL
91Tell the system that addresses in this range are likely to be accessed only
92once, so the system will free the resources mapping the address range as
93quickly as possible.
94.It Dv MADV_WILLNEED
95Tell the system that a certain address range is definitely needed so the kernel
96will start reading the specified range into memory.
97This can benefit programs wanting to minimize the time needed to access memory
98the first time, as the kernel would need to read in from the file.
99.It Dv MADV_DONTNEED
100Tell the kernel that the specified address range is no longer needed, so the
101system starts to free the resources associated with the address range.
102While the semantics of
103.Dv MADV_DONTNEED
104are similar to other systems, they differ significantly from the semantics on
105Linux, where
106.Dv MADV_DONTNEED
107will actually synchronously purge the address range, and subsequent faults will
108load from either backing store or be zero-filled on demand.
109If the peculiar Linux semantics are desired,
110.Dv MADV_PURGE
111should be used in lieu of
112.Dv MADV_DONTNEED .
113.It Dv MADV_FREE
114Tell the kernel that contents in the specified address range are no longer
115important and the range will be overwritten.
116When there is demand for memory, the system will free pages associated with the
117specified address range.
118In this instance, the next time a page in the address range is referenced, it
119will contain all zeroes.
120Otherwise, it will contain the data that was there prior to the
121.Dv MADV_FREE
122call.
123References made to the address range will not make the system read from backing
124store
125.Pq swap space
126until the page is modified again.
127.Pp
128This value cannot be used on mappings that have underlying file objects.
129.It Dv MADV_PURGE
130Tell the kernel to purge the specified address range.
131The mapping will be retained, but the pages themselves will be destroyed;
132subsequent faults on the range will result in the page being read from backing
133store
134.Pq if file-backed
135or being zero-filled on demand
136.Pq if anonymous .
137Note that these semantics are generally inferior to
138.Dv MADV_FREE ,
139which gives the system more flexibility and results in better performance when
140pages are, in fact, reused by the caller.
141Indeed,
142.Dv MADV_PURGE
143only exists to provide an equivalent to the unfortunate
144.Dv MADV_DONTNEED
145semantics found in Linux, upon which some programs have
146.Pq regrettably
147come to depend.
148In de novo applications,
149.Dv MADV_PURGE
150should be avoided;
151.Dv MADV_FREE
152should always be preferred.
153.It Dv MADV_ACCESS_LWP
154Tell the kernel that the next LWP to touch the specified address range will
155access it most heavily, so the kernel should try to allocate the memory and
156other resources for this range and the LWP accordingly.
157.It Dv MADV_ACCESS_MANY
158Tell the kernel that many processes and/or LWPs will access the specified
159address range randomly across the machine, so the kernel should try to allocate
160the memory and other resources for this range accordingly.
161.It Dv MADV_ACCESS_DEFAULT
162Reset the kernel's expectation for how the specified range will be accessed to
163the default.
164.El
165.Pp
166The
167.Fn madvise
168function should be used by applications with specific knowledge of their access
169patterns over a memory object, such as a mapped file, to increase system
170performance.
171.Sh RETURN VALUES
172Upon successful completion,
173.Fn madvise
174returns
175.Sy 0 ;
176otherwise, it returns
177.Sy -1
178and sets
179.Va errno
180to indicate the error.
181.Sh ERRORS
182.Bl -tag -width Er
183.It Er EAGAIN
184Some or all mappings in the address range
185.Pf [ Fa addr ,
186.Fa addr
187+
188.Fa len Ns
189) are locked for I/O.
190.It Er EBUSY
191.ad
192Some or all of the addresses in the range
193.Pf [ Fa addr ,
194.Fa addr
195+
196.Fa len Ns
197) are locked and
198.Dv MS_SYNC
199with the
200.Dv MS_INVALIDATE
201option is specified.
202.It Er EFAULT
203Some or all of the addresses in the specified range could not be read into
204memory from the underlying object when performing
205.Dv MADV_WILLNEED .
206The
207.Fn madvise
208function could return prior to this condition being detected, in which case
209.Va errno
210will not be set to
211.Er EFAULT .
212.It Er EINVAL
213The
214.Fa addr
215argument is not a multiple of the page size as returned by
216.Xr sysconf 3C ,
217the length of the specified address range is equal to 0, or the
218.Fa advice
219argument was invalid.
220.It Er EIO
221An I/O error occurred while reading from or writing to the file system.
222.It Er ENOMEM
223Addresses in the range
224.Pf [ Fa addr ,
225.Fa addr
226+
227.Fa len Ns
228) are outside the valid range for the address space of a process, or specify one
229or more pages that are not mapped.
230.It Er ESTALE
231Stale NFS file handle.
232.El
233.Sh INTERFACE STABILITY
234.Sy Committed
235.Sh MT-LEVEL
236.Sy MT-Safe
237.Sh SEE ALSO
238.Xr meminfo 2 ,
239.Xr mmap 2 ,
240.Xr sysconf 3C ,
241.Xr attributes 7
242