1 /* Copyright (c) 2008-2019. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_MC_ADDRESS_SPACE_H
7 #define SIMGRID_MC_ADDRESS_SPACE_H
9 #include "src/mc/mc_forward.hpp"
10 #include "src/mc/remote/RemotePtr.hpp"
15 /** Process index used when no process is available (SMPI privatization)
17 * The expected behavior is that if a process index is needed it will fail.
19 const int ProcessIndexMissing = -1;
21 /** Process index used when we don't care about the process index (SMPI privatization)
23 const int ProcessIndexDisabled = -2;
25 /** Constant used when any process will do (SMPI privatization)
27 * Note: This is is index of the first process.
29 const int ProcessIndexAny = 0;
31 /** Options for read operations
33 * This is a set of flags managed with bitwise operators. Only the
34 * meaningful operations are defined: addition, conversions to/from
35 * integers are not allowed.
39 constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
41 constexpr ReadOptions() : value_(0) {}
43 explicit constexpr operator bool() const { return value_ != 0; }
44 constexpr bool operator!() const { return value_ == 0; }
46 constexpr ReadOptions operator|(ReadOptions const& that) const
48 return ReadOptions(value_ | that.value_);
50 constexpr ReadOptions operator&(ReadOptions const& that) const
52 return ReadOptions(value_ & that.value_);
54 constexpr ReadOptions operator^(ReadOptions const& that) const
56 return ReadOptions(value_ ^ that.value_);
58 constexpr ReadOptions operator~() const
60 return ReadOptions(~value_);
63 ReadOptions& operator|=(ReadOptions const& that)
65 value_ |= that.value_;
68 ReadOptions& operator&=(ReadOptions const& that)
70 value_ &= that.value_;
73 ReadOptions& operator^=(ReadOptions const& that)
75 value_ &= that.value_;
79 /** Copy the data to the given buffer */
80 static constexpr ReadOptions none() { return ReadOptions(0); }
82 /** Allows to return a pointer to another buffer where the data is
83 * available instead of copying the data into the buffer
85 static constexpr ReadOptions lazy() { return ReadOptions(1); }
88 /** A given state of a given process (abstract base class)
90 * Currently, this might either be:
92 * * the current state of an existing process;
96 * In order to support SMPI privatization, the can read the memory from the
97 * context of a given SMPI process: if specified, the code reads data from the
98 * correct SMPI privatization VMA.
102 RemoteClient* process_;
105 explicit AddressSpace(RemoteClient* process) : process_(process) {}
106 virtual ~AddressSpace() = default;
108 /** The process of this address space
110 * This is where we can get debug informations, memory layout, etc.
112 simgrid::mc::RemoteClient* process() const { return process_; }
114 /** Read data from the address space
116 * @param buffer target buffer for the data
117 * @param size number of bytes to read
118 * @param address remote source address of the data
119 * @param process_index which process (used for SMPI privatization)
122 virtual const void* read_bytes(void* buffer, std::size_t size,
123 RemotePtr<void> address, int process_index = ProcessIndexAny,
124 ReadOptions options = ReadOptions::none()) const = 0;
126 /** Read a given data structure from the address space */
127 template<class T> inline
128 void read(T *buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny) const
130 this->read_bytes(buffer, sizeof(T), ptr, process_index);
133 template<class T> inline
134 void read(Remote<T>& buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny) const
136 this->read_bytes(buffer.getBuffer(), sizeof(T), ptr, process_index);
139 /** Read a given data structure from the addres space
141 * This version returns by value.
143 template<class T> inline
144 Remote<T> read(RemotePtr<T> ptr, int process_index = ProcessIndexMissing) const
147 this->read_bytes(&res, sizeof(T), ptr, process_index);
151 /** Read a string of known size */
152 std::string read_string(RemotePtr<char> address, std::size_t len) const
156 this->read_bytes(&res[0], len, address);