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