Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics
[simgrid.git] / src / mc / mc_checkpoint.c
1 #include <libgen.h>
2 #include "private.h"
3
4 static mc_mem_region_t MC_region_new(void *start_addr, size_t size);
5 static void MC_region_restore(mc_mem_region_t reg);
6 static void MC_region_destroy(mc_mem_region_t reg);
7
8 static void MC_snapshot_add_region(mc_snapshot_t snapshot, void *start_addr, size_t size);
9
10 static mc_mem_region_t MC_region_new(void *start_addr, size_t size)
11 {
12   mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
13   new_reg->start_addr = start_addr;
14   new_reg->size = size;
15   new_reg->data = xbt_malloc0(size);
16   memcpy(new_reg->data, start_addr, size);
17   return new_reg;
18 }
19
20 static void MC_region_restore(mc_mem_region_t reg)
21 {
22   /*FIXME: check if start_addr is still mapped, if it is not, then map it
23     before copying the data */
24   memcpy(reg->start_addr, reg->data, reg->size);
25 }
26
27 static void MC_region_destroy(mc_mem_region_t reg)
28 {
29   xbt_free(reg->data);
30   xbt_free(reg);
31 }
32
33 static void MC_snapshot_add_region(mc_snapshot_t snapshot, void *start_addr, size_t size)
34 {
35   mc_mem_region_t new_reg = MC_region_new(start_addr, size);
36   snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
37   snapshot->regions[snapshot->num_reg] = new_reg;
38   snapshot->num_reg++;
39   return;
40
41
42 void MC_take_snapshot(mc_snapshot_t snapshot)
43 {
44   unsigned int i = 0;
45   char copy = 0;
46   s_map_region reg;
47   memory_map_t maps = get_memory_map();
48
49   /* Save the std heap and the writtable mapped pages of libsimgrid */
50     while(i < maps->mapsize
51           && (maps->regions[i].pathname == NULL
52               || memcmp(maps->regions[i].pathname, "/lib/ld", 7))){
53       reg = maps->regions[i];
54       if((reg.prot & PROT_WRITE)){
55         if(reg.start_addr == std_heap){
56           MC_snapshot_add_region(snapshot, reg.start_addr,
57                                  (char*)reg.end_addr - (char*)reg.start_addr);
58
59         }else if(copy || (reg.pathname != NULL
60                  && !memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10))){
61           MC_snapshot_add_region(snapshot, reg.start_addr,
62                                  (char*)reg.end_addr - (char*)reg.start_addr);
63           /* This will force the save of the regions in the next iterations,
64            * but we assume that ld will be found mapped and break the loop
65            * before saving a wrong region.(This is ugly I know). */
66           copy = TRUE;
67         }
68       }
69       i++;
70     }
71
72   /* FIXME: free the memory map */
73 }
74
75 void MC_restore_snapshot(mc_snapshot_t snapshot)
76 {
77   unsigned int i;
78   for(i=0; i < snapshot->num_reg; i++)
79     MC_region_restore(snapshot->regions[i]);
80 }
81
82 void MC_free_snapshot(mc_snapshot_t snapshot)
83 {
84   unsigned int i;
85   for(i=0; i < snapshot->num_reg; i++)
86     MC_region_destroy(snapshot->regions[i]);
87
88   xbt_free(snapshot);
89 }