2 * Copyright (c) 2003 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "base/callback.hh"
37 #include "base/cprintf.hh"
38 #include "base/hostinfo.hh"
39 #include "base/misc.hh"
40 #include "base/python.hh"
41 #include "base/statistics.hh"
42 #include "base/str.hh"
43 #include "base/time.hh"
44 #include "base/trace.hh"
61 static int total_stats
= 0;
66 // This is a hack to get this parameter from the old stats package.
67 namespace Statistics
{
68 bool PrintDescriptions
= true;
69 DisplayMode DefaultMode
= mode_simplescalar
;
76 typedef list
<StatData
*> list_t
;
77 typedef map
<void *, StatData
*> map_t
;
92 void dump(ostream
&stream
, DisplayMode mode
);
93 void display(ostream
&stream
, DisplayMode mode
);
94 void python_start(const string
&file
);
95 void python_dump(const string
&name
, const string
&subname
);
96 void python(const string
&name
, const string
&subname
,
99 StatData
*find(void *stat
);
100 void mapStat(void *stat
, StatData
*data
);
104 void regBin(MainBin
*bin
, string name
);
105 void regPrint(void *stat
);
107 static std::string
name() { return "Statistics Database"; }
119 ccprintf(*stream
, "\n\nif __name__ == '__main__':\n");
120 ccprintf(*stream
, " program_display()\n");
127 Data::dump(ostream
&stream
, DisplayMode mode
)
129 MainBin
*orig
= MainBin::curBin();
133 case mode_simplescalar
:
134 display(stream
, mode
);
137 warn("invalid display mode!\n");
138 display(stream
, mode_m5
);
147 Data::display(ostream
&stream
, DisplayMode mode
)
150 list
<MainBin
*>::iterator i
= bins
.begin();
151 list
<MainBin
*>::iterator bins_end
= bins
.end();
152 ccprintf(stream
, "PRINTING BINNED STATS\n");
153 while (i
!= bins_end
) {
155 ccprintf(stream
,"---%s Bin------------\n", (*i
)->name());
157 list_t::iterator j
= printStats
.begin();
158 list_t::iterator end
= printStats
.end();
161 if (stat
->dodisplay())
162 stat
->display(stream
, mode
);
166 ccprintf(stream
, "---------------------------------\n");
169 list_t::iterator i
= printStats
.begin();
170 list_t::iterator end
= printStats
.end();
173 if (stat
->dodisplay() && !stat
->binned())
174 stat
->display(stream
, mode
);
181 Data::python_start(const string
&file
)
184 panic("can't start python twice!");
186 stream
= new ofstream(file
.c_str(), ios::trunc
);
187 py
= new Python(*stream
);
189 ccprintf(*stream
, "import sys\n");
190 ccprintf(*stream
, "sys.path.append('.')\n");
191 ccprintf(*stream
, "from m5stats import *\n\n");
195 Data::python_dump(const string
&name
, const string
&subname
)
198 panic("Can't dump python without first opening the file");
201 python(name
, subname
, "");
203 list
<MainBin
*>::iterator i
= bins
.begin();
204 list
<MainBin
*>::iterator end
= bins
.end();
208 python(name
, subname
, (*i
)->name());
216 Data::python(const string
&name
, const string
&subname
, const string
&bin
)
218 py
->name("collections.append");
220 py
->name("Collection");
228 py
->qarg(hostname());
230 py
->qarg(Time::start
.date());
233 list_t::iterator i
= allStats
.begin();
234 list_t::iterator end
= allStats
.end();
251 Data::find(void *stat
)
253 map_t::const_iterator i
= statMap
.find(stat
);
255 if (i
== statMap
.end())
264 list_t::iterator i
= allStats
.begin();
265 list_t::iterator end
= allStats
.end();
274 i
= allStats
.begin();
278 if (!(data
->flags
& print
))
279 data
->name
= "__Stat" + to_string(j
++);
287 // reset non-binned stats
288 list_t::iterator i
= allStats
.begin();
289 list_t::iterator end
= allStats
.end();
297 // save the bin so we can go back to where we were
298 MainBin
*orig
= MainBin::curBin();
300 // reset binned stats
301 list
<MainBin
*>::iterator bi
= bins
.begin();
302 list
<MainBin
*>::iterator be
= bins
.end();
307 i
= allStats
.begin();
318 MainBin::curBin() = orig
;
322 Data::mapStat(void *stat
, StatData
*data
)
324 if (statMap
.find(stat
) != statMap
.end())
325 panic("shouldn't register stat twice!");
327 allStats
.push_back(data
);
332 (statMap
.insert(make_pair(stat
, data
))).second
;
333 assert(statMap
.find(stat
) != statMap
.end());
334 assert(success
&& "this should never fail");
338 Data::regBin(MainBin
*bin
, string _name
)
341 DPRINTF(Stats
, "registering %s\n", _name
);
345 Data::regPrint(void *stat
)
347 StatData
*data
= find(stat
);
349 if (data
->flags
& print
)
352 data
->flags
|= print
;
354 list_t::iterator j
= printStats
.insert(printStats
.end(), data
);
355 inplace_merge(printStats
.begin(), j
, printStats
.end(), StatData::less
);
368 DataAccess::find() const
370 return Database::StatDB().find(const_cast<void *>((const void *)this));
374 getStatData(const void *stat
)
376 return Database::StatDB().find(const_cast<void *>(stat
));
380 DataAccess::map(StatData
*data
)
382 Database::StatDB().mapStat(this, data
);
386 DataAccess::statData()
388 StatData
*ptr
= find();
394 DataAccess::statData() const
396 const StatData
*ptr
= find();
402 DataAccess::setInit()
404 statData()->flags
|= init
;
408 DataAccess::setPrint()
410 Database::StatDB().regPrint(this);
413 StatData::~StatData()
418 StatData::less(StatData
*stat1
, StatData
*stat2
)
420 const string
&name1
= stat1
->name
;
421 const string
&name2
= stat2
->name
;
426 tokenize(v1
, name1
, '.');
427 tokenize(v2
, name2
, '.');
429 int last
= min(v1
.size(), v2
.size()) - 1;
430 for (int i
= 0; i
< last
; ++i
)
432 return v1
[i
] < v2
[i
];
434 // Special compare for last element.
435 if (v1
[last
] == v2
[last
])
436 return v1
.size() < v2
.size();
438 return v1
[last
] < v2
[last
];
444 StatData::baseCheck() const
446 if (!(flags
& init
)) {
448 cprintf("this is stat number %d\n",(*i
)->number
);
450 panic("Not all stats have been initialized");
454 if ((flags
& print
) && name
.empty()) {
455 panic("all printable stats must be named");
463 ValueToString(result_t value
, DisplayMode mode
, int precision
)
469 val
.precision(precision
);
470 else if (value
== rint(value
))
473 val
.unsetf(ios::showpoint
);
474 val
.setf(ios::fixed
);
477 val
<< (mode
== mode_m5
? "no value" : "<err: div-0>");
495 : value(0.0), flags(0), mode(DefaultMode
), precision(0),
499 void operator()(ostream
&stream
) const;
503 ScalarPrint::operator()(ostream
&stream
) const
505 if (flags
& nozero
&& value
== 0.0 ||
506 flags
& nonan
&& isnan(value
))
509 stringstream pdfstr
, cdfstr
;
512 ccprintf(pdfstr
, "%.2f%%", pdf
* 100.0);
515 ccprintf(cdfstr
, "%.2f%%", cdf
* 100.0);
517 if (mode
== mode_simplescalar
&& flags
& __substat
) {
518 ccprintf(stream
, "%32s %12s %10s %10s", name
,
519 ValueToString(value
, mode
, precision
), pdfstr
, cdfstr
);
521 ccprintf(stream
, "%-40s %12s %10s %10s", name
,
522 ValueToString(value
, mode
, precision
), pdfstr
, cdfstr
);
525 if (PrintDescriptions
) {
527 ccprintf(stream
, " # %s", desc
);
536 vector
<string
> subnames
;
537 vector
<string
> subdescs
;
545 : subnames(0), subdescs(0), flags(0), mode(DefaultMode
),
546 precision(-1), total(NAN
)
549 void operator()(ostream
&stream
) const;
553 VectorPrint::operator()(std::ostream
&stream
) const
555 int _size
= vec
.size();
556 result_t _total
= 0.0;
558 if (flags
& (pdf
| cdf
)) {
559 for (int i
= 0; i
< _size
; ++i
) {
564 string base
= name
+ ((mode
== mode_simplescalar
) ? "_" : "::");
569 print
.precision
= precision
;
572 bool havesub
= !subnames
.empty();
575 print
.value
= vec
[0];
577 } else if (mode
== mode_m5
) {
578 for (int i
= 0; i
< _size
; ++i
) {
579 if (havesub
&& (i
>= subnames
.size() || subnames
[i
].empty()))
582 print
.name
= base
+ (havesub
? subnames
[i
] : to_string(i
));
583 print
.desc
= subdescs
.empty() ? desc
: subdescs
[i
];
584 print
.value
= vec
[i
];
586 if (_total
&& (flags
& pdf
)) {
587 print
.pdf
= vec
[i
] / _total
;
588 print
.cdf
+= print
.pdf
;
594 if (flags
& ::Statistics::total
) {
595 print
.name
= base
+ "total";
601 if (flags
& ::Statistics::total
) {
609 ccprintf(stream
, "%s.start_dist\n", name
);
610 for (int i
= 0; i
< _size
; ++i
) {
611 print
.name
= havesub
? subnames
[i
] : to_string(i
);
612 print
.desc
= subdescs
.empty() ? desc
: subdescs
[i
];
613 print
.flags
|= __substat
;
614 print
.value
= vec
[i
];
617 _pdf
= vec
[i
] / _total
;
628 ccprintf(stream
, "%s.end_dist\n", name
);
630 for (int i
= 0; i
< _size
; ++i
) {
631 if (havesub
&& subnames
[i
].empty())
635 print
.name
+= havesub
? subnames
[i
] : to_string(i
);
636 print
.desc
= subdescs
.empty() ? desc
: subdescs
[i
];
637 print
.value
= vec
[i
];
640 _pdf
= vec
[i
] / _total
;
680 void operator()(ostream
&stream
) const;
684 DistPrint::operator()(ostream
&stream
) const
688 string base
= name
+ ((mode
== mode_m5
) ? "::" : "_");
690 print
.precision
= precision
;
695 print
.name
= base
+ "mean";
696 print
.value
= samples
? sum
/ samples
: NAN
;
699 print
.name
= base
+ "stdev";
700 print
.value
= samples
? sqrt((samples
* squares
- sum
* sum
) /
701 (samples
* (samples
- 1.0))) : NAN
;
704 print
.name
= "**Ignore: " + base
+ "TOT";
705 print
.value
= samples
;
710 assert(size
== vec
.size());
712 result_t total
= 0.0;
715 for (int i
= 0; i
< size
; ++i
)
719 string base
= name
+ (mode
== mode_m5
? "::" : ".");
722 print
.desc
= (mode
== mode_m5
) ? desc
: "";
725 print
.precision
= precision
;
727 if (mode
== mode_simplescalar
) {
728 ccprintf(stream
, "%-42s", base
+ "start_dist");
729 if (PrintDescriptions
&& !desc
.empty())
730 ccprintf(stream
, " # %s", desc
);
734 print
.name
= base
+ "samples";
735 print
.value
= samples
;
738 print
.name
= base
+ "min_value";
739 print
.value
= min_val
;
742 if (mode
== mode_m5
|| underflow
> 0.0) {
743 print
.name
= base
+ "underflows";
744 print
.value
= underflow
;
745 if (mode
== mode_m5
&& total
) {
746 print
.pdf
= underflow
/ total
;
747 print
.cdf
+= print
.pdf
;
753 if (mode
== mode_m5
) {
754 for (int i
= 0; i
< size
; ++i
) {
755 stringstream namestr
;
758 int low
= i
* bucket_size
+ min
;
759 int high
= ::min((i
+ 1) * bucket_size
+ min
- 1, max
);
762 namestr
<< "-" << high
;
764 print
.name
= namestr
.str();
765 print
.value
= vec
[i
];
767 print
.pdf
= vec
[i
] / total
;
768 print
.cdf
+= print
.pdf
;
778 print
.flags
= flags
| __substat
;
780 for (int i
= 0; i
< size
; ++i
) {
781 if (flags
& nozero
&& vec
[i
] == 0.0 ||
782 flags
& nonan
&& isnan(vec
[i
]))
785 _min
= i
* bucket_size
+ min
;
786 _pdf
= vec
[i
] / total
* 100.0;
790 print
.name
= ValueToString(_min
, mode
, 0);
791 print
.value
= vec
[i
];
792 print
.pdf
= (flags
& pdf
) ? _pdf
: NAN
;
793 print
.cdf
= (flags
& cdf
) ? _cdf
: NAN
;
800 if (mode
== mode_m5
|| overflow
> 0.0) {
801 print
.name
= base
+ "overflows";
802 print
.value
= overflow
;
803 if (mode
== mode_m5
&& total
) {
804 print
.pdf
= overflow
/ total
;
805 print
.cdf
+= print
.pdf
;
816 if (mode
!= mode_simplescalar
) {
817 print
.name
= base
+ "total";
822 print
.name
= base
+ "max_value";
823 print
.value
= max_val
;
826 if (mode
!= mode_simplescalar
&& samples
!= 0) {
827 print
.name
= base
+ "mean";
828 print
.value
= sum
/ samples
;
831 print
.name
= base
+ "stdev";
832 print
.value
= sqrt((samples
* squares
- sum
* sum
) /
833 (samples
* (samples
- 1.0)));
837 if (mode
== mode_simplescalar
)
838 ccprintf(stream
, "%send_dist\n\n", base
);
842 ScalarDataBase::display(ostream
&stream
, DisplayMode mode
) const
850 print
.precision
= precision
;
856 VectorDataBase::display(ostream
&stream
, DisplayMode mode
) const
858 int size
= this->size();
859 const_cast<VectorDataBase
*>(this)->update();
867 print
.precision
= precision
;
869 print
.total
= total();
871 if (!subnames
.empty()) {
872 for (int i
= 0; i
< size
; ++i
) {
873 if (!subnames
[i
].empty()) {
874 print
.subnames
= subnames
;
875 print
.subnames
.resize(size
);
876 for (int i
= 0; i
< size
; ++i
) {
877 if (!subnames
[i
].empty() && !subdescs
[i
].empty()) {
878 print
.subdescs
= subdescs
;
879 print
.subdescs
.resize(size
);
892 Vector2dDataBase::display(ostream
&stream
, DisplayMode mode
) const
894 const_cast<Vector2dDataBase
*>(this)->update();
896 bool havesub
= false;
899 print
.subnames
= y_subnames
;
902 print
.precision
= precision
;
904 if (!subnames
.empty()) {
905 for (int i
= 0; i
< x
; ++i
)
906 if (!subnames
[i
].empty())
911 result_t super_total
= 0.0;
912 for (int i
= 0; i
< x
; ++i
) {
913 if (havesub
&& (i
>= subnames
.size() || subnames
[i
].empty()))
919 result_t total
= 0.0;
920 for (int j
= 0; j
< y
; ++j
) {
921 yvec
[j
] = vec
[iy
+ j
];
922 tot_vec
[j
] += yvec
[j
];
924 super_total
+= yvec
[j
];
927 print
.name
= name
+ "_" + (havesub
? subnames
[i
] : to_string(i
));
934 if ((flags
& ::Statistics::total
) && (x
> 1)) {
938 print
.total
= super_total
;
944 DistDataBase::display(ostream
&stream
, DisplayMode mode
) const
946 const_cast<DistDataBase
*>(this)->update();
954 print
.precision
= precision
;
956 print
.min_val
= data
.min_val
;
957 print
.max_val
= data
.max_val
;
958 print
.underflow
= data
.underflow
;
959 print
.overflow
= data
.overflow
;
960 print
.vec
= data
.vec
;
961 print
.sum
= data
.sum
;
962 print
.squares
= data
.squares
;
963 print
.samples
= data
.samples
;
965 print
.min
= data
.min
;
966 print
.max
= data
.max
;
967 print
.bucket_size
= data
.bucket_size
;
968 print
.size
= data
.size
;
969 print
.fancy
= data
.fancy
;
975 VectorDistDataBase::display(ostream
&stream
, DisplayMode mode
) const
977 const_cast<VectorDistDataBase
*>(this)->update();
979 for (int i
= 0; i
< size(); ++i
) {
983 (subnames
[i
].empty() ? ("_" + to_string(i
)) : subnames
[i
]);
984 print
.desc
= subdescs
[i
].empty() ? desc
: subdescs
[i
];
987 print
.precision
= precision
;
989 print
.min_val
= data
[i
].min_val
;
990 print
.max_val
= data
[i
].max_val
;
991 print
.underflow
= data
[i
].underflow
;
992 print
.overflow
= data
[i
].overflow
;
993 print
.vec
= data
[i
].vec
;
994 print
.sum
= data
[i
].sum
;
995 print
.squares
= data
[i
].squares
;
996 print
.samples
= data
[i
].samples
;
998 print
.min
= data
[i
].min
;
999 print
.max
= data
[i
].max
;
1000 print
.bucket_size
= data
[i
].bucket_size
;
1001 print
.size
= data
[i
].size
;
1002 print
.fancy
= data
[i
].fancy
;
1009 ScalarDataBase::python(Python
&py
) const
1014 py
.kwarg("binned", binned());
1015 py
.kwarg("precision", precision
);
1016 py
.kwarg("flags", flags
);
1018 py
.qkwarg("prereq", prereq
->name
);
1019 py
.kwarg("value", val());
1024 VectorDataBase::python(Python
&py
) const
1026 const_cast<VectorDataBase
*>(this)->update();
1031 py
.kwarg("binned", binned());
1032 py
.kwarg("precision", precision
);
1033 py
.kwarg("flags", flags
);
1035 py
.qkwarg("prereq", prereq
->name
);
1036 py
.kwarg("value", val());
1037 if (!subnames
.empty())
1038 py
.qkwarg("subnames", subnames
);
1039 if (!subdescs
.empty())
1040 py
.qkwarg("subdescs", subdescs
);
1045 DistDataData::python(Python
&py
, const string
&name
) const
1047 string s
= name
.empty() ? "" : name
+ "=";
1049 if (samples
== 0 || fancy
)
1058 if (samples
&& !fancy
) {
1066 py
.arg(bucket_size
);
1073 FormulaDataBase::python(Python
&py
) const
1075 const_cast<FormulaDataBase
*>(this)->update();
1080 py
.kwarg("binned", binned());
1081 py
.kwarg("precision", precision
);
1082 py
.kwarg("flags", flags
);
1084 py
.qkwarg("prereq", prereq
->name
);
1085 py
.qkwarg("formula", str());
1086 if (!subnames
.empty())
1087 py
.qkwarg("subnames", subnames
);
1088 if (!subdescs
.empty())
1089 py
.qkwarg("subdescs", subdescs
);
1094 DistDataBase::python(Python
&py
) const
1096 const_cast<DistDataBase
*>(this)->update();
1101 py
.kwarg("binned", binned());
1102 py
.kwarg("precision", precision
);
1103 py
.kwarg("flags", flags
);
1105 py
.qkwarg("prereq", prereq
->name
);
1106 data
.python(py
, "dist");
1111 VectorDistDataBase::python(Python
&py
) const
1113 const_cast<VectorDistDataBase
*>(this)->update();
1115 py
.name("VectorDist");
1118 py
.kwarg("binned", binned());
1119 py
.kwarg("precision", precision
);
1120 py
.kwarg("flags", flags
);
1122 py
.qkwarg("prereq", prereq
->name
);
1123 if (!subnames
.empty())
1124 py
.qkwarg("subnames", subnames
);
1125 if (!subdescs
.empty())
1126 py
.qkwarg("subdescs", subdescs
);
1129 typedef std::vector
<DistDataData
>::const_iterator iter
;
1130 iter i
= data
.begin();
1131 iter end
= data
.end();
1141 Vector2dDataBase::python(Python
&py
) const
1143 const_cast<Vector2dDataBase
*>(this)->update();
1145 py
.name("Vector2d");
1148 py
.kwarg("binned", binned());
1149 py
.kwarg("precision", precision
);
1150 py
.kwarg("flags", flags
);
1152 py
.qkwarg("prereq", prereq
->name
);
1154 py
.kwarg("value", vec
);
1155 if (!subnames
.empty())
1156 py
.qkwarg("subnames", subnames
);
1157 if (!subdescs
.empty())
1158 py
.qkwarg("subdescs", subdescs
);
1159 if (!y_subnames
.empty())
1160 py
.qkwarg("ysubnames", y_subnames
);
1168 FormulaBase::val(rvec_t
&vec
) const
1175 FormulaBase::total() const
1177 return root
? root
->total() : 0.0;
1181 FormulaBase::size() const
1186 return root
->size();
1190 FormulaBase::binned() const
1192 return root
&& root
->binned();
1196 FormulaBase::reset()
1201 FormulaBase::zero() const
1205 for (int i
= 0; i
< vec
.size(); ++i
)
1212 FormulaBase::update(StatData
*)
1217 FormulaBase::str() const
1219 return root
? root
->str() : "";
1227 Formula::Formula(Temp r
)
1234 Formula::operator=(Temp r
)
1236 assert(!root
&& "Can't change formulas");
1243 Formula::operator+=(Temp r
)
1246 root
= NodePtr(new BinaryNode
<std::plus
<result_t
> >(root
, r
));
1253 MainBin::MainBin(const string
&name
)
1254 : _name(name
), mem(NULL
), memsize(-1)
1256 Database::StatDB().regBin(this, name
);
1266 MainBin::memory(off_t off
)
1269 memsize
= CeilPow2((size_t) offset());
1272 mem
= new char[memsize
];
1273 memset(mem
, 0, memsize
);
1276 assert(offset() <= size());
1283 Database::StatDB().check();
1287 dump(ostream
&stream
, DisplayMode mode
)
1289 Database::StatDB().dump(stream
, mode
);
1293 python_start(const string
&file
)
1295 Database::StatDB().python_start(file
);
1299 python_dump(const string
&name
, const string
&subname
)
1301 Database::StatDB().python_dump(name
, subname
);
1305 CallbackQueue resetQueue
;
1308 registerResetCallback(Callback
*cb
)
1316 Database::StatDB().reset();
1317 resetQueue
.process();
1320 } // namespace Statistics