OSDN Git Service

Update release for LDP 3.67
[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 .\"
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 以下のプログラムは最大で 4 つのコマンドライン引数を取り、 最初の 3 つは必須である。 最初の引数は、このプログラムが生成するスレッド数を指定する。
70 メインスレッドを含む全てのスレッドが第 2 引数で指定した数のメモリブロックの割り当てを行う。 第 3 引数は割り当てるブロックのサイズを制御する。
71 メインスレッドはこのサイズのブロックを作成し、 このプログラムが生成する 2 番目のスレッドはこのサイズの 2 倍のサイズのブロックを、 3
72 番目のスレッドはこのサイズの 3 倍のサイズのブロックを割り当て、 以下同様である。
73
74 このプログラムは \fBmalloc_info\fP() を 2 回呼び出し、メモリ割り当て状態を表示する。
75 最初の呼び出しはスレッドの生成もメモリの割り当ても行われる前に実行される。 2 回目の呼び出しは全てのスレッドがメモリ割り当てを行った後に実行される。
76
77 以下の例では、 コマンドライン引数で、 追加でスレッドを一つ生成し、メインスレッドと追加のスレッドの両方が 10000
78 個のメモリブロックを割り当てるように指定している。 メモリブロックの割り当て後の \fBmalloc_info\fP() では、 2
79 つの割り当て領域の状態が表示されている。
80 .in +4
81 .nf
82
83 $ \fBgetconf GNU_LIBC_VERSION\fP
84 glibc 2.13
85 $ \fB./a.out 1 10000 100\fP
86 ============ Before allocating blocks ============
87 <malloc version="1">
88 <heap nr="0">
89 <sizes>
90 </sizes>
91 <total type="fast" count="0" size="0"/>
92 <total type="rest" count="0" size="0"/>
93 <system type="current" size="135168"/>
94 <system type="max" size="135168"/>
95 <aspace type="total" size="135168"/>
96 <aspace type="mprotect" size="135168"/>
97 </heap>
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 </malloc>
105
106 ============ After allocating blocks ============
107 <malloc version="1">
108 <heap nr="0">
109 <sizes>
110 </sizes>
111 <total type="fast" count="0" size="0"/>
112 <total type="rest" count="0" size="0"/>
113 <system type="current" size="1081344"/>
114 <system type="max" size="1081344"/>
115 <aspace type="total" size="1081344"/>
116 <aspace type="mprotect" size="1081344"/>
117 </heap>
118 <heap nr="1">
119 <sizes>
120 </sizes>
121 <total type="fast" count="0" size="0"/>
122 <total type="rest" count="0" size="0"/>
123 <system type="current" size="1032192"/>
124 <system type="max" size="1032192"/>
125 <aspace type="total" size="1032192"/>
126 <aspace type="mprotect" size="1032192"/>
127 </heap>
128 <total type="fast" count="0" size="0"/>
129 <total type="rest" count="0" size="0"/>
130 <system type="current" size="2113536"/>
131 <system type="max" size="2113536"/>
132 <aspace type="total" size="2113536"/>
133 <aspace type="mprotect" size="2113536"/>
134 </malloc>
135 .fi
136 .in
137 .SS プログラムのソース
138 .nf
139
140 #include <unistd.h>
141 #include <stdlib.h>
142 #include <pthread.h>
143 #include <malloc.h>
144 #include <errno.h>
145
146 static size_t blockSize;
147 static int numThreads, numBlocks;
148
149 #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \e
150                         } while (0)
151
152 static void *
153 thread_func(void *arg)
154 {
155     int j;
156     int tn = (int) arg;
157
158     /* The multiplier \(aq(2 + tn)\(aq ensures that each thread (including
159        the main thread) allocates a different amount of memory */
160
161     for (j = 0; j < numBlocks; j++)
162         if (malloc(blockSize * (2 + tn)) == NULL)
163             errExit("malloc\-thread");
164
165     sleep(100);         /* Sleep until main thread terminates */
166     return NULL;
167 }
168
169 int
170 main(int argc, char *argv[])
171 {
172     int j, tn, sleepTime;
173     pthread_t *thr;
174
175     if (argc < 4) {
176         fprintf(stderr,
177                 "%s num\-threads num\-blocks block\-size [sleep\-time]\en",
178                 argv[0]);
179         exit(EXIT_FAILURE);
180     }
181
182     numThreads = atoi(argv[1]);
183     numBlocks = atoi(argv[2]);
184     blockSize = atoi(argv[3]);
185     sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
186
187     thr = calloc(numThreads, sizeof(pthread_t));
188     if (thr == NULL)
189         errExit("calloc");
190
191     printf("============ Before allocating blocks ============\en");
192     malloc_info(0, stdout);
193
194     /* Create threads that allocate different amounts of memory */
195
196     for (tn = 0; tn < numThreads; tn++) {
197         errno = pthread_create(&thr[tn], NULL, thread_func,
198                                (void *) tn);
199         if (errno != 0)
200             errExit("pthread_create");
201
202         /* If we add a sleep interval after the start\-up of each
203            thread, the threads likely won\(aqt contend for malloc
204            mutexes, and therefore additional arenas won\(aqt be
205            allocated (see malloc(3)). */
206
207         if (sleepTime > 0)
208             sleep(sleepTime);
209     }
210
211     /* The main thread also allocates some memory */
212
213     for (j = 0; j < numBlocks; j++)
214         if (malloc(blockSize) == NULL)
215             errExit("malloc");
216
217     sleep(2);           /* Give all threads a chance to
218                            complete allocations */
219
220     printf("\en============ After allocating blocks ============\en");
221     malloc_info(0, stdout);
222
223     exit(EXIT_SUCCESS);
224 }
225 .fi
226 .SH 関連項目
227 \fBmallinfo\fP(3), \fBmalloc\fP(3), \fBmalloc_stats\fP(3), \fBmallopt\fP(3),
228 \fBopen_memstream\fP(3)
229 .SH この文書について
230 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.67 の一部
231 である。プロジェクトの説明とバグ報告に関する情報は
232 http://www.kernel.org/doc/man\-pages/ に書かれている。