1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"*******************************************************************
27 .\" This file was generated with po4a. Translate the source file.
29 .\"*******************************************************************
31 .\" Japanese Version Copyright (c) 2013 Akihiro MOTOKI
32 .\" all rights reserved.
33 .\" Translated 2013-07-22, Akihiro MOTOKI <amotoki@gmail.com>
34 .\" Updated 2013-07-31, Akihiro MOTOKI <amotoki@gmail.com>
36 .TH MALLOC_INFO 3 2015\-01\-22 GNU "Linux Programmer's Manual"
38 malloc_info \- malloc の状態をストリームに出力する
41 \fB#include <malloc.h>\fP
43 \fBint malloc_info(int \fP\fIoptions\fP\fB, FILE *\fP\fIstream\fP\fB);\fP
46 \fBmalloc_info\fP() 関数は、 呼び出し元のメモリー割り当て実装の現在の状態を表す XML 文字列を出力する。 文字列は、
47 ファイルストリーム \fIstream\fP に出力される。 出力された文字列には、 全ての割り当て領域の情報が含まれる (\fBmalloc\fP(3) 参照)。
49 現在の実装では、 \fIoptions\fP は 0 でなければならない。
51 \fBmalloc_info\fP() は、成功すると 0 を返す。 エラーの場合、\-1 を返し、 \fIerrno\fP にエラーの原因を示す値を設定する。
55 \fIoptions\fP が 0 でなかった。
57 \fBmalloc_info\fP() は glibc バージョン 2.10 で追加された。
61 メモリー割り当て情報は (C の構造体ではなく) XML 文字列として提供される。 これは、 この情報は時間をたつと (内部で使用している実装によって)
62 変わる可能性があるからである。 出力される XML 文字列にはバージョンフィールドが付いている。
64 \fBopen_memstream\fP(3) 関数を使うと、 \fBmalloc_info\fP() の出力を、
65 ファイルではなくメモリー内のバッファーに直接送ることができる。
67 \fBmalloc_info\fP() 関数は、 \fBmalloc_stats\fP(3) と \fBmallinfo\fP(3) の不備を解決するために設定された。
69 以下のプログラムは最大で 4 つのコマンドライン引数を取り、 最初の 3 つは必須である。 最初の引数は、このプログラムが生成するスレッド数を指定する。
70 メインスレッドを含む全てのスレッドが第 2 引数で指定した数のメモリーブロックの割り当てを行う。 第 3 引数は割り当てるブロックのサイズを制御する。
71 メインスレッドはこのサイズのブロックを作成し、 このプログラムが生成する 2 番目のスレッドはこのサイズの 2 倍のサイズのブロックを、 3
72 番目のスレッドはこのサイズの 3 倍のサイズのブロックを割り当て、 以下同様である。
74 このプログラムは \fBmalloc_info\fP() を 2 回呼び出し、メモリー割り当て状態を表示する。
75 最初の呼び出しはスレッドの生成もメモリーの割り当ても行われる前に実行される。 2
76 回目の呼び出しは全てのスレッドがメモリー割り当てを行った後に実行される。
78 以下の例では、 コマンドライン引数で、 追加でスレッドを一つ生成し、メインスレッドと追加のスレッドの両方が 10000
79 個のメモリーブロックを割り当てるように指定している。 メモリーブロックの割り当て後の \fBmalloc_info\fP() では、 2
84 $ \fBgetconf GNU_LIBC_VERSION\fP
86 $ \fB./a.out 1 10000 100\fP
87 ============ Before allocating blocks ============
92 <total type="fast" count="0" size="0"/>
93 <total type="rest" count="0" size="0"/>
94 <system type="current" size="135168"/>
95 <system type="max" size="135168"/>
96 <aspace type="total" size="135168"/>
97 <aspace type="mprotect" size="135168"/>
99 <total type="fast" count="0" size="0"/>
100 <total type="rest" count="0" size="0"/>
101 <system type="current" size="135168"/>
102 <system type="max" size="135168"/>
103 <aspace type="total" size="135168"/>
104 <aspace type="mprotect" size="135168"/>
107 ============ After allocating blocks ============
112 <total type="fast" count="0" size="0"/>
113 <total type="rest" count="0" size="0"/>
114 <system type="current" size="1081344"/>
115 <system type="max" size="1081344"/>
116 <aspace type="total" size="1081344"/>
117 <aspace type="mprotect" size="1081344"/>
122 <total type="fast" count="0" size="0"/>
123 <total type="rest" count="0" size="0"/>
124 <system type="current" size="1032192"/>
125 <system type="max" size="1032192"/>
126 <aspace type="total" size="1032192"/>
127 <aspace type="mprotect" size="1032192"/>
129 <total type="fast" count="0" size="0"/>
130 <total type="rest" count="0" size="0"/>
131 <system type="current" size="2113536"/>
132 <system type="max" size="2113536"/>
133 <aspace type="total" size="2113536"/>
134 <aspace type="mprotect" size="2113536"/>
147 static size_t blockSize;
148 static int numThreads, numBlocks;
150 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
154 thread_func(void *arg)
159 /* The multiplier \(aq(2 + tn)\(aq ensures that each thread (including
160 the main thread) allocates a different amount of memory */
162 for (j = 0; j < numBlocks; j++)
163 if (malloc(blockSize * (2 + tn)) == NULL)
164 errExit("malloc\-thread");
166 sleep(100); /* Sleep until main thread terminates */
171 main(int argc, char *argv[])
173 int j, tn, sleepTime;
178 "%s num\-threads num\-blocks block\-size [sleep\-time]\en",
183 numThreads = atoi(argv[1]);
184 numBlocks = atoi(argv[2]);
185 blockSize = atoi(argv[3]);
186 sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
188 thr = calloc(numThreads, sizeof(pthread_t));
192 printf("============ Before allocating blocks ============\en");
193 malloc_info(0, stdout);
195 /* Create threads that allocate different amounts of memory */
197 for (tn = 0; tn < numThreads; tn++) {
198 errno = pthread_create(&thr[tn], NULL, thread_func,
201 errExit("pthread_create");
203 /* If we add a sleep interval after the start\-up of each
204 thread, the threads likely won\(aqt contend for malloc
205 mutexes, and therefore additional arenas won\(aqt be
206 allocated (see malloc(3)). */
212 /* The main thread also allocates some memory */
214 for (j = 0; j < numBlocks; j++)
215 if (malloc(blockSize) == NULL)
218 sleep(2); /* Give all threads a chance to
219 complete allocations */
221 printf("\en============ After allocating blocks ============\en");
222 malloc_info(0, stdout);
228 \fBmallinfo\fP(3), \fBmalloc\fP(3), \fBmalloc_stats\fP(3), \fBmallopt\fP(3),
229 \fBopen_memstream\fP(3)
231 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.78 の一部
232 である。プロジェクトの説明とバグ報告に関する情報は
233 http://www.kernel.org/doc/man\-pages/ に書かれている。