lemoncake

rbtree based memory allocator
git clone git://git.2f30.org/lemoncake
Log | Files | Refs | README | LICENSE

commit dd3037569e4a8f8561872b2303862026a2631158
parent 5174e98d13e5153e263fa5626fc4753700a783ac
Author: sin <sin@2f30.org>
Date:   Sat,  3 Aug 2013 13:18:33 +0100

More debug output

Diffstat:
Mlemoncake.c | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 60 insertions(+), 10 deletions(-)

diff --git a/lemoncake.c b/lemoncake.c @@ -8,6 +8,7 @@ #include <string.h> #include <stdint.h> #include <stdbool.h> +#include <stdarg.h> #include <errno.h> #include "tree.h" #include "spinlock.h" @@ -16,10 +17,26 @@ enum { ALIGN = 4 * sizeof(size_t) }; struct lemoncake_ctx { bool init; + /* # of mmap calls */ unsigned long nr_mmap; + /* # of malloc calls */ unsigned long nr_malloc; + /* # of realloc calls */ unsigned long nr_realloc; + /* # of free calls */ unsigned long nr_free; + /* # of nodes in the allocation tree */ + unsigned long nr_at_nodes; + /* # of nodes in the free tree */ + unsigned long nr_ft_nodes; + /* # of new allocations */ + unsigned long nr_alloc_new; + /* # of allocations satisfied from the free tree */ + unsigned long nr_alloc_free; + /* # of shrinked reallocs */ + unsigned long nr_shrink_realloc; + /* # of invalid frees */ + unsigned long nr_invalid_free; }; static struct lemoncake_ctx ctx; @@ -65,22 +82,47 @@ at_cmp(struct node *a, struct node *b) } static void +writelog(int fd, const char *fmt, ...) +{ + va_list ap; + char buf[BUFSIZ]; + + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + write(fd, buf, strlen(buf)); +} + +static void dump_stats(void) { - char buf[BUFSIZ], *p; int fd; + struct node *n; + char *p; fd = open("lemoncake.out", O_WRONLY | O_CREAT | O_APPEND, 0644); if (fd != -1) { p = getenv("_"); - if (p) { - snprintf(buf, sizeof(buf), "Lemoncake stats for process: %s\n", p); - write(fd, buf, strlen(buf)); - } - snprintf(buf, sizeof(buf), - "nr_mmap: %lu, nr_malloc: %lu, nr_realloc: %lu, nr_free: %lu\n", - ctx.nr_mmap, ctx.nr_malloc, ctx.nr_realloc, ctx.nr_free); - write(fd, buf, strlen(buf)); + if (p) + writelog(fd, "*** Lemoncake stats for process: %s ***\n", p); + writelog(fd, "Number of mmap calls: %lu\n", ctx.nr_mmap); + writelog(fd, "Number of malloc calls: %lu\n", ctx.nr_malloc); + writelog(fd, "Number of realloc calls: %lu\n", ctx.nr_realloc); + writelog(fd, "Number of shrinked realloc calls (no memcpy() required): %lu\n", + ctx.nr_shrink_realloc); + writelog(fd, "Number of free calls: %lu\n", ctx.nr_free); + writelog(fd, "Number of new allocations: %lu\n", ctx.nr_alloc_new); + writelog(fd, "Number of allocations from the free tree: %lu\n", + ctx.nr_alloc_free); + writelog(fd, "Number of invalid free calls: %lu\n", ctx.nr_invalid_free); + RB_FOREACH(n, alloc_tree, &at) + ctx.nr_at_nodes++; + RB_FOREACH(n, free_tree, &ft) + ctx.nr_ft_nodes++; + writelog(fd, "Number of nodes in the allocation tree: %lu\n", + ctx.nr_at_nodes); + writelog(fd, "Number of nodes in the free tree: %lu\n", + ctx.nr_ft_nodes); close(fd); } } @@ -168,12 +210,14 @@ malloc(size_t siz) RB_INSERT(alloc_tree, &at, an); unlock(&rblock); ctx.nr_malloc++; + ctx.nr_alloc_new++; return an->buf; } an = RB_REMOVE(free_tree, &ft, res); RB_INSERT(alloc_tree, &at, an); unlock(&rblock); ctx.nr_malloc++; + ctx.nr_alloc_free++; return an->buf; } @@ -202,6 +246,7 @@ realloc(void *oldp, size_t siz) if (res->siz >= siz) { unlock(&rblock); ctx.nr_realloc++; + ctx.nr_shrink_realloc++; return res->buf; } oldan = res; @@ -224,10 +269,12 @@ realloc(void *oldp, size_t siz) } newan->siz = siz; RB_INSERT(alloc_tree, &at, newan); + ctx.nr_alloc_new++; } else { /* Grab the block from the free tree instead */ newan = RB_REMOVE(free_tree, &ft, res); RB_INSERT(alloc_tree, &at, newan); + ctx.nr_alloc_free++; } /* Copy over the contents from `oldp' to the * new memory block */ @@ -277,9 +324,11 @@ free(void *p) if (res) { fn = RB_REMOVE(alloc_tree, &at, res); RB_INSERT(free_tree, &ft, fn); + ctx.nr_free++; + } else { + ctx.nr_invalid_free++; } unlock(&rblock); - ctx.nr_free++; } void @@ -319,6 +368,7 @@ memalign(size_t align, size_t siz) lock(&rblock); RB_INSERT(alloc_tree, &at, an); unlock(&rblock); + ctx.nr_alloc_new++; return p; }