gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
eventq.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2005 The Regents of The University of Michigan
3  * Copyright (c) 2008 The Hewlett-Packard Development Company
4  * Copyright (c) 2013 Advanced Micro Devices, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met: redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer;
11  * redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution;
14  * neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * Authors: Steve Reinhardt
31  * Nathan Binkert
32  * Steve Raasch
33  */
34 
35 #include <cassert>
36 #include <iostream>
37 #include <string>
38 #include <unordered_map>
39 #include <vector>
40 
41 #include "base/misc.hh"
42 #include "base/trace.hh"
43 #include "cpu/smt.hh"
44 #include "debug/Checkpoint.hh"
45 #include "sim/core.hh"
46 #include "sim/eventq_impl.hh"
47 
48 using namespace std;
49 
51 
52 //
53 // Main Event Queues
54 //
55 // Events on these queues are processed at the *beginning* of each
56 // cycle, before the pipeline simulation is performed.
57 //
58 uint32_t numMainEventQueues = 0;
60 __thread EventQueue *_curEventQueue = NULL;
61 bool inParallelMode = false;
62 
63 EventQueue *
65 {
66  while (numMainEventQueues <= index) {
68  mainEventQueue.push_back(
69  new EventQueue(csprintf("MainEventQueue-%d", index)));
70  }
71 
72  return mainEventQueue[index];
73 }
74 
75 #ifndef NDEBUG
77 #endif
78 
80 {
81  assert(!scheduled());
82  flags = 0;
83 }
84 
85 const std::string
86 Event::name() const
87 {
88 #ifndef NDEBUG
89  return csprintf("Event_%d", instance);
90 #else
91  return csprintf("Event_%x", (uintptr_t)this);
92 #endif
93 }
94 
95 
96 Event *
98 {
99  // Either way, event will be the top element in the 'in bin' list
100  // which is the pointer we need in order to look into the list, so
101  // we need to insert that into the bin list.
102  if (!curr || *event < *curr) {
103  // Insert the event before the current list since it is in the future.
104  event->nextBin = curr;
105  event->nextInBin = NULL;
106  } else {
107  // Since we're on the correct list, we need to point to the next list
108  event->nextBin = curr->nextBin; // curr->nextBin can now become stale
109 
110  // Insert event at the top of the stack
111  event->nextInBin = curr;
112  }
113 
114  return event;
115 }
116 
117 void
119 {
120  // Deal with the head case
121  if (!head || *event <= *head) {
122  head = Event::insertBefore(event, head);
123  return;
124  }
125 
126  // Figure out either which 'in bin' list we are on, or where a new list
127  // needs to be inserted
128  Event *prev = head;
129  Event *curr = head->nextBin;
130  while (curr && *curr < *event) {
131  prev = curr;
132  curr = curr->nextBin;
133  }
134 
135  // Note: this operation may render all nextBin pointers on the
136  // prev 'in bin' list stale (except for the top one)
137  prev->nextBin = Event::insertBefore(event, curr);
138 }
139 
140 Event *
142 {
143  Event *curr = top;
144  Event *next = top->nextInBin;
145 
146  // if we removed the top item, we need to handle things specially
147  // and just remove the top item, fixing up the next bin pointer of
148  // the new top item
149  if (event == top) {
150  if (!next)
151  return top->nextBin;
152  next->nextBin = top->nextBin;
153  return next;
154  }
155 
156  // Since we already checked the current element, we're going to
157  // keep checking event against the next element.
158  while (event != next) {
159  if (!next)
160  panic("event not found!");
161 
162  curr = next;
163  next = next->nextInBin;
164  }
165 
166  // remove next from the 'in bin' list since it's what we're looking for
167  curr->nextInBin = next->nextInBin;
168  return top;
169 }
170 
171 void
173 {
174  if (head == NULL)
175  panic("event not found!");
176 
177  assert(event->queue == this);
178 
179  // deal with an event on the head's 'in bin' list (event has the same
180  // time as the head)
181  if (*head == *event) {
182  head = Event::removeItem(event, head);
183  return;
184  }
185 
186  // Find the 'in bin' list that this event belongs on
187  Event *prev = head;
188  Event *curr = head->nextBin;
189  while (curr && *curr < *event) {
190  prev = curr;
191  curr = curr->nextBin;
192  }
193 
194  if (!curr || *curr != *event)
195  panic("event not found!");
196 
197  // curr points to the top item of the the correct 'in bin' list, when
198  // we remove an item, it returns the new top item (which may be
199  // unchanged)
200  prev->nextBin = Event::removeItem(event, curr);
201 }
202 
203 Event *
205 {
206  std::lock_guard<EventQueue> lock(*this);
207  Event *event = head;
208  Event *next = head->nextInBin;
209  event->flags.clear(Event::Scheduled);
210 
211  if (next) {
212  // update the next bin pointer since it could be stale
213  next->nextBin = head->nextBin;
214 
215  // pop the stack
216  head = next;
217  } else {
218  // this was the only element on the 'in bin' list, so get rid of
219  // the 'in bin' list and point to the next bin list
220  head = head->nextBin;
221  }
222 
223  // handle action
224  if (!event->squashed()) {
225  // forward current cycle to the time when this event occurs.
226  setCurTick(event->when());
227 
228  event->process();
229  if (event->isExitEvent()) {
230  assert(!event->flags.isSet(Event::Managed) ||
231  !event->flags.isSet(Event::IsMainQueue)); // would be silly
232  return event;
233  }
234  } else {
235  event->flags.clear(Event::Squashed);
236  }
237 
238  event->release();
239 
240  return NULL;
241 }
242 
243 void
245 {
246  SERIALIZE_SCALAR(_when);
247  SERIALIZE_SCALAR(_priority);
248  short _flags = flags;
249  SERIALIZE_SCALAR(_flags);
250 }
251 
252 void
254 {
255  assert(!scheduled());
256 
257  UNSERIALIZE_SCALAR(_when);
258  UNSERIALIZE_SCALAR(_priority);
259 
260  FlagsType _flags;
261  UNSERIALIZE_SCALAR(_flags);
262 
263  // Old checkpoints had no concept of the Initialized flag
264  // so restoring from old checkpoints always fail.
265  // Events are initialized on construction but original code
266  // "flags = _flags" would just overwrite the initialization.
267  // So, read in the checkpoint flags, but then set the Initialized
268  // flag on top of it in order to avoid failures.
269  assert(initialized());
270  flags = _flags;
271  flags.set(Initialized);
272 
273  // need to see if original event was in a scheduled, unsquashed
274  // state, but don't want to restore those flags in the current
275  // object itself (since they aren't immediately true)
276  if (flags.isSet(Scheduled) && !flags.isSet(Squashed)) {
277  flags.clear(Squashed | Scheduled);
278  } else {
279  DPRINTF(Checkpoint, "Event '%s' need to be scheduled @%d\n",
280  name(), _when);
281  }
282 }
283 
284 void
286 {
287  // It's safe to call insert() directly here since this method
288  // should only be called when restoring from a checkpoint (which
289  // happens before thread creation).
290  if (event->flags.isSet(Event::Scheduled))
291  insert(event);
292 }
293 void
295 {
296  cprintf("============================================================\n");
297  cprintf("EventQueue Dump (cycle %d)\n", curTick());
298  cprintf("------------------------------------------------------------\n");
299 
300  if (empty())
301  cprintf("<No Events>\n");
302  else {
303  Event *nextBin = head;
304  while (nextBin) {
305  Event *nextInBin = nextBin;
306  while (nextInBin) {
307  nextInBin->dump();
308  nextInBin = nextInBin->nextInBin;
309  }
310 
311  nextBin = nextBin->nextBin;
312  }
313  }
314 
315  cprintf("============================================================\n");
316 }
317 
318 bool
320 {
321  std::unordered_map<long, bool> map;
322 
323  Tick time = 0;
324  short priority = 0;
325 
326  Event *nextBin = head;
327  while (nextBin) {
328  Event *nextInBin = nextBin;
329  while (nextInBin) {
330  if (nextInBin->when() < time) {
331  cprintf("time goes backwards!");
332  nextInBin->dump();
333  return false;
334  } else if (nextInBin->when() == time &&
335  nextInBin->priority() < priority) {
336  cprintf("priority inverted!");
337  nextInBin->dump();
338  return false;
339  }
340 
341  if (map[reinterpret_cast<long>(nextInBin)]) {
342  cprintf("Node already seen");
343  nextInBin->dump();
344  return false;
345  }
346  map[reinterpret_cast<long>(nextInBin)] = true;
347 
348  time = nextInBin->when();
349  priority = nextInBin->priority();
350 
351  nextInBin = nextInBin->nextInBin;
352  }
353 
354  nextBin = nextBin->nextBin;
355  }
356 
357  return true;
358 }
359 
360 Event*
362 {
363  Event* t = head;
364  head = s;
365  return t;
366 }
367 
368 void
370 {
371  for (uint32_t i = 0; i < numMainEventQueues; ++i) {
372  mainEventQueue[i]->dump();
373  }
374 }
375 
376 
377 const char *
379 {
380  return "generic";
381 }
382 
383 void
384 Event::trace(const char *action)
385 {
386  // This DPRINTF is unconditional because calls to this function
387  // are protected by an 'if (DTRACE(Event))' in the inlined Event
388  // methods.
389  //
390  // This is just a default implementation for derived classes where
391  // it's not worth doing anything special. If you want to put a
392  // more informative message in the trace, override this method on
393  // the particular subclass where you have the information that
394  // needs to be printed.
395  DPRINTFN("%s event %s @ %d\n", description(), action, when());
396 }
397 
398 void
399 Event::dump() const
400 {
401  cprintf("Event %s (%s)\n", name(), description());
402  cprintf("Flags: %#x\n", flags);
403 #ifdef EVENTQ_DEBUG
404  cprintf("Created: %d\n", whenCreated);
405 #endif
406  if (scheduled()) {
407 #ifdef EVENTQ_DEBUG
408  cprintf("Scheduled at %d\n", whenScheduled);
409 #endif
410  cprintf("Scheduled for %d, priority %d\n", when(), _priority);
411  } else {
412  cprintf("Not Scheduled\n");
413  }
414 }
415 
416 EventQueue::EventQueue(const string &n)
417  : objName(n), head(NULL), _curTick(0)
418 {
419 }
420 
421 void
423 {
424  async_queue_mutex.lock();
425  async_queue.push_back(event);
426  async_queue_mutex.unlock();
427 }
428 
429 void
431 {
432  assert(this == curEventQueue());
433  async_queue_mutex.lock();
434 
435  while (!async_queue.empty()) {
436  insert(async_queue.front());
437  async_queue.pop_front();
438  }
439 
440  async_queue_mutex.unlock();
441 }
virtual ~Event()
Definition: eventq.cc:79
#define DPRINTF(x,...)
Definition: trace.hh:212
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: eventq.cc:244
EventQueue * getEventQueue(uint32_t index)
Function for returning eventq queue for the provided index.
Definition: eventq.cc:64
Bitfield< 30, 0 > index
Defines SMT_MAX_THREADS.
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: eventq.cc:253
const std::string & name()
Definition: trace.cc:49
virtual const std::string name() const
Definition: eventq.cc:86
Bitfield< 7 > i
Definition: miscregs.hh:1378
#define panic(...)
Definition: misc.hh:153
static const FlagsType Managed
Definition: eventq.hh:102
void asyncInsert(Event *event)
Function for adding events to the async queue.
Definition: eventq.cc:422
void clear()
Definition: flags.hh:68
Event * nextBin
Definition: eventq.hh:199
bool isSet() const
Definition: flags.hh:62
bool debugVerify() const
Definition: eventq.cc:319
static Event * insertBefore(Event *event, Event *curr)
Definition: eventq.cc:97
vector< EventQueue * > mainEventQueue
Array for main event queues.
Definition: eventq.cc:59
void insert(Event *event)
Insert / remove event from the queue.
Definition: eventq.cc:118
virtual void process()=0
#define DPRINTFN(...)
Definition: trace.hh:216
STL vector class.
Definition: stl.hh:40
bool inParallelMode
Current mode of execution: parallel / serial.
Definition: eventq.cc:61
Priority priority() const
Get the event priority.
Definition: eventq.hh:400
unsigned short FlagsType
Definition: eventq.hh:95
EventQueue(const EventQueue &)
Bitfield< 31 > n
Definition: miscregs.hh:1636
static const FlagsType Squashed
Definition: eventq.hh:100
#define UNSERIALIZE_SCALAR(scalar)
Definition: serialize.hh:145
void remove(Event *event)
Definition: eventq.cc:172
Tick curTick()
The current simulated tick.
Definition: core.hh:47
Flags flags
Definition: eventq.hh:207
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:161
Queue of events sorted in time order.
Definition: eventq.hh:488
Bitfield< 4 > s
Definition: miscregs.hh:1738
static const FlagsType Scheduled
Definition: eventq.hh:101
Tick when() const
Get the time that the event is scheduled.
Definition: eventq.hh:397
uint64_t Tick
Tick count type.
Definition: types.hh:63
EventQueue * curEventQueue()
Definition: eventq.hh:84
Event * nextInBin
Definition: eventq.hh:200
void dumpMainQueue()
Definition: eventq.cc:369
int64_t Counter
Statistics counter type.
Definition: types.hh:58
std::mutex async_queue_mutex
Mutex to protect async queue.
Definition: eventq.hh:496
Bitfield< 10, 5 > event
void checkpointReschedule(Event *event)
Reschedule an event after a checkpoint.
Definition: eventq.cc:285
#define SERIALIZE_SCALAR(scalar)
Definition: serialize.hh:143
Event * replaceHead(Event *s)
function for replacing the head of the event queue, so that a different set of events can run without...
Definition: eventq.cc:361
void handleAsyncInsertions()
Function for moving events from the async_queue to the main queue.
Definition: eventq.cc:430
void dump() const
Definition: eventq.cc:294
std::ostream CheckpointOut
Definition: serialize.hh:67
uint32_t numMainEventQueues
Current number of allocated main event queues.
Definition: eventq.cc:58
Definition: eventq.hh:185
static Event * removeItem(Event *event, Event *last)
Definition: eventq.cc:141
virtual void trace(const char *action)
trace event activity
Definition: eventq.cc:384
__thread EventQueue * _curEventQueue
The current event queue for the running thread.
Definition: eventq.cc:60
void dump() const
Dump the current event data.
Definition: eventq.cc:399
Bitfield< 5 > t
Definition: miscregs.hh:1382
virtual const char * description() const
Return a C string describing the event.
Definition: eventq.cc:378
Event * serviceOne()
Definition: eventq.cc:204
std::list< Event * > async_queue
List of events added by other threads to this event queue.
Definition: eventq.hh:499
static Counter instanceCounter
Global counter to generate unique IDs for Event instances.
Definition: eventq.hh:211
Bitfield< 5 > lock
Definition: types.hh:79
EventQueue * queue
queue to which this event belongs (though it may or may not be scheduled on this queue yet) ...
Definition: eventq.hh:221
Tick simQuantum
Simulation Quantum for multiple eventq simulation.
Definition: eventq.cc:50
void cprintf(const char *format, const Args &...args)
Definition: cprintf.hh:155
static const FlagsType IsMainQueue
Definition: eventq.hh:111

Generated on Fri Jun 9 2017 13:03:51 for gem5 by doxygen 1.8.6