added some more statistics in xma

This commit is contained in:
hyung-hwan 2025-06-15 17:23:23 +09:00
parent 02748cd24d
commit af13a3363c
3 changed files with 200 additions and 142 deletions

View File

@ -72,9 +72,7 @@
*/
#include <hcl-cmn.h>
#ifdef HCL_BUILD_DEBUG
#define HCL_XMA_ENABLE_STAT
#endif
/** @struct hcl_xma_t
* The hcl_xma_t type defines a simple memory allocator over a memory zone.
@ -105,14 +103,22 @@ struct hcl_xma_t
/** pre-computed value for fast xfree index calculation */
hcl_oow_t bdec;
#ifdef HCL_XMA_ENABLE_STAT
#if defined(HCL_XMA_ENABLE_STAT)
struct
{
hcl_oow_t total;
hcl_oow_t alloc;
hcl_oow_t avail;
hcl_oow_t nused;
hcl_oow_t nfree;
hcl_oow_t alloc; /* allocated size */
hcl_oow_t avail; /* available size */
hcl_oow_t nused; /* nubmer of used blocks */
hcl_oow_t nfree; /* number of free blocks */
hcl_oow_t alloc_hwmark; /* high watermark - highest total memory ever allocated */
hcl_oow_t nallocops; /* number of alloc operations */
hcl_oow_t nallocgoodops; /* number of successful alloc operations */
hcl_oow_t nallocbadops; /* number of failed alloc operations */
hcl_oow_t nreallocops; /* number of realloc operations */
hcl_oow_t nreallocgoodops; /* number of good realloc operations */
hcl_oow_t nreallocbadops; /* number of failed realloc operations - could fall back to normal alloc*/
hcl_oow_t nfreeops; /* number of free operations */
} stat;
#endif
};

View File

@ -977,8 +977,7 @@ hcl_pfbase_t* hcl_querymod (hcl_t* hcl, const hcl_ooch_t* pfid, hcl_oow_t pfidle
*mod = &mdp->mod;
HCL_DEBUG4 (hcl, "Found a primitive function [%.*js] in a module [%js] - %p\n",
pfidlen - mod_name_len - 1, sep + 1, mdp->mod.name, pfbase);
HCL_DEBUG4(hcl, "Found a primitive function [%.*js] in a module [%js] - %p\n", pfidlen - mod_name_len - 1, sep + 1, mdp->mod.name, pfbase);
return pfbase;
}

View File

@ -272,6 +272,15 @@ int hcl_xma_init (hcl_xma_t* xma, hcl_mmgr_t* mmgr, void* zoneptr, hcl_oow_t zon
xma->stat.avail = zonesize - MBLKHDRSIZE;
xma->stat.nfree = 1;
xma->stat.nused = 0;
xma->stat.alloc_hwmark = 0;
xma->stat.nallocops = 0;
xma->stat.nallocgoodops = 0;
xma->stat.nallocbadops = 0;
xma->stat.nreallocops = 0;
xma->stat.nreallocgoodops = 0;
xma->stat.nreallocbadops = 0;
xma->stat.nfreeops = 0;
#endif
return 0;
@ -394,6 +403,7 @@ static hcl_xma_fblk_t* alloc_from_freelist (hcl_xma_t* xma, hcl_oow_t xfi, hcl_o
xma->stat.nused++;
xma->stat.alloc += cand->size;
xma->stat.avail -= cand->size;
if (xma->stat.alloc > xma->stat.alloc_hwmark) xma->stat.alloc_hwmark = xma->stat.alloc;
#endif
return cand;
}
@ -410,6 +420,10 @@ void* hcl_xma_alloc (hcl_xma_t* xma, hcl_oow_t size)
DBG_VERIFY(xma, "alloc start");
#if defined(HCL_XMA_ENABLE_STAT)
xma->stat.nallocops++;
#endif
/* round up 'size' to the multiples of ALIGN */
if (size < MINALLOCSIZE) size = MINALLOCSIZE;
size = HCL_ALIGN_POW2(size, ALIGN);
@ -434,13 +448,20 @@ void* hcl_xma_alloc (hcl_xma_t* xma, hcl_oow_t size)
xma->stat.nused++;
xma->stat.alloc += cand->size;
xma->stat.avail -= cand->size;
if (xma->stat.alloc > xma->stat.alloc_hwmark) xma->stat.alloc_hwmark = xma->stat.alloc;
#endif
}
else if (xfi == XFIMAX(xma))
{
/* huge block */
cand = alloc_from_freelist(xma, XFIMAX(xma), size);
if (!cand) return HCL_NULL;
if (!cand)
{
#if defined(HCL_XMA_ENABLE_STAT)
xma->stat.nallocbadops++;
#endif
return HCL_NULL;
}
}
else
{
@ -469,10 +490,19 @@ void* hcl_xma_alloc (hcl_xma_t* xma, hcl_oow_t size)
cand = alloc_from_freelist(xma, xfi, size);
if (cand) break;
}
if (!cand) return HCL_NULL;
if (!cand)
{
#if defined(HCL_XMA_ENABLE_STAT)
xma->stat.nallocbadops++;
#endif
return HCL_NULL;
}
}
}
#if defined(HCL_XMA_ENABLE_STAT)
xma->stat.nallocgoodops++;
#endif
DBG_VERIFY(xma, "alloc end");
return SYS_TO_USR(cand);
}
@ -529,6 +559,7 @@ static void* _realloc_merge (hcl_xma_t* xma, void* b, hcl_oow_t size)
#if defined(HCL_XMA_ENABLE_STAT)
xma->stat.alloc += req;
xma->stat.avail -= req; /* req + MBLKHDRSIZE(tmp) - MBLKHDRSIZE(n) */
if (xma->stat.alloc > xma->stat.alloc_hwmark) xma->stat.alloc_hwmark = xma->stat.alloc;
#endif
}
else
@ -546,6 +577,7 @@ static void* _realloc_merge (hcl_xma_t* xma, void* b, hcl_oow_t size)
xma->stat.nfree--;
xma->stat.alloc += MBLKHDRSIZE + n->size;
xma->stat.avail -= n->size;
if (xma->stat.alloc > xma->stat.alloc_hwmark) xma->stat.alloc_hwmark = xma->stat.alloc;
#endif
}
}
@ -626,7 +658,7 @@ void* hcl_xma_realloc (hcl_xma_t* xma, void* b, hcl_oow_t size)
{
void* n;
if (b == HCL_NULL)
if (!b)
{
/* 'realloc' with NULL is the same as 'alloc' */
n = hcl_xma_alloc(xma, size);
@ -634,9 +666,15 @@ void* hcl_xma_realloc (hcl_xma_t* xma, void* b, hcl_oow_t size)
else
{
/* try reallocation by merging the adjacent continuous blocks */
#if defined(HAWK_XMA_ENABLE_STAT)
xma->stat.nreallocops++;
#endif
n = _realloc_merge(xma, b, size);
if (!n)
{
#if defined(HCL_XMA_ENABLE_STAT)
xma->stat.nreallocbadops++;
#endif
/* reallocation by merging failed. fall back to the slow
* allocation-copy-free scheme */
n = hcl_xma_alloc(xma, size);
@ -646,6 +684,12 @@ void* hcl_xma_realloc (hcl_xma_t* xma, void* b, hcl_oow_t size)
hcl_xma_free(xma, b);
}
}
else
{
#if defined(HCL_XMA_ENABLE_STAT)
xma->stat.nreallocgoodops++;
#endif
}
}
return n;
@ -665,6 +709,7 @@ void hcl_xma_free (hcl_xma_t* xma, void* b)
/* update statistical variables */
xma->stat.nused--;
xma->stat.alloc -= org_blk_size;
xma->stat.nfreeops++;
#endif
x = prev_mblk(blk);
@ -800,13 +845,14 @@ void hcl_xma_dump (hcl_xma_t* xma, hcl_xma_dumper_t dumper, void* ctx)
hcl_oow_t isum;
#endif
dumper (ctx, "<XMA DUMP>\n");
dumper(ctx, "[XMA DUMP]\n");
#if defined(HCL_XMA_ENABLE_STAT)
dumper(ctx, "== statistics ==\n");
dumper (ctx, "total = %zu\n", xma->stat.total);
dumper (ctx, "alloc = %zu\n", xma->stat.alloc);
dumper (ctx, "avail = %zu\n", xma->stat.avail);
dumper(ctx, "Total = %zu\n", xma->stat.total);
dumper(ctx, "Alloc = %zu\n", xma->stat.alloc);
dumper(ctx, "Avail = %zu\n", xma->stat.avail);
dumper(ctx, "Alloc High Watermark = %zu\n", xma->stat.alloc_hwmark);
#endif
dumper(ctx, "== blocks ==\n");
@ -830,13 +876,20 @@ void hcl_xma_dump (hcl_xma_t* xma, hcl_xma_dumper_t dumper, void* ctx)
#if defined(HCL_XMA_ENABLE_STAT)
dumper(ctx, "Internal use : %18zu bytes\n", isum);
dumper(ctx, "Total : %18zu bytes\n", (asum + fsum + isum));
dumper(ctx, "Alloc operations : %18zu\n", xma->stat.nallocops);
dumper(ctx, "Good alloc operations : %18zu\n", xma->stat.nallocgoodops);
dumper(ctx, "Bad alloc operations : %18zu\n", xma->stat.nallocbadops);
dumper(ctx, "Realloc operations : %18zu\n", xma->stat.nreallocops);
dumper(ctx, "Good realloc operations: %18zu\n", xma->stat.nreallocgoodops);
dumper(ctx, "Bad realloc operations : %18zu\n", xma->stat.nreallocbadops);
dumper(ctx, "Free operations : %18zu\n", xma->stat.nfreeops);
#endif
#if defined(HCL_XMA_ENABLE_STAT)
assert (asum == xma->stat.alloc);
assert (fsum == xma->stat.avail);
assert (isum == xma->stat.total - (xma->stat.alloc + xma->stat.avail));
assert (asum + fsum + isum == xma->stat.total);
HCL_ASSERT(hcl, asum == xma->stat.alloc);
HCL_ASSERT(hcl, fsum == xma->stat.avail);
HCL_ASSERT(hcl, isum == xma->stat.total - (xma->stat.alloc + xma->stat.avail));
HCL_ASSERT(hcl, asum + fsum + isum == xma->stat.total);
#endif
}