8 * GMQCC performs tons of allocations, constructions, and crazyness
9 * all around. When trying to optimizes systems, or just get fancy
10 * statistics out of the compiler, it's often printf mess. This file
11 * implements the statistics system of the compiler. I.E the allocator
12 * we use to track allocations, and other systems of interest.
16 typedef struct stat_mem_block_s {
20 struct stat_mem_block_s *next;
21 struct stat_mem_block_s *prev;
27 } stat_size_entry_t, **stat_size_table_t;
35 static uint64_t stat_mem_allocated = 0;
36 static uint64_t stat_mem_deallocated = 0;
37 static uint64_t stat_mem_allocated_total = 0;
38 static uint64_t stat_mem_deallocated_total = 0;
39 static uint64_t stat_mem_high = 0;
40 static uint64_t stat_mem_peak = 0;
41 static uint64_t stat_used_strdups = 0;
42 static uint64_t stat_used_vectors = 0;
43 static uint64_t stat_used_hashtables = 0;
44 static uint64_t stat_type_vectors = 0;
45 static uint64_t stat_type_hashtables = 0;
46 static stat_size_table_t stat_size_vectors = NULL;
47 static stat_size_table_t stat_size_hashtables = NULL;
48 static stat_mem_block_t *stat_mem_block_root = NULL;
51 * A tiny size_t key-value hashtbale for tracking vector and hashtable
52 * sizes. We can use it for other things too, if we need to. This is
53 * very TIGHT, and efficent in terms of space though.
55 static stat_size_table_t stat_size_new() {
56 return (stat_size_table_t)memset(
57 mem_a(sizeof(stat_size_entry_t*) * ST_SIZE),
58 0, ST_SIZE * sizeof(stat_size_entry_t*)
62 static void stat_size_del(stat_size_table_t table) {
64 for (; i < ST_SIZE; i++) if(table[i]) mem_d(table[i]);
68 static stat_size_entry_t *stat_size_get(stat_size_table_t table, size_t key) {
69 size_t hash = (key % ST_SIZE);
70 while (table[hash] && table[hash]->key != key)
71 hash = (hash + 1) % ST_SIZE;
74 static void stat_size_put(stat_size_table_t table, size_t key, size_t value) {
75 size_t hash = (key % ST_SIZE);
76 while (table[hash] && table[hash]->key != key)
77 hash = (hash + 1) % ST_SIZE;
78 table[hash] = (stat_size_entry_t*)mem_a(sizeof(stat_size_entry_t));
79 table[hash]->key = key;
80 table[hash]->value = value;
84 * A basic header of information wrapper allocator. Simply stores
85 * information as a header, returns the memory + 1 past it, can be
86 * retrieved again with - 1. Where type is stat_mem_block_t*.
88 void *stat_mem_allocate(size_t size, size_t line, const char *file) {
89 stat_mem_block_t *info = (stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size);
90 void *data = (void*)(info + 1);
99 info->next = stat_mem_block_root;
101 if (stat_mem_block_root)
102 stat_mem_block_root->prev = info;
104 stat_mem_block_root = info;
105 stat_mem_allocated += size;
106 stat_mem_high += size;
107 stat_mem_allocated_total ++;
109 if (stat_mem_high > stat_mem_peak)
110 stat_mem_peak = stat_mem_high;
115 void stat_mem_deallocate(void *ptr) {
116 stat_mem_block_t *info = NULL;
121 info = ((stat_mem_block_t*)ptr - 1);
123 stat_mem_deallocated += info->size;
124 stat_mem_high -= info->size;
125 stat_mem_deallocated_total ++;
127 if (info->prev) info->prev->next = info->next;
128 if (info->next) info->next->prev = info->prev;
131 if (info == stat_mem_block_root)
132 stat_mem_block_root = info->next;
137 void *stat_mem_reallocate(void *ptr, size_t size, size_t line, const char *file) {
138 stat_mem_block_t *oldinfo = NULL;
139 stat_mem_block_t *newinfo;
142 return stat_mem_allocate(size, line, file);
144 /* stay consistent with glic */
146 stat_mem_deallocate(ptr);
150 oldinfo = ((stat_mem_block_t*)ptr - 1);
151 newinfo = ((stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size));
154 stat_mem_deallocate(ptr);
158 memcpy(newinfo+1, oldinfo+1, oldinfo->size);
160 if (oldinfo->prev) oldinfo->prev->next = oldinfo->next;
161 if (oldinfo->next) oldinfo->next->prev = oldinfo->prev;
164 if (oldinfo == stat_mem_block_root)
165 stat_mem_block_root = oldinfo->next;
167 newinfo->line = line;
168 newinfo->size = size;
169 newinfo->file = file;
170 newinfo->prev = NULL;
171 newinfo->next = stat_mem_block_root;
173 if (stat_mem_block_root)
174 stat_mem_block_root->prev = newinfo;
176 stat_mem_block_root = newinfo;
177 stat_mem_allocated -= oldinfo->size;
178 stat_mem_high -= oldinfo->size;
179 stat_mem_allocated += newinfo->size;
180 stat_mem_high += newinfo->size;
182 if (stat_mem_high > stat_mem_peak)
183 stat_mem_peak = stat_mem_high;
191 * strdup does it's own malloc, we need to track malloc. We don't want
192 * to overwrite malloc though, infact, we can't really hook it at all
193 * without library specific assumptions. So we re implement strdup.
195 char *stat_mem_strdup(const char *src, size_t line, const char *file, bool empty) {
203 if (((!empty) ? len : true) && (ptr = (char*)stat_mem_allocate(len + 1, line, file))) {
204 memcpy(ptr, src, len);
208 stat_used_strdups ++;
213 * The reallocate function for resizing vectors.
215 void _util_vec_grow(void **a, size_t i, size_t s) {
216 vector_t *d = vec_meta(*a);
218 stat_size_entry_t *e = NULL;
222 m = 2 * d->allocated + i;
223 p = mem_r(d, s * m + sizeof(vector_t));
226 p = mem_a(s * m + sizeof(vector_t));
227 ((vector_t*)p)->used = 0;
231 if (!stat_size_vectors)
232 stat_size_vectors = stat_size_new();
234 if ((e = stat_size_get(stat_size_vectors, s))) {
237 stat_size_put(stat_size_vectors, s, 1); /* start off with 1 */
241 *a = (vector_t*)p + 1;
242 vec_meta(*a)->allocated = m;
246 * Hash table for generic data, based on dynamic memory allocations
247 * all around. This is the internal interface, please look for
248 * EXPOSED INTERFACE comment below
250 typedef struct hash_node_t {
251 char *key; /* the key for this node in table */
252 void *value; /* pointer to the data as void* */
253 struct hash_node_t *next; /* next node (linked list) */
256 GMQCC_INLINE size_t util_hthash(hash_table_t *ht, const char *key) {
257 const uint32_t mix = 0x5BD1E995;
258 const uint32_t rot = 24;
259 size_t size = strlen(key);
260 uint32_t hash = 0x1EF0 /* LICRC TAB */ ^ size;
262 const unsigned char *data = (const unsigned char*)key;
265 alias = (data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24));
267 alias ^= alias >> rot;
278 case 3: hash ^= data[2] << 16;
279 case 2: hash ^= data[1] << 8;
280 case 1: hash ^= data[0];
288 return (size_t) (hash % ht->size);
291 static hash_node_t *_util_htnewpair(const char *key, void *value) {
293 if (!(node = (hash_node_t*)mem_a(sizeof(hash_node_t))))
296 if (!(node->key = util_strdupe(key))) {
308 * EXPOSED INTERFACE for the hashtable implementation
309 * util_htnew(size) -- to make a new hashtable
310 * util_htset(table, key, value, sizeof(value)) -- to set something in the table
311 * util_htget(table, key) -- to get something from the table
312 * util_htdel(table) -- to delete the table
314 hash_table_t *util_htnew(size_t size) {
315 hash_table_t *hashtable = NULL;
316 stat_size_entry_t *find = NULL;
321 if (!stat_size_hashtables)
322 stat_size_hashtables = stat_size_new();
324 if (!(hashtable = (hash_table_t*)mem_a(sizeof(hash_table_t))))
327 if (!(hashtable->table = (hash_node_t**)mem_a(sizeof(hash_node_t*) * size))) {
332 if ((find = stat_size_get(stat_size_hashtables, size)))
335 stat_type_hashtables++;
336 stat_size_put(stat_size_hashtables, size, 1);
339 hashtable->size = size;
340 memset(hashtable->table, 0, sizeof(hash_node_t*) * size);
342 stat_used_hashtables++;
346 void util_htseth(hash_table_t *ht, const char *key, size_t bin, void *value) {
347 hash_node_t *newnode = NULL;
348 hash_node_t *next = NULL;
349 hash_node_t *last = NULL;
351 next = ht->table[bin];
353 while (next && next->key && strcmp(key, next->key) > 0)
354 last = next, next = next->next;
356 /* already in table, do a replace */
357 if (next && next->key && strcmp(key, next->key) == 0) {
360 /* not found, grow a pair man :P */
361 newnode = _util_htnewpair(key, value);
362 if (next == ht->table[bin]) {
363 newnode->next = next;
364 ht->table[bin] = newnode;
366 last->next = newnode;
368 newnode->next = next;
369 last->next = newnode;
374 void util_htset(hash_table_t *ht, const char *key, void *value) {
375 util_htseth(ht, key, util_hthash(ht, key), value);
378 void *util_htgeth(hash_table_t *ht, const char *key, size_t bin) {
379 hash_node_t *pair = ht->table[bin];
381 while (pair && pair->key && strcmp(key, pair->key) > 0)
384 if (!pair || !pair->key || strcmp(key, pair->key) != 0)
390 void *util_htget(hash_table_t *ht, const char *key) {
391 return util_htgeth(ht, key, util_hthash(ht, key));
394 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin) {
399 keylen = strlen(key);
401 pair = ht->table[bin];
402 while (pair && pair->key) {
403 len = strlen(pair->key);
409 cmp = strcmp(key, pair->key);
417 cmp = strcmp(key, pair->key + len - keylen);
419 uintptr_t up = (uintptr_t)pair->value;
429 * Free all allocated data in a hashtable, this is quite the amount
432 void util_htrem(hash_table_t *ht, void (*callback)(void *data)) {
434 for (; i < ht->size; i++) {
435 hash_node_t *n = ht->table[i];
455 void util_htrmh(hash_table_t *ht, const char *key, size_t bin, void (*cb)(void*)) {
456 hash_node_t **pair = &ht->table[bin];
459 while (*pair && (*pair)->key && strcmp(key, (*pair)->key) > 0)
460 pair = &(*pair)->next;
463 if (!tmp || !tmp->key || strcmp(key, tmp->key) != 0)
474 void util_htrm(hash_table_t *ht, const char *key, void (*cb)(void*)) {
475 util_htrmh(ht, key, util_hthash(ht, key), cb);
478 void util_htdel(hash_table_t *ht) {
479 util_htrem(ht, NULL);
483 * The following functions below implement printing / dumping of statistical
486 static void stat_dump_mem_contents(stat_mem_block_t *memory, uint16_t cols) {
488 for (i = 0; i < memory->size + ((memory->size % cols) ? (cols - memory->size % cols) : 0); i++) {
489 if (i % cols == 0) con_out(" 0x%06X: ", i);
490 if (i < memory->size) con_out("%02X " , 0xFF & ((unsigned char*)(memory + 1))[i]);
493 if ((uint16_t)(i % cols) == (cols - 1)) {
494 for (j = i - (cols - 1); j <= i; j++) {
498 : (isprint(((unsigned char*)(memory + 1))[j]))
499 ? 0xFF & ((unsigned char*)(memory + 1)) [j]
508 static void stat_dump_mem_leaks() {
509 stat_mem_block_t *info;
510 for (info = stat_mem_block_root; info; info = info->next) {
511 con_out("lost: %u (bytes) at %s:%u\n",
517 stat_dump_mem_contents(info, OPTS_OPTION_U16(OPTION_MEMDUMPCOLS));
521 static void stat_dump_mem_info() {
522 con_out("Memory information:\n\
523 Total allocations: %llu\n\
524 Total deallocations: %llu\n\
525 Total allocated: %f (MB)\n\
526 Total deallocated: %f (MB)\n\
527 Total peak memory: %f (MB)\n\
528 Total leaked memory: %f (MB) in %llu allocations\n",
529 stat_mem_allocated_total,
530 stat_mem_deallocated_total,
531 (float)(stat_mem_allocated) / 1048576.0f,
532 (float)(stat_mem_deallocated) / 1048576.0f,
533 (float)(stat_mem_peak) / 1048576.0f,
534 (float)(stat_mem_allocated - stat_mem_deallocated) / 1048576.0f,
535 stat_mem_allocated_total - stat_mem_deallocated_total
539 static void stat_dump_stats_table(stat_size_table_t table, const char *string, uint64_t *size) {
545 for (i = 0, j = 0; i < ST_SIZE; i++) {
546 stat_size_entry_t *entry;
548 if (!(entry = table[i]))
551 con_out(string, (unsigned)j, (unsigned)entry->key, (unsigned)entry->value);
555 *size += entry->key * entry->value;
560 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
561 stat_dump_mem_leaks();
563 if (OPTS_OPTION_BOOL(OPTION_DEBUG) ||
564 OPTS_OPTION_BOOL(OPTION_MEMCHK))
565 stat_dump_mem_info();
567 if (OPTS_OPTION_BOOL(OPTION_MEMCHK) ||
568 OPTS_OPTION_BOOL(OPTION_STATISTICS)) {
571 con_out("\nAdditional Statistics:\n\
572 Total vectors allocated: %llu\n\
573 Total string duplicates: %llu\n\
574 Total hashtables allocated: %llu\n\
575 Total unique vector sizes: %llu\n",
578 stat_used_hashtables,
582 stat_dump_stats_table (
584 " %2u| # of %4u byte vectors: %u\n",
589 " Total unique hashtable sizes: %llu\n",
593 stat_dump_stats_table (
594 stat_size_hashtables,
595 " %2u| # of %4u element hashtables: %u\n",
600 " Total vector memory: %f (MB)\n",
601 (float)(mem) / 1048576.0f
605 if (stat_size_vectors)
606 stat_size_del(stat_size_vectors);
607 if (stat_size_hashtables)
608 stat_size_del(stat_size_hashtables);