main.cc revision 1399
1/* 2 * Copyright (c) 2000-2004 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 29/// 30/// @file sim/main.cc 31/// 32#include <sys/types.h> 33#include <sys/stat.h> 34#include <errno.h> 35#include <libgen.h> 36#include <stdlib.h> 37#include <signal.h> 38 39#include <list> 40#include <string> 41#include <vector> 42 43#include "base/copyright.hh" 44#include "base/embedfile.hh" 45#include "base/inifile.hh" 46#include "base/misc.hh" 47#include "base/output.hh" 48#include "base/pollevent.hh" 49#include "base/statistics.hh" 50#include "base/str.hh" 51#include "base/time.hh" 52#include "cpu/base_cpu.hh" 53#include "cpu/full_cpu/smt.hh" 54#include "sim/async.hh" 55#include "sim/builder.hh" 56#include "sim/configfile.hh" 57#include "sim/host.hh" 58#include "sim/sim_events.hh" 59#include "sim/sim_exit.hh" 60#include "sim/sim_object.hh" 61#include "sim/stat_control.hh" 62#include "sim/stats.hh" 63#include "sim/universe.hh" 64#include "sim/pyconfig/pyconfig.hh" 65 66using namespace std; 67 68// See async.h. 69volatile bool async_event = false; 70volatile bool async_dump = false; 71volatile bool async_dumpreset = false; 72volatile bool async_exit = false; 73volatile bool async_io = false; 74volatile bool async_alarm = false; 75 76/// Stats signal handler. 77void 78dumpStatsHandler(int sigtype) 79{ 80 async_event = true; 81 async_dump = true; 82} 83 84void 85dumprstStatsHandler(int sigtype) 86{ 87 async_event = true; 88 async_dumpreset = true; 89} 90 91/// Exit signal handler. 92void 93exitNowHandler(int sigtype) 94{ 95 async_event = true; 96 async_exit = true; 97} 98 99/// Abort signal handler. 100void 101abortHandler(int sigtype) 102{ 103 cerr << "Program aborted at cycle " << curTick << endl; 104 105#if TRACING_ON 106 // dump trace buffer, if there is one 107 Trace::theLog.dump(cerr); 108#endif 109} 110 111/// Simulator executable name 112char *myProgName = ""; 113 114/// Show brief help message. 115void 116showBriefHelp(ostream &out) 117{ 118 char *prog = basename(myProgName); 119 120 ccprintf(out, "Usage:\n"); 121 ccprintf(out, 122"%s [-d <dir>] [-E <var>[=<val>]] [-I <dir>] [-P <python>]\n" 123" [--<var>=<val>] <config file>\n" 124"\n" 125" -d set the output directory to <dir>\n" 126" -E set the environment variable <var> to <val> (or 'True')\n" 127" -I add the directory <dir> to python's path\n" 128" -P execute <python> directly in the configuration\n" 129" --var=val set the python variable <var> to '<val>'\n" 130" <configfile> config file name (.py or .mpy)\n", 131 prog); 132 133 ccprintf(out, "%s -X\n -X extract embedded files\n", prog); 134 ccprintf(out, "%s -h\n -h print long help\n", prog); 135} 136 137/// Show verbose help message. Includes parameter listing from 138/// showBriefHelp(), plus an exhaustive list of ini-file parameters 139/// and SimObjects (with their parameters). 140void 141showLongHelp(ostream &out) 142{ 143 showBriefHelp(out); 144 145 out << endl 146 << endl 147 << "-----------------" << endl 148 << "Global Parameters" << endl 149 << "-----------------" << endl 150 << endl; 151 152 ParamContext::describeAllContexts(out); 153 154 out << endl 155 << endl 156 << "-----------------" << endl 157 << "Simulator Objects" << endl 158 << "-----------------" << endl 159 << endl; 160 161 SimObjectClass::describeAllClasses(out); 162} 163 164/// Print welcome message. 165void 166sayHello(ostream &out) 167{ 168 extern const char *compileDate; // from date.cc 169 170 ccprintf(out, "M5 Simulator System\n"); 171 // display copyright 172 ccprintf(out, "%s\n", briefCopyright); 173 ccprintf(out, "M5 compiled on %d\n", compileDate); 174 175 char *host = getenv("HOSTNAME"); 176 if (!host) 177 host = getenv("HOST"); 178 179 if (host) 180 ccprintf(out, "M5 executing on %s\n", host); 181 182 ccprintf(out, "M5 simulation started %s\n", Time::start); 183} 184 185/// 186/// Echo the command line for posterity in such a way that it can be 187/// used to rerun the same simulation (given the same .ini files). 188/// 189void 190echoCommandLine(int argc, char **argv, ostream &out) 191{ 192 out << "command line: " << argv[0]; 193 for (int i = 1; i < argc; i++) { 194 string arg(argv[i]); 195 196 out << ' '; 197 198 // If the arg contains spaces, we need to quote it. 199 // The rest of this is overkill to make it look purty. 200 201 // print dashes first outside quotes 202 int non_dash_pos = arg.find_first_not_of("-"); 203 out << arg.substr(0, non_dash_pos); // print dashes 204 string body = arg.substr(non_dash_pos); // the rest 205 206 // if it's an assignment, handle the lhs & rhs separately 207 int eq_pos = body.find("="); 208 if (eq_pos == string::npos) { 209 out << quote(body); 210 } 211 else { 212 string lhs(body.substr(0, eq_pos)); 213 string rhs(body.substr(eq_pos + 1)); 214 215 out << quote(lhs) << "=" << quote(rhs); 216 } 217 } 218 out << endl << endl; 219} 220 221char * 222getOptionString(int &index, int argc, char **argv) 223{ 224 char *option = argv[index] + 2; 225 if (*option != '\0') 226 return option; 227 228 // We didn't find an argument, it must be in the next variable. 229 if (++index >= argc) 230 panic("option string for option '%s' not found", argv[index - 1]); 231 232 return argv[index]; 233} 234 235int 236main(int argc, char **argv) 237{ 238 // Save off program name 239 myProgName = argv[0]; 240 241 signal(SIGFPE, SIG_IGN); // may occur on misspeculated paths 242 signal(SIGTRAP, SIG_IGN); 243 signal(SIGUSR1, dumpStatsHandler); // dump intermediate stats 244 signal(SIGUSR2, dumprstStatsHandler); // dump and reset stats 245 signal(SIGINT, exitNowHandler); // dump final stats and exit 246 signal(SIGABRT, abortHandler); 247 248 sayHello(cerr); 249 250 bool configfile_found = false; 251 PythonConfig pyconfig; 252 string outdir; 253 254 // Parse command-line options. 255 // Since most of the complex options are handled through the 256 // config database, we don't mess with getopts, and just parse 257 // manually. 258 for (int i = 1; i < argc; ++i) { 259 char *arg_str = argv[i]; 260 261 // if arg starts with '--', parse as a special python option 262 // of the format --<python var>=<string value>, if the arg 263 // starts with '-', it should be a simulator option with a 264 // format similar to getopt. In any other case, treat the 265 // option as a configuration file name and load it. 266 if (arg_str[0] == '-' && arg_str[1] == '-') { 267 string str = &arg_str[2]; 268 string var, val; 269 270 if (!split_first(str, var, val, '=')) 271 panic("Could not parse configuration argument '%s'\n" 272 "Expecting --<variable>=<value>\n", arg_str); 273 274 pyconfig.setVariable(var, val); 275 } else if (arg_str[0] == '-') { 276 char *option; 277 string var, val; 278 279 // switch on second char 280 switch (arg_str[1]) { 281 case 'd': 282 outdir = getOptionString(i, argc, argv); 283 break; 284 285 case 'h': 286 showLongHelp(cerr); 287 exit(1); 288 289 case 'E': 290 option = getOptionString(i, argc, argv); 291 if (!split_first(option, var, val, '=')) 292 val = "True"; 293 294 if (setenv(var.c_str(), val.c_str(), true) == -1) 295 panic("setenv: %s\n", strerror(errno)); 296 break; 297 298 case 'I': 299 option = getOptionString(i, argc, argv); 300 pyconfig.addPath(option); 301 break; 302 303 case 'P': 304 option = getOptionString(i, argc, argv); 305 pyconfig.writeLine(option); 306 break; 307 308 case 'X': { 309 list<EmbedFile> lst; 310 EmbedMap::all(lst); 311 list<EmbedFile>::iterator i = lst.begin(); 312 list<EmbedFile>::iterator end = lst.end(); 313 314 while (i != end) { 315 cprintf("Embedded File: %s\n", i->name); 316 cout.write(i->data, i->length); 317 ++i; 318 } 319 320 return 0; 321 } 322 323 default: 324 showBriefHelp(cerr); 325 panic("invalid argument '%s'\n", arg_str); 326 } 327 } else { 328 string file(arg_str); 329 string base, ext; 330 331 if (!split_last(file, base, ext, '.') || 332 ext != "py" && ext != "mpy") 333 panic("Config file '%s' must end in '.py' or '.mpy'\n", file); 334 335 pyconfig.load(file); 336 configfile_found = true; 337 } 338 } 339 340 if (outdir.empty()) { 341 char *env = getenv("OUTPUT_DIR"); 342 outdir = env ? env : "."; 343 } 344 345 simout.setDirectory(outdir); 346 347 char *env = getenv("CONFIG_OUTPUT"); 348 if (!env) 349 env = "config.out"; 350 configStream = simout.find(env); 351 352 if (!configfile_found) 353 panic("no configuration file specified!"); 354 355 // The configuration database is now complete; start processing it. 356 IniFile inifile; 357 if (!pyconfig.output(inifile)) 358 panic("Error processing python code"); 359 360 // Initialize statistics database 361 Stats::InitSimStats(); 362 363 // Now process the configuration hierarchy and create the SimObjects. 364 ConfigHierarchy configHierarchy(inifile); 365 configHierarchy.build(); 366 configHierarchy.createSimObjects(); 367 368 // Parse and check all non-config-hierarchy parameters. 369 ParamContext::parseAllContexts(inifile); 370 ParamContext::checkAllContexts(); 371 372 // Print hello message to stats file if it's actually a file. If 373 // it's not (i.e. it's cout or cerr) then we already did it above. 374 if (simout.isFile(*outputStream)) 375 sayHello(*outputStream); 376 377 // Echo command line and all parameter settings to stats file as well. 378 echoCommandLine(argc, argv, *outputStream); 379 ParamContext::showAllContexts(*configStream); 380 381 // Do a second pass to finish initializing the sim objects 382 SimObject::initAll(); 383 384 // Restore checkpointed state, if any. 385 configHierarchy.unserializeSimObjects(); 386 387 // Done processing the configuration database. 388 // Check for unreferenced entries. 389 if (inifile.printUnreferenced()) 390 panic("unreferenced sections/entries in the intermediate ini file"); 391 392 SimObject::regAllStats(); 393 394 // uncomment the following to get PC-based execution-time profile 395#ifdef DO_PROFILE 396 init_profile((char *)&_init, (char *)&_fini); 397#endif 398 399 // Check to make sure that the stats package is properly initialized 400 Stats::check(); 401 402 // Reset to put the stats in a consistent state. 403 Stats::reset(); 404 405 // Nothing to simulate if we don't have at least one CPU somewhere. 406 if (BaseCPU::numSimulatedCPUs() == 0) { 407 cerr << "Fatal: no CPUs to simulate." << endl; 408 exit(1); 409 } 410 411 warn("Entering event queue. Starting simulation...\n"); 412 SimStartup(); 413 while (!mainEventQueue.empty()) { 414 assert(curTick <= mainEventQueue.nextTick() && 415 "event scheduled in the past"); 416 417 // forward current cycle to the time of the first event on the 418 // queue 419 curTick = mainEventQueue.nextTick(); 420 mainEventQueue.serviceOne(); 421 422 if (async_event) { 423 async_event = false; 424 if (async_dump) { 425 async_dump = false; 426 427 using namespace Stats; 428 SetupEvent(Dump, curTick); 429 } 430 431 if (async_dumpreset) { 432 async_dumpreset = false; 433 434 using namespace Stats; 435 SetupEvent(Dump | Reset, curTick); 436 } 437 438 if (async_exit) { 439 async_exit = false; 440 new SimExitEvent("User requested STOP"); 441 } 442 443 if (async_io || async_alarm) { 444 async_io = false; 445 async_alarm = false; 446 pollQueue.service(); 447 } 448 } 449 } 450 451 // This should never happen... every conceivable way for the 452 // simulation to terminate (hit max cycles/insts, signal, 453 // simulated system halts/exits) generates an exit event, so we 454 // should never run out of events on the queue. 455 exitNow("no events on event loop! All CPUs must be idle.", 1); 456 457 return 0; 458} 459