malloc_info(3) Library Functions Manual malloc_info(3)

malloc_info - Exporter l'état de malloc vers un flux

Bibliothèque C standard (libc, -lc)

#include <malloc.h>
int malloc_info(int options, FILE *stream);

La fonction malloc_info() exporte une chaîne XML décrivant l'état actuel de l'allocation mémoire pour l'appelant. Cette chaîne est écrite dans le flux stream. La chaîne exportée contient des informations concernant toutes les domaines de mémoires (« arenas », consultez malloc(3)).

Dans la mise en œuvre actuelle, le paramètre options doit être nul.

En cas de succès, malloc_info() renvoie 0. En cas d'erreur, -1 est renvoyé et errno contient le code d'erreur.

Le paramètre options n'était pas nul.

Pour une explication des termes utilisés dans cette section, consulter attributes(7).

Interface Attribut Valeur
malloc_info() Sécurité des threads MT-Safe

GNU.

glibc 2.10.

L'information d'allocation mémoire est fournie sous forme de flux XML (plutôt que de structure C), car les informations fournies pourront évoluer dans le futur, en raison de modifications dans le code mis en œuvre. La sortie XML comporte un champ de version.

La fonction open_memstream(3) peut être utilisée pour envoyer la sortie de malloc_info() directement dans un tampon en mémoire plutôt que dans un fichier.

La fonction malloc_info() est conçue pour combler les manques de malloc_stats(3) et mallinfo(3).

Le programme ci-dessous accepte jusqu'à quatre paramètres en ligne de commande dont les trois premiers sont obligatoires. Le premier paramètre indique le nombre de processus légers (« threads ») que le programme doit créer. Tous les threads, thread principal compris, allouent le nombre de blocs de mémoire indiqué en deuxième paramètre. Le troisième paramètre contrôle la taille des blocs à allouer. Le thread principal crée des blocs de cette taille, le deuxième thread créé par le programme alloue des blocs deux fois plus grands, le troisième thread alloue des blocs trois fois plus grands et ainsi de suite.

Le programme appelle malloc_info() deux fois pour afficher l'état de l'allocation mémoire. Le premier appel est effectué avant la création d’un thread ou l'allocation de mémoire. Le deuxième appel est effectué une fois que tous les threads ont alloué de la mémoire.

Dans l'exemple suivant, les paramètres commandent la création d'un thread additionnel, allouant 10 000 blocs de mémoire, comme le thread principal. Une fois les blocs de mémoire alloués, malloc_info() affiche l'état des deux domaines de mémoire.


$ 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 <err.h>
#include <errno.h>
#include <malloc.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
static size_t        blockSize;
static size_t        numThreads;
static unsigned int  numBlocks;
static void *
thread_func(void *arg)
{
    int tn = (int) arg;
    /* The multiplier '(2 + tn)' ensures that each thread (including
       the main thread) allocates a different amount of memory. */
    for (unsigned int j = 0; j < numBlocks; j++)
        if (malloc(blockSize * (2 + tn)) == NULL)
            err(EXIT_FAILURE, "malloc-thread");
    sleep(100);         /* Sleep until main thread terminates. */
    return NULL;
}
int
main(int argc, char *argv[])
{
    int        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(*thr));
    if (thr == NULL)
        err(EXIT_FAILURE, "calloc");
    printf("============ Before allocating blocks ============\n");
    malloc_info(0, stdout);
    /* Create threads that allocate different amounts of memory. */
    for (size_t tn = 0; tn < numThreads; tn++) {
        errno = pthread_create(&thr[tn], NULL, thread_func,
                               (void *) tn);
        if (errno != 0)
            err(EXIT_FAILURE, "pthread_create");
        /* If we add a sleep interval after the start-up of each
           thread, the threads likely won't contend for malloc
           mutexes, and therefore additional arenas won't be
           allocated (see malloc(3)). */
        if (sleepTime > 0)
            sleep(sleepTime);
    }
    /* The main thread also allocates some memory. */
    for (unsigned int j = 0; j < numBlocks; j++)
        if (malloc(blockSize) == NULL)
            err(EXIT_FAILURE, "malloc");
    sleep(2);           /* Give all threads a chance to
                           complete allocations. */
    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)

La traduction française de cette page de manuel a été créée par Christophe Blaess https://www.blaess.fr/christophe/, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org> et Grégoire Scano <gregoire.scano@malloc.fr>

Cette traduction est une documentation libre ; veuillez vous reporter à la GNU General Public License version 3 concernant les conditions de copie et de distribution. Il n'y a aucune RESPONSABILITÉ LÉGALE.

Si vous découvrez un bogue dans la traduction de cette page de manuel, veuillez envoyer un message à debian-l10n-french@lists.debian.org.

20 juillet 2023 Pages du manuel de Linux 6.05.01