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),
65 cpu(_cpu
), _repeatEvent(true)
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, total committed:%i, progress insts committed: "
79 "%lli, IPC: %0.8d\n", cpu
->name(), temp
, temp
- lastNumInst
,
83 cprintf("%lli: %s progress event, total committed:%i, progress insts "
84 "committed: %lli\n", curTick
, cpu
->name(), temp
,
90 cpu
->schedule(this, curTick
+ _interval
);
94 CPUProgressEvent::description() const
96 return "CPU Progress";
100 BaseCPU::BaseCPU(Params
*p
)
101 : MemObject(p
), clock(p
->clock
), instCnt(0), _cpuId(p
->cpu_id
),
102 interrupts(p
->interrupts
),
103 numThreads(p
->numThreads
), system(p
->system
),
106 BaseCPU::BaseCPU(Params
*p
)
107 : MemObject(p
), clock(p
->clock
), _cpuId(p
->cpu_id
),
108 numThreads(p
->numThreads
), system(p
->system
),
112 // currentTick = curTick;
114 // if Python did not provide a valid ID, do it here
116 _cpuId
= cpuList
.size();
119 // add self to global list of CPUs
120 cpuList
.push_back(this);
122 DPRINTF(SyscallVerbose
, "Constructing CPU with id %d\n", _cpuId
);
124 if (numThreads
> maxThreadsPerCPU
)
125 maxThreadsPerCPU
= numThreads
;
127 // allocate per-thread instruction-based event queues
128 comInstEventQueue
= new EventQueue
*[numThreads
];
129 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
)
130 comInstEventQueue
[tid
] =
131 new EventQueue("instruction-based event queue");
134 // set up instruction-count-based termination events, if any
136 if (p
->max_insts_any_thread
!= 0) {
137 const char *cause
= "a thread reached the max instruction count";
138 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
) {
139 Event
*event
= new SimLoopExitEvent(cause
, 0);
140 comInstEventQueue
[tid
]->schedule(event
, p
->max_insts_any_thread
);
144 if (p
->max_insts_all_threads
!= 0) {
145 const char *cause
= "all threads reached the max instruction count";
147 // allocate & initialize shared downcounter: each event will
148 // decrement this when triggered; simulation will terminate
149 // when counter reaches 0
150 int *counter
= new int;
151 *counter
= numThreads
;
152 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
) {
153 Event
*event
= new CountedExitEvent(cause
, *counter
);
154 comInstEventQueue
[tid
]->schedule(event
, p
->max_insts_any_thread
);
158 // allocate per-thread load-based event queues
159 comLoadEventQueue
= new EventQueue
*[numThreads
];
160 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
)
161 comLoadEventQueue
[tid
] = new EventQueue("load-based event queue");
164 // set up instruction-count-based termination events, if any
166 if (p
->max_loads_any_thread
!= 0) {
167 const char *cause
= "a thread reached the max load count";
168 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
) {
169 Event
*event
= new SimLoopExitEvent(cause
, 0);
170 comLoadEventQueue
[tid
]->schedule(event
, p
->max_loads_any_thread
);
174 if (p
->max_loads_all_threads
!= 0) {
175 const char *cause
= "all threads reached the max load count";
176 // allocate & initialize shared downcounter: each event will
177 // decrement this when triggered; simulation will terminate
178 // when counter reaches 0
179 int *counter
= new int;
180 *counter
= numThreads
;
181 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
) {
182 Event
*event
= new CountedExitEvent(cause
, *counter
);
183 comLoadEventQueue
[tid
]->schedule(event
, p
->max_loads_all_threads
);
187 functionTracingEnabled
= false;
188 if (p
->function_trace
) {
189 functionTraceStream
= simout
.find(csprintf("ftrace.%s", name()));
190 currentFunctionStart
= currentFunctionEnd
= 0;
191 functionEntryTick
= p
->function_trace_start
;
193 if (p
->function_trace_start
== 0) {
194 functionTracingEnabled
= true;
196 typedef EventWrapper
<BaseCPU
, &BaseCPU::enableFunctionTrace
> wrap
;
197 Event
*event
= new wrap(this, true);
198 schedule(event
, p
->function_trace_start
);
202 interrupts
->setCPU(this);
205 if (params()->profile
)
206 profileEvent
= new ProfileEvent(this, params()->profile
);
208 tracer
= params()->tracer
;
212 BaseCPU::enableFunctionTrace()
214 functionTracingEnabled
= true;
224 if (!params()->defer_registration
)
225 registerThreadContexts();
232 if (!params()->defer_registration
&& profileEvent
)
233 schedule(profileEvent
, curTick
);
236 if (params()->progress_interval
) {
237 Tick num_ticks
= ticks(params()->progress_interval
);
240 event
= new CPUProgressEvent(this, num_ticks
);
248 using namespace Stats
;
251 .name(name() + ".numCycles")
252 .desc("number of cpu cycles simulated")
255 int size
= threadContexts
.size();
257 for (int i
= 0; i
< size
; ++i
) {
258 stringstream namestr
;
259 ccprintf(namestr
, "%s.ctx%d", name(), i
);
260 threadContexts
[i
]->regStats(namestr
.str());
262 } else if (size
== 1)
263 threadContexts
[0]->regStats(name());
272 Tick next_tick
= curTick
- phase
+ clock
- 1;
273 next_tick
-= (next_tick
% clock
);
279 BaseCPU::nextCycle(Tick begin_tick
)
281 Tick next_tick
= begin_tick
;
282 if (next_tick
% clock
!= 0)
283 next_tick
= next_tick
- (next_tick
% clock
) + clock
;
286 assert(next_tick
>= curTick
);
291 BaseCPU::registerThreadContexts()
293 ThreadID size
= threadContexts
.size();
294 for (ThreadID tid
= 0; tid
< size
; ++tid
) {
295 ThreadContext
*tc
= threadContexts
[tid
];
297 /** This is so that contextId and cpuId match where there is a
298 * 1cpu:1context relationship. Otherwise, the order of registration
299 * could affect the assignment and cpu 1 could have context id 3, for
300 * example. We may even want to do something like this for SMT so that
301 * cpu 0 has the lowest thread contexts and cpu N has the highest, but
302 * I'll just do this for now
305 tc
->setContextId(system
->registerThreadContext(tc
, _cpuId
));
307 tc
->setContextId(system
->registerThreadContext(tc
));
309 tc
->getProcessPtr()->assignThreadContext(tc
->contextId());
316 BaseCPU::findContext(ThreadContext
*tc
)
318 ThreadID size
= threadContexts
.size();
319 for (ThreadID tid
= 0; tid
< size
; ++tid
) {
320 if (tc
== threadContexts
[tid
])
329 // panic("This CPU doesn't support sampling!");
331 if (profileEvent
&& profileEvent
->scheduled())
332 deschedule(profileEvent
);
337 BaseCPU::takeOverFrom(BaseCPU
*oldCPU
, Port
*ic
, Port
*dc
)
339 assert(threadContexts
.size() == oldCPU
->threadContexts
.size());
341 _cpuId
= oldCPU
->cpuId();
343 ThreadID size
= threadContexts
.size();
344 for (ThreadID i
= 0; i
< size
; ++i
) {
345 ThreadContext
*newTC
= threadContexts
[i
];
346 ThreadContext
*oldTC
= oldCPU
->threadContexts
[i
];
348 newTC
->takeOverFrom(oldTC
);
350 CpuEvent::replaceThreadContext(oldTC
, newTC
);
352 assert(newTC
->contextId() == oldTC
->contextId());
353 assert(newTC
->threadId() == oldTC
->threadId());
354 system
->replaceThreadContext(newTC
, newTC
->contextId());
356 /* This code no longer works since the zero register (e.g.,
357 * r31 on Alpha) doesn't necessarily contain zero at this
360 ThreadContext::compare(oldTC, newTC);
365 interrupts
= oldCPU
->interrupts
;
366 interrupts
->setCPU(this);
368 for (ThreadID i
= 0; i
< size
; ++i
)
369 threadContexts
[i
]->profileClear();
372 schedule(profileEvent
, curTick
);
375 // Connect new CPU to old CPU's memory only if new CPU isn't
376 // connected to anything. Also connect old CPU's memory to new
378 if (!ic
->isConnected()) {
379 Port
*peer
= oldCPU
->getPort("icache_port")->getPeer();
384 if (!dc
->isConnected()) {
385 Port
*peer
= oldCPU
->getPort("dcache_port")->getPeer();
393 BaseCPU::ProfileEvent::ProfileEvent(BaseCPU
*_cpu
, Tick _interval
)
394 : cpu(_cpu
), interval(_interval
)
398 BaseCPU::ProfileEvent::process()
400 ThreadID size
= cpu
->threadContexts
.size();
401 for (ThreadID i
= 0; i
< size
; ++i
) {
402 ThreadContext
*tc
= cpu
->threadContexts
[i
];
406 cpu
->schedule(this, curTick
+ interval
);
410 BaseCPU::serialize(std::ostream
&os
)
412 SERIALIZE_SCALAR(instCnt
);
413 interrupts
->serialize(os
);
417 BaseCPU::unserialize(Checkpoint
*cp
, const std::string
§ion
)
419 UNSERIALIZE_SCALAR(instCnt
);
420 interrupts
->unserialize(cp
, section
);
423 #endif // FULL_SYSTEM
426 BaseCPU::traceFunctionsInternal(Addr pc
)
428 if (!debugSymbolTable
)
431 // if pc enters different function, print new function symbol and
432 // update saved range. Otherwise do nothing.
433 if (pc
< currentFunctionStart
|| pc
>= currentFunctionEnd
) {
435 bool found
= debugSymbolTable
->findNearestSymbol(pc
, sym_str
,
436 currentFunctionStart
,
440 // no symbol found: use addr as label
441 sym_str
= csprintf("0x%x", pc
);
442 currentFunctionStart
= pc
;
443 currentFunctionEnd
= pc
+ 1;
446 ccprintf(*functionTraceStream
, " (%d)\n%d: %s",
447 curTick
- functionEntryTick
, curTick
, sym_str
);
448 functionEntryTick
= curTick
;