Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : merge conflict resolved
[simgrid.git] / src / mc / mc_checkpoint.c
1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved.            */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <libgen.h>
7 #include "mc_private.h"
8
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
11                                 "Logging specific to mc_checkpoint");
12
13 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
14 static void MC_region_restore(mc_mem_region_t reg);
15 static void MC_region_destroy(mc_mem_region_t reg);
16
17 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
18
19 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
20 {
21   mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
22   new_reg->type = type;
23   new_reg->start_addr = start_addr;
24   new_reg->size = size;
25   new_reg->data = xbt_malloc0(size);
26   XBT_DEBUG("New reg data %p, start_addr %p, size %zu", new_reg->data, start_addr, size);
27   memcpy(new_reg->data, start_addr, size);
28   
29   return new_reg;
30 }
31
32 static void MC_region_restore(mc_mem_region_t reg)
33 {
34   /*FIXME: check if start_addr is still mapped, if it is not, then map it
35     before copying the data */
36   if(reg->type == 3){
37     memory_map_t maps = get_memory_map();
38     MC_UNSET_RAW_MEM;
39     unsigned int i=0;
40     s_map_region_t r;
41     while(i < maps->mapsize){
42       r = maps->regions[i];
43       if (maps->regions[i].pathname != NULL){
44         if (!memcmp(maps->regions[i].pathname, "[stack]", 7)){
45           size_t diff = (char*)reg->start_addr - (char*)r.start_addr;
46           void *segment = malloc(reg->size + diff);
47           XBT_DEBUG("Size of segment : %zu", sizeof(segment));
48           memcpy((char *)segment + diff, reg->data, reg->size);
49           memcpy(r.start_addr, segment, sizeof(segment));
50           XBT_DEBUG("Memcpy region ok");
51           break;
52         }
53       }
54       i++;
55     }
56   }else{
57     XBT_DEBUG("Memcpy : dest %p, src %p, size %zu", reg->start_addr, reg->data, reg->size);
58     memcpy(reg->start_addr, reg->data, reg->size);
59   }
60   
61   return;
62 }
63
64 static void MC_region_destroy(mc_mem_region_t reg)
65 {
66   xbt_free(reg->data);
67   xbt_free(reg);
68 }
69
70 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
71 {
72   switch(type){
73   case 0 : 
74     XBT_DEBUG("New region heap (%zu)", size);
75     break;
76   case 1 : 
77     XBT_DEBUG("New region libsimgrid (%zu)", size);
78     break;
79   case 2 : 
80     XBT_DEBUG("New region program data (%zu)", size);
81     break;
82   }
83   mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
84   snapshot->regions = xbt_realloc(snapshot->regions, (snapshot->num_reg + 1) * sizeof(mc_mem_region_t));
85   snapshot->regions[snapshot->num_reg] = new_reg;
86   snapshot->num_reg++;
87   return;
88
89
90 void MC_take_snapshot(mc_snapshot_t snapshot)
91 {
92   unsigned int i = 0;
93   s_map_region_t reg;
94   memory_map_t maps = get_memory_map();
95
96   /* Save the std heap and the writable mapped pages of libsimgrid */
97   while (i < maps->mapsize) {
98     reg = maps->regions[i];
99     if ((reg.prot & PROT_WRITE)){
100       if (maps->regions[i].pathname == NULL){
101         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
102           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
103         }
104       } else {
105         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
106           MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
107         } 
108       }
109     }
110     i++;
111   }
112
113   /* FIXME: free the memory map */
114 }
115
116 void MC_take_snapshot_liveness(mc_snapshot_t snapshot)
117 {
118   unsigned int i = 0;
119   s_map_region_t reg;
120   memory_map_t maps = get_memory_map();
121
122   for(i=0; i< snapshot->num_reg; i++){
123     MC_region_destroy(snapshot->regions[i]);
124   }
125
126   snapshot->num_reg = 0;
127
128   i = 0;
129
130   /* Save the std heap and the writable mapped pages of libsimgrid */
131   while (i < maps->mapsize) {
132     reg = maps->regions[i];
133     if ((reg.prot & PROT_WRITE)){
134       if (maps->regions[i].pathname == NULL){
135         if (reg.start_addr == std_heap){ // only save the std heap (and not the raw one)
136           MC_snapshot_add_region(snapshot, 0, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
137         }
138       } else {
139         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
140           MC_snapshot_add_region(snapshot, 1, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
141         } else {
142           if (!memcmp(basename(maps->regions[i].pathname), basename(prog_name), strlen(basename(prog_name)))){
143             MC_snapshot_add_region(snapshot, 2, reg.start_addr, (char*)reg.end_addr - (char*)reg.start_addr);
144           } 
145         }
146       }
147     }
148     i++;
149   }
150
151   /* FIXME: free the memory map */
152 }
153
154 void MC_restore_snapshot(mc_snapshot_t snapshot)
155 {
156   unsigned int i;
157   for(i=0; i < snapshot->num_reg; i++){
158     MC_region_restore(snapshot->regions[i]);
159     switch(snapshot->regions[i]->type){
160     case 0 : 
161       XBT_DEBUG("heap restored");
162       break;
163     case 1:
164       XBT_DEBUG("libsimgrid (data) restored");
165       break;
166     case 2:
167       XBT_DEBUG("data program restored");
168       break;
169     }
170
171   }
172
173 }
174
175 void MC_free_snapshot(mc_snapshot_t snapshot)
176 {
177   unsigned int i;
178   for(i=0; i < snapshot->num_reg; i++)
179     MC_region_destroy(snapshot->regions[i]);
180
181   xbt_free(snapshot);
182 }
183
184