Logo AND Algorithmique Numérique Distribuée

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