Scroll to navigation

MADVISE(2) Linux Programmer's Manual MADVISE(2)

名前

madvise - メモリ利用に関するアドバイスを与える

書式

#include <sys/mman.h>
 
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) の前後でマッピングは継承されるようになる。
MADV_HWPOISON (Linux 2.6.32 以降)
Poison a page and handle it like a hardware memory corruption. This operation is only available for privileged ( CAP_SYS_ADMIN) processes. This operation may result in the calling process receiving a SIGBUS and the page being unmapped. This feature is intended for testing of memory error-handling code; it is only available if the kernel was configured with CONFIG_MEMORY_FAILURE.
MADV_SOFT_OFFLINE (Linux 2.6.33 以降)
Soft offline the pages in the range specified by addr and length. The memory of each page in the specified range is preserved (i.e., when next accessed, the same content will be visible, but in a new physical page frame), and the original page is offlined (i.e., no longer used, and taken out of normal memory management). The effect of the MADV_SOFT_OFFLINE operation is invisible to (i.e., does not change the semantics of) the calling process. This feature is intended for testing of memory error-handling code; it is only available if the kernel was configured with CONFIG_MEMORY_FAILURE.
MADV_MERGEABLE (Linux 2.6.32 以降)
Enable Kernel Samepage Merging (KSM) for the pages in the range specified by addr and length. The kernel regularly scans those areas of user memory that have been marked as mergeable, looking for pages with identical content. These are replaced by a single write-protected page (which is automatically copied if a process later wants to update the content of the page). KSM only merges private anonymous pages (see mmap(2)). The KSM feature is intended for applications that generate many instances of the same data (e.g., virtualization systems such as KVM). It can consume a lot of processing power; use with care. See the kernel source file Documentation/vm/ksm.txt for more details. The MADV_MERGEABLE and MADV_UNMERGEABLE operations are only available if the kernel was configured with CONFIG_KSM.
MADV_UNMERGEABLE (Linux 2.6.32 以降)
Undo the effect of an earlier MADV_MERGEABLE operation on the specified address range; KSM unmerges whatever pages it had merged in the address range specified by addr and length.
MADV_HUGEPAGE (Linux 2.6.38 以降)
Enables Transparent Huge Pages (THP) for pages in the range specified by addr and length. Currently, Transparent Huge Pages only work with private anonymous pages (see mmap(2)). The kernel will regularly scan the areas marked as huge page candidates to replace them with huge pages. The kernel will also allocate huge pages directly when the region is naturally aligned to the huge page size (see posix_memalign(2)). This feature is primarily aimed at applications that use large mappings of data and access large regions of that memory at a time (e.g. virtualization systems such as QEMU). It can very easily waste memory (e.g. a 2MB mapping that only ever accesses 1 byte will result in 2MB of wired memory instead of one 4KB page). See the kernel source file Documentation/vm/transhuge.txt for more details. The MADV_HUGEPAGE and MADV_NOHUGEPAGE operations are only available if the kernel was configured with CONFIG_TRANSPARENT_HUGEPAGE.
MADV_NOHUGEPAGE (Linux 2.6.38 以降)
Ensures that memory in the address range specified by addr and length will not be collapsed into huge pages.

返り値

madvise() は成功すると 0 を返す。 エラーが起こると -1 を返し、 errno を適切な値に設定する。

エラー

EAGAIN
何らかのカーネルリソースが一時的に利用できなかった。
EBADF
指定したマップは存在するが、ファイルではないところをマップしている。
EINVAL
This error can occur for the following reasons:
*
len が負の値である。
*
addr is not page-aligned.
*
advice が有効な値でない。
*
アプリケーションがロックされたページや共有ページを ( MADV_DONTNEED で) 解放 しようとしている。
*
MADV_MERGEABLE or MADV_UNMERGEABLE was specified in advice, but the kernel was not configured with CONFIG_KSM.
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, MADV_HWPOISON, MADV_MERGEABLE, MADV_UNMERGEABLE は 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)

この文書について

この man ページは Linux man-pages プロジェクトのリリース 3.40 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man-pages/ に書かれている。
2011-09-18 Linux