Logo AND Algorithmique Numérique Distribuée

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