Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
forgot to add this include
[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 "internal_config.h"
8 #include "mc_private.h"
9 #include "smpi/private.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ignore, mc,
12                                 "Logging specific to MC ignore mechanism");
13
14
15 /**************************** Global variables ******************************/
16 xbt_dynar_t mc_checkpoint_ignore;
17 extern xbt_dynar_t mc_heap_comparison_ignore;
18 extern xbt_dynar_t stacks_areas;
19
20 /**************************** Structures ******************************/
21 typedef struct s_mc_stack_ignore_variable {
22   char *var_name;
23   char *frame;
24 } s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
25
26 /**************************** Free functions ******************************/
27
28 static void stack_ignore_variable_free(mc_stack_ignore_variable_t v)
29 {
30   xbt_free(v->var_name);
31   xbt_free(v->frame);
32   xbt_free(v);
33 }
34
35 static void stack_ignore_variable_free_voidp(void *v)
36 {
37   stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
38 }
39
40 void heap_ignore_region_free(mc_heap_ignore_region_t r)
41 {
42   xbt_free(r);
43 }
44
45 void heap_ignore_region_free_voidp(void *r)
46 {
47   heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
48 }
49
50 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r)
51 {
52   xbt_free(r);
53 }
54
55 static void checkpoint_ignore_region_free_voidp(void *r)
56 {
57   checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
58 }
59
60 /***********************************************************************/
61
62 void MC_ignore_heap(void *address, size_t size)
63 {
64
65   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
66
67   MC_SET_MC_HEAP;
68
69   mc_heap_ignore_region_t region = NULL;
70   region = xbt_new0(s_mc_heap_ignore_region_t, 1);
71   region->address = address;
72   region->size = size;
73
74   region->block =
75       ((char *) address -
76        (char *) std_heap->heapbase) / BLOCKSIZE + 1;
77
78   if (std_heap->heapinfo[region->block].type == 0) {
79     region->fragment = -1;
80     std_heap->heapinfo[region->block].busy_block.ignore++;
81   } else {
82     region->fragment =
83         ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
84         heapinfo[region->block].type;
85     std_heap->heapinfo[region->block].busy_frag.ignore[region->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 ||
228       (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
229
230     // Try to find the variable and remove it:
231     int start = 0;
232     int end = xbt_dynar_length(scope->variables) - 1;
233
234     // Dichotomic search:
235     while (start <= end) {
236       int cursor = (start + end) / 2;
237       dw_variable_t current_var =
238           (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
239                                            dw_variable_t);
240
241       int compare = strcmp(current_var->name, var_name);
242       if (compare == 0) {
243         // Variable found, remove it:
244         xbt_dynar_remove_at(scope->variables, cursor, NULL);
245
246         // and start again:
247         start = 0;
248         end = xbt_dynar_length(scope->variables) - 1;
249       } else if (compare < 0) {
250         start = cursor + 1;
251       } else {
252         end = cursor - 1;
253       }
254     }
255
256   }
257   // And recursive processing in nested scopes:
258   unsigned cursor = 0;
259   dw_frame_t nested_scope = NULL;
260   xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
261     // The new scope may be an inlined subroutine, in this case we want to use its
262     // namespaced name in recursive calls:
263     dw_frame_t nested_subprogram =
264         nested_scope->tag ==
265         DW_TAG_inlined_subroutine ? nested_scope : subprogram;
266
267     mc_ignore_local_variable_in_scope(var_name, subprogram_name,
268                                       nested_subprogram, nested_scope);
269   }
270 }
271
272 static void MC_ignore_local_variable_in_object(const char *var_name,
273                                                const char *subprogram_name,
274                                                mc_object_info_t info)
275 {
276   xbt_dict_cursor_t cursor2;
277   dw_frame_t frame;
278   char *key;
279   xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
280     mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
281   }
282 }
283
284 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
285 {
286
287   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
288
289   if (strcmp(frame_name, "*") == 0)
290     frame_name = NULL;
291
292   MC_SET_MC_HEAP;
293
294   MC_ignore_local_variable_in_object(var_name, frame_name, mc_libsimgrid_info);
295   if (frame_name != NULL)
296     MC_ignore_local_variable_in_object(var_name, frame_name, mc_binary_info);
297
298   if (!raw_mem_set)
299     MC_SET_STD_HEAP;
300
301 }
302
303 /** @brief Register a stack in the model checker
304  *
305  *  The stacks are allocated in the heap. The MC handle them especially
306  *  when we analyse/compare the content of theap so it must be told where
307  *  they are with this function.
308  *
309  *  @param stack
310  *  @param process Process owning the stack
311  *  @param context
312  *  @param size    Size of the stack
313  */
314 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
315 {
316
317   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
318
319   MC_SET_MC_HEAP;
320
321   if (stacks_areas == NULL)
322     stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
323
324   stack_region_t region = NULL;
325   region = xbt_new0(s_stack_region_t, 1);
326   region->address = stack;
327   region->process_name = process && process->name ? strdup(process->name) : NULL;
328   region->context = context;
329   region->size = size;
330   region->block =
331       ((char *) stack -
332        (char *) std_heap->heapbase) / BLOCKSIZE + 1;
333 #ifdef HAVE_SMPI
334   if (smpi_privatize_global_variables && process) {
335     region->process_index = smpi_process_index_of_smx_process(process);
336   } else
337 #endif
338   region->process_index = -1;
339
340   xbt_dynar_push(stacks_areas, &region);
341
342   if (!raw_mem_set)
343     MC_SET_STD_HEAP;
344 }
345
346 void MC_ignore(void *addr, size_t size)
347 {
348
349   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
350
351   MC_SET_MC_HEAP;
352
353   if (mc_checkpoint_ignore == NULL)
354     mc_checkpoint_ignore =
355         xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
356                       checkpoint_ignore_region_free_voidp);
357
358   mc_checkpoint_ignore_region_t region =
359       xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
360   region->addr = addr;
361   region->size = size;
362
363   if (xbt_dynar_is_empty(mc_checkpoint_ignore)) {
364     xbt_dynar_push(mc_checkpoint_ignore, &region);
365   } else {
366
367     unsigned int cursor = 0;
368     int start = 0;
369     int end = xbt_dynar_length(mc_checkpoint_ignore) - 1;
370     mc_checkpoint_ignore_region_t current_region = NULL;
371
372     while (start <= end) {
373       cursor = (start + end) / 2;
374       current_region =
375           (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(mc_checkpoint_ignore,
376                                                            cursor,
377                                                            mc_checkpoint_ignore_region_t);
378       if (current_region->addr == addr) {
379         if (current_region->size == size) {
380           checkpoint_ignore_region_free(region);
381           if (!raw_mem_set)
382             MC_SET_STD_HEAP;
383           return;
384         } else if (current_region->size < size) {
385           start = cursor + 1;
386         } else {
387           end = cursor - 1;
388         }
389       } else if (current_region->addr < addr) {
390         start = cursor + 1;
391       } else {
392         end = cursor - 1;
393       }
394     }
395
396     if (current_region->addr == addr) {
397       if (current_region->size < size) {
398         xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, &region);
399       } else {
400         xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, &region);
401       }
402     } else if (current_region->addr < addr) {
403       xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, &region);
404     } else {
405       xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, &region);
406     }
407   }
408
409   if (!raw_mem_set)
410     MC_SET_STD_HEAP;
411 }