eventq.cc revision 9983
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 <vector>
39
40#include "base/hashmap.hh"
41#include "base/misc.hh"
42#include "base/trace.hh"
43#include "cpu/smt.hh"
44#include "debug/Config.hh"
45#include "sim/core.hh"
46#include "sim/eventq_impl.hh"
47
48using namespace std;
49
50Tick simQuantum = 0;
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//
58uint32_t numMainEventQueues = 0;
59vector<EventQueue *> mainEventQueue;
60__thread EventQueue *_curEventQueue = NULL;
61bool inParallelMode = false;
62
63EventQueue *
64getEventQueue(uint32_t index)
65{
66    while (numMainEventQueues <= index) {
67        numMainEventQueues++;
68        mainEventQueue.push_back(
69            new EventQueue(csprintf("MainEventQueue-%d", index)));
70    }
71
72    return mainEventQueue[index];
73}
74
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    Event *event = head;
207    Event *next = head->nextInBin;
208    event->flags.clear(Event::Scheduled);
209
210    if (next) {
211        // update the next bin pointer since it could be stale
212        next->nextBin = head->nextBin;
213
214        // pop the stack
215        head = next;
216    } else {
217        // this was the only element on the 'in bin' list, so get rid of
218        // the 'in bin' list and point to the next bin list
219        head = head->nextBin;
220    }
221
222    // handle action
223    if (!event->squashed()) {
224        // forward current cycle to the time when this event occurs.
225        setCurTick(event->when());
226
227        event->process();
228        if (event->isExitEvent()) {
229            assert(!event->flags.isSet(Event::AutoDelete) ||
230                   !event->flags.isSet(Event::IsMainQueue)); // would be silly
231            return event;
232        }
233    } else {
234        event->flags.clear(Event::Squashed);
235    }
236
237    if (event->flags.isSet(Event::AutoDelete) && !event->scheduled())
238        delete event;
239
240    return NULL;
241}
242
243void
244Event::serialize(std::ostream &os)
245{
246    SERIALIZE_SCALAR(_when);
247    SERIALIZE_SCALAR(_priority);
248    short _flags = flags;
249    SERIALIZE_SCALAR(_flags);
250}
251
252void
253Event::unserialize(Checkpoint *cp, const string &section)
254{
255}
256
257void
258Event::unserialize(Checkpoint *cp, const string &section, EventQueue *eventq)
259{
260    if (scheduled())
261        eventq->deschedule(this);
262
263    UNSERIALIZE_SCALAR(_when);
264    UNSERIALIZE_SCALAR(_priority);
265
266    short _flags;
267    UNSERIALIZE_SCALAR(_flags);
268
269    // Old checkpoints had no concept of the Initialized flag
270    // so restoring from old checkpoints always fail.
271    // Events are initialized on construction but original code
272    // "flags = _flags" would just overwrite the initialization.
273    // So, read in the checkpoint flags, but then set the Initialized
274    // flag on top of it in order to avoid failures.
275    assert(initialized());
276    flags = _flags;
277    flags.set(Initialized);
278
279    // need to see if original event was in a scheduled, unsquashed
280    // state, but don't want to restore those flags in the current
281    // object itself (since they aren't immediately true)
282    bool wasScheduled = flags.isSet(Scheduled) && !flags.isSet(Squashed);
283    flags.clear(Squashed | Scheduled);
284
285    if (wasScheduled) {
286        DPRINTF(Config, "rescheduling at %d\n", _when);
287        eventq->schedule(this, _when);
288    }
289}
290
291void
292EventQueue::serialize(ostream &os)
293{
294    std::list<Event *> eventPtrs;
295
296    int numEvents = 0;
297    Event *nextBin = head;
298    while (nextBin) {
299        Event *nextInBin = nextBin;
300
301        while (nextInBin) {
302            if (nextInBin->flags.isSet(Event::AutoSerialize)) {
303                eventPtrs.push_back(nextInBin);
304                paramOut(os, csprintf("event%d", numEvents++),
305                         nextInBin->name());
306            }
307            nextInBin = nextInBin->nextInBin;
308        }
309
310        nextBin = nextBin->nextBin;
311    }
312
313    SERIALIZE_SCALAR(numEvents);
314
315    for (std::list<Event *>::iterator it = eventPtrs.begin();
316         it != eventPtrs.end(); ++it) {
317        (*it)->nameOut(os);
318        (*it)->serialize(os);
319    }
320}
321
322void
323EventQueue::unserialize(Checkpoint *cp, const std::string &section)
324{
325    int numEvents;
326    UNSERIALIZE_SCALAR(numEvents);
327
328    std::string eventName;
329    for (int i = 0; i < numEvents; i++) {
330        // get the pointer value associated with the event
331        paramIn(cp, section, csprintf("event%d", i), eventName);
332
333        // create the event based on its pointer value
334        Serializable::create(cp, eventName);
335    }
336}
337
338void
339EventQueue::dump() const
340{
341    cprintf("============================================================\n");
342    cprintf("EventQueue Dump  (cycle %d)\n", curTick());
343    cprintf("------------------------------------------------------------\n");
344
345    if (empty())
346        cprintf("<No Events>\n");
347    else {
348        Event *nextBin = head;
349        while (nextBin) {
350            Event *nextInBin = nextBin;
351            while (nextInBin) {
352                nextInBin->dump();
353                nextInBin = nextInBin->nextInBin;
354            }
355
356            nextBin = nextBin->nextBin;
357        }
358    }
359
360    cprintf("============================================================\n");
361}
362
363bool
364EventQueue::debugVerify() const
365{
366    m5::hash_map<long, bool> map;
367
368    Tick time = 0;
369    short priority = 0;
370
371    Event *nextBin = head;
372    while (nextBin) {
373        Event *nextInBin = nextBin;
374        while (nextInBin) {
375            if (nextInBin->when() < time) {
376                cprintf("time goes backwards!");
377                nextInBin->dump();
378                return false;
379            } else if (nextInBin->when() == time &&
380                       nextInBin->priority() < priority) {
381                cprintf("priority inverted!");
382                nextInBin->dump();
383                return false;
384            }
385
386            if (map[reinterpret_cast<long>(nextInBin)]) {
387                cprintf("Node already seen");
388                nextInBin->dump();
389                return false;
390            }
391            map[reinterpret_cast<long>(nextInBin)] = true;
392
393            time = nextInBin->when();
394            priority = nextInBin->priority();
395
396            nextInBin = nextInBin->nextInBin;
397        }
398
399        nextBin = nextBin->nextBin;
400    }
401
402    return true;
403}
404
405Event*
406EventQueue::replaceHead(Event* s)
407{
408    Event* t = head;
409    head = s;
410    return t;
411}
412
413void
414dumpMainQueue()
415{
416    for (uint32_t i = 0; i < numMainEventQueues; ++i) {
417        mainEventQueue[i]->dump();
418    }
419}
420
421
422const char *
423Event::description() const
424{
425    return "generic";
426}
427
428void
429Event::trace(const char *action)
430{
431    // This DPRINTF is unconditional because calls to this function
432    // are protected by an 'if (DTRACE(Event))' in the inlined Event
433    // methods.
434    //
435    // This is just a default implementation for derived classes where
436    // it's not worth doing anything special.  If you want to put a
437    // more informative message in the trace, override this method on
438    // the particular subclass where you have the information that
439    // needs to be printed.
440    DPRINTFN("%s event %s @ %d\n", description(), action, when());
441}
442
443void
444Event::dump() const
445{
446    cprintf("Event %s (%s)\n", name(), description());
447    cprintf("Flags: %#x\n", flags);
448#ifdef EVENTQ_DEBUG
449    cprintf("Created: %d\n", whenCreated);
450#endif
451    if (scheduled()) {
452#ifdef EVENTQ_DEBUG
453        cprintf("Scheduled at  %d\n", whenScheduled);
454#endif
455        cprintf("Scheduled for %d, priority %d\n", when(), _priority);
456    } else {
457        cprintf("Not Scheduled\n");
458    }
459}
460
461EventQueue::EventQueue(const string &n)
462    : objName(n), head(NULL), _curTick(0),
463    async_queue_mutex(new std::mutex())
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