malloc_info(3) - wariua/manpages-ko GitHub Wiki
malloc_info - malloc ์ํ๋ฅผ ์คํธ๋ฆผ์ผ๋ก ๋ด๋ณด๋ด๊ธฐ
#include <malloc.h>
int malloc_info(int options, FILE *stream);
malloc_info()
ํจ์๋ ํธ์ถ์ ๋ด์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น ๊ตฌํ์ ํ์ฌ ์ํ๋ฅผ ๊ธฐ์ ํ๋ XML ๋ฌธ์์ด์ ๋ด๋ณด๋ธ๋ค. ํ์ผ ์คํธ๋ฆผ stream
์ผ๋ก ๊ทธ ๋ฌธ์์ด์ ์ฐ๋๋ค. ๋ด๋ณด๋ด๋ ๋ฌธ์์ด์๋ ๋ชจ๋ ์๋ ๋์ ๋ํ ์ ๋ณด๊ฐ ํฌํจ๋์ด ์๋ค. (malloc(3) ์ฐธ๊ณ .)
ํ์ฌ ๊ตฌํ ๊ธฐ์ค์ผ๋ก options
๋ 0์ด์ด์ผ ํ๋ค.
์ฑ๊ณต ์ malloc_info()
๋ 0์ ๋ฐํํ๋ค. ์ค๋ฅ ์ -1์ ๋ฐํํ๋ฉฐ ์์ธ์ ๋ํ๋ด๋๋ก errno
๋ฅผ ์ค์ ํ๋ค.
-
options
๊ฐ 0์ด ์๋๋ค.
EINVAL
glibc ๋ฒ์ 2.10์์ malloc_info()
๊ฐ ์ถ๊ฐ๋์๋ค.
์ด ์ ์์ ์ฌ์ฉํ๋ ์ฉ์ด๋ค์ ๋ํ ์ค๋ช ์ attributes(7)๋ฅผ ๋ณด๋ผ.
์ธํฐํ์ด์ค | ์์ฑ | ๊ฐ |
---|---|---|
malloc_info() |
์ค๋ ๋ ์์ ์ฑ | MT-Safe |
์ด ํจ์๋ GNU ํ์ฅ์ด๋ค.
๋ฉ๋ชจ๋ฆฌ ํ ๋น ์ ๋ณด๋ฅผ (C ๊ตฌ์กฐ์ฒด๊ฐ ์๋๋ผ) XML ๋ฌธ์์ด๋ก ์ ๊ณตํ๋ ์ด์ ๋ ์๊ฐ์ด ํ๋ฅด๋ฉด์ (๊ธฐ๋ฐ ๊ตฌํ์ ๋ณํ์ ๋ฐ๋ผ) ์ ๋ณด๊ฐ ๋ฐ๋ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค. ์ถ๋ ฅ XML ๋ฌธ์์ด์ ๋ฒ์ ํ๋๊ฐ ๋ค์ด ์๋ค.
open_memstream(3) ํจ์๋ฅผ ์ฐ๋ฉด malloc_info()
์ถ๋ ฅ์ ํ์ผ์ด ์๋๋ผ ๋ฉ๋ชจ๋ฆฌ ๋ด ๋ฒํผ๋ก ๋ฐ๋ก ๋ณด๋ผ ์ ์๋ค.
malloc_info()
ํจ์๋ malloc_stats(3)์ mallinfo(3)์ ๊ฒฐ์ ๋ค์ ํด๊ฒฐํ๋๋ก ์ค๊ณ๋์๋ค.
์๋ ํ๋ก๊ทธ๋จ์ 4๊ฐ๊น์ง์ ๋ช ๋ นํ ์ธ์๋ฅผ ๋ฐ์ผ๋ฉฐ, ๊ทธ ์ค ์ฒ์ 3๊ฐ๋ ํ์์ด๋ค. ์ฒซ ๋ฒ์งธ ์ธ์๋ ํ๋ก๊ทธ๋จ์ด ์์ฑํด์ผ ํ๋ ์ค๋ ๋ ์๋ฅผ ์ง์ ํ๋ค. ๋ฉ์ธ ์ค๋ ๋๋ฅผ ํฌํจํ ๋ชจ๋ ์ค๋ ๋์์ ๋ ๋ฒ์งธ ์ธ์๋ก ์ง์ ํ ๊ฐ์์ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ก์ ํ ๋นํ๋ค. ์ธ ๋ฒ์งธ ์ธ์๋ ํ ๋นํ ๋ธ๋ก์ ํฌ๊ธฐ๋ฅผ ์ ์ดํ๋ค. ๋ฉ์ธ ์ค๋ ๋๋ ์ด ํฌ๊ธฐ์ ๋ธ๋ก์ ๋ง๋ค๊ณ , ๋ ๋ฒ์งธ ์ค๋ ๋๋ ๋ ๋ฐฐ ํฌ๊ธฐ ๋ธ๋ก์ ํ ๋นํ๊ณ , ์ธ ๋ฒ์งธ ์ค๋ ๋๋ ์ธ ๋ฐฐ ํฌ๊ธฐ ๋ธ๋ก์ ํ ๋นํ๊ณ ํ๋ ์์ด๋ค.
ํ๋ก๊ทธ๋จ์์ malloc_info()
๋ฅผ ๋ ๋ฒ ํธ์ถํด์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น ์ํ๋ฅผ ํ์ํ๋ค. ์ฒซ ๋ฒ์งธ ํธ์ถ์ ์ค๋ ๋ ์์ฑ์ด๋ ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ํ๊ธฐ ์ ์ ํ๋ค. ๋ ๋ฒ์งธ ํธ์ถ์ ๋ชจ๋ ์ค๋ ๋๊ฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ ํ์ ์ํํ๋ค.
๋ค์ ์์ ๋ช
๋ นํ ์ธ์๋ ์ถ๊ฐ ์ค๋ ๋๋ฅผ 1๊ฐ ๋ง๋ค๊ณ ๋ฉ์ธ ์ค๋ ๋์ ๊ทธ ์ถ๊ฐ ์ค๋ ๋ ๋ชจ๋ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ก 10000๊ฐ๋ฅผ ํ ๋นํ๋ผ๋ ๋ป์ด๋ค. ๊ทธ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ก๋ค์ ํ ๋นํ ํ์ malloc_info()
๋ก ๋ ํ ๋น ์๋ ๋์ ์ํ๋ฅผ ๋ณด์ธ๋ค.
$ getconf GNU_LIBC_VERSION
glibc 2.13
$ ./a.out 1 10000 100
============ Before allocating blocks ============
<malloc version="1">
<heap nr="0">
<sizes>
</sizes>
<total type="fast" count="0" size="0"/>
<total type="rest" count="0" size="0"/>
<system type="current" size="135168"/>
<system type="max" size="135168"/>
<aspace type="total" size="135168"/>
<aspace type="mprotect" size="135168"/>
</heap>
<total type="fast" count="0" size="0"/>
<total type="rest" count="0" size="0"/>
<system type="current" size="135168"/>
<system type="max" size="135168"/>
<aspace type="total" size="135168"/>
<aspace type="mprotect" size="135168"/>
</malloc>
============ After allocating blocks ============
<malloc version="1">
<heap nr="0">
<sizes>
</sizes>
<total type="fast" count="0" size="0"/>
<total type="rest" count="0" size="0"/>
<system type="current" size="1081344"/>
<system type="max" size="1081344"/>
<aspace type="total" size="1081344"/>
<aspace type="mprotect" size="1081344"/>
</heap>
<heap nr="1">
<sizes>
</sizes>
<total type="fast" count="0" size="0"/>
<total type="rest" count="0" size="0"/>
<system type="current" size="1032192"/>
<system type="max" size="1032192"/>
<aspace type="total" size="1032192"/>
<aspace type="mprotect" size="1032192"/>
</heap>
<total type="fast" count="0" size="0"/>
<total type="rest" count="0" size="0"/>
<system type="current" size="2113536"/>
<system type="max" size="2113536"/>
<aspace type="total" size="2113536"/>
<aspace type="mprotect" size="2113536"/>
</malloc>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <malloc.h>
#include <errno.h>
static size_t blockSize;
static int numThreads, numBlocks;
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
static void *
thread_func(void *arg)
{
int j;
int tn = (int) arg;
/* '(2 + tn)'์ ๊ณฑํด ์ฃผ๋ฉด ๊ฐ ์ค๋ ๋(๋ฉ์ธ ์ค๋ ๋ ํฌํจ)๊ฐ
๊ฐ๊ธฐ ๋ค๋ฅธ ์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ๊ฒ ๋๋ค */
for (j = 0; j < numBlocks; j++)
if (malloc(blockSize * (2 + tn)) == NULL)
errExit("malloc-thread");
sleep(100); /* ๋ฉ์ธ ์ค๋ ๋๊ฐ ์ข
๋ฃํ ๋๊น์ง ์ ๋ค๊ธฐ */
return NULL;
}
int
main(int argc, char *argv[])
{
int j, tn, sleepTime;
pthread_t *thr;
if (argc < 4) {
fprintf(stderr,
"%s num-threads num-blocks block-size [sleep-time]\n",
argv[0]);
exit(EXIT_FAILURE);
}
numThreads = atoi(argv[1]);
numBlocks = atoi(argv[2]);
blockSize = atoi(argv[3]);
sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
thr = calloc(numThreads, sizeof(pthread_t));
if (thr == NULL)
errExit("calloc");
printf("============ Before allocating blocks ============\n");
malloc_info(0, stdout);
/* ๊ฐ๊ธฐ ๋ค๋ฅธ ์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ๋ ์ค๋ ๋ ์์ฑ */
for (tn = 0; tn < numThreads; tn++) {
errno = pthread_create(&thr[tn], NULL, thread_func,
(void *) tn);
if (errno != 0)
errExit("pthread_create");
/* ์ค๋ ๋ ์์ ํ๋ง๋ค ์ ์๋ ๊ฐ๊ฒฉ์ ์ถ๊ฐํ๋ฉด ์ค๋ ๋๋ค์ด
malloc ๋ฎคํ
์ค๋ฅผ ๋๊ณ ๊ฒฝ์ํ์ง ์๊ฒ ๋๊ณ , ๊ทธ๋์ ์๋ ๋๋ฅผ
์ถ๊ฐ๋ก ํ ๋นํ์ง ์๊ฒ ๋จ. (malloc(3) ์ฐธ๊ณ .) */
if (sleepTime > 0)
sleep(sleepTime);
}
/* ๋ฉ์ธ ์ค๋ ๋์์๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ข ํ ๋น */
for (j = 0; j < numBlocks; j++)
if (malloc(blockSize) == NULL)
errExit("malloc");
sleep(2); /* ๋ชจ๋ ์ค๋ ๋๊ฐ ํ ๋น์ ๋ง์น ์๊ฐ ์ฃผ๊ธฐ */
printf("\n============ After allocating blocks ============\n");
malloc_info(0, stdout);
exit(EXIT_SUCCESS);
}
mallinfo(3), malloc(3), malloc_stats(3), mallopt(3), open_memstream(3)
2019-03-06