eventq.cc revision 12334:e0ab29a34764
17584SAli.Saidi@arm.com/*
27584SAli.Saidi@arm.com * Copyright (c) 2000-2005 The Regents of The University of Michigan
37584SAli.Saidi@arm.com * Copyright (c) 2008 The Hewlett-Packard Development Company
47584SAli.Saidi@arm.com * Copyright (c) 2013 Advanced Micro Devices, Inc.
57584SAli.Saidi@arm.com * All rights reserved.
67584SAli.Saidi@arm.com *
77584SAli.Saidi@arm.com * Redistribution and use in source and binary forms, with or without
87584SAli.Saidi@arm.com * modification, are permitted provided that the following conditions are
97584SAli.Saidi@arm.com * met: redistributions of source code must retain the above copyright
107584SAli.Saidi@arm.com * notice, this list of conditions and the following disclaimer;
117584SAli.Saidi@arm.com * redistributions in binary form must reproduce the above copyright
127584SAli.Saidi@arm.com * notice, this list of conditions and the following disclaimer in the
137584SAli.Saidi@arm.com * documentation and/or other materials provided with the distribution;
147584SAli.Saidi@arm.com * neither the name of the copyright holders nor the names of its
157584SAli.Saidi@arm.com * contributors may be used to endorse or promote products derived from
167584SAli.Saidi@arm.com * this software without specific prior written permission.
177584SAli.Saidi@arm.com *
187584SAli.Saidi@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
197584SAli.Saidi@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
207584SAli.Saidi@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
217584SAli.Saidi@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
227584SAli.Saidi@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
237584SAli.Saidi@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
247584SAli.Saidi@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
257584SAli.Saidi@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
267584SAli.Saidi@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
277584SAli.Saidi@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
287584SAli.Saidi@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
297584SAli.Saidi@arm.com *
307584SAli.Saidi@arm.com * Authors: Steve Reinhardt
317584SAli.Saidi@arm.com *          Nathan Binkert
327584SAli.Saidi@arm.com *          Steve Raasch
337584SAli.Saidi@arm.com */
347584SAli.Saidi@arm.com
357584SAli.Saidi@arm.com#include <cassert>
367584SAli.Saidi@arm.com#include <iostream>
377584SAli.Saidi@arm.com#include <string>
387584SAli.Saidi@arm.com#include <unordered_map>
397584SAli.Saidi@arm.com#include <vector>
407584SAli.Saidi@arm.com
417584SAli.Saidi@arm.com#include "base/logging.hh"
427584SAli.Saidi@arm.com#include "base/trace.hh"
437584SAli.Saidi@arm.com#include "cpu/smt.hh"
447584SAli.Saidi@arm.com#include "debug/Checkpoint.hh"
457584SAli.Saidi@arm.com#include "sim/core.hh"
467584SAli.Saidi@arm.com#include "sim/eventq_impl.hh"
477584SAli.Saidi@arm.com
487584SAli.Saidi@arm.comusing namespace std;
497584SAli.Saidi@arm.com
509525SAndreas.Sandberg@ARM.comTick simQuantum = 0;
517584SAli.Saidi@arm.com
529806Sstever@gmail.com//
537584SAli.Saidi@arm.com// Main Event Queues
547584SAli.Saidi@arm.com//
5510905Sandreas.sandberg@arm.com// Events on these queues are processed at the *beginning* of each
567584SAli.Saidi@arm.com// cycle, before the pipeline simulation is performed.
577584SAli.Saidi@arm.com//
587584SAli.Saidi@arm.comuint32_t numMainEventQueues = 0;
597584SAli.Saidi@arm.comvector<EventQueue *> mainEventQueue;
607584SAli.Saidi@arm.com__thread EventQueue *_curEventQueue = NULL;
617584SAli.Saidi@arm.combool inParallelMode = false;
627584SAli.Saidi@arm.com
637584SAli.Saidi@arm.comEventQueue *
647584SAli.Saidi@arm.comgetEventQueue(uint32_t index)
657584SAli.Saidi@arm.com{
667584SAli.Saidi@arm.com    while (numMainEventQueues <= index) {
677584SAli.Saidi@arm.com        numMainEventQueues++;
687584SAli.Saidi@arm.com        mainEventQueue.push_back(
697584SAli.Saidi@arm.com            new EventQueue(csprintf("MainEventQueue-%d", index)));
707584SAli.Saidi@arm.com    }
717584SAli.Saidi@arm.com
727584SAli.Saidi@arm.com    return mainEventQueue[index];
737584SAli.Saidi@arm.com}
747584SAli.Saidi@arm.com
757584SAli.Saidi@arm.com#ifndef NDEBUG
767584SAli.Saidi@arm.comCounter Event::instanceCounter = 0;
777584SAli.Saidi@arm.com#endif
787584SAli.Saidi@arm.com
797584SAli.Saidi@arm.comEvent::~Event()
807584SAli.Saidi@arm.com{
817584SAli.Saidi@arm.com    assert(!scheduled());
827584SAli.Saidi@arm.com    flags = 0;
837584SAli.Saidi@arm.com}
847584SAli.Saidi@arm.com
857584SAli.Saidi@arm.comconst std::string
869421Sandreas.hansson@arm.comEvent::name() const
877584SAli.Saidi@arm.com{
887584SAli.Saidi@arm.com#ifndef NDEBUG
899421Sandreas.hansson@arm.com    return csprintf("Event_%d", instance);
907584SAli.Saidi@arm.com#else
917584SAli.Saidi@arm.com    return csprintf("Event_%x", (uintptr_t)this);
927584SAli.Saidi@arm.com#endif
937584SAli.Saidi@arm.com}
947584SAli.Saidi@arm.com
957584SAli.Saidi@arm.com
967584SAli.Saidi@arm.comEvent *
977584SAli.Saidi@arm.comEvent::insertBefore(Event *event, Event *curr)
987584SAli.Saidi@arm.com{
997584SAli.Saidi@arm.com    // Either way, event will be the top element in the 'in bin' list
1007584SAli.Saidi@arm.com    // which is the pointer we need in order to look into the list, so
1017584SAli.Saidi@arm.com    // we need to insert that into the bin list.
1027584SAli.Saidi@arm.com    if (!curr || *event < *curr) {
1037584SAli.Saidi@arm.com        // Insert the event before the current list since it is in the future.
1047584SAli.Saidi@arm.com        event->nextBin = curr;
1057584SAli.Saidi@arm.com        event->nextInBin = NULL;
1067584SAli.Saidi@arm.com    } else {
1077584SAli.Saidi@arm.com        // Since we're on the correct list, we need to point to the next list
1087584SAli.Saidi@arm.com        event->nextBin = curr->nextBin;  // curr->nextBin can now become stale
1097584SAli.Saidi@arm.com
1107584SAli.Saidi@arm.com        // Insert event at the top of the stack
1117584SAli.Saidi@arm.com        event->nextInBin = curr;
1127584SAli.Saidi@arm.com    }
1137584SAli.Saidi@arm.com
1147584SAli.Saidi@arm.com    return event;
1157584SAli.Saidi@arm.com}
1167584SAli.Saidi@arm.com
1177584SAli.Saidi@arm.comvoid
1187584SAli.Saidi@arm.comEventQueue::insert(Event *event)
1197584SAli.Saidi@arm.com{
1207584SAli.Saidi@arm.com    // Deal with the head case
1217584SAli.Saidi@arm.com    if (!head || *event <= *head) {
1227584SAli.Saidi@arm.com        head = Event::insertBefore(event, head);
1237733SAli.Saidi@ARM.com        return;
12411168Sandreas.hansson@arm.com    }
12511168Sandreas.hansson@arm.com
1267584SAli.Saidi@arm.com    // Figure out either which 'in bin' list we are on, or where a new list
1277584SAli.Saidi@arm.com    // needs to be inserted
1287584SAli.Saidi@arm.com    Event *prev = head;
1299525SAndreas.Sandberg@ARM.com    Event *curr = head->nextBin;
1307584SAli.Saidi@arm.com    while (curr && *curr < *event) {
1317584SAli.Saidi@arm.com        prev = curr;
1327584SAli.Saidi@arm.com        curr = curr->nextBin;
1337584SAli.Saidi@arm.com    }
1347584SAli.Saidi@arm.com
1357584SAli.Saidi@arm.com    // Note: this operation may render all nextBin pointers on the
1367584SAli.Saidi@arm.com    // prev 'in bin' list stale (except for the top one)
1377584SAli.Saidi@arm.com    prev->nextBin = Event::insertBefore(event, curr);
1387584SAli.Saidi@arm.com}
1397584SAli.Saidi@arm.com
1407584SAli.Saidi@arm.comEvent *
1417584SAli.Saidi@arm.comEvent::removeItem(Event *event, Event *top)
1427584SAli.Saidi@arm.com{
1437584SAli.Saidi@arm.com    Event *curr = top;
1447584SAli.Saidi@arm.com    Event *next = top->nextInBin;
1457584SAli.Saidi@arm.com
1467584SAli.Saidi@arm.com    // if we removed the top item, we need to handle things specially
1477584SAli.Saidi@arm.com    // and just remove the top item, fixing up the next bin pointer of
1487584SAli.Saidi@arm.com    // the new top item
1497584SAli.Saidi@arm.com    if (event == top) {
1507584SAli.Saidi@arm.com        if (!next)
1517584SAli.Saidi@arm.com            return top->nextBin;
1527584SAli.Saidi@arm.com        next->nextBin = top->nextBin;
1537584SAli.Saidi@arm.com        return next;
1547584SAli.Saidi@arm.com    }
1557584SAli.Saidi@arm.com
1567584SAli.Saidi@arm.com    // Since we already checked the current element, we're going to
1577584SAli.Saidi@arm.com    // keep checking event against the next element.
1587584SAli.Saidi@arm.com    while (event != next) {
1597584SAli.Saidi@arm.com        if (!next)
1607584SAli.Saidi@arm.com            panic("event not found!");
1617584SAli.Saidi@arm.com
1627584SAli.Saidi@arm.com        curr = next;
16311168Sandreas.hansson@arm.com        next = next->nextInBin;
16411168Sandreas.hansson@arm.com    }
1657584SAli.Saidi@arm.com
1667584SAli.Saidi@arm.com    // remove next from the 'in bin' list since it's what we're looking for
1677584SAli.Saidi@arm.com    curr->nextInBin = next->nextInBin;
1687584SAli.Saidi@arm.com    return top;
1697584SAli.Saidi@arm.com}
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::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
243void
244Event::serialize(CheckpointOut &cp) const
245{
246    SERIALIZE_SCALAR(_when);
247    SERIALIZE_SCALAR(_priority);
248    short _flags = flags;
249    SERIALIZE_SCALAR(_flags);
250}
251
252void
253Event::unserialize(CheckpointIn &cp)
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
284void
285EventQueue::checkpointReschedule(Event *event)
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}
293void
294EventQueue::dump() const
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
318bool
319EventQueue::debugVerify() const
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
360Event*
361EventQueue::replaceHead(Event* s)
362{
363    Event* t = head;
364    head = s;
365    return t;
366}
367
368void
369dumpMainQueue()
370{
371    for (uint32_t i = 0; i < numMainEventQueues; ++i) {
372        mainEventQueue[i]->dump();
373    }
374}
375
376
377const char *
378Event::description() const
379{
380    return "generic";
381}
382
383void
384Event::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
398void
399Event::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
416EventQueue::EventQueue(const string &n)
417    : objName(n), head(NULL), _curTick(0)
418{
419}
420
421void
422EventQueue::asyncInsert(Event *event)
423{
424    async_queue_mutex.lock();
425    async_queue.push_back(event);
426    async_queue_mutex.unlock();
427}
428
429void
430EventQueue::handleAsyncInsertions()
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}
442