malloc_info(3) - wariua/manpages-ko GitHub Wiki

NAME

malloc_info - malloc ์ƒํƒœ๋ฅผ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋‚ด๋ณด๋‚ด๊ธฐ

SYNOPSIS

#include <malloc.h>

int malloc_info(int options, FILE *stream);

DESCRIPTION

malloc_info() ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœ์ž ๋‚ด์˜ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ๊ตฌํ˜„์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ๊ธฐ์ˆ ํ•˜๋Š” XML ๋ฌธ์ž์—ด์„ ๋‚ด๋ณด๋‚ธ๋‹ค. ํŒŒ์ผ ์ŠคํŠธ๋ฆผ stream์œผ๋กœ ๊ทธ ๋ฌธ์ž์—ด์„ ์ฐ๋Š”๋‹ค. ๋‚ด๋ณด๋‚ด๋Š” ๋ฌธ์ž์—ด์—๋Š” ๋ชจ๋“  ์•„๋ ˆ๋‚˜์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. (malloc(3) ์ฐธ๊ณ .)

ํ˜„์žฌ ๊ตฌํ˜„ ๊ธฐ์ค€์œผ๋กœ options๋Š” 0์ด์–ด์•ผ ํ•œ๋‹ค.

RETURN VALUE

์„ฑ๊ณต ์‹œ malloc_info()๋Š” 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์˜ค๋ฅ˜ ์‹œ -1์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์›์ธ์„ ๋‚˜ํƒ€๋‚ด๋„๋ก errno๋ฅผ ์„ค์ •ํ•œ๋‹ค.

ERRORS

EINVAL
options๊ฐ€ 0์ด ์•„๋‹ˆ๋‹ค.

VERSIONS

glibc ๋ฒ„์ „ 2.10์—์„œ malloc_info()๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

ATTRIBUTES

์ด ์ ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์šฉ์–ด๋“ค์— ๋Œ€ํ•œ ์„ค๋ช…์€ attributes(7)๋ฅผ ๋ณด๋ผ.

์ธํ„ฐํŽ˜์ด์Šค ์†์„ฑ ๊ฐ’
malloc_info() ์Šค๋ ˆ๋“œ ์•ˆ์ „์„ฑ MT-Safe

CONFORMING TO

์ด ํ•จ์ˆ˜๋Š” GNU ํ™•์žฅ์ด๋‹ค.

NOTES

๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ์ •๋ณด๋ฅผ (C ๊ตฌ์กฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ) XML ๋ฌธ์ž์—ด๋กœ ์ œ๊ณตํ•˜๋Š” ์ด์œ ๋Š” ์‹œ๊ฐ„์ด ํ๋ฅด๋ฉด์„œ (๊ธฐ๋ฐ˜ ๊ตฌํ˜„์˜ ๋ณ€ํ™”์— ๋”ฐ๋ผ) ์ •๋ณด๊ฐ€ ๋ฐ”๋€” ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ถœ๋ ฅ XML ๋ฌธ์ž์—ด์— ๋ฒ„์ „ ํ•„๋“œ๊ฐ€ ๋“ค์–ด ์žˆ๋‹ค.

open_memstream(3) ํ•จ์ˆ˜๋ฅผ ์“ฐ๋ฉด malloc_info() ์ถœ๋ ฅ์„ ํŒŒ์ผ์ด ์•„๋‹ˆ๋ผ ๋ฉ”๋ชจ๋ฆฌ ๋‚ด ๋ฒ„ํผ๋กœ ๋ฐ”๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

malloc_info() ํ•จ์ˆ˜๋Š” malloc_stats(3)์™€ mallinfo(3)์˜ ๊ฒฐ์ ๋“ค์„ ํ•ด๊ฒฐํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ๋‹ค.

EXAMPLE

์•„๋ž˜ ํ”„๋กœ๊ทธ๋žจ์€ 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);
}

SEE ALSO

mallinfo(3), malloc(3), malloc_stats(3), mallopt(3), open_memstream(3)


2019-03-06

โš ๏ธ **GitHub.com Fallback** โš ๏ธ