madviseのヘルプ・マニュアル
日本語 英語
madvise --help
man madvise
MADVISE(2) Linux Programmer’s Manual MADVISE(2)
名前
madvise - メモリ利用に関するアドバイスを与える
書式
#include
int madvise(void *addr, size_t length, int advice);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
madvise(): _BSD_SOURCE
説明
madvise() システムコールは、アドレス addr からはじまる length バイトの
メモリブロックのページング入出力をどう扱えば良いか、カーネルにアドバ イ
ス する。これを用いると、アプリケーションからカーネルに、マップされたメ
モリや共有メモリをどのように扱ってほしいか伝えることができ、カーネル は
そ れに応じて先読みやキャッシュなどの適切な手法を選択できる。このコール
はアプリケーションの動作そのものには影響しない (MADV_DONTNEED の場合 は
別) が、性能には影響しうる。なおこのアドバイスを受け入れるかどうかはカ
ーネルに任される。
アドバイスは引き数 advice によって与える。以下のいずれかを指定できる。
MADV_NORMAL
特別な扱いは行わない。これがデフォルトである。
MADV_RANDOM
ページ参照はランダムな順序で行われそうだ。 (したがって、先読みは
あまり効果がなさそうだ。)
MADV_SEQUENTIAL
ページ参照はシーケンシャルな順序で行われそうだ。 (したがって与え
た範囲のページは積極的に先読みしておくと良いだろう。またアクセス
が終わったら速やかに解放して良い。)
MADV_WILLNEED
近い将来にアクセスされそうだ。 (したがってこれらのページを今のう
ちに先読みしておくといいだろう。)
MADV_DONTNEED
しばらくアクセスはなさそうだ。 (現時点でアプリケーションは与えた
範囲の処理を終えている。したがってカーネルはこれに関連するリソー
スを解放して良い。) これ以降この範囲のページへのアクセスがあると
、成功はするが、メモリの内容をマップ元のファイルからロードし直す
ことになる (mmap(2) を見よ) か、または元ファイルがないマップペー
ジではアクセスがあったときに 0 埋めが行われることになる。
MADV_REMOVE (Linux 2.6.16 以降)
指定された範囲のページと関連するバッキングストアを解放する。現在
のところ、 shmfs/tmpfs だけがこれに対応している。他のファイル シ
ステムでは ENOSYS が返される。
MADV_DONTFORK (Linux 2.6.16 以降)
fork(2) が行われた後、指定された範囲のページを子プロセスが利用で
きないようにする。この機能は、書き込み時コピ ー (copy-on-write)
方 式で、 fork(2) の後で親プロセスがページに書き込みを行った場合
にページの物理位置が変化しないようにするのに有効である (ページの
再 配置はハードウェアがそのページに DMA 転送を行うような場合に問
題を起こすことがある)。
MADV_DOFORK (Linux 2.6.16 以降)
MADV_DONTFORK の影響を取り消し、デフォルトの動作に戻す。つまり、
fork(2) の前後でマッピングは継承されるようになる。
返り値
madvise() は成功すると 0 を返す。エラーが起こると -1 を返し、 errno を
適切な値に設定する。
エラー
EAGAIN 何らかのカーネルリソースが一時的に利用できなかった。
EBADF 指定したマップは存在するが、ファイルではないところをマップしてい
る。
EINVAL len の値が負、 addr がページ境界でない、 advice が正しい値でない
、アプリケーションがロックされたページや共有ページを (MADV_DONT-
NEED で) 解放しようとしている、など。
EIO (MADV_WILLNEED の場合) この範囲のページングを行うと、プロセスの
RSS (resident set size) の最大値を越えてしまう。
ENOMEM (MADV_WILLNEED の場合) メモリが足りず、ページングに失敗した。
ENOMEM 指定した範囲のアドレスが、現在マップされていない。あるいはプロセ
スのアドレス空間の内部にない。
準拠
POSIX.1b. POSIX.1-2001 では、 posix_madvise(3) を POSIX_MADV_NORMAL な
どの定数とともに記述していた (それぞれの振る舞いはここで述べたものに 近
い)。ファイルアクセスに対しても posix_fadvise(2) という類似の関数が存在
する。
MADV_REMOVE, MADV_DONTFORK, MADV_DOFORK は Linux 固有である。
注意
Linux での注意
現在の Linux の実装 (2.4.0) では、このシステムコールをアドバイスとい う
よ りは命令と見ている。したがってこのアドバイスに対して通常行われる動作
が不可能な場合は、エラーを返すことがある (上記の エラー の記述を参照)。
この振舞いは標準とは異なる。
Linux の実装では addr のアドレスはページ境界の値でなければならない。ま
た length は 0 であっても構わない。また Linux 版の madvise() では、指定
さ れたアドレス範囲にマップされていない部分があると、これらを無視して残
りの部分にアドバイスを適用する (しかしシステムコールに対してはちゃん と
ENOMEM を返す)。
関連項目
getrlimit(2), mincore(2), mmap(2), mprotect(2), msync(2), munmap(2)
Linux 2008-04-22 MADVISE(2)
MADVISE(2) Linux Programmer’s Manual MADVISE(2)
NAME
madvise - give advice about use of memory
SYNOPSIS
#include
int madvise(void *addr, size_t length, int advice);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
madvise(): _BSD_SOURCE
DESCRIPTION
The madvise() system call advises the kernel about how to handle paging
input/output in the address range beginning at address addr and with
size length bytes. It allows an application to tell the kernel how it
expects to use some mapped or shared memory areas, so that the kernel
can choose appropriate read-ahead and caching techniques. This call
does not influence the semantics of the application (except in the case
of MADV_DONTNEED), but may influence its performance. The kernel is
free to ignore the advice.
The advice is indicated in the advice argument which can be
MADV_NORMAL
No special treatment. This is the default.
MADV_RANDOM
Expect page references in random order. (Hence, read ahead may
be less useful than normally.)
MADV_SEQUENTIAL
Expect page references in sequential order. (Hence, pages in
the given range can be aggressively read ahead, and may be freed
soon after they are accessed.)
MADV_WILLNEED
Expect access in the near future. (Hence, it might be a good
idea to read some pages ahead.)
MADV_DONTNEED
Do not expect access in the near future. (For the time being,
the application is finished with the given range, so the kernel
can free resources associated with it.) Subsequent accesses of
pages in this range will succeed, but will result either in re-
loading of the memory contents from the underlying mapped file
(see mmap(2)) or zero-fill-on-demand pages for mappings without
an underlying file.
MADV_REMOVE (Since Linux 2.6.16)
Free up a given range of pages and its associated backing store.
Currently, only shmfs/tmpfs supports this; other file systems
return with the error ENOSYS.
MADV_DONTFORK (Since Linux 2.6.16)
Do not make the pages in this range available to the child after
a fork(2). This is useful to prevent copy-on-write semantics
from changing the physical location of a page(s) if the parent
writes to it after a fork(2). (Such page relocations cause
problems for hardware that DMAs into the page(s).)
MADV_DOFORK (Since Linux 2.6.16)
Undo the effect of MADV_DONTFORK, restoring the default behav-
ior, whereby a mapping is inherited across fork(2).
MADV_DONTDUMP
Exclude from a core dump those pages in the range specified by
addr and length. This is useful in applications that have large
areas of memory that are known not to be useful in a core dump.
The effect of MADV_DONTDUMP takes precedence over the bit mask
that is set via the /proc/PID/coredump_filter file (see
core(5)).
MADV_DODUMP
Undo the effect of an earlier MADV_DONTDUMP.
RETURN VALUE
On success madvise() returns zero. On error, it returns -1 and errno
is set appropriately.
ERRORS
EAGAIN A kernel resource was temporarily unavailable.
EBADF The map exists, but the area maps something that isn’t a file.
EINVAL The value len is negative, addr is not page-aligned, advice is
not a valid value, or the application is attempting to release
locked or shared pages (with MADV_DONTNEED).
EIO (for MADV_WILLNEED) Paging in this area would exceed the pro-
cess’s maximum resident set size.
ENOMEM (for MADV_WILLNEED) Not enough memory: paging in failed.
ENOMEM Addresses in the specified range are not currently mapped, or
are outside the address space of the process.
CONFORMING TO
POSIX.1b. POSIX.1-2001 describes posix_madvise(3) with constants
POSIX_MADV_NORMAL, etc., with a behavior close to that described here.
There is a similar posix_fadvise(2) for file access.
MADV_REMOVE, MADV_DONTFORK, and MADV_DOFORK are Linux-specific.
NOTES
Linux Notes
The current Linux implementation (2.4.0) views this system call more as
a command than as advice and hence may return an error when it cannot
do what it usually would do in response to this advice. (See the
ERRORS description above.) This is non-standard behavior.
The Linux implementation requires that the address addr be page-
aligned, and allows length to be zero. If there are some parts of the
specified address range that are not mapped, the Linux version of mad-
vise() ignores them and applies the call to the rest (but returns
ENOMEM from the system call, as it should).
SEE ALSO
getrlimit(2), mincore(2), mmap(2), mprotect(2), msync(2), munmap(2)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2008-04-22 MADVISE(2)