MADVISE(3C) | Standard C Library Functions | MADVISE(3C) |
madvise
— provide
advice to VM system
#include
<sys/types.h>
#include <sys/mman.h>
int
madvise
(void *addr,
size_t len, int advice);
The
madvise
()
function advises the kernel that a region of user mapped memory in the range
[addr, addr +
len) will be accessed following a type of pattern. The
kernel uses this information to optimize the procedure for manipulating and
maintaining the resources associated with the specified mapping range. In
general (and true to the name of the function), the advice is merely
advisory, and the only user-visible ramifications are in terms of
performance, not semantics. Note that MADV_PURGE
is
an exception to this; see below for details.
Values for advice are defined in
<sys/mman.h>
as:
#define MADV_NORMAL 0x0 /* No further special treatment */ #define MADV_RANDOM 0x1 /* Expect random page references */ #define MADV_SEQUENTIAL 0x2 /* Expect sequential page references */ #define MADV_WILLNEED 0x3 /* Will need these pages */ #define MADV_DONTNEED 0x4 /* Don't need these pages */ #define MADV_FREE 0x5 /* Contents can be freed */ #define MADV_ACCESS_DEFAULT 0x6 /* default access */ #define MADV_ACCESS_LWP 0x7 /* next LWP to access heavily */ #define MADV_ACCESS_MANY 0x8 /* many processes to access heavily */ #define MADV_PURGE 0x9 /* contents will be purged */
MADV_NORMAL
MADV_RANDOM
MADV_NORMAL
is in
effect when an address of a mapped file is accessed, the system tries to
read in as much data from the file as reasonable, in anticipation of other
accesses within a certain locality.MADV_SEQUENTIAL
MADV_WILLNEED
MADV_DONTNEED
MADV_DONTNEED
are similar
to other systems, they differ significantly from the semantics on Linux,
where MADV_DONTNEED
will actually synchronously
purge the address range, and subsequent faults will load from either
backing store or be zero-filled on demand. If the peculiar Linux semantics
are desired, MADV_PURGE
should be used in lieu of
MADV_DONTNEED
.MADV_FREE
MADV_FREE
call.
References made to the address range will not make the system read from
backing store (swap space) until the page is modified again.
This value cannot be used on mappings that have underlying file objects.
MADV_PURGE
MADV_FREE
, which gives the system more flexibility
and results in better performance when pages are, in fact, reused by the
caller. Indeed, MADV_PURGE
only exists to provide
an equivalent to the unfortunate MADV_DONTNEED
semantics found in Linux, upon which some programs have (regrettably) come
to depend. In de novo applications, MADV_PURGE
should be avoided; MADV_FREE
should always be
preferred.MADV_ACCESS_LWP
MADV_ACCESS_MANY
MADV_ACCESS_DEFAULT
The
madvise
()
function should be used by applications with specific knowledge of their
access patterns over a memory object, such as a mapped file, to increase
system performance.
Upon successful completion, madvise
()
returns 0;
otherwise, it returns
-1 and sets
errno to indicate the error.
EAGAIN
EBUSY
MS_SYNC
with the
MS_INVALIDATE
option is specified.EFAULT
MADV_WILLNEED
. The
madvise
() function could return prior to this
condition being detected, in which case errno will
not be set to
EFAULT
.EINVAL
EIO
ENOMEM
ESTALE
February 17, 2023 | OmniOS |