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