eventq.cc revision 5336:c7e21f4e5a2e
1/*
2 * Copyright (c) 2000-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Steve Reinhardt
29 *          Nathan Binkert
30 *          Steve Raasch
31 */
32
33#include <assert.h>
34
35#include <iostream>
36#include <string>
37#include <vector>
38
39#include "cpu/smt.hh"
40#include "base/misc.hh"
41
42#include "sim/eventq.hh"
43#include "base/trace.hh"
44#include "sim/core.hh"
45
46using namespace std;
47
48//
49// Main Event Queue
50//
51// Events on this queue are processed at the *beginning* of each
52// cycle, before the pipeline simulation is performed.
53//
54EventQueue mainEventQueue("MainEventQueue");
55
56#ifndef NDEBUG
57Counter Event::instanceCounter = 0;
58#endif
59
60void
61EventQueue::insert(Event *event)
62{
63    if (head == NULL || event->when() < head->when() ||
64        (event->when() == head->when() &&
65         event->priority() <= head->priority())) {
66        event->next = head;
67        head = event;
68    } else {
69        Event *prev = head;
70        Event *curr = head->next;
71
72        while (curr) {
73            if (event->when() <= curr->when() &&
74                (event->when() < curr->when() ||
75                 event->priority() <= curr->priority()))
76                break;
77
78            prev = curr;
79            curr = curr->next;
80        }
81
82        event->next = curr;
83        prev->next = event;
84    }
85}
86
87void
88EventQueue::remove(Event *event)
89{
90    if (head == NULL)
91        return;
92
93    if (head == event){
94        head = event->next;
95        return;
96    }
97
98    Event *prev = head;
99    Event *curr = head->next;
100    while (curr && curr != event) {
101        prev = curr;
102        curr = curr->next;
103    }
104
105    if (curr == event)
106        prev->next = curr->next;
107}
108
109Event *
110EventQueue::serviceOne()
111{
112    Event *event = head;
113    event->clearFlags(Event::Scheduled);
114    head = event->next;
115
116    // handle action
117    if (!event->squashed()) {
118        event->process();
119        if (event->isExitEvent()) {
120            assert(!event->getFlags(Event::AutoDelete)); // would be silly
121            return event;
122        }
123    } else {
124        event->clearFlags(Event::Squashed);
125    }
126
127    if (event->getFlags(Event::AutoDelete) && !event->scheduled())
128        delete event;
129
130    return NULL;
131}
132
133
134void
135Event::serialize(std::ostream &os)
136{
137    SERIALIZE_SCALAR(_when);
138    SERIALIZE_SCALAR(_priority);
139    SERIALIZE_ENUM(_flags);
140}
141
142
143void
144Event::unserialize(Checkpoint *cp, const string &section)
145{
146    if (scheduled())
147        deschedule();
148
149    UNSERIALIZE_SCALAR(_when);
150    UNSERIALIZE_SCALAR(_priority);
151
152    // need to see if original event was in a scheduled, unsquashed
153    // state, but don't want to restore those flags in the current
154    // object itself (since they aren't immediately true)
155    UNSERIALIZE_ENUM(_flags);
156    bool wasScheduled = (_flags & Scheduled) && !(_flags & Squashed);
157    _flags &= ~(Squashed | Scheduled);
158
159    if (wasScheduled) {
160        DPRINTF(Config, "rescheduling at %d\n", _when);
161        schedule(_when);
162    }
163}
164
165void
166EventQueue::serialize(ostream &os)
167{
168    std::list<Event *> eventPtrs;
169
170    int numEvents = 0;
171    Event *event = head;
172    while (event) {
173        if (event->getFlags(Event::AutoSerialize)) {
174            eventPtrs.push_back(event);
175            paramOut(os, csprintf("event%d", numEvents++), event->name());
176        }
177        event = event->next;
178    }
179
180    SERIALIZE_SCALAR(numEvents);
181
182    for (std::list<Event *>::iterator it=eventPtrs.begin();
183         it != eventPtrs.end(); ++it) {
184        (*it)->nameOut(os);
185        (*it)->serialize(os);
186    }
187}
188
189void
190EventQueue::unserialize(Checkpoint *cp, const std::string &section)
191{
192    int numEvents;
193    UNSERIALIZE_SCALAR(numEvents);
194
195    std::string eventName;
196    for (int i = 0; i < numEvents; i++) {
197        // get the pointer value associated with the event
198        paramIn(cp, section, csprintf("event%d", i), eventName);
199
200        // create the event based on its pointer value
201        Serializable::create(cp, eventName);
202    }
203}
204
205void
206EventQueue::dump()
207{
208    cprintf("============================================================\n");
209    cprintf("EventQueue Dump  (cycle %d)\n", curTick);
210    cprintf("------------------------------------------------------------\n");
211
212    if (empty())
213        cprintf("<No Events>\n");
214    else {
215        Event *event = head;
216        while (event) {
217            event->dump();
218            event = event->next;
219        }
220    }
221
222    cprintf("============================================================\n");
223}
224
225void
226dumpMainQueue()
227{
228    mainEventQueue.dump();
229}
230
231
232const char *
233Event::description() const
234{
235    return "generic";
236}
237
238#if TRACING_ON
239void
240Event::trace(const char *action)
241{
242    // This DPRINTF is unconditional because calls to this function
243    // are protected by an 'if (DTRACE(Event))' in the inlined Event
244    // methods.
245    //
246    // This is just a default implementation for derived classes where
247    // it's not worth doing anything special.  If you want to put a
248    // more informative message in the trace, override this method on
249    // the particular subclass where you have the information that
250    // needs to be printed.
251    DPRINTFN("%s event %s @ %d\n", description(), action, when());
252}
253#endif
254
255void
256Event::dump()
257{
258    cprintf("Event  (%s)\n", description());
259    cprintf("Flags: %#x\n", _flags);
260#if TRACING_ON
261    cprintf("Created: %d\n", when_created);
262#endif
263    if (scheduled()) {
264#if TRACING_ON
265        cprintf("Scheduled at  %d\n", when_scheduled);
266#endif
267        cprintf("Scheduled for %d, priority %d\n", when(), _priority);
268    }
269    else {
270        cprintf("Not Scheduled\n");
271    }
272}
273