OSDN Git Service

25b993a940026fba79e53de8411f67f1e81e45dd
[linuxjm/LDP_man-pages.git] / release / man3 / malloc_info.3
1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\"*******************************************************************
26 .\"
27 .\" This file was generated with po4a. Translate the source file.
28 .\"
29 .\"*******************************************************************
30 .TH MALLOC_INFO 3 2013\-04\-19 GNU "Linux Programmer's Manual"
31 .SH 名前
32 malloc_info \- malloc の状態をストリームに出力する
33 .SH 書式
34 .nf
35 \fB#include <malloc.h>\fP
36 .sp
37 \fBint malloc_info(int \fP\fIoptions\fP\fB, FILE *\fP\fIfp\fP\fB);\fP
38 .fi
39 .SH 説明
40 \fBmalloc_info\fP() 関数は、 呼び出し元のメモリ割り当て実装の現在の状態を表す XML 文字列を出力する。 文字列は、 ファイルストリーム
41 \fIfp\fP に出力される。 出力された文字列には、 全ての割り当て領域の情報が含まれる (\fBmalloc\fP(3) 参照)。
42
43 現在の実装では、 \fIoptions\fP は 0 でなければならない。
44 .SH 返り値
45 \fBmalloc_info\fP() は、成功すると 0 を返す。 エラーの場合、\-1 を返し、 \fIerrno\fP にエラーの原因を示す値を設定する。
46 .SH エラー
47 .TP 
48 \fBEINVAL\fP
49 \fIoptions\fP が 0 でなかった。
50 .SH バージョン
51 \fBmalloc_info\fP() は glibc バージョン 2.10 で追加された。
52 .SH 準拠
53 この関数は GNU による拡張である。
54 .SH 注意
55 メモリ割り当て情報は (C の構造体ではなく) XML 文字列として提供される。 これは、 この情報は時間をたつと (内部で使用している実装によって)
56 変わる可能性があるからである。 出力される XML 文字列にはバージョンフィールドが付いている。
57
58 \fBopen_memstream\fP(3) 関数を使うと、 \fBmalloc_info\fP() の出力を、
59 ファイルではなくメモリ内のバッファに直接送ることができる。
60
61 \fBmalloc_info\fP() 関数は、 \fBmalloc_stats\fP(3) と \fBmallinfo\fP(3) の不備を解決するために設定された。
62 .SH 例
63 The program below takes up to four command\-line arguments, of which the
64 first three are mandatory.  The first argument specifies the number of
65 threads that the program should create.  All of the threads, including the
66 main thread, allocate the number of blocks of memory specified by the second
67 argument.  The third argument controls the size of the blocks to be
68 allocated.  The main thread creates blocks of this size, the second thread
69 created by the program allocates blocks of twice this size, the third thread
70 allocates blocks of three times this size, and so on.
71
72 The program calls \fBmalloc_info\fP()  twice to display the memory\-allocation
73 state.  The first call takes place before any threads are created or memory
74 allocated.  The second call is performed after all threads have allocated
75 memory.
76
77 In the following example, the command\-line arguments specify the creation of
78 one additional thread, and both the main thread and the additional thread
79 allocate 10000 blocks of memory.  After the blocks of memory have been
80 allocated, \fBmalloc_info\fP()  shows the state of two allocation arenas.
81 .in +4
82 .nf
83
84 $ \fBgetconf GNU_LIBC_VERSION\fP
85 glibc 2.13
86 $ \fB./a.out 1 10000 100\fP
87 ============ Before allocating blocks ============
88 <malloc version="1">
89 <heap nr="0">
90 <sizes>
91 </sizes>
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"/>
98 </heap>
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"/>
105 </malloc>
106
107 ============ After allocating blocks ============
108 <malloc version="1">
109 <heap nr="0">
110 <sizes>
111 </sizes>
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"/>
118 </heap>
119 <heap nr="1">
120 <sizes>
121 </sizes>
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"/>
128 </heap>
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"/>
135 </malloc>
136 .fi
137 .in
138 .SS プログラムのソース
139 .nf
140
141 #include <unistd.h>
142 #include <stdlib.h>
143 #include <pthread.h>
144 #include <malloc.h>
145 #include <errno.h>
146
147 static size_t blockSize;
148 static int numThreads, numBlocks;
149
150 #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \e
151                         } while (0)
152
153 static void *
154 thread_func(void *arg)
155 {
156     int j;
157     int tn = (int) arg;
158
159     /* The multiplier \(aq(2 + tn)\(aq ensures that each thread (including
160        the main thread) allocates a different amount of memory */
161
162     for (j = 0; j < numBlocks; j++)
163         if (malloc(blockSize * (2 + tn)) == NULL)
164             errExit("malloc\-thread");
165
166     sleep(100);         /* Sleep until main thread terminates */
167     return NULL;
168 }
169
170 int
171 main(int argc, char *argv[])
172 {
173     int j, tn, sleepTime;
174     pthread_t *thr;
175
176     if (argc < 4) {
177         fprintf(stderr,
178                 "%s num\-threads num\-blocks block\-size [sleep\-time]\en",
179                 argv[0]);
180         exit(EXIT_FAILURE);
181     }
182
183     numThreads = atoi(argv[1]);
184     numBlocks = atoi(argv[2]);
185     blockSize = atoi(argv[3]);
186     sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
187
188     thr = calloc(numThreads, sizeof(pthread_t));
189     if (thr == NULL)
190         errExit("calloc");
191
192     printf("============ Before allocating blocks ============\en");
193     malloc_info(0, stdout);
194
195     /* Create threads that allocate different amounts of memory */
196
197     for (tn = 0; tn < numThreads; tn++) {
198         errno = pthread_create(&thr[tn], NULL, thread_func,
199                                (void *) tn);
200         if (errno != 0)
201             errExit("pthread_create");
202
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)). */
207
208         if (sleepTime > 0)
209             sleep(sleepTime);
210     }
211
212     /* The main thread also allocates some memory */
213
214     for (j = 0; j < numBlocks; j++)
215         if (malloc(blockSize) == NULL)
216             errExit("malloc");
217
218     sleep(2);           /* Give all threads a chance to
219                            complete allocations */
220
221     printf("\en============ After allocating blocks ============\en");
222     malloc_info(0, stdout);
223
224     exit(EXIT_SUCCESS);
225 }
226 .fi
227 .SH 関連項目
228 \fBmallinfo\fP(3), \fBmalloc\fP(3), \fBmalloc_stats\fP(3), \fBmallopt\fP(3),
229 \fBopen_memstream\fP(3)
230 .SH この文書について
231 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.52 の一部
232 である。プロジェクトの説明とバグ報告に関する情報は
233 http://www.kernel.org/doc/man\-pages/ に書かれている。