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 xbt_dynar_t mc_heap_comparison_ignore;
17 xbt_dynar_t stacks_areas;
19 static void heap_area_pair_free(heap_area_pair_t pair);
20 static void heap_area_pair_free_voidp(void *d);
21 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
22 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
23 static heap_area_t new_heap_area(int block, int fragment);
25 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore);
26 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals);
28 static int in_mc_comparison_ignore(int block, int fragment);
29 static size_t heap_comparison_ignore_size(void *address);
30 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2);
31 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a);
33 static char* is_stack(void *address);
35 void mmalloc_backtrace_block_display(void* heapinfo, int block){
39 /* if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) { */
40 /* fprintf(stderr, "No backtrace available for that block, sorry.\n"); */
44 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE); */
45 /* e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size; */
47 /* xbt_ex_setup_backtrace(&e); */
48 /* if (e.used == 0) { */
49 /* fprintf(stderr, "(backtrace not set)\n"); */
50 /* } else if (e.bt_strings == NULL) { */
51 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
55 /* fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used); */
56 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
57 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
62 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
66 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE); */
67 /* e.used = XBT_BACKTRACE_SIZE; */
69 /* xbt_ex_setup_backtrace(&e); */
70 /* if (e.used == 0) { */
71 /* fprintf(stderr, "(backtrace not set)\n"); */
72 /* } else if (e.bt_strings == NULL) { */
73 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
77 /* fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used); */
78 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
79 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
85 void mmalloc_backtrace_display(void *addr){
87 /* size_t block, frag_nb; */
90 /* xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); */
92 /* block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1); */
94 /* type = heap->heapinfo[block].type; */
97 /* case -1 : /\* Free block *\/ */
98 /* fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n"); */
101 /* case 0: /\* Large block *\/ */
102 /* mmalloc_backtrace_block_display(heap->heapinfo, block); */
104 /* default: /\* Fragmented block *\/ */
105 /* frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type; */
106 /* if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ */
107 /* fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n"); */
110 /* mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb); */
116 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
117 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
118 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
122 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stack1, xbt_dynar_t *stack2, xbt_dynar_t *equals){
124 if(heap1 == NULL && heap1 == NULL){
125 XBT_DEBUG("Malloc descriptors null");
129 if(heap1->heaplimit != heap2->heaplimit){
130 XBT_DEBUG("Different limit of valid info table indices");
134 /* Heap information */
135 heaplimit = ((struct mdesc *)heap1)->heaplimit;
137 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
139 heapbase1 = (char *)heap1 + BLOCKSIZE;
140 heapbase2 = (char *)heap2 + BLOCKSIZE;
142 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
143 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
145 heapsize1 = heap1->heapsize;
146 heapsize2 = heap2->heapsize;
148 /* Start comparison */
149 size_t i1, i2, j1, j2, k, current_block, current_fragment;
150 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
151 void *real_addr_block1, *real_addr_block2;
153 int nb_diff1 = 0, nb_diff2 = 0;
155 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
157 int equal, res_compare = 0;
159 /* Check busy blocks*/
163 while(i1 <= heaplimit){
167 if(heapinfo1[i1].type == -1){ /* Free block */
172 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
173 real_addr_block1 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block1) - (char *)heapbase1);
175 if(heapinfo1[i1].type == 0){ /* Large block */
177 if((xbt_dynar_length(*stack1) < xbt_dynar_length(stacks_areas)) && ((stack_name = is_stack(real_addr_block1)) != NULL)){
178 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
179 stack->address = addr_block1;
180 stack->process_name = strdup(stack_name);
181 stack->size = heapinfo1[i1].busy_block.busy_size;
182 xbt_dynar_push(*stack1, &stack);
186 if(heapinfo1[i1].busy_block.busy_size == 0){
191 if(heapinfo1[i1].busy_block.equal_to != NULL){
200 /* Try first to associate to same block in the other heap */
201 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
203 if(heapinfo2[current_block].busy_block.equal_to == NULL){
205 if(heapinfo1[current_block].busy_block.busy_size == heapinfo2[current_block].busy_block.busy_size){
207 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
208 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
210 if((xbt_dynar_length(*stack2) < xbt_dynar_length(stacks_areas)) && ((stack_name = is_stack(real_addr_block2)) != NULL)){
211 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
212 stack->address = addr_block2;
213 stack->process_name = strdup(stack_name);
214 stack->size = heapinfo2[current_block].busy_block.busy_size;
215 xbt_dynar_push(*stack2, &stack);
219 add_heap_area_pair(previous, current_block, -1, current_block, -1);
221 if(res_compare != -1){
222 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && heapinfo1[current_block].busy_block.ignore == 1){
223 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 1);
225 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
229 if(res_compare == 0 || res_compare == -1){
230 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
231 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
232 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
233 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
235 match_equals(previous, equals);
236 i1 = i1 + heapinfo1[current_block].busy_block.size;
239 xbt_dynar_reset(previous);
247 while(i2 <= heaplimit && !equal){
249 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
250 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
252 if((xbt_dynar_length(*stack2) < xbt_dynar_length(stacks_areas)) && ((stack_name = is_stack(real_addr_block2)) != NULL)){
253 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
254 stack->address = addr_block2;
255 stack->process_name = strdup(stack_name);
256 stack->size = heapinfo2[i2].busy_block.busy_size;
257 xbt_dynar_push(*stack2, &stack);
261 if(i2 == current_block){
266 if(heapinfo2[i2].type != 0){
271 if(heapinfo2[i2].busy_block.equal_to != NULL){
276 if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
281 if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
287 add_heap_area_pair(previous, i1, -1, i2, -1);
289 if(res_compare != -1){
290 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && heapinfo1[i1].busy_block.ignore == 1){
291 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 1);
293 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
297 if(res_compare == 0 || res_compare == -1){
298 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
299 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
300 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
301 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
303 match_equals(previous, equals);
304 i1 = i1 + heapinfo1[i1].busy_block.size;
307 xbt_dynar_reset(previous);
314 XBT_DEBUG("Block %zu not found (size_used = %zu)", i1, heapinfo1[i1].busy_block.busy_size);
319 }else{ /* Fragmented block */
321 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
323 current_fragment = j1;
325 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
328 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
331 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
336 /* Try first to associate to same fragment in the other heap */
337 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
339 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
341 if(heapinfo1[current_block].busy_frag.frag_size[current_fragment] == heapinfo2[current_block].busy_frag.frag_size[current_fragment]){
343 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
344 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << heapinfo2[current_block].type));
346 add_heap_area_pair(previous, current_block, current_fragment, current_block, current_fragment);
348 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && (heapinfo1[current_block].busy_frag.ignore[current_fragment] == 1)){
349 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 1);
351 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
354 if(res_compare == 0){
356 match_equals(previous, equals);
359 xbt_dynar_reset(previous);
366 while(i2 <= heaplimit && !equal){
369 if(heapinfo2[i2].type <= 0){
374 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
376 if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
379 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
382 if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]) /* Different size_used */
385 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
386 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << heapinfo2[i2].type));
389 add_heap_area_pair(previous, i1, j1, i2, j2);
391 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && (heapinfo1[i1].busy_frag.ignore[j1] == 1)){
392 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 1);
394 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
397 if(res_compare == 0){
399 match_equals(previous, equals);
400 xbt_dynar_reset(previous);
404 xbt_dynar_reset(previous);
412 if(heapinfo1[i1].busy_frag.equal_to[j1] == NULL){
413 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %d)", i1, j1, heapinfo1[i1].busy_frag.frag_size[j1]);
428 /* All blocks/fragments are equal to another block/fragment ? */
432 if(heapinfo1[i].type == 0){
433 if(current_block == heaplimit){
434 if(heapinfo1[i].busy_block.busy_size > 0){
435 if(heapinfo1[i].busy_block.equal_to == NULL){
436 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
437 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
438 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
439 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
445 xbt_free(heapinfo1[i].busy_block.equal_to);
446 heapinfo1[i].busy_block.equal_to = NULL;
448 if(heapinfo1[i].type > 0){
449 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
450 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
451 if(current_block == heaplimit){
452 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
453 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
454 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
455 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
456 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
457 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
463 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
464 heapinfo1[i].busy_frag.equal_to[j] = NULL;
473 if(heapinfo2[i].type == 0){
474 if(current_block == heaplimit){
475 if(heapinfo2[i].busy_block.busy_size > 0){
476 if(heapinfo2[i].busy_block.equal_to == NULL){
477 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
478 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
479 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
480 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
486 xbt_free(heapinfo2[i].busy_block.equal_to);
487 heapinfo2[i].busy_block.equal_to = NULL;
489 if(heapinfo2[i].type > 0){
490 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
491 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
492 if(current_block == heaplimit){
493 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
494 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
495 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
496 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
497 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
498 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
504 xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
505 heapinfo2[i].busy_frag.equal_to[j] = NULL;
511 xbt_dynar_free(&previous);
513 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
514 heapinfo1 = NULL, heapinfo2 = NULL;
515 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
517 return ((nb_diff1 > 0) || (nb_diff2 > 0));
520 static heap_area_t new_heap_area(int block, int fragment){
521 heap_area_t area = NULL;
522 area = xbt_new0(s_heap_area_t, 1);
524 area->fragment = fragment;
528 static int in_mc_comparison_ignore(int block, int fragment){
530 unsigned int cursor = 0;
532 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
533 mc_heap_ignore_region_t region;
536 cursor = (start + end) / 2;
537 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
538 if(region->block == block){
539 if(region->fragment == fragment)
541 if(region->fragment < fragment)
543 if(region->fragment > fragment)
546 if(region->block < block)
548 if(region->block > block)
555 static size_t heap_comparison_ignore_size(void *address){
556 unsigned int cursor = 0;
558 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
559 mc_heap_ignore_region_t region;
562 cursor = (start + end) / 2;
563 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
564 if(region->address == address)
566 if(region->address < address)
568 if(region->address > address)
576 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore){
578 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
579 void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
580 size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
582 void *current_area1, *current_area2;
588 current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
589 if((ignore1 = heap_comparison_ignore_size(current_area1)) > 0){
590 current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
591 if((ignore2 = heap_comparison_ignore_size(current_area2)) == ignore1){
600 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
602 /* Check pointer difference */
603 pointer_align = (i / sizeof(void*)) * sizeof(void*);
604 address_pointed1 = *((void **)((char *)area1 + pointer_align));
605 address_pointed2 = *((void **)((char *)area2 + pointer_align));
607 /* Get pointed blocks number */
608 block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
609 block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
611 /* Check if valid blocks number */
612 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)
615 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
617 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
618 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
620 if(heapinfo1[block_pointed1].type == 0){ /* Large block */
622 if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
626 if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
630 if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
632 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && (heapinfo1[block_pointed1].busy_block.ignore == 1)){
633 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 1);
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_heap_comparison_ignore)) && (heapinfo1[block_pointed1].busy_frag.ignore[frag_pointed1] == 1)){
658 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
660 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
672 if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
674 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
675 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
677 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
678 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
680 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
683 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
684 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
686 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
688 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && (heapinfo1[block_pointed1].busy_frag.ignore[frag_pointed1] == 1)){
689 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
691 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
705 i = pointer_align + sizeof(void *);
719 static void heap_area_pair_free(heap_area_pair_t pair){
726 static void heap_area_pair_free_voidp(void *d)
728 heap_area_pair_free((heap_area_pair_t) * (void **) d);
731 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
733 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
734 heap_area_pair_t pair = NULL;
735 pair = xbt_new0(s_heap_area_pair_t, 1);
736 pair->block1 = block1;
737 pair->fragment1 = fragment1;
738 pair->block2 = block2;
739 pair->fragment2 = fragment2;
741 xbt_dynar_push(list, &pair);
749 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
751 unsigned int cursor = 0;
752 heap_area_pair_t current_pair;
754 xbt_dynar_foreach(list, cursor, current_pair){
755 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
762 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals){
764 unsigned int cursor = 0;
765 heap_area_pair_t current_pair;
766 heap_area_t previous_area;
768 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
770 xbt_dynar_foreach(list, cursor, current_pair){
772 if(current_pair->fragment1 != -1){
774 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
775 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (current_pair->fragment1 << heapinfo1[current_pair->block1].type));
776 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
777 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (current_pair->fragment2 << heapinfo2[current_pair->block2].type));
779 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
780 remove_heap_equality(equals, 1, real_addr_frag1);
781 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
782 xbt_free(heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
783 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
784 xbt_free(previous_area);
786 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
787 remove_heap_equality(equals, 2, real_addr_frag2);
788 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
789 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
790 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
791 xbt_free(previous_area);
794 if(real_addr_frag1 != real_addr_frag2)
795 add_heap_equality(equals, real_addr_frag1, real_addr_frag2);
797 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
798 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
802 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
803 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
805 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
806 remove_heap_equality(equals, 1, real_addr_block1);
807 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
808 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
809 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
810 xbt_free(previous_area);
812 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
813 remove_heap_equality(equals, 2, real_addr_block2);
814 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
815 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
816 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
817 xbt_free(previous_area);
820 if(real_addr_block1 != real_addr_block2)
821 add_heap_equality(equals, real_addr_block1, real_addr_block2);
823 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
824 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
833 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
836 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
838 if(heap1 == NULL && heap1 == NULL){
839 XBT_DEBUG("Malloc descriptors null");
843 if(heap1->heaplimit != heap2->heaplimit){
844 XBT_DEBUG("Different limit of valid info table indices");
848 /* Heap information */
849 heaplimit = ((struct mdesc *)heap1)->heaplimit;
851 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
853 heapbase1 = (char *)heap1 + BLOCKSIZE;
854 heapbase2 = (char *)heap2 + BLOCKSIZE;
856 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
857 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
859 heapsize1 = heap1->heapsize;
860 heapsize2 = heap2->heapsize;
862 /* Start comparison */
864 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
868 /* Check busy blocks*/
872 while(i <= heaplimit){
874 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
875 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
877 if(heapinfo1[i].type != heapinfo2[i].type){
879 distance += BLOCKSIZE;
880 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
885 if(heapinfo1[i].type == -1){ /* Free block */
890 if(heapinfo1[i].type == 0){ /* Large block */
892 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
893 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
894 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
895 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);
899 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
900 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
901 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
902 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);
908 //while(k < (heapinfo1[i].busy_block.busy_size)){
909 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
910 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
918 }else { /* Fragmented block */
920 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
922 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
923 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
925 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
930 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
931 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
932 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);
938 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
939 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
940 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
960 static char* is_stack(void *address){
961 unsigned int cursor = 0;
962 stack_region_t stack;
964 xbt_dynar_foreach(stacks_areas, cursor, stack){
965 if(address == stack->address)
966 return stack->process_name;
972 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2){
974 if(xbt_dynar_is_empty(*equals)){
976 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
980 xbt_dynar_insert_at(*equals, 0, &he);
984 unsigned int cursor = 0;
986 int end = xbt_dynar_length(*equals) - 1;
987 heap_equality_t current_equality = NULL;
990 cursor = (start + end) / 2;
991 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
992 if(current_equality->address1 == a1){
993 if(current_equality->address2 == a2)
995 if(current_equality->address2 < a2)
997 if(current_equality->address2 > a2)
1000 if(current_equality->address1 < a1)
1002 if(current_equality->address1 > a1)
1006 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1010 if(current_equality->address1 < a1)
1011 xbt_dynar_insert_at(*equals, cursor + 1 , &he);
1013 xbt_dynar_insert_at(*equals, cursor, &he);
1019 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a){
1021 unsigned int cursor = 0;
1022 heap_equality_t current_equality;
1028 int end = xbt_dynar_length(*equals) - 1;
1031 while(start <= end && found == 0){
1032 cursor = (start + end) / 2;
1033 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1034 if(current_equality->address1 == a)
1036 if(current_equality->address1 < a)
1038 if(current_equality->address1 > a)
1043 xbt_dynar_remove_at(*equals, cursor, NULL);
1047 xbt_dynar_foreach(*equals, cursor, current_equality){
1048 if(current_equality->address2 == a){
1055 xbt_dynar_remove_at(*equals, cursor, NULL);
1061 int is_free_area(void *area, xbt_mheap_t heap){
1063 void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1064 malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1065 size_t heapsize = heap->heapsize;
1067 /* Get block number */
1068 size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1071 /* Check if valid block number */
1072 if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1075 if(heapinfo[block].type < 0)
1078 if(heapinfo[block].type == 0)
1081 if(heapinfo[block].type > 0){
1082 fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1083 if(heapinfo[block].busy_frag.frag_size[fragment] == 0)