Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
17e5a090a91457d065956459c36af57aab6f0e2e
[simgrid.git] / src / mc / snapshot / unitTest / mc_checkpoint_unit_BOOST.cpp
1 /* Copyright (c) 2008-2018. 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 /* 
8 * TODO: 
9 * 1. clean:
10 *    Some of the '#include's should not be here
11 *
12 * 2. Add a header file for 'mc_checkpoint.cpp'
13 *    so that we can test the functions in 'mc_checkpoint.cpp'
14 */
15
16 #include <unistd.h>
17
18 #include <cstring>
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <link.h>
22
23 #ifndef WIN32
24 #include <sys/mman.h>
25 #endif
26
27 #include "src/internal_config.h"
28 #include "src/mc/mc_private.hpp"
29 #include "src/smpi/include/private.hpp"
30 #include "xbt/file.hpp"
31 #include "xbt/mmalloc.h"
32 #include "xbt/module.h"
33
34 #include "src/xbt/mmalloc/mmprivate.h"
35
36 #include "src/simix/smx_private.hpp"
37
38 #include <libunwind.h>
39 #include <libelf.h>
40
41 #include "src/mc/mc_private.hpp"
42 #include <mc/mc.h>
43
44 #include "src/mc/mc_config.hpp"
45 #include "src/mc/mc_hash.hpp"
46 #include "src/mc/mc_mmu.hpp"
47 #include "src/mc/mc_smx.hpp"
48 #include "src/mc/mc_snapshot.hpp"
49 #include "src/mc/mc_unw.hpp"
50 #include "src/mc/remote/mc_protocol.h"
51
52 #include "src/mc/RegionSnapshot.hpp"
53 #include "src/mc/ObjectInformation.hpp"
54 #include "src/mc/Frame.hpp"
55 #include "src/mc/Variable.hpp"
56
57 #ifdef SIMGRID_TEST
58
59 /* **BOOST** */
60 #define BOOST_TEST_MODULE checkpoint
61 #define BOOST_TEST_DYN_LINK
62 #include <boost/test/unit_test.hpp>
63
64 #include <stdio.h>    // perror()
65 #include <errno.h>    // perror()
66 #include <stdlib.h>   // exit()
67 #include <iostream>
68 #include <functional>
69 #include <sys/mman.h>
70 #include <cstdlib>
71 #include <cstdio>
72 #include <cerrno>
73 #include <cstring>
74
75 void add_content(void*, int, size_t);
76 size_t make_hash(char*);
77
78 int add_region_BOOST()
79 {
80   
81   RemoteClient* this_process = new RemoteClient(getpid(), -1);
82   /* read /proc/getpid()/maps into "this_process->memory_map", etc. */
83   this_process->init();
84   simgrid::mc::Snapshot* snapshot = new simgrid::mc::Snapshot(this_process, 0); // first ckpt
85   simgrid::mc::RegionType type = simgrid::mc::RegionType::Unknown;
86   std::size_t PAGESIZE = 4096;
87   std::size_t region_num = 10;
88   std::size_t size = PAGESIZE * region_num;
89   void* addr;
90   int prot = PROT_READ|PROT_WRITE;
91   int flags = MAP_PRIVATE|MAP_ANONYMOUS;
92   if (mmap(addr, size, prot, flags, -1, 0) == (void *)-1) {
93     perror("mmap()");
94     exit(1);
95   }
96   size_t hashes[region_num];
97   void* source;
98   for(int i=0; i<region_num; ++i) {
99     source = (char*)addr + i*PAGESIZE;
100     add_content(source, i, PAGESIZE);
101     add_region(i, snaphot, type, NULL, source, source, size);
102   }
103   
104   BOOST_TEST((snapshot->snapshot_regions).size == 10, "snapshot_regions size");
105   /* test retrieving regions from a snapshot*/
106   void* src1;
107   void* src2;
108   for (int i=2; i<region_num; ++) {
109   /* hashes of even regions should be equal
110      and hashes of odd regions should also be equal. */
111     src1 = (snapshot->snapshot_regions[i])->start_addr_;
112     src2 = (snapshot->snapshot_regions[i-2])->start_addr_;
113     BOOST_TEST(make_hash((char*)src1) == make_hash((char*)src2), "retrieval failed");
114   }
115
116   /* release memory before returning */
117   if (munmap(addr, size) == -1) {
118     perror("munmap()");
119     exit(1);
120   }
121   return 1; // just for testing
122 }
123
124 // stack unwinding.
125
126 BOOST_AUTO_TEST_SUITE(checkpoint)
127 BOOST_AUTO_TEST_CASE(add_region_test) {
128   BOOST_TEST(add_region_BOOST() == 1);
129 }
130 BOOST_AUTO_TEST_SUITE_END()
131
132 void add_content(void* addr, int index, size_t size) {
133   memset(addr, index%2+1, size);
134   memset(addr+size-1, 0, 1); // NULL-termination
135 }
136
137 st::hash<std::string> str_hash;
138 void make_hash(char* addr) {
139   return str_hash(string(addr));
140 }
141
142 #endif // SIMGRID_TEST