2 * Copyright (c) 2002-2004 The Regents of The University of Michigan
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.
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.
29 #include <sys/types.h>
38 #include "base/misc.hh"
39 #include "sim/universe.hh"
40 #include "sim/host.hh"
41 #include "sim/param.hh"
52 string outputDirectory
;
53 ostream
*outputStream
;
54 ostream
*configStream
;
56 class UniverseParamContext
: public ParamContext
59 UniverseParamContext(const string
&is
)
60 : ParamContext(is
, OutputInitPhase
) {}
64 UniverseParamContext
universe("Universe");
66 Param
<Tick
> universe_freq(&universe
, "frequency", "tick frequency",
69 Param
<string
> universe_output_dir(&universe
, "output_dir",
70 "directory to output data to",
72 Param
<string
> universe_output_file(&universe
, "output_file",
73 "file to dump simulator output to",
75 Param
<string
> universe_config_output_file(&universe
, "config_output_file",
76 "file to dump simulator config to",
80 UniverseParamContext::checkParams()
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
;
89 if (universe_output_dir
.isValid()) {
90 outputDirectory
= universe_output_dir
;
92 // guarantee that directory ends with a '/'
93 if (outputDirectory
[outputDirectory
.size() - 1] != '/')
94 outputDirectory
+= "/";
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
);
104 outputStream
= makeOutputStream(universe_output_file
);
105 configStream
= universe_config_output_file
.isValid()
106 ? makeOutputStream(universe_config_output_file
)
112 makeOutputStream(std::string
&name
)
114 if (name
== "cerr" || name
== "stderr")
117 if (name
== "cout" || name
== "stdout")
120 string path
= (name
[0] != '/') ? outputDirectory
+ name
: name
;
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
);
128 fatal("Cannot open file %s", path
);
135 closeOutputStream(std::ostream
*os
)
137 // can't close cerr or cout
138 if (os
== &std::cerr
|| os
== &std::cout
)
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
);