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());
215 Data::python(const string
&name
, const string
&subname
, const string
&bin
)
217 py
->name("collections.append");
219 py
->name("Collection");
227 py
->qarg(hostname());
229 py
->qarg(Time::start
.date());
232 list_t::iterator i
= allStats
.begin();
233 list_t::iterator end
= allStats
.end();
250 Data::find(void *stat
)
252 map_t::const_iterator i
= statMap
.find(stat
);
254 if (i
== statMap
.end())
263 list_t::iterator i
= allStats
.begin();
264 list_t::iterator end
= allStats
.end();
273 i
= allStats
.begin();
277 if (!(data
->flags
& print
))
278 data
->name
= "__Stat" + to_string(j
++);
286 // reset non-binned stats
287 list_t::iterator i
= allStats
.begin();
288 list_t::iterator end
= allStats
.end();
296 // save the bin so we can go back to where we were
297 MainBin
*orig
= MainBin::curBin();
299 // reset binned stats
300 list
<MainBin
*>::iterator bi
= bins
.begin();
301 list
<MainBin
*>::iterator be
= bins
.end();
306 i
= allStats
.begin();
317 MainBin::curBin() = orig
;
321 Data::mapStat(void *stat
, StatData
*data
)
323 if (statMap
.find(stat
) != statMap
.end())
324 panic("shouldn't register stat twice!");
326 allStats
.push_back(data
);
331 (statMap
.insert(make_pair(stat
, data
))).second
;
332 assert(statMap
.find(stat
) != statMap
.end());
333 assert(success
&& "this should never fail");
337 Data::regBin(MainBin
*bin
, string _name
)
340 DPRINTF(Stats
, "registering %s\n", _name
);
344 Data::regPrint(void *stat
)
346 StatData
*data
= find(stat
);
348 if (data
->flags
& print
)
351 data
->flags
|= print
;
353 list_t::iterator j
= printStats
.insert(printStats
.end(), data
);
354 inplace_merge(printStats
.begin(), j
, printStats
.end(), StatData::less
);
367 DataAccess::find() const
369 return Database::StatDB().find(const_cast<void *>((const void *)this));
373 getStatData(const void *stat
)
375 return Database::StatDB().find(const_cast<void *>(stat
));
379 DataAccess::map(StatData
*data
)
381 Database::StatDB().mapStat(this, data
);
385 DataAccess::statData()
387 StatData
*ptr
= find();
393 DataAccess::statData() const
395 const StatData
*ptr
= find();
401 DataAccess::setInit()
403 statData()->flags
|= init
;
407 DataAccess::setPrint()
409 Database::StatDB().regPrint(this);
412 StatData::~StatData()
417 StatData::less(StatData
*stat1
, StatData
*stat2
)
419 const string
&name1
= stat1
->name
;
420 const string
&name2
= stat2
->name
;
425 tokenize(v1
, name1
, '.');
426 tokenize(v2
, name2
, '.');
428 int last
= min(v1
.size(), v2
.size()) - 1;
429 for (int i
= 0; i
< last
; ++i
)
431 return v1
[i
] < v2
[i
];
433 // Special compare for last element.
434 if (v1
[last
] == v2
[last
])
435 return v1
.size() < v2
.size();
437 return v1
[last
] < v2
[last
];
443 StatData::baseCheck() const
445 if (!(flags
& init
)) {
447 cprintf("this is stat number %d\n",(*i
)->number
);
449 panic("Not all stats have been initialized");
453 if ((flags
& print
) && name
.empty()) {
454 panic("all printable stats must be named");
462 ValueToString(result_t value
, DisplayMode mode
, int precision
)
468 val
.precision(precision
);
469 else if (value
== rint(value
))
472 val
.unsetf(ios::showpoint
);
473 val
.setf(ios::fixed
);
476 val
<< (mode
== mode_m5
? "no value" : "<err: div-0>");
494 : value(0.0), flags(0), mode(DefaultMode
), precision(0),
498 void operator()(ostream
&stream
) const;
502 ScalarPrint::operator()(ostream
&stream
) const
504 if (flags
& nozero
&& value
== 0.0 ||
505 flags
& nonan
&& isnan(value
))
508 stringstream pdfstr
, cdfstr
;
511 ccprintf(pdfstr
, "%.2f%%", pdf
* 100.0);
514 ccprintf(cdfstr
, "%.2f%%", cdf
* 100.0);
516 if (mode
== mode_simplescalar
&& flags
& __substat
) {
517 ccprintf(stream
, "%32s %12s %10s %10s", name
,
518 ValueToString(value
, mode
, precision
), pdfstr
, cdfstr
);
520 ccprintf(stream
, "%-40s %12s %10s %10s", name
,
521 ValueToString(value
, mode
, precision
), pdfstr
, cdfstr
);
524 if (PrintDescriptions
) {
526 ccprintf(stream
, " # %s", desc
);
535 vector
<string
> subnames
;
536 vector
<string
> subdescs
;
544 : subnames(0), subdescs(0), flags(0), mode(DefaultMode
),
545 precision(-1), total(NAN
)
548 void operator()(ostream
&stream
) const;
552 VectorPrint::operator()(std::ostream
&stream
) const
554 int _size
= vec
.size();
555 result_t _total
= 0.0;
557 if (flags
& (pdf
| cdf
)) {
558 for (int i
= 0; i
< _size
; ++i
) {
563 string base
= name
+ ((mode
== mode_simplescalar
) ? "_" : "::");
568 print
.precision
= precision
;
571 bool havesub
= !subnames
.empty();
574 print
.value
= vec
[0];
576 } else if (mode
== mode_m5
) {
577 for (int i
= 0; i
< _size
; ++i
) {
578 if (havesub
&& (i
>= subnames
.size() || subnames
[i
].empty()))
581 print
.name
= base
+ (havesub
? subnames
[i
] : to_string(i
));
582 print
.desc
= subdescs
.empty() ? desc
: subdescs
[i
];
583 print
.value
= vec
[i
];
585 if (_total
&& (flags
& pdf
)) {
586 print
.pdf
= vec
[i
] / _total
;
587 print
.cdf
+= print
.pdf
;
593 if (flags
& ::Statistics::total
) {
594 print
.name
= base
+ "total";
600 if (flags
& ::Statistics::total
) {
608 ccprintf(stream
, "%s.start_dist\n", name
);
609 for (int i
= 0; i
< _size
; ++i
) {
610 print
.name
= havesub
? subnames
[i
] : to_string(i
);
611 print
.desc
= subdescs
.empty() ? desc
: subdescs
[i
];
612 print
.flags
|= __substat
;
613 print
.value
= vec
[i
];
616 _pdf
= vec
[i
] / _total
;
627 ccprintf(stream
, "%s.end_dist\n", name
);
629 for (int i
= 0; i
< _size
; ++i
) {
630 if (havesub
&& subnames
[i
].empty())
634 print
.name
+= havesub
? subnames
[i
] : to_string(i
);
635 print
.desc
= subdescs
.empty() ? desc
: subdescs
[i
];
636 print
.value
= vec
[i
];
639 _pdf
= vec
[i
] / _total
;
679 void operator()(ostream
&stream
) const;
683 DistPrint::operator()(ostream
&stream
) const
687 string base
= name
+ ((mode
== mode_m5
) ? "::" : "_");
689 print
.precision
= precision
;
694 print
.name
= base
+ "mean";
695 print
.value
= samples
? sum
/ samples
: NAN
;
698 print
.name
= base
+ "stdev";
699 print
.value
= samples
? sqrt((samples
* squares
- sum
* sum
) /
700 (samples
* (samples
- 1.0))) : NAN
;
703 print
.name
= "**Ignore: " + base
+ "TOT";
704 print
.value
= samples
;
709 assert(size
== vec
.size());
711 result_t total
= 0.0;
714 for (int i
= 0; i
< size
; ++i
)
718 string base
= name
+ (mode
== mode_m5
? "::" : ".");
721 print
.desc
= (mode
== mode_m5
) ? desc
: "";
724 print
.precision
= precision
;
726 if (mode
== mode_simplescalar
) {
727 ccprintf(stream
, "%-42s", base
+ "start_dist");
728 if (PrintDescriptions
&& !desc
.empty())
729 ccprintf(stream
, " # %s", desc
);
733 print
.name
= base
+ "samples";
734 print
.value
= samples
;
737 print
.name
= base
+ "min_value";
738 print
.value
= min_val
;
741 if (mode
== mode_m5
|| underflow
> 0.0) {
742 print
.name
= base
+ "underflows";
743 print
.value
= underflow
;
744 if (mode
== mode_m5
&& total
) {
745 print
.pdf
= underflow
/ total
;
746 print
.cdf
+= print
.pdf
;
752 if (mode
== mode_m5
) {
753 for (int i
= 0; i
< size
; ++i
) {
754 stringstream namestr
;
757 int low
= i
* bucket_size
+ min
;
758 int high
= ::min((i
+ 1) * bucket_size
+ min
- 1, max
);
761 namestr
<< "-" << high
;
763 print
.name
= namestr
.str();
764 print
.value
= vec
[i
];
766 print
.pdf
= vec
[i
] / total
;
767 print
.cdf
+= print
.pdf
;
777 print
.flags
= flags
| __substat
;
779 for (int i
= 0; i
< size
; ++i
) {
780 if (flags
& nozero
&& vec
[i
] == 0.0 ||
781 flags
& nonan
&& isnan(vec
[i
]))
784 _min
= i
* bucket_size
+ min
;
785 _pdf
= vec
[i
] / total
* 100.0;
789 print
.name
= ValueToString(_min
, mode
, 0);
790 print
.value
= vec
[i
];
791 print
.pdf
= (flags
& pdf
) ? _pdf
: NAN
;
792 print
.cdf
= (flags
& cdf
) ? _cdf
: NAN
;
799 if (mode
== mode_m5
|| overflow
> 0.0) {
800 print
.name
= base
+ "overflows";
801 print
.value
= overflow
;
802 if (mode
== mode_m5
&& total
) {
803 print
.pdf
= overflow
/ total
;
804 print
.cdf
+= print
.pdf
;
815 if (mode
!= mode_simplescalar
) {
816 print
.name
= base
+ "total";
821 print
.name
= base
+ "max_value";
822 print
.value
= max_val
;
825 if (mode
!= mode_simplescalar
&& samples
!= 0) {
826 print
.name
= base
+ "mean";
827 print
.value
= sum
/ samples
;
830 print
.name
= base
+ "stdev";
831 print
.value
= sqrt((samples
* squares
- sum
* sum
) /
832 (samples
* (samples
- 1.0)));
836 if (mode
== mode_simplescalar
)
837 ccprintf(stream
, "%send_dist\n\n", base
);
841 ScalarDataBase::display(ostream
&stream
, DisplayMode mode
) const
849 print
.precision
= precision
;
855 VectorDataBase::display(ostream
&stream
, DisplayMode mode
) const
857 int size
= this->size();
858 const_cast<VectorDataBase
*>(this)->update();
866 print
.precision
= precision
;
868 print
.total
= total();
870 if (!subnames
.empty()) {
871 for (int i
= 0; i
< size
; ++i
) {
872 if (!subnames
[i
].empty()) {
873 print
.subnames
= subnames
;
874 print
.subnames
.resize(size
);
875 for (int i
= 0; i
< size
; ++i
) {
876 if (!subnames
[i
].empty() && !subdescs
[i
].empty()) {
877 print
.subdescs
= subdescs
;
878 print
.subdescs
.resize(size
);
891 Vector2dDataBase::display(ostream
&stream
, DisplayMode mode
) const
893 const_cast<Vector2dDataBase
*>(this)->update();
895 bool havesub
= false;
898 print
.subnames
= y_subnames
;
901 print
.precision
= precision
;
903 if (!subnames
.empty()) {
904 for (int i
= 0; i
< x
; ++i
)
905 if (!subnames
[i
].empty())
910 result_t super_total
= 0.0;
911 for (int i
= 0; i
< x
; ++i
) {
912 if (havesub
&& (i
>= subnames
.size() || subnames
[i
].empty()))
918 result_t total
= 0.0;
919 for (int j
= 0; j
< y
; ++j
) {
920 yvec
[j
] = vec
[iy
+ j
];
921 tot_vec
[j
] += yvec
[j
];
923 super_total
+= yvec
[j
];
926 print
.name
= name
+ "_" + (havesub
? subnames
[i
] : to_string(i
));
933 if ((flags
& ::Statistics::total
) && (x
> 1)) {
937 print
.total
= super_total
;
943 DistDataBase::display(ostream
&stream
, DisplayMode mode
) const
945 const_cast<DistDataBase
*>(this)->update();
953 print
.precision
= precision
;
955 print
.min_val
= data
.min_val
;
956 print
.max_val
= data
.max_val
;
957 print
.underflow
= data
.underflow
;
958 print
.overflow
= data
.overflow
;
959 print
.vec
= data
.vec
;
960 print
.sum
= data
.sum
;
961 print
.squares
= data
.squares
;
962 print
.samples
= data
.samples
;
964 print
.min
= data
.min
;
965 print
.max
= data
.max
;
966 print
.bucket_size
= data
.bucket_size
;
967 print
.size
= data
.size
;
968 print
.fancy
= data
.fancy
;
974 VectorDistDataBase::display(ostream
&stream
, DisplayMode mode
) const
976 const_cast<VectorDistDataBase
*>(this)->update();
978 for (int i
= 0; i
< size(); ++i
) {
982 (subnames
[i
].empty() ? ("_" + to_string(i
)) : subnames
[i
]);
983 print
.desc
= subdescs
[i
].empty() ? desc
: subdescs
[i
];
986 print
.precision
= precision
;
988 print
.min_val
= data
[i
].min_val
;
989 print
.max_val
= data
[i
].max_val
;
990 print
.underflow
= data
[i
].underflow
;
991 print
.overflow
= data
[i
].overflow
;
992 print
.vec
= data
[i
].vec
;
993 print
.sum
= data
[i
].sum
;
994 print
.squares
= data
[i
].squares
;
995 print
.samples
= data
[i
].samples
;
997 print
.min
= data
[i
].min
;
998 print
.max
= data
[i
].max
;
999 print
.bucket_size
= data
[i
].bucket_size
;
1000 print
.size
= data
[i
].size
;
1001 print
.fancy
= data
[i
].fancy
;
1008 ScalarDataBase::python(Python
&py
) const
1013 py
.kwarg("binned", binned());
1014 py
.kwarg("precision", precision
);
1015 py
.kwarg("flags", flags
);
1017 py
.qkwarg("prereq", prereq
->name
);
1018 py
.kwarg("value", val());
1023 VectorDataBase::python(Python
&py
) const
1025 const_cast<VectorDataBase
*>(this)->update();
1030 py
.kwarg("binned", binned());
1031 py
.kwarg("precision", precision
);
1032 py
.kwarg("flags", flags
);
1034 py
.qkwarg("prereq", prereq
->name
);
1035 py
.kwarg("value", val());
1036 if (!subnames
.empty())
1037 py
.qkwarg("subnames", subnames
);
1038 if (!subdescs
.empty())
1039 py
.qkwarg("subdescs", subdescs
);
1044 DistDataData::python(Python
&py
, const string
&name
) const
1046 string s
= name
.empty() ? "" : name
+ "=";
1048 if (samples
== 0 || fancy
)
1057 if (samples
&& !fancy
) {
1065 py
.arg(bucket_size
);
1072 FormulaDataBase::python(Python
&py
) const
1074 const_cast<FormulaDataBase
*>(this)->update();
1079 py
.kwarg("binned", binned());
1080 py
.kwarg("precision", precision
);
1081 py
.kwarg("flags", flags
);
1083 py
.qkwarg("prereq", prereq
->name
);
1084 py
.qkwarg("formula", str());
1085 if (!subnames
.empty())
1086 py
.qkwarg("subnames", subnames
);
1087 if (!subdescs
.empty())
1088 py
.qkwarg("subdescs", subdescs
);
1093 DistDataBase::python(Python
&py
) const
1095 const_cast<DistDataBase
*>(this)->update();
1100 py
.kwarg("binned", binned());
1101 py
.kwarg("precision", precision
);
1102 py
.kwarg("flags", flags
);
1104 py
.qkwarg("prereq", prereq
->name
);
1105 data
.python(py
, "dist");
1110 VectorDistDataBase::python(Python
&py
) const
1112 const_cast<VectorDistDataBase
*>(this)->update();
1114 py
.name("VectorDist");
1117 py
.kwarg("binned", binned());
1118 py
.kwarg("precision", precision
);
1119 py
.kwarg("flags", flags
);
1121 py
.qkwarg("prereq", prereq
->name
);
1122 if (!subnames
.empty())
1123 py
.qkwarg("subnames", subnames
);
1124 if (!subdescs
.empty())
1125 py
.qkwarg("subdescs", subdescs
);
1128 typedef std::vector
<DistDataData
>::const_iterator iter
;
1129 iter i
= data
.begin();
1130 iter end
= data
.end();
1140 Vector2dDataBase::python(Python
&py
) const
1142 const_cast<Vector2dDataBase
*>(this)->update();
1144 py
.name("Vector2d");
1147 py
.kwarg("binned", binned());
1148 py
.kwarg("precision", precision
);
1149 py
.kwarg("flags", flags
);
1151 py
.qkwarg("prereq", prereq
->name
);
1153 py
.kwarg("value", vec
);
1154 if (!subnames
.empty())
1155 py
.qkwarg("subnames", subnames
);
1156 if (!subdescs
.empty())
1157 py
.qkwarg("subdescs", subdescs
);
1158 if (!y_subnames
.empty())
1159 py
.qkwarg("ysubnames", y_subnames
);
1167 FormulaBase::val(rvec_t
&vec
) const
1174 FormulaBase::total() const
1176 return root
? root
->total() : 0.0;
1180 FormulaBase::size() const
1185 return root
->size();
1189 FormulaBase::binned() const
1191 return root
&& root
->binned();
1195 FormulaBase::reset()
1200 FormulaBase::zero() const
1204 for (int i
= 0; i
< vec
.size(); ++i
)
1211 FormulaBase::update(StatData
*)
1216 FormulaBase::str() const
1218 return root
? root
->str() : "";
1226 Formula::Formula(Temp r
)
1233 Formula::operator=(Temp r
)
1235 assert(!root
&& "Can't change formulas");
1242 Formula::operator+=(Temp r
)
1245 root
= NodePtr(new BinaryNode
<std::plus
<result_t
> >(root
, r
));
1252 MainBin::MainBin(const string
&name
)
1253 : _name(name
), mem(NULL
), memsize(-1)
1255 Database::StatDB().regBin(this, name
);
1265 MainBin::memory(off_t off
)
1268 memsize
= CeilPow2((size_t) offset());
1271 mem
= new char[memsize
];
1272 memset(mem
, 0, memsize
);
1275 assert(offset() <= size());
1282 Database::StatDB().check();
1286 dump(ostream
&stream
, DisplayMode mode
)
1288 Database::StatDB().dump(stream
, mode
);
1292 python_start(const string
&file
)
1294 Database::StatDB().python_start(file
);
1298 python_dump(const string
&name
, const string
&subname
)
1300 Database::StatDB().python_dump(name
, subname
);
1304 CallbackQueue resetQueue
;
1307 registerResetCallback(Callback
*cb
)
1315 Database::StatDB().reset();
1316 resetQueue
.process();
1319 } // namespace Statistics