2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Steve Reinhardt
36 #include "base/cprintf.hh"
37 #include "base/loader/symtab.hh"
38 #include "base/misc.hh"
39 #include "base/output.hh"
40 #include "cpu/base.hh"
41 #include "cpu/cpuevent.hh"
42 #include "cpu/thread_context.hh"
43 #include "cpu/profile.hh"
44 #include "sim/sim_exit.hh"
45 #include "sim/process.hh"
46 #include "sim/sim_events.hh"
47 #include "sim/system.hh"
49 #include "base/trace.hh"
52 #include "sim/stat_control.hh"
56 vector
<BaseCPU
*> BaseCPU::cpuList
;
58 // This variable reflects the max number of threads in any CPU. Be
59 // careful to only use it once all the CPUs that you care about have
61 int maxThreadsPerCPU
= 1;
63 CPUProgressEvent::CPUProgressEvent(EventQueue
*q
, Tick ival
,
65 : Event(q
, Event::Progress_Event_Pri
), interval(ival
),
66 lastNumInst(0), cpu(_cpu
)
69 schedule(curTick
+ interval
);
73 CPUProgressEvent::process()
75 Counter temp
= cpu
->totalInstructions();
77 double ipc
= double(temp
- lastNumInst
) / (interval
/ cpu
->cycles(1));
79 DPRINTFN("%s progress event, instructions committed: %lli, IPC: %0.8d\n",
80 cpu
->name(), temp
- lastNumInst
, ipc
);
83 cprintf("%lli: %s progress event, instructions committed: %lli\n",
84 curTick
, cpu
->name(), temp
- lastNumInst
);
87 schedule(curTick
+ interval
);
91 CPUProgressEvent::description()
93 return "CPU Progress event";
97 BaseCPU::BaseCPU(Params
*p
)
98 : MemObject(p
->name
), clock(p
->clock
), instCnt(0),
99 params(p
), number_of_threads(p
->numberOfThreads
), system(p
->system
),
102 BaseCPU::BaseCPU(Params
*p
)
103 : MemObject(p
->name
), clock(p
->clock
), params(p
),
104 number_of_threads(p
->numberOfThreads
), system(p
->system
),
108 // currentTick = curTick;
109 DPRINTF(FullCPU
, "BaseCPU: Creating object, mem address %#x.\n", this);
111 // add self to global list of CPUs
112 cpuList
.push_back(this);
114 DPRINTF(FullCPU
, "BaseCPU: CPU added to cpuList, mem address %#x.\n",
117 if (number_of_threads
> maxThreadsPerCPU
)
118 maxThreadsPerCPU
= number_of_threads
;
120 // allocate per-thread instruction-based event queues
121 comInstEventQueue
= new EventQueue
*[number_of_threads
];
122 for (int i
= 0; i
< number_of_threads
; ++i
)
123 comInstEventQueue
[i
] = new EventQueue("instruction-based event queue");
126 // set up instruction-count-based termination events, if any
128 if (p
->max_insts_any_thread
!= 0)
129 for (int i
= 0; i
< number_of_threads
; ++i
)
130 schedExitSimLoop("a thread reached the max instruction count",
131 p
->max_insts_any_thread
, 0,
132 comInstEventQueue
[i
]);
134 if (p
->max_insts_all_threads
!= 0) {
135 // allocate & initialize shared downcounter: each event will
136 // decrement this when triggered; simulation will terminate
137 // when counter reaches 0
138 int *counter
= new int;
139 *counter
= number_of_threads
;
140 for (int i
= 0; i
< number_of_threads
; ++i
)
141 new CountedExitEvent(comInstEventQueue
[i
],
142 "all threads reached the max instruction count",
143 p
->max_insts_all_threads
, *counter
);
146 // allocate per-thread load-based event queues
147 comLoadEventQueue
= new EventQueue
*[number_of_threads
];
148 for (int i
= 0; i
< number_of_threads
; ++i
)
149 comLoadEventQueue
[i
] = new EventQueue("load-based event queue");
152 // set up instruction-count-based termination events, if any
154 if (p
->max_loads_any_thread
!= 0)
155 for (int i
= 0; i
< number_of_threads
; ++i
)
156 schedExitSimLoop("a thread reached the max load count",
157 p
->max_loads_any_thread
, 0,
158 comLoadEventQueue
[i
]);
160 if (p
->max_loads_all_threads
!= 0) {
161 // allocate & initialize shared downcounter: each event will
162 // decrement this when triggered; simulation will terminate
163 // when counter reaches 0
164 int *counter
= new int;
165 *counter
= number_of_threads
;
166 for (int i
= 0; i
< number_of_threads
; ++i
)
167 new CountedExitEvent(comLoadEventQueue
[i
],
168 "all threads reached the max load count",
169 p
->max_loads_all_threads
, *counter
);
172 functionTracingEnabled
= false;
173 if (p
->functionTrace
) {
174 functionTraceStream
= simout
.find(csprintf("ftrace.%s", name()));
175 currentFunctionStart
= currentFunctionEnd
= 0;
176 functionEntryTick
= p
->functionTraceStart
;
178 if (p
->functionTraceStart
== 0) {
179 functionTracingEnabled
= true;
181 new EventWrapper
<BaseCPU
, &BaseCPU::enableFunctionTrace
>(this,
182 p
->functionTraceStart
,
189 profileEvent
= new ProfileEvent(this, params
->profile
);
191 tracer
= params
->tracer
;
194 BaseCPU::Params::Params()
204 BaseCPU::enableFunctionTrace()
206 functionTracingEnabled
= true;
216 if (!params
->deferRegistration
)
217 registerThreadContexts();
224 if (!params
->deferRegistration
&& profileEvent
)
225 profileEvent
->schedule(curTick
);
228 if (params
->progress_interval
) {
229 new CPUProgressEvent(&mainEventQueue
,
230 cycles(params
->progress_interval
),
239 using namespace Stats
;
242 .name(name() + ".numCycles")
243 .desc("number of cpu cycles simulated")
246 int size
= threadContexts
.size();
248 for (int i
= 0; i
< size
; ++i
) {
249 stringstream namestr
;
250 ccprintf(namestr
, "%s.ctx%d", name(), i
);
251 threadContexts
[i
]->regStats(namestr
.str());
253 } else if (size
== 1)
254 threadContexts
[0]->regStats(name());
263 Tick next_tick
= curTick
- phase
+ clock
- 1;
264 next_tick
-= (next_tick
% clock
);
270 BaseCPU::nextCycle(Tick begin_tick
)
272 Tick next_tick
= begin_tick
;
273 if (next_tick
% clock
!= 0)
274 next_tick
= next_tick
- (next_tick
% clock
) + clock
;
277 assert(next_tick
>= curTick
);
282 BaseCPU::registerThreadContexts()
284 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
285 ThreadContext
*tc
= threadContexts
[i
];
288 int id
= params
->cpu_id
;
292 tc
->setCpuId(system
->registerThreadContext(tc
, id
));
294 tc
->setCpuId(tc
->getProcessPtr()->registerThreadContext(tc
));
301 BaseCPU::findContext(ThreadContext
*tc
)
303 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
304 if (tc
== threadContexts
[i
])
313 // panic("This CPU doesn't support sampling!");
315 if (profileEvent
&& profileEvent
->scheduled())
316 profileEvent
->deschedule();
321 BaseCPU::takeOverFrom(BaseCPU
*oldCPU
, Port
*ic
, Port
*dc
)
323 assert(threadContexts
.size() == oldCPU
->threadContexts
.size());
325 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
326 ThreadContext
*newTC
= threadContexts
[i
];
327 ThreadContext
*oldTC
= oldCPU
->threadContexts
[i
];
329 newTC
->takeOverFrom(oldTC
);
331 CpuEvent::replaceThreadContext(oldTC
, newTC
);
333 assert(newTC
->readCpuId() == oldTC
->readCpuId());
335 system
->replaceThreadContext(newTC
, newTC
->readCpuId());
337 assert(newTC
->getProcessPtr() == oldTC
->getProcessPtr());
338 newTC
->getProcessPtr()->replaceThreadContext(newTC
, newTC
->readCpuId());
341 // TheISA::compareXCs(oldXC, newXC);
345 interrupts
= oldCPU
->interrupts
;
347 for (int i
= 0; i
< threadContexts
.size(); ++i
)
348 threadContexts
[i
]->profileClear();
350 // The Sampler must take care of this!
352 // profileEvent->schedule(curTick);
355 // Connect new CPU to old CPU's memory only if new CPU isn't
356 // connected to anything. Also connect old CPU's memory to new
359 if (ic
->getPeer() == NULL
) {
360 peer
= oldCPU
->getPort("icache_port")->getPeer();
363 peer
= ic
->getPeer();
367 if (dc
->getPeer() == NULL
) {
368 peer
= oldCPU
->getPort("dcache_port")->getPeer();
371 peer
= dc
->getPeer();
378 BaseCPU::ProfileEvent::ProfileEvent(BaseCPU
*_cpu
, int _interval
)
379 : Event(&mainEventQueue
), cpu(_cpu
), interval(_interval
)
383 BaseCPU::ProfileEvent::process()
385 for (int i
= 0, size
= cpu
->threadContexts
.size(); i
< size
; ++i
) {
386 ThreadContext
*tc
= cpu
->threadContexts
[i
];
390 schedule(curTick
+ interval
);
394 BaseCPU::post_interrupt(int int_num
, int index
)
396 interrupts
.post(int_num
, index
);
400 BaseCPU::clear_interrupt(int int_num
, int index
)
402 interrupts
.clear(int_num
, index
);
406 BaseCPU::clear_interrupts()
408 interrupts
.clear_all();
412 BaseCPU::get_interrupts(int int_num
)
414 return interrupts
.get_vec(int_num
);
418 BaseCPU::serialize(std::ostream
&os
)
420 SERIALIZE_SCALAR(instCnt
);
421 interrupts
.serialize(os
);
425 BaseCPU::unserialize(Checkpoint
*cp
, const std::string
§ion
)
427 UNSERIALIZE_SCALAR(instCnt
);
428 interrupts
.unserialize(cp
, section
);
431 #endif // FULL_SYSTEM
434 BaseCPU::traceFunctionsInternal(Addr pc
)
436 if (!debugSymbolTable
)
439 // if pc enters different function, print new function symbol and
440 // update saved range. Otherwise do nothing.
441 if (pc
< currentFunctionStart
|| pc
>= currentFunctionEnd
) {
443 bool found
= debugSymbolTable
->findNearestSymbol(pc
, sym_str
,
444 currentFunctionStart
,
448 // no symbol found: use addr as label
449 sym_str
= csprintf("0x%x", pc
);
450 currentFunctionStart
= pc
;
451 currentFunctionEnd
= pc
+ 1;
454 ccprintf(*functionTraceStream
, " (%d)\n%d: %s",
455 curTick
- functionEntryTick
, curTick
, sym_str
);
456 functionEntryTick
= curTick
;