Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6920bf2b97f94de545981d76a416a289f07f7a2c
[simgrid.git] / src / mc / mc_checkpoint.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 #define _GNU_SOURCE
8 #define UNW_LOCAL_ONLY
9
10
11 #include <string.h>
12 #include <link.h>
13
14 #include "internal_config.h"
15 #include "mc_private.h"
16 #include "xbt/module.h"
17 #include <xbt/mmalloc.h>
18 #include "../smpi/private.h"
19 #include <alloca.h>
20
21 #include "xbt/mmalloc/mmprivate.h"
22
23 #include "../simix/smx_private.h"
24
25 #include <libunwind.h>
26 #include <libelf.h>
27
28 #include "mc_private.h"
29 #include <mc/mc.h>
30
31 #include "mc_mmu.h"
32
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
34                                 "Logging specific to mc_checkpoint");
35
36 char *libsimgrid_path;
37
38 /************************************  Free functions **************************************/
39 /*****************************************************************************************/
40
41 static void MC_snapshot_stack_free(mc_snapshot_stack_t s)
42 {
43   if (s) {
44     xbt_dynar_free(&(s->local_variables));
45     xbt_dynar_free(&(s->stack_frames));
46     xbt_free(s);
47   }
48 }
49
50 static void MC_snapshot_stack_free_voidp(void *s)
51 {
52   MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
53 }
54
55 static void local_variable_free(local_variable_t v)
56 {
57   xbt_free(v->name);
58   xbt_free(v);
59 }
60
61 static void local_variable_free_voidp(void *v)
62 {
63   local_variable_free((local_variable_t) * (void **) v);
64 }
65
66 void MC_region_destroy(mc_mem_region_t reg)
67 {
68   if (!reg)
69     return;
70
71   //munmap(reg->data, reg->size);
72   xbt_free(reg->data);
73   if (reg->page_numbers) {
74     mc_free_page_snapshot_region(reg->page_numbers, mc_page_count(reg->size));
75   }
76   xbt_free(reg);
77 }
78
79 void MC_free_snapshot(mc_snapshot_t snapshot)
80 {
81   unsigned int i;
82   for (i = 0; i < NB_REGIONS; i++) {
83     MC_region_destroy(snapshot->regions[i]);
84   }
85
86   xbt_free(snapshot->stack_sizes);
87   xbt_dynar_free(&(snapshot->stacks));
88   xbt_dynar_free(&(snapshot->to_ignore));
89   xbt_dynar_free(&snapshot->ignored_data);
90
91   if (snapshot->privatization_regions) {
92     size_t n = xbt_dynar_length(snapshot->enabled_processes);
93     for (i = 0; i != n; ++i) {
94       MC_region_destroy(snapshot->privatization_regions[i]);
95     }
96     xbt_free(snapshot->privatization_regions);
97   }
98
99   xbt_free(snapshot);
100 }
101
102 /*******************************  Snapshot regions ********************************/
103 /*********************************************************************************/
104
105 static mc_mem_region_t mc_region_new_dense(int type, void *start_addr, void* permanent_addr, size_t size, mc_mem_region_t ref_reg)
106 {
107   mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
108   new_reg->start_addr = start_addr;
109   new_reg->permanent_addr = permanent_addr;
110   new_reg->data = NULL;
111   new_reg->size = size;
112   new_reg->page_numbers = NULL;
113   new_reg->data = xbt_malloc(size);
114   memcpy(new_reg->data, permanent_addr, size);
115   XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu",
116             type, new_reg->data, permanent_addr, size);
117   return new_reg;
118
119 }
120
121 /** @brief Take a snapshot of a given region
122  *
123  * @param type
124  * @param start_addr   Address of the region in the simulated process
125  * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the privatized mapping)
126  * @param size         Size of the data*
127  * @param ref_reg      Reference corresponding region
128  */
129 static mc_mem_region_t MC_region_new(int type, void *start_addr, void* permanent_addr, size_t size, mc_mem_region_t ref_reg)
130 {
131   if (_sg_mc_sparse_checkpoint) {
132     return mc_region_new_sparse(type, start_addr, permanent_addr, size, ref_reg);
133   } else  {
134     return mc_region_new_dense(type, start_addr, permanent_addr, size, ref_reg);
135   }
136 }
137
138 /** @brief Restore a region from a snapshot
139  *
140  *  If we are using per page snapshots, it is possible to use the reference
141  *  region in order to do an incremental restoration of the region: the
142  *  softclean pages which are shared between the two snapshots do not need
143  *  to be restored.
144  *
145  *  @param reg     Target region
146  *  @param reg_reg Current region (if not NULL), used for lazy per page restoration
147  */
148 static void MC_region_restore(mc_mem_region_t reg, mc_mem_region_t ref_reg)
149 {
150   /*FIXME: check if start_addr is still mapped, if it is not, then map it
151     before copying the data */
152   if (!reg->page_numbers) {
153     memcpy(reg->permanent_addr, reg->data, reg->size);
154   } else {
155     mc_region_restore_sparse(reg, ref_reg);
156   }
157   return;
158 }
159
160 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type,
161                                    void *start_addr, void* permanent_addr, size_t size)
162
163 {
164   mc_mem_region_t ref_reg =
165     mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->regions[type] : NULL;
166   mc_mem_region_t new_reg = MC_region_new(type, start_addr, permanent_addr, size, ref_reg);
167   snapshot->regions[type] = new_reg;
168   return;
169 }
170
171 static void MC_get_memory_regions(mc_snapshot_t snapshot)
172 {
173
174   void *start_heap = std_heap->base;
175   void *end_heap = std_heap->breakval;
176   MC_snapshot_add_region(snapshot, 0, start_heap, start_heap,
177                          (char *) end_heap - (char *) start_heap);
178   snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
179   snapshot->privatization_regions = NULL;
180
181   MC_snapshot_add_region(snapshot, 1,
182       mc_libsimgrid_info->start_rw, mc_libsimgrid_info->start_rw,
183       mc_libsimgrid_info->end_rw - mc_libsimgrid_info->start_rw);
184
185 #ifdef HAVE_SMPI
186   size_t i;
187   if (smpi_privatize_global_variables && smpi_process_count()) {
188     // Snapshot the global variable of the application separately for each
189     // simulated process:
190     snapshot->privatization_regions =
191       xbt_new(mc_mem_region_t, smpi_process_count());
192     for (i = 0; i < smpi_process_count(); i++) {
193       mc_mem_region_t ref_reg =
194         mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->privatization_regions[i] : NULL;
195       snapshot->privatization_regions[i] =
196         MC_region_new(-1, mc_binary_info->start_rw, smpi_privatisation_regions[i].address, size_data_exe, ref_reg);
197     }
198     snapshot->privatization_index = smpi_loaded_page;
199     snapshot->regions[2] = NULL;
200   } else
201 #endif
202   {
203     MC_snapshot_add_region(snapshot, 2,
204                            mc_binary_info->start_rw, mc_binary_info->start_rw,
205                            mc_binary_info->end_rw - mc_binary_info->start_rw);
206     snapshot->privatization_regions = NULL;
207     snapshot->privatization_index = -1;
208   }
209 }
210
211 /** @brief Finds the range of the different memory segments and binary paths */
212 void MC_init_memory_map_info()
213 {
214
215   unsigned int i = 0;
216   s_map_region_t reg;
217   memory_map_t maps = MC_get_memory_map();
218
219   maestro_stack_start = NULL;
220   maestro_stack_end = NULL;
221   libsimgrid_path = NULL;
222
223   while (i < maps->mapsize) {
224     reg = maps->regions[i];
225     if (maps->regions[i].pathname == NULL) {
226       // Nothing to do
227     } else if ((reg.prot & PROT_WRITE)
228                && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
229       maestro_stack_start = reg.start_addr;
230       maestro_stack_end = reg.end_addr;
231     } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
232                && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
233                           10)) {
234       if (libsimgrid_path == NULL)
235         libsimgrid_path = strdup(maps->regions[i].pathname);
236     }
237     i++;
238   }
239
240   xbt_assert(maestro_stack_start, "maestro_stack_start");
241   xbt_assert(maestro_stack_end, "maestro_stack_end");
242   xbt_assert(libsimgrid_path, "libsimgrid_path&");
243
244   MC_free_memory_map(maps);
245
246 }
247
248 /** \brief Fills the position of the segments (executable, read-only, read/write).
249  *
250  * TODO, use dl_iterate_phdr to be more robust
251  * */
252 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
253 {
254
255   unsigned int i = 0;
256   s_map_region_t reg;
257   const char *name = basename(result->file_name);
258   while (i < maps->mapsize) {
259     reg = maps->regions[i];
260     if (maps->regions[i].pathname == NULL
261         || strcmp(basename(maps->regions[i].pathname), name)) {
262       // Nothing to do
263     } else if ((reg.prot & PROT_WRITE)) {
264       xbt_assert(!result->start_rw,
265                  "Multiple read-write segments for %s, not supported",
266                  maps->regions[i].pathname);
267       result->start_rw = reg.start_addr;
268       result->end_rw = reg.end_addr;
269       // .bss is usually after the .data:
270       s_map_region_t *next = &(maps->regions[i + 1]);
271       if (next->pathname == NULL && (next->prot & PROT_WRITE)
272           && next->start_addr == reg.end_addr) {
273         result->end_rw = maps->regions[i + 1].end_addr;
274       }
275     } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
276       xbt_assert(!result->start_exec,
277                  "Multiple executable segments for %s, not supported",
278                  maps->regions[i].pathname);
279       result->start_exec = reg.start_addr;
280       result->end_exec = reg.end_addr;
281     } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
282       xbt_assert(!result->start_ro,
283                  "Multiple read only segments for %s, not supported",
284                  maps->regions[i].pathname);
285       result->start_ro = reg.start_addr;
286       result->end_ro = reg.end_addr;
287     }
288     i++;
289   }
290
291   xbt_assert(result->file_name);
292   xbt_assert(result->start_rw);
293   xbt_assert(result->start_exec);
294 }
295
296 /************************************* Take Snapshot ************************************/
297 /****************************************************************************************/
298
299 /** \brief Checks whether the variable is in scope for a given IP.
300  *
301  *  A variable may be defined only from a given value of IP.
302  *
303  *  \param var   Variable description
304  *  \param frame Scope description
305  *  \param ip    Instruction pointer
306  *  \return      true if the variable is valid
307  * */
308 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
309                               const void *ip)
310 {
311   // The variable is not yet valid:
312   if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
313     return false;
314   else
315     return true;
316 }
317
318 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
319                                            dw_frame_t scope, int process_index, xbt_dynar_t result)
320 {
321   void *ip = (void *) stack_frame->ip;
322   if (ip < scope->low_pc || ip >= scope->high_pc)
323     return;
324
325   unsigned cursor = 0;
326   dw_variable_t current_variable;
327   xbt_dynar_foreach(scope->variables, cursor, current_variable) {
328
329     if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
330       continue;
331
332     int region_type;
333     if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
334       region_type = 1;
335     else
336       region_type = 2;
337
338     local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
339     new_var->subprogram = stack_frame->frame;
340     new_var->ip = stack_frame->ip;
341     new_var->name = xbt_strdup(current_variable->name);
342     new_var->type = current_variable->type;
343     new_var->region = region_type;
344
345     if (current_variable->address != NULL) {
346       new_var->address = current_variable->address;
347     } else if (current_variable->locations.size != 0) {
348       s_mc_location_t location;
349       mc_dwarf_resolve_locations(&location, &current_variable->locations,
350                                               current_variable->object_info,
351                                               &(stack_frame->unw_cursor),
352                                               (void *) stack_frame->frame_base,
353                                               NULL, process_index);
354
355       switch(mc_get_location_type(&location)) {
356       case MC_LOCATION_TYPE_ADDRESS:
357         new_var->address = location.memory_location;
358         break;
359       case MC_LOCATION_TYPE_REGISTER:
360       default:
361         xbt_die("Cannot handle non-address variable");
362       }
363
364     } else {
365       xbt_die("No address");
366     }
367
368     xbt_dynar_push(result, &new_var);
369   }
370
371   // Recursive processing of nested scopes:
372   dw_frame_t nested_scope = NULL;
373   xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
374     mc_fill_local_variables_values(stack_frame, nested_scope, process_index, result);
375   }
376 }
377
378 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames, int process_index)
379 {
380
381   unsigned cursor1 = 0;
382   mc_stack_frame_t stack_frame;
383   xbt_dynar_t variables =
384       xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
385
386   xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
387     mc_fill_local_variables_values(stack_frame, stack_frame->frame, process_index, variables);
388   }
389
390   return variables;
391 }
392
393 static void MC_stack_frame_free_voipd(void *s)
394 {
395   mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
396   if (stack_frame) {
397     xbt_free(stack_frame->frame_name);
398     xbt_free(stack_frame);
399   }
400 }
401
402 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
403 {
404   xbt_dynar_t result =
405       xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
406
407   unw_cursor_t c;
408
409   // TODO, check condition check (unw_init_local==0 means end of frame)
410   if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
411
412     xbt_die("Could not initialize stack unwinding");
413
414   } else
415     while (1) {
416
417       mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
418       xbt_dynar_push(result, &stack_frame);
419
420       stack_frame->unw_cursor = c;
421
422       unw_word_t ip, sp;
423
424       unw_get_reg(&c, UNW_REG_IP, &ip);
425       unw_get_reg(&c, UNW_REG_SP, &sp);
426
427       stack_frame->ip = ip;
428       stack_frame->sp = sp;
429
430       // TODO, use real addresses in frame_t instead of fixing it here
431
432       dw_frame_t frame = MC_find_function_by_ip((void *) ip);
433       stack_frame->frame = frame;
434
435       if (frame) {
436         stack_frame->frame_name = xbt_strdup(frame->name);
437         stack_frame->frame_base =
438             (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
439       } else {
440         stack_frame->frame_base = 0;
441         stack_frame->frame_name = NULL;
442       }
443
444       /* Stop before context switch with maestro */
445       if (frame != NULL && frame->name != NULL
446           && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
447         break;
448
449       int ret = ret = unw_step(&c);
450       if (ret == 0) {
451         xbt_die("Unexpected end of stack.");
452       } else if (ret < 0) {
453         xbt_die("Error while unwinding stack.");
454       }
455     }
456
457   if (xbt_dynar_length(result) == 0) {
458     XBT_INFO("unw_init_local failed");
459     xbt_abort();
460   }
461
462   return result;
463 };
464
465 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot)
466 {
467
468   xbt_dynar_t res =
469       xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
470                     MC_snapshot_stack_free_voidp);
471
472   unsigned int cursor = 0;
473   stack_region_t current_stack;
474
475   xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
476     mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
477     st->stack_frames = MC_unwind_stack_frames(current_stack->context);
478     st->local_variables = MC_get_local_variables_values(st->stack_frames, current_stack->process_index);
479     st->process_index = current_stack->process_index;
480
481     unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
482
483     xbt_dynar_push(res, &st);
484     (*snapshot)->stack_sizes =
485         xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
486     (*snapshot)->stack_sizes[cursor] =
487       (char*) current_stack->address + current_stack->size - (char*) sp;
488   }
489
490   return res;
491
492 }
493
494 static xbt_dynar_t MC_take_snapshot_ignore()
495 {
496
497   if (mc_heap_comparison_ignore == NULL)
498     return NULL;
499
500   xbt_dynar_t cpy =
501       xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
502                     heap_ignore_region_free_voidp);
503
504   unsigned int cursor = 0;
505   mc_heap_ignore_region_t current_region;
506
507   xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region) {
508     mc_heap_ignore_region_t new_region = NULL;
509     new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
510     new_region->address = current_region->address;
511     new_region->size = current_region->size;
512     new_region->block = current_region->block;
513     new_region->fragment = current_region->fragment;
514     xbt_dynar_push(cpy, &new_region);
515   }
516
517   return cpy;
518
519 }
520
521 static void mc_free_snapshot_ignored_data_pvoid(void* data) {
522   mc_snapshot_ignored_data_t ignored_data = (mc_snapshot_ignored_data_t) data;
523   free(ignored_data->data);
524 }
525
526 static void MC_snapshot_handle_ignore(mc_snapshot_t snapshot)
527 {
528   snapshot->ignored_data = xbt_dynar_new(sizeof(s_mc_snapshot_ignored_data_t), mc_free_snapshot_ignored_data_pvoid);
529
530   // Copy the memory:
531   unsigned int cursor = 0;
532   mc_checkpoint_ignore_region_t region;
533   xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
534     s_mc_snapshot_ignored_data_t ignored_data;
535     ignored_data.start = region->addr;
536     ignored_data.size = region->size;
537     ignored_data.data = malloc(region->size);
538     memcpy(ignored_data.data, region->addr, region->size);
539     xbt_dynar_push(snapshot->ignored_data, &ignored_data);
540   }
541
542   // Zero the memory:
543   xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
544     memset(region->addr, 0, region->size);
545   }
546
547 }
548
549 static void MC_snapshot_ignore_restore(mc_snapshot_t snapshot)
550 {
551   unsigned int cursor = 0;
552   s_mc_snapshot_ignored_data_t ignored_data;
553   xbt_dynar_foreach (snapshot->ignored_data, cursor, ignored_data) {
554     memcpy(ignored_data.start, ignored_data.data, ignored_data.size);
555   }
556 }
557
558 /** @brief Can we remove this snapshot?
559  *
560  * Some snapshots cannot be removed (yet) because we need them
561  * at this point.
562  *
563  * @param snapshot
564  */
565 int mc_important_snapshot(mc_snapshot_t snapshot)
566 {
567   // We need this snapshot in order to know which
568   // pages needs to be stored in the next snapshot.
569   // This field is only non-NULL when using soft-dirty
570   // page tracking.
571   if (snapshot == mc_model_checker->parent_snapshot)
572     return true;
573
574   return false;
575 }
576
577 mc_snapshot_t MC_take_snapshot(int num_state)
578 {
579
580   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
581   snapshot->enabled_processes = xbt_dynar_new(sizeof(int), NULL);
582   smx_process_t process;
583   xbt_swag_foreach(process, simix_global->process_list) {
584     xbt_dynar_push_as(snapshot->enabled_processes, int, (int)process->pid);
585   }
586
587   MC_snapshot_handle_ignore(snapshot);
588
589   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
590   MC_get_memory_regions(snapshot);
591   if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
592     mc_softdirty_reset();
593   }
594
595   snapshot->to_ignore = MC_take_snapshot_ignore();
596
597   if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
598     snapshot->stacks =
599         MC_take_snapshot_stacks(&snapshot);
600     if (_sg_mc_hash && snapshot->stacks != NULL) {
601       snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
602     } else {
603       snapshot->hash = 0;
604     }
605   } else {
606     snapshot->hash = 0;
607   }
608
609   MC_snapshot_ignore_restore(snapshot);
610   if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
611     mc_model_checker->parent_snapshot = snapshot;
612   }
613   return snapshot;
614 }
615
616 void MC_restore_snapshot(mc_snapshot_t snapshot)
617 {
618   mc_snapshot_t parent_snapshot = mc_model_checker->parent_snapshot;
619
620   unsigned int i;
621   for (i = 0; i < NB_REGIONS; i++) {
622     // For privatized, variables we decided it was not necessary to take the snapshot:
623     if (snapshot->regions[i])
624       MC_region_restore(snapshot->regions[i],
625         parent_snapshot ? parent_snapshot->regions[i] : NULL);
626   }
627
628 #ifdef HAVE_SMPI
629   if (snapshot->privatization_regions) {
630     // Restore the global variables of the application separately for each
631     // simulated process:
632     for (i = 0; i < smpi_process_count(); i++) {
633       if (snapshot->privatization_regions[i]) {
634         MC_region_restore(snapshot->privatization_regions[i],
635           parent_snapshot ? parent_snapshot->privatization_regions[i] : NULL);
636       }
637     }
638   }
639   if(snapshot->privatization_index >= 0) {
640     // We just rewrote the global variables.
641     // The privatisation segment SMPI thinks
642     // is mapped might be inconsistent with the segment which
643     // is really mapped in memory (kernel state).
644     // We ask politely SMPI to map the segment anyway,
645     // even if it thinks it is the current one:
646     smpi_really_switch_data_segment(snapshot->privatization_index);
647   }
648 #endif
649
650   if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
651     mc_softdirty_reset();
652   }
653
654   MC_snapshot_ignore_restore(snapshot);
655   if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
656     mc_model_checker->parent_snapshot = snapshot;
657   }
658 }
659
660 mc_snapshot_t simcall_HANDLER_mc_snapshot(smx_simcall_t simcall)
661 {
662   return MC_take_snapshot(1);
663 }
664
665 void *MC_snapshot(void)
666 {
667   return simcall_mc_snapshot();
668 }