gem5  v21.1.0.2
drain.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 2015, 2017 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 
38 #include "sim/drain.hh"
39 
40 #include <algorithm>
41 
42 #include "base/logging.hh"
43 #include "base/named.hh"
44 #include "base/trace.hh"
45 #include "debug/Drain.hh"
46 #include "sim/sim_exit.hh"
47 
48 namespace gem5
49 {
50 
51 DrainManager DrainManager::_instance;
52 
54  : _count(0),
55  _state(DrainState::Running)
56 {
57 }
58 
60 {
61 }
62 
63 bool
65 {
67  "Trying to drain a drained system\n");
68 
69  panic_if(_count != 0,
70  "Drain counter must be zero at the start of a drain cycle\n");
71 
72  DPRINTF(Drain, "Trying to drain %u objects.\n", drainableCount());
74  for (auto *obj : _allDrainable) {
75  DrainState status = obj->dmDrain();
76  if (debug::Drain && status != DrainState::Drained) {
77  Named *temp = dynamic_cast<Named*>(obj);
78  if (temp)
79  DPRINTF(Drain, "Failed to drain %s\n", temp->name());
80  }
81  _count += status == DrainState::Drained ? 0 : 1;
82  }
83 
84  if (_count == 0) {
85  DPRINTF(Drain, "Drain done.\n");
87  return true;
88  } else {
89  DPRINTF(Drain, "Need another drain cycle. %u/%u objects not ready.\n",
91  return false;
92  }
93 }
94 
95 void
97 {
99  "Trying to resume a system that is already running\n");
100 
102  "Resuming a system that isn't fully drained, this is untested and "
103  "likely to break\n");
104 
106  "Resuming a system that is already trying to resume. This should "
107  "never happen.\n");
108 
109  panic_if(_count != 0,
110  "Resume called in the middle of a drain cycle. %u objects "
111  "left to drain.\n", _count);
112 
113  // At this point in time the DrainManager and all objects will be
114  // in the the Drained state. New objects (i.e., objects created
115  // while resuming) will inherit the Resuming state from the
116  // DrainManager, which means we have to resume objects until all
117  // objects are in the Running state.
119 
120  do {
121  DPRINTF(Drain, "Resuming %u objects.\n", drainableCount());
122  for (auto *obj : _allDrainable) {
123  if (obj->drainState() != DrainState::Running) {
124  assert(obj->drainState() == DrainState::Drained ||
125  obj->drainState() == DrainState::Resuming);
126  obj->dmDrainResume();
127  }
128  }
129  } while (!allInState(DrainState::Running));
130 
132 }
133 
134 void
136 {
138  "preCheckpointRestore() called on a system that isn't in the "
139  "Running state.\n");
140 
141  DPRINTF(Drain, "Applying pre-restore fixes to %u objects.\n",
142  drainableCount());
144  for (auto *obj : _allDrainable)
145  obj->_drainState = DrainState::Drained;
146 }
147 
148 void
150 {
151  assert(_count > 0);
152  if (--_count == 0) {
153  DPRINTF(Drain, "All %u objects drained..\n", drainableCount());
154  exitSimLoop("Finished drain", 0);
155  }
156 }
157 
158 
159 void
161 {
162  std::lock_guard<std::mutex> lock(globalLock);
163  assert(std::find(_allDrainable.begin(), _allDrainable.end(), obj) ==
164  _allDrainable.end());
165  _allDrainable.push_back(obj);
166 }
167 
168 void
170 {
171  std::lock_guard<std::mutex> lock(globalLock);
172  auto o = std::find(_allDrainable.begin(), _allDrainable.end(), obj);
173  assert(o != _allDrainable.end());
174  _allDrainable.erase(o);
175 }
176 
177 bool
179 {
180  for (const auto *obj : _allDrainable) {
181  if (obj->drainState() != state)
182  return false;
183  }
184 
185  return true;
186 }
187 
188 size_t
190 {
191  std::lock_guard<std::mutex> lock(globalLock);
192  return _allDrainable.size();
193 }
194 
195 
196 
198  : _drainManager(DrainManager::instance()),
199  _drainState(_drainManager.state())
200 {
202 }
203 
205 {
207 }
208 
211 {
213  _drainState = drain();
214  assert(_drainState == DrainState::Draining ||
216 
217  return _drainState;
218 }
219 
220 void
222 {
225  "Trying to resume an object that hasn't been drained\n");
226 
228  drainResume();
229 }
230 
231 } // namespace gem5
gem5::Drainable::_drainState
DrainState _drainState
Current drain state of the object.
Definition: drain.hh:360
gem5::DrainState::Resuming
@ Resuming
Transient state while the simulator is resuming.
gem5::DrainManager::_allDrainable
std::vector< Drainable * > _allDrainable
Set of all drainable objects.
Definition: drain.hh:183
named.hh
gem5::DrainManager::_count
std::atomic_uint _count
Number of objects still draining.
Definition: drain.hh:190
gem5::Drainable::~Drainable
virtual ~Drainable()
Definition: drain.cc:204
gem5::X86ISA::lock
Bitfield< 5 > lock
Definition: types.hh:82
gem5::DrainManager::unregisterDrainable
void unregisterDrainable(Drainable *obj)
Definition: drain.cc:169
gem5::Drainable::Drainable
Drainable()
Definition: drain.cc:197
gem5::DrainManager::drainableCount
size_t drainableCount() const
Thread-safe helper function to get the number of Drainable objects in a system.
Definition: drain.cc:189
gem5::DrainManager::_state
DrainState _state
Global simulator drain state.
Definition: drain.hh:193
gem5::DrainManager::allInState
bool allInState(DrainState state) const
Helper function to check if all Drainable objects are in a specific state.
Definition: drain.cc:178
sim_exit.hh
gem5::exitSimLoop
void exitSimLoop(const std::string &message, int exit_code, Tick when, Tick repeat, bool serialize)
Schedule an event to exit the simulation loop (returning to Python) at the end of the current cycle (...
Definition: sim_events.cc:88
gem5::Named
Interface for things with names.
Definition: named.hh:38
gem5::DrainManager::preCheckpointRestore
void preCheckpointRestore()
Run state fixups before a checkpoint restore operation.
Definition: drain.cc:135
gem5::DrainState
DrainState
Object drain/handover states.
Definition: drain.hh:74
gem5::Drainable::dmDrainResume
void dmDrainResume()
DrainManager interface to request a resume operation.
Definition: drain.cc:221
gem5::Named::name
virtual std::string name() const
Definition: named.hh:47
gem5::DrainManager::~DrainManager
~DrainManager()
Definition: drain.cc:59
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
gem5::Drainable::dmDrain
DrainState dmDrain()
DrainManager interface to request a drain operation.
Definition: drain.cc:210
gem5::Drainable::_drainManager
DrainManager & _drainManager
Convenience reference to the drain manager.
Definition: drain.hh:353
gem5::DrainManager
Definition: drain.hh:82
gem5::DrainState::Drained
@ Drained
Buffers drained, ready for serialization/handover.
gem5::DrainManager::tryDrain
bool tryDrain()
Try to drain the system.
Definition: drain.cc:64
warn_if
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Definition: logging.hh:272
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:203
gem5::DrainManager::resume
void resume()
Resume normal simulation in a Drained system.
Definition: drain.cc:96
gem5::Drainable::drainResume
virtual void drainResume()
Resume execution after a successful drain.
Definition: drain.hh:293
gem5::Drainable
Interface for objects that might require draining before checkpointing.
Definition: drain.hh:234
logging.hh
gem5::DrainManager::state
DrainState state() const
Get the simulators global drain state.
Definition: drain.hh:152
drain.hh
gem5::DrainManager::signalDrainDone
void signalDrainDone()
Notify the DrainManager that a Drainable object has finished draining.
Definition: drain.cc:149
trace.hh
gem5::DrainState::Running
@ Running
Running normally.
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::DrainManager::_instance
static DrainManager _instance
Singleton instance of the drain manager.
Definition: drain.hh:196
gem5::DrainManager::registerDrainable
void registerDrainable(Drainable *obj)
Definition: drain.cc:160
gem5::Drainable::drain
virtual DrainState drain()=0
Draining is the process of clearing out the states of SimObjects.These are the SimObjects that are pa...
gem5::DrainState::Draining
@ Draining
Draining buffers pending serialization/handover.
gem5::ArmISA::status
Bitfield< 5, 0 > status
Definition: misc_types.hh:422
gem5::DrainManager::DrainManager
DrainManager()
Definition: drain.cc:53
gem5::DrainManager::globalLock
std::mutex globalLock
Lock protecting the set of drainable objects.
Definition: drain.hh:180

Generated on Tue Sep 21 2021 12:25:47 for gem5 by doxygen 1.8.17