changeset 3301137733cd in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=3301137733cd
description:
        Output: Add hierarchical output support and cleanup existing codebase.

diffstat:

 src/base/output.cc     |  140 +++++++++++++++++++++++++++++++++++++++++++-----
 src/base/output.hh     |  121 +++++++++++++++++++++++++++++++++++++++++-
 src/base/stats/text.cc |    6 +-
 src/cpu/base.cc        |    6 +-
 src/dev/terminal.cc    |    3 +
 5 files changed, 255 insertions(+), 21 deletions(-)

diffs (truncated from 424 to 300 lines):

diff -r e56d1551d42d -r 3301137733cd src/base/output.cc
--- a/src/base/output.cc        Thu Dec 01 00:15:25 2011 -0800
+++ b/src/base/output.cc        Thu Dec 01 00:15:25 2011 -0800
@@ -26,11 +26,14 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * Authors: Nathan Binkert
+ *          Chris Emmons
  */
 
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <dirent.h>
 
+#include <cassert>
 #include <cerrno>
 #include <climits>
 #include <cstdlib>
@@ -46,7 +49,7 @@
 OutputDirectory simout;
 
 /**
- *
+ * @file This file manages creating / deleting output files for the simulator.
  */
 OutputDirectory::OutputDirectory()
 {}
@@ -73,26 +76,54 @@
 
 ostream *
 OutputDirectory::openFile(const string &filename,
-                          ios_base::openmode mode) const
+                          ios_base::openmode mode)
 {
     if (filename.find(".gz", filename.length()-3) < filename.length()) {
         ogzstream *file = new ogzstream(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 {
+            ogzstream *gfs = dynamic_cast<ogzstream*>(i->second);
+            if (gfs) {
+                gfs->close();
+                delete i->second;
+                break;
+            }
+        }
+    }
+
+    if (i == files.end())
+        fatal("Attempted to close an unregistred file stream");
+
+    files.erase(i);
+}
+
+void
 OutputDirectory::setDirectory(const string &d)
 {
     if (!dir.empty())
@@ -100,9 +131,9 @@
 
     dir = d;
 
-    // guarantee that directory ends with a '/'
-    if (dir[dir.size() - 1] != '/')
-        dir += "/";
+    // guarantee that directory ends with a path separator
+    if (dir[dir.size() - 1] != PATH_SEPARATOR)
+        dir += PATH_SEPARATOR;
 }
 
 const string &
@@ -117,7 +148,7 @@
 inline string
 OutputDirectory::resolve(const string &name) const
 {
-    return (name[0] != '/') ? dir + name : name;
+    return (name[0] != PATH_SEPARATOR) ? dir + name : name;
 }
 
 ostream *
@@ -136,20 +167,18 @@
 }
 
 ostream *
-OutputDirectory::find(const string &name)
+OutputDirectory::find(const string &name) const
 {
     ostream *file = checkForStdio(name);
     if (file)
         return file;
 
-    string filename = resolve(name);
-    map_t::iterator i = files.find(filename);
+    const string filename = resolve(name);
+    map_t::const_iterator i = files.find(filename);
     if (i != files.end())
         return (*i).second;
 
-    file = openFile(filename);
-    files[filename] = file;
-    return file;
+    return NULL;
 }
 
 bool
@@ -157,3 +186,82 @@
 {
     return os && os != &cerr && os != &cout;
 }
+
+bool
+OutputDirectory::isFile(const string &name) const
+{
+    // definitely a file if in our data structure
+    if (find(name) != NULL) return true;
+
+    struct stat st_buf;
+    int st = stat(name.c_str(), &st_buf);
+    return (st == 0) && S_ISREG(st_buf.st_mode);
+}
+
+string
+OutputDirectory::createSubdirectory(const string &name) const
+{
+    const string new_dir = resolve(name);
+    if (new_dir.find(directory()) == string::npos)
+        fatal("Attempting to create subdirectory not in m5 output dir\n");
+
+    // if it already exists, that's ok; otherwise, fail if we couldn't create
+    if ((mkdir(new_dir.c_str(), 0755) != 0) && (errno != EEXIST))
+        fatal("Failed to create new output subdirectory '%s'\n", new_dir);
+
+    return name + PATH_SEPARATOR;
+}
+
+void
+OutputDirectory::remove(const string &name, bool recursive)
+{
+    const string fname = resolve(name);
+
+    if (fname.find(directory()) == string::npos)
+        fatal("Attempting to remove file/dir not in output dir\n");
+
+    if (isFile(fname)) {
+        // close and release file if we have it open
+        map_t::iterator itr = files.find(fname);
+        if (itr != files.end()) {
+            delete itr->second;
+            files.erase(itr);
+        }
+
+        if (::remove(fname.c_str()) != 0)
+            fatal("Could not erase file '%s'\n", fname);
+    } else {
+        // assume 'name' is a directory
+        if (recursive) {
+            DIR *dir = opendir(fname.c_str());
+
+            // silently ignore removal request for non-existent directory
+            if ((!dir) && (errno == ENOENT))
+                return;
+
+            // fail on other errors
+            if (!dir) {
+                perror("opendir");
+                fatal("Error opening directory for recursive removal '%s'\n",
+                    fname);
+            }
+
+            struct dirent *de = readdir(dir);
+            while (de != NULL) {
+                // ignore files starting with a '.'; user must delete those
+                //   manually if they really want to
+                if (de->d_name[0] != '.')
+                    remove(name + PATH_SEPARATOR + de->d_name, recursive);
+
+                de = readdir(dir);
+            }
+        }
+
+        // try to force recognition that we deleted the files in the directory
+        sync();
+
+        if (::remove(fname.c_str()) != 0) {
+            perror("Warning!  'remove' failed.  Could not erase directory.");
+        }
+    }
+}
diff -r e56d1551d42d -r 3301137733cd src/base/output.hh
--- a/src/base/output.hh        Thu Dec 01 00:15:25 2011 -0800
+++ b/src/base/output.hh        Thu Dec 01 00:15:25 2011 -0800
@@ -26,6 +26,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * Authors: Nathan Binkert
+ *          Chris Emmons
  */
 
 #ifndef __BASE_OUTPUT_HH__
@@ -35,33 +36,147 @@
 #include <map>
 #include <string>
 
+/** Interface for creating files in a gem5 output directory. */
 class OutputDirectory
 {
   private:
+    /** File names and associated stream handles */
     typedef std::map<std::string, std::ostream *> map_t;
 
+    /** Open file streams within this directory */
     map_t files;
+
+    /** Name of this directory */
     std::string dir;
 
+    /** System-specific path separator character */
+    static const char PATH_SEPARATOR = '/';
+
+    /**
+     * Returns relative file names prepended with name of this directory.
+     * Returns absolute file names unaltered.
+     *
+     * @param name file name to prepend with directory name
+     * @return file name prepended with base directory name or unaltered
+     *          absolute file name
+     */
     std::string resolve(const std::string &name) const;
 
   protected:
+    /**
+     * Determines whether given file name corresponds to standard output
+     * streams.
+     *
+     * @param name name of file to check
+     * @return output stream for standard output or error stream if name
+     *         corresponds to one or the other; NULL otherwise
+     */
     std::ostream *checkForStdio(const std::string &name) const;
+
+    /** Opens a file (optionally compressed).
+     *
+     * Will open a file as a compressed stream if filename ends in .gz.
+     *
+     * @param filename file to open
+     * @param mode attributes to open file with
+     * @return stream pointer to opened file; will cause sim fail on error
+     */
     std::ostream *openFile(const std::string &filename,
-                        std::ios_base::openmode mode = std::ios::trunc) const;
+                        std::ios_base::openmode mode = std::ios::trunc);
 
   public:
+    /** Constructor. */
     OutputDirectory();
+
+    /** Destructor. */
     ~OutputDirectory();
 
+    /**
+     * Sets name of this directory.
+     * @param dir name of this directory
+     */
     void setDirectory(const std::string &dir);
+
+    /**
+     * Gets name of this directory.
+     * @return name of this directory
+     */
     const std::string &directory() const;
 
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to