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.
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.
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.
30 * Authors: Steve Reinhardt
40 #include "base/hashmap.hh"
41 #include "base/misc.hh"
42 #include "base/trace.hh"
44 #include "debug/Config.hh"
45 #include "sim/core.hh"
46 #include "sim/eventq_impl.hh"
55 // Events on these queues are processed at the *beginning* of each
56 // cycle, before the pipeline simulation is performed.
58 uint32_t numMainEventQueues
= 0;
59 vector
<EventQueue
*> mainEventQueue
;
60 __thread EventQueue
*_curEventQueue
= NULL
;
61 bool inParallelMode
= false;
64 getEventQueue(uint32_t index
)
66 while (numMainEventQueues
<= index
) {
68 mainEventQueue
.push_back(
69 new EventQueue(csprintf("MainEventQueue-%d", index
)));
72 return mainEventQueue
[index
];
76 Counter
Event::instanceCounter
= 0;
89 return csprintf("Event_%d", instance
);
91 return csprintf("Event_%x", (uintptr_t)this);
97 Event::insertBefore(Event
*event
, Event
*curr
)
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
;
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
110 // Insert event at the top of the stack
111 event
->nextInBin
= curr
;
118 EventQueue::insert(Event
*event
)
120 // Deal with the head case
121 if (!head
|| *event
<= *head
) {
122 head
= Event::insertBefore(event
, head
);
126 // Figure out either which 'in bin' list we are on, or where a new list
127 // needs to be inserted
129 Event
*curr
= head
->nextBin
;
130 while (curr
&& *curr
< *event
) {
132 curr
= curr
->nextBin
;
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
);
141 Event::removeItem(Event
*event
, Event
*top
)
144 Event
*next
= top
->nextInBin
;
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
152 next
->nextBin
= top
->nextBin
;
156 // Since we already checked the current element, we're going to
157 // keep checking event against the next element.
158 while (event
!= next
) {
160 panic("event not found!");
163 next
= next
->nextInBin
;
166 // remove next from the 'in bin' list since it's what we're looking for
167 curr
->nextInBin
= next
->nextInBin
;
172 EventQueue::remove(Event
*event
)
175 panic("event not found!");
177 assert(event
->queue
== this);
179 // deal with an event on the head's 'in bin' list (event has the same
181 if (*head
== *event
) {
182 head
= Event::removeItem(event
, head
);
186 // Find the 'in bin' list that this event belongs on
188 Event
*curr
= head
->nextBin
;
189 while (curr
&& *curr
< *event
) {
191 curr
= curr
->nextBin
;
194 if (!curr
|| *curr
!= *event
)
195 panic("event not found!");
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
200 prev
->nextBin
= Event::removeItem(event
, curr
);
204 EventQueue::serviceOne()
206 std::lock_guard
<EventQueue
> lock(*this);
208 Event
*next
= head
->nextInBin
;
209 event
->flags
.clear(Event::Scheduled
);
212 // update the next bin pointer since it could be stale
213 next
->nextBin
= head
->nextBin
;
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
;
224 if (!event
->squashed()) {
225 // forward current cycle to the time when this event occurs.
226 setCurTick(event
->when());
229 if (event
->isExitEvent()) {
230 assert(!event
->flags
.isSet(Event::AutoDelete
) ||
231 !event
->flags
.isSet(Event::IsMainQueue
)); // would be silly
235 event
->flags
.clear(Event::Squashed
);
238 if (event
->flags
.isSet(Event::AutoDelete
) && !event
->scheduled())
245 Event::serialize(std::ostream
&os
)
247 SERIALIZE_SCALAR(_when
);
248 SERIALIZE_SCALAR(_priority
);
249 short _flags
= flags
;
250 SERIALIZE_SCALAR(_flags
);
254 Event::unserialize(Checkpoint
*cp
, const string
§ion
)
259 Event::unserialize(Checkpoint
*cp
, const string
§ion
, EventQueue
*eventq
)
262 eventq
->deschedule(this);
264 UNSERIALIZE_SCALAR(_when
);
265 UNSERIALIZE_SCALAR(_priority
);
268 UNSERIALIZE_SCALAR(_flags
);
270 // Old checkpoints had no concept of the Initialized flag
271 // so restoring from old checkpoints always fail.
272 // Events are initialized on construction but original code
273 // "flags = _flags" would just overwrite the initialization.
274 // So, read in the checkpoint flags, but then set the Initialized
275 // flag on top of it in order to avoid failures.
276 assert(initialized());
278 flags
.set(Initialized
);
280 // need to see if original event was in a scheduled, unsquashed
281 // state, but don't want to restore those flags in the current
282 // object itself (since they aren't immediately true)
283 bool wasScheduled
= flags
.isSet(Scheduled
) && !flags
.isSet(Squashed
);
284 flags
.clear(Squashed
| Scheduled
);
287 DPRINTF(Config
, "rescheduling at %d\n", _when
);
288 eventq
->schedule(this, _when
);
293 EventQueue::serialize(ostream
&os
)
295 std::list
<Event
*> eventPtrs
;
298 Event
*nextBin
= head
;
300 Event
*nextInBin
= nextBin
;
303 if (nextInBin
->flags
.isSet(Event::AutoSerialize
)) {
304 eventPtrs
.push_back(nextInBin
);
305 paramOut(os
, csprintf("event%d", numEvents
++),
308 nextInBin
= nextInBin
->nextInBin
;
311 nextBin
= nextBin
->nextBin
;
314 SERIALIZE_SCALAR(numEvents
);
316 for (std::list
<Event
*>::iterator it
= eventPtrs
.begin();
317 it
!= eventPtrs
.end(); ++it
) {
319 (*it
)->serialize(os
);
324 EventQueue::unserialize(Checkpoint
*cp
, const std::string
§ion
)
327 UNSERIALIZE_SCALAR(numEvents
);
329 std::string eventName
;
330 for (int i
= 0; i
< numEvents
; i
++) {
331 // get the pointer value associated with the event
332 paramIn(cp
, section
, csprintf("event%d", i
), eventName
);
334 // create the event based on its pointer value
335 Serializable::create(cp
, eventName
);
340 EventQueue::dump() const
342 cprintf("============================================================\n");
343 cprintf("EventQueue Dump (cycle %d)\n", curTick());
344 cprintf("------------------------------------------------------------\n");
347 cprintf("<No Events>\n");
349 Event
*nextBin
= head
;
351 Event
*nextInBin
= nextBin
;
354 nextInBin
= nextInBin
->nextInBin
;
357 nextBin
= nextBin
->nextBin
;
361 cprintf("============================================================\n");
365 EventQueue::debugVerify() const
367 m5::hash_map
<long, bool> map
;
372 Event
*nextBin
= head
;
374 Event
*nextInBin
= nextBin
;
376 if (nextInBin
->when() < time
) {
377 cprintf("time goes backwards!");
380 } else if (nextInBin
->when() == time
&&
381 nextInBin
->priority() < priority
) {
382 cprintf("priority inverted!");
387 if (map
[reinterpret_cast<long>(nextInBin
)]) {
388 cprintf("Node already seen");
392 map
[reinterpret_cast<long>(nextInBin
)] = true;
394 time
= nextInBin
->when();
395 priority
= nextInBin
->priority();
397 nextInBin
= nextInBin
->nextInBin
;
400 nextBin
= nextBin
->nextBin
;
407 EventQueue::replaceHead(Event
* s
)
417 for (uint32_t i
= 0; i
< numMainEventQueues
; ++i
) {
418 mainEventQueue
[i
]->dump();
424 Event::description() const
430 Event::trace(const char *action
)
432 // This DPRINTF is unconditional because calls to this function
433 // are protected by an 'if (DTRACE(Event))' in the inlined Event
436 // This is just a default implementation for derived classes where
437 // it's not worth doing anything special. If you want to put a
438 // more informative message in the trace, override this method on
439 // the particular subclass where you have the information that
440 // needs to be printed.
441 DPRINTFN("%s event %s @ %d\n", description(), action
, when());
447 cprintf("Event %s (%s)\n", name(), description());
448 cprintf("Flags: %#x\n", flags
);
450 cprintf("Created: %d\n", whenCreated
);
454 cprintf("Scheduled at %d\n", whenScheduled
);
456 cprintf("Scheduled for %d, priority %d\n", when(), _priority
);
458 cprintf("Not Scheduled\n");
462 EventQueue::EventQueue(const string
&n
)
463 : objName(n
), head(NULL
), _curTick(0),
464 async_queue_mutex(new std::mutex())
469 EventQueue::asyncInsert(Event
*event
)
471 async_queue_mutex
->lock();
472 async_queue
.push_back(event
);
473 async_queue_mutex
->unlock();
477 EventQueue::handleAsyncInsertions()
479 assert(this == curEventQueue());
480 async_queue_mutex
->lock();
482 while (!async_queue
.empty()) {
483 insert(async_queue
.front());
484 async_queue
.pop_front();
487 async_queue_mutex
->unlock();