Logo AND Algorithmique Numérique Distribuée

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