Forgot about the tracing cpus for the changes to the base class
[gem5.git] / sim / universe.cc
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/universe.hh"
40 #include "sim/host.hh"
41 #include "sim/param.hh"
42
43 using namespace std;
44
45 Tick curTick = 0;
46 Tick ticksPerSecond;
47 double __ticksPerMS;
48 double __ticksPerUS;
49 double __ticksPerNS;
50 double __ticksPerPS;
51
52 string outputDirectory;
53 ostream *outputStream;
54 ostream *configStream;
55
56 class UniverseParamContext : public ParamContext
57 {
58 public:
59 UniverseParamContext(const string &is)
60 : ParamContext(is, OutputInitPhase) {}
61 void checkParams();
62 };
63
64 UniverseParamContext universe("Universe");
65
66 Param<Tick> universe_freq(&universe, "frequency", "tick frequency",
67 200000000);
68
69 Param<string> universe_output_dir(&universe, "output_dir",
70 "directory to output data to",
71 ".");
72 Param<string> universe_output_file(&universe, "output_file",
73 "file to dump simulator output to",
74 "cout");
75 Param<string> universe_config_output_file(&universe, "config_output_file",
76 "file to dump simulator config to",
77 "m5config.out");
78
79 void
80 UniverseParamContext::checkParams()
81 {
82 ticksPerSecond = universe_freq;
83 double freq = double(ticksPerSecond);
84 __ticksPerMS = freq / 1.0e3;
85 __ticksPerUS = freq / 1.0e6;
86 __ticksPerNS = freq / 1.0e9;
87 __ticksPerPS = freq / 1.0e12;
88
89 if (universe_output_dir.isValid()) {
90 outputDirectory = universe_output_dir;
91
92 // guarantee that directory ends with a '/'
93 if (outputDirectory[outputDirectory.size() - 1] != '/')
94 outputDirectory += "/";
95
96 if (mkdir(outputDirectory.c_str(), 0777) < 0) {
97 if (errno != EEXIST) {
98 panic("%s\ncould not make output directory: %s\n",
99 strerror(errno), outputDirectory);
100 }
101 }
102 }
103
104 outputStream = makeOutputStream(universe_output_file);
105 configStream = universe_config_output_file.isValid()
106 ? makeOutputStream(universe_config_output_file)
107 : outputStream;
108 }
109
110
111 std::ostream *
112 makeOutputStream(std::string &name)
113 {
114 if (name == "cerr" || name == "stderr")
115 return &std::cerr;
116
117 if (name == "cout" || name == "stdout")
118 return &std::cout;
119
120 string path = (name[0] != '/') ? outputDirectory + name : name;
121
122 // have to dynamically allocate a stream since we're going to
123 // return it... though the caller can't easily free it since it
124 // may be cerr or cout. need GC!
125 ofstream *s = new ofstream(path.c_str(), ios::trunc);
126
127 if (!s->is_open())
128 fatal("Cannot open file %s", path);
129
130 return s;
131 }
132
133
134 void
135 closeOutputStream(std::ostream *os)
136 {
137 // can't close cerr or cout
138 if (os == &std::cerr || os == &std::cout)
139 return;
140
141 // can only close ofstreams, not generic ostreams, so try to
142 // downcast and close only if the downcast succeeds
143 std::ofstream *ofs = dynamic_cast<std::ofstream *>(os);
144 if (ofs)
145 ofs->close();
146 }
147
148
149