Logo AND Algorithmique Numérique Distribuée

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