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 "base/trace.hh"
41 #include "cpu/base.hh"
42 #include "cpu/cpuevent.hh"
43 #include "cpu/thread_context.hh"
44 #include "cpu/profile.hh"
45 #include "params/BaseCPU.hh"
46 #include "sim/sim_exit.hh"
47 #include "sim/process.hh"
48 #include "sim/sim_events.hh"
49 #include "sim/system.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(BaseCPU
*_cpu
, Tick ival
)
64 : Event(Event::Progress_Event_Pri
), interval(ival
), lastNumInst(0),
68 cpu
->schedule(this, curTick
+ interval
);
72 CPUProgressEvent::process()
74 Counter temp
= cpu
->totalInstructions();
76 double ipc
= double(temp
- lastNumInst
) / (interval
/ cpu
->ticks(1));
78 DPRINTFN("%s progress event, instructions committed: %lli, IPC: %0.8d\n",
79 cpu
->name(), temp
- lastNumInst
, ipc
);
82 cprintf("%lli: %s progress event, instructions committed: %lli\n",
83 curTick
, cpu
->name(), temp
- lastNumInst
);
86 cpu
->schedule(this, curTick
+ interval
);
90 CPUProgressEvent::description() const
92 return "CPU Progress";
96 BaseCPU::BaseCPU(Params
*p
)
97 : MemObject(p
), clock(p
->clock
), instCnt(0), _cpuId(p
->cpu_id
),
98 interrupts(p
->interrupts
),
99 number_of_threads(p
->numThreads
), system(p
->system
),
102 BaseCPU::BaseCPU(Params
*p
)
103 : MemObject(p
), clock(p
->clock
), _cpuId(p
->cpu_id
),
104 number_of_threads(p
->numThreads
), system(p
->system
),
108 // currentTick = curTick;
110 // if Python did not provide a valid ID, do it here
112 _cpuId
= cpuList
.size();
115 // add self to global list of CPUs
116 cpuList
.push_back(this);
118 DPRINTF(SyscallVerbose
, "Constructing CPU with id %d\n", _cpuId
);
120 if (number_of_threads
> maxThreadsPerCPU
)
121 maxThreadsPerCPU
= number_of_threads
;
123 // allocate per-thread instruction-based event queues
124 comInstEventQueue
= new EventQueue
*[number_of_threads
];
125 for (int i
= 0; i
< number_of_threads
; ++i
)
126 comInstEventQueue
[i
] = new EventQueue("instruction-based event queue");
129 // set up instruction-count-based termination events, if any
131 if (p
->max_insts_any_thread
!= 0) {
132 const char *cause
= "a thread reached the max instruction count";
133 for (int i
= 0; i
< number_of_threads
; ++i
) {
134 Event
*event
= new SimLoopExitEvent(cause
, 0);
135 comInstEventQueue
[i
]->schedule(event
, p
->max_insts_any_thread
);
139 if (p
->max_insts_all_threads
!= 0) {
140 const char *cause
= "all threads reached the max instruction count";
142 // allocate & initialize shared downcounter: each event will
143 // decrement this when triggered; simulation will terminate
144 // when counter reaches 0
145 int *counter
= new int;
146 *counter
= number_of_threads
;
147 for (int i
= 0; i
< number_of_threads
; ++i
) {
148 Event
*event
= new CountedExitEvent(cause
, *counter
);
149 comInstEventQueue
[i
]->schedule(event
, p
->max_insts_any_thread
);
153 // allocate per-thread load-based event queues
154 comLoadEventQueue
= new EventQueue
*[number_of_threads
];
155 for (int i
= 0; i
< number_of_threads
; ++i
)
156 comLoadEventQueue
[i
] = new EventQueue("load-based event queue");
159 // set up instruction-count-based termination events, if any
161 if (p
->max_loads_any_thread
!= 0) {
162 const char *cause
= "a thread reached the max load count";
163 for (int i
= 0; i
< number_of_threads
; ++i
) {
164 Event
*event
= new SimLoopExitEvent(cause
, 0);
165 comLoadEventQueue
[i
]->schedule(event
, p
->max_loads_any_thread
);
169 if (p
->max_loads_all_threads
!= 0) {
170 const char *cause
= "all threads reached the max load count";
171 // allocate & initialize shared downcounter: each event will
172 // decrement this when triggered; simulation will terminate
173 // when counter reaches 0
174 int *counter
= new int;
175 *counter
= number_of_threads
;
176 for (int i
= 0; i
< number_of_threads
; ++i
) {
177 Event
*event
= new CountedExitEvent(cause
, *counter
);
178 comLoadEventQueue
[i
]->schedule(event
, p
->max_loads_all_threads
);
182 functionTracingEnabled
= false;
183 if (p
->function_trace
) {
184 functionTraceStream
= simout
.find(csprintf("ftrace.%s", name()));
185 currentFunctionStart
= currentFunctionEnd
= 0;
186 functionEntryTick
= p
->function_trace_start
;
188 if (p
->function_trace_start
== 0) {
189 functionTracingEnabled
= true;
191 typedef EventWrapper
<BaseCPU
, &BaseCPU::enableFunctionTrace
> wrap
;
192 Event
*event
= new wrap(this, true);
193 schedule(event
, p
->function_trace_start
);
197 interrupts
->setCPU(this);
200 if (params()->profile
)
201 profileEvent
= new ProfileEvent(this, params()->profile
);
203 tracer
= params()->tracer
;
207 BaseCPU::enableFunctionTrace()
209 functionTracingEnabled
= true;
219 if (!params()->defer_registration
)
220 registerThreadContexts();
227 if (!params()->defer_registration
&& profileEvent
)
228 schedule(profileEvent
, curTick
);
231 if (params()->progress_interval
) {
232 Tick num_ticks
= ticks(params()->progress_interval
);
233 Event
*event
= new CPUProgressEvent(this, num_ticks
);
234 schedule(event
, curTick
+ num_ticks
);
242 using namespace Stats
;
245 .name(name() + ".numCycles")
246 .desc("number of cpu cycles simulated")
249 int size
= threadContexts
.size();
251 for (int i
= 0; i
< size
; ++i
) {
252 stringstream namestr
;
253 ccprintf(namestr
, "%s.ctx%d", name(), i
);
254 threadContexts
[i
]->regStats(namestr
.str());
256 } else if (size
== 1)
257 threadContexts
[0]->regStats(name());
266 Tick next_tick
= curTick
- phase
+ clock
- 1;
267 next_tick
-= (next_tick
% clock
);
273 BaseCPU::nextCycle(Tick begin_tick
)
275 Tick next_tick
= begin_tick
;
276 if (next_tick
% clock
!= 0)
277 next_tick
= next_tick
- (next_tick
% clock
) + clock
;
280 assert(next_tick
>= curTick
);
285 BaseCPU::registerThreadContexts()
287 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
288 ThreadContext
*tc
= threadContexts
[i
];
290 /** This is so that contextId and cpuId match where there is a
291 * 1cpu:1context relationship. Otherwise, the order of registration
292 * could affect the assignment and cpu 1 could have context id 3, for
293 * example. We may even want to do something like this for SMT so that
294 * cpu 0 has the lowest thread contexts and cpu N has the highest, but
295 * I'll just do this for now
297 if (number_of_threads
== 1)
298 tc
->setContextId(system
->registerThreadContext(tc
, _cpuId
));
300 tc
->setContextId(system
->registerThreadContext(tc
));
302 tc
->getProcessPtr()->assignThreadContext(tc
->contextId());
309 BaseCPU::findContext(ThreadContext
*tc
)
311 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
312 if (tc
== threadContexts
[i
])
321 // panic("This CPU doesn't support sampling!");
323 if (profileEvent
&& profileEvent
->scheduled())
324 deschedule(profileEvent
);
329 BaseCPU::takeOverFrom(BaseCPU
*oldCPU
, Port
*ic
, Port
*dc
)
331 assert(threadContexts
.size() == oldCPU
->threadContexts
.size());
333 _cpuId
= oldCPU
->cpuId();
335 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
336 ThreadContext
*newTC
= threadContexts
[i
];
337 ThreadContext
*oldTC
= oldCPU
->threadContexts
[i
];
339 newTC
->takeOverFrom(oldTC
);
341 CpuEvent::replaceThreadContext(oldTC
, newTC
);
343 assert(newTC
->contextId() == oldTC
->contextId());
344 assert(newTC
->threadId() == oldTC
->threadId());
345 system
->replaceThreadContext(newTC
, newTC
->contextId());
347 /* This code no longer works since the zero register (e.g.,
348 * r31 on Alpha) doesn't necessarily contain zero at this
351 ThreadContext::compare(oldTC, newTC);
356 interrupts
= oldCPU
->interrupts
;
357 interrupts
->setCPU(this);
359 for (int i
= 0; i
< threadContexts
.size(); ++i
)
360 threadContexts
[i
]->profileClear();
363 schedule(profileEvent
, curTick
);
366 // Connect new CPU to old CPU's memory only if new CPU isn't
367 // connected to anything. Also connect old CPU's memory to new
369 if (!ic
->isConnected()) {
370 Port
*peer
= oldCPU
->getPort("icache_port")->getPeer();
375 if (!dc
->isConnected()) {
376 Port
*peer
= oldCPU
->getPort("dcache_port")->getPeer();
384 BaseCPU::ProfileEvent::ProfileEvent(BaseCPU
*_cpu
, Tick _interval
)
385 : cpu(_cpu
), interval(_interval
)
389 BaseCPU::ProfileEvent::process()
391 for (int i
= 0, size
= cpu
->threadContexts
.size(); i
< size
; ++i
) {
392 ThreadContext
*tc
= cpu
->threadContexts
[i
];
396 cpu
->schedule(this, curTick
+ interval
);
400 BaseCPU::serialize(std::ostream
&os
)
402 SERIALIZE_SCALAR(instCnt
);
403 interrupts
->serialize(os
);
407 BaseCPU::unserialize(Checkpoint
*cp
, const std::string
§ion
)
409 UNSERIALIZE_SCALAR(instCnt
);
410 interrupts
->unserialize(cp
, section
);
413 #endif // FULL_SYSTEM
416 BaseCPU::traceFunctionsInternal(Addr pc
)
418 if (!debugSymbolTable
)
421 // if pc enters different function, print new function symbol and
422 // update saved range. Otherwise do nothing.
423 if (pc
< currentFunctionStart
|| pc
>= currentFunctionEnd
) {
425 bool found
= debugSymbolTable
->findNearestSymbol(pc
, sym_str
,
426 currentFunctionStart
,
430 // no symbol found: use addr as label
431 sym_str
= csprintf("0x%x", pc
);
432 currentFunctionStart
= pc
;
433 currentFunctionEnd
= pc
+ 1;
436 ccprintf(*functionTraceStream
, " (%d)\n%d: %s",
437 curTick
- functionEntryTick
, curTick
, sym_str
);
438 functionEntryTick
= curTick
;