>From d95eba3d44b7e3154fc2a89755494f80d49e0e59 Mon Sep 17 00:00:00 2001 From: Dominique Martinet Date: Wed, 25 Jan 2023 16:19:37 +0900 Subject: [PATCH] mallocng debug statements --- src/malloc/mallocng/malloc.c | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/malloc/mallocng/malloc.c b/src/malloc/mallocng/malloc.c index d695ab8ec982..99855e7e0bd9 100644 --- a/src/malloc/mallocng/malloc.c +++ b/src/malloc/mallocng/malloc.c @@ -39,6 +39,24 @@ static const uint8_t med_cnt_tab[4] = { 28, 24, 20, 32 }; struct malloc_context ctx = { 0 }; +char dbg2_buf[1024*3] = { 0 }; +size_t dbg2_off = 0; + +#define dbg2(fmt, args...) do { \ + if (dbg2_off > sizeof(dbg2_buf) - 100) dbg2_off = 0; \ + dbg2_off += snprintf(dbg2_buf + dbg2_off, sizeof(dbg2_buf) - dbg2_off - 1, \ + "%s (%d): " fmt "\n", __func__, __LINE__, ##args); \ +} while (0) + +char dbg_buf[1024*100] = { 0 }; +size_t dbg_off = 0; + +#define dbg(fmt, args...) do { \ + if (dbg_off > sizeof(dbg_buf) - 200) dbg_off = 0; \ + dbg_off += snprintf(dbg_buf + dbg_off, sizeof(dbg_buf) - dbg_off - 1, \ + "%s (%d): " fmt "\n", __func__, __LINE__, ##args); \ +} while (0) + struct meta *alloc_meta(void) { struct meta *m; @@ -123,9 +141,13 @@ static uint32_t try_avail(struct meta **pm) dequeue(pm, m); m = *pm; if (!m) return 0; + if (m->sizeclass == 0) + dbg("new m: %p, avail %x, freed %x", m, m->avail_mask, m->freed_mask); } else { m = m->next; *pm = m; + if (m->sizeclass == 0) + dbg("new m: %p, avail %x, freed %x", m, m->avail_mask, m->freed_mask); } mask = m->freed_mask; @@ -136,6 +158,8 @@ static uint32_t try_avail(struct meta **pm) m = m->next; *pm = m; mask = m->freed_mask; + if (m->sizeclass == 0) + dbg("new m: %p, avail %x, freed %x", m, m->avail_mask, m->freed_mask); } // activate more slots in a not-fully-active group @@ -143,10 +167,19 @@ static uint32_t try_avail(struct meta **pm) // any other group with free slots. this avoids // touching & dirtying as-yet-unused pages. if (!(mask & ((2u<mem->active_idx)-1))) { + if (m->sizeclass == 0) { + dbg("mask %x, mem active_idx: %d, m/m->next %p/%p", mask, m->mem->active_idx, m, m->next); + } if (m->next != m) { m = m->next; *pm = m; } else { + if (m->sizeclass == 0) { + dbg("BUGGED"); + char msg[] = "\n\nSHOULD NEVER GET HERE!\n\n\n"; + write(2, msg, sizeof(msg)); + while(1); + } int cnt = m->mem->active_idx + 2; int size = size_classes[m->sizeclass]*UNIT; int span = UNIT + size*cnt; @@ -280,11 +313,18 @@ static struct meta *alloc_group(int sc, size_t req) m->last_idx = cnt-1; m->freeable = 1; m->sizeclass = sc; + dbg2("%p: sc %d idx %d", m, sc, active_idx); + dbg("%p: sc %d idx %d", m, sc, active_idx); return m; } static int alloc_slot(int sc, size_t req) { + if (sc == 0) { + dbg("%p: avail %x, freed %x", ctx.active[sc], + ctx.active[sc] ? ctx.active[sc]->avail_mask : 0, + ctx.active[sc] ? ctx.active[sc]->freed_mask : 0); + } uint32_t first = try_avail(&ctx.active[sc]); if (first) return a_ctz_32(first); @@ -293,6 +333,9 @@ static int alloc_slot(int sc, size_t req) g->avail_mask--; queue(&ctx.active[sc], g); + if (sc == 0) { + dbg("%p: avail %x", g, g->avail_mask); + } return 0; } -- 2.39.0