3 * $Id: lifespan.c 115 1999-01-20 01:52:45Z phil $
18 static unsigned long current_time = 0;
19 static unsigned long current_number = 0;
20 static lifespan_object** lifespan_objects = NULL;
21 static lifespan_object** lifespan_objects_end = NULL;
22 static void* lifespan_objects_off = NULL;
23 static FILE* lifespan_file = NULL;
25 static unsigned long lifespan_histo_size[64] = { 0 };
26 static unsigned long lifespan_histo_lifespan[64] = { 0 };
28 void lifespan_init(void* heap_base, unsigned long heap_size)
32 lifespan_objects = (lifespan_object**)malloc(heap_size);
33 lifespan_objects_end = ((lifespan_object**)((unsigned long)lifespan_objects + (unsigned long)heap_size));
34 lifespan_objects_off = (void*)((unsigned long)lifespan_objects - (unsigned long)heap_base);
35 lifespan_file = popen("gzip -9 >lifespans.gz", "w");
37 if (!lifespan_objects || !lifespan_file) {
38 fprintf(stderr, "Failed to allocate memory for lifespan-object index / popen lifespan output stream\n");
42 memset(lifespan_objects, 0, heap_size);
45 static inline void lifespan_free_object(lifespan_object** o)
48 /* file format: alloc time, size, lifespan */
51 fprintf(lifespan_file,
52 "%ld\t%ld\t%ld\t%ld\t%ld\n",
56 current_number - (*o)->number,
57 current_time - (*o)->time);
66 ++lifespan_histo_size[high];
70 size = current_time - (*o)->time;
76 ++lifespan_histo_lifespan[high];
85 if (lifespan_objects) {
86 while(lifespan_objects < lifespan_objects_end)
87 lifespan_free_object(--lifespan_objects_end);
89 free(lifespan_objects);
93 pclose(lifespan_file);
101 for (i = 4; i < 32; ++i)
103 fprintf(stderr, "%Lu-%Lu\t%Lu\n",
106 lifespan_histo_size[i]);
108 fprintf(stderr, "%Lu\n",
109 lifespan_histo_size[i]);
112 fprintf(stderr, "\n\n\n");
114 for (i = 4; i < 32; ++i)
115 fprintf(stderr, "%Lu-%Lu\t%Lu\n",
118 lifespan_histo_lifespan[i]);
121 void lifespan_free(void** from, void** limit)
123 lifespan_object** object;
124 object = (lifespan_object**)((unsigned long)from + (unsigned long)lifespan_objects_off);
126 while (from < limit) {
127 lifespan_free_object(object++);
132 void lifespan_alloc(void* addr, unsigned long size)
135 lifespan_object** object;
136 object = (lifespan_object**)((unsigned long)addr + (unsigned long)lifespan_objects_off);
138 *object = (lifespan_object*)malloc(sizeof(lifespan_object));
140 fprintf(stderr, "oops.\n");
143 (*object)->time = current_time;
144 (*object)->size = size;
145 (*object)->number = ++current_number;
151 ++lifespan_histo_size[high];
153 current_time += size;
157 * These are local overrides for various environment variables in Emacs.
158 * Please do not remove this and leave it at the end of the file, where
159 * Emacs will automagically detect them.
160 * ---------------------------------------------------------------------
163 * indent-tabs-mode: t