eventq.cc revision 237
1/*
2 * Copyright (c) 2003 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
29#include <assert.h>
30
31#include <iostream>
32#include <string>
33#include <sstream>
34#include <vector>
35
36#include "cpu/full_cpu/smt.hh"
37#include "base/misc.hh"
38
39#include "sim/eventq.hh"
40#include "base/trace.hh"
41#include "sim/universe.hh"
42
43using namespace std;
44
45const string Event::defaultName("event");
46
47//
48// Main Event Queue
49//
50// Events on this queue are processed at the *beginning* of each
51// cycle, before the pipeline simulation is performed.
52//
53EventQueue mainEventQueue("MainEventQueue");
54
55void
56EventQueue::insert(Event *event)
57{
58    if (head == NULL || event->when() < head->when() ||
59        (event->when() == head->when() &&
60         event->priority() <= head->priority())) {
61        event->next = head;
62        head = event;
63    } else {
64        Event *prev = head;
65        Event *curr = head->next;
66
67        while (curr) {
68            if (event->when() <= curr->when() &&
69                (event->when() < curr->when() ||
70                 event->priority() <= curr->priority()))
71                break;
72
73            prev = curr;
74            curr = curr->next;
75        }
76
77        event->next = curr;
78        prev->next = event;
79    }
80}
81
82void
83EventQueue::remove(Event *event)
84{
85    if (head == NULL)
86        return;
87
88    if (head == event){
89        head = event->next;
90        return;
91    }
92
93    Event *prev = head;
94    Event *curr = head->next;
95    while (curr && curr != event) {
96        prev = curr;
97        curr = curr->next;
98    }
99
100    if (curr == event)
101        prev->next = curr->next;
102}
103
104void
105EventQueue::serviceOne()
106{
107    Event *event = head;
108    event->clearFlags(Event::Scheduled);
109    head = event->next;
110
111    // handle action
112    if (!event->squashed())
113        event->process();
114    else
115        event->clearFlags(Event::Squashed);
116
117    if (event->getFlags(Event::AutoDelete))
118        delete event;
119}
120
121
122void
123Event::serialize(std::ostream &os)
124{
125    SERIALIZE_SCALAR(_when);
126    SERIALIZE_SCALAR(_priority);
127    SERIALIZE_ENUM(_flags);
128}
129
130
131void
132Event::unserialize(Checkpoint *cp, const string &section)
133{
134    if (scheduled())
135        deschedule();
136
137    UNSERIALIZE_SCALAR(_when);
138    UNSERIALIZE_SCALAR(_priority);
139
140    // need to see if original event was in a scheduled, unsquashed
141    // state, but don't want to restore those flags in the current
142    // object itself (since they aren't immediately true)
143    UNSERIALIZE_ENUM(_flags);
144    bool wasScheduled = (_flags & Scheduled) && !(_flags & Squashed);
145    _flags &= ~(Squashed | Scheduled);
146
147    if (wasScheduled) {
148        DPRINTF(Config, "rescheduling at %d\n", _when);
149        schedule(_when);
150    }
151}
152
153
154void
155EventQueue::nameChildren()
156{
157    int numEvents = 0;
158    Event *event = head;
159    while (event) {
160        if (event->getFlags(Event::AutoSerialize)) {
161            event->setName(csprintf("%s.event%d", name(), numEvents++));
162        }
163        event = event->next;
164    }
165
166    numAutoSerializeEvents = numEvents;
167}
168
169void
170EventQueue::serialize(ostream &os)
171{
172    // should have been set by a preceding call to nameChildren()
173    assert(numAutoSerializeEvents >= 0);
174
175    SERIALIZE_SCALAR(numAutoSerializeEvents);
176
177    int numEvents = 0;
178    Event *event = head;
179    while (event) {
180        if (event->getFlags(Event::AutoSerialize)) {
181            event->nameOut(os);
182            event->serialize(os);
183            numEvents++;
184        }
185        event = event->next;
186    }
187
188    assert(numEvents == numAutoSerializeEvents);
189}
190
191
192void
193EventQueue::unserialize(Checkpoint *cp, const std::string &section)
194{
195    UNSERIALIZE_SCALAR(numAutoSerializeEvents);
196    for (int eventNum = 0; eventNum < numAutoSerializeEvents; ++eventNum) {
197        Serializeable::create(cp, csprintf("%s.event%d", section, eventNum));
198    }
199}
200
201
202void
203EventQueue::dump()
204{
205    cprintf("============================================================\n");
206    cprintf("EventQueue Dump  (cycle %d)\n", curTick);
207    cprintf("------------------------------------------------------------\n");
208
209    if (empty())
210        cprintf("<No Events>\n");
211    else {
212        Event *event = head;
213        while (event) {
214            event->dump();
215            event = event->next;
216        }
217    }
218
219    cprintf("============================================================\n");
220}
221
222
223const char *
224Event::description()
225{
226    return "generic";
227}
228
229#if TRACING_ON
230void
231Event::trace(const char *action)
232{
233    // This DPRINTF is unconditional because calls to this function
234    // are protected by an 'if (DTRACE(Event))' in the inlined Event
235    // methods.
236    //
237    // This is just a default implementation for derived classes where
238    // it's not worth doing anything special.  If you want to put a
239    // more informative message in the trace, override this method on
240    // the particular subclass where you have the information that
241    // needs to be printed.
242    DPRINTFN("%s event %s @ %d\n", description(), action, when());
243}
244#endif
245
246void
247Event::dump()
248{
249#if TRACING_ON
250    cprintf("   Created: %d\n", when_created);
251#endif
252    if (scheduled()) {
253#if TRACING_ON
254        cprintf("   Scheduled at  %d\n", when_scheduled);
255#endif
256        cprintf("   Scheduled for %d\n", when());
257    }
258    else {
259        cprintf("   Not Scheduled\n");
260    }
261}
262