Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Multiple heap removal (partial)
[simgrid.git] / src / mc / mc_ignore.cpp
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_object_info.h"
9 #include "mc_private.h"
10 #include "smpi/private.h"
11 #include "mc/mc_snapshot.h"
12 #include "mc_ignore.h"
13 #include "mc_protocol.h"
14 #include "mc_client.h"
15
16 extern "C" {
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_ignore, mc,
19                                 "Logging specific to MC ignore mechanism");
20
21
22 /**************************** Global variables ******************************/
23 // Those structures live with the MCer and should be moved in the model_checker
24 // structure but they are currently used before the MC initialisation
25 // (in standalone mode).
26
27
28 extern xbt_dynar_t stacks_areas;
29
30 /**************************** Structures ******************************/
31 typedef struct s_mc_stack_ignore_variable {
32   char *var_name;
33   char *frame;
34 } s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
35
36 /**************************** Free functions ******************************/
37
38 static void stack_ignore_variable_free(mc_stack_ignore_variable_t v)
39 {
40   xbt_free(v->var_name);
41   xbt_free(v->frame);
42   xbt_free(v);
43 }
44
45 static void stack_ignore_variable_free_voidp(void *v)
46 {
47   stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
48 }
49
50 void heap_ignore_region_free(mc_heap_ignore_region_t r)
51 {
52   xbt_free(r);
53 }
54
55 void heap_ignore_region_free_voidp(void *r)
56 {
57   heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
58 }
59
60 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r)
61 {
62   xbt_free(r);
63 }
64
65 static void checkpoint_ignore_region_free_voidp(void *r)
66 {
67   checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
68 }
69
70 xbt_dynar_t MC_checkpoint_ignore_new(void)
71 {
72   return xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t),
73                         checkpoint_ignore_region_free_voidp);
74 }
75
76 /***********************************************************************/
77
78 // ***** Model-checked
79
80 void MC_ignore_heap(void *address, size_t size)
81 {
82   if (mc_mode != MC_MODE_CLIENT)
83     return;
84
85   s_mc_heap_ignore_region_t region;
86   memset(&region, 0, sizeof(region));
87   region.address = address;
88   region.size = size;
89   region.block =
90    ((char *) address -
91     (char *) std_heap->heapbase) / BLOCKSIZE + 1;
92   if (std_heap->heapinfo[region.block].type == 0) {
93     region.fragment = -1;
94     std_heap->heapinfo[region.block].busy_block.ignore++;
95   } else {
96     region.fragment =
97         ((uintptr_t) (ADDR2UINT(address) % (BLOCKSIZE))) >> std_heap->
98         heapinfo[region.block].type;
99     std_heap->heapinfo[region.block].busy_frag.ignore[region.fragment]++;
100   }
101
102   s_mc_ignore_heap_message_t message;
103   message.type = MC_MESSAGE_IGNORE_HEAP;
104   message.region = region;
105   if (MC_protocol_send(mc_client->fd, &message, sizeof(message)))
106     xbt_die("Could not send ignored region to MCer");
107 }
108
109 void MC_remove_ignore_heap(void *address, size_t size)
110 {
111   if (mc_mode != MC_MODE_CLIENT)
112     return;
113
114   s_mc_ignore_memory_message_t message;
115   message.type = MC_MESSAGE_UNIGNORE_HEAP;
116   message.addr = address;
117   message.size = size;
118   MC_client_send_message(&message, sizeof(message));
119 }
120
121 // ***** Model-checker:
122
123 void MC_ignore_global_variable(const char *name)
124 {
125   mc_process_t process = &mc_model_checker->process();
126   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
127   xbt_assert(process->object_infos, "MC subsystem not initialized");
128
129   size_t n = process->object_infos_size;
130   for (size_t i=0; i!=n; ++i) {
131     mc_object_info_t info = process->object_infos[i];
132
133     // Binary search:
134     int start = 0;
135     int end = xbt_dynar_length(info->global_variables) - 1;
136     while (start <= end) {
137       unsigned int cursor = (start + end) / 2;
138       dw_variable_t current_var =
139           (dw_variable_t) xbt_dynar_get_as(info->global_variables,
140                                            cursor, dw_variable_t);
141       if (strcmp(current_var->name, name) == 0) {
142         xbt_dynar_remove_at(info->global_variables, cursor, NULL);
143         start = 0;
144         end = xbt_dynar_length(info->global_variables) - 1;
145       } else if (strcmp(current_var->name, name) < 0) {
146         start = cursor + 1;
147       } else {
148         end = cursor - 1;
149       }
150     }
151   }
152   mmalloc_set_current_heap(heap);
153 }
154
155 /** \brief Ignore a local variable in a scope
156  *
157  *  Ignore all instances of variables with a given name in
158  *  any (possibly inlined) subprogram with a given namespaced
159  *  name.
160  *
161  *  \param var_name        Name of the local variable (or parameter to ignore)
162  *  \param subprogram_name Name of the subprogram fo ignore (NULL for any)
163  *  \param subprogram      (possibly inlined) Subprogram of the scope
164  *  \param scope           Current scope
165  */
166 static void mc_ignore_local_variable_in_scope(const char *var_name,
167                                               const char *subprogram_name,
168                                               dw_frame_t subprogram,
169                                               dw_frame_t scope)
170 {
171   // Processing of direct variables:
172
173   // If the current subprogram matches the given name:
174   if (!subprogram_name ||
175       (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
176
177     // Try to find the variable and remove it:
178     int start = 0;
179     int end = xbt_dynar_length(scope->variables) - 1;
180
181     // Dichotomic search:
182     while (start <= end) {
183       int cursor = (start + end) / 2;
184       dw_variable_t current_var =
185           (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
186                                            dw_variable_t);
187
188       int compare = strcmp(current_var->name, var_name);
189       if (compare == 0) {
190         // Variable found, remove it:
191         xbt_dynar_remove_at(scope->variables, cursor, NULL);
192
193         // and start again:
194         start = 0;
195         end = xbt_dynar_length(scope->variables) - 1;
196       } else if (compare < 0) {
197         start = cursor + 1;
198       } else {
199         end = cursor - 1;
200       }
201     }
202
203   }
204   // And recursive processing in nested scopes:
205   unsigned cursor = 0;
206   dw_frame_t nested_scope = NULL;
207   xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
208     // The new scope may be an inlined subroutine, in this case we want to use its
209     // namespaced name in recursive calls:
210     dw_frame_t nested_subprogram =
211         nested_scope->tag ==
212         DW_TAG_inlined_subroutine ? nested_scope : subprogram;
213
214     mc_ignore_local_variable_in_scope(var_name, subprogram_name,
215                                       nested_subprogram, nested_scope);
216   }
217 }
218
219 static void MC_ignore_local_variable_in_object(const char *var_name,
220                                                const char *subprogram_name,
221                                                mc_object_info_t info)
222 {
223   xbt_dict_cursor_t cursor2;
224   dw_frame_t frame;
225   char *key;
226   xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
227     mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
228   }
229 }
230
231 // MCer
232 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
233 {
234   mc_process_t process = &mc_model_checker->process();
235   if (strcmp(frame_name, "*") == 0)
236     frame_name = NULL;
237   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
238
239   size_t n = process->object_infos_size;
240   size_t i;
241   for (i=0; i!=n; ++i) {
242     MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
243   }
244
245   mmalloc_set_current_heap(heap);
246 }
247
248 void MC_stack_area_add(stack_region_t stack_area)
249 {
250   if (stacks_areas == NULL)
251     stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
252   xbt_dynar_push(stacks_areas, &stack_area);
253 }
254
255 /** @brief Register a stack in the model checker
256  *
257  *  The stacks are allocated in the heap. The MC handle them especially
258  *  when we analyse/compare the content of the heap so it must be told where
259  *  they are with this function.
260  *
261  *  @param stack
262  *  @param process Process owning the stack
263  *  @param context
264  *  @param size    Size of the stack
265  */
266 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
267 {
268   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
269
270   stack_region_t region = xbt_new0(s_stack_region_t, 1);
271   region->address = stack;
272   region->context = context;
273   region->size = size;
274   region->block =
275       ((char *) stack -
276        (char *) std_heap->heapbase) / BLOCKSIZE + 1;
277 #ifdef HAVE_SMPI
278   if (smpi_privatize_global_variables && process) {
279     region->process_index = smpi_process_index_of_smx_process(process);
280   } else
281 #endif
282   region->process_index = -1;
283
284   if (mc_mode == MC_MODE_CLIENT) {
285     s_mc_stack_region_message_t message;
286     message.type = MC_MESSAGE_STACK_REGION;
287     message.stack_region = *region;
288     MC_client_send_message(&message, sizeof(message));
289   }
290
291   MC_stack_area_add(region);
292
293   mmalloc_set_current_heap(heap);
294 }
295
296 void MC_process_ignore_memory(mc_process_t process, void *addr, size_t size)
297 {
298   xbt_dynar_t checkpoint_ignore = process->checkpoint_ignore;
299   mc_checkpoint_ignore_region_t region =
300       xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
301   region->addr = addr;
302   region->size = size;
303
304   if (xbt_dynar_is_empty(checkpoint_ignore)) {
305     xbt_dynar_push(checkpoint_ignore, &region);
306   } else {
307
308     unsigned int cursor = 0;
309     int start = 0;
310     int end = xbt_dynar_length(checkpoint_ignore) - 1;
311     mc_checkpoint_ignore_region_t current_region = NULL;
312
313     while (start <= end) {
314       cursor = (start + end) / 2;
315       current_region =
316           (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(checkpoint_ignore,
317                                                            cursor,
318                                                            mc_checkpoint_ignore_region_t);
319       if (current_region->addr == addr) {
320         if (current_region->size == size) {
321           checkpoint_ignore_region_free(region);
322           return;
323         } else if (current_region->size < size) {
324           start = cursor + 1;
325         } else {
326           end = cursor - 1;
327         }
328       } else if (current_region->addr < addr) {
329         start = cursor + 1;
330       } else {
331         end = cursor - 1;
332       }
333     }
334
335     if (current_region->addr == addr) {
336       if (current_region->size < size) {
337         xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, &region);
338       } else {
339         xbt_dynar_insert_at(checkpoint_ignore, cursor, &region);
340       }
341     } else if (current_region->addr < addr) {
342       xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, &region);
343     } else {
344       xbt_dynar_insert_at(checkpoint_ignore, cursor, &region);
345     }
346   }
347 }
348
349 }