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;
87 void *stat_mem_reallocate(void *ptr, size_t size, size_t line, const char *file) {
88 stat_mem_block_t *oldinfo = NULL;
89 stat_mem_block_t *newinfo;
92 return stat_mem_allocate(size, line, file);
94 /* stay consistent with glic */
96 stat_mem_deallocate(ptr);
100 oldinfo = ((stat_mem_block_t*)ptr - 1);
101 newinfo = ((stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size));
104 stat_mem_deallocate(ptr);
108 memcpy(newinfo+1, oldinfo+1, oldinfo->size);
110 if (oldinfo->prev) oldinfo->prev->next = oldinfo->next;
111 if (oldinfo->next) oldinfo->next->prev = oldinfo->prev;
114 if (oldinfo == stat_mem_block_root)
115 stat_mem_block_root = oldinfo->next;
117 newinfo->line = line;
118 newinfo->size = size;
119 newinfo->file = file;
120 newinfo->prev = NULL;
121 newinfo->next = stat_mem_block_root;
123 if (stat_mem_block_root)
124 stat_mem_block_root->prev = newinfo;
126 stat_mem_block_root = newinfo;
127 stat_mem_allocated -= oldinfo->size;
128 stat_mem_high -= oldinfo->size;
129 stat_mem_allocated += newinfo->size;
130 stat_mem_high += newinfo->size;
132 if (stat_mem_high > stat_mem_peak)
133 stat_mem_peak = stat_mem_high;
141 * strdup does it's own malloc, we need to track malloc. We don't want
142 * to overwrite malloc though, infact, we can't really hook it at all
143 * without library specific assumptions. So we re implement strdup.
145 char *stat_mem_strdup(const char *src, size_t line, const char *file, bool empty) {
153 if (((!empty) ? len : true) && (ptr = (char*)stat_mem_allocate(len + 1, line, file))) {
154 memcpy(ptr, src, len);
158 stat_used_strdups ++;
163 * The reallocate function for resizing vectors.
165 void _util_vec_grow(void **a, size_t i, size_t s) {
166 vector_t *d = vec_meta(*a);
168 stat_size_entry_t *e = NULL;
172 m = 2 * d->allocated + i;
173 p = mem_r(d, s * m + sizeof(vector_t));
176 p = mem_a(s * m + sizeof(vector_t));
177 ((vector_t*)p)->used = 0;
181 if (!stat_size_vectors)
182 stat_size_vectors = stat_size_new();
184 if ((e = stat_size_get(stat_size_vectors, s))) {
187 stat_size_put(stat_size_vectors, s, 1); /* start off with 1 */
191 *a = (vector_t*)p + 1;
192 vec_meta(*a)->allocated = m;
196 * Hash table for generic data, based on dynamic memory allocations
197 * all around. This is the internal interface, please look for
198 * EXPOSED INTERFACE comment below
200 typedef struct hash_node_t {
201 char *key; /* the key for this node in table */
202 void *value; /* pointer to the data as void* */
203 struct hash_node_t *next; /* next node (linked list) */
206 GMQCC_INLINE size_t util_hthash(hash_table_t *ht, const char *key) {
207 const uint32_t mix = 0x5BD1E995;
208 const uint32_t rot = 24;
209 size_t size = strlen(key);
210 uint32_t hash = 0x1EF0 /* LICRC TAB */ ^ size;
212 const unsigned char *data = (const unsigned char*)key;
215 alias = (data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24));
217 alias ^= alias >> rot;
228 case 3: hash ^= data[2] << 16;
229 case 2: hash ^= data[1] << 8;
230 case 1: hash ^= data[0];
238 return (size_t) (hash % ht->size);
241 static hash_node_t *_util_htnewpair(const char *key, void *value) {
243 if (!(node = (hash_node_t*)mem_a(sizeof(hash_node_t))))
246 if (!(node->key = util_strdupe(key))) {
258 * EXPOSED INTERFACE for the hashtable implementation
259 * util_htnew(size) -- to make a new hashtable
260 * util_htset(table, key, value, sizeof(value)) -- to set something in the table
261 * util_htget(table, key) -- to get something from the table
262 * util_htdel(table) -- to delete the table
264 hash_table_t *util_htnew(size_t size) {
265 hash_table_t *hashtable = NULL;
266 stat_size_entry_t *find = NULL;
271 if (!stat_size_hashtables)
272 stat_size_hashtables = stat_size_new();
274 if (!(hashtable = (hash_table_t*)mem_a(sizeof(hash_table_t))))
277 if (!(hashtable->table = (hash_node_t**)mem_a(sizeof(hash_node_t*) * size))) {
282 if ((find = stat_size_get(stat_size_hashtables, size)))
285 stat_used_hashtables++;
286 stat_size_put(stat_size_hashtables, size, 1);
289 hashtable->size = size;
290 memset(hashtable->table, 0, sizeof(hash_node_t*) * size);
292 stat_type_hashtables++;
296 void util_htseth(hash_table_t *ht, const char *key, size_t bin, void *value) {
297 hash_node_t *newnode = NULL;
298 hash_node_t *next = NULL;
299 hash_node_t *last = NULL;
301 next = ht->table[bin];
303 while (next && next->key && strcmp(key, next->key) > 0)
304 last = next, next = next->next;
306 /* already in table, do a replace */
307 if (next && next->key && strcmp(key, next->key) == 0) {
310 /* not found, grow a pair man :P */
311 newnode = _util_htnewpair(key, value);
312 if (next == ht->table[bin]) {
313 newnode->next = next;
314 ht->table[bin] = newnode;
316 last->next = newnode;
318 newnode->next = next;
319 last->next = newnode;
324 void util_htset(hash_table_t *ht, const char *key, void *value) {
325 util_htseth(ht, key, util_hthash(ht, key), value);
328 void *util_htgeth(hash_table_t *ht, const char *key, size_t bin) {
329 hash_node_t *pair = ht->table[bin];
331 while (pair && pair->key && strcmp(key, pair->key) > 0)
334 if (!pair || !pair->key || strcmp(key, pair->key) != 0)
340 void *util_htget(hash_table_t *ht, const char *key) {
341 return util_htgeth(ht, key, util_hthash(ht, key));
344 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin) {
349 keylen = strlen(key);
351 pair = ht->table[bin];
352 while (pair && pair->key) {
353 len = strlen(pair->key);
359 cmp = strcmp(key, pair->key);
367 cmp = strcmp(key, pair->key + len - keylen);
369 uintptr_t up = (uintptr_t)pair->value;
379 * Free all allocated data in a hashtable, this is quite the amount
382 void util_htrem(hash_table_t *ht, void (*callback)(void *data)) {
384 for (; i < ht->size; i++) {
385 hash_node_t *n = ht->table[i];
405 void util_htrmh(hash_table_t *ht, const char *key, size_t bin, void (*cb)(void*)) {
406 hash_node_t **pair = &ht->table[bin];
409 while (*pair && (*pair)->key && strcmp(key, (*pair)->key) > 0)
410 pair = &(*pair)->next;
413 if (!tmp || !tmp->key || strcmp(key, tmp->key) != 0)
424 void util_htrm(hash_table_t *ht, const char *key, void (*cb)(void*)) {
425 util_htrmh(ht, key, util_hthash(ht, key), cb);
428 void util_htdel(hash_table_t *ht) {
429 util_htrem(ht, NULL);
433 * A tiny size_t key-value hashtbale for tracking vector and hashtable
434 * sizes. We can use it for other things too, if we need to. This is
435 * very TIGHT, and efficent in terms of space though.
437 stat_size_table_t stat_size_new() {
438 return (stat_size_table_t)memset(
439 mem_a(sizeof(stat_size_entry_t*) * ST_SIZE),
440 0, ST_SIZE * sizeof(stat_size_entry_t*)
444 void stat_size_del(stat_size_table_t table) {
446 for (; i < ST_SIZE; i++) if(table[i]) mem_d(table[i]);
450 stat_size_entry_t *stat_size_get(stat_size_table_t table, size_t key) {
451 size_t hash = (key % ST_SIZE);
452 while (table[hash] && table[hash]->key != key)
453 hash = (hash + 1) % ST_SIZE;
456 void stat_size_put(stat_size_table_t table, size_t key, size_t value) {
457 size_t hash = (key % ST_SIZE);
458 while (table[hash] && table[hash]->key != key)
459 hash = (hash + 1) % ST_SIZE;
460 table[hash] = (stat_size_entry_t*)mem_a(sizeof(stat_size_entry_t));
461 table[hash]->key = key;
462 table[hash]->value = value;
466 * The following functions below implement printing / dumping of statistical
469 static void stat_dump_mem_contents(stat_mem_block_t *memory, uint16_t cols) {
471 for (i = 0; i < memory->size + ((memory->size % cols) ? (cols - memory->size % cols) : 0); i++) {
472 if (i % cols == 0) con_out(" 0x%06X: ", i);
473 if (i < memory->size) con_out("%02X " , 0xFF & ((unsigned char*)(memory + 1))[i]);
476 if ((uint16_t)(i % cols) == (cols - 1)) {
477 for (j = i - (cols - 1); j <= i; j++) {
481 : (isprint(((unsigned char*)(memory + 1))[j]))
482 ? 0xFF & ((unsigned char*)(memory + 1)) [j]
491 static void stat_dump_mem_leaks() {
492 stat_mem_block_t *info;
493 for (info = stat_mem_block_root; info; info = info->next) {
494 con_out("lost: %u (bytes) at %s:%u\n",
500 stat_dump_mem_contents(info, OPTS_OPTION_U16(OPTION_MEMDUMPCOLS));
504 static void stat_dump_mem_info() {
505 con_out("Memory information:\n\
506 Total allocations: %llu\n\
507 Total deallocations: %llu\n\
508 Total allocated: %f (MB)\n\
509 Total deallocated: %f (MB)\n\
510 Total peak memory: %f (MB)\n\
511 Total leaked memory: %f (MB) in %llu allocations\n",
512 stat_mem_allocated_total,
513 stat_mem_deallocated_total,
514 (float)(stat_mem_allocated) / 1048576.0f,
515 (float)(stat_mem_deallocated) / 1048576.0f,
516 (float)(stat_mem_high) / 1048576.0f,
517 (float)(stat_mem_allocated - stat_mem_deallocated) / 1048576.0f,
518 stat_mem_allocated_total - stat_mem_deallocated_total
522 static void stat_dump_stats_table(stat_size_table_t table, const char *string, uint64_t *size) {
525 for (i = 0, j = 0; i < ST_SIZE; i++) {
526 stat_size_entry_t *entry;
528 if (!(entry = table[i]))
531 con_out(string, (unsigned)j, (unsigned)entry->key, (unsigned)entry->value);
535 *size += entry->key * entry->value;
540 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
541 stat_dump_mem_leaks();
543 if (OPTS_OPTION_BOOL(OPTION_DEBUG) ||
544 OPTS_OPTION_BOOL(OPTION_MEMCHK))
545 stat_dump_mem_info();
547 if (OPTS_OPTION_BOOL(OPTION_MEMCHK) ||
548 OPTS_OPTION_BOOL(OPTION_STATISTICS)) {
551 con_out("\nAdditional Statistics:\n\
552 Total vectors allocated: %llu\n\
553 Total string duplicates: %llu\n\
554 Total hashtables allocated: %llu\n\
555 Total unique vector sizes: %llu\n",
558 stat_used_hashtables,
562 stat_dump_stats_table (
564 " %2u| # of %4u byte vectors: %u\n",
569 " Total unique hashtable sizes: %llu\n",
573 stat_dump_stats_table (
574 stat_size_hashtables,
575 " %2u| # of %4u element hashtables: %u\n",
580 " Total vector memory: %f (MB)\n",
581 (float)(mem) / 1048576.0f
585 if (stat_size_vectors)
586 stat_size_del(stat_size_vectors);
587 if (stat_size_hashtables)
588 stat_size_del(stat_size_hashtables);