eventq.cc revision 224
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(const IniFile *db, const string §ion) 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 153 154void 155EventQueue::nameChildren() 156{ 157#if 0 158 int j = 0; 159 160 Event *event = head; 161 while (event) { 162 stringstream stream; 163 ccprintf(stream, "%s.event%d", name(), j++); 164 event->setName(stream.str()); 165 166 event = event->next; 167 } 168#endif 169} 170 171void 172EventQueue::serialize(ostream &os) 173{ 174#if 0 175 string objects = ""; 176 177 Event *event = head; 178 while (event) { 179 objects += event->name(); 180 objects += " "; 181 event->serialize(os); 182 183 event = event->next; 184 } 185 nameOut(os, "Serialized"); 186 SERIALIZE_SCALAR(objects); 187#endif 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 210 211const char * 212Event::description() 213{ 214 return "generic"; 215} 216 217#if TRACING_ON 218void 219Event::trace(const char *action) 220{ 221 // This DPRINTF is unconditional because calls to this function 222 // are protected by an 'if (DTRACE(Event))' in the inlined Event 223 // methods. 224 // 225 // This is just a default implementation for derived classes where 226 // it's not worth doing anything special. If you want to put a 227 // more informative message in the trace, override this method on 228 // the particular subclass where you have the information that 229 // needs to be printed. 230 DPRINTFN("%s event %s @ %d\n", description(), action, when()); 231} 232#endif 233 234void 235Event::dump() 236{ 237#if TRACING_ON 238 cprintf(" Created: %d\n", when_created); 239#endif 240 if (scheduled()) { 241#if TRACING_ON 242 cprintf(" Scheduled at %d\n", when_scheduled); 243#endif 244 cprintf(" Scheduled for %d\n", when()); 245 } 246 else { 247 cprintf(" Not Scheduled\n"); 248 } 249} 250