eventq.cc (5768:ba6f2477d870) eventq.cc (5769:e53bdd0e4bf1)
1/*
2 * Copyright (c) 2000-2005 The Regents of The University of Michigan
3 * Copyright (c) 2008 The Hewlett-Packard Development Company
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Steve Reinhardt
30 * Nathan Binkert
31 * Steve Raasch
32 */
33
34#include <cassert>
35#include <iostream>
36#include <string>
37#include <vector>
38
39#include "base/hashmap.hh"
40#include "base/misc.hh"
41#include "base/trace.hh"
42#include "cpu/smt.hh"
43#include "sim/core.hh"
44#include "sim/eventq.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("Main Event Queue");
55
56#ifndef NDEBUG
57Counter Event::instanceCounter = 0;
58#endif
59
60Event::~Event()
61{
62}
63
64const std::string
65Event::name() const
66{
67#ifndef NDEBUG
68 return csprintf("Event_%d", instance);
69#else
70 return csprintf("Event_%x", (uintptr_t)this);
71#endif
72}
73
74
75Event *
76Event::insertBefore(Event *event, Event *curr)
77{
78 // Either way, event will be the top element in the 'in bin' list
79 // which is the pointer we need in order to look into the list, so
80 // we need to insert that into the bin list.
81 if (!curr || *event < *curr) {
82 // Insert the event before the current list since it is in the future.
83 event->nextBin = curr;
84 event->nextInBin = NULL;
85 } else {
86 // Since we're on the correct list, we need to point to the next list
87 event->nextBin = curr->nextBin; // curr->nextBin can now become stale
88
89 // Insert event at the top of the stack
90 event->nextInBin = curr;
91 }
92
93 return event;
94}
95
96void
97EventQueue::insert(Event *event)
98{
99 // Deal with the head case
100 if (!head || *event <= *head) {
101 head = Event::insertBefore(event, head);
102 return;
103 }
104
105 // Figure out either which 'in bin' list we are on, or where a new list
106 // needs to be inserted
107 Event *prev = head;
108 Event *curr = head->nextBin;
109 while (curr && *curr < *event) {
110 prev = curr;
111 curr = curr->nextBin;
112 }
113
114 // Note: this operation may render all nextBin pointers on the
115 // prev 'in bin' list stale (except for the top one)
116 prev->nextBin = Event::insertBefore(event, curr);
117}
118
119Event *
120Event::removeItem(Event *event, Event *top)
121{
122 Event *curr = top;
123 Event *next = top->nextInBin;
124
125 // if we removed the top item, we need to handle things specially
126 // and just remove the top item, fixing up the next bin pointer of
127 // the new top item
128 if (event == top) {
129 if (!next)
130 return top->nextBin;
131 next->nextBin = top->nextBin;
132 return next;
133 }
134
135 // Since we already checked the current element, we're going to
136 // keep checking event against the next element.
137 while (event != next) {
138 if (!next)
139 panic("event not found!");
140
141 curr = next;
142 next = next->nextInBin;
143 }
144
145 // remove next from the 'in bin' list since it's what we're looking for
146 curr->nextInBin = next->nextInBin;
147 return top;
148}
149
150void
151EventQueue::remove(Event *event)
152{
153 if (head == NULL)
154 panic("event not found!");
155
156 // deal with an event on the head's 'in bin' list (event has the same
157 // time as the head)
158 if (*head == *event) {
159 head = Event::removeItem(event, head);
160 return;
161 }
162
163 // Find the 'in bin' list that this event belongs on
164 Event *prev = head;
165 Event *curr = head->nextBin;
166 while (curr && *curr < *event) {
167 prev = curr;
168 curr = curr->nextBin;
169 }
170
171 if (!curr || *curr != *event)
172 panic("event not found!");
173
174 // curr points to the top item of the the correct 'in bin' list, when
175 // we remove an item, it returns the new top item (which may be
176 // unchanged)
177 prev->nextBin = Event::removeItem(event, curr);
178}
179
180Event *
181EventQueue::serviceOne()
182{
183 Event *event = head;
184 Event *next = head->nextInBin;
1/*
2 * Copyright (c) 2000-2005 The Regents of The University of Michigan
3 * Copyright (c) 2008 The Hewlett-Packard Development Company
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Steve Reinhardt
30 * Nathan Binkert
31 * Steve Raasch
32 */
33
34#include <cassert>
35#include <iostream>
36#include <string>
37#include <vector>
38
39#include "base/hashmap.hh"
40#include "base/misc.hh"
41#include "base/trace.hh"
42#include "cpu/smt.hh"
43#include "sim/core.hh"
44#include "sim/eventq.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("Main Event Queue");
55
56#ifndef NDEBUG
57Counter Event::instanceCounter = 0;
58#endif
59
60Event::~Event()
61{
62}
63
64const std::string
65Event::name() const
66{
67#ifndef NDEBUG
68 return csprintf("Event_%d", instance);
69#else
70 return csprintf("Event_%x", (uintptr_t)this);
71#endif
72}
73
74
75Event *
76Event::insertBefore(Event *event, Event *curr)
77{
78 // Either way, event will be the top element in the 'in bin' list
79 // which is the pointer we need in order to look into the list, so
80 // we need to insert that into the bin list.
81 if (!curr || *event < *curr) {
82 // Insert the event before the current list since it is in the future.
83 event->nextBin = curr;
84 event->nextInBin = NULL;
85 } else {
86 // Since we're on the correct list, we need to point to the next list
87 event->nextBin = curr->nextBin; // curr->nextBin can now become stale
88
89 // Insert event at the top of the stack
90 event->nextInBin = curr;
91 }
92
93 return event;
94}
95
96void
97EventQueue::insert(Event *event)
98{
99 // Deal with the head case
100 if (!head || *event <= *head) {
101 head = Event::insertBefore(event, head);
102 return;
103 }
104
105 // Figure out either which 'in bin' list we are on, or where a new list
106 // needs to be inserted
107 Event *prev = head;
108 Event *curr = head->nextBin;
109 while (curr && *curr < *event) {
110 prev = curr;
111 curr = curr->nextBin;
112 }
113
114 // Note: this operation may render all nextBin pointers on the
115 // prev 'in bin' list stale (except for the top one)
116 prev->nextBin = Event::insertBefore(event, curr);
117}
118
119Event *
120Event::removeItem(Event *event, Event *top)
121{
122 Event *curr = top;
123 Event *next = top->nextInBin;
124
125 // if we removed the top item, we need to handle things specially
126 // and just remove the top item, fixing up the next bin pointer of
127 // the new top item
128 if (event == top) {
129 if (!next)
130 return top->nextBin;
131 next->nextBin = top->nextBin;
132 return next;
133 }
134
135 // Since we already checked the current element, we're going to
136 // keep checking event against the next element.
137 while (event != next) {
138 if (!next)
139 panic("event not found!");
140
141 curr = next;
142 next = next->nextInBin;
143 }
144
145 // remove next from the 'in bin' list since it's what we're looking for
146 curr->nextInBin = next->nextInBin;
147 return top;
148}
149
150void
151EventQueue::remove(Event *event)
152{
153 if (head == NULL)
154 panic("event not found!");
155
156 // deal with an event on the head's 'in bin' list (event has the same
157 // time as the head)
158 if (*head == *event) {
159 head = Event::removeItem(event, head);
160 return;
161 }
162
163 // Find the 'in bin' list that this event belongs on
164 Event *prev = head;
165 Event *curr = head->nextBin;
166 while (curr && *curr < *event) {
167 prev = curr;
168 curr = curr->nextBin;
169 }
170
171 if (!curr || *curr != *event)
172 panic("event not found!");
173
174 // curr points to the top item of the the correct 'in bin' list, when
175 // we remove an item, it returns the new top item (which may be
176 // unchanged)
177 prev->nextBin = Event::removeItem(event, curr);
178}
179
180Event *
181EventQueue::serviceOne()
182{
183 Event *event = head;
184 Event *next = head->nextInBin;
185 event->clearFlags(Event::Scheduled);
185 event->flags.clear(Event::Scheduled);
186
187 if (next) {
188 // update the next bin pointer since it could be stale
189 next->nextBin = head->nextBin;
190
191 // pop the stack
192 head = next;
193 } else {
194 // this was the only element on the 'in bin' list, so get rid of
195 // the 'in bin' list and point to the next bin list
196 head = head->nextBin;
197 }
198
199 // handle action
200 if (!event->squashed()) {
201 event->process();
202 if (event->isExitEvent()) {
186
187 if (next) {
188 // update the next bin pointer since it could be stale
189 next->nextBin = head->nextBin;
190
191 // pop the stack
192 head = next;
193 } else {
194 // this was the only element on the 'in bin' list, so get rid of
195 // the 'in bin' list and point to the next bin list
196 head = head->nextBin;
197 }
198
199 // handle action
200 if (!event->squashed()) {
201 event->process();
202 if (event->isExitEvent()) {
203 assert(!event->getFlags(Event::AutoDelete)); // would be silly
203 assert(!event->flags.isSet(Event::AutoDelete)); // would be silly
204 return event;
205 }
206 } else {
204 return event;
205 }
206 } else {
207 event->clearFlags(Event::Squashed);
207 event->flags.clear(Event::Squashed);
208 }
209
208 }
209
210 if (event->getFlags(Event::AutoDelete) && !event->scheduled())
210 if (event->flags.isSet(Event::AutoDelete) && !event->scheduled())
211 delete event;
212
213 return NULL;
214}
215
216void
217Event::serialize(std::ostream &os)
218{
219 SERIALIZE_SCALAR(_when);
220 SERIALIZE_SCALAR(_priority);
211 delete event;
212
213 return NULL;
214}
215
216void
217Event::serialize(std::ostream &os)
218{
219 SERIALIZE_SCALAR(_when);
220 SERIALIZE_SCALAR(_priority);
221 SERIALIZE_ENUM(_flags);
221 short _flags = flags;
222 SERIALIZE_SCALAR(_flags);
222}
223
224void
225Event::unserialize(Checkpoint *cp, const string &section)
226{
227 if (scheduled())
228 mainEventQueue.deschedule(this);
229
230 UNSERIALIZE_SCALAR(_when);
231 UNSERIALIZE_SCALAR(_priority);
232
233 // need to see if original event was in a scheduled, unsquashed
234 // state, but don't want to restore those flags in the current
235 // object itself (since they aren't immediately true)
223}
224
225void
226Event::unserialize(Checkpoint *cp, const string &section)
227{
228 if (scheduled())
229 mainEventQueue.deschedule(this);
230
231 UNSERIALIZE_SCALAR(_when);
232 UNSERIALIZE_SCALAR(_priority);
233
234 // need to see if original event was in a scheduled, unsquashed
235 // state, but don't want to restore those flags in the current
236 // object itself (since they aren't immediately true)
236 UNSERIALIZE_ENUM(_flags);
237 bool wasScheduled = (_flags & Scheduled) && !(_flags & Squashed);
238 _flags &= ~(Squashed | Scheduled);
237 short _flags;
238 UNSERIALIZE_SCALAR(_flags);
239 flags = _flags;
239
240
241 bool wasScheduled = flags.isSet(Scheduled) && !flags.isSet(Squashed);
242 flags.clear(Squashed | Scheduled);
243
240 if (wasScheduled) {
241 DPRINTF(Config, "rescheduling at %d\n", _when);
242 mainEventQueue.schedule(this, _when);
243 }
244}
245
246void
247EventQueue::serialize(ostream &os)
248{
249 std::list<Event *> eventPtrs;
250
251 int numEvents = 0;
252 Event *nextBin = head;
253 while (nextBin) {
254 Event *nextInBin = nextBin;
255
256 while (nextInBin) {
244 if (wasScheduled) {
245 DPRINTF(Config, "rescheduling at %d\n", _when);
246 mainEventQueue.schedule(this, _when);
247 }
248}
249
250void
251EventQueue::serialize(ostream &os)
252{
253 std::list<Event *> eventPtrs;
254
255 int numEvents = 0;
256 Event *nextBin = head;
257 while (nextBin) {
258 Event *nextInBin = nextBin;
259
260 while (nextInBin) {
257 if (nextInBin->getFlags(Event::AutoSerialize)) {
261 if (nextInBin->flags.isSet(Event::AutoSerialize)) {
258 eventPtrs.push_back(nextInBin);
259 paramOut(os, csprintf("event%d", numEvents++),
260 nextInBin->name());
261 }
262 nextInBin = nextInBin->nextInBin;
263 }
264
265 nextBin = nextBin->nextBin;
266 }
267
268 SERIALIZE_SCALAR(numEvents);
269
270 for (std::list<Event *>::iterator it = eventPtrs.begin();
271 it != eventPtrs.end(); ++it) {
272 (*it)->nameOut(os);
273 (*it)->serialize(os);
274 }
275}
276
277void
278EventQueue::unserialize(Checkpoint *cp, const std::string &section)
279{
280 int numEvents;
281 UNSERIALIZE_SCALAR(numEvents);
282
283 std::string eventName;
284 for (int i = 0; i < numEvents; i++) {
285 // get the pointer value associated with the event
286 paramIn(cp, section, csprintf("event%d", i), eventName);
287
288 // create the event based on its pointer value
289 Serializable::create(cp, eventName);
290 }
291}
292
293void
294EventQueue::dump() const
295{
296 cprintf("============================================================\n");
297 cprintf("EventQueue Dump (cycle %d)\n", curTick);
298 cprintf("------------------------------------------------------------\n");
299
300 if (empty())
301 cprintf("<No Events>\n");
302 else {
303 Event *nextBin = head;
304 while (nextBin) {
305 Event *nextInBin = nextBin;
306 while (nextInBin) {
307 nextInBin->dump();
308 nextInBin = nextInBin->nextInBin;
309 }
310
311 nextBin = nextBin->nextBin;
312 }
313 }
314
315 cprintf("============================================================\n");
316}
317
318bool
319EventQueue::debugVerify() const
320{
321 m5::hash_map<long, bool> map;
322
323 Tick time = 0;
324 short priority = 0;
325
326 Event *nextBin = head;
327 while (nextBin) {
328 Event *nextInBin = nextBin;
329 while (nextInBin) {
330 if (nextInBin->when() < time) {
331 cprintf("time goes backwards!");
332 nextInBin->dump();
333 return false;
334 } else if (nextInBin->when() == time &&
335 nextInBin->priority() < priority) {
336 cprintf("priority inverted!");
337 nextInBin->dump();
338 return false;
339 }
340
341 if (map[reinterpret_cast<long>(nextInBin)]) {
342 cprintf("Node already seen");
343 nextInBin->dump();
344 return false;
345 }
346 map[reinterpret_cast<long>(nextInBin)] = true;
347
348 time = nextInBin->when();
349 priority = nextInBin->priority();
350
351 nextInBin = nextInBin->nextInBin;
352 }
353
354 nextBin = nextBin->nextBin;
355 }
356
357 return true;
358}
359
360void
361dumpMainQueue()
362{
363 mainEventQueue.dump();
364}
365
366
367const char *
368Event::description() const
369{
370 return "generic";
371}
372
373void
374Event::trace(const char *action)
375{
376 // This DPRINTF is unconditional because calls to this function
377 // are protected by an 'if (DTRACE(Event))' in the inlined Event
378 // methods.
379 //
380 // This is just a default implementation for derived classes where
381 // it's not worth doing anything special. If you want to put a
382 // more informative message in the trace, override this method on
383 // the particular subclass where you have the information that
384 // needs to be printed.
385 DPRINTFN("%s event %s @ %d\n", description(), action, when());
386}
387
388void
389Event::dump() const
390{
391 cprintf("Event %s (%s)\n", name(), description());
262 eventPtrs.push_back(nextInBin);
263 paramOut(os, csprintf("event%d", numEvents++),
264 nextInBin->name());
265 }
266 nextInBin = nextInBin->nextInBin;
267 }
268
269 nextBin = nextBin->nextBin;
270 }
271
272 SERIALIZE_SCALAR(numEvents);
273
274 for (std::list<Event *>::iterator it = eventPtrs.begin();
275 it != eventPtrs.end(); ++it) {
276 (*it)->nameOut(os);
277 (*it)->serialize(os);
278 }
279}
280
281void
282EventQueue::unserialize(Checkpoint *cp, const std::string &section)
283{
284 int numEvents;
285 UNSERIALIZE_SCALAR(numEvents);
286
287 std::string eventName;
288 for (int i = 0; i < numEvents; i++) {
289 // get the pointer value associated with the event
290 paramIn(cp, section, csprintf("event%d", i), eventName);
291
292 // create the event based on its pointer value
293 Serializable::create(cp, eventName);
294 }
295}
296
297void
298EventQueue::dump() const
299{
300 cprintf("============================================================\n");
301 cprintf("EventQueue Dump (cycle %d)\n", curTick);
302 cprintf("------------------------------------------------------------\n");
303
304 if (empty())
305 cprintf("<No Events>\n");
306 else {
307 Event *nextBin = head;
308 while (nextBin) {
309 Event *nextInBin = nextBin;
310 while (nextInBin) {
311 nextInBin->dump();
312 nextInBin = nextInBin->nextInBin;
313 }
314
315 nextBin = nextBin->nextBin;
316 }
317 }
318
319 cprintf("============================================================\n");
320}
321
322bool
323EventQueue::debugVerify() const
324{
325 m5::hash_map<long, bool> map;
326
327 Tick time = 0;
328 short priority = 0;
329
330 Event *nextBin = head;
331 while (nextBin) {
332 Event *nextInBin = nextBin;
333 while (nextInBin) {
334 if (nextInBin->when() < time) {
335 cprintf("time goes backwards!");
336 nextInBin->dump();
337 return false;
338 } else if (nextInBin->when() == time &&
339 nextInBin->priority() < priority) {
340 cprintf("priority inverted!");
341 nextInBin->dump();
342 return false;
343 }
344
345 if (map[reinterpret_cast<long>(nextInBin)]) {
346 cprintf("Node already seen");
347 nextInBin->dump();
348 return false;
349 }
350 map[reinterpret_cast<long>(nextInBin)] = true;
351
352 time = nextInBin->when();
353 priority = nextInBin->priority();
354
355 nextInBin = nextInBin->nextInBin;
356 }
357
358 nextBin = nextBin->nextBin;
359 }
360
361 return true;
362}
363
364void
365dumpMainQueue()
366{
367 mainEventQueue.dump();
368}
369
370
371const char *
372Event::description() const
373{
374 return "generic";
375}
376
377void
378Event::trace(const char *action)
379{
380 // This DPRINTF is unconditional because calls to this function
381 // are protected by an 'if (DTRACE(Event))' in the inlined Event
382 // methods.
383 //
384 // This is just a default implementation for derived classes where
385 // it's not worth doing anything special. If you want to put a
386 // more informative message in the trace, override this method on
387 // the particular subclass where you have the information that
388 // needs to be printed.
389 DPRINTFN("%s event %s @ %d\n", description(), action, when());
390}
391
392void
393Event::dump() const
394{
395 cprintf("Event %s (%s)\n", name(), description());
392 cprintf("Flags: %#x\n", _flags);
396 cprintf("Flags: %#x\n", flags);
393#ifdef EVENTQ_DEBUG
394 cprintf("Created: %d\n", whenCreated);
395#endif
396 if (scheduled()) {
397#ifdef EVENTQ_DEBUG
398 cprintf("Scheduled at %d\n", whenScheduled);
399#endif
400 cprintf("Scheduled for %d, priority %d\n", when(), _priority);
401 } else {
402 cprintf("Not Scheduled\n");
403 }
404}
397#ifdef EVENTQ_DEBUG
398 cprintf("Created: %d\n", whenCreated);
399#endif
400 if (scheduled()) {
401#ifdef EVENTQ_DEBUG
402 cprintf("Scheduled at %d\n", whenScheduled);
403#endif
404 cprintf("Scheduled for %d, priority %d\n", when(), _priority);
405 } else {
406 cprintf("Not Scheduled\n");
407 }
408}