Logo AND Algorithmique Numérique Distribuée

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