Fully Document Statistics::Detail::Stat. More to follow.
[gem5.git] / base / statistics.hh
1 /*
2 * Copyright (c) 2003 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 */
28
29 /** @file
30 * Declaration of Statistics objects.
31 */
32
33 /**
34 * @todo
35 *
36 * Generalized N-dimensinal vector
37 * documentation
38 * fix AvgStor
39 * key stats
40 * interval stats
41 * -- these both can use the same function that prints out a
42 * specific set of stats
43 * VectorStandardDeviation totals
44 * Document Namespaces
45 */
46 #ifndef __STATISTICS_HH__
47 #define __STATISTICS_HH__
48
49 #include <algorithm>
50 #include <functional>
51 #include <iosfwd>
52 #include <sstream>
53 #include <string>
54 #include <vector>
55
56 #include <assert.h>
57
58 #include "host.hh"
59 #include "refcnt.hh"
60 #include "str.hh"
61
62 #ifndef NAN
63 float __nan();
64 /** Define Not a number. */
65 #define NAN (__nan())
66 /** Need to define __nan() */
67 #define __M5_NAN
68 #endif
69
70 /** Print stats out in SS format. */
71 #define STAT_DISPLAY_COMPAT
72
73 /** The current simulated cycle. */
74 extern Tick curTick;
75
76 /* A namespace for all of the Statistics */
77 namespace Statistics {
78 /** All results are doubles. */
79 typedef double result_t;
80 /** A vector to hold results. */
81 typedef std::vector<result_t> rvec_t;
82
83 /**
84 * Define the storage for format flags.
85 * @todo Can probably shrink this.
86 */
87 typedef u_int32_t FormatFlags;
88 /** Nothing extra to print. */
89 const FormatFlags none = 0x0000;
90 /** Print the total. */
91 const FormatFlags total = 0x0001;
92 /** Print the percent of the total that this entry represents. */
93 const FormatFlags pdf = 0x0002;
94 /** Don't print if this is zero. */
95 const FormatFlags nozero = 0x0004;
96 /** Don't print if this is NAN */
97 const FormatFlags nonan = 0x0008;
98 /** Print the cumulative percentage of total upto this entry. */
99 const FormatFlags cdf = 0x0010;
100 /** Print the distribution. */
101 const FormatFlags dist = 0x0020;
102 /** Used for SS compatability. */
103 const FormatFlags __substat = 0x8000;
104 /** Mask of flags that can't be set directly */
105 const FormatFlags __reserved = __substat;
106
107 /* Contains the statistic implementation details */
108 namespace Detail {
109 //////////////////////////////////////////////////////////////////////
110 //
111 // Statistics Framework Base classes
112 //
113 //////////////////////////////////////////////////////////////////////
114 struct StatData;
115 struct SubData;
116
117 /**
118 * Common base class for all statistics, used to maintain a list and print.
119 * This class holds no data itself but is used to find the associated
120 * StatData in the stat database @sa Statistics::Database.
121 */
122 class Stat
123 {
124 protected:
125 /** Mark this statistics as initialized. */
126 void setInit();
127 /**
128 * Finds and returns the associated StatData from the database.
129 * @return The formatting and output data of this statistic.
130 */
131 StatData *mydata();
132 /**
133 * Finds and returns a const pointer to the associated StatData.
134 * @return The formatting and output data of this statistic.
135 */
136 const StatData *mydata() const;
137 /**
138 * Mark this stat for output at the end of simulation.
139 * @return The formatting and output data of this statistic.
140 */
141 StatData *print();
142 /**
143 * Finds and returns the SubData at the given index.
144 * @param index The index of the SubData to find.
145 * @return The name and description of the given index.
146 */
147 const SubData *mysubdata(int index) const;
148 /**
149 * Create and return a new SubData field for the given index.
150 * @param index The index to create a SubData for.
151 * @return A pointer to the created SubData.
152 */
153 SubData *mysubdata_create(int index);
154
155 public:
156 /**
157 * Return the name of this stat.
158 * @return the name of the stat.
159 */
160 virtual std::string myname() const;
161 /**
162 * Return the name of the sub field at the given index.
163 * @param index the subfield index.
164 * @return the name of the subfield.
165 */
166 virtual std::string mysubname(int index) const;
167 /**
168 * Return the description of this stat.
169 * @return the description of this stat.
170 */
171 virtual std::string mydesc() const;
172 /**
173 * Return the description of the subfield at the given index.
174 * @param index The subfield index.
175 * @return the description of the subfield.
176 */
177 virtual std::string mysubdesc(int index) const;
178 /**
179 * Return the format flags of this stat.
180 * @return the format flags.
181 */
182 virtual FormatFlags myflags() const;
183 /**
184 * Return true if this stat's prereqs have been satisfied (they are non
185 * zero).
186 * @return true if the prerequisite stats aren't zero.
187 */
188 virtual bool dodisplay() const;
189 /**
190 * Return the display percision.
191 * @return The display precision.
192 */
193 virtual int myprecision() const;
194
195 public:
196 /**
197 * Create this stat and register it if reg is true.
198 * @param reg Register this stat in the database?
199 */
200 Stat(bool reg);
201 /**
202 * Destructor
203 */
204 virtual ~Stat() {}
205
206 /**
207 * Print this stat to the given ostream.
208 * @param stream The stream to print to.
209 */
210 virtual void display(std::ostream &stream) const = 0;
211 /**
212 * Return the number of entries in this stat.
213 * @return The number of entries.
214 */
215 virtual size_t size() const = 0;
216 /**
217 * Return true if the stat has value zero.
218 * @return True if the stat is zero.
219 */
220 virtual bool zero() const = 0;
221
222
223 /**
224 * Set the name and marks this stat to print at the end of simulation.
225 * @param name The new name.
226 * @return A reference to this stat.
227 */
228 Stat &name(const std::string &name);
229 /**
230 * Set the description and marks this stat to print at the end of
231 * simulation.
232 * @param desc The new description.
233 * @return A reference to this stat.
234 */
235 Stat &desc(const std::string &desc);
236 /**
237 * Set the precision and marks this stat to print at the end of simulation.
238 * @param p The new precision
239 * @return A reference to this stat.
240 */
241 Stat &precision(int p);
242 /**
243 * Set the flags and marks this stat to print at the end of simulation.
244 * @param f The new flags.
245 * @return A reference to this stat.
246 */
247 Stat &flags(FormatFlags f);
248 /**
249 * Set the prerequisite stat and marks this stat to print at the end of
250 * simulation.
251 * @param prereq The prerequisite stat.
252 * @return A reference to this stat.
253 */
254 Stat &prereq(const Stat &prereq);
255 /**
256 * Set the subfield name for the given index, and marks this stat to print
257 * at the end of simulation.
258 * @param index The subfield index.
259 * @param name The new name of the subfield.
260 * @return A reference to this stat.
261 */
262 Stat &subname(int index, const std::string &name);
263 /**
264 * Set the subfield description for the given index and marks this stat to
265 * print at the end of simulation.
266 * @param index The subfield index.
267 * @param desc The new description of the subfield
268 * @return A reference to this stat.
269 */
270 Stat &subdesc(int index, const std::string &desc);
271
272 public:
273 /**
274 * Checks if the first stat's name is alphabetically less than the second.
275 * This function breaks names up at periods and considers each subname
276 * separately.
277 * @param stat1 The first stat.
278 * @param stat2 The second stat.
279 * @return stat1's name is alphabetically before stat2's
280 */
281 static bool less(Stat *stat1, Stat *stat2);
282
283 #ifdef STAT_DEBUG
284 /** A unique ID used for debugging. */
285 int number;
286 #endif
287 };
288
289 // Scalar stats involved in formulas
290 class ScalarStat : public Stat
291 {
292 public:
293 ScalarStat(bool reg) : Stat(reg) {}
294 virtual result_t val() const = 0;
295 virtual bool zero() const;
296 virtual void display(std::ostream &stream) const;
297 };
298
299 void
300 VectorDisplay(std::ostream &stream, const std::string &myname,
301 const std::vector<std::string> *mysubnames,
302 const std::string &mydesc,
303 const std::vector<std::string> *mysubdescs,
304 int myprecision, FormatFlags myflags, const rvec_t &vec,
305 result_t mytotal);
306
307 // Vector stats involved in formulas
308 class VectorStat : public Stat
309 {
310 public:
311 VectorStat(bool reg) : Stat(reg) {}
312 virtual const rvec_t &val() const = 0;
313 virtual result_t total() const = 0;
314 virtual bool zero() const;
315 virtual void display(std::ostream &stream) const;
316 };
317
318 //////////////////////////////////////////////////////////////////////
319 //
320 // Simple Statistics
321 //
322 //////////////////////////////////////////////////////////////////////
323 template <typename T>
324 struct StatStor
325 {
326 public:
327 struct Params { };
328
329 private:
330 T data;
331
332 public:
333 StatStor(const Params &) : data(T()) {}
334
335 void set(T val, const Params &p) { data = val; }
336 void inc(T val, const Params &p) { data += val; }
337 void dec(T val, const Params &p) { data -= val; }
338 result_t val(const Params &p) const { return (result_t)data; }
339 T value(const Params &p) const { return data; }
340 };
341
342 template <typename T>
343 struct AvgStor
344 {
345 public:
346 struct Params { };
347
348 private:
349 T current;
350 mutable result_t total;
351 mutable Tick last;
352
353 public:
354 AvgStor(const Params &) : current(T()), total(0), last(0) { }
355
356 void set(T val, const Params &p) {
357 total += current * (curTick - last);
358 last = curTick;
359 current = val;
360 }
361 void inc(T val, const Params &p) { set(current + val, p); }
362 void dec(T val, const Params &p) { set(current - val, p); }
363 result_t val(const Params &p) const {
364 total += current * (curTick - last);
365 last = curTick;
366 return (result_t)(total + current) / (result_t)(curTick + 1);
367 }
368 T value(const Params &p) const { return current; }
369 };
370
371 template <typename T, template <typename T> class Storage, class Bin>
372 class ScalarBase : public ScalarStat
373 {
374 protected:
375 typedef Storage<T> storage_t;
376 typedef typename storage_t::Params params_t;
377 typedef typename Bin::Bin<storage_t> bin_t;
378
379 protected:
380 bin_t bin;
381 params_t params;
382
383 protected:
384 storage_t *data() { return bin.data(params); }
385 const storage_t *data() const {
386 return (const_cast<bin_t *>(&bin))->data(params);
387 }
388
389 protected:
390 // Copying stats is not allowed
391 ScalarBase(const ScalarBase &stat);
392 const ScalarBase &operator=(const ScalarBase &);
393
394 public:
395 result_t val() const { return data()->val(params); }
396 T value() const { return data()->value(params); }
397
398 public:
399 ScalarBase() : ScalarStat(true) {
400 bin.init(params);
401 setInit();
402 }
403
404 public:
405 // Common operators for stats
406 void operator++() { data()->inc(1, params); }
407 void operator--() { data()->dec(1, params); }
408
409 void operator++(int) { ++*this; }
410 void operator--(int) { --*this; }
411
412 template <typename U>
413 void operator=(const U& v) { data()->set(v, params); }
414
415 template <typename U>
416 void operator+=(const U& v) { data()->inc(v, params); }
417
418 template <typename U>
419 void operator-=(const U& v) { data()->dec(v, params); }
420
421 virtual size_t size() const { return 1; }
422 };
423
424 //////////////////////////////////////////////////////////////////////
425 //
426 // Vector Statistics
427 //
428 //////////////////////////////////////////////////////////////////////
429 template <typename T, template <typename T> class Storage, class Bin>
430 class ScalarProxy;
431
432 template <typename T, template <typename T> class Storage, class Bin>
433 class VectorBase : public VectorStat
434 {
435 protected:
436 typedef Storage<T> storage_t;
437 typedef typename storage_t::Params params_t;
438 typedef typename Bin::VectorBin<storage_t> bin_t;
439
440 private:
441 mutable rvec_t *vec;
442
443 protected:
444 bin_t bin;
445 params_t params;
446
447 protected:
448 storage_t *data(int index) { return bin.data(index, params); }
449 const storage_t *data(int index) const {
450 return (const_cast<bin_t *>(&bin))->data(index, params);
451 }
452
453 protected:
454 // Copying stats is not allowed
455 VectorBase(const VectorBase &stat);
456 const VectorBase &operator=(const VectorBase &);
457
458 public:
459 const rvec_t &val() const {
460 if (vec)
461 vec->resize(size());
462 else
463 vec = new rvec_t(size());
464
465 for (int i = 0; i < size(); ++i)
466 (*vec)[i] = data(i)->val(params);
467
468 return *vec;
469 }
470
471 result_t total() const {
472 result_t total = 0.0;
473 for (int i = 0; i < size(); ++i)
474 total += data(i)->val(params);
475 return total;
476 }
477
478 public:
479 VectorBase() : VectorStat(true), vec(NULL) {}
480 ~VectorBase() { if (vec) delete vec; }
481
482 VectorBase &init(size_t size) {
483 bin.init(size, params);
484 setInit();
485
486 return *this;
487 }
488
489 friend class ScalarProxy<T, Storage, Bin>;
490 ScalarProxy<T, Storage, Bin> operator[](int index);
491
492 virtual size_t size() const { return bin.size(); }
493 };
494
495 template <typename T, template <typename T> class Storage, class Bin>
496 class ScalarProxy : public ScalarStat
497 {
498 protected:
499 typedef Storage<T> storage_t;
500 typedef typename storage_t::Params params_t;
501 typedef typename Bin::VectorBin<storage_t> bin_t;
502
503 private:
504 bin_t *bin;
505 params_t *params;
506 int index;
507
508 protected:
509 storage_t *data() { return bin->data(index, *params); }
510 const storage_t *data() const { return bin->data(index, *params); }
511
512 public:
513 result_t val() const { return data()->val(*params); }
514 T value() const { return data()->value(*params); }
515
516 public:
517 ScalarProxy(bin_t &b, params_t &p, int i)
518 : ScalarStat(false), bin(&b), params(&p), index(i) {}
519 ScalarProxy(const ScalarProxy &sp)
520 : ScalarStat(false), bin(sp.bin), params(sp.params), index(sp.index) {}
521 const ScalarProxy &operator=(const ScalarProxy &sp) {
522 bin = sp.bin;
523 params = sp.params;
524 index = sp.index;
525 return *this;
526 }
527
528 public:
529 // Common operators for stats
530 void operator++() { data()->inc(1, *params); }
531 void operator--() { data()->dec(1, *params); }
532
533 void operator++(int) { ++*this; }
534 void operator--(int) { --*this; }
535
536 template <typename U>
537 void operator=(const U& v) { data()->set(v, *params); }
538
539 template <typename U>
540 void operator+=(const U& v) { data()->inc(v, *params); }
541
542 template <typename U>
543 void operator-=(const U& v) { data()->dec(v, *params); }
544
545 virtual size_t size() const { return 1; }
546 };
547
548 template <typename T, template <typename T> class Storage, class Bin>
549 inline ScalarProxy<T, Storage, Bin>
550 VectorBase<T, Storage, Bin>::operator[](int index)
551 {
552 assert (index >= 0 && index < size());
553 return ScalarProxy<T, Storage, Bin>(bin, params, index);
554 }
555
556 template <typename T, template <typename T> class Storage, class Bin>
557 class VectorProxy;
558
559 template <typename T, template <typename T> class Storage, class Bin>
560 class Vector2dBase : public Stat
561 {
562 protected:
563 typedef Storage<T> storage_t;
564 typedef typename storage_t::Params params_t;
565 typedef typename Bin::VectorBin<storage_t> bin_t;
566
567 protected:
568 size_t x;
569 size_t y;
570 bin_t bin;
571 params_t params;
572 std::vector<std::string> *y_subnames;
573
574 protected:
575 storage_t *data(int index) { return bin.data(index, params); }
576 const storage_t *data(int index) const {
577 return (const_cast<bin_t *>(&bin))->data(index, params);
578 }
579
580 protected:
581 // Copying stats is not allowed
582 Vector2dBase(const Vector2dBase &stat);
583 const Vector2dBase &operator=(const Vector2dBase &);
584
585 public:
586 Vector2dBase() : Stat(true) {}
587 ~Vector2dBase() { }
588
589 Vector2dBase &init(size_t _x, size_t _y) {
590 x = _x;
591 y = _y;
592 bin.init(x * y, params);
593 setInit();
594 y_subnames = new std::vector<std::string>(y);
595
596 return *this;
597 }
598
599 /**
600 * This makes the assumption that if you're gonna subnames a 2d vector,
601 * you're subnaming across all y
602 */
603 Vector2dBase &ysubnames(const char **names)
604 {
605 for (int i=0; i < y; ++i) {
606 (*y_subnames)[i] = names[i];
607 }
608 return *this;
609 }
610 Vector2dBase &ysubname(int index, const std::string subname)
611 {
612 (*y_subnames)[i] = subname.c_str();
613 return *this;
614 }
615 std::string ysubname(int i) const { return (*y_subnames)[i]; }
616
617 friend class VectorProxy<T, Storage, Bin>;
618 VectorProxy<T, Storage, Bin> operator[](int index);
619
620 virtual size_t size() const { return bin.size(); }
621 virtual bool zero() const { return data(0)->value(params) == 0.0; }
622
623 virtual void
624 display(std::ostream &out) const
625 {
626 bool have_subname = false;
627 for (int i = 0; i < x; ++i) {
628 if (!mysubname(i).empty())
629 have_subname = true;
630 }
631
632 rvec_t tot_vec(y);
633 result_t super_total = 0.0;
634 for (int i = 0; i < x; ++i) {
635 std::string subname;
636 if (have_subname) {
637 subname = mysubname(i);
638 if (subname.empty())
639 continue;
640 } else
641 subname = to_string(i);
642
643 int iy = i * y;
644 rvec_t vec(y);
645
646 result_t total = 0.0;
647 for (int j = 0; j < y; ++j) {
648 vec[j] = data(iy + j)->val(params);
649 tot_vec[j] += vec[j];
650 total += vec[j];
651 super_total += vec[j];
652 }
653
654 std::string desc;
655 if (mysubdesc(i).empty()) {
656 desc = mydesc();
657 } else {
658 desc = mysubdesc(i);
659 }
660
661 VectorDisplay(out, myname() + "_" + subname, y_subnames, desc, 0,
662 myprecision(), myflags(), vec, total);
663
664 }
665 if ((myflags() & ::Statistics::total) && (x > 1)) {
666 VectorDisplay(out, myname(), y_subnames, mydesc(), 0,
667 myprecision(), myflags(), tot_vec, super_total);
668
669 }
670 }
671 };
672
673 template <typename T, template <typename T> class Storage, class Bin>
674 class VectorProxy : public VectorStat
675 {
676 protected:
677 typedef Storage<T> storage_t;
678 typedef typename storage_t::Params params_t;
679 typedef typename Bin::VectorBin<storage_t> bin_t;
680
681 private:
682 bin_t *bin;
683 params_t *params;
684 int offset;
685 int len;
686
687 private:
688 mutable rvec_t *vec;
689
690 storage_t *data(int index) {
691 assert(index < len);
692 return bin->data(offset + index, *params);
693 }
694
695 const storage_t *data(int index) const {
696 return (const_cast<bin_t *>(bin))->data(offset + index, *params);
697 }
698
699 public:
700 const rvec_t &val() const {
701 if (vec)
702 vec->resize(size());
703 else
704 vec = new rvec_t(size());
705
706 for (int i = 0; i < size(); ++i)
707 (*vec)[i] = data(i)->val(*params);
708
709 return *vec;
710 }
711
712 result_t total() const {
713 result_t total = 0.0;
714 for (int i = 0; i < size(); ++i)
715 total += data(i)->val(*params);
716 return total;
717 }
718
719 public:
720 VectorProxy(bin_t &b, params_t &p, int o, int l)
721 : VectorStat(false), bin(&b), params(&p), offset(o), len(l), vec(NULL)
722 { }
723 VectorProxy(const VectorProxy &sp)
724 : VectorStat(false), bin(sp.bin), params(sp.params), offset(sp.offset),
725 len(sp.len), vec(NULL)
726 { }
727 ~VectorProxy() {
728 if (vec)
729 delete vec;
730 }
731
732 const VectorProxy &operator=(const VectorProxy &sp) {
733 bin = sp.bin;
734 params = sp.params;
735 offset = sp.offset;
736 len = sp.len;
737 if (vec)
738 delete vec;
739 vec = NULL;
740 return *this;
741 }
742
743 virtual size_t size() const { return len; }
744
745 ScalarProxy<T, Storage, Bin> operator[](int index) {
746 assert (index >= 0 && index < size());
747 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index);
748 }
749 };
750
751 template <typename T, template <typename T> class Storage, class Bin>
752 inline VectorProxy<T, Storage, Bin>
753 Vector2dBase<T, Storage, Bin>::operator[](int index)
754 {
755 int offset = index * y;
756 assert (index >= 0 && offset < size());
757 return VectorProxy<T, Storage, Bin>(bin, params, offset, y);
758 }
759
760 //////////////////////////////////////////////////////////////////////
761 //
762 // Non formula statistics
763 //
764 //////////////////////////////////////////////////////////////////////
765
766 void DistDisplay(std::ostream &stream, const std::string &name,
767 const std::string &desc, int precision, FormatFlags flags,
768 result_t min_val, result_t max_val,
769 result_t underflow, result_t overflow,
770 const rvec_t &vec, int min, int max, int bucket_size,
771 int size);
772
773 template <typename T>
774 struct DistStor
775 {
776 public:
777 struct Params
778 {
779 int min;
780 int max;
781 int bucket_size;
782 int size;
783 };
784
785 private:
786 T min_val;
787 T max_val;
788 T underflow;
789 T overflow;
790 std::vector<T> vec;
791
792 public:
793 DistStor(const Params &params)
794 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
795 vec(params.size) {
796 }
797 void sample(T val, int number, const Params &params) {
798 if (val < params.min)
799 underflow += number;
800 else if (val > params.max)
801 overflow += number;
802 else {
803 int index = (val - params.min) / params.bucket_size;
804 assert(index < size(params));
805 vec[index] += number;
806 }
807
808 if (val < min_val)
809 min_val = val;
810
811 if (val > max_val)
812 max_val = val;
813 }
814
815 size_t size(const Params &) const { return vec.size(); }
816
817 bool zero(const Params &params) const {
818 if (underflow != 0 || overflow != 0)
819 return true;
820
821 int s = size(params);
822 for (int i = 0; i < s; i++)
823 if (vec[i] != 0)
824 return true;
825
826 return false;
827 }
828
829 void display(std::ostream &stream, const std::string &name,
830 const std::string &desc, int precision, FormatFlags flags,
831 const Params &params) const {
832
833 #ifdef STAT_DISPLAY_COMPAT
834 result_t min = params.min;
835 #else
836 result_t min = (min_val == INT_MAX) ? params.min : min_val;
837 #endif
838 result_t max = (max_val == INT_MIN) ? 0 : max_val;
839
840 rvec_t rvec(params.size);
841 for (int i = 0; i < params.size; ++i)
842 rvec[i] = vec[i];
843
844 DistDisplay(stream, name, desc, precision, flags,
845 (result_t)min, (result_t)max,
846 (result_t)underflow, (result_t)overflow,
847 rvec, params.min, params.max, params.bucket_size,
848 params.size);
849 }
850 };
851
852 void FancyDisplay(std::ostream &stream, const std::string &name,
853 const std::string &desc, int precision, FormatFlags flags,
854 result_t mean, result_t variance);
855 template <typename T>
856 struct FancyStor
857 {
858 public:
859 struct Params {};
860
861 private:
862 T sum;
863 T squares;
864 int total;
865
866 public:
867 FancyStor(const Params &) : sum(0), squares(0), total(0) {}
868
869 void sample(T val, int number, const Params &) {
870 T value = val * number;
871 sum += value;
872 squares += value * value;
873 total += number;
874 }
875 void display(std::ostream &stream, const std::string &name,
876 const std::string &desc, int precision, FormatFlags flags,
877 const Params &) const {
878
879 result_t mean = NAN;
880 result_t variance = NAN;
881
882 if (total != 0) {
883 result_t fsum = sum;
884 result_t fsq = squares;
885 result_t ftot = total;
886
887 mean = fsum / ftot;
888 variance = (ftot * fsq - (fsum * fsum)) / (ftot * (ftot - 1.0));
889 }
890
891 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
892 }
893
894 size_t size(const Params &) const { return 1; }
895 bool zero(const Params &) const { return total == 0; }
896 };
897
898 template <typename T>
899 struct AvgFancy
900 {
901 public:
902 struct Params {};
903
904 private:
905 T sum;
906 T squares;
907
908 public:
909 AvgFancy(const Params &) : sum(0), squares(0) {}
910
911 void sample(T val, int number, const Params& p) {
912 T value = val * number;
913 sum += value;
914 squares += value * value;
915 }
916 void display(std::ostream &stream, const std::string &name,
917 const std::string &desc, int precision, FormatFlags flags,
918 const Params &params) const {
919 result_t mean = sum / curTick;
920 result_t variance = (squares - sum * sum) / curTick;
921
922 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
923 }
924
925 size_t size(const Params &params) const { return 1; }
926 bool zero(const Params &params) const { return sum == 0; }
927 };
928
929 template <typename T, template <typename T> class Storage, class Bin>
930 class DistBase : public Stat
931 {
932 protected:
933 typedef Storage<T> storage_t;
934 typedef typename storage_t::Params params_t;
935 typedef typename Bin::Bin<storage_t> bin_t;
936
937 protected:
938 bin_t bin;
939 params_t params;
940
941 protected:
942 storage_t *data() { return bin.data(params); }
943 const storage_t *data() const {
944 return (const_cast<bin_t *>(&bin))->data(params);
945 }
946
947 protected:
948 // Copying stats is not allowed
949 DistBase(const DistBase &stat);
950 const DistBase &operator=(const DistBase &);
951
952 public:
953 DistBase() : Stat(true) { }
954 ~DistBase() { }
955
956 template <typename U>
957 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
958
959 virtual size_t size() const { return data()->size(params); }
960 virtual bool zero() const { return data()->zero(params); }
961 virtual void display(std::ostream &stream) const {
962 data()->display(stream, myname(), mydesc(), myprecision(), myflags(),
963 params);
964 }
965 };
966
967 template <typename T, template <typename T> class Storage, class Bin>
968 class VectorDistProxy;
969
970 template <typename T, template <typename T> class Storage, class Bin>
971 class VectorDistBase : public Stat
972 {
973 protected:
974 typedef Storage<T> storage_t;
975 typedef typename storage_t::Params params_t;
976 typedef typename Bin::VectorBin<storage_t> bin_t;
977
978 protected:
979 bin_t bin;
980 params_t params;
981
982 protected:
983 storage_t *data(int index) { return bin.data(index, params); }
984 const storage_t *data(int index) const {
985 return (const_cast<bin_t *>(&bin))->data(index, params);
986 }
987
988 protected:
989 // Copying stats is not allowed
990 VectorDistBase(const VectorDistBase &stat);
991 const VectorDistBase &operator=(const VectorDistBase &);
992
993 public:
994 VectorDistBase() : Stat(true) { }
995 ~VectorDistBase() { }
996
997 friend class VectorDistProxy<T, Storage, Bin>;
998 VectorDistProxy<T, Storage, Bin> operator[](int index);
999 const VectorDistProxy<T, Storage, Bin> operator[](int index) const;
1000
1001 virtual size_t size() const { return bin.size(); }
1002 virtual bool zero() const { return false; }
1003 virtual void display(std::ostream &stream) const;
1004 };
1005
1006 template <typename T, template <typename T> class Storage, class Bin>
1007 class VectorDistProxy : public Stat
1008 {
1009 protected:
1010 typedef Storage<T> storage_t;
1011 typedef typename storage_t::Params params_t;
1012 typedef typename Bin::Bin<storage_t> bin_t;
1013 typedef VectorDistBase<T, Storage, Bin> base_t;
1014
1015 private:
1016 union {
1017 base_t *stat;
1018 const base_t *cstat;
1019 };
1020 int index;
1021
1022 protected:
1023 storage_t *data() { return stat->data(index); }
1024 const storage_t *data() const { return cstat->data(index); }
1025
1026 public:
1027 VectorDistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1028 : Stat(false), cstat(&s), index(i) {}
1029 VectorDistProxy(const VectorDistProxy &sp)
1030 : Stat(false), cstat(sp.cstat), index(sp.index) {}
1031 const VectorDistProxy &operator=(const VectorDistProxy &sp) {
1032 cstat = sp.cstat; index = sp.index; return *this;
1033 }
1034
1035 public:
1036 template <typename U>
1037 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
1038
1039 virtual size_t size() const { return 1; }
1040 virtual bool zero() const {
1041 return data()->zero(cstat->params);
1042 }
1043 virtual void display(std::ostream &stream) const {
1044 std::stringstream name, desc;
1045
1046 if (!(cstat->mysubname(index).empty())) {
1047 name << cstat->myname() << cstat->mysubname(index);
1048 } else {
1049 name << cstat->myname() << "_" << index;
1050 }
1051 if (!(cstat->mysubdesc(index).empty())) {
1052 desc << cstat->mysubdesc(index);
1053 } else {
1054 desc << cstat->mydesc();
1055 }
1056
1057 data()->display(stream, name.str(), desc.str(),
1058 cstat->myprecision(), cstat->myflags(), cstat->params);
1059 }
1060 };
1061
1062 template <typename T, template <typename T> class Storage, class Bin>
1063 inline VectorDistProxy<T, Storage, Bin>
1064 VectorDistBase<T, Storage, Bin>::operator[](int index)
1065 {
1066 assert (index >= 0 && index < size());
1067 return VectorDistProxy<T, Storage, Bin>(*this, index);
1068 }
1069
1070 template <typename T, template <typename T> class Storage, class Bin>
1071 inline const VectorDistProxy<T, Storage, Bin>
1072 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1073 {
1074 assert (index >= 0 && index < size());
1075 return VectorDistProxy<T, Storage, Bin>(*this, index);
1076 }
1077
1078 /**
1079 * @todo Need a way to print Distribution totals across the Vector
1080 */
1081 template <typename T, template <typename T> class Storage, class Bin>
1082 void
1083 VectorDistBase<T, Storage, Bin>::display(std::ostream &stream) const
1084 {
1085 for (int i = 0; i < size(); ++i) {
1086 VectorDistProxy<T, Storage, Bin> proxy(*this, i);
1087 proxy.display(stream);
1088 }
1089 }
1090
1091 #if 0
1092 result_t
1093 VectorDistBase<T, Storage, Bin>::total(int index) const
1094 {
1095 int total = 0;
1096 for (int i=0; i < x_size(); ++i) {
1097 total += data(i)->val(*params);
1098 }
1099 }
1100 #endif
1101
1102 //////////////////////////////////////////////////////////////////////
1103 //
1104 // Formula Details
1105 //
1106 //////////////////////////////////////////////////////////////////////
1107 class Node : public RefCounted
1108 {
1109 public:
1110 virtual size_t size() const = 0;
1111 virtual const rvec_t &val() const = 0;
1112 virtual result_t total() const = 0;
1113 };
1114
1115 typedef RefCountingPtr<Node> NodePtr;
1116
1117 class ScalarStatNode : public Node
1118 {
1119 private:
1120 const ScalarStat &stat;
1121 mutable rvec_t result;
1122
1123 public:
1124 ScalarStatNode(const ScalarStat &s) : stat(s), result(1) {}
1125 const rvec_t &val() const { result[0] = stat.val(); return result; }
1126 virtual result_t total() const { return stat.val(); };
1127
1128 virtual size_t size() const { return 1; }
1129 };
1130
1131 template <typename T, template <typename T> class Storage, class Bin>
1132 class ScalarProxyNode : public Node
1133 {
1134 private:
1135 const ScalarProxy<T, Storage, Bin> proxy;
1136 mutable rvec_t result;
1137
1138 public:
1139 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1140 : proxy(p), result(1) { }
1141 const rvec_t &val() const { result[0] = proxy.val(); return result; }
1142 virtual result_t total() const { return proxy.val(); };
1143
1144 virtual size_t size() const { return 1; }
1145 };
1146
1147 class VectorStatNode : public Node
1148 {
1149 private:
1150 const VectorStat &stat;
1151
1152 public:
1153 VectorStatNode(const VectorStat &s) : stat(s) {}
1154 const rvec_t &val() const { return stat.val(); }
1155 virtual result_t total() const { return stat.total(); };
1156
1157 virtual size_t size() const { return stat.size(); }
1158 };
1159
1160 template <typename T>
1161 class ConstNode : public Node
1162 {
1163 private:
1164 rvec_t data;
1165
1166 public:
1167 ConstNode(T s) : data(1, (result_t)s) {}
1168 const rvec_t &val() const { return data; }
1169 virtual result_t total() const { return data[0]; };
1170
1171 virtual size_t size() const { return 1; }
1172 };
1173
1174 template <typename T>
1175 class FunctorNode : public Node
1176 {
1177 private:
1178 T &functor;
1179 mutable rvec_t result;
1180
1181 public:
1182 FunctorNode(T &f) : functor(f) { result.resize(1); }
1183 const rvec_t &val() const {
1184 result[0] = (result_t)functor();
1185 return result;
1186 }
1187 virtual result_t total() const { return (result_t)functor(); };
1188
1189 virtual size_t size() const { return 1; }
1190 };
1191
1192 template <typename T>
1193 class ScalarNode : public Node
1194 {
1195 private:
1196 T &scalar;
1197 mutable rvec_t result;
1198
1199 public:
1200 ScalarNode(T &s) : scalar(s) { result.resize(1); }
1201 const rvec_t &val() const {
1202 result[0] = (result_t)scalar;
1203 return result;
1204 }
1205 virtual result_t total() const { return (result_t)scalar; };
1206
1207 virtual size_t size() const { return 1; }
1208 };
1209
1210 template <class Op>
1211 class UnaryNode : public Node
1212 {
1213 public:
1214 NodePtr l;
1215 mutable rvec_t result;
1216
1217 public:
1218 UnaryNode(NodePtr p) : l(p) {}
1219
1220 const rvec_t &val() const {
1221 const rvec_t &lvec = l->val();
1222 int size = lvec.size();
1223
1224 assert(size > 0);
1225
1226 result.resize(size);
1227 Op op;
1228 for (int i = 0; i < size; ++i)
1229 result[i] = op(lvec[i]);
1230
1231 return result;
1232 }
1233
1234 result_t total() const {
1235 Op op;
1236 return op(l->total());
1237 }
1238
1239 virtual size_t size() const { return l->size(); }
1240 };
1241
1242 template <class Op>
1243 class BinaryNode : public Node
1244 {
1245 public:
1246 NodePtr l;
1247 NodePtr r;
1248 mutable rvec_t result;
1249
1250 public:
1251 BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {}
1252
1253 const rvec_t &val() const {
1254 Op op;
1255 const rvec_t &lvec = l->val();
1256 const rvec_t &rvec = r->val();
1257
1258 assert(lvec.size() > 0 && rvec.size() > 0);
1259
1260 if (lvec.size() == 1 && rvec.size() == 1) {
1261 result.resize(1);
1262 result[0] = op(lvec[0], rvec[0]);
1263 } else if (lvec.size() == 1) {
1264 int size = rvec.size();
1265 result.resize(size);
1266 for (int i = 0; i < size; ++i)
1267 result[i] = op(lvec[0], rvec[i]);
1268 } else if (rvec.size() == 1) {
1269 int size = lvec.size();
1270 result.resize(size);
1271 for (int i = 0; i < size; ++i)
1272 result[i] = op(lvec[i], rvec[0]);
1273 } else if (rvec.size() == lvec.size()) {
1274 int size = rvec.size();
1275 result.resize(size);
1276 for (int i = 0; i < size; ++i)
1277 result[i] = op(lvec[i], rvec[i]);
1278 }
1279
1280 return result;
1281 }
1282
1283 result_t total() const {
1284 Op op;
1285 return op(l->total(), r->total());
1286 }
1287
1288 virtual size_t size() const {
1289 int ls = l->size();
1290 int rs = r->size();
1291 if (ls == 1)
1292 return rs;
1293 else if (rs == 1)
1294 return ls;
1295 else {
1296 assert(ls == rs && "Node vector sizes are not equal");
1297 return ls;
1298 }
1299 }
1300 };
1301
1302 template <class Op>
1303 class SumNode : public Node
1304 {
1305 public:
1306 NodePtr l;
1307 mutable rvec_t result;
1308
1309 public:
1310 SumNode(NodePtr p) : l(p), result(1) {}
1311
1312 const rvec_t &val() const {
1313 const rvec_t &lvec = l->val();
1314 int size = lvec.size();
1315 assert(size > 0);
1316
1317 result[0] = 0.0;
1318
1319 Op op;
1320 for (int i = 0; i < size; ++i)
1321 result[0] = op(result[0], lvec[i]);
1322
1323 return result;
1324 }
1325
1326 result_t total() const {
1327 const rvec_t &lvec = l->val();
1328 int size = lvec.size();
1329 assert(size > 0);
1330
1331 result_t result = 0.0;
1332
1333 Op op;
1334 for (int i = 0; i < size; ++i)
1335 result = op(result, lvec[i]);
1336
1337 return result;
1338 }
1339
1340 virtual size_t size() const { return 1; }
1341 };
1342
1343 class Temp
1344 {
1345 private:
1346 NodePtr node;
1347
1348 public:
1349 Temp(NodePtr n) : node(n) {}
1350 Temp(const ScalarStat &s) : node(new ScalarStatNode(s)) {}
1351 template <typename T, template <typename T> class Storage, class Bin>
1352 Temp(const ScalarProxy<T, Storage, Bin> &p)
1353 : node(new ScalarProxyNode<T, Storage, Bin>(p)) {}
1354 Temp(const VectorStat &s) : node(new VectorStatNode(s)) {}
1355
1356 #define TempSCALAR(T) \
1357 Temp(T value) : node(new ConstNode<T>(value)) {}
1358
1359 TempSCALAR( signed char);
1360 TempSCALAR(unsigned char);
1361 TempSCALAR( signed short);
1362 TempSCALAR(unsigned short);
1363 TempSCALAR( signed int);
1364 TempSCALAR(unsigned int);
1365 TempSCALAR( signed long);
1366 TempSCALAR(unsigned long);
1367 TempSCALAR( signed long long);
1368 TempSCALAR(unsigned long long);
1369 TempSCALAR(float);
1370 TempSCALAR(double);
1371 #undef TempSCALAR
1372
1373 operator NodePtr() { return node;}
1374 };
1375
1376
1377 //////////////////////////////////////////////////////////////////////
1378 //
1379 // Binning Interface
1380 //
1381 //////////////////////////////////////////////////////////////////////
1382
1383 class BinBase
1384 {
1385 private:
1386 off_t memsize;
1387 char *mem;
1388
1389 protected:
1390 off_t size() const { return memsize; }
1391 char *memory();
1392
1393 public:
1394 BinBase(size_t size);
1395 ~BinBase();
1396 };
1397
1398 } // namespace Detail
1399
1400 template <class BinType>
1401 struct StatBin : public Detail::BinBase
1402 {
1403 static StatBin *&curBin() {
1404 static StatBin *current = NULL;
1405 return current;
1406 }
1407
1408 static void setCurBin(StatBin *bin) { curBin() = bin; }
1409 static StatBin *current() { assert(curBin()); return curBin(); }
1410
1411 static off_t &offset() {
1412 static off_t offset = 0;
1413 return offset;
1414 }
1415
1416 static off_t new_offset(size_t size) {
1417 size_t mask = sizeof(u_int64_t) - 1;
1418 off_t off = offset();
1419
1420 // That one is for the last trailing flags byte.
1421 offset() += (size + 1 + mask) & ~mask;
1422
1423 return off;
1424 }
1425
1426 explicit StatBin(size_t size = 1024) : Detail::BinBase(size) {}
1427
1428 char *memory(off_t off) {
1429 assert(offset() <= size());
1430 return Detail::BinBase::memory() + off;
1431 }
1432
1433 static void activate(StatBin &bin) { setCurBin(&bin); }
1434
1435 class BinBase
1436 {
1437 private:
1438 int offset;
1439
1440 public:
1441 BinBase() : offset(-1) {}
1442 void allocate(size_t size) {
1443 offset = new_offset(size);
1444 }
1445 char *access() {
1446 assert(offset != -1);
1447 return current()->memory(offset);
1448 }
1449 };
1450
1451 template <class Storage>
1452 class Bin : public BinBase
1453 {
1454 public:
1455 typedef typename Storage::Params Params;
1456
1457 public:
1458 Bin() { allocate(sizeof(Storage)); }
1459 bool initialized() const { return true; }
1460 void init(const Params &params) { }
1461
1462 int size() const { return 1; }
1463
1464 Storage *data(const Params &params) {
1465 assert(initialized());
1466 char *ptr = access();
1467 char *flags = ptr + sizeof(Storage);
1468 if (!(*flags & 0x1)) {
1469 *flags |= 0x1;
1470 new (ptr) Storage(params);
1471 }
1472 return reinterpret_cast<Storage *>(ptr);
1473 }
1474 };
1475
1476 template <class Storage>
1477 class VectorBin : public BinBase
1478 {
1479 public:
1480 typedef typename Storage::Params Params;
1481
1482 private:
1483 int _size;
1484
1485 public:
1486 VectorBin() : _size(0) {}
1487
1488 bool initialized() const { return _size > 0; }
1489 void init(int s, const Params &params) {
1490 assert(!initialized());
1491 assert(s > 0);
1492 _size = s;
1493 allocate(_size * sizeof(Storage));
1494 }
1495
1496 int size() const { return _size; }
1497
1498 Storage *data(int index, const Params &params) {
1499 assert(initialized());
1500 assert(index >= 0 && index < size());
1501 char *ptr = access();
1502 char *flags = ptr + size() * sizeof(Storage);
1503 if (!(*flags & 0x1)) {
1504 *flags |= 0x1;
1505 for (int i = 0; i < size(); ++i)
1506 new (ptr + i * sizeof(Storage)) Storage(params);
1507 }
1508 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
1509 }
1510 };
1511 };
1512
1513 class MainBinType {};
1514 typedef StatBin<MainBinType> MainBin;
1515
1516 struct NoBin
1517 {
1518 template <class Storage>
1519 struct Bin
1520 {
1521 public:
1522 typedef typename Storage::Params Params;
1523
1524 private:
1525 char ptr[sizeof(Storage)];
1526
1527 public:
1528 bool initialized() const { return true; }
1529 void init(const Params &params) {
1530 new (ptr) Storage(params);
1531 }
1532 int size() const{ return 1; }
1533 Storage *data(const Params &params) {
1534 assert(initialized());
1535 return reinterpret_cast<Storage *>(ptr);
1536 }
1537 };
1538
1539 template <class Storage>
1540 struct VectorBin
1541 {
1542 public:
1543 typedef typename Storage::Params Params;
1544
1545 private:
1546 char *ptr;
1547 int _size;
1548
1549 public:
1550 VectorBin() : ptr(NULL) { }
1551 ~VectorBin() {
1552 if (initialized())
1553 delete [] ptr;
1554 }
1555 bool initialized() const { return ptr != NULL; }
1556 void init(int s, const Params &params) {
1557 assert(s > 0 && "size must be positive!");
1558 assert(!initialized());
1559 _size = s;
1560 ptr = new char[_size * sizeof(Storage)];
1561 for (int i = 0; i < _size; ++i)
1562 new (ptr + i * sizeof(Storage)) Storage(params);
1563 }
1564
1565 int size() const { return _size; }
1566
1567 Storage *data(int index, const Params &params) {
1568 assert(initialized());
1569 assert(index >= 0 && index < size());
1570 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
1571 }
1572 };
1573 };
1574
1575 //////////////////////////////////////////////////////////////////////
1576 //
1577 // Visible Statistics Types
1578 //
1579 //////////////////////////////////////////////////////////////////////
1580 /**@defgroup VStats VisibleStatTypes
1581 */
1582
1583 /** @ingroup VStats
1584 *This is the simplest counting stat. Default type is Counter, but can be
1585 *anything (like double, int, etc). To bin, just designate the name of the bin
1586 * when declaring. It can be used like a regular Counter.
1587 *Example: Stat<> foo;
1588 *foo += num_foos;
1589 */
1590 template <typename T = Counter, class Bin = NoBin>
1591 class Scalar : public Detail::ScalarBase<T, Detail::StatStor, Bin>
1592 {
1593 public:
1594 typedef Detail::ScalarBase<T, Detail::StatStor, Bin> Base;
1595
1596 /** sets Stat equal to value of type U */
1597 template <typename U>
1598 void operator=(const U& v) { Base::operator=(v); }
1599 };
1600
1601 /** @ingroup VStats
1602 *This calculates averages over number of cycles. Additionally, the update per
1603 *cycle is implicit if there is no change. In other words, if you want to know
1604 *the average number of instructions in the IQ per cycle, then you can use this
1605 * stat and not have to update it on cycles where there is no change.
1606 */
1607 template <typename T = Counter, class Bin = NoBin>
1608 class Average : public Detail::ScalarBase<T, Detail::AvgStor, Bin>
1609 {
1610 public:
1611 typedef Detail::ScalarBase<T, Detail::AvgStor, Bin> Base;
1612
1613 /** sets Average equalt to value of type U*/
1614 template <typename U>
1615 void operator=(const U& v) { Base::operator=(v); }
1616 };
1617
1618 /** @ingroup VStats
1619 *This is a vector of type T, ideally suited to track stats across something like
1620 * SMT threads.
1621 */
1622 template <typename T = Counter, class Bin = NoBin>
1623 class Vector : public Detail::VectorBase<T, Detail::StatStor, Bin>
1624 { };
1625
1626 /** @ingroup VStats
1627 *This is a vector of Averages of type T
1628 */
1629 template <typename T = Counter, class Bin = NoBin>
1630 class AverageVector : public Detail::VectorBase<T, Detail::AvgStor, Bin>
1631 { };
1632
1633 /** @ingroup VStats
1634 *This is a 2-dimensional vector. Intended usage is for something like tracking a
1635 * Vector stat across another Vector like SMT threads.
1636 */
1637 template <typename T = Counter, class Bin = NoBin>
1638 class Vector2d : public Detail::Vector2dBase<T, Detail::StatStor, Bin>
1639 { };
1640
1641 /** @ingroup VStats
1642 * This is essentially a Vector, but with minor differences. Where a
1643 * Vector's index maps directly to what it's tracking, a Distribution's index can
1644 * map to an arbitrary bucket type. For example, you could map 1-8 to bucket 0
1645 * of a Distribution, and if ever there are 1-8 instructions within an IQ, increment
1646 * bucket 0.
1647 */
1648 template <typename T = Counter, class Bin = NoBin>
1649 class Distribution : public Detail::DistBase<T, Detail::DistStor, Bin>
1650 {
1651 private:
1652 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
1653 typedef typename Detail::DistStor<T>::Params Params;
1654
1655 public:
1656 /**
1657 *This must be called to set some data members of the distribution
1658 *as well as to allocate the appropriate storage size.
1659 *@param min The minimum value of the Distribution
1660 *@param max The maximum value of the Distribution (NOT the size!)
1661 *@param bkt The size of the buckets to indicate mapping. I.e. if you have
1662 *min=0, max=15, bkt=8, you will have two buckets, and anything from 0-7
1663 *will go into bucket 0, and anything from 8-15 be in bucket 1.
1664 *@return the Distribution itself.
1665 */
1666 Distribution &init(T min, T max, int bkt) {
1667 params.min = min;
1668 params.max = max;
1669 params.bucket_size = bkt;
1670 params.size = (max - min) / bkt + 1;
1671 bin.init(params);
1672 setInit();
1673
1674 return *this;
1675 }
1676 };
1677
1678 /** @ingroup VStats
1679 *This has the functionality of a standard deviation built into it. Update it
1680 *every cycle, and at the end you will have the standard deviation.
1681 */
1682 template <typename T = Counter, class Bin = NoBin>
1683 class StandardDeviation : public Detail::DistBase<T, Detail::FancyStor, Bin>
1684 {
1685 private:
1686 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
1687 typedef typename Detail::DistStor<T>::Params Params;
1688
1689 public:
1690 StandardDeviation() {
1691 bin.init(params);
1692 setInit();
1693 }
1694 };
1695
1696 /** @ingroup VStats
1697 *This also calculates standard deviations, but there is no need to
1698 *update every cycle if there is no change, the stat will update for you.
1699 */
1700 template <typename T = Counter, class Bin = NoBin>
1701 class AverageDeviation : public Detail::DistBase<T, Detail::AvgFancy, Bin>
1702 {
1703 private:
1704 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
1705 typedef typename Detail::DistStor<T>::Params Params;
1706
1707 public:
1708 AverageDeviation() {
1709 bin.init(params);
1710 setInit();
1711 }
1712 };
1713
1714 /** @ingroup VStats
1715 *This is a vector of Distributions. (The complexity increases!). Intended usage
1716 * is for something like tracking a distribution across a vector like SMT threads.
1717 */
1718 template <typename T = Counter, class Bin = NoBin>
1719 class VectorDistribution
1720 : public Detail::VectorDistBase<T, Detail::DistStor, Bin>
1721 {
1722 private:
1723 typedef Detail::VectorDistBase<T, Detail::DistStor, Bin> Base;
1724 typedef typename Detail::DistStor<T>::Params Params;
1725
1726 public:
1727 /**
1728 *This must be called to set some data members and allocate storage space.
1729 *@param size The size of the Vector
1730 *@param min The minumum value of the Distribution
1731 *@param max The maximum value of the Distribution (NOT the size)
1732 *@param bkt The range of the bucket. I.e if min=0, max=15, and bkt=8,
1733 *then 0-7 will be bucket 0, and 8-15 will be bucket 1.
1734 *@return return the VectorDistribution itself.
1735 */
1736 VectorDistribution &init(int size, T min, T max, int bkt) {
1737 params.min = min;
1738 params.max = max;
1739 params.bucket_size = bkt;
1740 params.size = (max - min) / bkt + 1;
1741 bin.init(size, params);
1742 setInit();
1743
1744 return *this;
1745 }
1746 };
1747
1748 /** @ingroup VStats
1749 *This is a vector of Standard Deviations. Intended usage is for tracking
1750 *Standard Deviations across a vector like SMT threads.
1751 */
1752 template <typename T = Counter, class Bin = NoBin>
1753 class VectorStandardDeviation
1754 : public Detail::VectorDistBase<T, Detail::FancyStor, Bin>
1755 {
1756 private:
1757 typedef Detail::VectorDistBase<T, Detail::FancyStor, Bin> Base;
1758 typedef typename Detail::DistStor<T>::Params Params;
1759
1760 public:
1761 /** This must be called to initialize some data members and allocate
1762 * approprate storage space for the stat.
1763 *@param size The size of the Vector
1764 * @return the VectorStandardDeviation itself.
1765 */
1766 VectorStandardDeviation &init(int size) {
1767 bin.init(size, params);
1768 setInit();
1769
1770 return *this;
1771 }
1772 };
1773
1774 /** @ingroup VStats
1775 * This is a vector of Average Deviations. Intended usage is for tracking
1776 *Average Deviations across a vector like SMT threads.
1777 */
1778 template <typename T = Counter, class Bin = NoBin>
1779 class VectorAverageDeviation
1780 : public Detail::VectorDistBase<T, Detail::AvgFancy, Bin>
1781 {
1782 private:
1783 typedef Detail::VectorDistBase<T, Detail::AvgFancy, Bin> Base;
1784 typedef typename Detail::DistStor<T>::Params Params;
1785
1786 public:
1787 /** This must be called to initialize some data members and allocate
1788 * approprate storage space for the stat.
1789 *@param size The size of the Vector
1790 * @return The VectorAverageDeviation itself.
1791 */
1792 VectorAverageDeviation &init(int size) {
1793 bin.init(size, params);
1794 setInit();
1795
1796 return *this;
1797 }
1798 };
1799
1800 /** @ingroup VStats
1801 *This is a formula type. When defining it, you can just say:
1802 *Formula foo = manchu + 3 / bar;
1803 *The calculations for Formulas are all done at the end of the simulation, this
1804 *really is just a definition of how to calculate at the end.
1805 */
1806 class Formula : public Detail::VectorStat
1807 {
1808 private:
1809 /** The root of the tree which represents the Formula */
1810 Detail::NodePtr root;
1811 friend class Detail::Temp;
1812
1813 public:
1814 Formula() : VectorStat(true) { setInit(); }
1815 Formula(Detail::Temp r) : VectorStat(true) {
1816 root = r;
1817 assert(size());
1818 }
1819
1820 const Formula &operator=(Detail::Temp r) {
1821 assert(!root && "Can't change formulas");
1822 root = r;
1823 assert(size());
1824 return *this;
1825 }
1826
1827 const Formula &operator+=(Detail::Temp r) {
1828 using namespace Detail;
1829 if (root)
1830 root = NodePtr(new BinaryNode<std::plus<result_t> >(root, r));
1831 else
1832 root = r;
1833 assert(size());
1834 return *this;
1835 }
1836
1837 const rvec_t &val() const { return root->val(); }
1838 result_t total() const { return root->total(); }
1839
1840 size_t size() const {
1841 if (!root)
1842 return 0;
1843 else
1844 return root->size();
1845 }
1846 };
1847
1848 void check();
1849 void dump(std::ostream &stream);
1850
1851 inline Detail::Temp
1852 operator+(Detail::Temp l, Detail::Temp r)
1853 {
1854 using namespace Detail;
1855 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
1856 }
1857
1858 inline Detail::Temp
1859 operator-(Detail::Temp l, Detail::Temp r)
1860 {
1861 using namespace Detail;
1862 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
1863 }
1864
1865 inline Detail::Temp
1866 operator*(Detail::Temp l, Detail::Temp r)
1867 {
1868 using namespace Detail;
1869 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
1870 }
1871
1872 inline Detail::Temp
1873 operator/(Detail::Temp l, Detail::Temp r)
1874 {
1875 using namespace Detail;
1876 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
1877 }
1878
1879 inline Detail::Temp
1880 operator%(Detail::Temp l, Detail::Temp r)
1881 {
1882 using namespace Detail;
1883 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
1884 }
1885
1886 inline Detail::Temp
1887 operator-(Detail::Temp l)
1888 {
1889 using namespace Detail;
1890 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
1891 }
1892
1893 template <typename T>
1894 inline Detail::Temp
1895 constant(T val)
1896 {
1897 using namespace Detail;
1898 return NodePtr(new ConstNode<T>(val));
1899 }
1900
1901 template <typename T>
1902 inline Detail::Temp
1903 functor(T &val)
1904 {
1905 using namespace Detail;
1906 return NodePtr(new FunctorNode<T>(val));
1907 }
1908
1909 template <typename T>
1910 inline Detail::Temp
1911 scalar(T &val)
1912 {
1913 using namespace Detail;
1914 return NodePtr(new ScalarNode<T>(val));
1915 }
1916
1917 inline Detail::Temp
1918 sum(Detail::Temp val)
1919 {
1920 using namespace Detail;
1921 return NodePtr(new SumNode<std::plus<result_t> >(val));
1922 }
1923
1924 extern bool PrintDescriptions;
1925
1926 } // namespace statistics
1927
1928 #endif // __STATISTICS_HH__