Logo AND Algorithmique Numérique Distribuée

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