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
->ticks(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() const
93 return "CPU Progress";
97 BaseCPU::BaseCPU(Params
*p
)
98 : MemObject(makeParams(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(makeParams(p
->name
)), clock(p
->clock
), params(p
),
104 number_of_threads(p
->numberOfThreads
), system(p
->system
),
108 // currentTick = curTick;
110 // add self to global list of CPUs
111 cpuList
.push_back(this);
113 if (number_of_threads
> maxThreadsPerCPU
)
114 maxThreadsPerCPU
= number_of_threads
;
116 // allocate per-thread instruction-based event queues
117 comInstEventQueue
= new EventQueue
*[number_of_threads
];
118 for (int i
= 0; i
< number_of_threads
; ++i
)
119 comInstEventQueue
[i
] = new EventQueue("instruction-based event queue");
122 // set up instruction-count-based termination events, if any
124 if (p
->max_insts_any_thread
!= 0)
125 for (int i
= 0; i
< number_of_threads
; ++i
)
126 schedExitSimLoop("a thread reached the max instruction count",
127 p
->max_insts_any_thread
, 0,
128 comInstEventQueue
[i
]);
130 if (p
->max_insts_all_threads
!= 0) {
131 // allocate & initialize shared downcounter: each event will
132 // decrement this when triggered; simulation will terminate
133 // when counter reaches 0
134 int *counter
= new int;
135 *counter
= number_of_threads
;
136 for (int i
= 0; i
< number_of_threads
; ++i
)
137 new CountedExitEvent(comInstEventQueue
[i
],
138 "all threads reached the max instruction count",
139 p
->max_insts_all_threads
, *counter
);
142 // allocate per-thread load-based event queues
143 comLoadEventQueue
= new EventQueue
*[number_of_threads
];
144 for (int i
= 0; i
< number_of_threads
; ++i
)
145 comLoadEventQueue
[i
] = new EventQueue("load-based event queue");
148 // set up instruction-count-based termination events, if any
150 if (p
->max_loads_any_thread
!= 0)
151 for (int i
= 0; i
< number_of_threads
; ++i
)
152 schedExitSimLoop("a thread reached the max load count",
153 p
->max_loads_any_thread
, 0,
154 comLoadEventQueue
[i
]);
156 if (p
->max_loads_all_threads
!= 0) {
157 // allocate & initialize shared downcounter: each event will
158 // decrement this when triggered; simulation will terminate
159 // when counter reaches 0
160 int *counter
= new int;
161 *counter
= number_of_threads
;
162 for (int i
= 0; i
< number_of_threads
; ++i
)
163 new CountedExitEvent(comLoadEventQueue
[i
],
164 "all threads reached the max load count",
165 p
->max_loads_all_threads
, *counter
);
168 functionTracingEnabled
= false;
169 if (p
->functionTrace
) {
170 functionTraceStream
= simout
.find(csprintf("ftrace.%s", name()));
171 currentFunctionStart
= currentFunctionEnd
= 0;
172 functionEntryTick
= p
->functionTraceStart
;
174 if (p
->functionTraceStart
== 0) {
175 functionTracingEnabled
= true;
177 new EventWrapper
<BaseCPU
, &BaseCPU::enableFunctionTrace
>(this,
178 p
->functionTraceStart
,
185 profileEvent
= new ProfileEvent(this, params
->profile
);
187 tracer
= params
->tracer
;
190 BaseCPU::Params::Params()
200 BaseCPU::enableFunctionTrace()
202 functionTracingEnabled
= true;
212 if (!params
->deferRegistration
)
213 registerThreadContexts();
220 if (!params
->deferRegistration
&& profileEvent
)
221 profileEvent
->schedule(curTick
);
224 if (params
->progress_interval
) {
225 new CPUProgressEvent(&mainEventQueue
,
226 ticks(params
->progress_interval
),
235 using namespace Stats
;
238 .name(name() + ".numCycles")
239 .desc("number of cpu cycles simulated")
242 int size
= threadContexts
.size();
244 for (int i
= 0; i
< size
; ++i
) {
245 stringstream namestr
;
246 ccprintf(namestr
, "%s.ctx%d", name(), i
);
247 threadContexts
[i
]->regStats(namestr
.str());
249 } else if (size
== 1)
250 threadContexts
[0]->regStats(name());
259 Tick next_tick
= curTick
- phase
+ clock
- 1;
260 next_tick
-= (next_tick
% clock
);
266 BaseCPU::nextCycle(Tick begin_tick
)
268 Tick next_tick
= begin_tick
;
269 if (next_tick
% clock
!= 0)
270 next_tick
= next_tick
- (next_tick
% clock
) + clock
;
273 assert(next_tick
>= curTick
);
278 BaseCPU::registerThreadContexts()
280 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
281 ThreadContext
*tc
= threadContexts
[i
];
284 int id
= params
->cpu_id
;
288 tc
->setCpuId(system
->registerThreadContext(tc
, id
));
290 tc
->setCpuId(tc
->getProcessPtr()->registerThreadContext(tc
));
297 BaseCPU::findContext(ThreadContext
*tc
)
299 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
300 if (tc
== threadContexts
[i
])
309 // panic("This CPU doesn't support sampling!");
311 if (profileEvent
&& profileEvent
->scheduled())
312 profileEvent
->deschedule();
317 BaseCPU::takeOverFrom(BaseCPU
*oldCPU
, Port
*ic
, Port
*dc
)
319 assert(threadContexts
.size() == oldCPU
->threadContexts
.size());
321 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
322 ThreadContext
*newTC
= threadContexts
[i
];
323 ThreadContext
*oldTC
= oldCPU
->threadContexts
[i
];
325 newTC
->takeOverFrom(oldTC
);
327 CpuEvent::replaceThreadContext(oldTC
, newTC
);
329 assert(newTC
->readCpuId() == oldTC
->readCpuId());
331 system
->replaceThreadContext(newTC
, newTC
->readCpuId());
333 assert(newTC
->getProcessPtr() == oldTC
->getProcessPtr());
334 newTC
->getProcessPtr()->replaceThreadContext(newTC
, newTC
->readCpuId());
338 ThreadContext::compare(oldTC
, newTC
);
342 interrupts
= oldCPU
->interrupts
;
344 for (int i
= 0; i
< threadContexts
.size(); ++i
)
345 threadContexts
[i
]->profileClear();
348 profileEvent
->schedule(curTick
);
351 // Connect new CPU to old CPU's memory only if new CPU isn't
352 // connected to anything. Also connect old CPU's memory to new
354 if (!ic
->isConnected()) {
355 Port
*peer
= oldCPU
->getPort("icache_port")->getPeer();
360 if (!dc
->isConnected()) {
361 Port
*peer
= oldCPU
->getPort("dcache_port")->getPeer();
369 BaseCPU::ProfileEvent::ProfileEvent(BaseCPU
*_cpu
, int _interval
)
370 : Event(&mainEventQueue
), cpu(_cpu
), interval(_interval
)
374 BaseCPU::ProfileEvent::process()
376 for (int i
= 0, size
= cpu
->threadContexts
.size(); i
< size
; ++i
) {
377 ThreadContext
*tc
= cpu
->threadContexts
[i
];
381 schedule(curTick
+ interval
);
385 BaseCPU::post_interrupt(int int_num
, int index
)
387 interrupts
.post(int_num
, index
);
391 BaseCPU::clear_interrupt(int int_num
, int index
)
393 interrupts
.clear(int_num
, index
);
397 BaseCPU::clear_interrupts()
399 interrupts
.clear_all();
403 BaseCPU::get_interrupts(int int_num
)
405 return interrupts
.get_vec(int_num
);
409 BaseCPU::serialize(std::ostream
&os
)
411 SERIALIZE_SCALAR(instCnt
);
412 interrupts
.serialize(os
);
416 BaseCPU::unserialize(Checkpoint
*cp
, const std::string
§ion
)
418 UNSERIALIZE_SCALAR(instCnt
);
419 interrupts
.unserialize(cp
, section
);
422 #endif // FULL_SYSTEM
425 BaseCPU::traceFunctionsInternal(Addr pc
)
427 if (!debugSymbolTable
)
430 // if pc enters different function, print new function symbol and
431 // update saved range. Otherwise do nothing.
432 if (pc
< currentFunctionStart
|| pc
>= currentFunctionEnd
) {
434 bool found
= debugSymbolTable
->findNearestSymbol(pc
, sym_str
,
435 currentFunctionStart
,
439 // no symbol found: use addr as label
440 sym_str
= csprintf("0x%x", pc
);
441 currentFunctionStart
= pc
;
442 currentFunctionEnd
= pc
+ 1;
445 ccprintf(*functionTraceStream
, " (%d)\n%d: %s",
446 curTick
- functionEntryTick
, curTick
, sym_str
);
447 functionEntryTick
= curTick
;