eventq.cc revision 265
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
153void
154EventQueue::serialize(ostream &os)
155{
156    std::list<Event *> eventPtrs;
157
158    int numEvents = 0;
159    Event *event = head;
160    while (event) {
161        if (event->getFlags(Event::AutoSerialize)) {
162            eventPtrs.push_back(event);
163            numEvents++;
164        }
165        event = event->next;
166    }
167
168    SERIALIZE_SCALAR(numEvents);
169
170    int i = 0;
171    for (std::list<Event *>::iterator it=eventPtrs.begin();
172         it != eventPtrs.end(); ++it) {
173        paramOut(os, csprintf("%s.eventPtr%d", name(), i++), (uintptr_t)*it);
174    }
175
176    for (std::list<Event *>::iterator it=eventPtrs.begin();
177         it != eventPtrs.end(); ++it) {
178        (*it)->nameOut(os);
179        (*it)->serialize(os);
180    }
181}
182
183void
184EventQueue::unserialize(Checkpoint *cp, const std::string &section)
185{
186    int numEvents;
187    uintptr_t ptr;
188
189    UNSERIALIZE_SCALAR(numEvents);
190
191    for (int i = 0; i < numEvents; i++) {
192        // get the pointer value associated with the event
193        paramIn(cp, section, csprintf("%s.eventPtr%d", name(), i), ptr);
194
195        // create the event based on its pointer value
196        Serializeable::create(cp, csprintf("%s_%x", Event::defaultName, ptr));
197    }
198}
199
200void
201EventQueue::dump()
202{
203    cprintf("============================================================\n");
204    cprintf("EventQueue Dump  (cycle %d)\n", curTick);
205    cprintf("------------------------------------------------------------\n");
206
207    if (empty())
208        cprintf("<No Events>\n");
209    else {
210        Event *event = head;
211        while (event) {
212            event->dump();
213            event = event->next;
214        }
215    }
216
217    cprintf("============================================================\n");
218}
219
220
221const char *
222Event::description()
223{
224    return "generic";
225}
226
227#if TRACING_ON
228void
229Event::trace(const char *action)
230{
231    // This DPRINTF is unconditional because calls to this function
232    // are protected by an 'if (DTRACE(Event))' in the inlined Event
233    // methods.
234    //
235    // This is just a default implementation for derived classes where
236    // it's not worth doing anything special.  If you want to put a
237    // more informative message in the trace, override this method on
238    // the particular subclass where you have the information that
239    // needs to be printed.
240    DPRINTFN("%s event %s @ %d\n", description(), action, when());
241}
242#endif
243
244void
245Event::dump()
246{
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\n", when());
255    }
256    else {
257        cprintf("   Not Scheduled\n");
258    }
259}
260