Reenable functioning copies.
[gem5.git] / sim / universe.cc
1 /*
2 * Copyright (c) 2003 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
51 string outputDirectory;
52 ostream *outputStream;
53
54 class UniverseParamContext : public ParamContext
55 {
56 private:
57 ofstream outputFile;
58
59 public:
60 UniverseParamContext(const string &is) : ParamContext(is) {}
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 Param<string> universe_output_file(&universe, "output_file",
72 "file to dump simulator output to");
73
74 void
75 UniverseParamContext::checkParams()
76 {
77 ticksPerSecond = universe_freq;
78 double freq = double(ticksPerSecond);
79 __ticksPerMS = freq / 1.0e3;
80 __ticksPerUS = freq / 1.0e6;
81 __ticksPerNS = freq / 1.0e9;
82
83 if (universe_output_dir.isValid()) {
84 outputDirectory = universe_output_dir;
85
86 // guarantee that directory ends with a '/'
87 if (outputDirectory[outputDirectory.size() - 1] != '/')
88 outputDirectory += "/";
89
90 if (mkdir(outputDirectory.c_str(), 0777) < 0) {
91 if (errno != EEXIST) {
92 panic("%s\ncould not make output directory: %s\n",
93 strerror(errno), outputDirectory);
94 }
95 }
96 }
97
98 string filename;
99 if (universe_output_file.isValid()) {
100 string f = universe_output_file;
101 if (f != "stdout" && f != "cout" && f != "stderr" && f != "cerr")
102 filename = outputDirectory + f;
103 else
104 filename = f;
105 } else {
106 if (outputDirectory.empty())
107 filename = "stdout";
108 else
109 filename = outputDirectory + "output.txt";
110 }
111
112 if (filename == "stdout" || filename == "cout")
113 outputStream = &cout;
114 else if (filename == "stderr" || filename == "cerr")
115 outputStream = &cerr;
116 else {
117 outputFile.open(filename.c_str(), ios::trunc);
118 outputStream = &outputFile;
119 }
120 }