1/*
2 * Copyright (c) 2012, 2015 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Andreas Sandberg
38 */
39
40#ifndef __SIM_DRAIN_HH__
41#define __SIM_DRAIN_HH__
42
43#include <atomic>
44#include <mutex>
45#include <unordered_set>
46
47#include "base/flags.hh"
48
49class Drainable;
50
51#ifndef SWIG // SWIG doesn't support strongly typed enums
52/**
53 * Object drain/handover states
54 *
55 * An object starts out in the Running state. When the simulator
56 * prepares to take a snapshot or prepares a CPU for handover, it
57 * calls the drain() method to transfer the object into the Draining
58 * or Drained state. If any object enters the Draining state
59 * (Drainable::drain() returning >0), simulation continues until it
60 * all objects have entered the Drained state.
61 *
62 * Before resuming simulation, the simulator calls resume() to
63 * transfer the object to the Running state.
64 *
65 * \note Even though the state of an object (visible to the rest of
66 * the world through Drainable::getState()) could be used to determine
67 * if all objects have entered the Drained state, the protocol is
68 * actually a bit more elaborate. See Drainable::drain() for details.
69 */
70enum class DrainState {
71 Running, /** Running normally */
72 Draining, /** Draining buffers pending serialization/handover */
73 Drained /** Buffers drained, ready for serialization/handover */
74};
75#endif
76
77/**
78 * This class coordinates draining of a System.
79 *
80 * When draining the simulator, we need to make sure that all
81 * Drainable objects within the system have ended up in the drained
82 * state before declaring the operation to be successful. This class
83 * keeps track of how many objects are still in the process of
84 * draining. Once it determines that all objects have drained their
85 * state, it exits the simulation loop.
86 *
87 * @note A System might not be completely drained even though the
88 * DrainManager has caused the simulation loop to exit. Draining needs
89 * to be restarted until all Drainable objects declare that they don't
90 * need further simulation to be completely drained. See Drainable for
91 * more information.
92 */
93class DrainManager
94{
95 private:
96 DrainManager();
97#ifndef SWIG
98 DrainManager(DrainManager &) = delete;
99#endif
100 ~DrainManager();
101
102 public:
103 /** Get the singleton DrainManager instance */
104 static DrainManager &instance() { return _instance; }
105
106 /**
107 * Try to drain the system.
108 *
109 * Try to drain the system and return true if all objects are in a
110 * the Drained state at which point the whole simulator is in a
111 * consistent state and ready for checkpointing or CPU
112 * handover. The simulation script must continue simulating until
113 * the simulation loop returns "Finished drain", at which point
114 * this method should be called again. This cycle should continue
115 * until this method returns true.
116 *
117 * @return true if all objects were drained successfully, false if
118 * more simulation is needed.
119 */
120 bool tryDrain();
121
122 /**
123 * Resume normal simulation in a Drained system.
124 */
125 void resume();
126
127 /**
128 * Run state fixups before a checkpoint restore operation
129 *
130 * The drain state of an object isn't stored in a checkpoint since
131 * the whole system is always going to be in the Drained state
132 * when the checkpoint is created. When the checkpoint is restored
133 * at a later stage, recreated objects will be in the Running
134 * state since the state isn't stored in checkpoints. This method
135 * performs state fixups on all Drainable objects and the
136 * DrainManager itself.
137 */
138 void preCheckpointRestore();
139
140 /** Check if the system is drained */
141 bool isDrained() { return _state == DrainState::Drained; }
142
143 /** Get the simulators global drain state */
144 DrainState state() { return _state; }
145
146 /**
147 * Notify the DrainManager that a Drainable object has finished
148 * draining.
149 */
150 void signalDrainDone();
151
152 public:
153 void registerDrainable(Drainable *obj);
154 void unregisterDrainable(Drainable *obj);
155
156 private:
157 /**
158 * Thread-safe helper function to get the number of Drainable
159 * objects in a system.
160 */
161 size_t drainableCount() const;
162
163 /** Lock protecting the set of drainable objects */
164 mutable std::mutex globalLock;
165
166 /** Set of all drainable objects */
167 std::unordered_set<Drainable *> _allDrainable;
168
169 /**
170 * Number of objects still draining. This is flagged atomic since
171 * it can be manipulated by SimObjects living in different
172 * threads.
173 */
174 std::atomic_uint _count;
175
176 /** Global simulator drain state */
177 DrainState _state;
178
179 /** Singleton instance of the drain manager */
180 static DrainManager _instance;
181};
182
183/**
184 * Interface for objects that might require draining before
185 * checkpointing.
186 *
187 * An object's internal state needs to be drained when creating a
188 * checkpoint, switching between CPU models, or switching between
189 * timing models. Once the internal state has been drained from
190 * <i>all</i> objects in the simulator, the objects are serialized to
191 * disc or the configuration change takes place. The process works as
192 * follows (see simulate.py for details):
193 *
194 * <ol>
195 * <li>DrainManager::tryDrain() calls Drainable::drain() for every
196 * object in the system. Draining has completed if all of them
197 * return true. Otherwise, the drain manager keeps track of the
198 * objects that requested draining and waits for them to signal
199 * that they are done draining using the signalDrainDone() method.
200 *
201 * <li>Continue simulation. When an object has finished draining its
202 * internal state, it calls DrainManager::signalDrainDone() on the
203 * manager. The drain manager keeps track of the objects that
204 * haven't drained yet, simulation stops when the set of
205 * non-drained objects becomes empty.
206 *
207 *Check if any object still needs draining
208 * (DrainManager::tryDrain()), if so repeat the process above.
209 *
210 * <li>Serialize objects, switch CPU model, or change timing model.
211 *
212 * <li>Call DrainManager::resume(), which intern calls
213 * Drainable::drainResume() for all objects, and continue the
214 * simulation.
215 * </ol>
216 *
217 */
218class Drainable
219{
220 friend class DrainManager;
221
222 protected:
223 Drainable();
224 virtual ~Drainable();
225
226 /**
227 * Determine if an object needs draining and register a
228 * DrainManager.
229 *
230 * If the object does not need further simulation to drain
231 * internal buffers, it returns true and automatically switches to
232 * the Drained state, otherwise it switches to the Draining state.
233 *
234 * @note An object that has entered the Drained state can be
235 * disturbed by other objects in the system and consequently be
236 * being drained. These perturbations are not visible in the
237 * drain state. The simulator therefore repeats the draining
238 * process until all objects return DrainState::Drained on the
239 * first call to drain().
240 *
241 * @return DrainState::Drained if the object is ready for
242 * serialization now, DrainState::Draining if it needs further
243 * simulation.
244 */
245 virtual DrainState drain() = 0;
246
247 /**
248 * Resume execution after a successful drain.
249 */
250 virtual void drainResume() {};
251
252 /**
253 * Signal that an object is drained
254 *
255 * This method is designed to be called whenever an object enters
256 * into a state where it is ready to be drained. The method is
257 * safe to call multiple times and there is no need to check that
258 * draining has been requested before calling this method.
259 */
260 void signalDrainDone() const {
261 switch (_drainState) {
262 case DrainState::Running:
263 case DrainState::Drained:
264 return;
265 case DrainState::Draining:
266 _drainState = DrainState::Drained;
267 _drainManager.signalDrainDone();
268 return;
269 }
270 }
271
272 public:
273 /** Return the current drain state of an object. */
274 DrainState drainState() const { return _drainState; }
275
276 private:
277 /** DrainManager interface to request a drain operation */
278 DrainState dmDrain();
279 /** DrainManager interface to request a resume operation */
280 void dmDrainResume();
281
282 /** Convenience reference to the drain manager */
283 DrainManager &_drainManager;
284
285 /**
286 * Current drain state of the object. Needs to be mutable since
287 * objects need to be able to signal that they have transitioned
288 * into a Drained state even if the calling method is const.
289 */
290 mutable DrainState _drainState;
291};
292
293#endif
2 * Copyright (c) 2012, 2015 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Andreas Sandberg
38 */
39
40#ifndef __SIM_DRAIN_HH__
41#define __SIM_DRAIN_HH__
42
43#include <atomic>
44#include <mutex>
45#include <unordered_set>
46
47#include "base/flags.hh"
48
49class Drainable;
50
51#ifndef SWIG // SWIG doesn't support strongly typed enums
52/**
53 * Object drain/handover states
54 *
55 * An object starts out in the Running state. When the simulator
56 * prepares to take a snapshot or prepares a CPU for handover, it
57 * calls the drain() method to transfer the object into the Draining
58 * or Drained state. If any object enters the Draining state
59 * (Drainable::drain() returning >0), simulation continues until it
60 * all objects have entered the Drained state.
61 *
62 * Before resuming simulation, the simulator calls resume() to
63 * transfer the object to the Running state.
64 *
65 * \note Even though the state of an object (visible to the rest of
66 * the world through Drainable::getState()) could be used to determine
67 * if all objects have entered the Drained state, the protocol is
68 * actually a bit more elaborate. See Drainable::drain() for details.
69 */
70enum class DrainState {
71 Running, /** Running normally */
72 Draining, /** Draining buffers pending serialization/handover */
73 Drained /** Buffers drained, ready for serialization/handover */
74};
75#endif
76
77/**
78 * This class coordinates draining of a System.
79 *
80 * When draining the simulator, we need to make sure that all
81 * Drainable objects within the system have ended up in the drained
82 * state before declaring the operation to be successful. This class
83 * keeps track of how many objects are still in the process of
84 * draining. Once it determines that all objects have drained their
85 * state, it exits the simulation loop.
86 *
87 * @note A System might not be completely drained even though the
88 * DrainManager has caused the simulation loop to exit. Draining needs
89 * to be restarted until all Drainable objects declare that they don't
90 * need further simulation to be completely drained. See Drainable for
91 * more information.
92 */
93class DrainManager
94{
95 private:
96 DrainManager();
97#ifndef SWIG
98 DrainManager(DrainManager &) = delete;
99#endif
100 ~DrainManager();
101
102 public:
103 /** Get the singleton DrainManager instance */
104 static DrainManager &instance() { return _instance; }
105
106 /**
107 * Try to drain the system.
108 *
109 * Try to drain the system and return true if all objects are in a
110 * the Drained state at which point the whole simulator is in a
111 * consistent state and ready for checkpointing or CPU
112 * handover. The simulation script must continue simulating until
113 * the simulation loop returns "Finished drain", at which point
114 * this method should be called again. This cycle should continue
115 * until this method returns true.
116 *
117 * @return true if all objects were drained successfully, false if
118 * more simulation is needed.
119 */
120 bool tryDrain();
121
122 /**
123 * Resume normal simulation in a Drained system.
124 */
125 void resume();
126
127 /**
128 * Run state fixups before a checkpoint restore operation
129 *
130 * The drain state of an object isn't stored in a checkpoint since
131 * the whole system is always going to be in the Drained state
132 * when the checkpoint is created. When the checkpoint is restored
133 * at a later stage, recreated objects will be in the Running
134 * state since the state isn't stored in checkpoints. This method
135 * performs state fixups on all Drainable objects and the
136 * DrainManager itself.
137 */
138 void preCheckpointRestore();
139
140 /** Check if the system is drained */
141 bool isDrained() { return _state == DrainState::Drained; }
142
143 /** Get the simulators global drain state */
144 DrainState state() { return _state; }
145
146 /**
147 * Notify the DrainManager that a Drainable object has finished
148 * draining.
149 */
150 void signalDrainDone();
151
152 public:
153 void registerDrainable(Drainable *obj);
154 void unregisterDrainable(Drainable *obj);
155
156 private:
157 /**
158 * Thread-safe helper function to get the number of Drainable
159 * objects in a system.
160 */
161 size_t drainableCount() const;
162
163 /** Lock protecting the set of drainable objects */
164 mutable std::mutex globalLock;
165
166 /** Set of all drainable objects */
167 std::unordered_set<Drainable *> _allDrainable;
168
169 /**
170 * Number of objects still draining. This is flagged atomic since
171 * it can be manipulated by SimObjects living in different
172 * threads.
173 */
174 std::atomic_uint _count;
175
176 /** Global simulator drain state */
177 DrainState _state;
178
179 /** Singleton instance of the drain manager */
180 static DrainManager _instance;
181};
182
183/**
184 * Interface for objects that might require draining before
185 * checkpointing.
186 *
187 * An object's internal state needs to be drained when creating a
188 * checkpoint, switching between CPU models, or switching between
189 * timing models. Once the internal state has been drained from
190 * <i>all</i> objects in the simulator, the objects are serialized to
191 * disc or the configuration change takes place. The process works as
192 * follows (see simulate.py for details):
193 *
194 * <ol>
195 * <li>DrainManager::tryDrain() calls Drainable::drain() for every
196 * object in the system. Draining has completed if all of them
197 * return true. Otherwise, the drain manager keeps track of the
198 * objects that requested draining and waits for them to signal
199 * that they are done draining using the signalDrainDone() method.
200 *
201 * <li>Continue simulation. When an object has finished draining its
202 * internal state, it calls DrainManager::signalDrainDone() on the
203 * manager. The drain manager keeps track of the objects that
204 * haven't drained yet, simulation stops when the set of
205 * non-drained objects becomes empty.
206 *
207 *
208 * (DrainManager::tryDrain()), if so repeat the process above.
209 *
210 * <li>Serialize objects, switch CPU model, or change timing model.
211 *
212 * <li>Call DrainManager::resume(), which intern calls
213 * Drainable::drainResume() for all objects, and continue the
214 * simulation.
215 * </ol>
216 *
217 */
218class Drainable
219{
220 friend class DrainManager;
221
222 protected:
223 Drainable();
224 virtual ~Drainable();
225
226 /**
227 * Determine if an object needs draining and register a
228 * DrainManager.
229 *
230 * If the object does not need further simulation to drain
231 * internal buffers, it returns true and automatically switches to
232 * the Drained state, otherwise it switches to the Draining state.
233 *
234 * @note An object that has entered the Drained state can be
235 * disturbed by other objects in the system and consequently be
236 * being drained. These perturbations are not visible in the
237 * drain state. The simulator therefore repeats the draining
238 * process until all objects return DrainState::Drained on the
239 * first call to drain().
240 *
241 * @return DrainState::Drained if the object is ready for
242 * serialization now, DrainState::Draining if it needs further
243 * simulation.
244 */
245 virtual DrainState drain() = 0;
246
247 /**
248 * Resume execution after a successful drain.
249 */
250 virtual void drainResume() {};
251
252 /**
253 * Signal that an object is drained
254 *
255 * This method is designed to be called whenever an object enters
256 * into a state where it is ready to be drained. The method is
257 * safe to call multiple times and there is no need to check that
258 * draining has been requested before calling this method.
259 */
260 void signalDrainDone() const {
261 switch (_drainState) {
262 case DrainState::Running:
263 case DrainState::Drained:
264 return;
265 case DrainState::Draining:
266 _drainState = DrainState::Drained;
267 _drainManager.signalDrainDone();
268 return;
269 }
270 }
271
272 public:
273 /** Return the current drain state of an object. */
274 DrainState drainState() const { return _drainState; }
275
276 private:
277 /** DrainManager interface to request a drain operation */
278 DrainState dmDrain();
279 /** DrainManager interface to request a resume operation */
280 void dmDrainResume();
281
282 /** Convenience reference to the drain manager */
283 DrainManager &_drainManager;
284
285 /**
286 * Current drain state of the object. Needs to be mutable since
287 * objects need to be able to signal that they have transitioned
288 * into a Drained state even if the calling method is const.
289 */
290 mutable DrainState _drainState;
291};
292
293#endif