summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Emmons <chris.emmons@arm.com>2011-12-01 00:15:25 -0800
committerChris Emmons <chris.emmons@arm.com>2011-12-01 00:15:25 -0800
commit5bde1d359f0a0ce1d5ed46c3a9bb0ba33882f7b6 (patch)
tree348fc397dc9bb90a8233ce4f38bd0edd2dbbbcdd
parent5d50ee420d78114e90ef4eb1207838d5eb153789 (diff)
downloadgem5-5bde1d359f0a0ce1d5ed46c3a9bb0ba33882f7b6.tar.xz
Output: Add hierarchical output support and cleanup existing codebase.
--HG-- extra : rebase_source : 3301137733cdf5fdb471d56ef7990e7a3a865442
-rw-r--r--src/base/output.cc140
-rw-r--r--src/base/output.hh121
-rw-r--r--src/base/stats/text.cc6
-rw-r--r--src/cpu/base.cc6
-rw-r--r--src/dev/terminal.cc3
5 files changed, 255 insertions, 21 deletions
diff --git a/src/base/output.cc b/src/base/output.cc
index 020247152..1c749e5bf 100644
--- a/src/base/output.cc
+++ b/src/base/output.cc
@@ -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 @@ using namespace std;
OutputDirectory simout;
/**
- *
+ * @file This file manages creating / deleting output files for the simulator.
*/
OutputDirectory::OutputDirectory()
{}
@@ -73,26 +76,54 @@ OutputDirectory::checkForStdio(const string &name) const
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 @@ OutputDirectory::setDirectory(const string &d)
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 @@ OutputDirectory::directory() const
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 @@ OutputDirectory::create(const string &name, bool binary)
}
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 @@ OutputDirectory::isFile(const std::ostream *os)
{
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 --git a/src/base/output.hh b/src/base/output.hh
index 38c63714c..b86e68856 100644
--- a/src/base/output.hh
+++ b/src/base/output.hh
@@ -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;
+ /**
+ * Creates a file in this directory (optionally compressed).
+ *
+ * Will open a file as a compressed stream if filename ends in .gz.
+ *
+ * @param name name of file to create (without this directory's name
+ * leading it)
+ * @param binary true to create a binary file; false otherwise
+ * @return stream to the opened file
+ */
std::ostream *create(const std::string &name, bool binary = false);
- std::ostream *find(const std::string &name);
+ /**
+ * Closes a file stream.
+ *
+ * Stream must have been opened through this interface, or sim will fail.
+ *
+ * @param openStream open stream to close
+ */
+ void close(std::ostream *openStream);
+
+ /**
+ * Finds stream associated with a file.
+ * @param name of file
+ * @return stream to specified file or NULL if file does not exist
+ */
+ std::ostream *find(const std::string &name) const;
+
+ /**
+ * Returns true if stream is open and not standard output or error.
+ * @param os output stream to evaluate
+ * @return true if os is non-NULL and not cout or cerr
+ */
static bool isFile(const std::ostream *os);
- static inline bool isFile(const std::ostream &os) { return isFile(&os); }
+
+ /**
+ * Determines whether a file name corresponds to a file in this directory.
+ * @param name name of file to evaluate
+ * @return true iff file has been opened in this directory or exists on the
+ * file system within this directory
+ */
+ bool isFile(const std::string &name) const;
+
+ /**
+ * Returns true if stream is open and not standard output or error.
+ * @param os output stream to evaluate
+ * @return true if os is non-NULL and not cout or cerr
+ */
+ static inline bool isFile(const std::ostream &os) {
+ return isFile(&os);
+ }
+
+ /**
+ * Creates a subdirectory within this directory.
+ * @param name name of subdirectory
+ * @return the new subdirectory's name suffixed with a path separator
+ */
+ std::string createSubdirectory(const std::string &name) const;
+
+ /**
+ * Removes a specified file or subdirectory.
+ *
+ * Will cause sim to fail for most errors. However, it will only warn the
+ * user if a directory could not be removed. This is in place to
+ * accommodate slow file systems where file deletions within a subdirectory
+ * may not be recognized quickly enough thereby causing the subsequent call
+ * to remove the directory to fail (seemingly unempty directory).
+ *
+ * @param name name of file or subdirectory to remove; name should not
+ * be prepended with the name of this directory object
+ * @param recursive set to true to attempt to recursively delete a
+ * subdirectory and its contents
+ */
+ void remove(const std::string &name, bool recursive=false);
};
extern OutputDirectory simout;
diff --git a/src/base/stats/text.cc b/src/base/stats/text.cc
index f8471f1a1..683ba7fe4 100644
--- a/src/base/stats/text.cc
+++ b/src/base/stats/text.cc
@@ -674,7 +674,11 @@ initText(const string &filename, bool desc)
static bool connected = false;
if (!connected) {
- text.open(*simout.find(filename));
+ ostream *os = simout.find(filename);
+ if (!os)
+ os = simout.create(filename);
+
+ text.open(*os);
text.descriptions = desc;
connected = true;
}
diff --git a/src/cpu/base.cc b/src/cpu/base.cc
index 76c7c964b..c37f45856 100644
--- a/src/cpu/base.cc
+++ b/src/cpu/base.cc
@@ -190,7 +190,11 @@ BaseCPU::BaseCPU(Params *p)
functionTracingEnabled = false;
if (p->function_trace) {
- functionTraceStream = simout.find(csprintf("ftrace.%s", name()));
+ const string fname = csprintf("ftrace.%s", name());
+ functionTraceStream = simout.find(fname);
+ if (!functionTraceStream)
+ functionTraceStream = simout.create(fname);
+
currentFunctionStart = currentFunctionEnd = 0;
functionEntryTick = p->function_trace_start;
diff --git a/src/dev/terminal.cc b/src/dev/terminal.cc
index bae4c9194..74d5ddde7 100644
--- a/src/dev/terminal.cc
+++ b/src/dev/terminal.cc
@@ -102,6 +102,9 @@ Terminal::Terminal(const Params *p)
{
if (p->output) {
outfile = simout.find(p->name);
+ if (!outfile)
+ outfile = simout.create(p->name);
+
outfile->setf(ios::unitbuf);
}