main.cc revision 146
112027Sjungma@eit.uni-kl.de/*
212027Sjungma@eit.uni-kl.de * Copyright (c) 2003 The Regents of The University of Michigan
312027Sjungma@eit.uni-kl.de * All rights reserved.
412027Sjungma@eit.uni-kl.de *
512027Sjungma@eit.uni-kl.de * Redistribution and use in source and binary forms, with or without
612027Sjungma@eit.uni-kl.de * modification, are permitted provided that the following conditions are
712027Sjungma@eit.uni-kl.de * met: redistributions of source code must retain the above copyright
812027Sjungma@eit.uni-kl.de * notice, this list of conditions and the following disclaimer;
912027Sjungma@eit.uni-kl.de * redistributions in binary form must reproduce the above copyright
1012027Sjungma@eit.uni-kl.de * notice, this list of conditions and the following disclaimer in the
1112027Sjungma@eit.uni-kl.de * documentation and/or other materials provided with the distribution;
1212027Sjungma@eit.uni-kl.de * neither the name of the copyright holders nor the names of its
1312027Sjungma@eit.uni-kl.de * contributors may be used to endorse or promote products derived from
1412027Sjungma@eit.uni-kl.de * this software without specific prior written permission.
1512027Sjungma@eit.uni-kl.de *
1612027Sjungma@eit.uni-kl.de * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712027Sjungma@eit.uni-kl.de * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812027Sjungma@eit.uni-kl.de * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912027Sjungma@eit.uni-kl.de * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012027Sjungma@eit.uni-kl.de * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112027Sjungma@eit.uni-kl.de * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212027Sjungma@eit.uni-kl.de * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312027Sjungma@eit.uni-kl.de * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412027Sjungma@eit.uni-kl.de * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512027Sjungma@eit.uni-kl.de * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612027Sjungma@eit.uni-kl.de * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712027Sjungma@eit.uni-kl.de */
2812027Sjungma@eit.uni-kl.de
2912027Sjungma@eit.uni-kl.de///
3012027Sjungma@eit.uni-kl.de/// @file sim/main.cc
3112027Sjungma@eit.uni-kl.de///
3212027Sjungma@eit.uni-kl.de#include <sys/types.h>
3312027Sjungma@eit.uni-kl.de#include <sys/stat.h>
3412027Sjungma@eit.uni-kl.de#include <stdlib.h>
3512027Sjungma@eit.uni-kl.de#include <signal.h>
3612027Sjungma@eit.uni-kl.de
3712027Sjungma@eit.uni-kl.de#include <string>
3812027Sjungma@eit.uni-kl.de#include <vector>
3912027Sjungma@eit.uni-kl.de
4012027Sjungma@eit.uni-kl.de#include "base/copyright.hh"
4112027Sjungma@eit.uni-kl.de#include "base/inifile.hh"
4212027Sjungma@eit.uni-kl.de#include "base/misc.hh"
4312027Sjungma@eit.uni-kl.de#include "base/pollevent.hh"
4412027Sjungma@eit.uni-kl.de#include "base/statistics.hh"
4512027Sjungma@eit.uni-kl.de#include "cpu/base_cpu.hh"
4612027Sjungma@eit.uni-kl.de#include "cpu/full_cpu/smt.hh"
4712027Sjungma@eit.uni-kl.de#include "sim/async.hh"
4812027Sjungma@eit.uni-kl.de#include "sim/builder.hh"
4912027Sjungma@eit.uni-kl.de#include "sim/configfile.hh"
5012027Sjungma@eit.uni-kl.de#include "sim/host.hh"
5112027Sjungma@eit.uni-kl.de#include "sim/sim_events.hh"
5212027Sjungma@eit.uni-kl.de#include "sim/sim_exit.hh"
5312027Sjungma@eit.uni-kl.de#include "sim/sim_object.hh"
5412027Sjungma@eit.uni-kl.de#include "sim/sim_stats.hh"
5512027Sjungma@eit.uni-kl.de#include "sim/sim_time.hh"
5612027Sjungma@eit.uni-kl.de
5712027Sjungma@eit.uni-kl.deusing namespace std;
5812027Sjungma@eit.uni-kl.de
5912027Sjungma@eit.uni-kl.de// See async.h.
6012027Sjungma@eit.uni-kl.devolatile bool async_event = false;
6112027Sjungma@eit.uni-kl.devolatile bool async_dump = false;
6212027Sjungma@eit.uni-kl.devolatile bool async_exit = false;
6312027Sjungma@eit.uni-kl.devolatile bool async_io = false;
6412027Sjungma@eit.uni-kl.devolatile bool async_alarm = false;
6512027Sjungma@eit.uni-kl.de
6612027Sjungma@eit.uni-kl.de/// Stats signal handler.
6712027Sjungma@eit.uni-kl.devoid
6812027Sjungma@eit.uni-kl.dedumpStatsHandler(int sigtype)
6912027Sjungma@eit.uni-kl.de{
7012027Sjungma@eit.uni-kl.de    async_event = true;
7112027Sjungma@eit.uni-kl.de    async_dump = true;
7212027Sjungma@eit.uni-kl.de}
7312027Sjungma@eit.uni-kl.de
7412027Sjungma@eit.uni-kl.de/// Exit signal handler.
7512027Sjungma@eit.uni-kl.devoid
7612027Sjungma@eit.uni-kl.deexitNowHandler(int sigtype)
7712027Sjungma@eit.uni-kl.de{
7812027Sjungma@eit.uni-kl.de    async_event = true;
7912027Sjungma@eit.uni-kl.de    async_exit = true;
8012027Sjungma@eit.uni-kl.de}
8112027Sjungma@eit.uni-kl.de
8212027Sjungma@eit.uni-kl.de/// Simulator executable name
8312027Sjungma@eit.uni-kl.deconst char *myProgName = "";
8412027Sjungma@eit.uni-kl.de
8512027Sjungma@eit.uni-kl.de/// Show brief help message.
8612027Sjungma@eit.uni-kl.destatic void
8712027Sjungma@eit.uni-kl.deshowBriefHelp(ostream &out)
8812027Sjungma@eit.uni-kl.de{
8912027Sjungma@eit.uni-kl.de    out << "Usage: " << myProgName
9012027Sjungma@eit.uni-kl.de         << " [-hn] [-Dname[=def]] [-Uname] [-I[dir]] "
9112027Sjungma@eit.uni-kl.de         << "[--<section>:<param>=<value>] [<config file> ...]" << endl
9212027Sjungma@eit.uni-kl.de         << "   -h: print long help (including parameter listing)" << endl
9312027Sjungma@eit.uni-kl.de         << "   -n: don't load default.ini" << endl
9412027Sjungma@eit.uni-kl.de         << "   -u: don't quit on unreferenced parameters" << endl
9512027Sjungma@eit.uni-kl.de         << "   -D,-U,-I: passed to cpp for preprocessing .ini files" << endl;
9612027Sjungma@eit.uni-kl.de}
9712027Sjungma@eit.uni-kl.de
9812027Sjungma@eit.uni-kl.de/// Show verbose help message.  Includes parameter listing from
9912027Sjungma@eit.uni-kl.de/// showBriefHelp(), plus an exhaustive list of ini-file parameters
10012027Sjungma@eit.uni-kl.de/// and SimObjects (with their parameters).
10112027Sjungma@eit.uni-kl.destatic void
10212027Sjungma@eit.uni-kl.deshowLongHelp(ostream &out)
10312027Sjungma@eit.uni-kl.de{
10412027Sjungma@eit.uni-kl.de    showBriefHelp(out);
10512027Sjungma@eit.uni-kl.de
10612027Sjungma@eit.uni-kl.de    out << endl
10712027Sjungma@eit.uni-kl.de        << endl
10812027Sjungma@eit.uni-kl.de        << "-----------------" << endl
10912027Sjungma@eit.uni-kl.de        << "Global Parameters" << endl
11012027Sjungma@eit.uni-kl.de        << "-----------------" << endl
11112027Sjungma@eit.uni-kl.de        << endl;
11212027Sjungma@eit.uni-kl.de
11312027Sjungma@eit.uni-kl.de    ParamContext::describeAllContexts(out);
11412027Sjungma@eit.uni-kl.de
11512027Sjungma@eit.uni-kl.de    out << endl
11612027Sjungma@eit.uni-kl.de        << endl
11712027Sjungma@eit.uni-kl.de        << "-----------------" << endl
11812027Sjungma@eit.uni-kl.de        << "Simulator Objects" << endl
11912027Sjungma@eit.uni-kl.de        << "-----------------" << endl
12012027Sjungma@eit.uni-kl.de        << endl;
12112027Sjungma@eit.uni-kl.de
12212027Sjungma@eit.uni-kl.de    SimObjectClass::describeAllClasses(out);
12312027Sjungma@eit.uni-kl.de}
12412027Sjungma@eit.uni-kl.de
12512027Sjungma@eit.uni-kl.de/// Print welcome message.
12612027Sjungma@eit.uni-kl.destatic void
12712027Sjungma@eit.uni-kl.desayHello(ostream &out)
12812027Sjungma@eit.uni-kl.de{
12912027Sjungma@eit.uni-kl.de    extern const char *compileDate;	// from date.cc
13012027Sjungma@eit.uni-kl.de
13112027Sjungma@eit.uni-kl.de    ccprintf(out, "M5 Simulator System\n");
13212027Sjungma@eit.uni-kl.de    // display copyright
13312027Sjungma@eit.uni-kl.de    ccprintf(out, "%s\n", briefCopyright);
13412027Sjungma@eit.uni-kl.de    ccprintf(out, "M5 compiled on %d\n", compileDate);
13512027Sjungma@eit.uni-kl.de
13612027Sjungma@eit.uni-kl.de    char *host = getenv("HOSTNAME");
13712027Sjungma@eit.uni-kl.de    if (!host)
13812027Sjungma@eit.uni-kl.de        host = getenv("HOST");
13912027Sjungma@eit.uni-kl.de
14012027Sjungma@eit.uni-kl.de    if (host)
14112027Sjungma@eit.uni-kl.de        ccprintf(out, "M5 executing on %s\n", host);
14212027Sjungma@eit.uni-kl.de
14312027Sjungma@eit.uni-kl.de    ccprintf(out, "M5 simulation started %s\n", Time::start);
14412027Sjungma@eit.uni-kl.de}
14512027Sjungma@eit.uni-kl.de
14612027Sjungma@eit.uni-kl.de///
14712027Sjungma@eit.uni-kl.de/// Echo the command line for posterity in such a way that it can be
14812027Sjungma@eit.uni-kl.de/// used to rerun the same simulation (given the same .ini files).
14912027Sjungma@eit.uni-kl.de///
15012027Sjungma@eit.uni-kl.destatic void
15112027Sjungma@eit.uni-kl.deechoCommandLine(int argc, char **argv, ostream &out)
15212027Sjungma@eit.uni-kl.de{
15312027Sjungma@eit.uni-kl.de    out << "command line: " << argv[0];
15412027Sjungma@eit.uni-kl.de    for (int i = 1; i < argc; i++) {
15512027Sjungma@eit.uni-kl.de        string arg(argv[i]);
15612027Sjungma@eit.uni-kl.de
15712027Sjungma@eit.uni-kl.de        out << ' ';
15812027Sjungma@eit.uni-kl.de
15912027Sjungma@eit.uni-kl.de        // If the arg contains spaces, we need to quote it.
16012027Sjungma@eit.uni-kl.de        // The rest of this is overkill to make it look purty.
16112027Sjungma@eit.uni-kl.de
16212027Sjungma@eit.uni-kl.de        // print dashes first outside quotes
16312027Sjungma@eit.uni-kl.de        int non_dash_pos = arg.find_first_not_of("-");
16412027Sjungma@eit.uni-kl.de        out << arg.substr(0, non_dash_pos);	// print dashes
16512027Sjungma@eit.uni-kl.de        string body = arg.substr(non_dash_pos);	// the rest
16612027Sjungma@eit.uni-kl.de
16712027Sjungma@eit.uni-kl.de        // if it's an assignment, handle the lhs & rhs separately
16812027Sjungma@eit.uni-kl.de        int eq_pos = body.find("=");
16912027Sjungma@eit.uni-kl.de        if (eq_pos == string::npos) {
17012027Sjungma@eit.uni-kl.de            out << quote(body);
17112027Sjungma@eit.uni-kl.de        }
17212027Sjungma@eit.uni-kl.de        else {
17312027Sjungma@eit.uni-kl.de            string lhs(body.substr(0, eq_pos));
17412027Sjungma@eit.uni-kl.de            string rhs(body.substr(eq_pos + 1));
17512027Sjungma@eit.uni-kl.de
17612027Sjungma@eit.uni-kl.de            out << quote(lhs) << "=" << quote(rhs);
17712027Sjungma@eit.uni-kl.de        }
17812027Sjungma@eit.uni-kl.de    }
17912027Sjungma@eit.uni-kl.de    out << endl << endl;
18012027Sjungma@eit.uni-kl.de}
18112027Sjungma@eit.uni-kl.de
18212027Sjungma@eit.uni-kl.de
18312027Sjungma@eit.uni-kl.de///
18412027Sjungma@eit.uni-kl.de/// The simulator configuration database.  This is the union of all
18512027Sjungma@eit.uni-kl.de/// specified .ini files.  This shouldn't need to be visible outside
18612027Sjungma@eit.uni-kl.de/// this file, as it is passed as a parameter to all the param-parsing
18712027Sjungma@eit.uni-kl.de/// routines.
18812027Sjungma@eit.uni-kl.de///
18912027Sjungma@eit.uni-kl.destatic IniFile simConfigDB;
19012027Sjungma@eit.uni-kl.de
19112027Sjungma@eit.uni-kl.de/// Check for a default.ini file and load it if necessary.
19212027Sjungma@eit.uni-kl.destatic void
19312027Sjungma@eit.uni-kl.dehandleDefaultIni(bool &loadIt, vector<char *> &cppArgs)
19412027Sjungma@eit.uni-kl.de{
19512027Sjungma@eit.uni-kl.de    struct stat sb;
19612027Sjungma@eit.uni-kl.de
19712027Sjungma@eit.uni-kl.de    if (loadIt) {
19812027Sjungma@eit.uni-kl.de        if (stat("default.ini", &sb) == 0) {
19912027Sjungma@eit.uni-kl.de            if (!simConfigDB.loadCPP("default.ini", cppArgs)) {
200                cout << "Error processing file default.ini" << endl;
201                exit(1);
202            }
203        }
204
205        // set this whether it actually was found or not, so we don't
206        // bother to check again next time
207        loadIt = false;
208    }
209}
210
211
212/// M5 entry point.
213int
214main(int argc, char **argv)
215{
216    // Save off program name
217    myProgName = argv[0];
218
219    signal(SIGFPE, SIG_IGN);		// may occur on misspeculated paths
220    signal(SIGPIPE, SIG_IGN);
221    signal(SIGTRAP, SIG_IGN);
222    signal(SIGUSR1, dumpStatsHandler);	// dump intermediate stats
223    signal(SIGINT, exitNowHandler);	// dump final stats and exit
224
225    sayHello(cerr);
226
227    // Initialize statistics database
228    initBaseStats();
229
230    vector<char *> cppArgs;
231
232    // Should we use default.ini if it exists?  By default, yes.  (Use
233    // -n to override.)
234    bool loadDefaultIni = true;
235
236    // Should we quit if there are unreferenced parameters?  By
237    // default, yes... it's a good way of catching typos in
238    // section/parameter names (which otherwise go by silently).  Use
239    // -u to override.
240    bool quitOnUnreferenced = true;
241
242    // Parse command-line options.  The tricky part here is figuring
243    // out whether to look for & load default.ini, and if needed,
244    // doing so at the right time w.r.t. processing the other
245    // parameters.
246    //
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 'h':
260                // -h: show help
261                showLongHelp(cerr);
262                exit(1);
263
264              case 'n':
265                // -n: don't load default.ini
266                if (!loadDefaultIni) {
267                    cerr << "Warning: -n option needs to precede any "
268                         << "explicit configuration file name " << endl
269                         << "         or command-line configuration parameter."
270                         << endl;
271                }
272                loadDefaultIni = false;
273                break;
274
275              case 'u':
276                // -u: don't quit on unreferenced parameters
277                quitOnUnreferenced = false;
278                break;
279
280              case 'D':
281              case 'U':
282              case 'I':
283                // cpp options: record & pass to cpp.  Note that these
284                // cannot have spaces, i.e., '-Dname=val' is OK, but
285                // '-D name=val' is not.  I don't consider this a
286                // problem, since even though gnu cpp accepts the
287                // latter, other cpp implementations do not (Tru64,
288                // for one).
289                cppArgs.push_back(arg_str);
290                break;
291
292              case '-':
293                // command-line configuration parameter:
294                // '--<section>:<parameter>=<value>'
295
296                // Load default.ini if necessary -- see comment in
297                // else clause below.
298                handleDefaultIni(loadDefaultIni, cppArgs);
299
300                if (!simConfigDB.add(arg_str + 2)) {
301                    // parse error
302                    ccprintf(cerr,
303                             "Could not parse configuration argument '%s'\n"
304                             "Expecting --<section>:<parameter>=<value>\n",
305                             arg_str);
306                    exit(0);
307                }
308                break;
309
310              default:
311                showBriefHelp(cerr);
312                ccprintf(cerr, "Fatal: invalid argument '%s'\n", arg_str);
313                exit(0);
314            }
315        }
316        else {
317            // no '-', treat as config file name
318
319            // If we haven't loaded default.ini yet, and we want to,
320            // now is the time.  Can't do it sooner because we need to
321            // look for '-n', can't do it later since we want
322            // default.ini loaded first (so that any other settings
323            // override it).
324            handleDefaultIni(loadDefaultIni, cppArgs);
325
326            if (!simConfigDB.loadCPP(arg_str, cppArgs)) {
327                cprintf("Error processing file %s\n", arg_str);
328                exit(1);
329            }
330        }
331    }
332
333    // Final check for default.ini, in case no config files or
334    // command-line config parameters were given.
335    handleDefaultIni(loadDefaultIni, cppArgs);
336
337    // The configuration database is now complete; start processing it.
338
339    // Parse and check all non-config-hierarchy parameters.
340    ParamContext::parseAllContexts(simConfigDB);
341    ParamContext::checkAllContexts();
342
343    // Print header info into stats file.  Can't do this sooner since
344    // the stat file name is set via a .ini param... thus it just got
345    // opened above during ParamContext::checkAllContexts().
346
347    // Print hello message to stats file if it's actually a file.  If
348    // it's not (i.e. it's cout or cerr) then we already did it above.
349    if (statStreamIsFile)
350        sayHello(*statStream);
351
352    // Echo command line and all parameter settings to stats file as well.
353    echoCommandLine(argc, argv, *statStream);
354    ParamContext::showAllContexts(*statStream);
355
356    // Now process the configuration hierarchy and create the SimObjects.
357    ConfigHierarchy configHierarchy(simConfigDB);
358    configHierarchy.build();
359    configHierarchy.createSimObjects();
360
361    // Restore checkpointed state, if any.
362    configHierarchy.unserializeSimObjects();
363
364    // Done processing the configuration database.
365    // Check for unreferenced entries.
366    if (simConfigDB.printUnreferenced() && quitOnUnreferenced) {
367        cerr << "Fatal: unreferenced .ini sections/entries." << endl
368             << "If this is not an error, add 'unref_section_ok=y' or "
369             << "'unref_entries_ok=y' to the appropriate sections "
370             << "to suppress this message." << endl;
371        exit(1);
372    }
373
374    SimObject::regAllStats();
375
376    // uncomment the following to get PC-based execution-time profile
377#ifdef DO_PROFILE
378    init_profile((char *)&_init, (char *)&_fini);
379#endif
380
381    // Check to make sure that the stats package is properly initialized
382    Statistics::check();
383
384    // Nothing to simulate if we don't have at least one CPU somewhere.
385    if (BaseCPU::numSimulatedCPUs() == 0) {
386        cerr << "Fatal: no CPUs to simulate." << endl;
387        exit(1);
388    }
389
390    while (!mainEventQueue.empty()) {
391        assert(curTick <= mainEventQueue.nextTick() &&
392               "event scheduled in the past");
393
394        // forward current cycle to the time of the first event on the
395        // queue
396        curTick = mainEventQueue.nextTick();
397        mainEventQueue.serviceOne();
398
399        if (async_event) {
400            async_event = false;
401            if (async_dump) {
402                async_dump = false;
403                new DumpStatsEvent();
404            }
405
406            if (async_exit) {
407                async_exit = false;
408                new SimExitEvent("User requested STOP");
409            }
410
411            if (async_io || async_alarm) {
412                async_io = false;
413                async_alarm = false;
414                pollQueue.service();
415            }
416        }
417    }
418
419    // This should never happen... every conceivable way for the
420    // simulation to terminate (hit max cycles/insts, signal,
421    // simulated system halts/exits) generates an exit event, so we
422    // should never run out of events on the queue.
423    exitNow("improperly exited event loop!", 1);
424
425    return 0;
426}
427