Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Avoid loosing meaningful bits ot pair.p2 when hashing pointer pair
[simgrid.git] / src / mc / mc_ignore.c
1 /* Copyright (c) 2008-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "mc_private.h"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ignore, mc,
10                                 "Logging specific to MC ignore mechanism");
11
12
13 /**************************** Global variables ******************************/
14 xbt_dynar_t mc_checkpoint_ignore;
15 extern xbt_dynar_t mc_heap_comparison_ignore;
16 extern xbt_dynar_t stacks_areas;
17
18 /**************************** Structures ******************************/
19 typedef struct s_mc_stack_ignore_variable {
20   char *var_name;
21   char *frame;
22 } s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
23
24 /**************************** Free functions ******************************/
25
26 static void stack_ignore_variable_free(mc_stack_ignore_variable_t v)
27 {
28   xbt_free(v->var_name);
29   xbt_free(v->frame);
30   xbt_free(v);
31 }
32
33 static void stack_ignore_variable_free_voidp(void *v)
34 {
35   stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
36 }
37
38 void heap_ignore_region_free(mc_heap_ignore_region_t r)
39 {
40   xbt_free(r);
41 }
42
43 void heap_ignore_region_free_voidp(void *r)
44 {
45   heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
46 }
47
48 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r)
49 {
50   xbt_free(r);
51 }
52
53 static void checkpoint_ignore_region_free_voidp(void *r)
54 {
55   checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
56 }
57
58 /***********************************************************************/
59
60 void MC_ignore_heap(void *address, size_t size)
61 {
62
63   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
64
65   MC_SET_MC_HEAP;
66
67   mc_heap_ignore_region_t region = NULL;
68   region = xbt_new0(s_mc_heap_ignore_region_t, 1);
69   region->address = address;
70   region->size = size;
71
72   region->block =
73       ((char *) address -
74        (char *) ((xbt_mheap_t) std_heap)->heapbase) / BLOCKSIZE + 1;
75
76   if (((xbt_mheap_t) std_heap)->heapinfo[region->block].type == 0) {
77     region->fragment = -1;
78     ((xbt_mheap_t) std_heap)->heapinfo[region->block].busy_block.ignore++;
79   } else {
80     region->fragment =
81         ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> ((xbt_mheap_t)
82                                                              std_heap)->
83         heapinfo[region->block].type;
84     ((xbt_mheap_t) std_heap)->heapinfo[region->block].busy_frag.ignore[region->
85                                                                        fragment]++;
86   }
87
88   if (mc_heap_comparison_ignore == NULL) {
89     mc_heap_comparison_ignore =
90         xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
91                       heap_ignore_region_free_voidp);
92     xbt_dynar_push(mc_heap_comparison_ignore, &region);
93     if (!raw_mem_set)
94       MC_SET_STD_HEAP;
95     return;
96   }
97
98   unsigned int cursor = 0;
99   mc_heap_ignore_region_t current_region = NULL;
100   int start = 0;
101   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
102
103   while (start <= end) {
104     cursor = (start + end) / 2;
105     current_region =
106         (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
107                                                    cursor,
108                                                    mc_heap_ignore_region_t);
109     if (current_region->address == address) {
110       heap_ignore_region_free(region);
111       if (!raw_mem_set)
112         MC_SET_STD_HEAP;
113       return;
114     } else if (current_region->address < address) {
115       start = cursor + 1;
116     } else {
117       end = cursor - 1;
118     }
119   }
120
121   if (current_region->address < address)
122     xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, &region);
123   else
124     xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, &region);
125
126   if (!raw_mem_set)
127     MC_SET_STD_HEAP;
128 }
129
130 void MC_remove_ignore_heap(void *address, size_t size)
131 {
132
133   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
134
135   MC_SET_MC_HEAP;
136
137   unsigned int cursor = 0;
138   int start = 0;
139   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
140   mc_heap_ignore_region_t region;
141   int ignore_found = 0;
142
143   while (start <= end) {
144     cursor = (start + end) / 2;
145     region =
146         (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
147                                                    cursor,
148                                                    mc_heap_ignore_region_t);
149     if (region->address == address) {
150       ignore_found = 1;
151       break;
152     } else if (region->address < address) {
153       start = cursor + 1;
154     } else {
155       if ((char *) region->address <= ((char *) address + size)) {
156         ignore_found = 1;
157         break;
158       } else {
159         end = cursor - 1;
160       }
161     }
162   }
163
164   if (ignore_found == 1) {
165     xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
166     MC_remove_ignore_heap(address, size);
167   }
168
169   if (!raw_mem_set)
170     MC_SET_STD_HEAP;
171
172 }
173
174 void MC_ignore_global_variable(const char *name)
175 {
176
177   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
178
179   MC_SET_MC_HEAP;
180
181   xbt_assert(mc_libsimgrid_info, "MC subsystem not initialized");
182
183   unsigned int cursor = 0;
184   dw_variable_t current_var;
185   int start = 0;
186   int end = xbt_dynar_length(mc_libsimgrid_info->global_variables) - 1;
187
188   while (start <= end) {
189     cursor = (start + end) / 2;
190     current_var =
191         (dw_variable_t) xbt_dynar_get_as(mc_libsimgrid_info->global_variables,
192                                          cursor, dw_variable_t);
193     if (strcmp(current_var->name, name) == 0) {
194       xbt_dynar_remove_at(mc_libsimgrid_info->global_variables, cursor, NULL);
195       start = 0;
196       end = xbt_dynar_length(mc_libsimgrid_info->global_variables) - 1;
197     } else if (strcmp(current_var->name, name) < 0) {
198       start = cursor + 1;
199     } else {
200       end = cursor - 1;
201     }
202   }
203
204   if (!raw_mem_set)
205     MC_SET_STD_HEAP;
206 }
207
208 /** \brief Ignore a local variable in a scope
209  *
210  *  Ignore all instances of variables with a given name in
211  *  any (possibly inlined) subprogram with a given namespaced
212  *  name.
213  *
214  *  \param var_name        Name of the local variable (or parameter to ignore)
215  *  \param subprogram_name Name of the subprogram fo ignore (NULL for any)
216  *  \param subprogram      (possibly inlined) Subprogram of the scope
217  *  \param scope           Current scope
218  */
219 static void mc_ignore_local_variable_in_scope(const char *var_name,
220                                               const char *subprogram_name,
221                                               dw_frame_t subprogram,
222                                               dw_frame_t scope)
223 {
224   // Processing of direct variables:
225
226   // If the current subprogram matche the given name:
227   if (subprogram_name == NULL || strcmp(subprogram_name, subprogram->name) == 0) {
228
229     // Try to find the variable and remove it:
230     int start = 0;
231     int end = xbt_dynar_length(scope->variables) - 1;
232
233     // Dichotomic search:
234     while (start <= end) {
235       int cursor = (start + end) / 2;
236       dw_variable_t current_var =
237           (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
238                                            dw_variable_t);
239
240       int compare = strcmp(current_var->name, var_name);
241       if (compare == 0) {
242         // Variable found, remove it:
243         xbt_dynar_remove_at(scope->variables, cursor, NULL);
244
245         // and start again:
246         start = 0;
247         end = xbt_dynar_length(scope->variables) - 1;
248       } else if (compare < 0) {
249         start = cursor + 1;
250       } else {
251         end = cursor - 1;
252       }
253     }
254
255   }
256   // And recursive processing in nested scopes:
257   unsigned cursor = 0;
258   dw_frame_t nested_scope = NULL;
259   xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
260     // The new scope may be an inlined subroutine, in this case we want to use its
261     // namespaced name in recursive calls:
262     dw_frame_t nested_subprogram =
263         nested_scope->tag ==
264         DW_TAG_inlined_subroutine ? nested_scope : subprogram;
265
266     mc_ignore_local_variable_in_scope(var_name, subprogram_name,
267                                       nested_subprogram, nested_scope);
268   }
269 }
270
271 static void MC_ignore_local_variable_in_object(const char *var_name,
272                                                const char *subprogram_name,
273                                                mc_object_info_t info)
274 {
275   xbt_dict_cursor_t cursor2;
276   dw_frame_t frame;
277   char *key;
278   xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
279     mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
280   }
281 }
282
283 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
284 {
285
286   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
287
288   if (strcmp(frame_name, "*") == 0)
289     frame_name = NULL;
290
291   MC_SET_MC_HEAP;
292
293   MC_ignore_local_variable_in_object(var_name, frame_name, mc_libsimgrid_info);
294   if (frame_name != NULL)
295     MC_ignore_local_variable_in_object(var_name, frame_name, mc_binary_info);
296
297   if (!raw_mem_set)
298     MC_SET_STD_HEAP;
299
300 }
301
302 void MC_new_stack_area(void *stack, char *name, void *context, size_t size)
303 {
304
305   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
306
307   MC_SET_MC_HEAP;
308
309   if (stacks_areas == NULL)
310     stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
311
312   stack_region_t region = NULL;
313   region = xbt_new0(s_stack_region_t, 1);
314   region->address = stack;
315   region->process_name = strdup(name);
316   region->context = context;
317   region->size = size;
318   region->block =
319       ((char *) stack -
320        (char *) ((xbt_mheap_t) std_heap)->heapbase) / BLOCKSIZE + 1;
321   xbt_dynar_push(stacks_areas, &region);
322
323   if (!raw_mem_set)
324     MC_SET_STD_HEAP;
325 }
326
327 void MC_ignore(void *addr, size_t size)
328 {
329
330   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
331
332   MC_SET_MC_HEAP;
333
334   if (mc_checkpoint_ignore == NULL)
335     mc_checkpoint_ignore =
336         xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
337                       checkpoint_ignore_region_free_voidp);
338
339   mc_checkpoint_ignore_region_t region =
340       xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
341   region->addr = addr;
342   region->size = size;
343
344   if (xbt_dynar_is_empty(mc_checkpoint_ignore)) {
345     xbt_dynar_push(mc_checkpoint_ignore, &region);
346   } else {
347
348     unsigned int cursor = 0;
349     int start = 0;
350     int end = xbt_dynar_length(mc_checkpoint_ignore) - 1;
351     mc_checkpoint_ignore_region_t current_region = NULL;
352
353     while (start <= end) {
354       cursor = (start + end) / 2;
355       current_region =
356           (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(mc_checkpoint_ignore,
357                                                            cursor,
358                                                            mc_checkpoint_ignore_region_t);
359       if (current_region->addr == addr) {
360         if (current_region->size == size) {
361           checkpoint_ignore_region_free(region);
362           if (!raw_mem_set)
363             MC_SET_STD_HEAP;
364           return;
365         } else if (current_region->size < size) {
366           start = cursor + 1;
367         } else {
368           end = cursor - 1;
369         }
370       } else if (current_region->addr < addr) {
371         start = cursor + 1;
372       } else {
373         end = cursor - 1;
374       }
375     }
376
377     if (current_region->addr == addr) {
378       if (current_region->size < size) {
379         xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, &region);
380       } else {
381         xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, &region);
382       }
383     } else if (current_region->addr < addr) {
384       xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, &region);
385     } else {
386       xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, &region);
387     }
388   }
389
390   if (!raw_mem_set)
391     MC_SET_STD_HEAP;
392 }