Logo AND Algorithmique Numérique Distribuée

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