From 1544a66bad7aef9470cc2c6c123b7145c72ccbab Mon Sep 17 00:00:00 2001 From: phil Date: Mon, 30 Nov 1998 14:53:05 +0000 Subject: [PATCH] added lifespan analyser --- mm/lifespan.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ mm/lifespan.h | 24 ++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 mm/lifespan.c create mode 100644 mm/lifespan.h diff --git a/mm/lifespan.c b/mm/lifespan.c new file mode 100644 index 000000000..15e0b77a4 --- /dev/null +++ b/mm/lifespan.c @@ -0,0 +1,106 @@ +/* + * cacao/mm/lifespan.c + * $Id: lifespan.c 95 1998-11-30 14:53:05Z phil $ + */ + +#include "lifespan.h" +#include +#include +#include + +typedef struct { + unsigned long time; + unsigned long size; +} lifespan_object; + +static unsigned long current_time = 0; +static lifespan_object** lifespan_objects = NULL; +static lifespan_object** lifespan_objects_end = NULL; +static void* lifespan_objects_off = NULL; +static FILE* lifespan_file = NULL; + +void lifespan_init(void* heap_base, unsigned long heap_size) +{ + current_time = 0; + + lifespan_objects = (lifespan_object**)malloc(heap_size); + lifespan_objects_end = ((lifespan_object**)((unsigned long)lifespan_objects + (unsigned long)heap_size)); + lifespan_objects_off = (void*)((unsigned long)lifespan_objects - (unsigned long)heap_base); + lifespan_file = popen("gzip -9 >lifespans.gz", "w"); + + if (!lifespan_objects || !lifespan_file) { + fprintf(stderr, "Failed to allocate memory for lifespan-object index / popen lifespan output stream\n"); + exit(-1); + } + + memset(lifespan_objects, 0, heap_size); +} + +static inline void lifespan_free_object(lifespan_object** o) +{ + /* file format: alloc time, size, lifespan */ + + if (*o) { + fprintf(lifespan_file, "%ld\t%ld\t%ld\n", (*o)->time, (*o)->size, current_time - (*o)->time); + free(*o); + *o = NULL; + } +} + +void lifespan_close() +{ + if (lifespan_objects) { + while(lifespan_objects < lifespan_objects_end) + lifespan_free_object(--lifespan_objects_end); + + free(lifespan_objects); + } + + if (lifespan_file) + pclose(lifespan_file); +} + +void lifespan_emit() +{ + /* emit summary */ +} + +void lifespan_free(void* from, void* limit) +{ + lifespan_object** object; + object = (lifespan_object**)((unsigned long)from + (unsigned long)lifespan_objects_off); + + while (from < limit) { + lifespan_free_object(object++); + ++from; + } +} + +void lifespan_alloc(void* addr, unsigned long size) +{ + lifespan_object** object; + object = (lifespan_object**)((unsigned long)addr + (unsigned long)lifespan_objects_off); + + *object = (lifespan_object*)malloc(sizeof(lifespan_object)); + if (!*object) { + fprintf(stderr, "oops.\n"); + exit(-10); + } + (*object)->time = current_time; + (*object)->size = size; + + current_time += size; +} + +/* + * These are local overrides for various environment variables in Emacs. + * Please do not remove this and leave it at the end of the file, where + * Emacs will automagically detect them. + * --------------------------------------------------------------------- + * Local variables: + * mode: c + * indent-tabs-mode: t + * c-basic-offset: 4 + * tab-width: 4 + * End: + */ diff --git a/mm/lifespan.h b/mm/lifespan.h new file mode 100644 index 000000000..90a47b364 --- /dev/null +++ b/mm/lifespan.h @@ -0,0 +1,24 @@ +#ifndef __mm_lifespan_h__ +#define __mm_lifespan_h__ + +void lifespan_init(void* heap_base, unsigned long heap_size); +void lifespan_close(); +void lifespan_emit(); + +void lifespan_free(void* from, void* limit); +void lifespan_alloc(void* addr, unsigned long size); + +#endif + +/* + * These are local overrides for various environment variables in Emacs. + * Please do not remove this and leave it at the end of the file, where + * Emacs will automagically detect them. + * --------------------------------------------------------------------- + * Local variables: + * mode: c + * indent-tabs-mode: t + * c-basic-offset: 4 + * tab-width: 4 + * End: + */ -- 2.25.1