eventq.cc revision 2
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 "smt.hh"
37#include "misc.hh"
38
39#include "eventq.hh"
40#include "trace.hh"
41#include "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("Main Event Queue");
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
121void
122EventQueue::nameChildren()
123{
124    int j = 0;
125
126    Event *event = head;
127    while (event) {
128        stringstream stream;
129        ccprintf(stream, "%s.event%d", name(), j++);
130        event->setName(stream.str());
131
132        event = event->next;
133    }
134}
135
136void
137EventQueue::serialize()
138{
139    string objects = "";
140
141    Event *event = head;
142    while (event) {
143        objects += event->name();
144        objects += " ";
145        event->serialize();
146
147        event = event->next;
148    }
149    nameOut("Serialized");
150    paramOut("objects",objects);
151}
152
153void
154EventQueue::dump()
155{
156    cprintf("============================================================\n");
157    cprintf("EventQueue Dump  (cycle %d)\n", curTick);
158    cprintf("------------------------------------------------------------\n");
159
160    if (empty())
161        cprintf("<No Events>\n");
162    else {
163        Event *event = head;
164        while (event) {
165            event->dump();
166            event = event->next;
167        }
168    }
169
170    cprintf("============================================================\n");
171}
172
173
174const char *
175Event::description()
176{
177    return "generic";
178}
179
180#if TRACING_ON
181void
182Event::trace(const char *action)
183{
184    // This DPRINTF is unconditional because calls to this function
185    // are protected by an 'if (DTRACE(Event))' in the inlined Event
186    // methods.
187    //
188    // This is just a default implementation for derived classes where
189    // it's not worth doing anything special.  If you want to put a
190    // more informative message in the trace, override this method on
191    // the particular subclass where you have the information that
192    // needs to be printed.
193    DPRINTFN("%s event %s @ %d\n", description(), action, when());
194}
195#endif
196
197void
198Event::dump()
199{
200#if TRACING_ON
201    cprintf("   Created: %d\n", when_created);
202#endif
203    if (scheduled()) {
204#if TRACING_ON
205        cprintf("   Scheduled at  %d\n", when_scheduled);
206#endif
207        cprintf("   Scheduled for %d\n", when());
208    }
209    else {
210        cprintf("   Not Scheduled\n");
211    }
212}
213