root.cc revision 1310
1/*
2 * Copyright (c) 2002-2004 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#include <sys/types.h>
30#include <sys/stat.h>
31
32#include <cstring>
33#include <fstream>
34#include <list>
35#include <string>
36#include <vector>
37
38#include "base/misc.hh"
39#include "sim/builder.hh"
40#include "sim/host.hh"
41#include "sim/sim_object.hh"
42#include "sim/universe.hh"
43
44using namespace std;
45
46Tick curTick = 0;
47Tick ticksPerSecond;
48double __ticksPerMS;
49double __ticksPerUS;
50double __ticksPerNS;
51double __ticksPerPS;
52
53string outputDirectory;
54ostream *outputStream;
55ostream *configStream;
56
57// Dummy Object
58class Root : public SimObject
59{
60  public:
61    Root(const std::string &name) : SimObject(name) {}
62};
63Root *root = NULL;
64
65std::ostream *
66makeOutputStream(std::string &name)
67{
68    if (name == "cerr" || name == "stderr")
69        return &std::cerr;
70
71    if (name == "cout" || name == "stdout")
72        return &std::cout;
73
74    string path = (name[0] != '/') ? outputDirectory + name : name;
75
76    // have to dynamically allocate a stream since we're going to
77    // return it... though the caller can't easily free it since it
78    // may be cerr or cout.  need GC!
79    ofstream *s = new ofstream(path.c_str(), ios::trunc);
80
81    if (!s->is_open())
82        fatal("Cannot open file %s", path);
83
84    return s;
85}
86
87
88void
89closeOutputStream(std::ostream *os)
90{
91    // can't close cerr or cout
92    if (os == &std::cerr || os == &std::cout)
93        return;
94
95    // can only close ofstreams, not generic ostreams, so try to
96    // downcast and close only if the downcast succeeds
97    std::ofstream *ofs = dynamic_cast<std::ofstream *>(os);
98    if (ofs)
99        ofs->close();
100}
101
102
103BEGIN_DECLARE_SIM_OBJECT_PARAMS(Root)
104
105    Param<bool> full_system;
106    Param<Tick> frequency;
107    Param<string> output_dir;
108    Param<string> output_file;
109    Param<string> config_output_file;
110
111END_DECLARE_SIM_OBJECT_PARAMS(Root)
112
113BEGIN_INIT_SIM_OBJECT_PARAMS(Root)
114
115    INIT_PARAM_DFLT(full_system, "full system simulation", true),
116    INIT_PARAM_DFLT(frequency, "tick frequency", 200000000),
117    INIT_PARAM_DFLT(output_dir, "directory to output data to", "."),
118    INIT_PARAM_DFLT(output_file, "file to dump simulator output to", "cout"),
119    INIT_PARAM_DFLT(config_output_file, "file to dump simulator config to",
120                    "m5config.out")
121
122END_INIT_SIM_OBJECT_PARAMS(Root)
123
124CREATE_SIM_OBJECT(Root)
125{
126#ifdef FULL_SYSTEM
127    if (!bool(full_system))
128        panic("FULL_SYSTEM compiled and configuration not full_system");
129#else
130    if (bool(full_system))
131        panic("FULL_SYSTEM not compiled but configuration is full_system");
132#endif
133
134    if (root)
135        panic("only one root object allowed!");
136    root = new Root(getInstanceName());
137
138    ticksPerSecond = frequency;
139    double freq = double(ticksPerSecond);
140    __ticksPerMS = freq / 1.0e3;
141    __ticksPerUS = freq / 1.0e6;
142    __ticksPerNS = freq / 1.0e9;
143    __ticksPerPS = freq / 1.0e12;
144
145    if (output_dir.isValid()) {
146        outputDirectory = output_dir;
147
148        // guarantee that directory ends with a '/'
149        if (outputDirectory[outputDirectory.size() - 1] != '/')
150            outputDirectory += "/";
151
152        if (mkdir(outputDirectory.c_str(), 0777) < 0) {
153            if (errno != EEXIST) {
154                panic("%s\ncould not make output directory: %s\n",
155                      strerror(errno), outputDirectory);
156            }
157        }
158    }
159
160    outputStream = makeOutputStream(output_file);
161    configStream = config_output_file.isValid()
162        ? makeOutputStream(config_output_file)
163        : outputStream;
164
165    return root;
166}
167
168REGISTER_SIM_OBJECT("Root", Root)
169
170