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/misc.hh"
39 #include "base/statistics.hh"
40 #include "base/str.hh"
41 #include "base/trace.hh"
42 #include "sim/universe.hh"
59 static int total_stats
= 0;
64 // This is a hack to get this parameter from the old stats package.
65 namespace Statistics
{
66 bool PrintDescriptions
= true;
67 DisplayMode DefaultMode
= mode_simplescalar
;
74 typedef list
<StatData
*> list_t
;
75 typedef map
<void *, StatData
*> map_t
;
84 void dump(ostream
&stream
, DisplayMode mode
);
85 void display(ostream
&stream
, DisplayMode mode
);
87 StatData
*find(void *stat
);
88 void mapStat(void *stat
, StatData
*data
);
92 void regBin(MainBin
*bin
, string name
);
93 void regPrint(void *stat
);
95 static std::string
name() { return "Statistics Database"; }
100 Data::dump(ostream
&stream
, DisplayMode mode
)
102 MainBin
*orig
= MainBin::curBin();
106 case mode_simplescalar
:
107 display(stream
, mode
);
110 warn("invalid display mode!\n");
111 display(stream
, mode_m5
);
120 Data::display(ostream
&stream
, DisplayMode mode
)
123 list
<MainBin
*>::iterator i
= bins
.begin();
124 list
<MainBin
*>::iterator bins_end
= bins
.end();
125 ccprintf(stream
, "PRINTING BINNED STATS\n");
126 while (i
!= bins_end
) {
128 ccprintf(stream
,"---%s Bin------------\n", (*i
)->name());
130 list_t::iterator j
= printStats
.begin();
131 list_t::iterator end
= printStats
.end();
134 if (stat
->dodisplay())
135 stat
->display(stream
, mode
);
139 ccprintf(stream
, "---------------------------------\n");
142 list_t::iterator i
= printStats
.begin();
143 list_t::iterator end
= printStats
.end();
146 if (stat
->dodisplay() && !stat
->binned())
147 stat
->display(stream
, mode
);
154 Data::find(void *stat
)
156 map_t::const_iterator i
= statMap
.find(stat
);
158 if (i
== statMap
.end())
167 list_t::iterator i
= allStats
.begin();
168 list_t::iterator end
= allStats
.end();
177 i
= allStats
.begin();
181 if (!(data
->flags
& print
))
182 data
->name
= "__Stat" + to_string(j
++);
190 // reset non-binned stats
191 list_t::iterator i
= allStats
.begin();
192 list_t::iterator end
= allStats
.end();
200 // save the bin so we can go back to where we were
201 MainBin
*orig
= MainBin::curBin();
203 // reset binned stats
204 list
<MainBin
*>::iterator bi
= bins
.begin();
205 list
<MainBin
*>::iterator be
= bins
.end();
210 i
= allStats
.begin();
221 MainBin::curBin() = orig
;
225 Data::mapStat(void *stat
, StatData
*data
)
227 if (statMap
.find(stat
) != statMap
.end())
228 panic("shouldn't register stat twice!");
230 allStats
.push_back(data
);
235 (statMap
.insert(make_pair(stat
, data
))).second
;
236 assert(statMap
.find(stat
) != statMap
.end());
237 assert(success
&& "this should never fail");
241 Data::regBin(MainBin
*bin
, string _name
)
244 DPRINTF(Stats
, "registering %s\n", _name
);
248 Data::regPrint(void *stat
)
250 StatData
*data
= find(stat
);
252 if (data
->flags
& print
)
255 data
->flags
|= print
;
257 list_t::iterator j
= printStats
.insert(printStats
.end(), data
);
258 inplace_merge(printStats
.begin(), j
, printStats
.end(), StatData::less
);
271 DataAccess::find() const
273 return Database::StatDB().find(const_cast<void *>((const void *)this));
277 DataAccess::map(StatData
*data
)
279 Database::StatDB().mapStat(this, data
);
283 DataAccess::statData()
285 StatData
*ptr
= find();
291 DataAccess::statData() const
293 const StatData
*ptr
= find();
299 DataAccess::setInit()
301 statData()->flags
|= init
;
305 DataAccess::setPrint()
307 Database::StatDB().regPrint(this);
310 StatData::~StatData()
315 StatData::less(StatData
*stat1
, StatData
*stat2
)
317 const string
&name1
= stat1
->name
;
318 const string
&name2
= stat2
->name
;
323 tokenize(v1
, name1
, '.');
324 tokenize(v2
, name2
, '.');
326 int last
= min(v1
.size(), v2
.size()) - 1;
327 for (int i
= 0; i
< last
; ++i
)
329 return v1
[i
] < v2
[i
];
331 // Special compare for last element.
332 if (v1
[last
] == v2
[last
])
333 return v1
.size() < v2
.size();
335 return v1
[last
] < v2
[last
];
341 StatData::baseCheck() const
343 if (!(flags
& init
)) {
345 cprintf("this is stat number %d\n",(*i
)->number
);
347 panic("Not all stats have been initialized");
351 if ((flags
& print
) && name
.empty()) {
352 panic("all printable stats must be named");
360 ValueToString(result_t value
, DisplayMode mode
, int precision
)
366 val
.precision(precision
);
367 else if (value
== rint(value
))
370 val
.unsetf(ios::showpoint
);
371 val
.setf(ios::fixed
);
374 val
<< (mode
== mode_m5
? "no value" : "<err: div-0>");
392 : value(0.0), flags(0), mode(DefaultMode
), precision(0),
396 void operator()(ostream
&stream
) const;
400 ScalarPrint::operator()(ostream
&stream
) const
402 if (flags
& nozero
&& value
== 0.0 ||
403 flags
& nonan
&& isnan(value
))
406 stringstream pdfstr
, cdfstr
;
409 ccprintf(pdfstr
, "%.2f%%", pdf
* 100.0);
412 ccprintf(cdfstr
, "%.2f%%", cdf
* 100.0);
414 if (mode
== mode_simplescalar
&& flags
& __substat
) {
415 ccprintf(stream
, "%32s %12s %10s %10s", name
,
416 ValueToString(value
, mode
, precision
), pdfstr
, cdfstr
);
418 ccprintf(stream
, "%-40s %12s %10s %10s", name
,
419 ValueToString(value
, mode
, precision
), pdfstr
, cdfstr
);
422 if (PrintDescriptions
) {
424 ccprintf(stream
, " # %s", desc
);
433 vector
<string
> subnames
;
434 vector
<string
> subdescs
;
442 : subnames(0), subdescs(0), flags(0), mode(DefaultMode
),
443 precision(-1), total(NAN
)
446 void operator()(ostream
&stream
) const;
450 VectorPrint::operator()(std::ostream
&stream
) const
452 int _size
= vec
.size();
453 result_t _total
= 0.0;
455 if (flags
& (pdf
| cdf
)) {
456 for (int i
= 0; i
< _size
; ++i
) {
461 string base
= name
+ ((mode
== mode_simplescalar
) ? "_" : "::");
466 print
.precision
= precision
;
469 bool havesub
= !subnames
.empty();
472 print
.value
= vec
[0];
474 } else if (mode
== mode_m5
) {
475 for (int i
= 0; i
< _size
; ++i
) {
476 if (havesub
&& (i
>= subnames
.size() || subnames
[i
].empty()))
479 print
.name
= base
+ (havesub
? subnames
[i
] : to_string(i
));
480 print
.desc
= subdescs
.empty() ? desc
: subdescs
[i
];
481 print
.value
= vec
[i
];
483 if (_total
&& (flags
& pdf
)) {
484 print
.pdf
= vec
[i
] / _total
;
485 print
.cdf
+= print
.pdf
;
491 if (flags
& ::Statistics::total
) {
492 print
.name
= base
+ "total";
498 if (flags
& ::Statistics::total
) {
506 ccprintf(stream
, "%s.start_dist\n", name
);
507 for (int i
= 0; i
< _size
; ++i
) {
508 print
.name
= havesub
? subnames
[i
] : to_string(i
);
509 print
.desc
= subdescs
.empty() ? desc
: subdescs
[i
];
510 print
.flags
|= __substat
;
511 print
.value
= vec
[i
];
514 _pdf
= vec
[i
] / _total
;
525 ccprintf(stream
, "%s.end_dist\n", name
);
527 for (int i
= 0; i
< _size
; ++i
) {
528 if (havesub
&& subnames
[i
].empty())
532 print
.name
+= havesub
? subnames
[i
] : to_string(i
);
533 print
.desc
= subdescs
.empty() ? desc
: subdescs
[i
];
534 print
.value
= vec
[i
];
537 _pdf
= vec
[i
] / _total
;
577 void operator()(ostream
&stream
) const;
581 DistPrint::operator()(ostream
&stream
) const
585 string base
= name
+ ((mode
== mode_m5
) ? "::" : "_");
587 print
.precision
= precision
;
592 print
.name
= base
+ "mean";
593 print
.value
= samples
? sum
/ samples
: NAN
;
596 print
.name
= base
+ "stdev";
597 print
.value
= samples
? sqrt((samples
* squares
- sum
* sum
) /
598 (samples
* (samples
- 1.0))) : NAN
;
601 print
.name
= "**Ignore: " + base
+ "TOT";
602 print
.value
= samples
;
607 assert(size
== vec
.size());
609 result_t total
= 0.0;
612 for (int i
= 0; i
< size
; ++i
)
616 string base
= name
+ (mode
== mode_m5
? "::" : ".");
619 print
.desc
= (mode
== mode_m5
) ? desc
: "";
622 print
.precision
= precision
;
624 if (mode
== mode_simplescalar
) {
625 ccprintf(stream
, "%-42s", base
+ "start_dist");
626 if (PrintDescriptions
&& !desc
.empty())
627 ccprintf(stream
, " # %s", desc
);
631 print
.name
= base
+ "samples";
632 print
.value
= samples
;
635 print
.name
= base
+ "min_value";
636 print
.value
= min_val
;
639 if (mode
== mode_m5
|| underflow
> 0.0) {
640 print
.name
= base
+ "underflows";
641 print
.value
= underflow
;
642 if (mode
== mode_m5
&& total
) {
643 print
.pdf
= underflow
/ total
;
644 print
.cdf
+= print
.pdf
;
650 if (mode
== mode_m5
) {
651 for (int i
= 0; i
< size
; ++i
) {
652 stringstream namestr
;
655 int low
= i
* bucket_size
+ min
;
656 int high
= ::min((i
+ 1) * bucket_size
+ min
- 1, max
);
659 namestr
<< "-" << high
;
661 print
.name
= namestr
.str();
662 print
.value
= vec
[i
];
664 print
.pdf
= vec
[i
] / total
;
665 print
.cdf
+= print
.pdf
;
675 print
.flags
= flags
| __substat
;
677 for (int i
= 0; i
< size
; ++i
) {
678 if (flags
& nozero
&& vec
[i
] == 0.0 ||
679 flags
& nonan
&& isnan(vec
[i
]))
682 _min
= i
* bucket_size
+ min
;
683 _pdf
= vec
[i
] / total
* 100.0;
687 print
.name
= ValueToString(_min
, mode
, 0);
688 print
.value
= vec
[i
];
689 print
.pdf
= (flags
& pdf
) ? _pdf
: NAN
;
690 print
.cdf
= (flags
& cdf
) ? _cdf
: NAN
;
697 if (mode
== mode_m5
|| overflow
> 0.0) {
698 print
.name
= base
+ "overflows";
699 print
.value
= overflow
;
700 if (mode
== mode_m5
&& total
) {
701 print
.pdf
= overflow
/ total
;
702 print
.cdf
+= print
.pdf
;
713 if (mode
!= mode_simplescalar
) {
714 print
.name
= base
+ "total";
719 print
.name
= base
+ "max_value";
720 print
.value
= max_val
;
723 if (mode
!= mode_simplescalar
&& samples
!= 0) {
724 print
.name
= base
+ "mean";
725 print
.value
= sum
/ samples
;
728 print
.name
= base
+ "stdev";
729 print
.value
= sqrt((samples
* squares
- sum
* sum
) /
730 (samples
* (samples
- 1.0)));
734 if (mode
== mode_simplescalar
)
735 ccprintf(stream
, "%send_dist\n\n", base
);
739 ScalarDataBase::display(ostream
&stream
, DisplayMode mode
) const
747 print
.precision
= precision
;
753 VectorDataBase::display(ostream
&stream
, DisplayMode mode
) const
755 int size
= this->size();
756 const_cast<VectorDataBase
*>(this)->update();
764 print
.precision
= precision
;
766 print
.total
= total();
768 if (!subnames
.empty()) {
769 for (int i
= 0; i
< size
; ++i
) {
770 if (!subnames
[i
].empty()) {
771 print
.subnames
= subnames
;
772 print
.subnames
.resize(size
);
773 for (int i
= 0; i
< size
; ++i
) {
774 if (!subnames
[i
].empty() && !subdescs
[i
].empty()) {
775 print
.subdescs
= subdescs
;
776 print
.subdescs
.resize(size
);
789 Vector2dDataBase::display(ostream
&stream
, DisplayMode mode
) const
791 const_cast<Vector2dDataBase
*>(this)->update();
793 bool havesub
= false;
796 print
.subnames
= y_subnames
;
799 print
.precision
= precision
;
801 if (!subnames
.empty()) {
802 for (int i
= 0; i
< x
; ++i
)
803 if (!subnames
[i
].empty())
808 result_t super_total
= 0.0;
809 for (int i
= 0; i
< x
; ++i
) {
810 if (havesub
&& (i
>= subnames
.size() || subnames
[i
].empty()))
816 result_t total
= 0.0;
817 for (int j
= 0; j
< y
; ++j
) {
818 yvec
[j
] = vec
[iy
+ j
];
819 tot_vec
[j
] += yvec
[j
];
821 super_total
+= yvec
[j
];
824 print
.name
= name
+ "_" + (havesub
? subnames
[i
] : to_string(i
));
831 if ((flags
& ::Statistics::total
) && (x
> 1)) {
835 print
.total
= super_total
;
841 DistDataBase::display(ostream
&stream
, DisplayMode mode
) const
843 const_cast<DistDataBase
*>(this)->update();
851 print
.precision
= precision
;
853 print
.min_val
= data
.min_val
;
854 print
.max_val
= data
.max_val
;
855 print
.underflow
= data
.underflow
;
856 print
.overflow
= data
.overflow
;
857 print
.vec
= data
.vec
;
858 print
.sum
= data
.sum
;
859 print
.squares
= data
.squares
;
860 print
.samples
= data
.samples
;
862 print
.min
= data
.min
;
863 print
.max
= data
.max
;
864 print
.bucket_size
= data
.bucket_size
;
865 print
.size
= data
.size
;
866 print
.fancy
= data
.fancy
;
872 VectorDistDataBase::display(ostream
&stream
, DisplayMode mode
) const
874 const_cast<VectorDistDataBase
*>(this)->update();
876 for (int i
= 0; i
< size(); ++i
) {
880 (subnames
[i
].empty() ? ("_" + to_string(i
)) : subnames
[i
]);
881 print
.desc
= subdescs
[i
].empty() ? desc
: subdescs
[i
];
884 print
.precision
= precision
;
886 print
.min_val
= data
[i
].min_val
;
887 print
.max_val
= data
[i
].max_val
;
888 print
.underflow
= data
[i
].underflow
;
889 print
.overflow
= data
[i
].overflow
;
890 print
.vec
= data
[i
].vec
;
891 print
.sum
= data
[i
].sum
;
892 print
.squares
= data
[i
].squares
;
893 print
.samples
= data
[i
].samples
;
895 print
.min
= data
[i
].min
;
896 print
.max
= data
[i
].max
;
897 print
.bucket_size
= data
[i
].bucket_size
;
898 print
.size
= data
[i
].size
;
899 print
.fancy
= data
[i
].fancy
;
906 FormulaBase::val(rvec_t
&vec
) const
912 FormulaBase::total() const
914 return root
->total();
918 FormulaBase::size() const
927 FormulaBase::binned() const
929 return root
&& root
->binned();
938 FormulaBase::zero() const
942 for (int i
= 0; i
< vec
.size(); ++i
)
949 FormulaBase::update(StatData
*)
958 Formula::Formula(Temp r
)
965 Formula::operator=(Temp r
)
967 assert(!root
&& "Can't change formulas");
974 Formula::operator+=(Temp r
)
977 root
= NodePtr(new BinaryNode
<std::plus
<result_t
> >(root
, r
));
984 MainBin::MainBin(const string
&name
)
985 : _name(name
), mem(NULL
), memsize(-1)
987 Database::StatDB().regBin(this, name
);
997 MainBin::memory(off_t off
)
1000 memsize
= CeilPow2((size_t) offset());
1003 mem
= new char[memsize
];
1004 memset(mem
, 0, memsize
);
1007 assert(offset() <= size());
1014 Database::StatDB().check();
1018 dump(ostream
&stream
, DisplayMode mode
)
1020 Database::StatDB().dump(stream
, mode
);
1023 CallbackQueue resetQueue
;
1026 registerResetCallback(Callback
*cb
)
1034 Database::StatDB().reset();
1035 resetQueue
.process();
1038 } // namespace Statistics