Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
480a74b2a3363fd1d0523c1e210ee11d08e4e906
[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 == 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 /** @brief Register a stack in the model checker
303  *
304  *  The stacks are allocated in the heap. The MC handle them especially
305  *  when we analyse/compare the content of theap so it must be told where
306  *  they are with this function.
307  *
308  *  @param stack
309  *  @param process Process owning the stack
310  *  @param context
311  *  @param size    Size of the stack
312  */
313 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
314 {
315
316   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
317
318   MC_SET_MC_HEAP;
319
320   if (stacks_areas == NULL)
321     stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
322
323   stack_region_t region = NULL;
324   region = xbt_new0(s_stack_region_t, 1);
325   region->address = stack;
326   region->process_name = process && process->name ? strdup(process->name) : NULL;
327   region->context = context;
328   region->size = size;
329   region->block =
330       ((char *) stack -
331        (char *) std_heap->heapbase) / BLOCKSIZE + 1;
332 #ifdef HAVE_SMPI
333   if (smpi_privatize_global_variables && process) {
334     region->process_index = smpi_process_index_of_smx_process(process);
335   } else
336 #endif
337   region->process_index = -1;
338
339   xbt_dynar_push(stacks_areas, &region);
340
341   if (!raw_mem_set)
342     MC_SET_STD_HEAP;
343 }
344
345 void MC_ignore(void *addr, size_t size)
346 {
347
348   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
349
350   MC_SET_MC_HEAP;
351
352   if (mc_checkpoint_ignore == NULL)
353     mc_checkpoint_ignore =
354         xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
355                       checkpoint_ignore_region_free_voidp);
356
357   mc_checkpoint_ignore_region_t region =
358       xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
359   region->addr = addr;
360   region->size = size;
361
362   if (xbt_dynar_is_empty(mc_checkpoint_ignore)) {
363     xbt_dynar_push(mc_checkpoint_ignore, &region);
364   } else {
365
366     unsigned int cursor = 0;
367     int start = 0;
368     int end = xbt_dynar_length(mc_checkpoint_ignore) - 1;
369     mc_checkpoint_ignore_region_t current_region = NULL;
370
371     while (start <= end) {
372       cursor = (start + end) / 2;
373       current_region =
374           (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(mc_checkpoint_ignore,
375                                                            cursor,
376                                                            mc_checkpoint_ignore_region_t);
377       if (current_region->addr == addr) {
378         if (current_region->size == size) {
379           checkpoint_ignore_region_free(region);
380           if (!raw_mem_set)
381             MC_SET_STD_HEAP;
382           return;
383         } else if (current_region->size < size) {
384           start = cursor + 1;
385         } else {
386           end = cursor - 1;
387         }
388       } else if (current_region->addr < addr) {
389         start = cursor + 1;
390       } else {
391         end = cursor - 1;
392       }
393     }
394
395     if (current_region->addr == addr) {
396       if (current_region->size < size) {
397         xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, &region);
398       } else {
399         xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, &region);
400       }
401     } else if (current_region->addr < addr) {
402       xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, &region);
403     } else {
404       xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, &region);
405     }
406   }
407
408   if (!raw_mem_set)
409     MC_SET_STD_HEAP;
410 }