eventq.cc revision 10412
112600Sodanrc@yahoo.com.br/*
212600Sodanrc@yahoo.com.br * Copyright (c) 2000-2005 The Regents of The University of Michigan
312600Sodanrc@yahoo.com.br * Copyright (c) 2008 The Hewlett-Packard Development Company
412600Sodanrc@yahoo.com.br * Copyright (c) 2013 Advanced Micro Devices, Inc.
512600Sodanrc@yahoo.com.br * All rights reserved.
612600Sodanrc@yahoo.com.br *
712600Sodanrc@yahoo.com.br * Redistribution and use in source and binary forms, with or without
812600Sodanrc@yahoo.com.br * modification, are permitted provided that the following conditions are
912600Sodanrc@yahoo.com.br * met: redistributions of source code must retain the above copyright
1012600Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer;
1112600Sodanrc@yahoo.com.br * redistributions in binary form must reproduce the above copyright
1212600Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer in the
1312600Sodanrc@yahoo.com.br * documentation and/or other materials provided with the distribution;
1412600Sodanrc@yahoo.com.br * neither the name of the copyright holders nor the names of its
1512600Sodanrc@yahoo.com.br * contributors may be used to endorse or promote products derived from
1612600Sodanrc@yahoo.com.br * this software without specific prior written permission.
1712600Sodanrc@yahoo.com.br *
1812600Sodanrc@yahoo.com.br * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1912600Sodanrc@yahoo.com.br * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2012600Sodanrc@yahoo.com.br * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2112600Sodanrc@yahoo.com.br * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2212600Sodanrc@yahoo.com.br * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2312600Sodanrc@yahoo.com.br * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2412600Sodanrc@yahoo.com.br * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2512600Sodanrc@yahoo.com.br * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2612600Sodanrc@yahoo.com.br * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2712600Sodanrc@yahoo.com.br * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2812600Sodanrc@yahoo.com.br * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2912600Sodanrc@yahoo.com.br *
3012600Sodanrc@yahoo.com.br * Authors: Steve Reinhardt
3112600Sodanrc@yahoo.com.br *          Nathan Binkert
3212600Sodanrc@yahoo.com.br *          Steve Raasch
3312600Sodanrc@yahoo.com.br */
3412600Sodanrc@yahoo.com.br
3512600Sodanrc@yahoo.com.br#include <cassert>
3612600Sodanrc@yahoo.com.br#include <iostream>
3712600Sodanrc@yahoo.com.br#include <string>
3812607Sodanrc@yahoo.com.br#include <vector>
3912607Sodanrc@yahoo.com.br
4012607Sodanrc@yahoo.com.br#include "base/hashmap.hh"
4112607Sodanrc@yahoo.com.br#include "base/misc.hh"
4212607Sodanrc@yahoo.com.br#include "base/trace.hh"
4312628Sodanrc@yahoo.com.br#include "cpu/smt.hh"
4412628Sodanrc@yahoo.com.br#include "debug/Config.hh"
4512628Sodanrc@yahoo.com.br#include "sim/core.hh"
4612628Sodanrc@yahoo.com.br#include "sim/eventq_impl.hh"
4712628Sodanrc@yahoo.com.br
4812600Sodanrc@yahoo.com.brusing namespace std;
4912600Sodanrc@yahoo.com.br
5012600Sodanrc@yahoo.com.brTick simQuantum = 0;
5112600Sodanrc@yahoo.com.br
5212600Sodanrc@yahoo.com.br//
5312601Sodanrc@yahoo.com.br// Main Event Queues
5412601Sodanrc@yahoo.com.br//
5512601Sodanrc@yahoo.com.br// Events on these queues are processed at the *beginning* of each
5612601Sodanrc@yahoo.com.br// cycle, before the pipeline simulation is performed.
5712601Sodanrc@yahoo.com.br//
5812600Sodanrc@yahoo.com.bruint32_t numMainEventQueues = 0;
5912600Sodanrc@yahoo.com.brvector<EventQueue *> mainEventQueue;
6012600Sodanrc@yahoo.com.br__thread EventQueue *_curEventQueue = NULL;
6112600Sodanrc@yahoo.com.brbool inParallelMode = false;
6212626Sodanrc@yahoo.com.br
6312626Sodanrc@yahoo.com.brEventQueue *
6412626Sodanrc@yahoo.com.brgetEventQueue(uint32_t index)
6512626Sodanrc@yahoo.com.br{
6612626Sodanrc@yahoo.com.br    while (numMainEventQueues <= index) {
6712626Sodanrc@yahoo.com.br        numMainEventQueues++;
6812626Sodanrc@yahoo.com.br        mainEventQueue.push_back(
6912626Sodanrc@yahoo.com.br            new EventQueue(csprintf("MainEventQueue-%d", index)));
7012626Sodanrc@yahoo.com.br    }
7112626Sodanrc@yahoo.com.br
7212627Sodanrc@yahoo.com.br    return mainEventQueue[index];
7312627Sodanrc@yahoo.com.br}
7412627Sodanrc@yahoo.com.br
75#ifndef NDEBUG
76Counter Event::instanceCounter = 0;
77#endif
78
79Event::~Event()
80{
81    assert(!scheduled());
82    flags = 0;
83}
84
85const std::string
86Event::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
96Event *
97Event::insertBefore(Event *event, Event *curr)
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
117void
118EventQueue::insert(Event *event)
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
140Event *
141Event::removeItem(Event *event, Event *top)
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
171void
172EventQueue::remove(Event *event)
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
203Event *
204EventQueue::serviceOne()
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::AutoDelete) ||
231                   !event->flags.isSet(Event::IsMainQueue)); // would be silly
232            return event;
233        }
234    } else {
235        event->flags.clear(Event::Squashed);
236    }
237
238    if (event->flags.isSet(Event::AutoDelete) && !event->scheduled())
239        delete event;
240
241    return NULL;
242}
243
244void
245Event::serialize(std::ostream &os)
246{
247    SERIALIZE_SCALAR(_when);
248    SERIALIZE_SCALAR(_priority);
249    short _flags = flags;
250    SERIALIZE_SCALAR(_flags);
251}
252
253void
254Event::unserialize(Checkpoint *cp, const string &section)
255{
256}
257
258void
259Event::unserialize(Checkpoint *cp, const string &section, EventQueue *eventq)
260{
261    if (scheduled())
262        eventq->deschedule(this);
263
264    UNSERIALIZE_SCALAR(_when);
265    UNSERIALIZE_SCALAR(_priority);
266
267    short _flags;
268    UNSERIALIZE_SCALAR(_flags);
269
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());
277    flags = _flags;
278    flags.set(Initialized);
279
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);
285
286    if (wasScheduled) {
287        DPRINTF(Config, "rescheduling at %d\n", _when);
288        eventq->schedule(this, _when);
289    }
290}
291
292void
293EventQueue::serialize(ostream &os)
294{
295    std::list<Event *> eventPtrs;
296
297    int numEvents = 0;
298    Event *nextBin = head;
299    while (nextBin) {
300        Event *nextInBin = nextBin;
301
302        while (nextInBin) {
303            if (nextInBin->flags.isSet(Event::AutoSerialize)) {
304                eventPtrs.push_back(nextInBin);
305                paramOut(os, csprintf("event%d", numEvents++),
306                         nextInBin->name());
307            }
308            nextInBin = nextInBin->nextInBin;
309        }
310
311        nextBin = nextBin->nextBin;
312    }
313
314    SERIALIZE_SCALAR(numEvents);
315
316    for (std::list<Event *>::iterator it = eventPtrs.begin();
317         it != eventPtrs.end(); ++it) {
318        (*it)->nameOut(os);
319        (*it)->serialize(os);
320    }
321}
322
323void
324EventQueue::unserialize(Checkpoint *cp, const std::string &section)
325{
326    int numEvents;
327    UNSERIALIZE_SCALAR(numEvents);
328
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);
333
334        // create the event based on its pointer value
335        Serializable::create(cp, eventName);
336    }
337}
338
339void
340EventQueue::dump() const
341{
342    cprintf("============================================================\n");
343    cprintf("EventQueue Dump  (cycle %d)\n", curTick());
344    cprintf("------------------------------------------------------------\n");
345
346    if (empty())
347        cprintf("<No Events>\n");
348    else {
349        Event *nextBin = head;
350        while (nextBin) {
351            Event *nextInBin = nextBin;
352            while (nextInBin) {
353                nextInBin->dump();
354                nextInBin = nextInBin->nextInBin;
355            }
356
357            nextBin = nextBin->nextBin;
358        }
359    }
360
361    cprintf("============================================================\n");
362}
363
364bool
365EventQueue::debugVerify() const
366{
367    m5::hash_map<long, bool> map;
368
369    Tick time = 0;
370    short priority = 0;
371
372    Event *nextBin = head;
373    while (nextBin) {
374        Event *nextInBin = nextBin;
375        while (nextInBin) {
376            if (nextInBin->when() < time) {
377                cprintf("time goes backwards!");
378                nextInBin->dump();
379                return false;
380            } else if (nextInBin->when() == time &&
381                       nextInBin->priority() < priority) {
382                cprintf("priority inverted!");
383                nextInBin->dump();
384                return false;
385            }
386
387            if (map[reinterpret_cast<long>(nextInBin)]) {
388                cprintf("Node already seen");
389                nextInBin->dump();
390                return false;
391            }
392            map[reinterpret_cast<long>(nextInBin)] = true;
393
394            time = nextInBin->when();
395            priority = nextInBin->priority();
396
397            nextInBin = nextInBin->nextInBin;
398        }
399
400        nextBin = nextBin->nextBin;
401    }
402
403    return true;
404}
405
406Event*
407EventQueue::replaceHead(Event* s)
408{
409    Event* t = head;
410    head = s;
411    return t;
412}
413
414void
415dumpMainQueue()
416{
417    for (uint32_t i = 0; i < numMainEventQueues; ++i) {
418        mainEventQueue[i]->dump();
419    }
420}
421
422
423const char *
424Event::description() const
425{
426    return "generic";
427}
428
429void
430Event::trace(const char *action)
431{
432    // This DPRINTF is unconditional because calls to this function
433    // are protected by an 'if (DTRACE(Event))' in the inlined Event
434    // methods.
435    //
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());
442}
443
444void
445Event::dump() const
446{
447    cprintf("Event %s (%s)\n", name(), description());
448    cprintf("Flags: %#x\n", flags);
449#ifdef EVENTQ_DEBUG
450    cprintf("Created: %d\n", whenCreated);
451#endif
452    if (scheduled()) {
453#ifdef EVENTQ_DEBUG
454        cprintf("Scheduled at  %d\n", whenScheduled);
455#endif
456        cprintf("Scheduled for %d, priority %d\n", when(), _priority);
457    } else {
458        cprintf("Not Scheduled\n");
459    }
460}
461
462EventQueue::EventQueue(const string &n)
463    : objName(n), head(NULL), _curTick(0)
464{
465}
466
467void
468EventQueue::asyncInsert(Event *event)
469{
470    async_queue_mutex.lock();
471    async_queue.push_back(event);
472    async_queue_mutex.unlock();
473}
474
475void
476EventQueue::handleAsyncInsertions()
477{
478    assert(this == curEventQueue());
479    async_queue_mutex.lock();
480
481    while (!async_queue.empty()) {
482        insert(async_queue.front());
483        async_queue.pop_front();
484    }
485
486    async_queue_mutex.unlock();
487}
488