Improve python output support

fix some bugs

base/statistics.cc:
    -  Move the python dump stream stuff into the statistics package
    because it needs more control over the file when there
    are multiple dumps
    -  add a subname to the output
    -  make all of the stats a list instead of variable arguments to
    get around the 255 argument limit
    -  the description needs to be triple quoted
    -  avoid errors for formulas that don't have any root element
    set up.
base/statistics.hh:
    -  get rid of mode_python and just separate python dumping from
    regular stats dumping
    -  fix a huge bug that made a Formula use a VectorData instead
    of a FormulaData

--HG--
extra : convert_revision : 7303cff3ccdcc3d306ab17375219fc7fecac7e5e
This commit is contained in:
Nathan Binkert 2003-12-28 12:48:13 -05:00
parent 85d75e6dfc
commit 9dcfdce732
2 changed files with 89 additions and 44 deletions

View file

@ -27,7 +27,7 @@
*/ */
#include <iomanip> #include <iomanip>
#include <iostream> #include <fstream>
#include <list> #include <list>
#include <map> #include <map>
#include <string> #include <string>
@ -82,11 +82,19 @@ namespace Database
list_t printStats; list_t printStats;
map_t statMap; map_t statMap;
ofstream *stream;
Python *py;
public: public:
void dump(ostream &stream, const string &name, DisplayMode mode); Data();
~Data();
void dump(ostream &stream, DisplayMode mode);
void display(ostream &stream, DisplayMode mode); void display(ostream &stream, DisplayMode mode);
void python(ostream &stream, const string &name); void python_start(const string &file);
void python(Python &py, const string &name, const string &bin); void python_dump(const string &name, const string &subname);
void python(const string &name, const string &subname,
const string &bin);
StatData *find(void *stat); StatData *find(void *stat);
void mapStat(void *stat, StatData *data); void mapStat(void *stat, StatData *data);
@ -99,16 +107,28 @@ namespace Database
static std::string name() { return "Statistics Database"; } static std::string name() { return "Statistics Database"; }
}; };
Data::Data()
: stream(0), py(0)
{
}
Data::~Data()
{
if (stream) {
delete py;
ccprintf(*stream, "if __name__ == '__main__':\n");
ccprintf(*stream, " program_display()\n");
stream->close();
delete stream;
}
}
void void
Data::dump(ostream &stream, const string &name, DisplayMode mode) Data::dump(ostream &stream, DisplayMode mode)
{ {
MainBin *orig = MainBin::curBin(); MainBin *orig = MainBin::curBin();
switch (mode) { switch (mode) {
case mode_python:
python(stream, name);
break;
case mode_m5: case mode_m5:
case mode_simplescalar: case mode_simplescalar:
display(stream, mode); display(stream, mode);
@ -158,50 +178,61 @@ Data::display(ostream &stream, DisplayMode mode)
} }
void void
Data::python(ostream &stream, const string &name) Data::python_start(const string &file)
{ {
Python py(stream); if (stream)
panic("can't start python twice!");
ccprintf(stream, "import sys\n"); stream = new ofstream(file.c_str(), ios::trunc);
ccprintf(stream, "sys.path.append('.')\n"); py = new Python(*stream);
ccprintf(stream, "from m5stats import *\n\n");
ccprintf(*stream, "import sys\n");
ccprintf(*stream, "sys.path.append('.')\n");
ccprintf(*stream, "from m5stats import *\n\n");
}
void
Data::python_dump(const string &name, const string &subname)
{
if (!py)
panic("Can't dump python without first opening the file");
if (bins.empty()) { if (bins.empty()) {
python(py, name, ""); python(name, subname, "");
} else { } else {
list<MainBin *>::iterator i = bins.begin(); list<MainBin *>::iterator i = bins.begin();
list<MainBin *>::iterator end = bins.end(); list<MainBin *>::iterator end = bins.end();
while (i != end) { while (i != end) {
(*i)->activate(); (*i)->activate();
python(py, name, (*i)->name()); python(name, subname, (*i)->name());
++i; ++i;
} }
} }
py->next();
py.next();
ccprintf(stream, "if __name__ == '__main__':\n");
ccprintf(stream, " program_display()\n");
} }
void void
Data::python(Python &py, const string &name, const string &bin) Data::python(const string &name, const string &subname, const string &bin)
{ {
py.start("collections.append"); py->start("collections.append");
py.start("Collection"); py->start("Collection");
py.qarg(name); py->qarg(name);
py.qarg(bin); py->qarg(subname);
py.qarg(hostname()); py->qarg(bin);
py.qarg(Time::start.date()); py->qarg(hostname());
py->qarg(Time::start.date());
py->startList();
list_t::iterator i = allStats.begin(); list_t::iterator i = allStats.begin();
list_t::iterator end = allStats.end(); list_t::iterator end = allStats.end();
while (i != end) { while (i != end) {
StatData *stat = *i; StatData *stat = *i;
stat->python(py); stat->python(*py);
++i; ++i;
} }
py.end(); py->endList();
py.end(); py->end();
py->end();
} }
StatData * StatData *
@ -967,7 +998,7 @@ ScalarDataBase::python(Python &py) const
{ {
py.start("Scalar"); py.start("Scalar");
py.qarg(name); py.qarg(name);
py.qarg(desc); py.qqqarg(desc);
py.kwarg("binned", binned()); py.kwarg("binned", binned());
py.kwarg("precision", precision); py.kwarg("precision", precision);
py.kwarg("flags", flags); py.kwarg("flags", flags);
@ -984,7 +1015,7 @@ VectorDataBase::python(Python &py) const
py.start("Vector"); py.start("Vector");
py.qarg(name); py.qarg(name);
py.qarg(desc); py.qqqarg(desc);
py.kwarg("binned", binned()); py.kwarg("binned", binned());
py.kwarg("precision", precision); py.kwarg("precision", precision);
py.kwarg("flags", flags); py.kwarg("flags", flags);
@ -1033,7 +1064,7 @@ FormulaDataBase::python(Python &py) const
py.start("Formula"); py.start("Formula");
py.qarg(name); py.qarg(name);
py.qarg(desc); py.qqqarg(desc);
py.kwarg("binned", binned()); py.kwarg("binned", binned());
py.kwarg("precision", precision); py.kwarg("precision", precision);
py.kwarg("flags", flags); py.kwarg("flags", flags);
@ -1054,7 +1085,7 @@ DistDataBase::python(Python &py) const
py.start("Dist"); py.start("Dist");
py.qarg(name); py.qarg(name);
py.qarg(desc); py.qqqarg(desc);
py.kwarg("binned", binned()); py.kwarg("binned", binned());
py.kwarg("precision", precision); py.kwarg("precision", precision);
py.kwarg("flags", flags); py.kwarg("flags", flags);
@ -1071,7 +1102,7 @@ VectorDistDataBase::python(Python &py) const
py.start("VectorDist"); py.start("VectorDist");
py.qarg(name); py.qarg(name);
py.qarg(desc); py.qqqarg(desc);
py.kwarg("binned", binned()); py.kwarg("binned", binned());
py.kwarg("precision", precision); py.kwarg("precision", precision);
py.kwarg("flags", flags); py.kwarg("flags", flags);
@ -1101,7 +1132,7 @@ Vector2dDataBase::python(Python &py) const
py.start("Vector2d"); py.start("Vector2d");
py.qarg(name); py.qarg(name);
py.qarg(desc); py.qqqarg(desc);
py.kwarg("binned", binned()); py.kwarg("binned", binned());
py.kwarg("precision", precision); py.kwarg("precision", precision);
py.kwarg("flags", flags); py.kwarg("flags", flags);
@ -1124,13 +1155,14 @@ Vector2dDataBase::python(Python &py) const
void void
FormulaBase::val(rvec_t &vec) const FormulaBase::val(rvec_t &vec) const
{ {
vec = root->val(); if (root)
vec = root->val();
} }
result_t result_t
FormulaBase::total() const FormulaBase::total() const
{ {
return root->total(); return root ? root->total() : 0.0;
} }
size_t size_t
@ -1240,11 +1272,24 @@ check()
} }
void void
dump(ostream &stream, const string &name, DisplayMode mode) dump(ostream &stream, DisplayMode mode)
{ {
Database::StatDB().dump(stream, name, mode); Database::StatDB().dump(stream, mode);
} }
void
python_start(const string &file)
{
Database::StatDB().python_start(file);
}
void
python_dump(const string &name, const string &subname)
{
Database::StatDB().python_dump(name, subname);
}
CallbackQueue resetQueue; CallbackQueue resetQueue;
void void

View file

@ -120,8 +120,7 @@ const StatFlags __reserved = init | print | __substat;
enum DisplayMode enum DisplayMode
{ {
mode_m5, mode_m5,
mode_simplescalar, mode_simplescalar
mode_python
}; };
extern DisplayMode DefaultMode; extern DisplayMode DefaultMode;
@ -2931,7 +2930,7 @@ class Temp;
class Formula class Formula
: public WrapVec<Formula, : public WrapVec<Formula,
FormulaBase, FormulaBase,
VectorData> FormulaData>
{ {
public: public:
/** /**
@ -3132,8 +3131,9 @@ class Temp
*/ */
void check(); void check();
void dump(std::ostream &stream, const std::string &name = "", void dump(std::ostream &stream, DisplayMode mode = DefaultMode);
DisplayMode mode = DefaultMode); void python_start(const std::string &file);
void python_dump(const std::string &name, const std::string &subname);
void reset(); void reset();
void registerResetCallback(Callback *cb); void registerResetCallback(Callback *cb);