2 * Copyright (c) 2012, 2015, 2017 ARM Limited
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.
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.
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.
37 * Authors: Andreas Sandberg
40 #ifndef __SIM_DRAIN_HH__
41 #define __SIM_DRAIN_HH__
50 * Object drain/handover states
52 * An object starts out in the Running state. When the simulator
53 * prepares to take a snapshot or prepares a CPU for handover, it
54 * calls the drain() method to transfer the object into the Draining
55 * or Drained state. If any object enters the Draining state
56 * (Drainable::drain() returning >0), simulation continues until it
57 * all objects have entered the Drained state.
59 * Before resuming simulation, the simulator calls resume() to
60 * transfer the object to the Running state. This in turn results in a
61 * call to drainResume() for all Drainable objects in the
62 * simulator. New Drainable objects may be created while resuming. In
63 * such cases, the new objects will be created in the Resuming state
66 * \note Even though the state of an object (visible to the rest of
67 * the world through Drainable::getState()) could be used to determine
68 * if all objects have entered the Drained state, the protocol is
69 * actually a bit more elaborate. See Drainable::drain() for details.
71 enum class DrainState {
72 Running, /** Running normally */
73 Draining, /** Draining buffers pending serialization/handover */
74 Drained, /** Buffers drained, ready for serialization/handover */
75 Resuming, /** Transient state while the simulator is resuming */
79 * This class coordinates draining of a System.
81 * When draining the simulator, we need to make sure that all
82 * Drainable objects within the system have ended up in the drained
83 * state before declaring the operation to be successful. This class
84 * keeps track of how many objects are still in the process of
85 * draining. Once it determines that all objects have drained their
86 * state, it exits the simulation loop.
88 * @note A System might not be completely drained even though the
89 * DrainManager has caused the simulation loop to exit. Draining needs
90 * to be restarted until all Drainable objects declare that they don't
91 * need further simulation to be completely drained. See Drainable for
98 DrainManager(DrainManager &) = delete;
102 /** Get the singleton DrainManager instance */
103 static DrainManager &instance() { return _instance; }
106 * Try to drain the system.
108 * Try to drain the system and return true if all objects are in a
109 * the Drained state at which point the whole simulator is in a
110 * consistent state and ready for checkpointing or CPU
111 * handover. The simulation script must continue simulating until
112 * the simulation loop returns "Finished drain", at which point
113 * this method should be called again. This cycle should continue
114 * until this method returns true.
116 * @return true if all objects were drained successfully, false if
117 * more simulation is needed.
122 * Resume normal simulation in a Drained system.
127 * Run state fixups before a checkpoint restore operation
129 * The drain state of an object isn't stored in a checkpoint since
130 * the whole system is always going to be in the Drained state
131 * when the checkpoint is created. When the checkpoint is restored
132 * at a later stage, recreated objects will be in the Running
133 * state since the state isn't stored in checkpoints. This method
134 * performs state fixups on all Drainable objects and the
135 * DrainManager itself.
137 void preCheckpointRestore();
139 /** Check if the system is drained */
140 bool isDrained() const { return _state == DrainState::Drained; }
142 /** Get the simulators global drain state */
143 DrainState state() const { return _state; }
146 * Notify the DrainManager that a Drainable object has finished
149 void signalDrainDone();
152 void registerDrainable(Drainable *obj);
153 void unregisterDrainable(Drainable *obj);
157 * Helper function to check if all Drainable objects are in a
160 bool allInState(DrainState state) const;
163 * Thread-safe helper function to get the number of Drainable
164 * objects in a system.
166 size_t drainableCount() const;
168 /** Lock protecting the set of drainable objects */
169 mutable std::mutex globalLock;
171 /** Set of all drainable objects */
172 std::vector<Drainable *> _allDrainable;
175 * Number of objects still draining. This is flagged atomic since
176 * it can be manipulated by SimObjects living in different
179 std::atomic_uint _count;
181 /** Global simulator drain state */
184 /** Singleton instance of the drain manager */
185 static DrainManager _instance;
189 * Interface for objects that might require draining before
192 * An object's internal state needs to be drained when creating a
193 * checkpoint, switching between CPU models, or switching between
194 * timing models. Once the internal state has been drained from
195 * <i>all</i> objects in the simulator, the objects are serialized to
196 * disc or the configuration change takes place. The process works as
197 * follows (see simulate.py for details):
200 * <li>DrainManager::tryDrain() calls Drainable::drain() for every
201 * object in the system. Draining has completed if all of them
202 * return true. Otherwise, the drain manager keeps track of the
203 * objects that requested draining and waits for them to signal
204 * that they are done draining using the signalDrainDone() method.
206 * <li>Continue simulation. When an object has finished draining its
207 * internal state, it calls DrainManager::signalDrainDone() on the
208 * manager. The drain manager keeps track of the objects that
209 * haven't drained yet, simulation stops when the set of
210 * non-drained objects becomes empty.
212 * <li>Check if any object still needs draining
213 * (DrainManager::tryDrain()), if so repeat the process above.
215 * <li>Serialize objects, switch CPU model, or change timing model.
217 * <li>Call DrainManager::resume(), which in turn calls
218 * Drainable::drainResume() for all objects, and then continue the
225 friend class DrainManager;
229 virtual ~Drainable();
232 * Notify an object that it needs to drain its state.
234 * If the object does not need further simulation to drain
235 * internal buffers, it returns DrainState::Drained and
236 * automatically switches to the Drained state. If the object
237 * needs more simulation, it returns DrainState::Draining and
238 * automatically enters the Draining state. Other return values
241 * @note An object that has entered the Drained state can be
242 * disturbed by other objects in the system and consequently stop
243 * being drained. These perturbations are not visible in the drain
244 * state. The simulator therefore repeats the draining process
245 * until all objects return DrainState::Drained on the first call
248 * @return DrainState::Drained if the object is drained at this
249 * point in time, DrainState::Draining if it needs further
252 virtual DrainState drain() = 0;
255 * Resume execution after a successful drain.
257 virtual void drainResume() {};
260 * Signal that an object is drained
262 * This method is designed to be called whenever an object enters
263 * into a state where it is ready to be drained. The method is
264 * safe to call multiple times and there is no need to check that
265 * draining has been requested before calling this method.
267 void signalDrainDone() const {
268 switch (_drainState) {
269 case DrainState::Running:
270 case DrainState::Drained:
271 case DrainState::Resuming:
273 case DrainState::Draining:
274 _drainState = DrainState::Drained;
275 _drainManager.signalDrainDone();
281 /** Return the current drain state of an object. */
282 DrainState drainState() const { return _drainState; }
285 * Notify a child process of a fork.
287 * When calling fork in gem5, we need to ensure that resources
288 * shared between the parent and the child are consistent. This
289 * method is intended to be overloaded to handle that. For
290 * example, an object could use this method to re-open input files
291 * to get a separate file description with a private file offset.
293 * This method is only called in the child of the fork. The call
294 * takes place in a drained system.
296 virtual void notifyFork() {};
299 /** DrainManager interface to request a drain operation */
300 DrainState dmDrain();
301 /** DrainManager interface to request a resume operation */
302 void dmDrainResume();
304 /** Convenience reference to the drain manager */
305 DrainManager &_drainManager;
308 * Current drain state of the object. Needs to be mutable since
309 * objects need to be able to signal that they have transitioned
310 * into a Drained state even if the calling method is const.
312 mutable DrainState _drainState;