From 166040ed934667cc799cd5dc44e161509ec4322d Mon Sep 17 00:00:00 2001 From: Stathis Kamperis Date: Wed, 10 Oct 2007 07:37:11 +0300 Subject: [PATCH] Implement full simulation --- malloc/test.c | 63 ++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/malloc/test.c b/malloc/test.c index 6e86503..5d613f4 100644 --- a/malloc/test.c +++ b/malloc/test.c @@ -5,10 +5,10 @@ #include "mpool.h" -#define MAXLOGSIZE 10 /* Maximum logarithm of size */ -#define MAXLIFETIME 10 /* Maximum lifetime of reserved blocks */ -#define MAXNODES 10 /* Maximum number of simulation nodes */ -#define TI 200 /* Every `TI' steps dump statistics */ +#define MAXLOGSIZE 18 /* Maximum logarithm of size */ +#define MAXLIFETIME 100 /* Maximum lifetime of reserved blocks */ +#define MAXNODES 20000 /* Maximum number of simulation nodes */ +#define TI 10 /* Every `TI' steps dump statistics */ typedef struct simnode { void *ptr; @@ -21,6 +21,7 @@ typedef struct simhead simhead_t; /* Function prototypes */ void sim_add_to_list(simhead_t *simhead, simnode_t *simnode); +void sim_free_from_list(mpool_t *mpool, simhead_t *simhead, unsigned int t); void sim_print_stats(const mpool_t *mpool, unsigned int t, FILE *fp); int main(void) @@ -34,7 +35,7 @@ int main(void) srand(time(NULL)); /* Initialize memory pool */ - if (mpool_init(&mpool, 20, 3) == MP_ENOMEM) { + if (mpool_init(&mpool, 25, 2) == MP_ENOMEM) { fprintf(stderr, "Not enough memory\n"); exit(EXIT_FAILURE); } @@ -43,26 +44,29 @@ int main(void) LIST_INIT(&simhead); /* Run simulation */ - for (t = 0; ; t++) { - printf("t = %u\n", t); + for (t = 0; t < MAXNODES; t++) { /* Is it time to dump statistics ? */ if (t % TI == 0) - /*sim_print_stats(mpool, t, stdout);*/ + sim_print_stats(mpool, t, stdout); + + /* */ + sim_free_from_list(mpool, &simhead, t); /* Calculate a random size `sz' and a random lifetime `lt' */ sz = (1 << rand() % (1 + MAXLOGSIZE)); lt = 1 + rand() % MAXLIFETIME; - - printf("sz = %u\tlt = %u\n", sz, lt); + /*printf("t = %u\tsz = %u\tlt = %u\n", t, sz, lt);*/ /* Allocate a block of size `sz' and make it last `lt' time intervals */ - printf("mpool = %p\n", mpool); if ((simnode[t].ptr = mpool_alloc(mpool, sz)) == NULL) { - fprintf(stderr, "mpool: no available block found\n"); + fprintf(stderr, "mpool: no available block\n"); mpool_destroy(mpool); exit(EXIT_FAILURE); } - simnode[t].lifetime = lt; + simnode[t].lifetime = t + lt; + + /* Add block to list, in the proper position */ + sim_add_to_list(&simhead, &simnode[t]); } /* Destroy memory pool and free all resources */ @@ -75,16 +79,43 @@ void sim_add_to_list(simhead_t *simhead, simnode_t *simnode) { simnode_t *pnode; + /* + LIST_FOREACH(pnode, simhead, next_block) { + printf("%u -> ", pnode->lifetime); + } + printf("\n"); + */ + LIST_FOREACH(pnode, simhead, next_block) { - if (pnode->lifetime > simnode->lifetime) { + if (simnode->lifetime < pnode->lifetime) { LIST_INSERT_BEFORE(pnode, simnode, next_block); return; } + else if (LIST_NEXT(pnode, next_block) == NULL) { + LIST_INSERT_AFTER(pnode, simnode, next_block); + return; + } } + /* 1st element goes here */ LIST_INSERT_HEAD(simhead, simnode, next_block); } +void sim_free_from_list(mpool_t *mpool, simhead_t *simhead, unsigned int t) +{ + simnode_t *pnode; + + LIST_FOREACH(pnode, simhead, next_block) { + if (t == pnode->lifetime) { + /*printf("freeing %u\n", t);*/ + mpool_free(mpool, pnode->ptr); + LIST_REMOVE(pnode, next_block); + } + else + return; + } +} + void sim_print_stats(const mpool_t *mpool, unsigned int t, FILE *fp) { size_t an, un; /* nodes */ @@ -96,8 +127,8 @@ void sim_print_stats(const mpool_t *mpool, unsigned int t, FILE *fp) me = mpool_stat_get_merges(mpool); sp = mpool_stat_get_splits(mpool); - fprintf(fp, "%u\t%u\t%u\t%f\t%u\t%u\t%f\t%u\t%u\n", - t, an, un, 100.0 * an / (an + un), ab, ub, 100.0 * ab / (ab + ub), sp, me); + fprintf(fp, "%u\t%u\t%u\t%f\t%u\t%u\t%f\t%u\t%u\t%f\n", + t, an, un, 100.0 * an / (an + un), ab, ub, 100.0 * ab / (ab + ub), sp, me, 10.0*sp/(1+me)); /* fprintf(fp, "avail nodes = %u\tused nodes = %u\tfree(%%) = %f\n", an, un, 100.0 * an / (an + un)); -- 2.11.4.GIT