1 /* mm_diff - Memory snapshooting and comparison */
3 /* Copyright (c) 2008-2012. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/ex_interface.h" /* internals of backtrace setup */
11 #include "xbt/mmalloc.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
14 "Logging specific to mm_diff in mmalloc");
16 extern char *xbt_binary_name;
18 xbt_dynar_t mc_heap_comparison_ignore;
19 xbt_dynar_t stacks_areas;
21 static void heap_area_pair_free(heap_area_pair_t pair);
22 static void heap_area_pair_free_voidp(void *d);
23 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
24 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
25 static heap_area_t new_heap_area(int block, int fragment);
27 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore);
28 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals);
30 static int in_mc_comparison_ignore(int block, int fragment);
31 static size_t heap_comparison_ignore_size(void *address);
32 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2);
33 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a);
35 static char* is_stack(void *address);
37 void mmalloc_backtrace_block_display(void* heapinfo, int block){
41 if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) {
42 fprintf(stderr, "No backtrace available for that block, sorry.\n");
46 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
47 e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size;
49 xbt_ex_setup_backtrace(&e);
51 fprintf(stderr, "(backtrace not set)\n");
52 } else if (e.bt_strings == NULL) {
53 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
57 fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used);
58 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
59 fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4);
65 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
69 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
70 e.used = XBT_BACKTRACE_SIZE;
72 xbt_ex_setup_backtrace(&e);
74 fprintf(stderr, "(backtrace not set)\n");
75 } else if (e.bt_strings == NULL) {
76 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
80 fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used);
81 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
82 fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4);
88 void mmalloc_backtrace_display(void *addr){
90 size_t block, frag_nb;
93 xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
95 block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1);
97 type = heap->heapinfo[block].type;
100 case -1 : /* Free block */
101 fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n");
104 case 0: /* Large block */
105 mmalloc_backtrace_block_display(heap->heapinfo, block);
107 default: /* Fragmented block */
108 frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type;
109 if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){
110 fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n");
113 mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb);
120 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
121 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
122 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
126 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stack1, xbt_dynar_t *stack2, xbt_dynar_t *equals){
128 if(heap1 == NULL && heap1 == NULL){
129 XBT_DEBUG("Malloc descriptors null");
133 if(heap1->heaplimit != heap2->heaplimit){
134 XBT_DEBUG("Different limit of valid info table indices");
138 if(heap1->heapstats.chunks_used != heap2->heapstats.chunks_used){
139 XBT_DEBUG("Different number of chunks used in heap : %zu - %zu", heap1->heapstats.chunks_used, heap2->heapstats.chunks_used);
143 /* Heap information */
144 heaplimit = ((struct mdesc *)heap1)->heaplimit;
146 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
148 heapbase1 = (char *)heap1 + BLOCKSIZE;
149 heapbase2 = (char *)heap2 + BLOCKSIZE;
151 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
152 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
154 heapsize1 = heap1->heapsize;
155 heapsize2 = heap2->heapsize;
157 /* Start comparison */
158 size_t i1, i2, j1, j2, k, current_block, current_fragment;
159 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
160 void *real_addr_block1, *real_addr_block2;
163 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
165 int equal, res_compare;
167 /* Init equal information */
170 while(i1<=heaplimit){
171 if(heapinfo1[i1].type == 0){
172 heapinfo1[i1].busy_block.equal_to = NULL;
174 if(heapinfo1[i1].type > 0){
175 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
176 heapinfo1[i1].busy_frag.equal_to[j1] = NULL;
184 while(i2<=heaplimit){
185 if(heapinfo2[i2].type == 0){
186 heapinfo2[i2].busy_block.equal_to = NULL;
188 if(heapinfo2[i2].type > 0){
189 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
190 heapinfo2[i2].busy_frag.equal_to[j2] = NULL;
196 /* Check busy blocks*/
200 while(i1 <= heaplimit){
204 if(heapinfo1[i1].type == -1){ /* Free block */
209 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
210 real_addr_block1 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block1) - (char *)heapbase1);
212 if(heapinfo1[i1].type == 0){ /* Large block */
214 if((stack_name = is_stack(real_addr_block1)) != NULL){
215 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
216 stack->address = addr_block1;
217 stack->process_name = strdup(stack_name);
218 stack->size = heapinfo1[i1].busy_block.busy_size;
219 xbt_dynar_push(*stack1, &stack);
222 if(heapinfo1[i1].busy_block.busy_size == 0){
227 if(heapinfo1[i1].busy_block.equal_to != NULL){
235 /* Try first to associate to same block in the other heap */
236 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
238 if(heapinfo2[current_block].busy_block.equal_to == NULL){
240 if(heapinfo1[current_block].busy_block.busy_size == heapinfo2[current_block].busy_block.busy_size){
242 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
243 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
245 if((stack_name = is_stack(real_addr_block2)) != NULL){
246 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
247 stack->address = addr_block2;
248 stack->process_name = strdup(stack_name);
249 stack->size = heapinfo2[current_block].busy_block.busy_size;
250 xbt_dynar_push(*stack2, &stack);
253 add_heap_area_pair(previous, current_block, -1, current_block, -1);
255 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
256 if(in_mc_comparison_ignore((int)current_block, -1))
257 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 1);
259 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
261 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
264 if(res_compare == 0){
265 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
266 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
267 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
268 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
270 match_equals(previous, equals);
271 i1 = i1 + heapinfo1[current_block].busy_block.size;
274 xbt_dynar_reset(previous);
282 while(i2 <= heaplimit && !equal){
284 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
285 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
287 if((stack_name = is_stack(real_addr_block2)) != NULL){
288 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
289 stack->address = addr_block2;
290 stack->process_name = strdup(stack_name);
291 stack->size = heapinfo2[i2].busy_block.busy_size;
292 xbt_dynar_push(*stack2, &stack);
295 if(i2 == current_block){
300 if(heapinfo2[i2].type != 0){
305 if(heapinfo2[i2].busy_block.equal_to != NULL){
310 if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
315 if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
321 add_heap_area_pair(previous, i1, -1, i2, -1);
323 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
324 if(in_mc_comparison_ignore((int)i1, -1))
325 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 1);
327 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
329 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
332 if(res_compare == 0){
333 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
334 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
335 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
336 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
338 match_equals(previous, equals);
339 i1 = i1 + heapinfo1[i1].busy_block.size;
342 xbt_dynar_reset(previous);
351 }else{ /* Fragmented block */
353 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
355 current_fragment = j1;
357 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
360 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
363 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
368 /* Try first to associate to same fragment in the other heap */
369 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
371 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
373 if(heapinfo1[current_block].busy_frag.frag_size[current_fragment] == heapinfo2[current_block].busy_frag.frag_size[current_fragment]){
375 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
376 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << heapinfo2[current_block].type));
378 add_heap_area_pair(previous, current_block, current_fragment, current_block, current_fragment);
380 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
381 if(in_mc_comparison_ignore((int)current_block, (int)current_fragment))
382 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 1);
384 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
386 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
389 if(res_compare == 0){
391 match_equals(previous, equals);
394 xbt_dynar_reset(previous);
402 while(i2 <= heaplimit && !equal){
405 if(heapinfo2[i2].type <= 0){
410 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
412 if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
415 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
418 if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]) /* Different size_used */
421 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
422 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << heapinfo2[i2].type));
425 add_heap_area_pair(previous, i1, j1, i2, j2);
427 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
428 if(in_mc_comparison_ignore((int)i1, (int)j1))
429 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 1);
431 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
433 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
436 if(res_compare == 0){
438 match_equals(previous, equals);
442 xbt_dynar_reset(previous);
458 /* All blocks/fragments are equal to another block/fragment ? */
460 int nb_diff1 = 0, nb_diff2 = 0;
463 if(heapinfo1[i].type == 0){
464 if(heapinfo1[i].busy_block.busy_size > 0){
465 if(heapinfo1[i].busy_block.equal_to == NULL){
466 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
467 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
468 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
469 mmalloc_backtrace_block_display((void*)heapinfo1, i);
473 xbt_free(heapinfo1[i].busy_block.equal_to);
477 if(heapinfo1[i].type > 0){
478 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
479 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
480 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
481 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
482 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
483 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
484 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
485 mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
489 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
498 XBT_DEBUG("Different blocks or fragments in heap1 : %d", nb_diff1);
503 if(heapinfo2[i].type == 0){
504 if(heapinfo2[i].busy_block.busy_size > 0){
505 if(heapinfo2[i].busy_block.equal_to == NULL){
506 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
507 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
508 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
509 mmalloc_backtrace_block_display((void*)heapinfo2, i);
513 xbt_free(heapinfo2[i].busy_block.equal_to);
517 if(heapinfo2[i].type > 0){
518 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
519 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
520 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
521 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
522 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
523 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
524 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
525 mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
529 xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
537 XBT_DEBUG("Different blocks or fragments in heap2 : %d", nb_diff2);
539 xbt_dynar_free(&previous);
541 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
542 heapinfo1 = NULL, heapinfo2 = NULL;
543 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
545 return ((nb_diff1 > 0) || (nb_diff2 > 0));
549 static heap_area_t new_heap_area(int block, int fragment){
550 heap_area_t area = NULL;
551 area = xbt_new0(s_heap_area_t, 1);
553 area->fragment = fragment;
557 static int in_mc_comparison_ignore(int block, int fragment){
559 unsigned int cursor = 0;
561 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
562 mc_heap_ignore_region_t region;
565 cursor = (start + end) / 2;
566 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
567 if(region->block == block){
568 if(region->fragment == fragment)
570 if(region->fragment < fragment)
572 if(region->fragment > fragment)
575 if(region->block < block)
577 if(region->block > block)
584 static size_t heap_comparison_ignore_size(void *address){
585 unsigned int cursor = 0;
587 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
588 mc_heap_ignore_region_t region;
591 cursor = (start + end) / 2;
592 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
593 if(region->address == address)
595 if(region->address < address)
597 if(region->address > address)
605 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore){
607 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
608 void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
609 size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
611 void *current_area1, *current_area2;
617 current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
618 if((ignore1 = heap_comparison_ignore_size(current_area1)) > 0){
619 current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
620 if((ignore2 = heap_comparison_ignore_size(current_area2)) == ignore1){
629 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
631 /* Check pointer difference */
632 pointer_align = (i / sizeof(void*)) * sizeof(void*);
633 address_pointed1 = *((void **)((char *)area1 + pointer_align));
634 address_pointed2 = *((void **)((char *)area2 + pointer_align));
636 /* Get pointed blocks number */
637 block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
638 block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
640 /* Check if valid blocks number */
641 if((char *)address_pointed1 < (char*)((xbt_mheap_t)s_heap)->heapbase || block_pointed1 > heapsize1 || block_pointed1 < 1 || (char *)address_pointed2 < (char*)((xbt_mheap_t)s_heap)->heapbase || block_pointed2 > heapsize2 || block_pointed2 < 1)
644 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
646 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
647 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
649 if(heapinfo1[block_pointed1].type == 0){ /* Large block */
651 if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
655 if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
659 if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
661 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
662 if(in_mc_comparison_ignore(block_pointed1, -1))
663 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 1);
665 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
667 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
675 }else{ /* Fragmented block */
677 /* Get pointed fragments number */
678 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
679 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
681 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
684 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
685 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
687 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
689 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
690 if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
691 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
693 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
695 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
707 if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
709 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
710 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
712 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
713 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
715 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
718 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
719 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
721 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
723 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
724 if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
725 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
727 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
729 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
743 i = pointer_align + sizeof(void *);
757 static void heap_area_pair_free(heap_area_pair_t pair){
764 static void heap_area_pair_free_voidp(void *d)
766 heap_area_pair_free((heap_area_pair_t) * (void **) d);
769 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
771 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
772 heap_area_pair_t pair = NULL;
773 pair = xbt_new0(s_heap_area_pair_t, 1);
774 pair->block1 = block1;
775 pair->fragment1 = fragment1;
776 pair->block2 = block2;
777 pair->fragment2 = fragment2;
779 xbt_dynar_push(list, &pair);
787 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
789 unsigned int cursor = 0;
790 heap_area_pair_t current_pair;
792 xbt_dynar_foreach(list, cursor, current_pair){
793 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
800 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals){
802 unsigned int cursor = 0;
803 heap_area_pair_t current_pair;
804 heap_area_t previous_area;
806 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
808 xbt_dynar_foreach(list, cursor, current_pair){
810 if(current_pair->fragment1 != -1){
812 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
813 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (current_pair->fragment1 << heapinfo1[current_pair->block1].type));
814 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
815 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (current_pair->fragment2 << heapinfo2[current_pair->block2].type));
817 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
818 remove_heap_equality(equals, 1, real_addr_frag1);
819 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
820 xbt_free( heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
821 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
822 xbt_free(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1]);
824 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
825 remove_heap_equality(equals, 2, real_addr_frag2);
826 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
827 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
828 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
829 xbt_free(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2]);
832 if(real_addr_frag1 != real_addr_frag2)
833 add_heap_equality(equals, real_addr_frag1, real_addr_frag2);
835 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
836 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
840 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
841 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
843 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
844 remove_heap_equality(equals, 1, real_addr_block1);
845 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
846 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
847 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
848 xbt_free(heapinfo1[current_pair->block1].busy_block.equal_to);
850 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
851 remove_heap_equality(equals, 2, real_addr_block2);
852 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
853 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
854 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
855 xbt_free(heapinfo2[current_pair->block2].busy_block.equal_to);
858 if(real_addr_block1 != real_addr_block2)
859 add_heap_equality(equals, real_addr_block1, real_addr_block2);
861 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
862 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
871 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
874 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
876 if(heap1 == NULL && heap1 == NULL){
877 XBT_DEBUG("Malloc descriptors null");
881 if(heap1->heaplimit != heap2->heaplimit){
882 XBT_DEBUG("Different limit of valid info table indices");
886 /* Heap information */
887 heaplimit = ((struct mdesc *)heap1)->heaplimit;
889 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
891 heapbase1 = (char *)heap1 + BLOCKSIZE;
892 heapbase2 = (char *)heap2 + BLOCKSIZE;
894 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
895 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
897 heapsize1 = heap1->heapsize;
898 heapsize2 = heap2->heapsize;
900 /* Start comparison */
902 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
906 /* Check busy blocks*/
910 while(i <= heaplimit){
912 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
913 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
915 if(heapinfo1[i].type != heapinfo2[i].type){
917 distance += BLOCKSIZE;
918 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
923 if(heapinfo1[i].type == -1){ /* Free block */
928 if(heapinfo1[i].type == 0){ /* Large block */
930 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
931 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
932 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
933 XBT_DEBUG("Different larger of cluster at block %zu : %zu - %zu -> distance = %d", i, heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size, distance);
937 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
938 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
939 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
940 XBT_DEBUG("Different size used oin large cluster at block %zu : %zu - %zu -> distance = %d", i, heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size, distance);
946 //while(k < (heapinfo1[i].busy_block.busy_size)){
947 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
948 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
956 }else { /* Fragmented block */
958 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
960 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
961 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
963 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
968 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
969 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
970 XBT_DEBUG("Different size used in fragment %zu in block %zu : %d - %d -> distance = %d", j, i, heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j], distance);
976 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
977 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
978 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
998 static char * is_stack(void *address){
999 unsigned int cursor = 0;
1000 stack_region_t stack;
1002 xbt_dynar_foreach(stacks_areas, cursor, stack){
1003 if(address == stack->address)
1004 return stack->process_name;
1010 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2){
1012 if(xbt_dynar_is_empty(*equals)){
1014 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1018 xbt_dynar_insert_at(*equals, 0, &he);
1022 unsigned int cursor = 0;
1024 int end = xbt_dynar_length(*equals) - 1;
1025 heap_equality_t current_equality = NULL;
1027 while(start <= end){
1028 cursor = (start + end) / 2;
1029 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1030 if(current_equality->address1 == a1){
1031 if(current_equality->address2 == a2)
1033 if(current_equality->address2 < a2)
1035 if(current_equality->address2 > a2)
1038 if(current_equality->address1 < a1)
1040 if(current_equality->address1 > a1)
1044 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1048 if(current_equality->address1 < a1)
1049 xbt_dynar_insert_at(*equals, cursor + 1 , &he);
1051 xbt_dynar_insert_at(*equals, cursor, &he);
1057 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a){
1059 unsigned int cursor = 0;
1060 heap_equality_t current_equality;
1066 int end = xbt_dynar_length(*equals) - 1;
1069 while(start <= end && found == 0){
1070 cursor = (start + end) / 2;
1071 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1072 if(current_equality->address1 == a)
1074 if(current_equality->address1 < a)
1076 if(current_equality->address1 > a)
1081 xbt_dynar_remove_at(*equals, cursor, NULL);
1085 xbt_dynar_foreach(*equals, cursor, current_equality){
1086 if(current_equality->address2 == a){
1093 xbt_dynar_remove_at(*equals, cursor, NULL);
1100 int is_free_area(void *area, xbt_mheap_t heap){
1102 void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1103 malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1104 size_t heapsize = heap->heapsize;
1106 /* Get block number */
1107 size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1110 /* Check if valid block number */
1111 if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1114 if(heapinfo[block].type < 0)
1117 if(heapinfo[block].type == 0)
1120 if(heapinfo[block].type > 0){
1121 fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1122 if(heapinfo[block].busy_frag.frag_size[fragment] == 0)