eventq.cc revision 2667:fe64b8353b1c
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/root.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
56void
57EventQueue::insert(Event *event)
58{
59    if (head == NULL || event->when() < head->when() ||
60        (event->when() == head->when() &&
61         event->priority() <= head->priority())) {
62        event->next = head;
63        head = event;
64    } else {
65        Event *prev = head;
66        Event *curr = head->next;
67
68        while (curr) {
69            if (event->when() <= curr->when() &&
70                (event->when() < curr->when() ||
71                 event->priority() <= curr->priority()))
72                break;
73
74            prev = curr;
75            curr = curr->next;
76        }
77
78        event->next = curr;
79        prev->next = event;
80    }
81}
82
83void
84EventQueue::remove(Event *event)
85{
86    if (head == NULL)
87        return;
88
89    if (head == event){
90        head = event->next;
91        return;
92    }
93
94    Event *prev = head;
95    Event *curr = head->next;
96    while (curr && curr != event) {
97        prev = curr;
98        curr = curr->next;
99    }
100
101    if (curr == event)
102        prev->next = curr->next;
103}
104
105Event *
106EventQueue::serviceOne()
107{
108    Event *event = head;
109    event->clearFlags(Event::Scheduled);
110    head = event->next;
111
112    // handle action
113    if (!event->squashed()) {
114        event->process();
115        if (event->isExitEvent()) {
116            assert(!event->getFlags(Event::AutoDelete)); // would be silly
117            return event;
118        }
119    } else {
120        event->clearFlags(Event::Squashed);
121    }
122
123    if (event->getFlags(Event::AutoDelete) && !event->scheduled())
124        delete event;
125
126    return NULL;
127}
128
129
130void
131Event::serialize(std::ostream &os)
132{
133    SERIALIZE_SCALAR(_when);
134    SERIALIZE_SCALAR(_priority);
135    SERIALIZE_ENUM(_flags);
136}
137
138
139void
140Event::unserialize(Checkpoint *cp, const string &section)
141{
142    if (scheduled())
143        deschedule();
144
145    UNSERIALIZE_SCALAR(_when);
146    UNSERIALIZE_SCALAR(_priority);
147
148    // need to see if original event was in a scheduled, unsquashed
149    // state, but don't want to restore those flags in the current
150    // object itself (since they aren't immediately true)
151    UNSERIALIZE_ENUM(_flags);
152    bool wasScheduled = (_flags & Scheduled) && !(_flags & Squashed);
153    _flags &= ~(Squashed | Scheduled);
154
155    if (wasScheduled) {
156        DPRINTF(Config, "rescheduling at %d\n", _when);
157        schedule(_when);
158    }
159}
160
161void
162EventQueue::serialize(ostream &os)
163{
164    std::list<Event *> eventPtrs;
165
166    int numEvents = 0;
167    Event *event = head;
168    while (event) {
169        if (event->getFlags(Event::AutoSerialize)) {
170            eventPtrs.push_back(event);
171            paramOut(os, csprintf("event%d", numEvents++), event->name());
172        }
173        event = event->next;
174    }
175
176    SERIALIZE_SCALAR(numEvents);
177
178    for (std::list<Event *>::iterator it=eventPtrs.begin();
179         it != eventPtrs.end(); ++it) {
180        (*it)->nameOut(os);
181        (*it)->serialize(os);
182    }
183}
184
185void
186EventQueue::unserialize(Checkpoint *cp, const std::string &section)
187{
188    int numEvents;
189    UNSERIALIZE_SCALAR(numEvents);
190
191    std::string eventName;
192    for (int i = 0; i < numEvents; i++) {
193        // get the pointer value associated with the event
194        paramIn(cp, section, csprintf("event%d", i), eventName);
195
196        // create the event based on its pointer value
197        Serializable::create(cp, eventName);
198    }
199}
200
201void
202EventQueue::dump()
203{
204    cprintf("============================================================\n");
205    cprintf("EventQueue Dump  (cycle %d)\n", curTick);
206    cprintf("------------------------------------------------------------\n");
207
208    if (empty())
209        cprintf("<No Events>\n");
210    else {
211        Event *event = head;
212        while (event) {
213            event->dump();
214            event = event->next;
215        }
216    }
217
218    cprintf("============================================================\n");
219}
220
221extern "C"
222void
223dumpMainQueue()
224{
225    mainEventQueue.dump();
226}
227
228
229const char *
230Event::description()
231{
232    return "generic";
233}
234
235#if TRACING_ON
236void
237Event::trace(const char *action)
238{
239    // This DPRINTF is unconditional because calls to this function
240    // are protected by an 'if (DTRACE(Event))' in the inlined Event
241    // methods.
242    //
243    // This is just a default implementation for derived classes where
244    // it's not worth doing anything special.  If you want to put a
245    // more informative message in the trace, override this method on
246    // the particular subclass where you have the information that
247    // needs to be printed.
248    DPRINTFN("%s event %s @ %d\n", description(), action, when());
249}
250#endif
251
252void
253Event::dump()
254{
255    cprintf("Event  (%s)\n", description());
256    cprintf("Flags: %#x\n", _flags);
257#if TRACING_ON
258    cprintf("Created: %d\n", when_created);
259#endif
260    if (scheduled()) {
261#if TRACING_ON
262        cprintf("Scheduled at  %d\n", when_scheduled);
263#endif
264        cprintf("Scheduled for %d, priority %d\n", when(), _priority);
265    }
266    else {
267        cprintf("Not Scheduled\n");
268    }
269}
270