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>
39 #include "base/misc.hh"
40 #include "sim/universe.hh"
41 #include "sim/host.hh"
42 #include "sim/param.hh"
53 string outputDirectory
;
54 ostream
*outputStream
;
55 ostream
*configStream
;
57 class UniverseParamContext
: public ParamContext
60 UniverseParamContext(const string
&is
)
61 : ParamContext(is
, OutputInitPhase
) {}
65 UniverseParamContext
universe("Universe");
67 Param
<Tick
> universe_freq(&universe
, "frequency", "tick frequency",
70 Param
<string
> universe_output_dir(&universe
, "output_dir",
71 "directory to output data to",
73 Param
<string
> universe_output_file(&universe
, "output_file",
74 "file to dump simulator output to",
76 Param
<string
> universe_config_output_file(&universe
, "config_output_file",
77 "file to dump simulator config to",
81 UniverseParamContext::checkParams()
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
;
90 if (universe_output_dir
.isValid()) {
91 outputDirectory
= universe_output_dir
;
93 // guarantee that directory ends with a '/'
94 if (outputDirectory
[outputDirectory
.size() - 1] != '/')
95 outputDirectory
+= "/";
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
);
105 outputStream
= makeOutputStream(universe_output_file
);
106 configStream
= universe_config_output_file
.isValid()
107 ? makeOutputStream(universe_config_output_file
)
113 makeOutputStream(std::string
&name
)
115 if (name
== "cerr" || name
== "stderr")
118 if (name
== "cout" || name
== "stdout")
121 string path
= (name
[0] != '/') ? outputDirectory
+ name
: name
;
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
);
129 fatal("Cannot open file %s", path
);
136 closeOutputStream(std::ostream
*os
)
138 // can't close cerr or cout
139 if (os
== &std::cerr
|| os
== &std::cout
)
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
);