gem5  v21.1.0.2
output.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 ARM Limited
3  * Copyright (c) 2020 Barkhausen Institut
4  * All rights reserved
5  *
6  * The license below extends only to copyright in the software and shall
7  * not be construed as granting a license to any other intellectual
8  * property including but not limited to intellectual property relating
9  * to a hardware implementation of the functionality of the software
10  * licensed hereunder. You may use the software subject to the license
11  * terms below provided that you ensure that this notice is replicated
12  * unmodified and in its entirety in all distributions of the software,
13  * modified or unmodified, in source code or in binary form.
14  *
15  * Copyright (c) 2013 Andreas Sandberg
16  * Copyright (c) 2005 The Regents of The University of Michigan
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions are
21  * met: redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer;
23  * redistributions in binary form must reproduce the above copyright
24  * notice, this list of conditions and the following disclaimer in the
25  * documentation and/or other materials provided with the distribution;
26  * neither the name of the copyright holders nor the names of its
27  * contributors may be used to endorse or promote products derived from
28  * this software without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  */
42 
43 #include "base/output.hh"
44 
45 #include <dirent.h>
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 #include <unistd.h>
49 #include <zfstream.h>
50 
51 #include <cassert>
52 #include <cerrno>
53 #include <climits>
54 #include <cstdlib>
55 #include <fstream>
56 
57 #include "base/logging.hh"
58 
59 namespace gem5
60 {
61 
63 
64 
65 OutputStream::OutputStream(const std::string &name, std::ostream *stream)
66  : _name(name), _stream(stream)
67 {
68 }
69 
71 {
72 }
73 
74 void
76 {
77 }
78 
79 template<class StreamType>
81  const std::string &name,
82  std::ios_base::openmode mode,
83  bool recreateable)
85  _mode(mode), _recreateable(recreateable),
86  _fstream(static_cast<stream_type_t *>(_stream))
87 {
88  std::string resolved_path = dir.resolve(_name);
89 
90  _fstream->open(resolved_path.c_str(), _mode);
91 
92  panic_if(!_fstream->is_open(), "Failed to open \"%s\"\n", resolved_path);
93 }
94 
95 template<class StreamType>
97 {
98  if (_fstream->is_open())
99  _fstream->close();
100 }
101 
102 template<class StreamType>
103 void
105 {
106  if (_recreateable) {
107  _fstream->close();
108  _fstream->open(dir.resolve(_name).c_str(), _mode);
109  }
110 }
111 
112 OutputStream OutputDirectory::stdout("stdout", &std::cout);
113 OutputStream OutputDirectory::stderr("stderr", &std::cerr);
114 
119 {}
120 
122 {
124 }
125 
127 {
128  for (auto& f: files) {
129  if (f.second)
130  delete f.second;
131  }
132 }
133 
134 OutputStream *
136 {
137  if (name == "cerr" || name == "stderr")
138  return &stderr;
139 
140  if (name == "cout" || name == "stdout")
141  return &stdout;
142 
143  return NULL;
144 }
145 
146 void
148 {
149  if (file == &stdout || file == &stderr) {
150  file->stream()->flush();
151  return;
152  }
153 
154  auto i = files.find(file->name());
155  if (i == files.end())
156  fatal("Attempted to close an unregistred file stream");
157 
158  files.erase(i);
159 
160  delete file;
161 }
162 
163 void
164 OutputDirectory::setDirectory(const std::string &d)
165 {
166  const std::string old_dir(dir);
167 
168  dir = d;
169 
170  // guarantee that directory ends with a path separator
171  if (dir[dir.size() - 1] != PATH_SEPARATOR)
172  dir += PATH_SEPARATOR;
173 
174  // Try to create the directory. If it already exists, that's ok;
175  // otherwise, fail if we couldn't create it.
176  if ((mkdir(dir.c_str(), 0755) != 0) && (errno != EEXIST))
177  fatal("Failed to create new output subdirectory '%s'\n", dir);
178 
179  // Check if we need to recreate anything
180  if (!old_dir.empty()) {
181  // Recreate output files
182  for (file_map_t::iterator i = files.begin(); i != files.end(); ++i) {
183  i->second->relocate(*this);
184  }
185 
186  // Relocate sub-directories
187  for (dir_map_t::iterator i = dirs.begin(); i != dirs.end(); ++i) {
188  i->second->setDirectory(dir + PATH_SEPARATOR + i->first);
189  }
190  }
191 
192 }
193 
194 const std::string &
196 {
197  if (dir.empty())
198  panic("Output directory not set!");
199 
200  return dir;
201 }
202 
203 std::string
204 OutputDirectory::resolve(const std::string &name) const
205 {
206  return !isAbsolute(name) ? dir + name : name;
207 }
208 
209 OutputStream *
210 OutputDirectory::create(const std::string &name, bool binary, bool no_gz)
211 {
213  if (file)
214  return file;
215 
216  const std::ios_base::openmode mode(
217  std::ios::trunc | (binary ? std::ios::binary : (std::ios::openmode)0));
218  const bool recreateable(!isAbsolute(name));
219 
220  return open(name, mode, recreateable, no_gz);
221 }
222 
223 OutputStream *
224 OutputDirectory::open(const std::string &name,
225  std::ios_base::openmode mode,
226  bool recreateable,
227  bool no_gz)
228 {
229  OutputStream *os;
230 
231  if (!no_gz && name.find(".gz", name.length() - 3) < name.length()) {
232  // Although we are creating an output stream, we still need to pass the
233  // correct mode for gzofstream as this used directly to set the file
234  // mode.
235  mode |= std::ios::out;
236  os = new OutputFile<gzofstream>(*this, name, mode, recreateable);
237  } else {
238  os = new OutputFile<std::ofstream>(*this, name, mode, recreateable);
239  }
240 
241  files[name] = os;
242 
243  return os;
244 }
245 
246 OutputStream *
247 OutputDirectory::find(const std::string &name) const
248 {
250  if (file)
251  return file;
252 
253  auto i = files.find(name);
254  if (i != files.end())
255  return (*i).second;
256 
257  return NULL;
258 }
259 
260 
261 OutputStream *
262 OutputDirectory::findOrCreate(const std::string &name, bool binary)
263 {
265  if (os)
266  return os;
267  else
268  return create(name, binary);
269 }
270 
271 bool
272 OutputDirectory::isFile(const std::string &name) const
273 {
274  // definitely a file if in our data structure
275  if (find(name) != NULL) return true;
276 
277  struct stat st_buf;
278  int st = stat(name.c_str(), &st_buf);
279  return (st == 0) && S_ISREG(st_buf.st_mode);
280 }
281 
284 {
285  const std::string new_dir = resolve(name);
286  if (new_dir.find(directory()) == std::string::npos)
287  fatal("Attempting to create subdirectory not in m5 output dir\n");
288 
289  OutputDirectory *dir(new OutputDirectory(new_dir));
290  dirs[name] = dir;
291 
292  return dir;
293 }
294 
295 void
296 OutputDirectory::remove(const std::string &name, bool recursive)
297 {
298  const std::string fname = resolve(name);
299 
300  if (fname.find(directory()) == std::string::npos)
301  fatal("Attempting to remove file/dir not in output dir\n");
302 
303  if (isFile(fname)) {
304  // close and release file if we have it open
305  auto i = files.find(fname);
306  if (i != files.end()) {
307  delete i->second;
308  files.erase(i);
309  }
310 
311  if (::remove(fname.c_str()) != 0)
312  fatal("Could not erase file '%s'\n", fname);
313  } else {
314  // assume 'name' is a directory
315  if (recursive) {
316  DIR *subdir = opendir(fname.c_str());
317 
318  // silently ignore removal request for non-existent directory
319  if ((!subdir) && (errno == ENOENT))
320  return;
321 
322  // fail on other errors
323  if (!subdir) {
324  perror("opendir");
325  fatal("Error opening directory for recursive removal '%s'\n",
326  fname);
327  }
328 
329  struct dirent *de = readdir(subdir);
330  while (de != NULL) {
331  // ignore files starting with a '.'; user must delete those
332  // manually if they really want to
333  if (de->d_name[0] != '.')
334  remove(name + PATH_SEPARATOR + de->d_name, recursive);
335 
336  de = readdir(subdir);
337  }
338 
339  closedir(subdir);
340  }
341 
342  // try to force recognition that we deleted the files in the directory
343  sync();
344 
345  if (::remove(fname.c_str()) != 0) {
346  perror("Warning! 'remove' failed. Could not erase directory.");
347  }
348  }
349 }
350 
351 } // namespace gem5
fatal
#define fatal(...)
This implements a cprintf based fatal() function.
Definition: logging.hh:189
gem5::OutputStream::_name
const std::string _name
Name in output directory.
Definition: output.hh:89
gem5::OutputDirectory::PATH_SEPARATOR
static const char PATH_SEPARATOR
System-specific path separator character.
Definition: output.hh:156
gem5::OutputDirectory::~OutputDirectory
~OutputDirectory()
Destructor.
Definition: output.cc:126
gem5::OutputDirectory::isAbsolute
static bool isAbsolute(const std::string &name)
Test if a path is absolute.
Definition: output.hh:277
gem5::OutputStream::~OutputStream
virtual ~OutputStream()
Definition: output.cc:70
gem5::OutputDirectory::checkForStdio
static OutputStream * checkForStdio(const std::string &name)
Determines whether given file name corresponds to standard output streams.
Definition: output.cc:135
gem5::OutputDirectory
Interface for creating files in a gem5 output directory.
Definition: output.hh:137
gem5::ArmISA::f
Bitfield< 6 > f
Definition: misc_types.hh:67
gem5::OutputFile::relocate
void relocate(const OutputDirectory &dir) override
Re-create the file in a new location if it is relocatable.
Definition: output.cc:104
gem5::simout
OutputDirectory simout
Definition: output.cc:62
gem5::OutputDirectory::stderr
static OutputStream stderr
Definition: output.hh:159
gem5::OutputDirectory::create
OutputStream * create(const std::string &name, bool binary=false, bool no_gz=false)
Creates a file in this directory (optionally compressed).
Definition: output.cc:210
gem5::OutputDirectory::stdout
static OutputStream stdout
Definition: output.hh:158
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
gem5::OutputDirectory::dir
std::string dir
Name of this directory.
Definition: output.hh:153
gem5::OutputDirectory::createSubdirectory
OutputDirectory * createSubdirectory(const std::string &name)
Creates a subdirectory within this directory.
Definition: output.cc:283
gem5::OutputDirectory::findOrCreate
OutputStream * findOrCreate(const std::string &name, bool binary=false)
Definition: output.cc:262
output.hh
gem5::OutputStream::OutputStream
OutputStream(const std::string &name, std::ostream *stream)
Wrap an existing stream.
Definition: output.cc:65
gem5::X86ISA::de
Bitfield< 3 > de
Definition: misc.hh:641
gem5::OutputStream::stream
std::ostream * stream() const
Get the output underlying output stream.
Definition: output.hh:62
gem5::OutputDirectory::close
void close(OutputStream *file)
Closes an output file and free the corresponding OutputFile.
Definition: output.cc:147
gem5::OutputFile::OutputFile
OutputFile(const OutputDirectory &dir, const std::string &name, std::ios_base::openmode mode, bool recreateable)
Definition: output.cc:80
gem5::ArmISA::d
Bitfield< 9 > d
Definition: misc_types.hh:63
gem5::OutputDirectory::setDirectory
void setDirectory(const std::string &dir)
Sets name of this directory.
Definition: output.cc:164
gem5::OutputFile::~OutputFile
virtual ~OutputFile()
Definition: output.cc:96
gem5::OutputDirectory::resolve
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
Definition: output.cc:204
gem5::OutputDirectory::open
OutputStream * open(const std::string &name, std::ios_base::openmode mode, bool recreateable=true, bool no_gz=false)
Open a file in this directory (optionally compressed).
Definition: output.cc:224
gem5::OutputDirectory::OutputDirectory
OutputDirectory()
Constructor.
Definition: output.cc:118
gem5::OutputDirectory::directory
const std::string & directory() const
Gets name of this directory.
Definition: output.cc:195
gem5::OutputFile
Definition: output.hh:96
gem5::OutputStream::name
const std::string & name() const
Get the file name in the output directory.
Definition: output.hh:73
name
const std::string & name()
Definition: trace.cc:49
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:203
gem5::OutputDirectory::files
file_map_t files
Open file streams within this directory.
Definition: output.hh:147
gem5::X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:809
gem5::OutputDirectory::find
OutputStream * find(const std::string &name) const
Finds stream associated with an open file or stdout/stderr.
Definition: output.cc:247
gem5::OutputDirectory::isFile
bool isFile(const std::string &name) const
Determines whether a file name corresponds to a file in this directory.
Definition: output.cc:272
logging.hh
gem5::OutputFile::stream_type_t
StreamType stream_type_t
Definition: output.hh:100
gem5::OutputDirectory::remove
void remove(const std::string &name, bool recursive=false)
Removes a specified file or subdirectory.
Definition: output.cc:296
gem5::OutputStream::relocate
virtual void relocate(const OutputDirectory &dir)
Re-create the in a new location if recreateable.
Definition: output.cc:75
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::OutputFile::_mode
const std::ios_base::openmode _mode
File mode when opened.
Definition: output.hh:127
gem5::ArmISA::st
Bitfield< 31, 28 > st
Definition: misc_types.hh:155
gem5::OutputStream
Definition: output.hh:56
gem5::OutputDirectory::dirs
dir_map_t dirs
Output sub-directories.
Definition: output.hh:150
gem5::OutputFile::_fstream
stream_type_t *const _fstream
Pointer to the file stream.
Definition: output.hh:133
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:177
gem5::ArmISA::mode
Bitfield< 4, 0 > mode
Definition: misc_types.hh:73

Generated on Tue Sep 21 2021 12:24:57 for gem5 by doxygen 1.8.17