Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b194a8078be95e822dc67e581c6fc94b68fbc9ea
[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, where identical pages are stored only once */
21 class RegionSnapshot {
22 public:
23   static const RegionType UnknownRegion = RegionType::Unknown;
24   static const RegionType HeapRegion    = RegionType::Heap;
25   static const RegionType DataRegion    = RegionType::Data;
26
27 protected:
28   RegionType region_type_                      = UnknownRegion;
29   simgrid::mc::ObjectInformation* object_info_ = nullptr;
30
31   /** @brief  Virtual address of the region in the simulated process */
32   void* start_addr_ = nullptr;
33
34   /** @brief Size of the data region in bytes */
35   std::size_t size_ = 0;
36
37   /** @brief Permanent virtual address of the region
38    *
39    * This is usually the same address as the simuilated process address.
40    * However, when using SMPI privatization of global variables,
41    * each SMPI process has its own set of global variables stored
42    * at a different virtual address. The scheduler maps those region
43    * on the region of the global variables.
44    *
45    * */
46   void* permanent_addr_ = nullptr;
47
48   ChunkedData page_numbers_;
49
50 public:
51   RegionSnapshot(RegionType type, void* start_addr, void* permanent_addr, size_t size);
52   ~RegionSnapshot()                     = default;
53   RegionSnapshot(RegionSnapshot const&) = delete;
54   RegionSnapshot& operator=(RegionSnapshot const&) = delete;
55   RegionSnapshot(RegionSnapshot&& that)
56       : region_type_(that.region_type_)
57       , object_info_(that.object_info_)
58       , start_addr_(that.start_addr_)
59       , size_(that.size_)
60       , permanent_addr_(that.permanent_addr_)
61       , page_numbers_(std::move(that.page_numbers_))
62   {
63     that.clear();
64   }
65   RegionSnapshot& operator=(RegionSnapshot&& that)
66   {
67     region_type_        = that.region_type_;
68     object_info_        = that.object_info_;
69     start_addr_         = that.start_addr_;
70     size_               = that.size_;
71     permanent_addr_     = that.permanent_addr_;
72     page_numbers_       = std::move(that.page_numbers_);
73     that.clear();
74     return *this;
75   }
76
77   // Data
78
79   void clear()
80   {
81     region_type_  = UnknownRegion;
82     page_numbers_.clear();
83     object_info_    = nullptr;
84     start_addr_     = nullptr;
85     size_           = 0;
86     permanent_addr_ = nullptr;
87   }
88
89   void clear_data()
90   {
91     page_numbers_.clear();
92   }
93
94   ChunkedData const& page_data() const { return page_numbers_; }
95
96   simgrid::mc::ObjectInformation* object_info() const { return object_info_; }
97   void object_info(simgrid::mc::ObjectInformation* info) { object_info_ = info; }
98
99   // Other getters
100
101   RemotePtr<void> start() const { return remote(start_addr_); }
102   RemotePtr<void> end() const { return remote((char*)start_addr_ + size_); }
103   RemotePtr<void> permanent_address() const { return remote(permanent_addr_); }
104   std::size_t size() const { return size_; }
105   RegionType region_type() const { return region_type_; }
106
107   bool contain(RemotePtr<void> p) const { return p >= start() && p < end(); }
108
109   /** @brief Restore a region from a snapshot */
110   void restore();
111 };
112
113 } // namespace mc
114 } // namespace simgrid
115
116 #endif