3b8d85c9c12bb708a0907ae44389aeab1eb12d76
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";
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
);
193 BaseCPU::Params::Params()
202 BaseCPU::enableFunctionTrace()
204 functionTracingEnabled
= true;
214 if (!params
->deferRegistration
)
215 registerThreadContexts();
222 if (!params
->deferRegistration
&& profileEvent
)
223 profileEvent
->schedule(curTick
);
226 if (params
->progress_interval
) {
227 new CPUProgressEvent(&mainEventQueue
,
228 cycles(params
->progress_interval
),
237 using namespace Stats
;
240 .name(name() + ".numCycles")
241 .desc("number of cpu cycles simulated")
244 int size
= threadContexts
.size();
246 for (int i
= 0; i
< size
; ++i
) {
247 stringstream namestr
;
248 ccprintf(namestr
, "%s.ctx%d", name(), i
);
249 threadContexts
[i
]->regStats(namestr
.str());
251 } else if (size
== 1)
252 threadContexts
[0]->regStats(name());
261 Tick next_tick
= curTick
- phase
+ clock
- 1;
262 next_tick
-= (next_tick
% clock
);
268 BaseCPU::nextCycle(Tick begin_tick
)
270 Tick next_tick
= begin_tick
;
271 if (next_tick
% clock
!= 0)
272 next_tick
= next_tick
- (next_tick
% clock
) + clock
;
275 assert(next_tick
>= curTick
);
280 BaseCPU::registerThreadContexts()
282 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
283 ThreadContext
*tc
= threadContexts
[i
];
286 int id
= params
->cpu_id
;
290 tc
->setCpuId(system
->registerThreadContext(tc
, id
));
292 tc
->setCpuId(tc
->getProcessPtr()->registerThreadContext(tc
));
299 BaseCPU::findContext(ThreadContext
*tc
)
301 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
302 if (tc
== threadContexts
[i
])
311 // panic("This CPU doesn't support sampling!");
313 if (profileEvent
&& profileEvent
->scheduled())
314 profileEvent
->deschedule();
319 BaseCPU::takeOverFrom(BaseCPU
*oldCPU
, Port
*ic
, Port
*dc
)
321 assert(threadContexts
.size() == oldCPU
->threadContexts
.size());
323 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
324 ThreadContext
*newTC
= threadContexts
[i
];
325 ThreadContext
*oldTC
= oldCPU
->threadContexts
[i
];
327 newTC
->takeOverFrom(oldTC
);
329 CpuEvent::replaceThreadContext(oldTC
, newTC
);
331 assert(newTC
->readCpuId() == oldTC
->readCpuId());
333 system
->replaceThreadContext(newTC
, newTC
->readCpuId());
335 assert(newTC
->getProcessPtr() == oldTC
->getProcessPtr());
336 newTC
->getProcessPtr()->replaceThreadContext(newTC
, newTC
->readCpuId());
339 // TheISA::compareXCs(oldXC, newXC);
343 interrupts
= oldCPU
->interrupts
;
345 for (int i
= 0; i
< threadContexts
.size(); ++i
)
346 threadContexts
[i
]->profileClear();
348 // The Sampler must take care of this!
350 // profileEvent->schedule(curTick);
353 // Connect new CPU to old CPU's memory only if new CPU isn't
354 // connected to anything. Also connect old CPU's memory to new
357 if (ic
->getPeer() == NULL
) {
358 peer
= oldCPU
->getPort("icache_port")->getPeer();
361 peer
= ic
->getPeer();
365 if (dc
->getPeer() == NULL
) {
366 peer
= oldCPU
->getPort("dcache_port")->getPeer();
369 peer
= dc
->getPeer();
376 BaseCPU::ProfileEvent::ProfileEvent(BaseCPU
*_cpu
, int _interval
)
377 : Event(&mainEventQueue
), cpu(_cpu
), interval(_interval
)
381 BaseCPU::ProfileEvent::process()
383 for (int i
= 0, size
= cpu
->threadContexts
.size(); i
< size
; ++i
) {
384 ThreadContext
*tc
= cpu
->threadContexts
[i
];
388 schedule(curTick
+ interval
);
392 BaseCPU::post_interrupt(int int_num
, int index
)
394 interrupts
.post(int_num
, index
);
398 BaseCPU::clear_interrupt(int int_num
, int index
)
400 interrupts
.clear(int_num
, index
);
404 BaseCPU::clear_interrupts()
406 interrupts
.clear_all();
410 BaseCPU::get_interrupts(int int_num
)
412 return interrupts
.get_vec(int_num
);
416 BaseCPU::serialize(std::ostream
&os
)
418 SERIALIZE_SCALAR(instCnt
);
419 interrupts
.serialize(os
);
423 BaseCPU::unserialize(Checkpoint
*cp
, const std::string
§ion
)
425 UNSERIALIZE_SCALAR(instCnt
);
426 interrupts
.unserialize(cp
, section
);
429 #endif // FULL_SYSTEM
432 BaseCPU::traceFunctionsInternal(Addr pc
)
434 if (!debugSymbolTable
)
437 // if pc enters different function, print new function symbol and
438 // update saved range. Otherwise do nothing.
439 if (pc
< currentFunctionStart
|| pc
>= currentFunctionEnd
) {
441 bool found
= debugSymbolTable
->findNearestSymbol(pc
, sym_str
,
442 currentFunctionStart
,
446 // no symbol found: use addr as label
447 sym_str
= csprintf("0x%x", pc
);
448 currentFunctionStart
= pc
;
449 currentFunctionEnd
= pc
+ 1;
452 ccprintf(*functionTraceStream
, " (%d)\n%d: %s",
453 curTick
- functionEntryTick
, curTick
, sym_str
);
454 functionEntryTick
= curTick
;