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;
23 } stat_size_entry_t, **stat_size_table_t;
25 static uint64_t stat_mem_allocated = 0;
26 static uint64_t stat_mem_deallocated = 0;
27 static uint64_t stat_mem_allocated_total = 0;
28 static uint64_t stat_mem_deallocated_total = 0;
29 static uint64_t stat_mem_high = 0;
30 static uint64_t stat_mem_peak = 0;
31 static uint64_t stat_used_strdups = 0;
32 static uint64_t stat_used_vectors = 0;
33 static uint64_t stat_used_hashtables = 0;
34 static uint64_t stat_type_vectors = 0;
35 static uint64_t stat_type_hashtables = 0;
36 static stat_size_table_t stat_size_vectors = NULL;
37 static stat_size_table_t stat_size_hashtables = NULL;
38 static stat_mem_block_t *stat_mem_block_root = NULL;
42 * A tiny size_t key-value hashtbale for tracking vector and hashtable
43 * sizes. We can use it for other things too, if we need to. This is
44 * very TIGHT, and efficent in terms of space though.
46 static stat_size_table_t stat_size_new() {
47 return (stat_size_table_t)memset(
48 mem_a(sizeof(stat_size_entry_t*) * ST_SIZE),
49 0, ST_SIZE * sizeof(stat_size_entry_t*)
53 static void stat_size_del(stat_size_table_t table) {
55 for (; i < ST_SIZE; i++) if(table[i]) mem_d(table[i]);
59 static stat_size_entry_t *stat_size_get(stat_size_table_t table, size_t key) {
60 size_t hash = (key % ST_SIZE);
61 while (table[hash] && table[hash]->key != key)
62 hash = (hash + 1) % ST_SIZE;
65 static void stat_size_put(stat_size_table_t table, size_t key, size_t value) {
66 size_t hash = (key % ST_SIZE);
67 while (table[hash] && table[hash]->key != key)
68 hash = (hash + 1) % ST_SIZE;
69 table[hash] = (stat_size_entry_t*)mem_a(sizeof(stat_size_entry_t));
70 table[hash]->key = key;
71 table[hash]->value = value;
75 * A basic header of information wrapper allocator. Simply stores
76 * information as a header, returns the memory + 1 past it, can be
77 * retrieved again with - 1. Where type is stat_mem_block_t*.
79 void *stat_mem_allocate(size_t size, size_t line, const char *file) {
80 stat_mem_block_t *info = (stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size);
81 void *data = (void*)(info + 1);
90 info->next = stat_mem_block_root;
92 if (stat_mem_block_root)
93 stat_mem_block_root->prev = info;
95 stat_mem_block_root = info;
96 stat_mem_allocated += size;
97 stat_mem_high += size;
98 stat_mem_allocated_total ++;
100 if (stat_mem_high > stat_mem_peak)
101 stat_mem_peak = stat_mem_high;
106 void stat_mem_deallocate(void *ptr) {
107 stat_mem_block_t *info = NULL;
112 info = ((stat_mem_block_t*)ptr - 1);
114 stat_mem_deallocated += info->size;
115 stat_mem_high -= info->size;
116 stat_mem_deallocated_total ++;
118 if (info->prev) info->prev->next = info->next;
119 if (info->next) info->next->prev = info->prev;
122 if (info == stat_mem_block_root)
123 stat_mem_block_root = info->next;
128 void *stat_mem_reallocate(void *ptr, size_t size, size_t line, const char *file) {
129 stat_mem_block_t *oldinfo = NULL;
130 stat_mem_block_t *newinfo;
133 return stat_mem_allocate(size, line, file);
135 /* stay consistent with glic */
137 stat_mem_deallocate(ptr);
141 oldinfo = ((stat_mem_block_t*)ptr - 1);
142 newinfo = ((stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size));
145 stat_mem_deallocate(ptr);
149 memcpy(newinfo+1, oldinfo+1, oldinfo->size);
151 if (oldinfo->prev) oldinfo->prev->next = oldinfo->next;
152 if (oldinfo->next) oldinfo->next->prev = oldinfo->prev;
155 if (oldinfo == stat_mem_block_root)
156 stat_mem_block_root = oldinfo->next;
158 newinfo->line = line;
159 newinfo->size = size;
160 newinfo->file = file;
161 newinfo->prev = NULL;
162 newinfo->next = stat_mem_block_root;
164 if (stat_mem_block_root)
165 stat_mem_block_root->prev = newinfo;
167 stat_mem_block_root = newinfo;
168 stat_mem_allocated -= oldinfo->size;
169 stat_mem_high -= oldinfo->size;
170 stat_mem_allocated += newinfo->size;
171 stat_mem_high += newinfo->size;
173 if (stat_mem_high > stat_mem_peak)
174 stat_mem_peak = stat_mem_high;
182 * strdup does it's own malloc, we need to track malloc. We don't want
183 * to overwrite malloc though, infact, we can't really hook it at all
184 * without library specific assumptions. So we re implement strdup.
186 char *stat_mem_strdup(const char *src, size_t line, const char *file, bool empty) {
194 if (((!empty) ? len : true) && (ptr = (char*)stat_mem_allocate(len + 1, line, file))) {
195 memcpy(ptr, src, len);
199 stat_used_strdups ++;
204 * The reallocate function for resizing vectors.
206 void _util_vec_grow(void **a, size_t i, size_t s) {
207 vector_t *d = vec_meta(*a);
209 stat_size_entry_t *e = NULL;
213 m = 2 * d->allocated + i;
214 p = mem_r(d, s * m + sizeof(vector_t));
217 p = mem_a(s * m + sizeof(vector_t));
218 ((vector_t*)p)->used = 0;
222 if (!stat_size_vectors)
223 stat_size_vectors = stat_size_new();
225 if ((e = stat_size_get(stat_size_vectors, s))) {
228 stat_size_put(stat_size_vectors, s, 1); /* start off with 1 */
232 *a = (vector_t*)p + 1;
233 vec_meta(*a)->allocated = m;
237 * Hash table for generic data, based on dynamic memory allocations
238 * all around. This is the internal interface, please look for
239 * EXPOSED INTERFACE comment below
241 typedef struct hash_node_t {
242 char *key; /* the key for this node in table */
243 void *value; /* pointer to the data as void* */
244 struct hash_node_t *next; /* next node (linked list) */
247 GMQCC_INLINE size_t util_hthash(hash_table_t *ht, const char *key) {
248 const uint32_t mix = 0x5BD1E995;
249 const uint32_t rot = 24;
250 size_t size = strlen(key);
251 uint32_t hash = 0x1EF0 /* LICRC TAB */ ^ size;
253 const unsigned char *data = (const unsigned char*)key;
256 alias = (data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24));
258 alias ^= alias >> rot;
269 case 3: hash ^= data[2] << 16;
270 case 2: hash ^= data[1] << 8;
271 case 1: hash ^= data[0];
279 return (size_t) (hash % ht->size);
282 static hash_node_t *_util_htnewpair(const char *key, void *value) {
284 if (!(node = (hash_node_t*)mem_a(sizeof(hash_node_t))))
287 if (!(node->key = util_strdupe(key))) {
299 * EXPOSED INTERFACE for the hashtable implementation
300 * util_htnew(size) -- to make a new hashtable
301 * util_htset(table, key, value, sizeof(value)) -- to set something in the table
302 * util_htget(table, key) -- to get something from the table
303 * util_htdel(table) -- to delete the table
305 hash_table_t *util_htnew(size_t size) {
306 hash_table_t *hashtable = NULL;
307 stat_size_entry_t *find = NULL;
312 if (!stat_size_hashtables)
313 stat_size_hashtables = stat_size_new();
315 if (!(hashtable = (hash_table_t*)mem_a(sizeof(hash_table_t))))
318 if (!(hashtable->table = (hash_node_t**)mem_a(sizeof(hash_node_t*) * size))) {
323 if ((find = stat_size_get(stat_size_hashtables, size)))
326 stat_type_hashtables++;
327 stat_size_put(stat_size_hashtables, size, 1);
330 hashtable->size = size;
331 memset(hashtable->table, 0, sizeof(hash_node_t*) * size);
333 stat_used_hashtables++;
337 void util_htseth(hash_table_t *ht, const char *key, size_t bin, void *value) {
338 hash_node_t *newnode = NULL;
339 hash_node_t *next = NULL;
340 hash_node_t *last = NULL;
342 next = ht->table[bin];
344 while (next && next->key && strcmp(key, next->key) > 0)
345 last = next, next = next->next;
347 /* already in table, do a replace */
348 if (next && next->key && strcmp(key, next->key) == 0) {
351 /* not found, grow a pair man :P */
352 newnode = _util_htnewpair(key, value);
353 if (next == ht->table[bin]) {
354 newnode->next = next;
355 ht->table[bin] = newnode;
357 last->next = newnode;
359 newnode->next = next;
360 last->next = newnode;
365 void util_htset(hash_table_t *ht, const char *key, void *value) {
366 util_htseth(ht, key, util_hthash(ht, key), value);
369 void *util_htgeth(hash_table_t *ht, const char *key, size_t bin) {
370 hash_node_t *pair = ht->table[bin];
372 while (pair && pair->key && strcmp(key, pair->key) > 0)
375 if (!pair || !pair->key || strcmp(key, pair->key) != 0)
381 void *util_htget(hash_table_t *ht, const char *key) {
382 return util_htgeth(ht, key, util_hthash(ht, key));
385 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin) {
390 keylen = strlen(key);
392 pair = ht->table[bin];
393 while (pair && pair->key) {
394 len = strlen(pair->key);
400 cmp = strcmp(key, pair->key);
408 cmp = strcmp(key, pair->key + len - keylen);
410 uintptr_t up = (uintptr_t)pair->value;
420 * Free all allocated data in a hashtable, this is quite the amount
423 void util_htrem(hash_table_t *ht, void (*callback)(void *data)) {
425 for (; i < ht->size; i++) {
426 hash_node_t *n = ht->table[i];
446 void util_htrmh(hash_table_t *ht, const char *key, size_t bin, void (*cb)(void*)) {
447 hash_node_t **pair = &ht->table[bin];
450 while (*pair && (*pair)->key && strcmp(key, (*pair)->key) > 0)
451 pair = &(*pair)->next;
454 if (!tmp || !tmp->key || strcmp(key, tmp->key) != 0)
465 void util_htrm(hash_table_t *ht, const char *key, void (*cb)(void*)) {
466 util_htrmh(ht, key, util_hthash(ht, key), cb);
469 void util_htdel(hash_table_t *ht) {
470 util_htrem(ht, NULL);
474 * The following functions below implement printing / dumping of statistical
477 static void stat_dump_mem_contents(stat_mem_block_t *memory, uint16_t cols) {
479 for (i = 0; i < memory->size + ((memory->size % cols) ? (cols - memory->size % cols) : 0); i++) {
480 if (i % cols == 0) con_out(" 0x%06X: ", i);
481 if (i < memory->size) con_out("%02X " , 0xFF & ((unsigned char*)(memory + 1))[i]);
484 if ((uint16_t)(i % cols) == (cols - 1)) {
485 for (j = i - (cols - 1); j <= i; j++) {
489 : (isprint(((unsigned char*)(memory + 1))[j]))
490 ? 0xFF & ((unsigned char*)(memory + 1)) [j]
499 static void stat_dump_mem_leaks() {
500 stat_mem_block_t *info;
501 for (info = stat_mem_block_root; info; info = info->next) {
502 con_out("lost: %u (bytes) at %s:%u\n",
508 stat_dump_mem_contents(info, OPTS_OPTION_U16(OPTION_MEMDUMPCOLS));
512 static void stat_dump_mem_info() {
513 con_out("Memory information:\n\
514 Total allocations: %llu\n\
515 Total deallocations: %llu\n\
516 Total allocated: %f (MB)\n\
517 Total deallocated: %f (MB)\n\
518 Total peak memory: %f (MB)\n\
519 Total leaked memory: %f (MB) in %llu allocations\n",
520 stat_mem_allocated_total,
521 stat_mem_deallocated_total,
522 (float)(stat_mem_allocated) / 1048576.0f,
523 (float)(stat_mem_deallocated) / 1048576.0f,
524 (float)(stat_mem_peak) / 1048576.0f,
525 (float)(stat_mem_allocated - stat_mem_deallocated) / 1048576.0f,
526 stat_mem_allocated_total - stat_mem_deallocated_total
530 static void stat_dump_stats_table(stat_size_table_t table, const char *string, uint64_t *size) {
536 for (i = 0, j = 0; i < ST_SIZE; i++) {
537 stat_size_entry_t *entry;
539 if (!(entry = table[i]))
542 con_out(string, (unsigned)j, (unsigned)entry->key, (unsigned)entry->value);
546 *size += entry->key * entry->value;
551 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
552 stat_dump_mem_leaks();
554 if (OPTS_OPTION_BOOL(OPTION_DEBUG) ||
555 OPTS_OPTION_BOOL(OPTION_MEMCHK))
556 stat_dump_mem_info();
558 if (OPTS_OPTION_BOOL(OPTION_MEMCHK) ||
559 OPTS_OPTION_BOOL(OPTION_STATISTICS)) {
562 con_out("\nAdditional Statistics:\n\
563 Total vectors allocated: %llu\n\
564 Total string duplicates: %llu\n\
565 Total hashtables allocated: %llu\n\
566 Total unique vector sizes: %llu\n",
569 stat_used_hashtables,
573 stat_dump_stats_table (
575 " %2u| # of %4u byte vectors: %u\n",
580 " Total unique hashtable sizes: %llu\n",
584 stat_dump_stats_table (
585 stat_size_hashtables,
586 " %2u| # of %4u element hashtables: %u\n",
591 " Total vector memory: %f (MB)\n",
592 (float)(mem) / 1048576.0f
596 if (stat_size_vectors)
597 stat_size_del(stat_size_vectors);
598 if (stat_size_hashtables)
599 stat_size_del(stat_size_hashtables);