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 size_t hash(const char *key);
358 size_t util_hthash(hash_table_t *ht, const char *key) {
359 return hash(key) % ht->size;
362 static hash_node_t *_util_htnewpair(const char *key, void *value) {
364 if (!(node = (hash_node_t*)mem_a(sizeof(hash_node_t))))
367 if (!(node->key = util_strdupe(key))) {
379 * EXPOSED INTERFACE for the hashtable implementation
380 * util_htnew(size) -- to make a new hashtable
381 * util_htset(table, key, value, sizeof(value)) -- to set something in the table
382 * util_htget(table, key) -- to get something from the table
383 * util_htdel(table) -- to delete the table
385 hash_table_t *util_htnew(size_t size) {
386 hash_table_t *hashtable = NULL;
387 stat_size_entry_t *find = NULL;
392 if (!stat_size_hashtables)
393 stat_size_hashtables = stat_size_new();
395 if (!(hashtable = (hash_table_t*)mem_a(sizeof(hash_table_t))))
398 if (!(hashtable->table = (hash_node_t**)mem_a(sizeof(hash_node_t*) * size))) {
403 if ((find = stat_size_get(stat_size_hashtables, size)))
406 stat_type_hashtables++;
407 stat_size_put(stat_size_hashtables, size, 1);
410 hashtable->size = size;
411 memset(hashtable->table, 0, sizeof(hash_node_t*) * size);
413 stat_used_hashtables++;
417 void util_htseth(hash_table_t *ht, const char *key, size_t bin, void *value) {
418 hash_node_t *newnode = NULL;
419 hash_node_t *next = NULL;
420 hash_node_t *last = NULL;
422 next = ht->table[bin];
424 while (next && next->key && strcmp(key, next->key) > 0)
425 last = next, next = next->next;
427 /* already in table, do a replace */
428 if (next && next->key && strcmp(key, next->key) == 0) {
431 /* not found, grow a pair man :P */
432 newnode = _util_htnewpair(key, value);
433 if (next == ht->table[bin]) {
434 newnode->next = next;
435 ht->table[bin] = newnode;
437 last->next = newnode;
439 newnode->next = next;
440 last->next = newnode;
445 void util_htset(hash_table_t *ht, const char *key, void *value) {
446 util_htseth(ht, key, util_hthash(ht, key), value);
449 void *util_htgeth(hash_table_t *ht, const char *key, size_t bin) {
450 hash_node_t *pair = ht->table[bin];
452 while (pair && pair->key && strcmp(key, pair->key) > 0)
455 if (!pair || !pair->key || strcmp(key, pair->key) != 0)
461 void *util_htget(hash_table_t *ht, const char *key) {
462 return util_htgeth(ht, key, util_hthash(ht, key));
465 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin);
466 void *code_util_str_htgeth(hash_table_t *ht, const char *key, size_t bin) {
471 keylen = strlen(key);
473 pair = ht->table[bin];
474 while (pair && pair->key) {
475 len = strlen(pair->key);
481 cmp = strcmp(key, pair->key);
489 cmp = strcmp(key, pair->key + len - keylen);
491 uintptr_t up = (uintptr_t)pair->value;
501 * Free all allocated data in a hashtable, this is quite the amount
504 void util_htrem(hash_table_t *ht, void (*callback)(void *data)) {
507 for (; i < ht->size; ++i) {
508 hash_node_t *n = ht->table[i];
528 void util_htrmh(hash_table_t *ht, const char *key, size_t bin, void (*cb)(void*)) {
529 hash_node_t **pair = &ht->table[bin];
532 while (*pair && (*pair)->key && strcmp(key, (*pair)->key) > 0)
533 pair = &(*pair)->next;
536 if (!tmp || !tmp->key || strcmp(key, tmp->key) != 0)
547 void util_htrm(hash_table_t *ht, const char *key, void (*cb)(void*)) {
548 util_htrmh(ht, key, util_hthash(ht, key), cb);
551 void util_htdel(hash_table_t *ht) {
552 util_htrem(ht, NULL);
556 * The following functions below implement printing / dumping of statistical
559 static void stat_dump_mem_contents(stat_mem_block_t *block, uint16_t cols) {
560 unsigned char *buffer = (unsigned char *)mem_a(cols);
561 unsigned char *memory = (unsigned char *)(block + 1);
564 for (i = 0; i < block->size; i++) {
567 con_out(" %s\n", buffer);
568 con_out(" 0x%08X: ", i);
571 con_out(" %02X", memory[i]);
573 buffer[i % cols] = ((memory[i] < 0x20) || (memory[i] > 0x7E))
577 buffer[(i % cols) + 1] = '\0';
580 while ((i % cols) != 0) {
585 con_out(" %s\n", buffer);
589 static void stat_dump_mem_leaks(void) {
590 stat_mem_block_t *info;
591 /* we need access to the root for this */
592 VALGRIND_MAKE_MEM_DEFINED(stat_mem_block_root, sizeof(stat_mem_block_t));
593 for (info = stat_mem_block_root; info; info = info->next) {
594 /* we need access to the block */
595 VALGRIND_MAKE_MEM_DEFINED(info, sizeof(stat_mem_block_t));
596 con_out("lost: %u (bytes) at %s:%u from expression `%s`\n",
603 stat_dump_mem_contents(info, OPTS_OPTION_U16(OPTION_MEMDUMPCOLS));
606 * we're finished with the access, the redzone should be marked
607 * inaccesible so that invalid read/writes that could 'step-into'
608 * those redzones will show up as invalid read/writes in valgrind.
610 VALGRIND_MAKE_MEM_NOACCESS(info, sizeof(stat_mem_block_t));
612 VALGRIND_MAKE_MEM_NOACCESS(stat_mem_block_root, sizeof(stat_mem_block_t));
615 static void stat_dump_mem_info(void) {
616 con_out("Memory Information:\n\
617 Total allocations: %llu\n\
618 Total deallocations: %llu\n\
619 Total allocated: %f (MB)\n\
620 Total deallocated: %f (MB)\n\
621 Total peak memory: %f (MB)\n\
622 Total leaked memory: %f (MB) in %llu allocations\n",
623 stat_mem_allocated_total,
624 stat_mem_deallocated_total,
625 (float)(stat_mem_allocated) / 1048576.0f,
626 (float)(stat_mem_deallocated) / 1048576.0f,
627 (float)(stat_mem_peak) / 1048576.0f,
628 (float)(stat_mem_allocated - stat_mem_deallocated) / 1048576.0f,
629 stat_mem_allocated_total - stat_mem_deallocated_total
633 static void stat_dump_stats_table(stat_size_table_t table, const char *string, uint64_t *size) {
639 for (i = 0, j = 1; i < ST_SIZE; i++) {
640 stat_size_entry_t *entry;
642 if (!(entry = table[i]))
645 con_out(string, (unsigned)j, (unsigned)entry->key, (unsigned)entry->value);
649 *size += entry->key * entry->value;
654 if (OPTS_OPTION_BOOL(OPTION_MEMCHK) ||
655 OPTS_OPTION_BOOL(OPTION_STATISTICS)) {
658 con_out("Memory Statistics:\n\
659 Total vectors allocated: %llu\n\
660 Total string duplicates: %llu\n\
661 Total string duplicate memory: %f (MB)\n\
662 Total hashtables allocated: %llu\n\
663 Total unique vector sizes: %llu\n",
666 (float)(stat_mem_strdups) / 1048576.0f,
667 stat_used_hashtables,
671 stat_dump_stats_table (
673 " %2u| # of %5u byte vectors: %u\n",
678 " Total unique hashtable sizes: %llu\n",
682 stat_dump_stats_table (
683 stat_size_hashtables,
684 " %2u| # of %5u element hashtables: %u\n",
689 " Total vector memory: %f (MB)\n\n",
690 (float)(mem) / 1048576.0f
694 if (stat_size_vectors)
695 stat_size_del(stat_size_vectors);
696 if (stat_size_hashtables)
697 stat_size_del(stat_size_hashtables);
699 if (OPTS_OPTION_BOOL(OPTION_DEBUG) ||
700 OPTS_OPTION_BOOL(OPTION_MEMCHK))
701 stat_dump_mem_info();
703 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
704 stat_dump_mem_leaks();