Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Add unit test for mc_snapshot
[simgrid.git] / src / mc / mc_snapshot.c
1 /* Copyright (c) 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 #include <stdbool.h>
8
9 #include "mc_private.h"
10 #include "mc_mmu.h"
11 #include "mc_page_store.h"
12
13 mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot)
14 {
15   for (size_t i = 0; i != NB_REGIONS; ++i) {
16     mc_mem_region_t region = snapshot->regions[i];
17     void* start = region->start_addr;
18     void* end = (char*) start + region->size;
19
20     if (addr >= start && addr < end) {
21       return region;
22     }
23   }
24
25   return NULL;
26 }
27
28 /** @brief Read memory from a snapshot region broken across fragmented pages
29  *
30  *  @param addr    Process (non-snapshot) address of the data
31  *  @param region  Snapshot memory region where the data is located
32  *  @param target  Buffer to store the value
33  *  @param size    Size of the data to read in bytes
34  *  @return Pointer where the data is located (target buffer of original location)
35  */
36 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size)
37 {
38   void* end = (char*) addr + size - 1;
39   size_t page_end = mc_page_number(NULL, end);
40   void* dest = target;
41
42   if (dest==NULL) {
43     xbt_die("Missing destination buffer for fragmented memory access");
44   }
45
46   // Read each page:
47   while (mc_page_number(NULL, addr) != page_end) {
48     void* snapshot_addr = mc_translate_address_region((uintptr_t) addr, region);
49     void* next_page = mc_page_from_number(NULL, mc_page_number(NULL, addr) + 1);
50     size_t readable = (char*) next_page - (char*) addr;
51     memcpy(dest, snapshot_addr, readable);
52     addr = (char*) addr + readable;
53     dest = (char*) dest + readable;
54     size -= readable;
55   }
56
57   // Read the end:
58   void* snapshot_addr = mc_translate_address_region((uintptr_t)addr, region);
59   memcpy(dest, snapshot_addr, size);
60
61   return target;
62 }
63
64 /** @brief Read memory from a snapshot
65  *
66  *  @param addr     Process (non-snapshot) address of the data
67  *  @param snapshot Snapshot (or NULL is no snapshot)
68  *  @param target   Buffer to store the value
69  *  @param size     Size of the data to read in bytes
70  *  @return Pointer where the data is located (target buffer of original location)
71  */
72 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, void* target, size_t size)
73 {
74   if (snapshot) {
75     mc_mem_region_t region = mc_get_snapshot_region(addr, snapshot);
76     return mc_snapshot_read_region(addr, region, target, size);
77   } else {
78     return addr;
79   }
80 }
81
82 /** Compare memory between snapshots (with known regions)
83  *
84  * @param addr1 Address in the first snapshot
85  * @param snapshot2 Region of the address in the first snapshot
86  * @param addr2 Address in the second snapshot
87  * @param snapshot2 Region of the address in the second snapshot
88  * @return same as memcmp
89  * */
90 int mc_snapshot_region_memcmp(
91   void* addr1, mc_mem_region_t region1,
92   void* addr2, mc_mem_region_t region2, size_t size)
93 {
94   // Using alloca() for large allocations may trigger stack overflow:
95   // use malloc if the buffer is too big.
96   bool stack_alloc = size < 64;
97   void* buffer1a = (region1==NULL || region1->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
98   void* buffer2a = (region2==NULL || region2->data) ? NULL : stack_alloc ? alloca(size) : malloc(size);
99   void* buffer1 = mc_snapshot_read_region(addr1, region1, buffer1a, size);
100   void* buffer2 = mc_snapshot_read_region(addr2, region2, buffer2a, size);
101   int res;
102   if (buffer1 == buffer2) {
103     res = 0;
104   } else {
105     res = memcmp(buffer1, buffer2, size);
106   }
107   if (!stack_alloc) {
108     free(buffer1a);
109     free(buffer2a);
110   }
111   return res;
112 }
113
114 /** Compare memory between snapshots
115  *
116  * @param addr1 Address in the first snapshot
117  * @param snapshot1 First snapshot
118  * @param addr2 Address in the second snapshot
119  * @param snapshot2 Second snapshot
120  * @return same as memcmp
121  * */
122 int mc_snapshot_memcp(
123   void* addr1, mc_snapshot_t snapshot1,
124   void* addr2, mc_snapshot_t snapshot2, size_t size)
125 {
126   mc_mem_region_t region1 = mc_get_snapshot_region(addr1, snapshot1);
127   mc_mem_region_t region2 = mc_get_snapshot_region(addr2, snapshot2);
128   return mc_snapshot_region_memcmp(addr1, region1, addr2, region2, size);
129 }
130
131 #ifdef SIMGRID_TEST
132
133 #include <string.h>
134 #include <stdlib.h>
135
136 #include <sys/mman.h>
137
138 #include "mc/mc_private.h"
139
140 XBT_TEST_SUITE("mc_snapshot", "Snapshots");
141
142 static inline void init_memory(void* mem, size_t size)
143 {
144   size_t hash = 5381;
145   hash = ((hash << 5) + hash) + (uintptr_t) size;
146   hash = ((hash << 5) + hash) + size;
147   char* dest = (char*) mem;
148   for (int i=0; i!=size; ++i) {
149     hash = ((hash << 5) + hash) + size;
150     dest[i] = hash & 255;
151   }
152 }
153
154 XBT_TEST_UNIT("page_region", test_snapshot_page_read, "Test page snapshots")
155 {
156   xbt_test_add("Initialisation");
157   _sg_mc_soft_dirty = 0;
158   xbt_assert(xbt_pagesize == getpagesize());
159   xbt_assert(1 << xbt_pagebits == xbt_pagesize);
160   mc_model_checker = xbt_new0(s_mc_model_checker_t, 1);
161   mc_model_checker->pages = mc_pages_store_new();
162
163   for(int n=1; n!=10; ++n) {
164
165     // Store region page(s):
166     size_t byte_size = n * xbt_pagesize;
167     void* source = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
168     xbt_assert(source!=MAP_FAILED, "Could not allocate source memory");
169
170     // Init memory and take snapshots:
171     init_memory(source, byte_size);
172     mc_mem_region_t region0 = mc_region_new_sparse(0, source, byte_size, NULL);
173     for(int i=1; i<n; i+=2) {
174       init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
175     }
176     mc_mem_region_t region = mc_region_new_sparse(0, source, byte_size, NULL);
177
178     void* destination = mmap(NULL, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
179     xbt_assert(source!=MAP_FAILED, "Could not allocate destination memory");
180
181     xbt_test_add("Reading region data for %i page(s)", n);
182     for(int j=0; j!=100; ++j) {
183       size_t offset = rand() % byte_size;
184       size_t size = rand() % (byte_size - offset);
185       void* read = mc_snapshot_read_region((char*) source+offset, region, destination, size);
186       xbt_test_assert(!memcmp((char*) source+offset, read, size), "Mismatch in mc_snapshot_read_region()");
187     }
188
189     xbt_test_add("Compare region data for %i page(s)", n);
190     for(int j=0; j!=100; ++j) {
191       size_t offset = rand() % byte_size;
192       size_t size = rand() % (byte_size - offset);
193       xbt_test_assert(!mc_snapshot_region_memcmp((char*) source+offset, NULL, (char*) source+offset, region, size),
194         "Mismatch in mc_snapshot_region_memcmp()");
195     }
196
197
198     if (n==1) {
199       xbt_test_add("Read pointer for %i page(s)", n);
200       memcpy(source, &mc_model_checker, sizeof(void*));
201       mc_mem_region_t region2 = mc_region_new_sparse(0, source, byte_size, NULL);
202       xbt_test_assert(mc_snapshot_read_pointer_region(source, region2) == mc_model_checker,
203         "Mismtach in mc_snapshot_read_pointer_region()");
204       MC_region_destroy(region2);
205     }
206
207     MC_region_destroy(region);
208     MC_region_destroy(region0);
209     munmap(destination, byte_size);
210     munmap(source, byte_size);
211   }
212
213   mc_pages_store_delete(mc_model_checker->pages);
214   xbt_free(mc_model_checker);
215   mc_model_checker = NULL;
216 }
217
218 #endif /* SIMGRID_TEST */
219