Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7e04041f8dbb335b7cdae2aae984d6316382a976
[simgrid.git] / src / mc / mcer_ignore.cpp
1 /* Copyright (c) 2008-2015. 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_object_info.h"
9 #include "mc/mc_private.h"
10 #include "smpi/private.h"
11 #include "mc/mc_snapshot.h"
12 #include "mc/mc_ignore.h"
13 #include "mc/mc_protocol.h"
14 #include "mc/mc_client.h"
15
16 extern "C" {
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mcer_ignore, mc,
19                                 "Logging specific to MC ignore mechanism");
20
21 // ***** Ignore heap chunks
22
23 extern xbt_dynar_t mc_heap_comparison_ignore;
24
25 void heap_ignore_region_free(mc_heap_ignore_region_t r)
26 {
27   xbt_free(r);
28 }
29
30 void heap_ignore_region_free_voidp(void *r)
31 {
32   heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
33 }
34
35
36 void MC_heap_region_ignore_insert(mc_heap_ignore_region_t region)
37 {
38   if (mc_heap_comparison_ignore == NULL) {
39     mc_heap_comparison_ignore =
40         xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
41                       heap_ignore_region_free_voidp);
42     xbt_dynar_push(mc_heap_comparison_ignore, &region);
43     return;
44   }
45
46   unsigned int cursor = 0;
47   mc_heap_ignore_region_t current_region = NULL;
48   int start = 0;
49   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
50
51   // Find the position where we want to insert the mc_heap_ignore_region_t:
52   while (start <= end) {
53     cursor = (start + end) / 2;
54     current_region =
55         (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
56                                                    cursor,
57                                                    mc_heap_ignore_region_t);
58     if (current_region->address == region->address) {
59       heap_ignore_region_free(region);
60       return;
61     } else if (current_region->address < region->address) {
62       start = cursor + 1;
63     } else {
64       end = cursor - 1;
65     }
66   }
67
68   // Insert it mc_heap_ignore_region_t:
69   if (current_region->address < region->address)
70     xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, &region);
71   else
72     xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, &region);
73 }
74
75 void MC_heap_region_ignore_remove(void *address, size_t size)
76 {
77   unsigned int cursor = 0;
78   int start = 0;
79   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
80   mc_heap_ignore_region_t region;
81   int ignore_found = 0;
82
83   while (start <= end) {
84     cursor = (start + end) / 2;
85     region =
86         (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
87                                                    cursor,
88                                                    mc_heap_ignore_region_t);
89     if (region->address == address) {
90       ignore_found = 1;
91       break;
92     } else if (region->address < address) {
93       start = cursor + 1;
94     } else {
95       if ((char *) region->address <= ((char *) address + size)) {
96         ignore_found = 1;
97         break;
98       } else {
99         end = cursor - 1;
100       }
101     }
102   }
103
104   if (ignore_found == 1) {
105     xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
106     MC_remove_ignore_heap(address, size);
107   }
108 }
109
110 // ***** Ignore global variables
111
112 void MCer_ignore_global_variable(const char *name)
113 {
114   mc_process_t process = &mc_model_checker->process();
115   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
116   xbt_assert(process->object_infos, "MC subsystem not initialized");
117
118   size_t n = process->object_infos_size;
119   for (size_t i=0; i!=n; ++i) {
120     mc_object_info_t info = process->object_infos[i];
121
122     // Binary search:
123     int start = 0;
124     int end = xbt_dynar_length(info->global_variables) - 1;
125     while (start <= end) {
126       unsigned int cursor = (start + end) / 2;
127       dw_variable_t current_var =
128           (dw_variable_t) xbt_dynar_get_as(info->global_variables,
129                                            cursor, dw_variable_t);
130       if (strcmp(current_var->name, name) == 0) {
131         xbt_dynar_remove_at(info->global_variables, cursor, NULL);
132         start = 0;
133         end = xbt_dynar_length(info->global_variables) - 1;
134       } else if (strcmp(current_var->name, name) < 0) {
135         start = cursor + 1;
136       } else {
137         end = cursor - 1;
138       }
139     }
140   }
141   mmalloc_set_current_heap(heap);
142 }
143
144 // ***** Ignore local variables
145
146 static void mc_ignore_local_variable_in_scope(const char *var_name,
147                                               const char *subprogram_name,
148                                               dw_frame_t subprogram,
149                                               dw_frame_t scope);
150 static void MC_ignore_local_variable_in_object(const char *var_name,
151                                                const char *subprogram_name,
152                                                mc_object_info_t info);
153
154 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
155 {
156   mc_process_t process = &mc_model_checker->process();
157   if (strcmp(frame_name, "*") == 0)
158     frame_name = NULL;
159   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
160
161   size_t n = process->object_infos_size;
162   size_t i;
163   for (i=0; i!=n; ++i) {
164     MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
165   }
166
167   mmalloc_set_current_heap(heap);
168 }
169
170 static void MC_ignore_local_variable_in_object(const char *var_name,
171                                                const char *subprogram_name,
172                                                mc_object_info_t info)
173 {
174   xbt_dict_cursor_t cursor2;
175   dw_frame_t frame;
176   char *key;
177   xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
178     mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
179   }
180 }
181
182 /** \brief Ignore a local variable in a scope
183  *
184  *  Ignore all instances of variables with a given name in
185  *  any (possibly inlined) subprogram with a given namespaced
186  *  name.
187  *
188  *  \param var_name        Name of the local variable (or parameter to ignore)
189  *  \param subprogram_name Name of the subprogram fo ignore (NULL for any)
190  *  \param subprogram      (possibly inlined) Subprogram of the scope
191  *  \param scope           Current scope
192  */
193 static void mc_ignore_local_variable_in_scope(const char *var_name,
194                                               const char *subprogram_name,
195                                               dw_frame_t subprogram,
196                                               dw_frame_t scope)
197 {
198   // Processing of direct variables:
199
200   // If the current subprogram matches the given name:
201   if (!subprogram_name ||
202       (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
203
204     // Try to find the variable and remove it:
205     int start = 0;
206     int end = xbt_dynar_length(scope->variables) - 1;
207
208     // Dichotomic search:
209     while (start <= end) {
210       int cursor = (start + end) / 2;
211       dw_variable_t current_var =
212           (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
213                                            dw_variable_t);
214
215       int compare = strcmp(current_var->name, var_name);
216       if (compare == 0) {
217         // Variable found, remove it:
218         xbt_dynar_remove_at(scope->variables, cursor, NULL);
219
220         // and start again:
221         start = 0;
222         end = xbt_dynar_length(scope->variables) - 1;
223       } else if (compare < 0) {
224         start = cursor + 1;
225       } else {
226         end = cursor - 1;
227       }
228     }
229
230   }
231   // And recursive processing in nested scopes:
232   unsigned cursor = 0;
233   dw_frame_t nested_scope = NULL;
234   xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
235     // The new scope may be an inlined subroutine, in this case we want to use its
236     // namespaced name in recursive calls:
237     dw_frame_t nested_subprogram =
238         nested_scope->tag ==
239         DW_TAG_inlined_subroutine ? nested_scope : subprogram;
240
241     mc_ignore_local_variable_in_scope(var_name, subprogram_name,
242                                       nested_subprogram, nested_scope);
243   }
244 }
245
246 // ****** Checkpoint ignore:
247
248 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r)
249 {
250   xbt_free(r);
251 }
252
253 static void checkpoint_ignore_region_free_voidp(void *r)
254 {
255   checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
256 }
257
258 xbt_dynar_t MC_checkpoint_ignore_new(void)
259 {
260   return xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
261                         checkpoint_ignore_region_free_voidp);
262 }
263
264 // ***** Generic memory ignore mechanism
265
266 void MC_process_ignore_memory(mc_process_t process, void *addr, size_t size)
267 {
268   xbt_dynar_t checkpoint_ignore = process->checkpoint_ignore;
269   mc_checkpoint_ignore_region_t region =
270       xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
271   region->addr = addr;
272   region->size = size;
273
274   if (xbt_dynar_is_empty(checkpoint_ignore)) {
275     xbt_dynar_push(checkpoint_ignore, &region);
276   } else {
277
278     unsigned int cursor = 0;
279     int start = 0;
280     int end = xbt_dynar_length(checkpoint_ignore) - 1;
281     mc_checkpoint_ignore_region_t current_region = NULL;
282
283     while (start <= end) {
284       cursor = (start + end) / 2;
285       current_region =
286           (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(checkpoint_ignore,
287                                                            cursor,
288                                                            mc_checkpoint_ignore_region_t);
289       if (current_region->addr == addr) {
290         if (current_region->size == size) {
291           checkpoint_ignore_region_free(region);
292           return;
293         } else if (current_region->size < size) {
294           start = cursor + 1;
295         } else {
296           end = cursor - 1;
297         }
298       } else if (current_region->addr < addr) {
299         start = cursor + 1;
300       } else {
301         end = cursor - 1;
302       }
303     }
304
305     if (current_region->addr == addr) {
306       if (current_region->size < size) {
307         xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, &region);
308       } else {
309         xbt_dynar_insert_at(checkpoint_ignore, cursor, &region);
310       }
311     } else if (current_region->addr < addr) {
312       xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, &region);
313     } else {
314       xbt_dynar_insert_at(checkpoint_ignore, cursor, &region);
315     }
316   }
317 }
318
319 }