Using madvise
()
The madvise
()
function advises the kernel that a region of user virtual
memory in the range starting at the address specified in addr
and with length equal to the value of the len
parameter is
expected to follow a particular pattern of use. The kernel uses this information to optimize the
procedure for manipulating and maintaining the resources associated with the
specified range. Use of the madvise
()
function can increase
system performance when used by programs that have specific knowledge of their
access patterns over memory.
#include <sys/types.h> #include <sys/mman.h> int madvise(caddr_t addr, size_t len, int advice);
The madvise
()
function provides the following flags
to affect how a thread's memory is allocated among lgroups:
-
MADV_ACCESS_DEFAULT
-
This flag resets the kernel's expected access pattern for the specified range to the default.
-
MADV_ACCESS_LWP
-
This flag advises the kernel that the next LWP to touch the specified address range is the LWP that accesses the range the most. The kernel allocates the memory and other resources for this range and the LWP accordingly.
-
MADV_ACCESS_MANY
-
This flag advises the kernel that many processes or LWPs will access the specified address range randomly across the system. The kernel allocates the memory and other resources for this range accordingly.
The madvise
()
function can return the following values:
-
EAGAIN
-
Some or all of the mappings in the specified address range, from
addr
toaddr
+len
, are locked for I/O. -
EINVAL
-
The value of the
addr
parameter is not a multiple of the page size as returned bysysconf
() function, the length of the specified address range is less than or equal to zero, or the advice is invalid. For more information, see thesysconf
(3C) man page. -
EIO
-
An I/O error occurs while reading from or writing to the file system.
-
ENOMEM
-
Addresses in the specified range are outside the valid range for the address space of a process or the addresses in the specified range specify one or more pages that are not mapped.
-
ESTALE
-
The NFS file handle is stale.