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