1/* 2 * Copyright (c) 2000-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 Raasch 29 * Nathan Binkert 30 * Steve Reinhardt 31 */ 32 33/// 34/// @file sim/main.cc 35/// 36#include <Python.h> // must be before system headers... see Python docs 37 38#include <sys/types.h> 39#include <sys/stat.h> 40#include <errno.h> 41#include <libgen.h> 42#include <stdlib.h> 43#include <signal.h> 44#include <getopt.h> 45 46#include <list> 47#include <string> 48#include <vector> 49 50#include "base/callback.hh" 51#include "base/inifile.hh" 52#include "base/misc.hh" 53#include "base/output.hh" 54#include "base/pollevent.hh" 55#include "base/statistics.hh" 56#include "base/str.hh" 57#include "base/time.hh" 58#include "config/pythonhome.hh" 59#include "cpu/base.hh" 60#include "cpu/smt.hh" 61#include "mem/mem_object.hh" 62#include "mem/port.hh" 63#include "python/swig/init.hh" 64#include "sim/async.hh" 65#include "sim/builder.hh" 66#include "sim/host.hh" 67#include "sim/serialize.hh" 68#include "sim/sim_events.hh" 69#include "sim/sim_exit.hh" 70#include "sim/sim_object.hh" 71#include "sim/system.hh" 72#include "sim/stat_control.hh" 73#include "sim/stats.hh" 74#include "sim/root.hh" 75 76using namespace std; 77 78// See async.h. 79volatile bool async_event = false; 80volatile bool async_dump = false; 81volatile bool async_dumpreset = false; 82volatile bool async_exit = false; 83volatile bool async_io = false; 84volatile bool async_alarm = false;
|
86 87/// Stats signal handler. 88void 89dumpStatsHandler(int sigtype) 90{ 91 async_event = true; 92 async_dump = true; 93} 94 95void 96dumprstStatsHandler(int sigtype) 97{ 98 async_event = true; 99 async_dumpreset = true; 100} 101 102/// Exit signal handler. 103void 104exitNowHandler(int sigtype) 105{ 106 async_event = true; 107 async_exit = true; 108} 109 110/// Abort signal handler. 111void 112abortHandler(int sigtype) 113{ 114 cerr << "Program aborted at cycle " << curTick << endl; 115} 116 117int 118main(int argc, char **argv) 119{ 120 signal(SIGFPE, SIG_IGN); // may occur on misspeculated paths 121 signal(SIGTRAP, SIG_IGN); 122 signal(SIGUSR1, dumpStatsHandler); // dump intermediate stats 123 signal(SIGUSR2, dumprstStatsHandler); // dump and reset stats 124 signal(SIGINT, exitNowHandler); // dump final stats and exit 125 signal(SIGABRT, abortHandler); 126 127 Py_SetProgramName(argv[0]); 128 129 // default path to m5 python code is the currently executing 130 // file... Python ZipImporter will find embedded zip archive. 131 // The M5_ARCHIVE environment variable can be used to override this. 132 char *m5_archive = getenv("M5_ARCHIVE"); 133 string pythonpath = m5_archive ? m5_archive : argv[0]; 134 135 char *oldpath = getenv("PYTHONPATH"); 136 if (oldpath != NULL) { 137 pythonpath += ":"; 138 pythonpath += oldpath; 139 } 140 141 if (setenv("PYTHONPATH", pythonpath.c_str(), true) == -1) 142 fatal("setenv: %s\n", strerror(errno)); 143 144 char *python_home = getenv("PYTHONHOME"); 145 if (!python_home) 146 python_home = PYTHONHOME; 147 Py_SetPythonHome(python_home); 148 149 // initialize embedded Python interpreter 150 Py_Initialize(); 151 PySys_SetArgv(argc, argv); 152 153 // initialize SWIG modules 154 init_swig(); 155 156 PyRun_SimpleString("import m5.main"); 157 PyRun_SimpleString("m5.main.main()"); 158 159 // clean up Python intepreter. 160 Py_Finalize(); 161} 162 163 164void 165setOutputDir(const string &dir) 166{ 167 simout.setDirectory(dir); 168} 169 170 171IniFile inifile; 172 173SimObject * 174createSimObject(const string &name) 175{ 176 return SimObjectClass::createObject(inifile, name); 177} 178 179 180/** 181 * Pointer to the Python function that maps names to SimObjects. 182 */ 183PyObject *resolveFunc = NULL; 184 185/** 186 * Convert a pointer to the Python object that SWIG wraps around a C++ 187 * SimObject pointer back to the actual C++ pointer. See main.i. 188 */ 189extern "C" SimObject *convertSwigSimObjectPtr(PyObject *); 190 191 192SimObject * 193resolveSimObject(const string &name) 194{ 195 PyObject *pyPtr = PyEval_CallFunction(resolveFunc, "(s)", name.c_str()); 196 if (pyPtr == NULL) { 197 PyErr_Print(); 198 panic("resolveSimObject: failure on call to Python for %s", name); 199 } 200 201 SimObject *simObj = convertSwigSimObjectPtr(pyPtr); 202 if (simObj == NULL) 203 panic("resolveSimObject: failure on pointer conversion for %s", name); 204 205 return simObj; 206} 207 208 209/** 210 * Load config.ini into C++ database. Exported to Python via SWIG; 211 * invoked from m5.instantiate(). 212 */ 213void 214loadIniFile(PyObject *_resolveFunc) 215{ 216 resolveFunc = _resolveFunc; 217 configStream = simout.find("config.out"); 218 219 // The configuration database is now complete; start processing it. 220 inifile.load(simout.resolve("config.ini")); 221 222 // Initialize statistics database 223 Stats::InitSimStats(); 224} 225 226 227/** 228 * Look up a MemObject port. Helper function for connectPorts(). 229 */ 230Port * 231lookupPort(SimObject *so, const std::string &name, int i) 232{ 233 MemObject *mo = dynamic_cast<MemObject *>(so); 234 if (mo == NULL) { 235 warn("error casting SimObject %s to MemObject", so->name()); 236 return NULL; 237 } 238 239 Port *p = mo->getPort(name, i); 240 if (p == NULL) 241 warn("error looking up port %s on object %s", name, so->name()); 242 return p; 243} 244 245 246/** 247 * Connect the described MemObject ports. Called from Python via SWIG. 248 */ 249int 250connectPorts(SimObject *o1, const std::string &name1, int i1, 251 SimObject *o2, const std::string &name2, int i2) 252{ 253 Port *p1 = lookupPort(o1, name1, i1); 254 Port *p2 = lookupPort(o2, name2, i2); 255 256 if (p1 == NULL || p2 == NULL) { 257 warn("connectPorts: port lookup error"); 258 return 0; 259 } 260 261 p1->setPeer(p2); 262 p2->setPeer(p1); 263 264 return 1; 265} 266 267/** 268 * Do final initialization steps after object construction but before 269 * start of simulation. 270 */ 271void 272finalInit() 273{ 274 // Parse and check all non-config-hierarchy parameters. 275 ParamContext::parseAllContexts(inifile); 276 ParamContext::checkAllContexts(); 277 278 // Echo all parameter settings to stats file as well. 279 ParamContext::showAllContexts(*configStream); 280 281 // Do a second pass to finish initializing the sim objects 282 SimObject::initAll(); 283 284 // Restore checkpointed state, if any. 285#if 0 286 configHierarchy.unserializeSimObjects(); 287#endif 288 289 SimObject::regAllStats(); 290 291 // Check to make sure that the stats package is properly initialized 292 Stats::check(); 293 294 // Reset to put the stats in a consistent state. 295 Stats::reset(); 296 297 SimStartup(); 298} 299 300 301/** Simulate for num_cycles additional cycles. If num_cycles is -1 302 * (the default), do not limit simulation; some other event must 303 * terminate the loop. Exported to Python via SWIG. 304 * @return The SimLoopExitEvent that caused the loop to exit. 305 */ 306SimLoopExitEvent * 307simulate(Tick num_cycles = MaxTick) 308{ 309 warn("Entering event queue @ %d. Starting simulation...\n", curTick); 310 311 if (num_cycles < 0) 312 fatal("simulate: num_cycles must be >= 0 (was %d)\n", num_cycles); 313 else if (curTick + num_cycles < 0) //Overflow 314 num_cycles = MaxTick; 315 else 316 num_cycles = curTick + num_cycles; 317 318 Event *limit_event = schedExitSimLoop("simulate() limit reached", 319 num_cycles); 320 321 while (1) { 322 // there should always be at least one event (the SimLoopExitEvent 323 // we just scheduled) in the queue 324 assert(!mainEventQueue.empty()); 325 assert(curTick <= mainEventQueue.nextTick() && 326 "event scheduled in the past"); 327 328 // forward current cycle to the time of the first event on the 329 // queue 330 curTick = mainEventQueue.nextTick(); 331 Event *exit_event = mainEventQueue.serviceOne(); 332 if (exit_event != NULL) { 333 // hit some kind of exit event; return to Python 334 // event must be subclass of SimLoopExitEvent... 335 SimLoopExitEvent *se_event = dynamic_cast<SimLoopExitEvent *>(exit_event); 336 if (se_event == NULL) 337 panic("Bogus exit event class!"); 338 339 // if we didn't hit limit_event, delete it 340 if (se_event != limit_event) { 341 assert(limit_event->scheduled()); 342 limit_event->deschedule(); 343 delete limit_event; 344 } 345 346 return se_event; 347 } 348 349 if (async_event) { 350 async_event = false; 351 if (async_dump) { 352 async_dump = false; 353 354 using namespace Stats; 355 SetupEvent(Dump, curTick); 356 } 357 358 if (async_dumpreset) { 359 async_dumpreset = false; 360 361 using namespace Stats; 362 SetupEvent(Dump | Reset, curTick); 363 } 364 365 if (async_exit) { 366 async_exit = false; 367 exitSimLoop("user interrupt received"); 368 } 369 370 if (async_io || async_alarm) { 371 async_io = false; 372 async_alarm = false; 373 pollQueue.service(); 374 }
|