4 * GMQCC performs tons of allocations, constructions, and crazyness
5 * all around. When trying to optimizes systems, or just get fancy
6 * statistics out of the compiler, it's often printf mess. This file
7 * implements the statistics system of the compiler. I.E the allocator
8 * we use to track allocations, and other systems of interest.
12 typedef struct stat_mem_block_s {
16 struct stat_mem_block_s *next;
17 struct stat_mem_block_s *prev;
20 static uint64_t stat_mem_allocated = 0;
21 static uint64_t stat_mem_deallocated = 0;
22 static uint64_t stat_mem_allocated_total = 0;
23 static uint64_t stat_mem_deallocated_total = 0;
24 static uint64_t stat_mem_high = 0;
25 static uint64_t stat_mem_peak = 0;
26 static uint64_t stat_used_strdups = 0;
27 static uint64_t stat_used_vectors = 0;
28 static uint64_t stat_used_hashtables = 0;
29 static uint64_t stat_type_vectors = 0;
30 static uint64_t stat_type_hashtables = 0;
31 static stat_size_table_t stat_size_vectors = NULL;
32 static stat_size_table_t stat_size_hashtables = NULL;
33 static stat_mem_block_t *stat_mem_block_root = NULL;
36 * A basic header of information wrapper allocator. Simply stores
37 * information as a header, returns the memory + 1 past it, can be
38 * retrieved again with - 1. Where type is stat_mem_block_t*.
40 void *stat_mem_allocate(size_t size, size_t line, const char *file) {
41 stat_mem_block_t *info = (stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size);
42 void *data = (void*)(info + 1);
51 info->next = stat_mem_block_root;
53 if (stat_mem_block_root)
54 stat_mem_block_root->prev = info;
56 stat_mem_block_root = info;
57 stat_mem_allocated += size;
58 stat_mem_high += size;
59 stat_mem_allocated_total ++;
61 if (stat_mem_high > stat_mem_peak)
62 stat_mem_peak = stat_mem_high;
67 void stat_mem_deallocate(void *ptr) {
68 stat_mem_block_t *info = NULL;
73 info = ((stat_mem_block_t*)ptr - 1);
75 stat_mem_deallocated += info->size;
76 stat_mem_high -= info->size;
77 stat_mem_deallocated_total ++;
79 if (info->prev) info->prev->next = info->next;
80 if (info->next) info->next->prev = info->prev;
83 if (info == stat_mem_block_root)
84 stat_mem_block_root = info->next;
89 void *stat_mem_reallocate(void *ptr, size_t size, size_t line, const char *file) {
90 stat_mem_block_t *oldinfo = NULL;
91 stat_mem_block_t *newinfo;
94 return stat_mem_allocate(size, line, file);
96 /* stay consistent with glic */
98 stat_mem_deallocate(ptr);
102 oldinfo = ((stat_mem_block_t*)ptr - 1);
103 newinfo = ((stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size));
106 stat_mem_deallocate(ptr);
110 memcpy(newinfo+1, oldinfo+1, oldinfo->size);
112 if (oldinfo->prev) oldinfo->prev->next = oldinfo->next;
113 if (oldinfo->next) oldinfo->next->prev = oldinfo->prev;
116 if (oldinfo == stat_mem_block_root)
117 stat_mem_block_root = oldinfo->next;
119 newinfo->line = line;
120 newinfo->size = size;
121 newinfo->file = file;
122 newinfo->prev = NULL;
123 newinfo->next = stat_mem_block_root;
125 if (stat_mem_block_root)
126 stat_mem_block_root->prev = newinfo;
128 stat_mem_block_root = newinfo;
129 stat_mem_allocated -= oldinfo->size;
130 stat_mem_high -= oldinfo->size;
131 stat_mem_allocated += newinfo->size;
132 stat_mem_high += newinfo->size;
134 if (stat_mem_high > stat_mem_peak)
135 stat_mem_peak = stat_mem_high;
143 * strdup does it's own malloc, we need to track malloc. We don't want
144 * to overwrite malloc though, infact, we can't really hook it at all
145 * without library specific assumptions. So we re implement strdup.
147 char *stat_mem_strdup(const char *src, size_t line, const char *file, bool empty) {
155 if (((!empty) ? len : true) && (ptr = (char*)stat_mem_allocate(len + 1, line, file))) {
156 memcpy(ptr, src, len);
160 stat_used_strdups ++;
165 * The reallocate function for resizing vectors.
167 void _util_vec_grow(void **a, size_t i, size_t s) {
168 vector_t *d = vec_meta(*a);
170 stat_size_entry_t *e = NULL;
174 m = 2 * d->allocated + i;
175 p = mem_r(d, s * m + sizeof(vector_t));
178 p = mem_a(s * m + sizeof(vector_t));
179 ((vector_t*)p)->used = 0;
183 if (!stat_size_vectors)
184 stat_size_vectors = stat_size_new();
186 if ((e = stat_size_get(stat_size_vectors, s))) {
189 stat_size_put(stat_size_vectors, s, 1); /* start off with 1 */
193 *a = (vector_t*)p + 1;
194 vec_meta(*a)->allocated = m;
198 * Hash table for generic data, based on dynamic memory allocations
199 * all around. This is the internal interface, please look for
200 * EXPOSED INTERFACE comment below
202 typedef struct hash_node_t {
203 char *key; /* the key for this node in table */
204 void *value; /* pointer to the data as void* */
205 struct hash_node_t *next; /* next node (linked list) */
208 GMQCC_INLINE size_t util_hthash(hash_table_t *ht, const char *key) {
209 const uint32_t mix = 0x5BD1E995;
210 const uint32_t rot = 24;
211 size_t size = strlen(key);
212 uint32_t hash = 0x1EF0 /* LICRC TAB */ ^ size;
214 const unsigned char *data = (const unsigned char*)key;
217 alias = (data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24));
219 alias ^= alias >> rot;
230 case 3: hash ^= data[2] << 16;
231 case 2: hash ^= data[1] << 8;
232 case 1: hash ^= data[0];
240 return (size_t) (hash % ht->size);
243 static hash_node_t *_util_htnewpair(const char *key, void *value) {
245 if (!(node = (hash_node_t*)mem_a(sizeof(hash_node_t))))
248 if (!(node->key = util_strdupe(key))) {
260 * EXPOSED INTERFACE for the hashtable implementation
261 * util_htnew(size) -- to make a new hashtable
262 * util_htset(table, key, value, sizeof(value)) -- to set something in the table
263 * util_htget(table, key) -- to get something from the table
264 * util_htdel(table) -- to delete the table
266 hash_table_t *util_htnew(size_t size) {
267 hash_table_t *hashtable = NULL;
268 stat_size_entry_t *find = NULL;
273 if (!stat_size_hashtables)
274 stat_size_hashtables = stat_size_new();
276 if (!(hashtable = (hash_table_t*)mem_a(sizeof(hash_table_t))))
279 if (!(hashtable->table = (hash_node_t**)mem_a(sizeof(hash_node_t*) * size))) {
284 if ((find = stat_size_get(stat_size_hashtables, size)))
287 stat_used_hashtables++;
288 stat_size_put(stat_size_hashtables, size, 1);
291 hashtable->size = size;
292 memset(hashtable->table, 0, sizeof(hash_node_t*) * size);
294 stat_type_hashtables++;
298 void util_htseth(hash_table_t *ht, const char *key, size_t bin, void *value) {
299 hash_node_t *newnode = NULL;
300 hash_node_t *next = NULL;
301 hash_node_t *last = NULL;
303 next = ht->table[bin];
305 while (next && next->key && strcmp(key, next->key) > 0)
306 last = next, next = next->next;
308 /* already in table, do a replace */
309 if (next && next->key && strcmp(key, next->key) == 0) {
312 /* not found, grow a pair man :P */
313 newnode = _util_htnewpair(key, value);
314 if (next == ht->table[bin]) {
315 newnode->next = next;
316 ht->table[bin] = newnode;
318 last->next = newnode;
320 newnode->next = next;
321 last->next = newnode;
326 void util_htset(hash_table_t *ht, const char *key, void *value) {
327 util_htseth(ht, key, util_hthash(ht, key), value);
330 void *util_htgeth(hash_table_t *ht, const char *key, size_t bin) {
331 hash_node_t *pair = ht->table[bin];
333 while (pair && pair->key && strcmp(key, pair->key) > 0)
336 if (!pair || !pair->key || strcmp(key, pair->key) != 0)
342 void *util_htget(hash_table_t *ht, const char *key) {
343 return util_htgeth(ht, key, util_hthash(ht, key));
346 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin) {
351 keylen = strlen(key);
353 pair = ht->table[bin];
354 while (pair && pair->key) {
355 len = strlen(pair->key);
361 cmp = strcmp(key, pair->key);
369 cmp = strcmp(key, pair->key + len - keylen);
371 uintptr_t up = (uintptr_t)pair->value;
381 * Free all allocated data in a hashtable, this is quite the amount
384 void util_htrem(hash_table_t *ht, void (*callback)(void *data)) {
386 for (; i < ht->size; i++) {
387 hash_node_t *n = ht->table[i];
407 void util_htrmh(hash_table_t *ht, const char *key, size_t bin, void (*cb)(void*)) {
408 hash_node_t **pair = &ht->table[bin];
411 while (*pair && (*pair)->key && strcmp(key, (*pair)->key) > 0)
412 pair = &(*pair)->next;
415 if (!tmp || !tmp->key || strcmp(key, tmp->key) != 0)
426 void util_htrm(hash_table_t *ht, const char *key, void (*cb)(void*)) {
427 util_htrmh(ht, key, util_hthash(ht, key), cb);
430 void util_htdel(hash_table_t *ht) {
431 util_htrem(ht, NULL);
435 * A tiny size_t key-value hashtbale for tracking vector and hashtable
436 * sizes. We can use it for other things too, if we need to. This is
437 * very TIGHT, and efficent in terms of space though.
439 stat_size_table_t stat_size_new() {
440 return (stat_size_table_t)memset(
441 mem_a(sizeof(stat_size_entry_t*) * ST_SIZE),
442 0, ST_SIZE * sizeof(stat_size_entry_t*)
446 void stat_size_del(stat_size_table_t table) {
448 for (; i < ST_SIZE; i++) if(table[i]) mem_d(table[i]);
452 stat_size_entry_t *stat_size_get(stat_size_table_t table, size_t key) {
453 size_t hash = (key % ST_SIZE);
454 while (table[hash] && table[hash]->key != key)
455 hash = (hash + 1) % ST_SIZE;
458 void stat_size_put(stat_size_table_t table, size_t key, size_t value) {
459 size_t hash = (key % ST_SIZE);
460 while (table[hash] && table[hash]->key != key)
461 hash = (hash + 1) % ST_SIZE;
462 table[hash] = (stat_size_entry_t*)mem_a(sizeof(stat_size_entry_t));
463 table[hash]->key = key;
464 table[hash]->value = value;
468 * The following functions below implement printing / dumping of statistical
471 static void stat_dump_mem_contents(stat_mem_block_t *memory, uint16_t cols) {
473 for (i = 0; i < memory->size + ((memory->size % cols) ? (cols - memory->size % cols) : 0); i++) {
474 if (i % cols == 0) con_out(" 0x%06X: ", i);
475 if (i < memory->size) con_out("%02X " , 0xFF & ((unsigned char*)(memory + 1))[i]);
478 if ((uint16_t)(i % cols) == (cols - 1)) {
479 for (j = i - (cols - 1); j <= i; j++) {
483 : (isprint(((unsigned char*)(memory + 1))[j]))
484 ? 0xFF & ((unsigned char*)(memory + 1)) [j]
493 static void stat_dump_mem_leaks() {
494 stat_mem_block_t *info;
495 for (info = stat_mem_block_root; info; info = info->next) {
496 con_out("lost: %u (bytes) at %s:%u\n",
502 stat_dump_mem_contents(info, OPTS_OPTION_U16(OPTION_MEMDUMPCOLS));
506 static void stat_dump_mem_info() {
507 con_out("Memory information:\n\
508 Total allocations: %llu\n\
509 Total deallocations: %llu\n\
510 Total allocated: %f (MB)\n\
511 Total deallocated: %f (MB)\n\
512 Total peak memory: %f (MB)\n\
513 Total leaked memory: %f (MB) in %llu allocations\n",
514 stat_mem_allocated_total,
515 stat_mem_deallocated_total,
516 (float)(stat_mem_allocated) / 1048576.0f,
517 (float)(stat_mem_deallocated) / 1048576.0f,
518 (float)(stat_mem_peak) / 1048576.0f,
519 (float)(stat_mem_allocated - stat_mem_deallocated) / 1048576.0f,
520 stat_mem_allocated_total - stat_mem_deallocated_total
524 static void stat_dump_stats_table(stat_size_table_t table, const char *string, uint64_t *size) {
527 for (i = 0, j = 0; i < ST_SIZE; i++) {
528 stat_size_entry_t *entry;
530 if (!(entry = table[i]))
533 con_out(string, (unsigned)j, (unsigned)entry->key, (unsigned)entry->value);
537 *size += entry->key * entry->value;
542 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
543 stat_dump_mem_leaks();
545 if (OPTS_OPTION_BOOL(OPTION_DEBUG) ||
546 OPTS_OPTION_BOOL(OPTION_MEMCHK))
547 stat_dump_mem_info();
549 if (OPTS_OPTION_BOOL(OPTION_MEMCHK) ||
550 OPTS_OPTION_BOOL(OPTION_STATISTICS)) {
553 con_out("\nAdditional Statistics:\n\
554 Total vectors allocated: %llu\n\
555 Total string duplicates: %llu\n\
556 Total hashtables allocated: %llu\n\
557 Total unique vector sizes: %llu\n",
560 stat_used_hashtables,
564 stat_dump_stats_table (
566 " %2u| # of %4u byte vectors: %u\n",
571 " Total unique hashtable sizes: %llu\n",
575 stat_dump_stats_table (
576 stat_size_hashtables,
577 " %2u| # of %4u element hashtables: %u\n",
582 " Total vector memory: %f (MB)\n",
583 (float)(mem) / 1048576.0f
587 if (stat_size_vectors)
588 stat_size_del(stat_size_vectors);
589 if (stat_size_hashtables)
590 stat_size_del(stat_size_hashtables);