OSDN Git Service

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