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