Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
447a8dbb8b2b0508c0acb555ed108fa979249725
[simgrid.git] / src / mc / mc_checkpoint.c
1 #include <libgen.h>
2 #include "private.h"
3
4
5 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
6                                 "Logging specific to mc_checkpoint");
7
8 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
9 static void MC_region_restore(mc_mem_region_t reg);
10 static void MC_region_destroy(mc_mem_region_t reg);
11
12 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
13
14 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
15 {
16   mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
17   new_reg->type = type;
18   new_reg->start_addr = start_addr;
19   new_reg->size = size;
20   new_reg->data = xbt_malloc0(size);
21   memcpy(new_reg->data, start_addr, size);
22   return new_reg;
23 }
24
25 static void MC_region_restore(mc_mem_region_t reg)
26 {
27   /*FIXME: check if start_addr is still mapped, if it is not, then map it
28     before copying the data */
29   memcpy(reg->start_addr, reg->data, reg->size);
30 }
31
32 static void MC_region_destroy(mc_mem_region_t reg)
33 {
34   xbt_free(reg->data);
35   xbt_free(reg);
36 }
37
38 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
39 {
40   mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
41   snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
42   snapshot->regions[snapshot->num_reg] = new_reg;
43   snapshot->num_reg++;
44   return;
45
46
47 void MC_take_snapshot(mc_snapshot_t snapshot)
48 {
49   unsigned int i = 0;
50   s_map_region reg;
51   memory_map_t maps = get_memory_map();
52
53   /* Save the std heap and the writable mapped pages of libsimgrid */
54   while (i < maps->mapsize) {
55     reg = maps->regions[i];
56     if ((reg.prot & PROT_WRITE)){
57       if (maps->regions[i].pathname == NULL){
58         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
59           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
60         }
61       } else {
62         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
63           MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
64         } 
65       }
66     }
67     i++;
68   }
69
70   /* FIXME: free the memory map */
71 }
72
73
74 void MC_take_snapshot_liveness(mc_snapshot_t snapshot, char *prgm)
75 {
76   unsigned int i = 0;
77   s_map_region reg;
78   memory_map_t maps = get_memory_map();
79
80   /* Save the std heap and the writable mapped pages of libsimgrid */
81   while (i < maps->mapsize) {
82     reg = maps->regions[i];
83     if ((reg.prot & PROT_WRITE)){
84       if (maps->regions[i].pathname == NULL){
85         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
86           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
87         }
88       } else {
89         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
90           MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
91         } else {
92           if (!memcmp(basename(maps->regions[i].pathname), basename(prgm), strlen(basename(prgm)))){
93             MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
94           } 
95         }
96       }
97     }
98     i++;
99   }
100
101   /* FIXME: free the memory map */
102 }
103
104 void MC_restore_snapshot(mc_snapshot_t snapshot)
105 {
106   unsigned int i;
107   for(i=0; i < snapshot->num_reg; i++)
108     MC_region_restore(snapshot->regions[i]);
109 }
110
111 void MC_free_snapshot(mc_snapshot_t snapshot)
112 {
113   unsigned int i;
114   for(i=0; i < snapshot->num_reg; i++)
115     MC_region_destroy(snapshot->regions[i]);
116
117   xbt_free(snapshot);
118 }