2 * Copyright (c) 2000-2005 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>
43 #include "base/copyright.hh"
44 #include "base/embedfile.hh"
45 #include "base/inifile.hh"
46 #include "base/misc.hh"
47 #include "base/output.hh"
48 #include "base/pollevent.hh"
49 #include "base/statistics.hh"
50 #include "base/str.hh"
51 #include "base/time.hh"
52 #include "cpu/base.hh"
54 #include "python/pyconfig.hh"
55 #include "sim/async.hh"
56 #include "sim/builder.hh"
57 #include "sim/configfile.hh"
58 #include "sim/host.hh"
59 #include "sim/sim_events.hh"
60 #include "sim/sim_exit.hh"
61 #include "sim/sim_object.hh"
62 #include "sim/stat_control.hh"
63 #include "sim/stats.hh"
64 #include "sim/root.hh"
69 volatile bool async_event
= false;
70 volatile bool async_dump
= false;
71 volatile bool async_dumpreset
= false;
72 volatile bool async_exit
= false;
73 volatile bool async_io
= false;
74 volatile bool async_alarm
= false;
76 /// Stats signal handler.
78 dumpStatsHandler(int sigtype
)
85 dumprstStatsHandler(int sigtype
)
88 async_dumpreset
= true;
91 /// Exit signal handler.
93 exitNowHandler(int sigtype
)
99 /// Abort signal handler.
101 abortHandler(int sigtype
)
103 cerr
<< "Program aborted at cycle " << curTick
<< endl
;
106 // dump trace buffer, if there is one
107 Trace::theLog
.dump(cerr
);
111 /// Simulator executable name
112 char *myProgName
= "";
114 /// Show brief help message.
116 showBriefHelp(ostream
&out
)
118 char *prog
= basename(myProgName
);
120 ccprintf(out
, "Usage:\n");
122 "%s [-d <dir>] [-E <var>[=<val>]] [-I <dir>] [-P <python>]\n"
123 " [--<var>=<val>] <config file>\n"
125 " -d set the output directory to <dir>\n"
126 " -E set the environment variable <var> to <val> (or 'True')\n"
127 " -I add the directory <dir> to python's path\n"
128 " -P execute <python> directly in the configuration\n"
129 " --var=val set the python variable <var> to '<val>'\n"
130 " <configfile> config file name (ends in .py)\n\n",
133 ccprintf(out
, "%s -X\n -X extract embedded files\n\n", prog
);
134 ccprintf(out
, "%s -h\n -h print short help\n\n", prog
);
137 /// Print welcome message.
139 sayHello(ostream
&out
)
141 extern const char *compileDate
; // from date.cc
143 ccprintf(out
, "M5 Simulator System\n");
145 ccprintf(out
, "%s\n", briefCopyright
);
146 ccprintf(out
, "M5 compiled on %d\n", compileDate
);
148 char *host
= getenv("HOSTNAME");
150 host
= getenv("HOST");
153 ccprintf(out
, "M5 executing on %s\n", host
);
155 ccprintf(out
, "M5 simulation started %s\n", Time::start
);
159 /// Echo the command line for posterity in such a way that it can be
160 /// used to rerun the same simulation (given the same .ini files).
163 echoCommandLine(int argc
, char **argv
, ostream
&out
)
165 out
<< "command line: " << argv
[0];
166 for (int i
= 1; i
< argc
; i
++) {
171 // If the arg contains spaces, we need to quote it.
172 // The rest of this is overkill to make it look purty.
174 // print dashes first outside quotes
175 int non_dash_pos
= arg
.find_first_not_of("-");
176 out
<< arg
.substr(0, non_dash_pos
); // print dashes
177 string body
= arg
.substr(non_dash_pos
); // the rest
179 // if it's an assignment, handle the lhs & rhs separately
180 int eq_pos
= body
.find("=");
181 if (eq_pos
== string::npos
) {
185 string
lhs(body
.substr(0, eq_pos
));
186 string
rhs(body
.substr(eq_pos
+ 1));
188 out
<< quote(lhs
) << "=" << quote(rhs
);
195 getOptionString(int &index
, int argc
, char **argv
)
197 char *option
= argv
[index
] + 2;
201 // We didn't find an argument, it must be in the next variable.
203 panic("option string for option '%s' not found", argv
[index
- 1]);
209 main(int argc
, char **argv
)
211 // Save off program name
212 myProgName
= argv
[0];
214 signal(SIGFPE
, SIG_IGN
); // may occur on misspeculated paths
215 signal(SIGTRAP
, SIG_IGN
);
216 signal(SIGUSR1
, dumpStatsHandler
); // dump intermediate stats
217 signal(SIGUSR2
, dumprstStatsHandler
); // dump and reset stats
218 signal(SIGINT
, exitNowHandler
); // dump final stats and exit
219 signal(SIGABRT
, abortHandler
);
221 bool configfile_found
= false;
222 PythonConfig pyconfig
;
232 // Parse command-line options.
233 // Since most of the complex options are handled through the
234 // config database, we don't mess with getopts, and just parse
236 for (int i
= 1; i
< argc
; ++i
) {
237 char *arg_str
= argv
[i
];
239 // if arg starts with '--', parse as a special python option
240 // of the format --<python var>=<string value>, if the arg
241 // starts with '-', it should be a simulator option with a
242 // format similar to getopt. In any other case, treat the
243 // option as a configuration file name and load it.
244 if (arg_str
[0] == '-' && arg_str
[1] == '-') {
245 string str
= &arg_str
[2];
248 if (!split_first(str
, var
, val
, '='))
249 panic("Could not parse configuration argument '%s'\n"
250 "Expecting --<variable>=<value>\n", arg_str
);
252 pyconfig
.setVariable(var
, val
);
253 } else if (arg_str
[0] == '-') {
257 // switch on second char
258 switch (arg_str
[1]) {
260 outdir
= getOptionString(i
, argc
, argv
);
268 option
= getOptionString(i
, argc
, argv
);
269 if (!split_first(option
, var
, val
, '='))
272 if (setenv(var
.c_str(), val
.c_str(), true) == -1)
273 panic("setenv: %s\n", strerror(errno
));
277 option
= getOptionString(i
, argc
, argv
);
278 pyconfig
.addPath(option
);
282 option
= getOptionString(i
, argc
, argv
);
283 pyconfig
.writeLine(option
);
289 list
<EmbedFile
>::iterator i
= lst
.begin();
290 list
<EmbedFile
>::iterator end
= lst
.end();
293 cprintf("Embedded File: %s\n", i
->name
);
294 cout
.write(i
->data
, i
->length
);
303 panic("invalid argument '%s'\n", arg_str
);
306 string
file(arg_str
);
309 if (!split_last(file
, base
, ext
, '.') || ext
!= "py")
310 panic("Config file '%s' must end in '.py'\n", file
);
313 configfile_found
= true;
317 if (outdir
.empty()) {
318 char *env
= getenv("OUTPUT_DIR");
319 outdir
= env
? env
: ".";
322 simout
.setDirectory(outdir
);
324 char *env
= getenv("CONFIG_OUTPUT");
327 configStream
= simout
.find(env
);
329 if (!configfile_found
)
330 panic("no configuration file specified!");
332 // The configuration database is now complete; start processing it.
334 if (!pyconfig
.output(inifile
))
335 panic("Error processing python code");
337 // Initialize statistics database
338 Stats::InitSimStats();
340 // Now process the configuration hierarchy and create the SimObjects.
341 ConfigHierarchy
configHierarchy(inifile
);
342 configHierarchy
.build();
343 configHierarchy
.createSimObjects();
345 // Parse and check all non-config-hierarchy parameters.
346 ParamContext::parseAllContexts(inifile
);
347 ParamContext::checkAllContexts();
349 // Print hello message to stats file if it's actually a file. If
350 // it's not (i.e. it's cout or cerr) then we already did it above.
351 if (simout
.isFile(*outputStream
))
352 sayHello(*outputStream
);
354 // Echo command line and all parameter settings to stats file as well.
355 echoCommandLine(argc
, argv
, *outputStream
);
356 ParamContext::showAllContexts(*configStream
);
358 // Do a second pass to finish initializing the sim objects
359 SimObject::initAll();
361 // Restore checkpointed state, if any.
362 configHierarchy
.unserializeSimObjects();
364 // Done processing the configuration database.
365 // Check for unreferenced entries.
366 if (inifile
.printUnreferenced())
367 panic("unreferenced sections/entries in the intermediate ini file");
369 SimObject::regAllStats();
371 // uncomment the following to get PC-based execution-time profile
373 init_profile((char *)&_init
, (char *)&_fini
);
376 // Check to make sure that the stats package is properly initialized
379 // Reset to put the stats in a consistent state.
382 warn("Entering event queue. Starting simulation...\n");
384 while (!mainEventQueue
.empty()) {
385 assert(curTick
<= mainEventQueue
.nextTick() &&
386 "event scheduled in the past");
388 // forward current cycle to the time of the first event on the
390 curTick
= mainEventQueue
.nextTick();
391 mainEventQueue
.serviceOne();
398 using namespace Stats
;
399 SetupEvent(Dump
, curTick
);
402 if (async_dumpreset
) {
403 async_dumpreset
= false;
405 using namespace Stats
;
406 SetupEvent(Dump
| Reset
, curTick
);
411 new SimExitEvent("User requested STOP");
414 if (async_io
|| async_alarm
) {
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("no events on event loop! All CPUs must be idle.", 1);