main.cc revision 2
19888Sandreas@sandberg.pp.se/* 29888Sandreas@sandberg.pp.se * Copyright (c) 2003 The Regents of The University of Michigan 39888Sandreas@sandberg.pp.se * All rights reserved. 49888Sandreas@sandberg.pp.se * 59888Sandreas@sandberg.pp.se * Redistribution and use in source and binary forms, with or without 69888Sandreas@sandberg.pp.se * modification, are permitted provided that the following conditions are 79888Sandreas@sandberg.pp.se * met: redistributions of source code must retain the above copyright 89888Sandreas@sandberg.pp.se * notice, this list of conditions and the following disclaimer; 99888Sandreas@sandberg.pp.se * redistributions in binary form must reproduce the above copyright 109888Sandreas@sandberg.pp.se * notice, this list of conditions and the following disclaimer in the 119888Sandreas@sandberg.pp.se * documentation and/or other materials provided with the distribution; 129888Sandreas@sandberg.pp.se * neither the name of the copyright holders nor the names of its 139888Sandreas@sandberg.pp.se * contributors may be used to endorse or promote products derived from 149888Sandreas@sandberg.pp.se * this software without specific prior written permission. 159888Sandreas@sandberg.pp.se * 169888Sandreas@sandberg.pp.se * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 179888Sandreas@sandberg.pp.se * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 189888Sandreas@sandberg.pp.se * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 199888Sandreas@sandberg.pp.se * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 209888Sandreas@sandberg.pp.se * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 219888Sandreas@sandberg.pp.se * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 229888Sandreas@sandberg.pp.se * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 239888Sandreas@sandberg.pp.se * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 249888Sandreas@sandberg.pp.se * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 259888Sandreas@sandberg.pp.se * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 269888Sandreas@sandberg.pp.se * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 279888Sandreas@sandberg.pp.se */ 289888Sandreas@sandberg.pp.se 299888Sandreas@sandberg.pp.se/// 309888Sandreas@sandberg.pp.se/// @file sim/main.cc 319888Sandreas@sandberg.pp.se/// 329888Sandreas@sandberg.pp.se#include <sys/types.h> 339888Sandreas@sandberg.pp.se#include <sys/stat.h> 349888Sandreas@sandberg.pp.se#include <stdlib.h> 359888Sandreas@sandberg.pp.se#include <signal.h> 369888Sandreas@sandberg.pp.se 379888Sandreas@sandberg.pp.se#include <string> 389888Sandreas@sandberg.pp.se#include <vector> 399888Sandreas@sandberg.pp.se 409888Sandreas@sandberg.pp.se#include "host.hh" 419888Sandreas@sandberg.pp.se#include "misc.hh" 429888Sandreas@sandberg.pp.se#include "stats.hh" 439888Sandreas@sandberg.pp.se 449888Sandreas@sandberg.pp.se#include "copyright.hh" 459888Sandreas@sandberg.pp.se#include "inifile.hh" 469888Sandreas@sandberg.pp.se#include "configfile.hh" 479888Sandreas@sandberg.pp.se#include "pollevent.hh" 489888Sandreas@sandberg.pp.se#include "statistics.hh" 499888Sandreas@sandberg.pp.se#include "sim_events.hh" 509888Sandreas@sandberg.pp.se#include "sim_exit.hh" 519888Sandreas@sandberg.pp.se#include "sim_object.hh" 529888Sandreas@sandberg.pp.se#include "sim_stats.hh" 539888Sandreas@sandberg.pp.se#include "sim_time.hh" 549888Sandreas@sandberg.pp.se#include "smt.hh" 559888Sandreas@sandberg.pp.se 569888Sandreas@sandberg.pp.se#include "base_cpu.hh" 579888Sandreas@sandberg.pp.se#include "async.hh" 589888Sandreas@sandberg.pp.se 599888Sandreas@sandberg.pp.seusing namespace std; 609888Sandreas@sandberg.pp.se 619888Sandreas@sandberg.pp.se// See async.h. 629888Sandreas@sandberg.pp.sevolatile bool async_event = false; 639888Sandreas@sandberg.pp.sevolatile bool async_dump = false; 649888Sandreas@sandberg.pp.sevolatile bool async_exit = false; 659888Sandreas@sandberg.pp.sevolatile bool async_io = false; 669888Sandreas@sandberg.pp.sevolatile bool async_alarm = false; 679888Sandreas@sandberg.pp.se 689888Sandreas@sandberg.pp.se/// Stats signal handler. 699888Sandreas@sandberg.pp.sevoid 709888Sandreas@sandberg.pp.sedumpStatsHandler(int sigtype) 719888Sandreas@sandberg.pp.se{ 729888Sandreas@sandberg.pp.se async_event = true; 739888Sandreas@sandberg.pp.se async_dump = true; 749888Sandreas@sandberg.pp.se} 759888Sandreas@sandberg.pp.se 769888Sandreas@sandberg.pp.se/// Exit signal handler. 779888Sandreas@sandberg.pp.sevoid 789888Sandreas@sandberg.pp.seexitNowHandler(int sigtype) 799888Sandreas@sandberg.pp.se{ 809888Sandreas@sandberg.pp.se async_event = true; 819888Sandreas@sandberg.pp.se async_exit = true; 829888Sandreas@sandberg.pp.se} 839888Sandreas@sandberg.pp.se 849888Sandreas@sandberg.pp.se/// Simulator executable name 859888Sandreas@sandberg.pp.seconst char *myProgName = ""; 869888Sandreas@sandberg.pp.se 879888Sandreas@sandberg.pp.se/// Show brief help message. 889888Sandreas@sandberg.pp.sestatic void 899888Sandreas@sandberg.pp.seshowBriefHelp(ostream &out) 909888Sandreas@sandberg.pp.se{ 919888Sandreas@sandberg.pp.se out << "Usage: " << myProgName 929888Sandreas@sandberg.pp.se << " [-hn] [-Dname[=def]] [-Uname] [-I[dir]] " 939888Sandreas@sandberg.pp.se << "[--<section>:<param>=<value>] [<config file> ...]" << endl 949888Sandreas@sandberg.pp.se << " -h: print long help (including parameter listing)" << endl 959888Sandreas@sandberg.pp.se << " -n: don't load default.ini" << endl 969888Sandreas@sandberg.pp.se << " -u: don't quit on unreferenced parameters" << endl 979888Sandreas@sandberg.pp.se << " -D,-U,-I: passed to cpp for preprocessing .ini files" << endl; 989888Sandreas@sandberg.pp.se} 999888Sandreas@sandberg.pp.se 1009888Sandreas@sandberg.pp.se/// Show verbose help message. Includes parameter listing from 1019888Sandreas@sandberg.pp.se/// showBriefHelp(), plus an exhaustive list of ini-file parameters 1029888Sandreas@sandberg.pp.se/// and SimObjects (with their parameters). 1039888Sandreas@sandberg.pp.sestatic void 1049888Sandreas@sandberg.pp.seshowLongHelp(ostream &out) 1059888Sandreas@sandberg.pp.se{ 1069888Sandreas@sandberg.pp.se showBriefHelp(out); 1079888Sandreas@sandberg.pp.se 1089888Sandreas@sandberg.pp.se out << endl 1099888Sandreas@sandberg.pp.se << endl 1109888Sandreas@sandberg.pp.se << "-----------------" << endl 1119888Sandreas@sandberg.pp.se << "Global Parameters" << endl 1129888Sandreas@sandberg.pp.se << "-----------------" << endl 1139888Sandreas@sandberg.pp.se << endl; 1149888Sandreas@sandberg.pp.se 1159888Sandreas@sandberg.pp.se ParamContext::describeAllContexts(out); 1169888Sandreas@sandberg.pp.se 1179888Sandreas@sandberg.pp.se out << endl 1189888Sandreas@sandberg.pp.se << endl 1199888Sandreas@sandberg.pp.se << "-----------------" << endl 1209888Sandreas@sandberg.pp.se << "Simulator Objects" << endl 1219888Sandreas@sandberg.pp.se << "-----------------" << endl 1229888Sandreas@sandberg.pp.se << endl; 1239888Sandreas@sandberg.pp.se 1249888Sandreas@sandberg.pp.se SimObjectClass::describeAllClasses(out); 1259888Sandreas@sandberg.pp.se} 1269888Sandreas@sandberg.pp.se 1279888Sandreas@sandberg.pp.se/// Print welcome message. 1289888Sandreas@sandberg.pp.sestatic void 1299888Sandreas@sandberg.pp.sesayHello(ostream &out) 1309888Sandreas@sandberg.pp.se{ 1319888Sandreas@sandberg.pp.se extern const char *compileDate; // from date.cc 1329888Sandreas@sandberg.pp.se 1339888Sandreas@sandberg.pp.se ccprintf(out, "M5 Simulator System\n"); 1349888Sandreas@sandberg.pp.se // display copyright 1359888Sandreas@sandberg.pp.se ccprintf(out, "%s\n", briefCopyright); 1369888Sandreas@sandberg.pp.se ccprintf(out, "M5 compiled on %d\n", compileDate); 1379888Sandreas@sandberg.pp.se 1389888Sandreas@sandberg.pp.se char *host = getenv("HOSTNAME"); 1399888Sandreas@sandberg.pp.se if (!host) 1409888Sandreas@sandberg.pp.se host = getenv("HOST"); 1419888Sandreas@sandberg.pp.se 1429888Sandreas@sandberg.pp.se if (host) 1439888Sandreas@sandberg.pp.se ccprintf(out, "M5 executing on %s\n", host); 1449888Sandreas@sandberg.pp.se 1459888Sandreas@sandberg.pp.se ccprintf(out, "M5 simulation started %s\n", Time::start); 1469888Sandreas@sandberg.pp.se} 1479888Sandreas@sandberg.pp.se 1489888Sandreas@sandberg.pp.se/// 1499888Sandreas@sandberg.pp.se/// Echo the command line for posterity in such a way that it can be 1509888Sandreas@sandberg.pp.se/// used to rerun the same simulation (given the same .ini files). 1519888Sandreas@sandberg.pp.se/// 1529888Sandreas@sandberg.pp.sestatic void 1539888Sandreas@sandberg.pp.seechoCommandLine(int argc, char **argv, ostream &out) 1549888Sandreas@sandberg.pp.se{ 1559888Sandreas@sandberg.pp.se out << "command line: " << argv[0]; 1569888Sandreas@sandberg.pp.se for (int i = 1; i < argc; i++) { 1579888Sandreas@sandberg.pp.se 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