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_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(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 XBT_DEBUG("No backtrace available for that block, sorry.");
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 XBT_DEBUG("(backtrace not set)");
52 } else if (e.bt_strings == NULL) {
53 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
57 XBT_DEBUG("Backtrace of where the block %d was malloced (%d frames):", block ,e.used);
58 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
59 XBT_DEBUG("%d ---> %s",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 XBT_DEBUG("(backtrace not set)");
75 } else if (e.bt_strings == NULL) {
76 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
80 XBT_DEBUG("Backtrace of where the fragment %d in block %d was malloced (%d frames):", frag, block ,e.used);
81 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
82 XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
89 void *s_heap, *heapbase1, *heapbase2;
90 malloc_info *heapinfo1, *heapinfo2;
91 size_t heaplimit, heapsize1, heapsize2;
95 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stack1, xbt_dynar_t *stack2, xbt_dynar_t *equals){
97 if(heap1 == NULL && heap1 == NULL){
98 XBT_DEBUG("Malloc descriptors null");
102 if(heap1->heaplimit != heap2->heaplimit){
103 XBT_DEBUG("Different limit of valid info table indices");
107 /* Heap information */
108 heaplimit = ((struct mdesc *)heap1)->heaplimit;
110 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
112 heapbase1 = (char *)heap1 + BLOCKSIZE;
113 heapbase2 = (char *)heap2 + BLOCKSIZE;
115 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
116 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
118 heapsize1 = heap1->heapsize;
119 heapsize2 = heap2->heapsize;
121 /* Start comparison */
122 size_t i1, i2, j1, j2, k, current_block, current_fragment;
123 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
124 void *real_addr_block1, *real_addr_block2;
126 int nb_block1=0, nb_frag1=0, nb_block2=0, nb_frag2=0;
128 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
130 int equal, res_compare;
134 /* Init equal information */
137 while(i1<=heaplimit){
138 if(heapinfo1[i1].type == 0){
139 if(heapinfo1[i1].busy_block.busy_size > 0)
141 heapinfo1[i1].busy_block.equal_to = NULL;
143 if(heapinfo1[i1].type > 0){
144 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
145 if(heapinfo1[i1].busy_frag.frag_size[j1] > 0)
147 heapinfo1[i1].busy_frag.equal_to[j1] = NULL;
155 while(i2<=heaplimit){
156 if(heapinfo2[i2].type == 0){
157 if(heapinfo2[i2].busy_block.busy_size > 0)
159 heapinfo2[i2].busy_block.equal_to = NULL;
161 if(heapinfo2[i2].type > 0){
162 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
163 if(heapinfo2[i2].busy_frag.frag_size[j2] > 0)
165 heapinfo2[i2].busy_frag.equal_to[j2] = NULL;
171 if(nb_block1 != nb_block2 || nb_frag1 != nb_frag2){
172 XBT_DEBUG("Different number of busy blocks (%d - %d) or busy fragments (%d - %d)", nb_block1, nb_block2, nb_frag1, nb_frag2);
176 /* Check busy blocks*/
180 while(i1 <= heaplimit){
184 if(heapinfo1[i1].type == -1){ /* Free block */
189 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
190 real_addr_block1 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block1) - (char *)heapbase1);
192 if(heapinfo1[i1].type == 0){ /* Large block */
194 if((stack_name = is_stack(real_addr_block1)) != NULL){
195 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
196 stack->address = addr_block1;
197 stack->process_name = strdup(stack_name);
198 stack->size = heapinfo1[i1].busy_block.busy_size;
199 xbt_dynar_push(*stack1, &stack);
202 if(heapinfo1[i1].busy_block.busy_size == 0){
207 if(heapinfo1[i1].busy_block.equal_to != NULL){
215 /* Try first to associate to same block in the other heap */
216 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
218 if(heapinfo2[current_block].busy_block.equal_to == NULL){
220 if(heapinfo1[current_block].busy_block.busy_size == heapinfo2[current_block].busy_block.busy_size){
222 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
223 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
225 if((stack_name = is_stack(real_addr_block2)) != NULL){
226 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
227 stack->address = addr_block2;
228 stack->process_name = strdup(stack_name);
229 stack->size = heapinfo2[current_block].busy_block.busy_size;
230 xbt_dynar_push(*stack2, &stack);
233 add_heap_area_pair(previous, current_block, -1, current_block, -1);
235 if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
236 if(in_mc_comparison_ignore((int)current_block, -1))
237 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 1);
239 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
241 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
244 if(res_compare == 0){
245 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
246 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
247 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
248 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
250 match_equals(previous, equals);
251 i1 = i1 + heapinfo1[current_block].busy_block.size;
254 xbt_dynar_reset(previous);
262 while(i2 <= heaplimit && !equal){
264 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
265 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
267 if((stack_name = is_stack(real_addr_block2)) != NULL){
268 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
269 stack->address = addr_block2;
270 stack->process_name = strdup(stack_name);
271 stack->size = heapinfo2[i2].busy_block.busy_size;
272 xbt_dynar_push(*stack2, &stack);
275 if(i2 == current_block){
280 if(heapinfo2[i2].type != 0){
285 if(heapinfo2[i2].busy_block.equal_to != NULL){
290 if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
295 if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
301 add_heap_area_pair(previous, i1, -1, i2, -1);
303 if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
304 if(in_mc_comparison_ignore((int)i1, -1))
305 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 1);
307 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
309 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
312 if(res_compare == 0){
313 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
314 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
315 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
316 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
318 match_equals(previous, equals);
319 i1 = i1 + heapinfo1[i1].busy_block.size;
322 xbt_dynar_reset(previous);
331 }else{ /* Fragmented block */
333 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
335 current_fragment = j1;
337 if(heapinfo1[i1].busy_frag.frag_size[j1] == 0) /* Free fragment */
340 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
343 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
348 /* Try first to associate to same fragment in the other heap */
349 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
351 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
353 if(heapinfo1[current_block].busy_frag.frag_size[current_fragment] == heapinfo2[current_block].busy_frag.frag_size[current_fragment]){
355 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
356 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << heapinfo2[current_block].type));
358 add_heap_area_pair(previous, current_block, current_fragment, current_block, current_fragment);
360 if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
361 if(in_mc_comparison_ignore((int)current_block, (int)current_fragment))
362 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 1);
364 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
366 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
369 if(res_compare == 0){
371 match_equals(previous, equals);
374 xbt_dynar_reset(previous);
382 while(i2 <= heaplimit && !equal){
385 if(heapinfo2[i2].type <= 0){
390 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
392 if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
395 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
398 if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]) /* Different size_used */
401 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
402 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << heapinfo2[i2].type));
405 add_heap_area_pair(previous, i1, j1, i2, j2);
407 if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
408 if(in_mc_comparison_ignore((int)i1, (int)j1))
409 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 1);
411 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
413 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
416 if(res_compare == 0){
418 match_equals(previous, equals);
422 xbt_dynar_reset(previous);
438 /* All blocks/fragments are equal to another block/fragment ? */
440 int nb_diff1 = 0, nb_diff2 = 0;
443 if(heapinfo1[i].type == 0){
444 if(heapinfo1[i].busy_block.busy_size > 0){
445 if(heapinfo1[i].busy_block.equal_to == NULL){
446 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
447 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
448 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
449 mmalloc_backtrace_block_display((void*)heapinfo1, i);
455 if(heapinfo1[i].type > 0){
456 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
457 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
458 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
459 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
460 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
461 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
462 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
463 mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
474 XBT_DEBUG("Different blocks or fragments in heap1 : %d", nb_diff1);
479 if(heapinfo2[i].type == 0){
480 if(heapinfo2[i].busy_block.busy_size > 0){
481 if(heapinfo2[i].busy_block.equal_to == NULL){
482 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
483 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
484 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
485 mmalloc_backtrace_block_display((void*)heapinfo2, i);
491 if(heapinfo2[i].type > 0){
492 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
493 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
494 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
495 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
496 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
497 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
498 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
499 mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
509 XBT_DEBUG("Different blocks or fragments in heap2 : %d", nb_diff2);
511 xbt_dynar_free(&previous);
513 return ((nb_diff1 > 0) || (nb_diff2 > 0));
517 static heap_area_t new_heap_area(int block, int fragment){
518 heap_area_t area = NULL;
519 area = xbt_new0(s_heap_area_t, 1);
521 area->fragment = fragment;
525 static int in_mc_comparison_ignore(int block, int fragment){
527 unsigned int cursor = 0;
529 int end = xbt_dynar_length(mc_comparison_ignore) - 1;
530 mc_ignore_region_t region;
533 cursor = (start + end) / 2;
534 region = (mc_ignore_region_t)xbt_dynar_get_as(mc_comparison_ignore, cursor, mc_ignore_region_t);
535 if(region->block == block){
536 if(region->fragment == fragment)
538 if(region->fragment < fragment)
540 if(region->fragment > fragment)
543 if(region->block < block)
545 if(region->block > block)
552 static size_t heap_comparison_ignore(void *address){
553 unsigned int cursor = 0;
555 int end = xbt_dynar_length(mc_comparison_ignore) - 1;
556 mc_ignore_region_t region;
559 cursor = (start + end) / 2;
560 region = (mc_ignore_region_t)xbt_dynar_get_as(mc_comparison_ignore, cursor, mc_ignore_region_t);
561 if(region->address == address)
563 if(region->address < address)
565 if(region->address > address)
573 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore){
575 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
576 void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
577 size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
579 void *current_area1, *current_area2;
585 current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
586 if((ignore1 = heap_comparison_ignore(current_area1)) > 0){
587 current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
588 if((ignore2 = heap_comparison_ignore(current_area2)) == ignore1){
597 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
599 /* Check pointer difference */
600 pointer_align = (i / sizeof(void*)) * sizeof(void*);
601 address_pointed1 = *((void **)((char *)area1 + pointer_align));
602 address_pointed2 = *((void **)((char *)area2 + pointer_align));
604 /* Get pointed blocks number */
605 block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
606 block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
608 /* Check if valid blocks number */
609 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)
612 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
614 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
615 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
617 if(heapinfo1[block_pointed1].type == 0){ /* Large block */
619 if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
623 if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
627 if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
629 if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
630 if(in_mc_comparison_ignore(block_pointed1, -1))
631 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 1);
633 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
635 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
643 }else{ /* Fragmented block */
645 /* Get pointed fragments number */
646 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
647 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
649 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
652 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
653 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
655 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
657 if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
658 if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
659 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
661 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
663 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
675 if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
677 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
678 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
680 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
681 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
683 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
686 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
687 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
689 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
691 if(ignore_done < xbt_dynar_length(mc_comparison_ignore)){
692 if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
693 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
695 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
697 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
711 i = pointer_align + sizeof(void *);
725 static void heap_area_pair_free(heap_area_pair_t pair){
732 static void heap_area_pair_free_voidp(void *d)
734 heap_area_pair_free((heap_area_pair_t) * (void **) d);
737 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
739 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
740 heap_area_pair_t pair = NULL;
741 pair = xbt_new0(s_heap_area_pair_t, 1);
742 pair->block1 = block1;
743 pair->fragment1 = fragment1;
744 pair->block2 = block2;
745 pair->fragment2 = fragment2;
747 xbt_dynar_push(list, &pair);
755 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
757 unsigned int cursor = 0;
758 heap_area_pair_t current_pair;
760 xbt_dynar_foreach(list, cursor, current_pair){
761 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
768 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals){
770 unsigned int cursor = 0;
771 heap_area_pair_t current_pair;
772 heap_area_t previous_area;
774 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
776 xbt_dynar_foreach(list, cursor, current_pair){
778 if(current_pair->fragment1 != -1){
780 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
781 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (current_pair->fragment1 << heapinfo1[current_pair->block1].type));
782 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
783 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (current_pair->fragment2 << heapinfo2[current_pair->block2].type));
785 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
786 remove_heap_equality(equals, 1, real_addr_frag1);
787 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
788 xbt_free( heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
789 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
790 xbt_free(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1]);
792 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
793 remove_heap_equality(equals, 2, real_addr_frag2);
794 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
795 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
796 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
797 xbt_free(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2]);
800 if(real_addr_frag1 != real_addr_frag2)
801 add_heap_equality(equals, real_addr_frag1, real_addr_frag2);
803 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
804 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
808 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
809 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
811 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
812 remove_heap_equality(equals, 1, real_addr_block1);
813 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
814 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
815 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
816 xbt_free(heapinfo1[current_pair->block1].busy_block.equal_to);
818 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
819 remove_heap_equality(equals, 2, real_addr_block2);
820 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
821 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
822 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
823 xbt_free(heapinfo2[current_pair->block2].busy_block.equal_to);
826 if(real_addr_block1 != real_addr_block2)
827 add_heap_equality(equals, real_addr_block1, real_addr_block2);
829 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
830 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
839 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
842 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
844 if(heap1 == NULL && heap1 == NULL){
845 XBT_DEBUG("Malloc descriptors null");
849 if(heap1->heaplimit != heap2->heaplimit){
850 XBT_DEBUG("Different limit of valid info table indices");
854 /* Heap information */
855 heaplimit = ((struct mdesc *)heap1)->heaplimit;
857 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
859 heapbase1 = (char *)heap1 + BLOCKSIZE;
860 heapbase2 = (char *)heap2 + BLOCKSIZE;
862 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
863 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
865 heapsize1 = heap1->heapsize;
866 heapsize2 = heap2->heapsize;
868 /* Start comparison */
870 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
874 /* Check busy blocks*/
878 while(i <= heaplimit){
880 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
881 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
883 if(heapinfo1[i].type != heapinfo2[i].type){
885 distance += BLOCKSIZE;
886 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
891 if(heapinfo1[i].type == -1){ /* Free block */
896 if(heapinfo1[i].type == 0){ /* Large block */
898 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
899 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
900 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
901 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);
905 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
906 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
907 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
908 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);
914 //while(k < (heapinfo1[i].busy_block.busy_size)){
915 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
916 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
924 }else { /* Fragmented block */
926 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
928 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
929 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
931 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
936 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
937 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
938 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);
944 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
945 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
946 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
966 static char * is_stack(void *address){
967 unsigned int cursor = 0;
968 stack_region_t stack;
970 xbt_dynar_foreach(stacks_areas, cursor, stack){
971 if(address == stack->address)
972 return stack->process_name;
978 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2){
980 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
984 if(xbt_dynar_is_empty(*equals)){
986 xbt_dynar_insert_at(*equals, 0, &he);
990 unsigned int cursor = 0;
992 int end = xbt_dynar_length(*equals) - 1;
993 heap_equality_t current_equality = NULL;
996 cursor = (start + end) / 2;
997 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
998 if(current_equality->address1 == a1){
999 if(current_equality->address2 == a2)
1001 if(current_equality->address2 < a2)
1003 if(current_equality->address2 > a2)
1006 if(current_equality->address1 < a1)
1008 if(current_equality->address1 > a1)
1012 if(current_equality->address1 < a1)
1013 xbt_dynar_insert_at(*equals, cursor + 1 , &he);
1015 xbt_dynar_insert_at(*equals, cursor, &he);
1021 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a){
1023 unsigned int cursor = 0;
1024 heap_equality_t current_equality;
1030 int end = xbt_dynar_length(*equals) - 1;
1033 while(start <= end && found == 0){
1034 cursor = (start + end) / 2;
1035 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1036 if(current_equality->address1 == a)
1038 if(current_equality->address1 < a)
1040 if(current_equality->address1 > a)
1045 xbt_dynar_remove_at(*equals, cursor, NULL);
1049 xbt_dynar_foreach(*equals, cursor, current_equality){
1050 if(current_equality->address2 == a){
1057 xbt_dynar_remove_at(*equals, cursor, NULL);
1064 int is_free_area(void *area, xbt_mheap_t heap){
1066 void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1067 malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1068 size_t heapsize = heap->heapsize;
1070 /* Get block number */
1071 size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1074 /* Check if valid block number */
1075 if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1078 if(heapinfo[block].type < 0)
1081 if(heapinfo[block].type == 0)
1084 if(heapinfo[block].type > 0){
1085 fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1086 if(heapinfo[block].busy_frag.frag_size[fragment] == 0)