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