eventq.cc revision 1019
112841Sgabeblack@google.com/*
212841Sgabeblack@google.com * Copyright (c) 2000-2003 The Regents of The University of Michigan
312841Sgabeblack@google.com * All rights reserved.
412841Sgabeblack@google.com *
512841Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612841Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712841Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912841Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112841Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212841Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312841Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412841Sgabeblack@google.com * this software without specific prior written permission.
1512841Sgabeblack@google.com *
1612841Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712841Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812841Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912841Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012841Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112841Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212841Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312841Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412841Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512841Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612841Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712841Sgabeblack@google.com */
2812841Sgabeblack@google.com
2912841Sgabeblack@google.com#include <assert.h>
3012841Sgabeblack@google.com
3112841Sgabeblack@google.com#include <iostream>
3212841Sgabeblack@google.com#include <string>
3312841Sgabeblack@google.com#include <sstream>
3412841Sgabeblack@google.com#include <vector>
3512841Sgabeblack@google.com
3612841Sgabeblack@google.com#include "cpu/full_cpu/smt.hh"
3712841Sgabeblack@google.com#include "base/misc.hh"
3812841Sgabeblack@google.com
3912841Sgabeblack@google.com#include "sim/eventq.hh"
4012841Sgabeblack@google.com#include "base/trace.hh"
4112841Sgabeblack@google.com#include "sim/universe.hh"
4212841Sgabeblack@google.com
4312841Sgabeblack@google.comusing namespace std;
4412841Sgabeblack@google.com
4512841Sgabeblack@google.com//
4612841Sgabeblack@google.com// Main Event Queue
4712841Sgabeblack@google.com//
4812841Sgabeblack@google.com// Events on this queue are processed at the *beginning* of each
4912841Sgabeblack@google.com// cycle, before the pipeline simulation is performed.
5012841Sgabeblack@google.com//
5112841Sgabeblack@google.comEventQueue mainEventQueue("MainEventQueue");
5212868Sgabeblack@google.com
5312868Sgabeblack@google.comvoid
5412868Sgabeblack@google.comEventQueue::insert(Event *event)
5512868Sgabeblack@google.com{
5612868Sgabeblack@google.com    if (head == NULL || event->when() < head->when() ||
5712868Sgabeblack@google.com        (event->when() == head->when() &&
5812868Sgabeblack@google.com         event->priority() <= head->priority())) {
5912868Sgabeblack@google.com        event->next = head;
6012868Sgabeblack@google.com        head = event;
6112868Sgabeblack@google.com    } else {
6212868Sgabeblack@google.com        Event *prev = head;
6312868Sgabeblack@google.com        Event *curr = head->next;
6412868Sgabeblack@google.com
6512868Sgabeblack@google.com        while (curr) {
6612868Sgabeblack@google.com            if (event->when() <= curr->when() &&
6712868Sgabeblack@google.com                (event->when() < curr->when() ||
6812868Sgabeblack@google.com                 event->priority() <= curr->priority()))
6912868Sgabeblack@google.com                break;
7012868Sgabeblack@google.com
7112868Sgabeblack@google.com            prev = curr;
7212841Sgabeblack@google.com            curr = curr->next;
7312841Sgabeblack@google.com        }
7412841Sgabeblack@google.com
7512841Sgabeblack@google.com        event->next = curr;
7612841Sgabeblack@google.com        prev->next = event;
7712841Sgabeblack@google.com    }
7812841Sgabeblack@google.com}
7912841Sgabeblack@google.com
8012841Sgabeblack@google.comvoid
8112841Sgabeblack@google.comEventQueue::remove(Event *event)
8212841Sgabeblack@google.com{
8312841Sgabeblack@google.com    if (head == NULL)
8412841Sgabeblack@google.com        return;
8512841Sgabeblack@google.com
8612841Sgabeblack@google.com    if (head == event){
8712841Sgabeblack@google.com        head = event->next;
8812841Sgabeblack@google.com        return;
8912841Sgabeblack@google.com    }
9012841Sgabeblack@google.com
9112841Sgabeblack@google.com    Event *prev = head;
9212841Sgabeblack@google.com    Event *curr = head->next;
9312841Sgabeblack@google.com    while (curr && curr != event) {
9412841Sgabeblack@google.com        prev = curr;
9512841Sgabeblack@google.com        curr = curr->next;
9612841Sgabeblack@google.com    }
9712841Sgabeblack@google.com
9812841Sgabeblack@google.com    if (curr == event)
9912841Sgabeblack@google.com        prev->next = curr->next;
10012841Sgabeblack@google.com}
10112841Sgabeblack@google.com
10212841Sgabeblack@google.comvoid
10312841Sgabeblack@google.comEventQueue::serviceOne()
10412841Sgabeblack@google.com{
10512841Sgabeblack@google.com    Event *event = head;
10612841Sgabeblack@google.com    event->clearFlags(Event::Scheduled);
10712841Sgabeblack@google.com    head = event->next;
10812841Sgabeblack@google.com
10912841Sgabeblack@google.com    // handle action
11012841Sgabeblack@google.com    if (!event->squashed())
11112841Sgabeblack@google.com        event->process();
11212841Sgabeblack@google.com    else
11312841Sgabeblack@google.com        event->clearFlags(Event::Squashed);
11412841Sgabeblack@google.com
11512841Sgabeblack@google.com    if (event->getFlags(Event::AutoDelete) && !event->scheduled())
11612841Sgabeblack@google.com        delete event;
11712841Sgabeblack@google.com}
118
119
120void
121Event::serialize(std::ostream &os)
122{
123    SERIALIZE_SCALAR(_when);
124    SERIALIZE_SCALAR(_priority);
125    SERIALIZE_ENUM(_flags);
126}
127
128
129void
130Event::unserialize(Checkpoint *cp, const string &section)
131{
132    if (scheduled())
133        deschedule();
134
135    UNSERIALIZE_SCALAR(_when);
136    UNSERIALIZE_SCALAR(_priority);
137
138    // need to see if original event was in a scheduled, unsquashed
139    // state, but don't want to restore those flags in the current
140    // object itself (since they aren't immediately true)
141    UNSERIALIZE_ENUM(_flags);
142    bool wasScheduled = (_flags & Scheduled) && !(_flags & Squashed);
143    _flags &= ~(Squashed | Scheduled);
144
145    if (wasScheduled) {
146        DPRINTF(Config, "rescheduling at %d\n", _when);
147        schedule(_when);
148    }
149}
150
151void
152EventQueue::serialize(ostream &os)
153{
154    std::list<Event *> eventPtrs;
155
156    int numEvents = 0;
157    Event *event = head;
158    while (event) {
159        if (event->getFlags(Event::AutoSerialize)) {
160            eventPtrs.push_back(event);
161            paramOut(os, csprintf("event%d", numEvents++), event->name());
162        }
163        event = event->next;
164    }
165
166    SERIALIZE_SCALAR(numEvents);
167
168    for (std::list<Event *>::iterator it=eventPtrs.begin();
169         it != eventPtrs.end(); ++it) {
170        (*it)->nameOut(os);
171        (*it)->serialize(os);
172    }
173}
174
175void
176EventQueue::unserialize(Checkpoint *cp, const std::string &section)
177{
178    int numEvents;
179    UNSERIALIZE_SCALAR(numEvents);
180
181    std::string eventName;
182    for (int i = 0; i < numEvents; i++) {
183        // get the pointer value associated with the event
184        paramIn(cp, section, csprintf("event%d", i), eventName);
185
186        // create the event based on its pointer value
187        Serializable::create(cp, eventName);
188    }
189}
190
191void
192EventQueue::dump()
193{
194    cprintf("============================================================\n");
195    cprintf("EventQueue Dump  (cycle %d)\n", curTick);
196    cprintf("------------------------------------------------------------\n");
197
198    if (empty())
199        cprintf("<No Events>\n");
200    else {
201        Event *event = head;
202        while (event) {
203            event->dump();
204            event = event->next;
205        }
206    }
207
208    cprintf("============================================================\n");
209}
210
211extern "C"
212void
213dumpMainQueue()
214{
215    mainEventQueue.dump();
216}
217
218
219const char *
220Event::description()
221{
222    return "generic";
223}
224
225#if TRACING_ON
226void
227Event::trace(const char *action)
228{
229    // This DPRINTF is unconditional because calls to this function
230    // are protected by an 'if (DTRACE(Event))' in the inlined Event
231    // methods.
232    //
233    // This is just a default implementation for derived classes where
234    // it's not worth doing anything special.  If you want to put a
235    // more informative message in the trace, override this method on
236    // the particular subclass where you have the information that
237    // needs to be printed.
238    DPRINTFN("%s event %s @ %d\n", description(), action, when());
239}
240#endif
241
242void
243Event::dump()
244{
245    cprintf("Event  (%s)\n", description());
246    cprintf("Flags: %#x\n", _flags);
247#if TRACING_ON
248    cprintf("Created: %d\n", when_created);
249#endif
250    if (scheduled()) {
251#if TRACING_ON
252        cprintf("Scheduled at  %d\n", when_scheduled);
253#endif
254        cprintf("Scheduled for %d, priority %d\n", when(), _priority);
255    }
256    else {
257        cprintf("Not Scheduled\n");
258    }
259}
260