changeset b0b976a1ceda in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=b0b976a1ceda
description:
        base: Add support for changing output directories

        This changeset adds support for changing the simulator output
        directory. This can be useful when the simulation goes through several
        stages (e.g., a warming phase, a simulation phase, and a verification
        phase) since it allows the output from each stage to be located in a
        different directory. Relocation is done by calling core.setOutputDir()
        from Python or simout.setOutputDirectory() from C++.

        This change affects several parts of the design of the gem5's output
        subsystem. First, files returned by an OutputDirectory instance (e.g.,
        simout) are of the type OutputStream instead of a std::ostream. This
        allows us to do some more book keeping and control re-opening of files
        when the output directory is changed. Second, new subdirectories are
        OutputDirectory instances, which should be used to create files in
        that sub-directory.

        Signed-off-by: Andreas Sandberg <[email protected]>
        [[email protected]: Rebased patches onto a newer gem5 version]
        Signed-off-by: Sascha Bischoff <[email protected]>
        Signed-off-by: Andreas Sandberg <[email protected]>

diffstat:

 src/arch/arm/linux/system.cc      |    6 +-
 src/arch/arm/linux/system.hh      |    2 +-
 src/base/output.cc                |  231 +++++++++++++++++++++++++------------
 src/base/output.hh                |  199 ++++++++++++++++++++++++++------
 src/base/stats/text.cc            |    6 +-
 src/base/vnc/vncinput.cc          |    7 +-
 src/base/vnc/vncinput.hh          |    4 +-
 src/cpu/base.cc                   |    4 +-
 src/cpu/o3/thread_state.hh        |    6 +-
 src/cpu/simple/probes/simpoint.cc |    6 +-
 src/cpu/simple/probes/simpoint.hh |    3 +-
 src/cpu/simple_thread.cc          |    6 +-
 src/dev/arm/hdlcd.cc              |    4 +-
 src/dev/arm/hdlcd.hh              |    3 +-
 src/dev/arm/pl111.cc              |    7 +-
 src/dev/arm/pl111.hh              |    3 +-
 src/dev/net/etherdump.cc          |    2 +-
 src/dev/terminal.cc               |   14 +-
 src/dev/terminal.hh               |    3 +-
 src/python/swig/trace.i           |    4 +-
 src/sim/pseudo_inst.cc            |   11 +-
 21 files changed, 361 insertions(+), 170 deletions(-)

diffs (truncated from 1011 to 300 lines):

diff -r cd41493e8b7e -r b0b976a1ceda src/arch/arm/linux/system.cc
--- a/src/arch/arm/linux/system.cc      Mon Feb 29 19:13:15 2016 -0600
+++ b/src/arch/arm/linux/system.cc      Fri Nov 27 14:41:59 2015 +0000
@@ -304,15 +304,15 @@
     tc->getCpuPtr()->taskId(taskMap[pid]);
     tc->getCpuPtr()->setPid(pid);
 
-    std::ostream* taskFile = sys->taskFile;
+    OutputStream* taskFile = sys->taskFile;
 
     // Task file is read by cache occupancy plotting script or
     // Streamline conversion script.
-    ccprintf(*taskFile,
+    ccprintf(*(taskFile->stream()),
              "tick=%lld %d cpu_id=%d next_pid=%d next_tgid=%d next_task=%s\n",
              curTick(), taskMap[pid], tc->cpuId(), (int) pid, (int) tgid,
              next_task_str);
-    taskFile->flush();
+    taskFile->stream()->flush();
 
     // Dump and reset statistics
     Stats::schedStatEvent(true, true, curTick(), 0);
diff -r cd41493e8b7e -r b0b976a1ceda src/arch/arm/linux/system.hh
--- a/src/arch/arm/linux/system.hh      Mon Feb 29 19:13:15 2016 -0600
+++ b/src/arch/arm/linux/system.hh      Fri Nov 27 14:41:59 2015 +0000
@@ -82,7 +82,7 @@
 
     /** This is a file that is placed in the run directory that prints out
      * mappings between taskIds and OS process IDs */
-    std::ostream* taskFile;
+    OutputStream* taskFile;
 
     LinuxArmSystem(Params *p);
     ~LinuxArmSystem();
diff -r cd41493e8b7e -r b0b976a1ceda src/base/output.cc
--- a/src/base/output.cc        Mon Feb 29 19:13:15 2016 -0600
+++ b/src/base/output.cc        Fri Nov 27 14:41:59 2015 +0000
@@ -1,4 +1,17 @@
 /*
+ * Copyright (c) 2015 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Copyright (c) 2013 Andreas Sandberg
  * Copyright (c) 2005 The Regents of The University of Michigan
  * All rights reserved.
  *
@@ -27,6 +40,8 @@
  *
  * Authors: Nathan Binkert
  *          Chris Emmons
+ *          Andreas Sandberg
+ *          Sascha Bischoff
  */
 
 #include <sys/stat.h>
@@ -49,94 +64,127 @@
 
 OutputDirectory simout;
 
+
+OutputStream::OutputStream(const std::string &name, std::ostream *stream)
+    : _name(name), _stream(stream)
+{
+}
+
+OutputStream::~OutputStream()
+{
+}
+
+void
+OutputStream::relocate(const OutputDirectory &dir)
+{
+}
+
+template<class StreamType>
+OutputFile<StreamType>::OutputFile(const OutputDirectory &dir,
+                                   const std::string &name,
+                                   std::ios_base::openmode mode,
+                                   bool recreateable)
+  : OutputStream(name, new stream_type_t()),
+    _mode(mode), _recreateable(recreateable),
+    _fstream(static_cast<stream_type_t *>(_stream))
+{
+    _fstream->open(dir.resolve(_name).c_str(), _mode);
+
+    assert(_fstream->is_open());
+}
+
+template<class StreamType>
+OutputFile<StreamType>::~OutputFile()
+{
+    if (_fstream->is_open())
+        _fstream->close();
+}
+
+template<class StreamType>
+void
+OutputFile<StreamType>::relocate(const OutputDirectory &dir)
+{
+    if (_recreateable) {
+        _fstream->close();
+        _fstream->open(dir.resolve(_name).c_str(), _mode);
+    }
+}
+
+OutputStream OutputDirectory::stdout("stdout", &cout);
+OutputStream OutputDirectory::stderr("stderr", &cerr);
+
 /**
  * @file This file manages creating / deleting output files for the simulator.
  */
 OutputDirectory::OutputDirectory()
 {}
 
+OutputDirectory::OutputDirectory(const std::string &name)
+{
+    setDirectory(name);
+}
+
 OutputDirectory::~OutputDirectory()
 {
-    for (map_t::iterator i = files.begin(); i != files.end(); i++) {
-        if (i->second)
-            delete i->second;
+    for (auto& f: files) {
+        if (f.second)
+            delete f.second;
     }
 }
 
-std::ostream *
-OutputDirectory::checkForStdio(const string &name) const
+OutputStream *
+OutputDirectory::checkForStdio(const string &name)
 {
     if (name == "cerr" || name == "stderr")
-        return &cerr;
+        return &stderr;
 
     if (name == "cout" || name == "stdout")
-        return &cout;
+        return &stdout;
 
     return NULL;
 }
 
-ostream *
-OutputDirectory::openFile(const string &filename,
-                          ios_base::openmode mode, bool no_gz)
+void
+OutputDirectory::close(OutputStream *file)
 {
-    bool gz = !no_gz;
-    gz = gz && filename.find(".gz", filename.length()-3) < filename.length();
-    if (gz) {
-        gzofstream *file = new gzofstream(filename.c_str(), mode);
-        if (!file->is_open())
-            fatal("Cannot open file %s", filename);
-        assert(files.find(filename) == files.end());
-        files[filename] = file;
-        return file;
-    } else {
-        ofstream *file = new ofstream(filename.c_str(), mode);
-        if (!file->is_open())
-            fatal("Cannot open file %s", filename);
-        assert(files.find(filename) == files.end());
-        files[filename] = file;
-        return file;
-    }
-}
-
-void
-OutputDirectory::close(ostream *openStream) {
-    map_t::iterator i;
-    for (i = files.begin(); i != files.end(); i++) {
-        if (i->second != openStream)
-            continue;
-
-        ofstream *fs = dynamic_cast<ofstream*>(i->second);
-        if (fs) {
-            fs->close();
-            delete i->second;
-            break;
-        } else {
-            gzofstream *gfs = dynamic_cast<gzofstream*>(i->second);
-            if (gfs) {
-                gfs->close();
-                delete i->second;
-                break;
-            }
-        }
-    }
-
+    auto i = files.find(file->name());
     if (i == files.end())
         fatal("Attempted to close an unregistred file stream");
 
     files.erase(i);
+
+    delete file;
 }
 
 void
 OutputDirectory::setDirectory(const string &d)
 {
-    if (!dir.empty())
-        panic("Output directory already set!\n");
+    const string old_dir(dir);
 
     dir = d;
 
     // guarantee that directory ends with a path separator
     if (dir[dir.size() - 1] != PATH_SEPARATOR)
         dir += PATH_SEPARATOR;
+
+    // Try to create the directory. If it already exists, that's ok;
+    // otherwise, fail if we couldn't create it.
+    if ((mkdir(dir.c_str(), 0755) != 0) && (errno != EEXIST))
+        fatal("Failed to create new output subdirectory '%s'\n", dir);
+
+    // Check if we need to recreate anything
+    if (!old_dir.empty()) {
+        // Recreate output files
+        for (file_map_t::iterator i = files.begin(); i != files.end(); ++i) {
+            i->second->relocate(*this);
+        }
+
+        // Relocate sub-directories
+        for (dir_map_t::iterator i = dirs.begin(); i != dirs.end(); ++i) {
+            i->second->setDirectory(dir + PATH_SEPARATOR + i->first);
+        }
+    }
+
 }
 
 const string &
@@ -151,43 +199,69 @@
 string
 OutputDirectory::resolve(const string &name) const
 {
-    return (name[0] != PATH_SEPARATOR) ? dir + name : name;
+    return !isAbsolute(name) ? dir + name : name;
 }
 
-ostream *
+OutputStream *
 OutputDirectory::create(const string &name, bool binary, bool no_gz)
 {
-    ostream *file = checkForStdio(name);
+    OutputStream *file = checkForStdio(name);
     if (file)
         return file;
 
-    string filename = resolve(name);
-    ios_base::openmode mode =
-        ios::trunc | (binary ? ios::binary : (ios::openmode)0);
-    file = openFile(filename, mode, no_gz);
+    const ios_base::openmode mode(
+        ios::trunc | (binary ? ios::binary : (ios::openmode)0));
+    const bool recreateable(!isAbsolute(name));
 
-    return file;
+    return open(name, mode, recreateable, no_gz);
 }
 
-ostream *
+OutputStream *
+OutputDirectory::open(const std::string &name,
+                      ios_base::openmode mode,
+                      bool recreateable,
+                      bool no_gz)
+{
+    OutputStream *os;
+
+    if (!no_gz && name.find(".gz", name.length() - 3) < name.length()) {
+        // Although we are creating an output stream, we still need to pass the
+        // correct mode for gzofstream as this used directly to set the file
+        // mode.
+        mode |= std::ios::out;
+        os = new OutputFile<gzofstream>(*this, name, mode, recreateable);
+    } else {
+        os = new OutputFile<ofstream>(*this, name, mode, recreateable);
+    }
+
+    files[name] = os;
+
+    return os;
+}
+
+OutputStream *
 OutputDirectory::find(const string &name) const
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to