2 * Copyright (C) 2012, 2013
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * For the valgrind integration of our allocator. This allows us to have
32 * more `accurate` valgrind output for our allocator, and also secures the
33 * possible underflows (where one could obtain access to the redzone that
34 * represents info about that allocation).
37 # include <valgrind/valgrind.h>
38 # include <valgrind/memcheck.h>
40 # define VALGRIND_MALLOCLIKE_BLOCK(PTR, ALLOC_SIZE, REDZONE_SIZE, ZEROED)
41 # define VALGRIND_FREELIKE_BLOCK(PTR, REDZONE_SIZE)
42 # define VALGRIND_MAKE_MEM_DEFINED(PTR, REDZONE_SIZE)
43 # define VALGRIND_MAKE_MEM_NOACCESS(PTR, REDZONE_SIZE)
47 * GMQCC performs tons of allocations, constructions, and crazyness
48 * all around. When trying to optimizes systems, or just get fancy
49 * statistics out of the compiler, it's often printf mess. This file
50 * implements the statistics system of the compiler. I.E the allocator
51 * we use to track allocations, and other systems of interest.
55 typedef struct stat_mem_block_s {
60 struct stat_mem_block_s *next;
61 struct stat_mem_block_s *prev;
67 } stat_size_entry_t, **stat_size_table_t;
69 static uint64_t stat_mem_allocated = 0;
70 static uint64_t stat_mem_deallocated = 0;
71 static uint64_t stat_mem_allocated_total = 0;
72 static uint64_t stat_mem_deallocated_total = 0;
73 static uint64_t stat_mem_high = 0;
74 static uint64_t stat_mem_peak = 0;
75 static uint64_t stat_mem_strdups = 0;
76 static uint64_t stat_used_strdups = 0;
77 static uint64_t stat_used_vectors = 0;
78 static uint64_t stat_used_hashtables = 0;
79 static uint64_t stat_type_vectors = 0;
80 static uint64_t stat_type_hashtables = 0;
81 static stat_size_table_t stat_size_vectors = NULL;
82 static stat_size_table_t stat_size_hashtables = NULL;
83 static stat_mem_block_t *stat_mem_block_root = NULL;
86 * A tiny size_t key-value hashtbale for tracking vector and hashtable
87 * sizes. We can use it for other things too, if we need to. This is
88 * very TIGHT, and efficent in terms of space though.
90 static stat_size_table_t stat_size_new(void) {
91 return (stat_size_table_t)memset(
92 mem_a(sizeof(stat_size_entry_t*) * ST_SIZE),
93 0, ST_SIZE * sizeof(stat_size_entry_t*)
97 static void stat_size_del(stat_size_table_t table) {
99 for (; i < ST_SIZE; i++) if(table[i]) mem_d(table[i]);
103 static stat_size_entry_t *stat_size_get(stat_size_table_t table, size_t key) {
104 size_t hash = (key % ST_SIZE);
105 while (table[hash] && table[hash]->key != key)
106 hash = (hash + 1) % ST_SIZE;
109 static void stat_size_put(stat_size_table_t table, size_t key, size_t value) {
110 size_t hash = (key % ST_SIZE);
111 while (table[hash] && table[hash]->key != key)
112 hash = (hash + 1) % ST_SIZE;
113 table[hash] = (stat_size_entry_t*)mem_a(sizeof(stat_size_entry_t));
114 table[hash]->key = key;
115 table[hash]->value = value;
119 * A basic header of information wrapper allocator. Simply stores
120 * information as a header, returns the memory + 1 past it, can be
121 * retrieved again with - 1. Where type is stat_mem_block_t*.
123 void *stat_mem_allocate(size_t size, size_t line, const char *file, const char *expr) {
124 stat_mem_block_t *info = (stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size);
125 void *data = (void*)(info + 1);
127 if(GMQCC_UNLIKELY(!info))
135 info->next = stat_mem_block_root;
137 /* likely since it only happens once */
138 if (GMQCC_LIKELY(stat_mem_block_root != NULL)) {
139 VALGRIND_MAKE_MEM_DEFINED(stat_mem_block_root, sizeof(stat_mem_block_t));
140 stat_mem_block_root->prev = info;
141 VALGRIND_MAKE_MEM_NOACCESS(stat_mem_block_root, sizeof(stat_mem_block_t));
144 stat_mem_block_root = info;
145 stat_mem_allocated += size;
146 stat_mem_high += size;
147 stat_mem_allocated_total ++;
149 if (stat_mem_high > stat_mem_peak)
150 stat_mem_peak = stat_mem_high;
152 VALGRIND_MALLOCLIKE_BLOCK(data, size, sizeof(stat_mem_block_t), 0);
156 void stat_mem_deallocate(void *ptr) {
157 stat_mem_block_t *info = NULL;
159 if (GMQCC_UNLIKELY(!ptr))
162 info = ((stat_mem_block_t*)ptr - 1);
165 * we need access to the redzone that represents the info block
168 VALGRIND_MAKE_MEM_DEFINED(info, sizeof(stat_mem_block_t));
170 stat_mem_deallocated += info->size;
171 stat_mem_high -= info->size;
172 stat_mem_deallocated_total ++;
175 /* just need access for a short period */
176 VALGRIND_MAKE_MEM_DEFINED(info->prev, sizeof(stat_mem_block_t));
177 info->prev->next = info->next;
178 /* don't need access anymore */
179 VALGRIND_MAKE_MEM_NOACCESS(info->prev, sizeof(stat_mem_block_t));
182 /* just need access for a short period */
183 VALGRIND_MAKE_MEM_DEFINED(info->next, sizeof(stat_mem_block_t));
184 info->next->prev = info->prev;
185 /* don't need access anymore */
186 VALGRIND_MAKE_MEM_NOACCESS(info->next, sizeof(stat_mem_block_t));
190 if (info == stat_mem_block_root)
191 stat_mem_block_root = info->next;
194 VALGRIND_MAKE_MEM_NOACCESS(info, sizeof(stat_mem_block_t));
195 VALGRIND_FREELIKE_BLOCK(ptr, sizeof(stat_mem_block_t));
198 void *stat_mem_reallocate(void *ptr, size_t size, size_t line, const char *file, const char *expr) {
199 stat_mem_block_t *oldinfo = NULL;
200 stat_mem_block_t *newinfo;
202 if (GMQCC_UNLIKELY(!ptr))
203 return stat_mem_allocate(size, line, file, expr);
205 /* stay consistent with glibc */
206 if (GMQCC_UNLIKELY(!size)) {
207 stat_mem_deallocate(ptr);
211 oldinfo = ((stat_mem_block_t*)ptr - 1);
212 newinfo = ((stat_mem_block_t*)malloc(sizeof(stat_mem_block_t) + size));
214 if (GMQCC_UNLIKELY(!newinfo)) {
215 stat_mem_deallocate(ptr);
219 VALGRIND_MALLOCLIKE_BLOCK(newinfo + 1, size, sizeof(stat_mem_block_t), 0);
221 /* we need access to the old info redzone */
222 VALGRIND_MAKE_MEM_DEFINED(oldinfo, sizeof(stat_mem_block_t));
224 memcpy(newinfo+1, oldinfo+1, oldinfo->size);
227 /* just need access for a short period */
228 VALGRIND_MAKE_MEM_DEFINED(oldinfo->prev, sizeof(stat_mem_block_t));
229 oldinfo->prev->next = oldinfo->next;
230 /* don't need access anymore */
231 VALGRIND_MAKE_MEM_NOACCESS(oldinfo->prev, sizeof(stat_mem_block_t));
235 /* just need access for a short period */
236 VALGRIND_MAKE_MEM_DEFINED(oldinfo->next, sizeof(stat_mem_block_t));
237 oldinfo->next->prev = oldinfo->prev;
238 /* don't need access anymore */
239 VALGRIND_MAKE_MEM_NOACCESS(oldinfo->next, sizeof(stat_mem_block_t));
243 if (oldinfo == stat_mem_block_root)
244 stat_mem_block_root = oldinfo->next;
246 /* we need access to the redzone for the newinfo block */
247 VALGRIND_MAKE_MEM_DEFINED(newinfo, sizeof(stat_mem_block_t));
249 newinfo->line = line;
250 newinfo->size = size;
251 newinfo->file = file;
252 newinfo->expr = expr;
253 newinfo->prev = NULL;
254 newinfo->next = stat_mem_block_root;
257 * likely since the only time there is no root is when it's
258 * being initialized first.
260 if (GMQCC_LIKELY(stat_mem_block_root != NULL)) {
261 /* we need access to the root */
262 VALGRIND_MAKE_MEM_DEFINED(stat_mem_block_root, sizeof(stat_mem_block_t));
263 stat_mem_block_root->prev = newinfo;
265 VALGRIND_MAKE_MEM_NOACCESS(stat_mem_block_root, sizeof(stat_mem_block_t));
268 stat_mem_block_root = newinfo;
269 stat_mem_allocated -= oldinfo->size;
270 stat_mem_high -= oldinfo->size;
271 stat_mem_allocated += newinfo->size;
272 stat_mem_high += newinfo->size;
275 * we're finished with the redzones, lets kill the access
278 VALGRIND_MAKE_MEM_NOACCESS(newinfo, sizeof(stat_mem_block_t));
279 VALGRIND_MAKE_MEM_NOACCESS(oldinfo, sizeof(stat_mem_block_t));
281 if (stat_mem_high > stat_mem_peak)
282 stat_mem_peak = stat_mem_high;
285 VALGRIND_FREELIKE_BLOCK(ptr, sizeof(stat_mem_block_t));
290 * strdup does it's own malloc, we need to track malloc. We don't want
291 * to overwrite malloc though, infact, we can't really hook it at all
292 * without library specific assumptions. So we re implement strdup.
294 char *stat_mem_strdup(const char *src, size_t line, const char *file, bool empty) {
302 if (((!empty) ? len : true) && (ptr = (char*)stat_mem_allocate(len + 1, line, file, "strdup"))) {
303 memcpy(ptr, src, len);
307 stat_used_strdups ++;
308 stat_mem_strdups += len;
313 * The reallocate function for resizing vectors.
315 void _util_vec_grow(void **a, size_t i, size_t s) {
316 vector_t *d = vec_meta(*a);
318 stat_size_entry_t *e = NULL;
322 m = 2 * d->allocated + i;
323 p = mem_r(d, s * m + sizeof(vector_t));
326 p = mem_a(s * m + sizeof(vector_t));
327 ((vector_t*)p)->used = 0;
331 if (!stat_size_vectors)
332 stat_size_vectors = stat_size_new();
334 if ((e = stat_size_get(stat_size_vectors, s))) {
337 stat_size_put(stat_size_vectors, s, 1); /* start off with 1 */
341 *a = (vector_t*)p + 1;
342 vec_meta(*a)->allocated = m;
346 * Hash table for generic data, based on dynamic memory allocations
347 * all around. This is the internal interface, please look for
348 * EXPOSED INTERFACE comment below
350 typedef struct hash_node_t {
351 char *key; /* the key for this node in table */
352 void *value; /* pointer to the data as void* */
353 struct hash_node_t *next; /* next node (linked list) */
357 * This is a patched version of the Murmur2 hashing function to use
358 * a proper pre-mix and post-mix setup. Infact this is Murmur3 for
359 * the most part just reinvented.
361 * Murmur 2 contains an inner loop such as:
374 * The two u32s that form the key are the same value x (pulled from data)
375 * this premix stage will perform the same results for both values. Unrolled
376 * this produces just:
386 * This appears to be fine, except what happens when m == 1? well x
387 * cancels out entierly, leaving just:
392 * So all keys hash to the same value, but how often does m == 1?
393 * well, it turns out testing x for all possible values yeilds only
394 * 172,013,942 unique results instead of 2^32. So nearly ~4.6 bits
395 * are cancelled out on average!
397 * This means we have a 14.5% (rounded) chance of colliding more, which
398 * results in another bucket/chain for the hashtable.
400 * We fix it buy upgrading the pre and post mix ssystems to align with murmur
404 #define GMQCC_ROTL32(X, R) (((X) << (R)) | ((X) >> (32 - (R))))
405 GMQCC_INLINE size_t util_hthash(hash_table_t *ht, const char *key) {
406 const unsigned char *data = (const unsigned char *)key;
407 const size_t len = strlen(key);
408 const size_t block = len / 4;
409 const uint32_t mask1 = 0xCC9E2D51;
410 const uint32_t mask2 = 0x1B873593;
411 const uint32_t *blocks = (const uint32_t*)(data + block * 4);
412 const unsigned char *tail = (const unsigned char *)(data + block * 4);
416 uint32_t h = 0x1EF0 ^ len;
418 for (i = -((int)block); i; i++) {
421 k = GMQCC_ROTL32(k, 15);
424 h = GMQCC_ROTL32(h, 13);
425 h = h * 5 + 0xE6546B64;
437 k = GMQCC_ROTL32(k, 15);
449 return (size_t) (h % ht->size);
453 /* We keep the old for reference */
454 GMQCC_INLINE size_t util_hthash(hash_table_t *ht, const char *key) {
455 const uint32_t mix = 0x5BD1E995;
456 const uint32_t rot = 24;
457 size_t size = strlen(key);
458 uint32_t hash = 0x1EF0 /* LICRC TAB */ ^ size;
460 const unsigned char *data = (const unsigned char*)key;
463 alias = (data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24));
465 alias ^= alias >> rot;
476 case 3: hash ^= data[2] << 16;
477 case 2: hash ^= data[1] << 8;
478 case 1: hash ^= data[0];
486 return (size_t) (hash % ht->size);
490 static hash_node_t *_util_htnewpair(const char *key, void *value) {
492 if (!(node = (hash_node_t*)mem_a(sizeof(hash_node_t))))
495 if (!(node->key = util_strdupe(key))) {
507 * EXPOSED INTERFACE for the hashtable implementation
508 * util_htnew(size) -- to make a new hashtable
509 * util_htset(table, key, value, sizeof(value)) -- to set something in the table
510 * util_htget(table, key) -- to get something from the table
511 * util_htdel(table) -- to delete the table
513 hash_table_t *util_htnew(size_t size) {
514 hash_table_t *hashtable = NULL;
515 stat_size_entry_t *find = NULL;
520 if (!stat_size_hashtables)
521 stat_size_hashtables = stat_size_new();
523 if (!(hashtable = (hash_table_t*)mem_a(sizeof(hash_table_t))))
526 if (!(hashtable->table = (hash_node_t**)mem_a(sizeof(hash_node_t*) * size))) {
531 if ((find = stat_size_get(stat_size_hashtables, size)))
534 stat_type_hashtables++;
535 stat_size_put(stat_size_hashtables, size, 1);
538 hashtable->size = size;
539 memset(hashtable->table, 0, sizeof(hash_node_t*) * size);
541 stat_used_hashtables++;
545 void util_htseth(hash_table_t *ht, const char *key, size_t bin, void *value) {
546 hash_node_t *newnode = NULL;
547 hash_node_t *next = NULL;
548 hash_node_t *last = NULL;
550 next = ht->table[bin];
552 while (next && next->key && strcmp(key, next->key) > 0)
553 last = next, next = next->next;
555 /* already in table, do a replace */
556 if (next && next->key && strcmp(key, next->key) == 0) {
559 /* not found, grow a pair man :P */
560 newnode = _util_htnewpair(key, value);
561 if (next == ht->table[bin]) {
562 newnode->next = next;
563 ht->table[bin] = newnode;
565 last->next = newnode;
567 newnode->next = next;
568 last->next = newnode;
573 void util_htset(hash_table_t *ht, const char *key, void *value) {
574 util_htseth(ht, key, util_hthash(ht, key), value);
577 void *util_htgeth(hash_table_t *ht, const char *key, size_t bin) {
578 hash_node_t *pair = ht->table[bin];
580 while (pair && pair->key && strcmp(key, pair->key) > 0)
583 if (!pair || !pair->key || strcmp(key, pair->key) != 0)
589 void *util_htget(hash_table_t *ht, const char *key) {
590 return util_htgeth(ht, key, util_hthash(ht, key));
593 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin);
594 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin) {
599 keylen = strlen(key);
601 pair = ht->table[bin];
602 while (pair && pair->key) {
603 len = strlen(pair->key);
609 cmp = strcmp(key, pair->key);
617 cmp = strcmp(key, pair->key + len - keylen);
619 uintptr_t up = (uintptr_t)pair->value;
629 * Free all allocated data in a hashtable, this is quite the amount
632 void util_htrem(hash_table_t *ht, void (*callback)(void *data)) {
635 for (; i < ht->size; ++i) {
636 hash_node_t *n = ht->table[i];
656 void util_htrmh(hash_table_t *ht, const char *key, size_t bin, void (*cb)(void*)) {
657 hash_node_t **pair = &ht->table[bin];
660 while (*pair && (*pair)->key && strcmp(key, (*pair)->key) > 0)
661 pair = &(*pair)->next;
664 if (!tmp || !tmp->key || strcmp(key, tmp->key) != 0)
675 void util_htrm(hash_table_t *ht, const char *key, void (*cb)(void*)) {
676 util_htrmh(ht, key, util_hthash(ht, key), cb);
679 void util_htdel(hash_table_t *ht) {
680 util_htrem(ht, NULL);
684 * The following functions below implement printing / dumping of statistical
687 static void stat_dump_mem_contents(stat_mem_block_t *block, uint16_t cols) {
688 unsigned char *buffer = (unsigned char *)mem_a(cols);
689 unsigned char *memory = (unsigned char *)(block + 1);
692 for (i = 0; i < block->size; i++) {
695 con_out(" %s\n", buffer);
696 con_out(" 0x%08X: ", i);
699 con_out(" %02X", memory[i]);
701 buffer[i % cols] = ((memory[i] < 0x20) || (memory[i] > 0x7E))
705 buffer[(i % cols) + 1] = '\0';
708 while ((i % cols) != 0) {
713 con_out(" %s\n", buffer);
717 static void stat_dump_mem_leaks(void) {
718 stat_mem_block_t *info;
719 /* we need access to the root for this */
720 VALGRIND_MAKE_MEM_DEFINED(stat_mem_block_root, sizeof(stat_mem_block_t));
721 for (info = stat_mem_block_root; info; info = info->next) {
722 /* we need access to the block */
723 VALGRIND_MAKE_MEM_DEFINED(info, sizeof(stat_mem_block_t));
724 con_out("lost: %u (bytes) at %s:%u from expression `%s`\n",
731 stat_dump_mem_contents(info, OPTS_OPTION_U16(OPTION_MEMDUMPCOLS));
734 * we're finished with the access, the redzone should be marked
735 * inaccesible so that invalid read/writes that could 'step-into'
736 * those redzones will show up as invalid read/writes in valgrind.
738 VALGRIND_MAKE_MEM_NOACCESS(info, sizeof(stat_mem_block_t));
740 VALGRIND_MAKE_MEM_NOACCESS(stat_mem_block_root, sizeof(stat_mem_block_t));
743 static void stat_dump_mem_info(void) {
744 con_out("Memory Information:\n\
745 Total allocations: %llu\n\
746 Total deallocations: %llu\n\
747 Total allocated: %f (MB)\n\
748 Total deallocated: %f (MB)\n\
749 Total peak memory: %f (MB)\n\
750 Total leaked memory: %f (MB) in %llu allocations\n",
751 stat_mem_allocated_total,
752 stat_mem_deallocated_total,
753 (float)(stat_mem_allocated) / 1048576.0f,
754 (float)(stat_mem_deallocated) / 1048576.0f,
755 (float)(stat_mem_peak) / 1048576.0f,
756 (float)(stat_mem_allocated - stat_mem_deallocated) / 1048576.0f,
757 stat_mem_allocated_total - stat_mem_deallocated_total
761 static void stat_dump_stats_table(stat_size_table_t table, const char *string, uint64_t *size) {
767 for (i = 0, j = 1; i < ST_SIZE; i++) {
768 stat_size_entry_t *entry;
770 if (!(entry = table[i]))
773 con_out(string, (unsigned)j, (unsigned)entry->key, (unsigned)entry->value);
777 *size += entry->key * entry->value;
782 if (OPTS_OPTION_BOOL(OPTION_MEMCHK) ||
783 OPTS_OPTION_BOOL(OPTION_STATISTICS)) {
786 con_out("Memory Statistics:\n\
787 Total vectors allocated: %llu\n\
788 Total string duplicates: %llu\n\
789 Total string duplicate memory: %f (MB)\n\
790 Total hashtables allocated: %llu\n\
791 Total unique vector sizes: %llu\n",
794 (float)(stat_mem_strdups) / 1048576.0f,
795 stat_used_hashtables,
799 stat_dump_stats_table (
801 " %2u| # of %5u byte vectors: %u\n",
806 " Total unique hashtable sizes: %llu\n",
810 stat_dump_stats_table (
811 stat_size_hashtables,
812 " %2u| # of %5u element hashtables: %u\n",
817 " Total vector memory: %f (MB)\n\n",
818 (float)(mem) / 1048576.0f
822 if (stat_size_vectors)
823 stat_size_del(stat_size_vectors);
824 if (stat_size_hashtables)
825 stat_size_del(stat_size_hashtables);
827 if (OPTS_OPTION_BOOL(OPTION_DEBUG) ||
828 OPTS_OPTION_BOOL(OPTION_MEMCHK))
829 stat_dump_mem_info();
831 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
832 stat_dump_mem_leaks();