Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8ceb44604088635708b13d5644bd1951946f5422
[simgrid.git] / src / mc / sosp / RegionSnapshot.hpp
1 /* Copyright (c) 2007-2019. The 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 #ifndef SIMGRID_MC_REGION_SNAPSHOT_HPP
7 #define SIMGRID_MC_REGION_SNAPSHOT_HPP
8
9 #include "src/mc/remote/RemotePtr.hpp"
10 #include "src/mc/sosp/ChunkedData.hpp"
11
12 #include <memory>
13 #include <vector>
14
15 namespace simgrid {
16 namespace mc {
17
18 enum class RegionType { Unknown = 0, Heap = 1, Data = 2 };
19
20 /** A copy/snapshot of a given memory region
21  *
22  *  Different types of region snapshot storage types exist:
23  *
24  *  * flat/dense snapshots are a simple copy of the region;
25  *
26  *  * sparse/per-page snapshots are snaapshots which shared
27  *    identical pages.
28  *
29  *  * privatized (SMPI global variable privatization).
30  *
31  *  This is handled with a variant based approach:
32  *
33  *  * `storage_type` identified the type of storage;
34  *
35  *  * an anonymous enum is used to distinguish the relevant types for
36  *    each type.
37  */
38 class RegionSnapshot {
39 public:
40   static const RegionType UnknownRegion = RegionType::Unknown;
41   static const RegionType HeapRegion    = RegionType::Heap;
42   static const RegionType DataRegion    = RegionType::Data;
43
44 protected:
45   RegionType region_type_                      = UnknownRegion;
46   simgrid::mc::ObjectInformation* object_info_ = nullptr;
47
48   /** @brief  Virtual address of the region in the simulated process */
49   void* start_addr_ = nullptr;
50
51   /** @brief Size of the data region in bytes */
52   std::size_t size_ = 0;
53
54   /** @brief Permanent virtual address of the region
55    *
56    * This is usually the same address as the simuilated process address.
57    * However, when using SMPI privatization of global variables,
58    * each SMPI process has its own set of global variables stored
59    * at a different virtual address. The scheduler maps those region
60    * on the region of the global variables.
61    *
62    * */
63   void* permanent_addr_ = nullptr;
64
65   ChunkedData page_numbers_;
66
67 public:
68   RegionSnapshot(RegionType type, void* start_addr, void* permanent_addr, size_t size);
69   ~RegionSnapshot()                     = default;
70   RegionSnapshot(RegionSnapshot const&) = delete;
71   RegionSnapshot& operator=(RegionSnapshot const&) = delete;
72   RegionSnapshot(RegionSnapshot&& that)
73       : region_type_(that.region_type_)
74       , object_info_(that.object_info_)
75       , start_addr_(that.start_addr_)
76       , size_(that.size_)
77       , permanent_addr_(that.permanent_addr_)
78       , page_numbers_(std::move(that.page_numbers_))
79   {
80     that.clear();
81   }
82   RegionSnapshot& operator=(RegionSnapshot&& that)
83   {
84     region_type_        = that.region_type_;
85     object_info_        = that.object_info_;
86     start_addr_         = that.start_addr_;
87     size_               = that.size_;
88     permanent_addr_     = that.permanent_addr_;
89     page_numbers_       = std::move(that.page_numbers_);
90     that.clear();
91     return *this;
92   }
93
94   // Data
95
96   void clear()
97   {
98     region_type_  = UnknownRegion;
99     page_numbers_.clear();
100     object_info_    = nullptr;
101     start_addr_     = nullptr;
102     size_           = 0;
103     permanent_addr_ = nullptr;
104   }
105
106   void clear_data()
107   {
108     page_numbers_.clear();
109   }
110
111   ChunkedData const& page_data() const { return page_numbers_; }
112
113   simgrid::mc::ObjectInformation* object_info() const { return object_info_; }
114   void object_info(simgrid::mc::ObjectInformation* info) { object_info_ = info; }
115
116   // Other getters
117
118   RemotePtr<void> start() const { return remote(start_addr_); }
119   RemotePtr<void> end() const { return remote((char*)start_addr_ + size_); }
120   RemotePtr<void> permanent_address() const { return remote(permanent_addr_); }
121   std::size_t size() const { return size_; }
122   RegionType region_type() const { return region_type_; }
123
124   bool contain(RemotePtr<void> p) const { return p >= start() && p < end(); }
125
126   /** @brief Restore a region from a snapshot */
127   void restore();
128 };
129
130 RegionSnapshot* region(RegionType type, void* start_addr, void* data_addr, std::size_t size);
131
132 } // namespace mc
133 } // namespace simgrid
134
135 #endif