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