eventq.cc revision 1717
16657Snate@binkert.org/*
26657Snate@binkert.org * Copyright (c) 2000-2003 The Regents of The University of Michigan
36657Snate@binkert.org * All rights reserved.
46657Snate@binkert.org *
56657Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66657Snate@binkert.org * modification, are permitted provided that the following conditions are
76657Snate@binkert.org * met: redistributions of source code must retain the above copyright
86657Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96657Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106657Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116657Snate@binkert.org * documentation and/or other materials provided with the distribution;
126657Snate@binkert.org * neither the name of the copyright holders nor the names of its
136657Snate@binkert.org * contributors may be used to endorse or promote products derived from
146657Snate@binkert.org * this software without specific prior written permission.
156657Snate@binkert.org *
166657Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176657Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186657Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196657Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206657Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216657Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226657Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236657Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246657Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256657Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266657Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276657Snate@binkert.org */
286657Snate@binkert.org
296657Snate@binkert.org#include <assert.h>
306657Snate@binkert.org
316657Snate@binkert.org#include <iostream>
326657Snate@binkert.org#include <string>
336657Snate@binkert.org#include <sstream>
346657Snate@binkert.org#include <vector>
356657Snate@binkert.org
366657Snate@binkert.org#include "cpu/smt.hh"
376657Snate@binkert.org#include "base/misc.hh"
386657Snate@binkert.org
39#include "sim/eventq.hh"
40#include "base/trace.hh"
41#include "sim/root.hh"
42
43using namespace std;
44
45//
46// Main Event Queue
47//
48// Events on this queue are processed at the *beginning* of each
49// cycle, before the pipeline simulation is performed.
50//
51EventQueue mainEventQueue("MainEventQueue");
52
53void
54EventQueue::insert(Event *event)
55{
56    if (head == NULL || event->when() < head->when() ||
57        (event->when() == head->when() &&
58         event->priority() <= head->priority())) {
59        event->next = head;
60        head = event;
61    } else {
62        Event *prev = head;
63        Event *curr = head->next;
64
65        while (curr) {
66            if (event->when() <= curr->when() &&
67                (event->when() < curr->when() ||
68                 event->priority() <= curr->priority()))
69                break;
70
71            prev = curr;
72            curr = curr->next;
73        }
74
75        event->next = curr;
76        prev->next = event;
77    }
78}
79
80void
81EventQueue::remove(Event *event)
82{
83    if (head == NULL)
84        return;
85
86    if (head == event){
87        head = event->next;
88        return;
89    }
90
91    Event *prev = head;
92    Event *curr = head->next;
93    while (curr && curr != event) {
94        prev = curr;
95        curr = curr->next;
96    }
97
98    if (curr == event)
99        prev->next = curr->next;
100}
101
102void
103EventQueue::serviceOne()
104{
105    Event *event = head;
106    event->clearFlags(Event::Scheduled);
107    head = event->next;
108
109    // handle action
110    if (!event->squashed())
111        event->process();
112    else
113        event->clearFlags(Event::Squashed);
114
115    if (event->getFlags(Event::AutoDelete) && !event->scheduled())
116        delete event;
117}
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