Provided by: manpages-ja-dev_0.5.0.0.20131015+dfsg-2_all bug

名前

       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  available  only
              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 available only 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 available only 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 merges only
              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 Linux kernel source file Documentation/vm/ksm.txt for
              more details.  The MADV_MERGEABLE and MADV_UNMERGEABLE operations are available only 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  work  only  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 Linux kernel
              source  file   Documentation/vm/transhuge.txt   for   more   details.    The   MADV_HUGEPAGE   and
              MADV_NOHUGEPAGE   operations   are   available   only   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.

       MADV_DONTDUMP (Linux 3.4 以降)
              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 (Linux 3.4 以降)
              Undo the effect of an earlier MADV_DONTDUMP.

返り値

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

エラー

       EAGAIN 何らかのカーネルリソースが一時的に利用できなかった。

       EBADF  指定したマップは存在するが、ファイルではないところをマップしている。

       EINVAL このエラーは以下の理由で発生する。

              *  len が負の値である。

              *  addr がページ境界ではない。

              *  advice が有効な値でない。

              *  アプリケーションがロックされたページや共有ページを (MADV_DONTNEED で) 解放 しようとしている。

              *  adviceMADV_MERGEABLEMADV_UNMERGEABLE が指定されたが、 カーネルの設定が 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), core(5)

この文書について

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

Linux                                              2012-04-28                                         MADVISE(2)