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 "sim/async.hh" 64#include "sim/builder.hh" 65#include "sim/host.hh" 66#include "sim/serialize.hh" 67#include "sim/sim_events.hh" 68#include "sim/sim_exit.hh" 69#include "sim/sim_object.hh" 70#include "sim/system.hh" 71#include "sim/stat_control.hh" 72#include "sim/stats.hh" 73#include "sim/root.hh" 74 75using namespace std; 76 77// See async.h. 78volatile bool async_event = false; 79volatile bool async_dump = false; 80volatile bool async_dumpreset = false; 81volatile bool async_exit = false; 82volatile bool async_io = false; 83volatile bool async_alarm = false; 84 85/// Stats signal handler. 86void 87dumpStatsHandler(int sigtype) 88{ 89 async_event = true; 90 async_dump = true; 91} 92 93void 94dumprstStatsHandler(int sigtype) 95{ 96 async_event = true; 97 async_dumpreset = true; 98} 99 100/// Exit signal handler. 101void 102exitNowHandler(int sigtype) 103{ 104 async_event = true; 105 async_exit = true; 106} 107 108/// Abort signal handler. 109void 110abortHandler(int sigtype) 111{ 112 cerr << "Program aborted at cycle " << curTick << endl; 113 114#if TRACING_ON 115 // dump trace buffer, if there is one 116 Trace::theLog.dump(cerr); 117#endif 118} 119 120extern "C" { void init_cc_main(); } 121 122int 123main(int argc, char **argv) 124{ 125 signal(SIGFPE, SIG_IGN); // may occur on misspeculated paths 126 signal(SIGTRAP, SIG_IGN); 127 signal(SIGUSR1, dumpStatsHandler); // dump intermediate stats 128 signal(SIGUSR2, dumprstStatsHandler); // dump and reset stats 129 signal(SIGINT, exitNowHandler); // dump final stats and exit 130 signal(SIGABRT, abortHandler); 131 132 Py_SetProgramName(argv[0]); 133 134 // default path to m5 python code is the currently executing 135 // file... Python ZipImporter will find embedded zip archive. 136 // The M5_ARCHIVE environment variable can be used to override this. 137 char *m5_archive = getenv("M5_ARCHIVE"); 138 string pythonpath = m5_archive ? m5_archive : argv[0]; 139 140 char *oldpath = getenv("PYTHONPATH"); 141 if (oldpath != NULL) { 142 pythonpath += ":"; 143 pythonpath += oldpath; 144 } 145 146 if (setenv("PYTHONPATH", pythonpath.c_str(), true) == -1) 147 fatal("setenv: %s\n", strerror(errno)); 148 149 char *python_home = getenv("PYTHONHOME"); 150 if (!python_home) 151 python_home = PYTHONHOME; 152 Py_SetPythonHome(python_home); 153 154 // initialize embedded Python interpreter 155 Py_Initialize(); 156 PySys_SetArgv(argc, argv); 157 158 // initialize SWIG 'cc_main' module 159 init_cc_main(); 160 161 PyRun_SimpleString("import m5.main"); 162 PyRun_SimpleString("m5.main.main()"); 163 164 // clean up Python intepreter. 165 Py_Finalize(); 166} 167 168 169void 170setOutputDir(const string &dir) 171{ 172 simout.setDirectory(dir); 173} 174 175 176IniFile inifile; 177 178SimObject * 179createSimObject(const string &name) 180{ 181 return SimObjectClass::createObject(inifile, name); 182} 183 184 185/** 186 * Pointer to the Python function that maps names to SimObjects. 187 */ 188PyObject *resolveFunc = NULL; 189 190/** 191 * Convert a pointer to the Python object that SWIG wraps around a C++ 192 * SimObject pointer back to the actual C++ pointer. See main.i. 193 */ 194extern "C" SimObject *convertSwigSimObjectPtr(PyObject *); 195 196 197SimObject * 198resolveSimObject(const string &name) 199{ 200 PyObject *pyPtr = PyEval_CallFunction(resolveFunc, "(s)", name.c_str()); 201 if (pyPtr == NULL) { 202 PyErr_Print(); 203 panic("resolveSimObject: failure on call to Python for %s", name); 204 } 205 206 SimObject *simObj = convertSwigSimObjectPtr(pyPtr); 207 if (simObj == NULL) 208 panic("resolveSimObject: failure on pointer conversion for %s", name); 209 210 return simObj; 211} 212 213 214/** 215 * Load config.ini into C++ database. Exported to Python via SWIG; 216 * invoked from m5.instantiate(). 217 */ 218void 219loadIniFile(PyObject *_resolveFunc) 220{ 221 resolveFunc = _resolveFunc; 222 configStream = simout.find("config.out"); 223 224 // The configuration database is now complete; start processing it. 225 inifile.load(simout.resolve("config.ini")); 226 227 // Initialize statistics database 228 Stats::InitSimStats(); 229} 230 231 232/** 233 * Look up a MemObject port. Helper function for connectPorts(). 234 */ 235Port * 236lookupPort(SimObject *so, const std::string &name, int i) 237{ 238 MemObject *mo = dynamic_cast<MemObject *>(so); 239 if (mo == NULL) { 240 warn("error casting SimObject %s to MemObject", so->name()); 241 return NULL; 242 } 243 244 Port *p = mo->getPort(name, i); 245 if (p == NULL) 246 warn("error looking up port %s on object %s", name, so->name()); 247 return p; 248} 249 250 251/** 252 * Connect the described MemObject ports. Called from Python via SWIG. 253 */ 254int 255connectPorts(SimObject *o1, const std::string &name1, int i1, 256 SimObject *o2, const std::string &name2, int i2) 257{ 258 Port *p1 = lookupPort(o1, name1, i1); 259 Port *p2 = lookupPort(o2, name2, i2); 260 261 if (p1 == NULL || p2 == NULL) { 262 warn("connectPorts: port lookup error"); 263 return 0; 264 } 265 266 p1->setPeer(p2); 267 p2->setPeer(p1); 268 269 return 1; 270} 271 272/** 273 * Do final initialization steps after object construction but before 274 * start of simulation. 275 */ 276void 277finalInit() 278{ 279 // Parse and check all non-config-hierarchy parameters. 280 ParamContext::parseAllContexts(inifile); 281 ParamContext::checkAllContexts(); 282 283 // Echo all parameter settings to stats file as well. 284 ParamContext::showAllContexts(*configStream); 285 286 // Do a second pass to finish initializing the sim objects 287 SimObject::initAll(); 288 289 // Restore checkpointed state, if any. 290#if 0 291 configHierarchy.unserializeSimObjects(); 292#endif 293 294 SimObject::regAllStats(); 295 296 // Check to make sure that the stats package is properly initialized 297 Stats::check(); 298 299 // Reset to put the stats in a consistent state. 300 Stats::reset(); 301 302 SimStartup(); 303} 304 305 306/** Simulate for num_cycles additional cycles. If num_cycles is -1 307 * (the default), do not limit simulation; some other event must 308 * terminate the loop. Exported to Python via SWIG. 309 * @return The SimLoopExitEvent that caused the loop to exit. 310 */ 311SimLoopExitEvent *
| 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 "sim/async.hh" 64#include "sim/builder.hh" 65#include "sim/host.hh" 66#include "sim/serialize.hh" 67#include "sim/sim_events.hh" 68#include "sim/sim_exit.hh" 69#include "sim/sim_object.hh" 70#include "sim/system.hh" 71#include "sim/stat_control.hh" 72#include "sim/stats.hh" 73#include "sim/root.hh" 74 75using namespace std; 76 77// See async.h. 78volatile bool async_event = false; 79volatile bool async_dump = false; 80volatile bool async_dumpreset = false; 81volatile bool async_exit = false; 82volatile bool async_io = false; 83volatile bool async_alarm = false; 84 85/// Stats signal handler. 86void 87dumpStatsHandler(int sigtype) 88{ 89 async_event = true; 90 async_dump = true; 91} 92 93void 94dumprstStatsHandler(int sigtype) 95{ 96 async_event = true; 97 async_dumpreset = true; 98} 99 100/// Exit signal handler. 101void 102exitNowHandler(int sigtype) 103{ 104 async_event = true; 105 async_exit = true; 106} 107 108/// Abort signal handler. 109void 110abortHandler(int sigtype) 111{ 112 cerr << "Program aborted at cycle " << curTick << endl; 113 114#if TRACING_ON 115 // dump trace buffer, if there is one 116 Trace::theLog.dump(cerr); 117#endif 118} 119 120extern "C" { void init_cc_main(); } 121 122int 123main(int argc, char **argv) 124{ 125 signal(SIGFPE, SIG_IGN); // may occur on misspeculated paths 126 signal(SIGTRAP, SIG_IGN); 127 signal(SIGUSR1, dumpStatsHandler); // dump intermediate stats 128 signal(SIGUSR2, dumprstStatsHandler); // dump and reset stats 129 signal(SIGINT, exitNowHandler); // dump final stats and exit 130 signal(SIGABRT, abortHandler); 131 132 Py_SetProgramName(argv[0]); 133 134 // default path to m5 python code is the currently executing 135 // file... Python ZipImporter will find embedded zip archive. 136 // The M5_ARCHIVE environment variable can be used to override this. 137 char *m5_archive = getenv("M5_ARCHIVE"); 138 string pythonpath = m5_archive ? m5_archive : argv[0]; 139 140 char *oldpath = getenv("PYTHONPATH"); 141 if (oldpath != NULL) { 142 pythonpath += ":"; 143 pythonpath += oldpath; 144 } 145 146 if (setenv("PYTHONPATH", pythonpath.c_str(), true) == -1) 147 fatal("setenv: %s\n", strerror(errno)); 148 149 char *python_home = getenv("PYTHONHOME"); 150 if (!python_home) 151 python_home = PYTHONHOME; 152 Py_SetPythonHome(python_home); 153 154 // initialize embedded Python interpreter 155 Py_Initialize(); 156 PySys_SetArgv(argc, argv); 157 158 // initialize SWIG 'cc_main' module 159 init_cc_main(); 160 161 PyRun_SimpleString("import m5.main"); 162 PyRun_SimpleString("m5.main.main()"); 163 164 // clean up Python intepreter. 165 Py_Finalize(); 166} 167 168 169void 170setOutputDir(const string &dir) 171{ 172 simout.setDirectory(dir); 173} 174 175 176IniFile inifile; 177 178SimObject * 179createSimObject(const string &name) 180{ 181 return SimObjectClass::createObject(inifile, name); 182} 183 184 185/** 186 * Pointer to the Python function that maps names to SimObjects. 187 */ 188PyObject *resolveFunc = NULL; 189 190/** 191 * Convert a pointer to the Python object that SWIG wraps around a C++ 192 * SimObject pointer back to the actual C++ pointer. See main.i. 193 */ 194extern "C" SimObject *convertSwigSimObjectPtr(PyObject *); 195 196 197SimObject * 198resolveSimObject(const string &name) 199{ 200 PyObject *pyPtr = PyEval_CallFunction(resolveFunc, "(s)", name.c_str()); 201 if (pyPtr == NULL) { 202 PyErr_Print(); 203 panic("resolveSimObject: failure on call to Python for %s", name); 204 } 205 206 SimObject *simObj = convertSwigSimObjectPtr(pyPtr); 207 if (simObj == NULL) 208 panic("resolveSimObject: failure on pointer conversion for %s", name); 209 210 return simObj; 211} 212 213 214/** 215 * Load config.ini into C++ database. Exported to Python via SWIG; 216 * invoked from m5.instantiate(). 217 */ 218void 219loadIniFile(PyObject *_resolveFunc) 220{ 221 resolveFunc = _resolveFunc; 222 configStream = simout.find("config.out"); 223 224 // The configuration database is now complete; start processing it. 225 inifile.load(simout.resolve("config.ini")); 226 227 // Initialize statistics database 228 Stats::InitSimStats(); 229} 230 231 232/** 233 * Look up a MemObject port. Helper function for connectPorts(). 234 */ 235Port * 236lookupPort(SimObject *so, const std::string &name, int i) 237{ 238 MemObject *mo = dynamic_cast<MemObject *>(so); 239 if (mo == NULL) { 240 warn("error casting SimObject %s to MemObject", so->name()); 241 return NULL; 242 } 243 244 Port *p = mo->getPort(name, i); 245 if (p == NULL) 246 warn("error looking up port %s on object %s", name, so->name()); 247 return p; 248} 249 250 251/** 252 * Connect the described MemObject ports. Called from Python via SWIG. 253 */ 254int 255connectPorts(SimObject *o1, const std::string &name1, int i1, 256 SimObject *o2, const std::string &name2, int i2) 257{ 258 Port *p1 = lookupPort(o1, name1, i1); 259 Port *p2 = lookupPort(o2, name2, i2); 260 261 if (p1 == NULL || p2 == NULL) { 262 warn("connectPorts: port lookup error"); 263 return 0; 264 } 265 266 p1->setPeer(p2); 267 p2->setPeer(p1); 268 269 return 1; 270} 271 272/** 273 * Do final initialization steps after object construction but before 274 * start of simulation. 275 */ 276void 277finalInit() 278{ 279 // Parse and check all non-config-hierarchy parameters. 280 ParamContext::parseAllContexts(inifile); 281 ParamContext::checkAllContexts(); 282 283 // Echo all parameter settings to stats file as well. 284 ParamContext::showAllContexts(*configStream); 285 286 // Do a second pass to finish initializing the sim objects 287 SimObject::initAll(); 288 289 // Restore checkpointed state, if any. 290#if 0 291 configHierarchy.unserializeSimObjects(); 292#endif 293 294 SimObject::regAllStats(); 295 296 // Check to make sure that the stats package is properly initialized 297 Stats::check(); 298 299 // Reset to put the stats in a consistent state. 300 Stats::reset(); 301 302 SimStartup(); 303} 304 305 306/** Simulate for num_cycles additional cycles. If num_cycles is -1 307 * (the default), do not limit simulation; some other event must 308 * terminate the loop. Exported to Python via SWIG. 309 * @return The SimLoopExitEvent that caused the loop to exit. 310 */ 311SimLoopExitEvent *
|