2 * Copyright (c) 2000-2004 The Regents of The University of Michigan
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.
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.
32 #include <sys/types.h>
40 #include "base/copyright.hh"
41 #include "base/inifile.hh"
42 #include "base/misc.hh"
43 #include "base/pollevent.hh"
44 #include "base/statistics.hh"
45 #include "base/time.hh"
46 #include "cpu/base_cpu.hh"
47 #include "cpu/full_cpu/smt.hh"
48 #include "sim/async.hh"
49 #include "sim/builder.hh"
50 #include "sim/configfile.hh"
51 #include "sim/host.hh"
52 #include "sim/sim_events.hh"
53 #include "sim/sim_exit.hh"
54 #include "sim/sim_init.hh"
55 #include "sim/sim_object.hh"
56 #include "sim/stat_control.hh"
57 #include "sim/stats.hh"
58 #include "sim/universe.hh"
63 volatile bool async_event
= false;
64 volatile bool async_dump
= false;
65 volatile bool async_dumpreset
= false;
66 volatile bool async_exit
= false;
67 volatile bool async_io
= false;
68 volatile bool async_alarm
= false;
70 /// Stats signal handler.
72 dumpStatsHandler(int sigtype
)
79 dumprstStatsHandler(int sigtype
)
82 async_dumpreset
= true;
85 /// Exit signal handler.
87 exitNowHandler(int sigtype
)
93 /// Abort signal handler.
95 abortHandler(int sigtype
)
97 cerr
<< "Program aborted at cycle " << curTick
<< endl
;
100 // dump trace buffer, if there is one
101 Trace::theLog
.dump(cerr
);
105 /// Simulator executable name
106 const char *myProgName
= "";
108 /// Show brief help message.
110 showBriefHelp(ostream
&out
)
112 out
<< "Usage: " << myProgName
113 << " [-hn] [-Dname[=def]] [-Uname] [-I[dir]] "
114 << "[--<section>:<param>=<value>] [<config file> ...]" << endl
115 << " -h: print long help (including parameter listing)" << endl
116 << " -n: don't load default.ini" << endl
117 << " -u: don't quit on unreferenced parameters" << endl
118 << " -D,-U,-I: passed to cpp for preprocessing .ini files" << endl
;
121 /// Show verbose help message. Includes parameter listing from
122 /// showBriefHelp(), plus an exhaustive list of ini-file parameters
123 /// and SimObjects (with their parameters).
125 showLongHelp(ostream
&out
)
131 << "-----------------" << endl
132 << "Global Parameters" << endl
133 << "-----------------" << endl
136 ParamContext::describeAllContexts(out
);
140 << "-----------------" << endl
141 << "Simulator Objects" << endl
142 << "-----------------" << endl
145 SimObjectClass::describeAllClasses(out
);
148 /// Print welcome message.
150 sayHello(ostream
&out
)
152 extern const char *compileDate
; // from date.cc
154 ccprintf(out
, "M5 Simulator System\n");
156 ccprintf(out
, "%s\n", briefCopyright
);
157 ccprintf(out
, "M5 compiled on %d\n", compileDate
);
159 char *host
= getenv("HOSTNAME");
161 host
= getenv("HOST");
164 ccprintf(out
, "M5 executing on %s\n", host
);
166 ccprintf(out
, "M5 simulation started %s\n", Time::start
);
170 /// Echo the command line for posterity in such a way that it can be
171 /// used to rerun the same simulation (given the same .ini files).
174 echoCommandLine(int argc
, char **argv
, ostream
&out
)
176 out
<< "command line: " << argv
[0];
177 for (int i
= 1; i
< argc
; i
++) {
182 // If the arg contains spaces, we need to quote it.
183 // The rest of this is overkill to make it look purty.
185 // print dashes first outside quotes
186 int non_dash_pos
= arg
.find_first_not_of("-");
187 out
<< arg
.substr(0, non_dash_pos
); // print dashes
188 string body
= arg
.substr(non_dash_pos
); // the rest
190 // if it's an assignment, handle the lhs & rhs separately
191 int eq_pos
= body
.find("=");
192 if (eq_pos
== string::npos
) {
196 string
lhs(body
.substr(0, eq_pos
));
197 string
rhs(body
.substr(eq_pos
+ 1));
199 out
<< quote(lhs
) << "=" << quote(rhs
);
207 /// The simulator configuration database. This is the union of all
208 /// specified .ini files. This shouldn't need to be visible outside
209 /// this file, as it is passed as a parameter to all the param-parsing
212 static IniFile simConfigDB
;
214 /// Check for a default.ini file and load it if necessary.
216 handleDefaultIni(bool &loadIt
, vector
<char *> &cppArgs
)
221 if (stat("default.ini", &sb
) == 0) {
222 if (!simConfigDB
.loadCPP("default.ini", cppArgs
)) {
223 cout
<< "Error processing file default.ini" << endl
;
228 // set this whether it actually was found or not, so we don't
229 // bother to check again next time
237 main(int argc
, char **argv
)
239 // Save off program name
240 myProgName
= argv
[0];
242 signal(SIGFPE
, SIG_IGN
); // may occur on misspeculated paths
243 signal(SIGTRAP
, SIG_IGN
);
244 signal(SIGUSR1
, dumpStatsHandler
); // dump intermediate stats
245 signal(SIGUSR2
, dumprstStatsHandler
); // dump and reset stats
246 signal(SIGINT
, exitNowHandler
); // dump final stats and exit
247 signal(SIGABRT
, abortHandler
);
251 // Initialize statistics database
252 Stats::InitSimStats();
254 vector
<char *> cppArgs
;
256 // Should we use default.ini if it exists? By default, yes. (Use
258 bool loadDefaultIni
= true;
260 // Should we quit if there are unreferenced parameters? By
261 // default, yes... it's a good way of catching typos in
262 // section/parameter names (which otherwise go by silently). Use
264 bool quitOnUnreferenced
= true;
266 // Parse command-line options. The tricky part here is figuring
267 // out whether to look for & load default.ini, and if needed,
268 // doing so at the right time w.r.t. processing the other
271 // Since most of the complex options are handled through the
272 // config database, we don't mess with getopts, and just parse
274 for (int i
= 1; i
< argc
; ++i
) {
275 char *arg_str
= argv
[i
];
277 // if arg starts with '-', parse as option,
278 // else treat it as a configuration file name and load it
279 if (arg_str
[0] == '-') {
281 // switch on second char
282 switch (arg_str
[1]) {
289 // -n: don't load default.ini
290 if (!loadDefaultIni
) {
291 cerr
<< "Warning: -n option needs to precede any "
292 << "explicit configuration file name " << endl
293 << " or command-line configuration parameter."
296 loadDefaultIni
= false;
300 // -u: don't quit on unreferenced parameters
301 quitOnUnreferenced
= false;
307 // cpp options: record & pass to cpp. Note that these
308 // cannot have spaces, i.e., '-Dname=val' is OK, but
309 // '-D name=val' is not. I don't consider this a
310 // problem, since even though gnu cpp accepts the
311 // latter, other cpp implementations do not (Tru64,
313 cppArgs
.push_back(arg_str
);
317 // command-line configuration parameter:
318 // '--<section>:<parameter>=<value>'
320 // Load default.ini if necessary -- see comment in
321 // else clause below.
322 handleDefaultIni(loadDefaultIni
, cppArgs
);
324 if (!simConfigDB
.add(arg_str
+ 2)) {
327 "Could not parse configuration argument '%s'\n"
328 "Expecting --<section>:<parameter>=<value>\n",
336 ccprintf(cerr
, "Fatal: invalid argument '%s'\n", arg_str
);
341 // no '-', treat as config file name
343 // If we haven't loaded default.ini yet, and we want to,
344 // now is the time. Can't do it sooner because we need to
345 // look for '-n', can't do it later since we want
346 // default.ini loaded first (so that any other settings
348 handleDefaultIni(loadDefaultIni
, cppArgs
);
350 if (!simConfigDB
.loadCPP(arg_str
, cppArgs
)) {
351 cprintf("Error processing file %s\n", arg_str
);
357 // Final check for default.ini, in case no config files or
358 // command-line config parameters were given.
359 handleDefaultIni(loadDefaultIni
, cppArgs
);
361 // The configuration database is now complete; start processing it.
363 // Parse and check all non-config-hierarchy parameters.
364 ParamContext::parseAllContexts(simConfigDB
);
365 ParamContext::checkAllContexts();
367 // Print header info into stats file. Can't do this sooner since
368 // the stat file name is set via a .ini param... thus it just got
369 // opened above during ParamContext::checkAllContexts().
371 // Print hello message to stats file if it's actually a file. If
372 // it's not (i.e. it's cout or cerr) then we already did it above.
373 if (outputStream
!= &cout
&& outputStream
!= &cerr
)
374 sayHello(*outputStream
);
376 // Echo command line and all parameter settings to stats file as well.
377 echoCommandLine(argc
, argv
, *outputStream
);
378 ParamContext::showAllContexts(builderStream());
380 // Now process the configuration hierarchy and create the SimObjects.
381 ConfigHierarchy
configHierarchy(simConfigDB
);
382 configHierarchy
.build();
383 configHierarchy
.createSimObjects();
385 // Restore checkpointed state, if any.
386 configHierarchy
.unserializeSimObjects();
388 // Done processing the configuration database.
389 // Check for unreferenced entries.
390 if (simConfigDB
.printUnreferenced() && quitOnUnreferenced
) {
391 cerr
<< "Fatal: unreferenced .ini sections/entries." << endl
392 << "If this is not an error, add 'unref_section_ok=y' or "
393 << "'unref_entries_ok=y' to the appropriate sections "
394 << "to suppress this message." << endl
;
398 SimObject::regAllStats();
400 // uncomment the following to get PC-based execution-time profile
402 init_profile((char *)&_init
, (char *)&_fini
);
405 // Check to make sure that the stats package is properly initialized
408 // Reset to put the stats in a consistent state.
411 // Nothing to simulate if we don't have at least one CPU somewhere.
412 if (BaseCPU::numSimulatedCPUs() == 0) {
413 cerr
<< "Fatal: no CPUs to simulate." << endl
;
418 warn("Entering event queue. Starting simulation...\n");
420 while (!mainEventQueue
.empty()) {
421 assert(curTick
<= mainEventQueue
.nextTick() &&
422 "event scheduled in the past");
424 // forward current cycle to the time of the first event on the
426 curTick
= mainEventQueue
.nextTick();
427 mainEventQueue
.serviceOne();
434 using namespace Stats
;
435 SetupEvent(Dump
, curTick
);
438 if (async_dumpreset
) {
439 async_dumpreset
= false;
441 using namespace Stats
;
442 SetupEvent(Dump
| Reset
, curTick
);
447 new SimExitEvent("User requested STOP");
450 if (async_io
|| async_alarm
) {
458 // This should never happen... every conceivable way for the
459 // simulation to terminate (hit max cycles/insts, signal,
460 // simulated system halts/exits) generates an exit event, so we
461 // should never run out of events on the queue.
462 exitNow("no events on event loop! All CPUs must be idle.", 1);