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 "sim/universe.hh"
58 static int total_stats
= 0;
63 // This is a hack to get this parameter from the old stats package.
64 namespace Statistics
{
65 bool PrintDescriptions
= true;
66 DisplayMode default_mode
= mode_simplescalar
;
73 typedef list
<StatData
*> list_t
;
74 typedef map
<void *, StatData
*> map_t
;
77 map
<const MainBin
*, string
> bin_names
;
85 void dump(ostream
&stream
);
87 StatData
*find(void *stat
);
88 void mapStat(void *stat
, StatData
*data
);
92 void regBin(MainBin
*bin
, string name
);
93 void regPrint(void *stat
);
98 Data::dump(ostream
&stream
)
101 list_t::iterator i
= printStats
.begin();
102 list_t::iterator end
= printStats
.end();
106 binnedStats
.push_back(stat
);
111 list
<MainBin
*>::iterator j
= bins
.begin();
112 list
<MainBin
*>::iterator bins_end
=bins
.end();
115 ccprintf(stream
, "PRINTING BINNED STATS\n");
116 while (j
!= bins_end
) {
118 map
<const MainBin
*, string
>::const_iterator iter
;
119 iter
= bin_names
.find(*j
);
120 if (iter
== bin_names
.end())
121 panic("a binned stat not found in names map!");
122 ccprintf(stream
,"---%s Bin------------\n", (*iter
).second
);
125 list_t::iterator i
= printStats
.begin();
126 list_t::iterator end
= printStats
.end();
128 list_t::iterator i
= binnedStats
.begin();
129 list_t::iterator end
= binnedStats
.end();
133 if (stat
->dodisplay())
134 stat
->display(stream
);
138 ccprintf(stream
, "---------------------------------\n");
141 ccprintf(stream
, "**************ALL STATS************\n");
146 * get bin totals working, then print the stat here (as total), even if
147 * its' binned. (this is only for the case you selectively bin a few stats
150 list_t::iterator k
= printStats
.begin();
151 list_t::iterator endprint
= printStats
.end();
152 while (k
!= endprint
) {
154 if (stat
->dodisplay() /*&& !stat->binned()*/)
155 stat
->display(stream
);
162 Data::find(void *stat
)
164 map_t::const_iterator i
= statMap
.find(stat
);
166 if (i
== statMap
.end())
175 list_t::iterator i
= allStats
.begin();
176 list_t::iterator end
= allStats
.end();
189 list_t::iterator i
= allStats
.begin();
190 list_t::iterator end
= allStats
.end();
197 MainBin
*orig
= MainBin::curBin();
199 list
<MainBin
*>::iterator bi
= bins
.begin();
200 list
<MainBin
*>::iterator be
= bins
.end();
205 i
= allStats
.begin();
219 Data::mapStat(void *stat
, StatData
*data
)
221 if (statMap
.find(stat
) != statMap
.end())
222 panic("shouldn't register stat twice!");
224 allStats
.push_back(data
);
229 (statMap
.insert(make_pair(stat
, data
))).second
;
230 assert(statMap
.find(stat
) != statMap
.end());
231 assert(success
&& "this should never fail");
235 Data::regBin(MainBin
*bin
, string name
)
237 if (bin_names
.find(bin
) != bin_names
.end())
238 panic("shouldn't register bin twice");
245 (bin_names
.insert(make_pair(bin
,name
))).second
;
246 assert(bin_names
.find(bin
) != bin_names
.end());
247 assert(success
&& "this should not fail");
249 cprintf("registering %s\n", name
);
253 Data::regPrint(void *stat
)
255 StatData
*data
= find(stat
);
260 list_t::iterator j
= printStats
.insert(printStats
.end(), data
);
261 inplace_merge(printStats
.begin(), j
,
262 printStats
.end(), StatData::less
);
277 DataAccess::find() const
279 return Database::StatDB().find(const_cast<void *>((const void *)this));
283 DataAccess::map(StatData
*data
)
285 Database::StatDB().mapStat(this, data
);
289 DataAccess::statData()
291 StatData
*ptr
= find();
297 DataAccess::statData() const
299 const StatData
*ptr
= find();
305 DataAccess::setInit()
307 statData()->init
= true;
311 DataAccess::setPrint()
313 Database::StatDB().regPrint(this);
316 StatData::~StatData()
321 StatData::less(StatData
*stat1
, StatData
*stat2
)
323 const string
&name1
= stat1
->name
;
324 const string
&name2
= stat2
->name
;
329 tokenize(v1
, name1
, '.');
330 tokenize(v2
, name2
, '.');
332 int last
= min(v1
.size(), v2
.size()) - 1;
333 for (int i
= 0; i
< last
; ++i
)
335 return v1
[i
] < v2
[i
];
337 // Special compare for last element.
338 if (v1
[last
] == v2
[last
])
339 return v1
.size() < v2
.size();
341 return v1
[last
] < v2
[last
];
347 StatData::baseCheck() const
351 cprintf("this is stat number %d\n",(*i
)->number
);
353 panic("Not all stats have been initialized");
357 if (print
&& name
.empty()) {
358 panic("all printable stats must be named");
366 ValueToString(result_t value
, DisplayMode mode
, int precision
)
372 val
.precision(precision
);
373 else if (value
== rint(value
))
376 val
.unsetf(ios::showpoint
);
377 val
.setf(ios::fixed
);
380 val
<< (mode
== mode_m5
? "no value" : "<err: div-0>");
398 : value(0.0), precision(0), mode(default_mode
), flags(0),
402 void operator()(ostream
&stream
) const;
406 ScalarPrint::operator()(ostream
&stream
) const
408 if (flags
& nozero
&& value
== 0.0 ||
409 flags
& nonan
&& isnan(value
))
412 stringstream pdfstr
, cdfstr
;
415 ccprintf(pdfstr
, "%.2f%%", pdf
* 100.0);
418 ccprintf(cdfstr
, "%.2f%%", cdf
* 100.0);
420 if (mode
== mode_simplescalar
&& flags
& __substat
) {
421 ccprintf(stream
, "%32s %12s %10s %10s", name
,
422 ValueToString(value
, mode
, precision
),
425 ccprintf(stream
, "%-40s %12s %10s %10s", name
,
426 ValueToString(value
, mode
, precision
), pdfstr
, cdfstr
);
429 if (PrintDescriptions
) {
431 ccprintf(stream
, " # %s", desc
);
440 vector
<string
> subnames
;
441 vector
<string
> subdescs
;
449 : subnames(0), subdescs(0), precision(-1), mode(default_mode
),
453 void operator()(ostream
&stream
) const;
457 VectorPrint::operator()(std::ostream
&stream
) const
459 int _size
= vec
.size();
460 result_t _total
= 0.0;
462 if (flags
& (pdf
| cdf
)) {
463 for (int i
= 0; i
< _size
; ++i
) {
468 string base
= name
+ ((mode
== mode_simplescalar
) ? "_" : "::");
473 print
.precision
= precision
;
476 bool havesub
= !subnames
.empty();
479 print
.value
= vec
[0];
481 } else if (mode
== mode_m5
) {
482 for (int i
= 0; i
< _size
; ++i
) {
483 if (havesub
&& (i
>= subnames
.size() || subnames
[i
].empty()))
486 print
.name
= base
+ (havesub
? subnames
[i
] : to_string(i
));
487 print
.desc
= subdescs
.empty() ? desc
: subdescs
[i
];
488 print
.value
= vec
[i
];
490 if (_total
&& (flags
& pdf
)) {
491 print
.pdf
= vec
[i
] / _total
;
492 print
.cdf
+= print
.pdf
;
498 if (flags
& ::Statistics::total
) {
499 print
.name
= base
+ "total";
505 if (flags
& ::Statistics::total
) {
513 ccprintf(stream
, "%s.start_dist\n", name
);
514 for (int i
= 0; i
< _size
; ++i
) {
515 print
.name
= havesub
? subnames
[i
] : to_string(i
);
516 print
.desc
= subdescs
.empty() ? desc
: subdescs
[i
];
517 print
.flags
|= __substat
;
518 print
.value
= vec
[i
];
521 _pdf
= vec
[i
] / _total
;
532 ccprintf(stream
, "%s.end_dist\n", name
);
534 for (int i
= 0; i
< _size
; ++i
) {
535 if (havesub
&& subnames
[i
].empty())
539 print
.name
+= havesub
? subnames
[i
] : to_string(i
);
540 print
.desc
= subdescs
.empty() ? desc
: subdescs
[i
];
541 print
.value
= vec
[i
];
544 _pdf
= vec
[i
] / _total
;
584 void operator()(ostream
&stream
) const;
588 DistPrint::operator()(ostream
&stream
) const
592 string base
= name
+ ((mode
== mode_m5
) ? "::" : "_");
594 print
.precision
= precision
;
598 print
.name
= base
+ "mean";
599 print
.value
= samples
? sum
/ samples
: NAN
;
602 print
.name
= base
+ "stdev";
603 print
.value
= samples
? sqrt((samples
* squares
- sum
* sum
) /
604 (samples
* (samples
- 1.0))) : NAN
;
607 print
.name
= "**Ignore: " + base
+ "TOT";
608 print
.value
= samples
;
613 assert(size
== vec
.size());
615 result_t total
= 0.0;
618 for (int i
= 0; i
< size
; ++i
)
622 string base
= name
+ (mode
== mode_m5
? "::" : ".");
625 print
.desc
= (mode
== mode_m5
) ? desc
: "";
626 print
.precision
= precision
;
630 if (mode
== mode_simplescalar
) {
631 ccprintf(stream
, "%-42s", base
+ "start_dist");
632 if (PrintDescriptions
&& !desc
.empty())
633 ccprintf(stream
, " # %s", desc
);
637 print
.name
= base
+ "samples";
638 print
.value
= samples
;
641 print
.name
= base
+ "min_value";
642 print
.value
= min_val
;
645 if (mode
== mode_m5
|| underflow
> 0.0) {
646 print
.name
= base
+ "underflows";
647 print
.value
= underflow
;
648 if (mode
== mode_m5
&& total
) {
649 print
.pdf
= underflow
/ total
;
650 print
.cdf
+= print
.pdf
;
656 if (mode
== mode_m5
) {
657 for (int i
= 0; i
< size
; ++i
) {
658 stringstream namestr
;
661 int low
= i
* bucket_size
+ min
;
662 int high
= ::min((i
+ 1) * bucket_size
+ min
- 1, max
);
665 namestr
<< "-" << high
;
667 print
.name
= namestr
.str();
668 print
.value
= vec
[i
];
670 print
.pdf
= vec
[i
] / total
;
671 print
.cdf
+= print
.pdf
;
681 print
.flags
= flags
| __substat
;
683 for (int i
= 0; i
< size
; ++i
) {
684 if (flags
& nozero
&& vec
[i
] == 0.0 ||
685 flags
& nonan
&& isnan(vec
[i
]))
688 _min
= i
* bucket_size
+ min
;
689 _pdf
= vec
[i
] / total
* 100.0;
693 print
.name
= ValueToString(_min
, mode
, 0);
694 print
.value
= vec
[i
];
695 print
.pdf
= (flags
& pdf
) ? _pdf
: NAN
;
696 print
.cdf
= (flags
& cdf
) ? _cdf
: NAN
;
701 if (flags
& (pdf
|| cdf
)) {
707 if (mode
== mode_m5
|| overflow
> 0.0) {
708 print
.name
= base
+ "overflows";
709 print
.value
= overflow
;
710 if (mode
== mode_m5
&& total
) {
711 print
.pdf
= overflow
/ total
;
712 print
.cdf
+= print
.pdf
;
720 if (mode
!= mode_simplescalar
) {
721 print
.name
= base
+ "total";
726 print
.name
= base
+ "max_value";
727 print
.value
= max_val
;
730 if (mode
!= mode_simplescalar
&& samples
!= 0) {
731 print
.name
= base
+ "mean";
732 print
.value
= sum
/ samples
;
735 print
.name
= base
+ "stdev";
736 print
.value
= sqrt((samples
* squares
- sum
* sum
) /
737 (samples
* (samples
- 1.0)));
741 if (mode
== mode_simplescalar
)
742 ccprintf(stream
, "%send_dist\n\n", base
);
746 ScalarDataBase::display(ostream
&stream
) const
752 print
.precision
= precision
;
759 VectorDataBase::display(ostream
&stream
) const
761 int size
= this->size();
762 const_cast<VectorDataBase
*>(this)->update();
770 print
.precision
= precision
;
772 print
.total
= total();
774 for (int i
= 0; i
< size
; ++i
) {
775 if (!subnames
[i
].empty()) {
776 print
.subnames
= subnames
;
777 print
.subnames
.resize(size
);
778 for (int i
= 0; i
< size
; ++i
) {
779 if (!subnames
[i
].empty() && !subdescs
[i
].empty()) {
780 print
.subdescs
= subdescs
;
781 print
.subdescs
.resize(size
);
794 Vector2dDataBase::display(ostream
&stream
) const
796 const_cast<Vector2dDataBase
*>(this)->update();
798 bool havesub
= false;
801 print
.subnames
= y_subnames
;
804 print
.precision
= precision
;
806 if (!subnames
.empty()) {
807 for (int i
= 0; i
< x
; ++i
)
808 if (!subnames
[i
].empty())
813 result_t super_total
= 0.0;
814 for (int i
= 0; i
< x
; ++i
) {
815 if (havesub
&& (i
>= subnames
.size() || subnames
[i
].empty()))
821 result_t total
= 0.0;
822 for (int j
= 0; j
< y
; ++j
) {
823 yvec
[j
] = vec
[iy
+ j
];
824 tot_vec
[j
] += yvec
[j
];
826 super_total
+= yvec
[j
];
829 print
.name
= name
+ "_" + (havesub
? subnames
[i
] : to_string(i
));
836 if ((flags
& ::Statistics::total
) && (x
> 1)) {
840 print
.total
= super_total
;
846 DistDataBase::display(ostream
&stream
) const
848 const_cast<DistDataBase
*>(this)->update();
854 print
.precision
= precision
;
858 print
.min_val
= data
.min_val
;
859 print
.max_val
= data
.max_val
;
860 print
.underflow
= data
.underflow
;
861 print
.overflow
= data
.overflow
;
862 print
.vec
= data
.vec
;
863 print
.sum
= data
.sum
;
864 print
.squares
= data
.squares
;
865 print
.samples
= data
.samples
;
867 print
.min
= data
.min
;
868 print
.max
= data
.max
;
869 print
.bucket_size
= data
.bucket_size
;
870 print
.size
= data
.size
;
871 print
.fancy
= data
.fancy
;
877 VectorDistDataBase::display(ostream
&stream
) const
879 const_cast<VectorDistDataBase
*>(this)->update();
881 for (int i
= 0; i
< size(); ++i
) {
885 (subnames
[i
].empty() ? ("_" + to_string(i
)) : subnames
[i
]);
886 print
.desc
= subdescs
[i
].empty() ? desc
: subdescs
[i
];
887 print
.precision
= precision
;
891 print
.min_val
= data
[i
].min_val
;
892 print
.max_val
= data
[i
].max_val
;
893 print
.underflow
= data
[i
].underflow
;
894 print
.overflow
= data
[i
].overflow
;
895 print
.vec
= data
[i
].vec
;
896 print
.sum
= data
[i
].sum
;
897 print
.squares
= data
[i
].squares
;
898 print
.samples
= data
[i
].samples
;
900 print
.min
= data
[i
].min
;
901 print
.max
= data
[i
].max
;
902 print
.bucket_size
= data
[i
].bucket_size
;
903 print
.size
= data
[i
].size
;
904 print
.fancy
= data
[i
].fancy
;
911 FormulaBase::val(rvec_t
&vec
) const
917 FormulaBase::total() const
919 return root
->total();
923 FormulaBase::size() const
932 FormulaBase::binned() const
934 return root
->binned();
943 FormulaBase::zero() const
947 for (int i
= 0; i
< vec
.size(); ++i
)
954 FormulaBase::update(StatData
*)
963 Formula::Formula(Temp r
)
970 Formula::operator=(Temp r
)
972 assert(!root
&& "Can't change formulas");
979 Formula::operator+=(Temp r
)
982 root
= NodePtr(new BinaryNode
<std::plus
<result_t
> >(root
, r
));
989 MainBin::MainBin(const string
&name
)
990 : _name(name
), mem(NULL
), memsize(-1)
992 Database::StatDB().regBin(this, name
);
1002 MainBin::memory(off_t off
)
1005 memsize
= CeilPow2((size_t) offset());
1008 mem
= new char[memsize
];
1009 memset(mem
, 0, memsize
);
1012 assert(offset() <= size());
1019 Database::StatDB().check();
1023 dump(ostream
&stream
)
1025 Database::StatDB().dump(stream
);
1028 CallbackQueue resetQueue
;
1031 registerResetCallback(Callback
*cb
)
1039 Database::StatDB().reset();
1040 resetQueue
.process();
1043 } // namespace Statistics