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