Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d80a13014e78304796dfde1fbe023c0e6023c3d6
[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_region_message_t message;
115   message.type = MC_MESSAGE_IGNORE_REGION;
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   if(!std_heap)
126     return;
127
128   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
129   MC_SET_MC_HEAP;
130
131   mc_heap_ignore_region_t region = NULL;
132   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
157   if (!raw_mem_set)
158     MC_SET_STD_HEAP;
159 }
160
161 void MC_remove_ignore_heap(void *address, size_t size)
162 {
163
164   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
165
166   MC_SET_MC_HEAP;
167
168   unsigned int cursor = 0;
169   int start = 0;
170   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
171   mc_heap_ignore_region_t region;
172   int ignore_found = 0;
173
174   while (start <= end) {
175     cursor = (start + end) / 2;
176     region =
177         (mc_heap_ignore_region_t) xbt_dynar_get_as(mc_heap_comparison_ignore,
178                                                    cursor,
179                                                    mc_heap_ignore_region_t);
180     if (region->address == address) {
181       ignore_found = 1;
182       break;
183     } else if (region->address < address) {
184       start = cursor + 1;
185     } else {
186       if ((char *) region->address <= ((char *) address + size)) {
187         ignore_found = 1;
188         break;
189       } else {
190         end = cursor - 1;
191       }
192     }
193   }
194
195   if (ignore_found == 1) {
196     xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
197     MC_remove_ignore_heap(address, size);
198   }
199
200   if (!raw_mem_set)
201     MC_SET_STD_HEAP;
202
203 }
204
205 // FIXME, cross-process support?
206 void MC_ignore_global_variable(const char *name)
207 {
208   mc_process_t process = &mc_model_checker->process;
209   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
210
211   MC_SET_MC_HEAP;
212
213   xbt_assert(process->object_infos, "MC subsystem not initialized");
214
215   size_t n = process->object_infos_size;
216   for (size_t i=0; i!=n; ++i) {
217     mc_object_info_t info = process->object_infos[i];
218
219     // Binary search:
220     int start = 0;
221     int end = xbt_dynar_length(info->global_variables) - 1;
222     while (start <= end) {
223       unsigned int cursor = (start + end) / 2;
224       dw_variable_t current_var =
225           (dw_variable_t) xbt_dynar_get_as(info->global_variables,
226                                            cursor, dw_variable_t);
227       if (strcmp(current_var->name, name) == 0) {
228         xbt_dynar_remove_at(info->global_variables, cursor, NULL);
229         start = 0;
230         end = xbt_dynar_length(info->global_variables) - 1;
231       } else if (strcmp(current_var->name, name) < 0) {
232         start = cursor + 1;
233       } else {
234         end = cursor - 1;
235       }
236     }
237   }
238
239   if (!raw_mem_set)
240     MC_SET_STD_HEAP;
241 }
242
243 /** \brief Ignore a local variable in a scope
244  *
245  *  Ignore all instances of variables with a given name in
246  *  any (possibly inlined) subprogram with a given namespaced
247  *  name.
248  *
249  *  \param var_name        Name of the local variable (or parameter to ignore)
250  *  \param subprogram_name Name of the subprogram fo ignore (NULL for any)
251  *  \param subprogram      (possibly inlined) Subprogram of the scope
252  *  \param scope           Current scope
253  */
254  // FIXME, cross-process support (messaging?)
255 static void mc_ignore_local_variable_in_scope(const char *var_name,
256                                               const char *subprogram_name,
257                                               dw_frame_t subprogram,
258                                               dw_frame_t scope)
259 {
260   // Processing of direct variables:
261
262   // If the current subprogram matches the given name:
263   if (!subprogram_name ||
264       (subprogram->name && strcmp(subprogram_name, subprogram->name) == 0)) {
265
266     // Try to find the variable and remove it:
267     int start = 0;
268     int end = xbt_dynar_length(scope->variables) - 1;
269
270     // Dichotomic search:
271     while (start <= end) {
272       int cursor = (start + end) / 2;
273       dw_variable_t current_var =
274           (dw_variable_t) xbt_dynar_get_as(scope->variables, cursor,
275                                            dw_variable_t);
276
277       int compare = strcmp(current_var->name, var_name);
278       if (compare == 0) {
279         // Variable found, remove it:
280         xbt_dynar_remove_at(scope->variables, cursor, NULL);
281
282         // and start again:
283         start = 0;
284         end = xbt_dynar_length(scope->variables) - 1;
285       } else if (compare < 0) {
286         start = cursor + 1;
287       } else {
288         end = cursor - 1;
289       }
290     }
291
292   }
293   // And recursive processing in nested scopes:
294   unsigned cursor = 0;
295   dw_frame_t nested_scope = NULL;
296   xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
297     // The new scope may be an inlined subroutine, in this case we want to use its
298     // namespaced name in recursive calls:
299     dw_frame_t nested_subprogram =
300         nested_scope->tag ==
301         DW_TAG_inlined_subroutine ? nested_scope : subprogram;
302
303     mc_ignore_local_variable_in_scope(var_name, subprogram_name,
304                                       nested_subprogram, nested_scope);
305   }
306 }
307
308 static void MC_ignore_local_variable_in_object(const char *var_name,
309                                                const char *subprogram_name,
310                                                mc_object_info_t info)
311 {
312   xbt_dict_cursor_t cursor2;
313   dw_frame_t frame;
314   char *key;
315   xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
316     mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
317   }
318 }
319
320 void MC_ignore_local_variable(const char *var_name, const char *frame_name)
321 {
322   mc_process_t process = &mc_model_checker->process;
323
324
325   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
326
327   if (strcmp(frame_name, "*") == 0)
328     frame_name = NULL;
329
330   MC_SET_MC_HEAP;
331
332   size_t n = process->object_infos_size;
333   size_t i;
334   for (i=0; i!=n; ++i) {
335     MC_ignore_local_variable_in_object(var_name, frame_name, process->object_infos[i]);
336   }
337
338   if (!raw_mem_set)
339     MC_SET_STD_HEAP;
340
341 }
342
343 /** @brief Register a stack in the model checker
344  *
345  *  The stacks are allocated in the heap. The MC handle them especially
346  *  when we analyse/compare the content of the heap so it must be told where
347  *  they are with this function.
348  *
349  *  @param stack
350  *  @param process Process owning the stack
351  *  @param context
352  *  @param size    Size of the stack
353  */
354 void MC_new_stack_area(void *stack, smx_process_t process, void *context, size_t size)
355 {
356
357   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
358
359   MC_SET_MC_HEAP;
360
361   if (stacks_areas == NULL)
362     stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
363
364   stack_region_t region = NULL;
365   region = xbt_new0(s_stack_region_t, 1);
366   region->address = stack;
367   region->process_name = process && process->name ? strdup(process->name) : NULL;
368   region->context = context;
369   region->size = size;
370   region->block =
371       ((char *) stack -
372        (char *) std_heap->heapbase) / BLOCKSIZE + 1;
373 #ifdef HAVE_SMPI
374   if (smpi_privatize_global_variables && process) {
375     region->process_index = smpi_process_index_of_smx_process(process);
376   } else
377 #endif
378   region->process_index = -1;
379
380   xbt_dynar_push(stacks_areas, &region);
381
382   if (!raw_mem_set)
383     MC_SET_STD_HEAP;
384 }
385
386 void MC_process_ignore_memory(mc_process_t process, void *addr, size_t size)
387 {
388   xbt_dynar_t checkpoint_ignore = process->checkpoint_ignore;
389   mc_checkpoint_ignore_region_t region =
390       xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
391   region->addr = addr;
392   region->size = size;
393
394   if (xbt_dynar_is_empty(checkpoint_ignore)) {
395     xbt_dynar_push(checkpoint_ignore, &region);
396   } else {
397
398     unsigned int cursor = 0;
399     int start = 0;
400     int end = xbt_dynar_length(checkpoint_ignore) - 1;
401     mc_checkpoint_ignore_region_t current_region = NULL;
402
403     while (start <= end) {
404       cursor = (start + end) / 2;
405       current_region =
406           (mc_checkpoint_ignore_region_t) xbt_dynar_get_as(checkpoint_ignore,
407                                                            cursor,
408                                                            mc_checkpoint_ignore_region_t);
409       if (current_region->addr == addr) {
410         if (current_region->size == size) {
411           checkpoint_ignore_region_free(region);
412           return;
413         } else if (current_region->size < size) {
414           start = cursor + 1;
415         } else {
416           end = cursor - 1;
417         }
418       } else if (current_region->addr < addr) {
419         start = cursor + 1;
420       } else {
421         end = cursor - 1;
422       }
423     }
424
425     if (current_region->addr == addr) {
426       if (current_region->size < size) {
427         xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, &region);
428       } else {
429         xbt_dynar_insert_at(checkpoint_ignore, cursor, &region);
430       }
431     } else if (current_region->addr < addr) {
432       xbt_dynar_insert_at(checkpoint_ignore, cursor + 1, &region);
433     } else {
434       xbt_dynar_insert_at(checkpoint_ignore, cursor, &region);
435     }
436   }
437 }