base.cc revision 5536:17c0c17726ff
1/* 2 * Copyright (c) 2002-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 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. 15 * 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. 27 * 28 * Authors: Steve Reinhardt 29 * Nathan Binkert 30 */ 31 32#include <iostream> 33#include <string> 34#include <sstream> 35 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" 50 51// Hack 52#include "sim/stat_control.hh" 53 54using namespace std; 55 56vector<BaseCPU *> BaseCPU::cpuList; 57 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 60// been initialized 61int maxThreadsPerCPU = 1; 62 63CPUProgressEvent::CPUProgressEvent(EventQueue *q, Tick ival, 64 BaseCPU *_cpu) 65 : Event(q, Event::Progress_Event_Pri), interval(ival), 66 lastNumInst(0), cpu(_cpu) 67{ 68 if (interval) 69 schedule(curTick + interval); 70} 71 72void 73CPUProgressEvent::process() 74{ 75 Counter temp = cpu->totalInstructions(); 76#ifndef NDEBUG 77 double ipc = double(temp - lastNumInst) / (interval / cpu->ticks(1)); 78 79 DPRINTFN("%s progress event, instructions committed: %lli, IPC: %0.8d\n", 80 cpu->name(), temp - lastNumInst, ipc); 81 ipc = 0.0; 82#else 83 cprintf("%lli: %s progress event, instructions committed: %lli\n", 84 curTick, cpu->name(), temp - lastNumInst); 85#endif 86 lastNumInst = temp; 87 schedule(curTick + interval); 88} 89 90const char * 91CPUProgressEvent::description() const 92{ 93 return "CPU Progress"; 94} 95 96#if FULL_SYSTEM 97BaseCPU::BaseCPU(Params *p) 98 : MemObject(p), clock(p->clock), instCnt(0), 99 number_of_threads(p->numThreads), system(p->system), 100 phase(p->phase) 101#else 102BaseCPU::BaseCPU(Params *p) 103 : MemObject(p), clock(p->clock), 104 number_of_threads(p->numThreads), system(p->system), 105 phase(p->phase) 106#endif 107{ 108// currentTick = curTick; 109 110 // add self to global list of CPUs 111 cpuList.push_back(this); 112 113 if (number_of_threads > maxThreadsPerCPU) 114 maxThreadsPerCPU = number_of_threads; 115 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"); 120 121 // 122 // set up instruction-count-based termination events, if any 123 // 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]); 129 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); 140 } 141 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"); 146 147 // 148 // set up instruction-count-based termination events, if any 149 // 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]); 155 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); 166 } 167 168 functionTracingEnabled = false; 169 if (p->function_trace) { 170 functionTraceStream = simout.find(csprintf("ftrace.%s", name())); 171 currentFunctionStart = currentFunctionEnd = 0; 172 functionEntryTick = p->function_trace_start; 173 174 if (p->function_trace_start == 0) { 175 functionTracingEnabled = true; 176 } else { 177 new EventWrapper<BaseCPU, 178 &BaseCPU::enableFunctionTrace>( 179 this, p->function_trace_start, true); 180 } 181 } 182#if FULL_SYSTEM 183 profileEvent = NULL; 184 if (params()->profile) 185 profileEvent = new ProfileEvent(this, params()->profile); 186#endif 187 tracer = params()->tracer; 188} 189 190void 191BaseCPU::enableFunctionTrace() 192{ 193 functionTracingEnabled = true; 194} 195 196BaseCPU::~BaseCPU() 197{ 198} 199 200void 201BaseCPU::init() 202{ 203 if (!params()->defer_registration) 204 registerThreadContexts(); 205} 206 207void 208BaseCPU::startup() 209{ 210#if FULL_SYSTEM 211 if (!params()->defer_registration && profileEvent) 212 profileEvent->schedule(curTick); 213#endif 214 215 if (params()->progress_interval) { 216 new CPUProgressEvent(&mainEventQueue, 217 ticks(params()->progress_interval), 218 this); 219 } 220} 221 222 223void 224BaseCPU::regStats() 225{ 226 using namespace Stats; 227 228 numCycles 229 .name(name() + ".numCycles") 230 .desc("number of cpu cycles simulated") 231 ; 232 233 int size = threadContexts.size(); 234 if (size > 1) { 235 for (int i = 0; i < size; ++i) { 236 stringstream namestr; 237 ccprintf(namestr, "%s.ctx%d", name(), i); 238 threadContexts[i]->regStats(namestr.str()); 239 } 240 } else if (size == 1) 241 threadContexts[0]->regStats(name()); 242 243#if FULL_SYSTEM 244#endif 245} 246 247Tick 248BaseCPU::nextCycle() 249{ 250 Tick next_tick = curTick - phase + clock - 1; 251 next_tick -= (next_tick % clock); 252 next_tick += phase; 253 return next_tick; 254} 255 256Tick 257BaseCPU::nextCycle(Tick begin_tick) 258{ 259 Tick next_tick = begin_tick; 260 if (next_tick % clock != 0) 261 next_tick = next_tick - (next_tick % clock) + clock; 262 next_tick += phase; 263 264 assert(next_tick >= curTick); 265 return next_tick; 266} 267 268void 269BaseCPU::registerThreadContexts() 270{ 271 for (int i = 0; i < threadContexts.size(); ++i) { 272 ThreadContext *tc = threadContexts[i]; 273 274#if FULL_SYSTEM 275 int id = params()->cpu_id; 276 if (id != -1) 277 id += i; 278 279 tc->setCpuId(system->registerThreadContext(tc, id)); 280#else 281 tc->setCpuId(tc->getProcessPtr()->registerThreadContext(tc)); 282#endif 283 } 284} 285 286 287int 288BaseCPU::findContext(ThreadContext *tc) 289{ 290 for (int i = 0; i < threadContexts.size(); ++i) { 291 if (tc == threadContexts[i]) 292 return i; 293 } 294 return 0; 295} 296 297void 298BaseCPU::switchOut() 299{ 300// panic("This CPU doesn't support sampling!"); 301#if FULL_SYSTEM 302 if (profileEvent && profileEvent->scheduled()) 303 profileEvent->deschedule(); 304#endif 305} 306 307void 308BaseCPU::takeOverFrom(BaseCPU *oldCPU, Port *ic, Port *dc) 309{ 310 assert(threadContexts.size() == oldCPU->threadContexts.size()); 311 312 for (int i = 0; i < threadContexts.size(); ++i) { 313 ThreadContext *newTC = threadContexts[i]; 314 ThreadContext *oldTC = oldCPU->threadContexts[i]; 315 316 newTC->takeOverFrom(oldTC); 317 318 CpuEvent::replaceThreadContext(oldTC, newTC); 319 320 assert(newTC->readCpuId() == oldTC->readCpuId()); 321#if FULL_SYSTEM 322 system->replaceThreadContext(newTC, newTC->readCpuId()); 323#else 324 assert(newTC->getProcessPtr() == oldTC->getProcessPtr()); 325 newTC->getProcessPtr()->replaceThreadContext(newTC, newTC->readCpuId()); 326#endif 327 328 if (DTRACE(Context)) 329 ThreadContext::compare(oldTC, newTC); 330 } 331 332#if FULL_SYSTEM 333 interrupts = oldCPU->interrupts; 334 335 for (int i = 0; i < threadContexts.size(); ++i) 336 threadContexts[i]->profileClear(); 337 338 if (profileEvent) 339 profileEvent->schedule(curTick); 340#endif 341 342 // Connect new CPU to old CPU's memory only if new CPU isn't 343 // connected to anything. Also connect old CPU's memory to new 344 // CPU. 345 if (!ic->isConnected()) { 346 Port *peer = oldCPU->getPort("icache_port")->getPeer(); 347 ic->setPeer(peer); 348 peer->setPeer(ic); 349 } 350 351 if (!dc->isConnected()) { 352 Port *peer = oldCPU->getPort("dcache_port")->getPeer(); 353 dc->setPeer(peer); 354 peer->setPeer(dc); 355 } 356} 357 358 359#if FULL_SYSTEM 360BaseCPU::ProfileEvent::ProfileEvent(BaseCPU *_cpu, Tick _interval) 361 : Event(&mainEventQueue), cpu(_cpu), interval(_interval) 362{ } 363 364void 365BaseCPU::ProfileEvent::process() 366{ 367 for (int i = 0, size = cpu->threadContexts.size(); i < size; ++i) { 368 ThreadContext *tc = cpu->threadContexts[i]; 369 tc->profileSample(); 370 } 371 372 schedule(curTick + interval); 373} 374 375void 376BaseCPU::post_interrupt(int int_num, int index) 377{ 378 interrupts.post(int_num, index); 379} 380 381void 382BaseCPU::clear_interrupt(int int_num, int index) 383{ 384 interrupts.clear(int_num, index); 385} 386 387void 388BaseCPU::clear_interrupts() 389{ 390 interrupts.clear_all(); 391} 392 393uint64_t 394BaseCPU::get_interrupts(int int_num) 395{ 396 return interrupts.get_vec(int_num); 397} 398 399void 400BaseCPU::serialize(std::ostream &os) 401{ 402 SERIALIZE_SCALAR(instCnt); 403 interrupts.serialize(os); 404} 405 406void 407BaseCPU::unserialize(Checkpoint *cp, const std::string §ion) 408{ 409 UNSERIALIZE_SCALAR(instCnt); 410 interrupts.unserialize(cp, section); 411} 412 413#endif // FULL_SYSTEM 414 415void 416BaseCPU::traceFunctionsInternal(Addr pc) 417{ 418 if (!debugSymbolTable) 419 return; 420 421 // if pc enters different function, print new function symbol and 422 // update saved range. Otherwise do nothing. 423 if (pc < currentFunctionStart || pc >= currentFunctionEnd) { 424 string sym_str; 425 bool found = debugSymbolTable->findNearestSymbol(pc, sym_str, 426 currentFunctionStart, 427 currentFunctionEnd); 428 429 if (!found) { 430 // no symbol found: use addr as label 431 sym_str = csprintf("0x%x", pc); 432 currentFunctionStart = pc; 433 currentFunctionEnd = pc + 1; 434 } 435 436 ccprintf(*functionTraceStream, " (%d)\n%d: %s", 437 curTick - functionEntryTick, curTick, sym_str); 438 functionEntryTick = curTick; 439 } 440} 441