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