statistics.hh:
[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 * key stats
39 * interval stats
40 * -- these both can use the same function that prints out a
41 * specific set of stats
42 * VectorStandardDeviation totals
43 * Document Namespaces
44 */
45 #ifndef __STATISTICS_HH__
46 #define __STATISTICS_HH__
47
48 #include <algorithm>
49 #include <functional>
50 #include <iosfwd>
51 #include <sstream>
52 #include <string>
53 #include <vector>
54
55 #include <assert.h>
56
57 #include "base/refcnt.hh"
58 #include "base/str.hh"
59
60 #include "sim/host.hh"
61
62 //
63 // Un-comment this to enable wierdo-stat debugging
64 //
65 // #define STAT_DEBUG
66
67
68 #ifndef NAN
69 float __nan();
70 /** Define Not a number. */
71 #define NAN (__nan())
72 /** Need to define __nan() */
73 #define __M5_NAN
74 #endif
75
76 /** Print stats out in SS format. */
77 #define STAT_DISPLAY_COMPAT
78
79 class Callback;
80
81 /** The current simulated cycle. */
82 extern Tick curTick;
83
84 /* A namespace for all of the Statistics */
85 namespace Statistics {
86 /** All results are doubles. */
87 typedef double result_t;
88 /** A vector to hold results. */
89 typedef std::vector<result_t> rvec_t;
90
91 /**
92 * Define the storage for format flags.
93 * @todo Can probably shrink this.
94 */
95 typedef u_int32_t FormatFlags;
96 /** Nothing extra to print. */
97 const FormatFlags none = 0x0000;
98 /** Print the total. */
99 const FormatFlags total = 0x0001;
100 /** Print the percent of the total that this entry represents. */
101 const FormatFlags pdf = 0x0002;
102 /** Don't print if this is zero. */
103 const FormatFlags nozero = 0x0004;
104 /** Don't print if this is NAN */
105 const FormatFlags nonan = 0x0008;
106 /** Print the cumulative percentage of total upto this entry. */
107 const FormatFlags cdf = 0x0010;
108 /** Print the distribution. */
109 const FormatFlags dist = 0x0020;
110 /** Used for SS compatability. */
111 const FormatFlags __substat = 0x8000;
112 /** Mask of flags that can't be set directly */
113 const FormatFlags __reserved = __substat;
114
115 /* Contains the statistic implementation details */
116 namespace Detail {
117 //////////////////////////////////////////////////////////////////////
118 //
119 // Statistics Framework Base classes
120 //
121 //////////////////////////////////////////////////////////////////////
122 struct StatData;
123 struct SubData;
124
125 /**
126 * Common base class for all statistics, used to maintain a list and print.
127 * This class holds no data itself but is used to find the associated
128 * StatData in the stat database @sa Statistics::Database.
129 */
130 class Stat
131 {
132 protected:
133 /** Mark this statistics as initialized. */
134 void setInit();
135 /**
136 * Finds and returns the associated StatData from the database.
137 * @return The formatting and output data of this statistic.
138 */
139 StatData *mydata();
140 /**
141 * Finds and returns a const pointer to the associated StatData.
142 * @return The formatting and output data of this statistic.
143 */
144 const StatData *mydata() const;
145 /**
146 * Mark this stat for output at the end of simulation.
147 * @return The formatting and output data of this statistic.
148 */
149 StatData *print();
150 /**
151 * Finds and returns the SubData at the given index.
152 * @param index The index of the SubData to find.
153 * @return The name and description of the given index.
154 */
155 const SubData *mysubdata(int index) const;
156 /**
157 * Create and return a new SubData field for the given index.
158 * @param index The index to create a SubData for.
159 * @return A pointer to the created SubData.
160 */
161 SubData *mysubdata_create(int index);
162
163 public:
164 /**
165 * Return the name of this stat.
166 * @return the name of the stat.
167 */
168 virtual std::string myname() const;
169 /**
170 * Return the name of the sub field at the given index.
171 * @param index the subfield index.
172 * @return the name of the subfield.
173 */
174 virtual std::string mysubname(int index) const;
175 /**
176 * Return the description of this stat.
177 * @return the description of this stat.
178 */
179 virtual std::string mydesc() const;
180 /**
181 * Return the description of the subfield at the given index.
182 * @param index The subfield index.
183 * @return the description of the subfield.
184 */
185 virtual std::string mysubdesc(int index) const;
186 /**
187 * Return the format flags of this stat.
188 * @return the format flags.
189 */
190 virtual FormatFlags myflags() const;
191 /**
192 * Return true if this stat's prereqs have been satisfied (they are non
193 * zero).
194 * @return true if the prerequisite stats aren't zero.
195 */
196 virtual bool dodisplay() const;
197 /**
198 * Return the display percision.
199 * @return The display precision.
200 */
201 virtual int myprecision() const;
202
203 public:
204 /**
205 * Create this stat and perhaps register it with the stat database. To be
206 * printed a stat must be registered with the database.
207 * @param reg If true, register this stat in the database.
208 */
209 Stat(bool reg);
210 /**
211 * Destructor
212 */
213 virtual ~Stat() {}
214
215 /**
216 * Print this stat to the given ostream.
217 * @param stream The stream to print to.
218 */
219 virtual void display(std::ostream &stream) const = 0;
220 /**
221 * Reset this stat to the default state.
222 */
223 virtual void reset() = 0;
224 /**
225 * Return the number of entries in this stat.
226 * @return The number of entries.
227 */
228 virtual size_t size() const = 0;
229 /**
230 * Return true if the stat has value zero.
231 * @return True if the stat is zero.
232 */
233 virtual bool zero() const = 0;
234
235
236 /**
237 * Set the name and marks this stat to print at the end of simulation.
238 * @param name The new name.
239 * @return A reference to this stat.
240 */
241 Stat &name(const std::string &name);
242 /**
243 * Set the description and marks this stat to print at the end of
244 * simulation.
245 * @param desc The new description.
246 * @return A reference to this stat.
247 */
248 Stat &desc(const std::string &desc);
249 /**
250 * Set the precision and marks this stat to print at the end of simulation.
251 * @param p The new precision
252 * @return A reference to this stat.
253 */
254 Stat &precision(int p);
255 /**
256 * Set the flags and marks this stat to print at the end of simulation.
257 * @param f The new flags.
258 * @return A reference to this stat.
259 */
260 Stat &flags(FormatFlags f);
261 /**
262 * Set the prerequisite stat and marks this stat to print at the end of
263 * simulation.
264 * @param prereq The prerequisite stat.
265 * @return A reference to this stat.
266 */
267 Stat &prereq(const Stat &prereq);
268 /**
269 * Set the subfield name for the given index, and marks this stat to print
270 * at the end of simulation.
271 * @param index The subfield index.
272 * @param name The new name of the subfield.
273 * @return A reference to this stat.
274 */
275 Stat &subname(int index, const std::string &name);
276 /**
277 * Set the subfield description for the given index and marks this stat to
278 * print at the end of simulation.
279 * @param index The subfield index.
280 * @param desc The new description of the subfield
281 * @return A reference to this stat.
282 */
283 Stat &subdesc(int index, const std::string &desc);
284
285 public:
286 /**
287 * Checks if the first stat's name is alphabetically less than the second.
288 * This function breaks names up at periods and considers each subname
289 * separately.
290 * @param stat1 The first stat.
291 * @param stat2 The second stat.
292 * @return stat1's name is alphabetically before stat2's
293 */
294 static bool less(Stat *stat1, Stat *stat2);
295
296 #ifdef STAT_DEBUG
297 /** A unique ID used for debugging. */
298 int number;
299 #endif
300 };
301
302 /**
303 * Base class for all scalar stats. The class provides an interface to access
304 * the current value of the stat. This class can be used in formulas.
305 */
306 class ScalarStat : public Stat
307 {
308 public:
309 /**
310 * Create and perhaps register this stat with the database.
311 * @param reg If true, register this stat with the database.
312 */
313 ScalarStat(bool reg) : Stat(reg) {}
314 /**
315 * Return the current value of this statistic as a result type.
316 * @return The current value of this statistic.
317 */
318 virtual result_t val() const = 0;
319 /**
320 * Return true if this stat has value zero.
321 * @return True if this stat is zero.
322 */
323 virtual bool zero() const;
324 /**
325 * Print this stat to the provided ostream.
326 * @param stream The output stream.
327 */
328 virtual void display(std::ostream &stream) const;
329 };
330
331 void
332 VectorDisplay(std::ostream &stream, const std::string &myname,
333 const std::vector<std::string> *mysubnames,
334 const std::string &mydesc,
335 const std::vector<std::string> *mysubdescs,
336 int myprecision, FormatFlags myflags, const rvec_t &vec,
337 result_t mytotal);
338
339 /**
340 * Base class for all vector stats. This class provides interfaces to access
341 * the current values of the stats as well as the totals. This class can be
342 * used in formulas.
343 */
344 class VectorStat : public Stat
345 {
346 public:
347 /**
348 * Create and perhaps register this stat with the database.
349 * @param reg If true, register this stat with the database.
350 */
351 VectorStat(bool reg) : Stat(reg) {}
352 /**
353 * Return a vector of result typesd of all the values in the vector.
354 * @return The values of the vector.
355 */
356 virtual const rvec_t &val() const = 0;
357 /**
358 * Return the total of all the entries in the vector.
359 * @return The total of the vector.
360 */
361 virtual result_t total() const = 0;
362 /**
363 * Return true if this stat has value zero.
364 * @return True if this stat is zero.
365 */
366 virtual bool zero() const;
367 /**
368 * Print this stat to the provided ostream.
369 * @param stream The output stream.
370 */
371 virtual void display(std::ostream &stream) const;
372 };
373
374 //////////////////////////////////////////////////////////////////////
375 //
376 // Simple Statistics
377 //
378 //////////////////////////////////////////////////////////////////////
379
380 /**
381 * Templatized storage and interface for a simple scalar stat.
382 */
383 template <typename T>
384 struct StatStor
385 {
386 public:
387 /** The paramaters for this storage type, none for a scalar. */
388 struct Params { };
389
390 private:
391 /** The statistic value. */
392 T data;
393
394 public:
395 /**
396 * Builds this storage element and calls the base constructor of the
397 * datatype.
398 */
399 StatStor(const Params &) : data(T()) {}
400
401 /**
402 * The the stat to the given value.
403 * @param val The new value.
404 * @param p The paramters of this storage type.
405 */
406 void set(T val, const Params &p) { data = val; }
407 /**
408 * Increment the stat by the given value.
409 * @param val The new value.
410 * @param p The paramters of this storage type.
411 */
412 void inc(T val, const Params &p) { data += val; }
413 /**
414 * Decrement the stat by the given value.
415 * @param val The new value.
416 * @param p The paramters of this storage type.
417 */
418 void dec(T val, const Params &p) { data -= val; }
419 /**
420 * Return the value of this stat as a result type.
421 * @param p The parameters of this storage type.
422 * @return The value of this stat.
423 */
424 result_t val(const Params &p) const { return (result_t)data; }
425 /**
426 * Return the value of this stat as its base type.
427 * @param p The params of this storage type.
428 * @return The value of this stat.
429 */
430 T value(const Params &p) const { return data; }
431 /**
432 * Reset stat value to default
433 */
434 void reset() { data = T(); }
435 };
436
437 /**
438 * Templatized storage and interface to a per-cycle average stat. This keeps
439 * a current count and updates a total (count * cycles) when this count
440 * changes. This allows the quick calculation of a per cycle count of the item
441 * being watched. This is good for keeping track of residencies in structures
442 * among other things.
443 * @todo add lateny to the stat and fix binning.
444 */
445 template <typename T>
446 struct AvgStor
447 {
448 public:
449 /** The paramaters for this storage type, none for this average. */
450 struct Params { };
451
452 private:
453 /** The current count. */
454 T current;
455 /** The total count for all cycles. */
456 mutable result_t total;
457 /** The cycle that current last changed. */
458 mutable Tick last;
459
460 public:
461 /**
462 * Build and initializes this stat storage.
463 */
464 AvgStor(const Params &) : current(T()), total(0), last(0) { }
465
466 /**
467 * Set the current count to the one provided, update the total and last
468 * set values.
469 * @param val The new count.
470 * @param p The parameters for this storage.
471 */
472 void set(T val, const Params &p) {
473 total += current * (curTick - last);
474 last = curTick;
475 current = val;
476 }
477 /**
478 * Increment the current count by the provided value, calls set.
479 * @param val The amount to increment.
480 * @param p The parameters for this storage.
481 */
482 void inc(T val, const Params &p) { set(current + val, p); }
483 /**
484 * Deccrement the current count by the provided value, calls set.
485 * @param val The amount to decrement.
486 * @param p The parameters for this storage.
487 */
488 void dec(T val, const Params &p) { set(current - val, p); }
489 /**
490 * Return the current average.
491 * @param p The parameters for this storage.
492 * @return The current average.
493 */
494 result_t val(const Params &p) const {
495 total += current * (curTick - last);
496 last = curTick;
497 return (result_t)(total + current) / (result_t)(curTick + 1);
498 }
499 /**
500 * Return the current count.
501 * @param p The parameters for this storage.
502 * @return The current count.
503 */
504 T value(const Params &p) const { return current; }
505 /**
506 * Reset stat value to default
507 */
508 void reset()
509 {
510 current = T();
511 total = 0;
512 last = curTick;
513 }
514 };
515
516 /**
517 * Implementation of a scalar stat. The type of stat is determined by the
518 * Storage template. The storage for this stat is held within the Bin class.
519 * This allows for breaking down statistics across multiple bins easily.
520 */
521 template <typename T, template <typename T> class Storage, class Bin>
522 class ScalarBase : public ScalarStat
523 {
524 protected:
525 /** Define the type of the storage class. */
526 typedef Storage<T> storage_t;
527 /** Define the params of the storage class. */
528 typedef typename storage_t::Params params_t;
529 /** Define the bin type. */
530 typedef typename Bin::Bin<storage_t> bin_t;
531
532 protected:
533 /** The bin of this stat. */
534 bin_t bin;
535 /** The parameters for this stat. */
536 params_t params;
537
538 protected:
539 /**
540 * Retrieve the storage from the bin.
541 * @return The storage object for this stat.
542 */
543 storage_t *data() { return bin.data(params); }
544 /**
545 * Retrieve a const pointer to the storage from the bin.
546 * @return A const pointer to the storage object for this stat.
547 */
548 const storage_t *data() const {
549 return (const_cast<bin_t *>(&bin))->data(params);
550 }
551
552 protected:
553 /**
554 * Copy constructor, copies are not allowed.
555 */
556 ScalarBase(const ScalarBase &stat);
557 /**
558 * Can't copy stats.
559 */
560 const ScalarBase &operator=(const ScalarBase &);
561
562 public:
563 /**
564 * Return the current value of this stat as a result type.
565 * @return The current value.
566 */
567 result_t val() const { return data()->val(params); }
568 /**
569 * Return the current value of this stat as its base type.
570 * @return The current value.
571 */
572 T value() const { return data()->value(params); }
573
574 public:
575 /**
576 * Create and initialize this stat, register it with the database.
577 */
578 ScalarBase() : ScalarStat(true) {
579 bin.init(params);
580 setInit();
581 }
582
583 public:
584 // Common operators for stats
585 /**
586 * Increment the stat by 1. This calls the associated storage object inc
587 * function.
588 */
589 void operator++() { data()->inc(1, params); }
590 /**
591 * Decrement the stat by 1. This calls the associated storage object dec
592 * function.
593 */
594 void operator--() { data()->dec(1, params); }
595
596 /** Increment the stat by 1. */
597 void operator++(int) { ++*this; }
598 /** Decrement the stat by 1. */
599 void operator--(int) { --*this; }
600
601 /**
602 * Set the data value to the given value. This calls the associated storage
603 * object set function.
604 * @param v The new value.
605 */
606 template <typename U>
607 void operator=(const U& v) { data()->set(v, params); }
608
609 /**
610 * Increment the stat by the given value. This calls the associated
611 * storage object inc function.
612 * @param v The value to add.
613 */
614 template <typename U>
615 void operator+=(const U& v) { data()->inc(v, params); }
616
617 /**
618 * Decrement the stat by the given value. This calls the associated
619 * storage object dec function.
620 * @param v The value to substract.
621 */
622 template <typename U>
623 void operator-=(const U& v) { data()->dec(v, params); }
624
625 /**
626 * Return the number of elements, always 1 for a scalar.
627 * @return 1.
628 */
629 virtual size_t size() const { return 1; }
630
631 /**
632 * Reset stat value to default
633 */
634 void reset() { bin.reset(); }
635 };
636
637 //////////////////////////////////////////////////////////////////////
638 //
639 // Vector Statistics
640 //
641 //////////////////////////////////////////////////////////////////////
642 template <typename T, template <typename T> class Storage, class Bin>
643 class ScalarProxy;
644
645 /**
646 * Implementation of a vector of stats. The type of stat is determined by the
647 * Storage class. @sa ScalarBase
648 */
649 template <typename T, template <typename T> class Storage, class Bin>
650 class VectorBase : public VectorStat
651 {
652 protected:
653 /** Define the type of the storage class. */
654 typedef Storage<T> storage_t;
655 /** Define the params of the storage class. */
656 typedef typename storage_t::Params params_t;
657 /** Define the bin type. */
658 typedef typename Bin::VectorBin<storage_t> bin_t;
659
660 private:
661 /** Local storage for the entry values, used for printing. */
662 mutable rvec_t *vec;
663
664 protected:
665 /** The bin of this stat. */
666 bin_t bin;
667 /** The parameters for this stat. */
668 params_t params;
669
670 protected:
671 /**
672 * Retrieve the storage from the bin for the given index.
673 * @param index The vector index to access.
674 * @return The storage object at the given index.
675 */
676 storage_t *data(int index) { return bin.data(index, params); }
677 /**
678 * Retrieve a const pointer to the storage from the bin
679 * for the given index.
680 * @param index The vector index to access.
681 * @return A const pointer to the storage object at the given index.
682 */
683 const storage_t *data(int index) const {
684 return (const_cast<bin_t *>(&bin))->data(index, params);
685 }
686
687 protected:
688 // Copying stats is not allowed
689 /** Copying stats isn't allowed. */
690 VectorBase(const VectorBase &stat);
691 /** Copying stats isn't allowed. */
692 const VectorBase &operator=(const VectorBase &);
693
694 public:
695 /**
696 * Copy the values to a local vector and return a reference to it.
697 * @return A reference to a vector of the stat values.
698 */
699 const rvec_t &val() const {
700 if (vec)
701 vec->resize(size());
702 else
703 vec = new rvec_t(size());
704
705 for (int i = 0; i < size(); ++i)
706 (*vec)[i] = data(i)->val(params);
707
708 return *vec;
709 }
710
711 /**
712 * Return a total of all entries in this vector.
713 * @return The total of all vector entries.
714 */
715 result_t total() const {
716 result_t total = 0.0;
717 for (int i = 0; i < size(); ++i)
718 total += data(i)->val(params);
719 return total;
720 }
721
722 public:
723 /**
724 * Create this vector and register it with the database.
725 */
726 VectorBase() : VectorStat(true), vec(NULL) {}
727 /**
728 * Destructor.
729 */
730 ~VectorBase() { if (vec) delete vec; }
731
732 /**
733 * Set this vector to have the given size.
734 * @param size The new size.
735 * @return A reference to this stat.
736 */
737 VectorBase &init(size_t size) {
738 bin.init(size, params);
739 setInit();
740
741 return *this;
742 }
743
744 /** Friend this class with the associated scalar proxy. */
745 friend class ScalarProxy<T, Storage, Bin>;
746
747 /**
748 * Return a reference (ScalarProxy) to the stat at the given index.
749 * @param index The vector index to access.
750 * @return A reference of the stat.
751 */
752 ScalarProxy<T, Storage, Bin> operator[](int index);
753
754 /**
755 * Return the number of elements in this vector.
756 * @return The size of the vector.
757 */
758 virtual size_t size() const { return bin.size(); }
759 /**
760 * Reset stat value to default
761 */
762 virtual void reset() { bin.reset(); }
763 };
764
765 /**
766 * A proxy class to access the stat at a given index in a VectorBase stat.
767 * Behaves like a ScalarBase.
768 */
769 template <typename T, template <typename T> class Storage, class Bin>
770 class ScalarProxy : public ScalarStat
771 {
772 protected:
773 /** Define the type of the storage class. */
774 typedef Storage<T> storage_t;
775 /** Define the params of the storage class. */
776 typedef typename storage_t::Params params_t;
777 /** Define the bin type. */
778 typedef typename Bin::VectorBin<storage_t> bin_t;
779
780 private:
781 /** Pointer to the bin in the parent VectorBase. */
782 bin_t *bin;
783 /** Pointer to the params in the parent VectorBase. */
784 params_t *params;
785 /** The index to access in the parent VectorBase. */
786 int index;
787
788 protected:
789 /**
790 * Retrieve the storage from the bin.
791 * @return The storage from the bin for this stat.
792 */
793 storage_t *data() { return bin->data(index, *params); }
794 /**
795 * Retrieve a const pointer to the storage from the bin.
796 * @return A const pointer to the storage for this stat.
797 */
798 const storage_t *data() const { return bin->data(index, *params); }
799
800 public:
801 /**
802 * Return the current value of this statas a result type.
803 * @return The current value.
804 */
805 result_t val() const { return data()->val(*params); }
806 /**
807 * Return the current value of this stat as its base type.
808 * @return The current value.
809 */
810 T value() const { return data()->value(*params); }
811
812 public:
813 /**
814 * Create and initialize this proxy, do not register it with the database.
815 * @param b The bin to use.
816 * @param p The params to use.
817 * @param i The index to access.
818 */
819 ScalarProxy(bin_t &b, params_t &p, int i)
820 : ScalarStat(false), bin(&b), params(&p), index(i) {}
821 /**
822 * Create a copy of the provided ScalarProxy.
823 * @param sp The proxy to copy.
824 */
825 ScalarProxy(const ScalarProxy &sp)
826 : ScalarStat(false), bin(sp.bin), params(sp.params), index(sp.index) {}
827 /**
828 * Set this proxy equal to the provided one.
829 * @param sp The proxy to copy.
830 * @return A reference to this proxy.
831 */
832 const ScalarProxy &operator=(const ScalarProxy &sp) {
833 bin = sp.bin;
834 params = sp.params;
835 index = sp.index;
836 return *this;
837 }
838
839 public:
840 // Common operators for stats
841 /**
842 * Increment the stat by 1. This calls the associated storage object inc
843 * function.
844 */
845 void operator++() { data()->inc(1, *params); }
846 /**
847 * Decrement the stat by 1. This calls the associated storage object dec
848 * function.
849 */
850 void operator--() { data()->dec(1, *params); }
851
852 /** Increment the stat by 1. */
853 void operator++(int) { ++*this; }
854 /** Decrement the stat by 1. */
855 void operator--(int) { --*this; }
856
857 /**
858 * Set the data value to the given value. This calls the associated storage
859 * object set function.
860 * @param v The new value.
861 */
862 template <typename U>
863 void operator=(const U& v) { data()->set(v, *params); }
864
865 /**
866 * Increment the stat by the given value. This calls the associated
867 * storage object inc function.
868 * @param v The value to add.
869 */
870 template <typename U>
871 void operator+=(const U& v) { data()->inc(v, *params); }
872
873 /**
874 * Decrement the stat by the given value. This calls the associated
875 * storage object dec function.
876 * @param v The value to substract.
877 */
878 template <typename U>
879 void operator-=(const U& v) { data()->dec(v, *params); }
880
881 /**
882 * Return the number of elements, always 1 for a scalar.
883 * @return 1.
884 */
885 virtual size_t size() const { return 1; }
886 /**
887 * This stat has no state. Nothing to reset
888 */
889 virtual void reset() { }
890 };
891
892 template <typename T, template <typename T> class Storage, class Bin>
893 inline ScalarProxy<T, Storage, Bin>
894 VectorBase<T, Storage, Bin>::operator[](int index)
895 {
896 assert (index >= 0 && index < size());
897 return ScalarProxy<T, Storage, Bin>(bin, params, index);
898 }
899
900 template <typename T, template <typename T> class Storage, class Bin>
901 class VectorProxy;
902
903 template <typename T, template <typename T> class Storage, class Bin>
904 class Vector2dBase : public Stat
905 {
906 protected:
907 typedef Storage<T> storage_t;
908 typedef typename storage_t::Params params_t;
909 typedef typename Bin::VectorBin<storage_t> bin_t;
910
911 protected:
912 size_t x;
913 size_t y;
914 bin_t bin;
915 params_t params;
916 std::vector<std::string> *y_subnames;
917
918 protected:
919 storage_t *data(int index) { return bin.data(index, params); }
920 const storage_t *data(int index) const {
921 return (const_cast<bin_t *>(&bin))->data(index, params);
922 }
923
924 protected:
925 // Copying stats is not allowed
926 Vector2dBase(const Vector2dBase &stat);
927 const Vector2dBase &operator=(const Vector2dBase &);
928
929 public:
930 Vector2dBase() : Stat(true) {}
931 ~Vector2dBase() { }
932
933 Vector2dBase &init(size_t _x, size_t _y) {
934 x = _x;
935 y = _y;
936 bin.init(x * y, params);
937 setInit();
938 y_subnames = new std::vector<std::string>(y);
939
940 return *this;
941 }
942
943 /**
944 * @warning This makes the assumption that if you're gonna subnames a 2d
945 * vector, you're subnaming across all y
946 */
947 Vector2dBase &ysubnames(const char **names)
948 {
949 for (int i=0; i < y; ++i) {
950 (*y_subnames)[i] = names[i];
951 }
952 return *this;
953 }
954 Vector2dBase &ysubname(int index, const std::string subname)
955 {
956 (*y_subnames)[i] = subname.c_str();
957 return *this;
958 }
959 std::string ysubname(int i) const { return (*y_subnames)[i]; }
960
961 friend class VectorProxy<T, Storage, Bin>;
962 VectorProxy<T, Storage, Bin> operator[](int index);
963
964 virtual size_t size() const { return bin.size(); }
965 virtual bool zero() const { return data(0)->value(params) == 0.0; }
966
967 virtual void
968 display(std::ostream &out) const
969 {
970 bool have_subname = false;
971 for (int i = 0; i < x; ++i) {
972 if (!mysubname(i).empty())
973 have_subname = true;
974 }
975
976 rvec_t tot_vec(y);
977 result_t super_total = 0.0;
978 for (int i = 0; i < x; ++i) {
979 std::string subname;
980 if (have_subname) {
981 subname = mysubname(i);
982 if (subname.empty())
983 continue;
984 } else
985 subname = to_string(i);
986
987 int iy = i * y;
988 rvec_t vec(y);
989
990 result_t total = 0.0;
991 for (int j = 0; j < y; ++j) {
992 vec[j] = data(iy + j)->val(params);
993 tot_vec[j] += vec[j];
994 total += vec[j];
995 super_total += vec[j];
996 }
997
998 std::string desc;
999 if (mysubdesc(i).empty()) {
1000 desc = mydesc();
1001 } else {
1002 desc = mysubdesc(i);
1003 }
1004
1005 VectorDisplay(out, myname() + "_" + subname, y_subnames, desc, 0,
1006 myprecision(), myflags(), vec, total);
1007
1008 }
1009 if ((myflags() & ::Statistics::total) && (x > 1)) {
1010 VectorDisplay(out, myname(), y_subnames, mydesc(), 0,
1011 myprecision(), myflags(), tot_vec, super_total);
1012
1013 }
1014 }
1015 /**
1016 * Reset stat value to default
1017 */
1018 virtual void reset() { bin.reset(); }
1019 };
1020
1021 template <typename T, template <typename T> class Storage, class Bin>
1022 class VectorProxy : public VectorStat
1023 {
1024 protected:
1025 typedef Storage<T> storage_t;
1026 typedef typename storage_t::Params params_t;
1027 typedef typename Bin::VectorBin<storage_t> bin_t;
1028
1029 private:
1030 bin_t *bin;
1031 params_t *params;
1032 int offset;
1033 int len;
1034
1035 private:
1036 mutable rvec_t *vec;
1037
1038 storage_t *data(int index) {
1039 assert(index < len);
1040 return bin->data(offset + index, *params);
1041 }
1042
1043 const storage_t *data(int index) const {
1044 return (const_cast<bin_t *>(bin))->data(offset + index, *params);
1045 }
1046
1047 public:
1048 const rvec_t &val() const {
1049 if (vec)
1050 vec->resize(size());
1051 else
1052 vec = new rvec_t(size());
1053
1054 for (int i = 0; i < size(); ++i)
1055 (*vec)[i] = data(i)->val(*params);
1056
1057 return *vec;
1058 }
1059
1060 result_t total() const {
1061 result_t total = 0.0;
1062 for (int i = 0; i < size(); ++i)
1063 total += data(i)->val(*params);
1064 return total;
1065 }
1066
1067 public:
1068 VectorProxy(bin_t &b, params_t &p, int o, int l)
1069 : VectorStat(false), bin(&b), params(&p), offset(o), len(l), vec(NULL)
1070 { }
1071 VectorProxy(const VectorProxy &sp)
1072 : VectorStat(false), bin(sp.bin), params(sp.params), offset(sp.offset),
1073 len(sp.len), vec(NULL)
1074 { }
1075 ~VectorProxy() {
1076 if (vec)
1077 delete vec;
1078 }
1079
1080 const VectorProxy &operator=(const VectorProxy &sp) {
1081 bin = sp.bin;
1082 params = sp.params;
1083 offset = sp.offset;
1084 len = sp.len;
1085 if (vec)
1086 delete vec;
1087 vec = NULL;
1088 return *this;
1089 }
1090
1091 virtual size_t size() const { return len; }
1092
1093 ScalarProxy<T, Storage, Bin> operator[](int index) {
1094 assert (index >= 0 && index < size());
1095 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index);
1096 }
1097
1098 /**
1099 * This stat has no state. Nothing to reset.
1100 */
1101 virtual void reset() { }
1102 };
1103
1104 template <typename T, template <typename T> class Storage, class Bin>
1105 inline VectorProxy<T, Storage, Bin>
1106 Vector2dBase<T, Storage, Bin>::operator[](int index)
1107 {
1108 int offset = index * y;
1109 assert (index >= 0 && offset < size());
1110 return VectorProxy<T, Storage, Bin>(bin, params, offset, y);
1111 }
1112
1113 //////////////////////////////////////////////////////////////////////
1114 //
1115 // Non formula statistics
1116 //
1117 //////////////////////////////////////////////////////////////////////
1118
1119 void DistDisplay(std::ostream &stream, const std::string &name,
1120 const std::string &desc, int precision, FormatFlags flags,
1121 result_t min_val, result_t max_val,
1122 result_t underflow, result_t overflow,
1123 const rvec_t &vec, int min, int max, int bucket_size,
1124 int size);
1125 /**
1126 * Templatized storage and interface for a distrbution stat.
1127 */
1128 template <typename T>
1129 struct DistStor
1130 {
1131 public:
1132 /** The parameters for a distribution stat. */
1133 struct Params
1134 {
1135 /** The minimum value to track. */
1136 int min;
1137 /** The maximum value to track. */
1138 int max;
1139 /** The number of entries in each bucket. */
1140 int bucket_size;
1141 /** The number of buckets. Equal to (max-min)/bucket_size. */
1142 int size;
1143 };
1144
1145 private:
1146 /** The smallest value sampled. */
1147 T min_val;
1148 /** The largest value sampled. */
1149 T max_val;
1150 /** The number of values sampled less than min. */
1151 T underflow;
1152 /** The number of values sampled more than max. */
1153 T overflow;
1154 /** Counter for each bucket. */
1155 std::vector<T> vec;
1156
1157 public:
1158 /**
1159 * Construct this storage with the supplied params.
1160 * @param params The parameters.
1161 */
1162 DistStor(const Params &params)
1163 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
1164 vec(params.size)
1165 {
1166 reset();
1167 }
1168
1169 /**
1170 * Add a value to the distribution for the given number of times.
1171 * @param val The value to add.
1172 * @param number The number of times to add the value.
1173 * @param params The paramters of the distribution.
1174 */
1175 void sample(T val, int number, const Params &params) {
1176 if (val < params.min)
1177 underflow += number;
1178 else if (val > params.max)
1179 overflow += number;
1180 else {
1181 int index = (val - params.min) / params.bucket_size;
1182 assert(index < size(params));
1183 vec[index] += number;
1184 }
1185
1186 if (val < min_val)
1187 min_val = val;
1188
1189 if (val > max_val)
1190 max_val = val;
1191 }
1192
1193 /**
1194 * Return the number of buckets in this distribution.
1195 * @return the number of buckets.
1196 * @todo Is it faster to return the size from the parameters?
1197 */
1198 size_t size(const Params &) const { return vec.size(); }
1199
1200 /**
1201 * Returns true if any calls to sample have been made.
1202 * @param params The paramters of the distribution.
1203 * @return True if any values have been sampled.
1204 */
1205 bool zero(const Params &params) const {
1206 if (underflow != 0 || overflow != 0)
1207 return false;
1208
1209 int s = size(params);
1210 for (int i = 0; i < s; i++)
1211 if (vec[i] != 0)
1212 return false;
1213
1214 return true;
1215 }
1216
1217 /**
1218 * Print this distribution and the given print data to the given ostream.
1219 * @param stream The output stream.
1220 * @param name The name of this stat (from StatData).
1221 * @param desc The description of this stat (from StatData).
1222 * @param precision The print precision (from StatData).
1223 * @param flags The format flags (from StatData).
1224 * @param params The paramters of this distribution.
1225 */
1226 void display(std::ostream &stream, const std::string &name,
1227 const std::string &desc, int precision, FormatFlags flags,
1228 const Params &params) const {
1229
1230 #ifdef STAT_DISPLAY_COMPAT
1231 result_t min = params.min;
1232 #else
1233 result_t min = (min_val == INT_MAX) ? params.min : min_val;
1234 #endif
1235 result_t max = (max_val == INT_MIN) ? 0 : max_val;
1236
1237 rvec_t rvec(params.size);
1238 for (int i = 0; i < params.size; ++i)
1239 rvec[i] = vec[i];
1240
1241 DistDisplay(stream, name, desc, precision, flags,
1242 (result_t)min, (result_t)max,
1243 (result_t)underflow, (result_t)overflow,
1244 rvec, params.min, params.max, params.bucket_size,
1245 params.size);
1246 }
1247 /**
1248 * Reset stat value to default
1249 */
1250 void reset()
1251 {
1252 min_val = INT_MAX;
1253 max_val = INT_MIN;
1254 underflow = 0;
1255 overflow = 0;
1256
1257 int size = vec.size();
1258 for (int i = 0; i < size; ++i)
1259 vec[i] = T();
1260 }
1261
1262 };
1263
1264 void FancyDisplay(std::ostream &stream, const std::string &name,
1265 const std::string &desc, int precision, FormatFlags flags,
1266 result_t mean, result_t variance);
1267
1268 /**
1269 * Templatized storage and interface for a distribution that calculates mean
1270 * and variance.
1271 */
1272 template <typename T>
1273 struct FancyStor
1274 {
1275 public:
1276 /**
1277 * No paramters for this storage.
1278 */
1279 struct Params {};
1280
1281 private:
1282 /** The current sum. */
1283 T sum;
1284 /** The sum of squares. */
1285 T squares;
1286 /** The total number of samples. */
1287 int total;
1288
1289 public:
1290 /**
1291 * Create and initialize this storage.
1292 */
1293 FancyStor(const Params &) : sum(T()), squares(T()), total(0) {}
1294
1295 /**
1296 * Add a value the given number of times to this running average.
1297 * Update the running sum and sum of squares, increment the number of
1298 * values seen by the given number.
1299 * @param val The value to add.
1300 * @param number The number of times to add the value.
1301 * @param p The parameters of this stat.
1302 */
1303 void sample(T val, int number, const Params &p) {
1304 T value = val * number;
1305 sum += value;
1306 squares += value * value;
1307 total += number;
1308 }
1309
1310 /**
1311 * Print this distribution and the given print data to the given ostream.
1312 * @param stream The output stream.
1313 * @param name The name of this stat (from StatData).
1314 * @param desc The description of this stat (from StatData).
1315 * @param precision The print precision (from StatData).
1316 * @param flags The format flags (from StatData).
1317 * @param params The paramters of this distribution.
1318 */
1319 void display(std::ostream &stream, const std::string &name,
1320 const std::string &desc, int precision, FormatFlags flags,
1321 const Params &params) const {
1322
1323 result_t mean = NAN;
1324 result_t variance = NAN;
1325
1326 if (total != 0) {
1327 result_t fsum = sum;
1328 result_t fsq = squares;
1329 result_t ftot = total;
1330
1331 mean = fsum / ftot;
1332 variance = (ftot * fsq - (fsum * fsum)) / (ftot * (ftot - 1.0));
1333 }
1334
1335 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1336 }
1337
1338 /**
1339 * Return the number of entries in this stat, 1
1340 * @return 1.
1341 */
1342 size_t size(const Params &) const { return 1; }
1343 /**
1344 * Return true if no samples have been added.
1345 * @return True if no samples have been added.
1346 */
1347 bool zero(const Params &) const { return total == 0; }
1348 /**
1349 * Reset stat value to default
1350 */
1351 virtual void reset()
1352 {
1353 sum = T();
1354 squares = T();
1355 total = 0;
1356 }
1357 };
1358
1359 /**
1360 * Templatized storage for distribution that calculates per cycle mean and
1361 * variance.
1362 */
1363 template <typename T>
1364 struct AvgFancy
1365 {
1366 public:
1367 /** No parameters for this storage. */
1368 struct Params {};
1369
1370 private:
1371 /** Current total. */
1372 T sum;
1373 /** Current sum of squares. */
1374 T squares;
1375
1376 public:
1377 /**
1378 * Create and initialize this storage.
1379 */
1380 AvgFancy(const Params &) : sum(T()), squares(T()) {}
1381
1382 /**
1383 * Add a value to the distribution for the given number of times.
1384 * Update the running sum and sum of squares.
1385 * @param val The value to add.
1386 * @param number The number of times to add the value.
1387 * @param p The paramters of the distribution.
1388 */
1389 void sample(T val, int number, const Params& p) {
1390 T value = val * number;
1391 sum += value;
1392 squares += value * value;
1393 }
1394
1395 /**
1396 * Print this distribution and the given print data to the given ostream.
1397 * @param stream The output stream.
1398 * @param name The name of this stat (from StatData).
1399 * @param desc The description of this stat (from StatData).
1400 * @param precision The print precision (from StatData).
1401 * @param flags The format flags (from StatData).
1402 * @param params The paramters of this distribution.
1403 */
1404 void display(std::ostream &stream, const std::string &name,
1405 const std::string &desc, int precision, FormatFlags flags,
1406 const Params &params) const {
1407 result_t mean = sum / curTick;
1408 result_t variance = (squares - sum * sum) / curTick;
1409
1410 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1411 }
1412
1413 /**
1414 * Return the number of entries, in this case 1.
1415 * @return 1.
1416 */
1417 size_t size(const Params &params) const { return 1; }
1418 /**
1419 * Return true if no samples have been added.
1420 * @return True if the sum is zero.
1421 */
1422 bool zero(const Params &params) const { return sum == 0; }
1423 /**
1424 * Reset stat value to default
1425 */
1426 virtual void reset()
1427 {
1428 sum = T();
1429 squares = T();
1430 }
1431 };
1432
1433 /**
1434 * Implementation of a distribution stat. The type of distribution is
1435 * determined by the Storage template. @sa ScalarBase
1436 */
1437 template <typename T, template <typename T> class Storage, class Bin>
1438 class DistBase : public Stat
1439 {
1440 protected:
1441 /** Define the type of the storage class. */
1442 typedef Storage<T> storage_t;
1443 /** Define the params of the storage class. */
1444 typedef typename storage_t::Params params_t;
1445 /** Define the bin type. */
1446 typedef typename Bin::Bin<storage_t> bin_t;
1447
1448 protected:
1449 /** The bin of this stat. */
1450 bin_t bin;
1451 /** The parameters for this stat. */
1452 params_t params;
1453
1454 protected:
1455 /**
1456 * Retrieve the storage from the bin.
1457 * @return The storage object for this stat.
1458 */
1459 storage_t *data() { return bin.data(params); }
1460 /**
1461 * Retrieve a const pointer to the storage from the bin.
1462 * @return A const pointer to the storage object for this stat.
1463 */
1464 const storage_t *data() const {
1465 return (const_cast<bin_t *>(&bin))->data(params);
1466 }
1467
1468 protected:
1469 // Copying stats is not allowed
1470 /** Copies are not allowed. */
1471 DistBase(const DistBase &stat);
1472 /** Copies are not allowed. */
1473 const DistBase &operator=(const DistBase &);
1474
1475 public:
1476 /**
1477 * Create this distrubition and register it with the database.
1478 */
1479 DistBase() : Stat(true) { }
1480 /**
1481 * Destructor.
1482 */
1483 ~DistBase() { }
1484
1485 /**
1486 * Add a value to the distribtion n times. Calls sample on the storage
1487 * class.
1488 * @param v The value to add.
1489 * @param n The number of times to add it, defaults to 1.
1490 */
1491 template <typename U>
1492 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
1493
1494 /**
1495 * Return the number of entries in this stat.
1496 * @return The number of entries.
1497 */
1498 virtual size_t size() const { return data()->size(params); }
1499 /**
1500 * Return true if no samples have been added.
1501 * @return True if there haven't been any samples.
1502 */
1503 virtual bool zero() const { return data()->zero(params); }
1504 /**
1505 * Print this distribution to the given ostream.
1506 * @param stream The output stream.
1507 */
1508 virtual void display(std::ostream &stream) const {
1509 data()->display(stream, myname(), mydesc(), myprecision(), myflags(),
1510 params);
1511 }
1512 /**
1513 * Reset stat value to default
1514 */
1515 virtual void reset()
1516 {
1517 bin.reset();
1518 }
1519 };
1520
1521 template <typename T, template <typename T> class Storage, class Bin>
1522 class DistProxy;
1523
1524 template <typename T, template <typename T> class Storage, class Bin>
1525 class VectorDistBase : public Stat
1526 {
1527 protected:
1528 typedef Storage<T> storage_t;
1529 typedef typename storage_t::Params params_t;
1530 typedef typename Bin::VectorBin<storage_t> bin_t;
1531
1532 protected:
1533 bin_t bin;
1534 params_t params;
1535
1536 protected:
1537 storage_t *data(int index) { return bin.data(index, params); }
1538 const storage_t *data(int index) const {
1539 return (const_cast<bin_t *>(&bin))->data(index, params);
1540 }
1541
1542 protected:
1543 // Copying stats is not allowed
1544 VectorDistBase(const VectorDistBase &stat);
1545 const VectorDistBase &operator=(const VectorDistBase &);
1546
1547 public:
1548 VectorDistBase() : Stat(true) { }
1549 ~VectorDistBase() { }
1550
1551 friend class DistProxy<T, Storage, Bin>;
1552 DistProxy<T, Storage, Bin> operator[](int index);
1553 const DistProxy<T, Storage, Bin> operator[](int index) const;
1554
1555 virtual size_t size() const { return bin.size(); }
1556 virtual bool zero() const { return false; }
1557 virtual void display(std::ostream &stream) const;
1558 /**
1559 * Reset stat value to default
1560 */
1561 virtual void reset()
1562 {
1563 bin.reset();
1564 }
1565 };
1566
1567 template <typename T, template <typename T> class Storage, class Bin>
1568 class DistProxy : public Stat
1569 {
1570 protected:
1571 typedef Storage<T> storage_t;
1572 typedef typename storage_t::Params params_t;
1573 typedef typename Bin::Bin<storage_t> bin_t;
1574 typedef VectorDistBase<T, Storage, Bin> base_t;
1575
1576 private:
1577 union {
1578 base_t *stat;
1579 const base_t *cstat;
1580 };
1581 int index;
1582
1583 protected:
1584 storage_t *data() { return stat->data(index); }
1585 const storage_t *data() const { return cstat->data(index); }
1586
1587 public:
1588 DistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1589 : Stat(false), cstat(&s), index(i) {}
1590 DistProxy(const DistProxy &sp)
1591 : Stat(false), cstat(sp.cstat), index(sp.index) {}
1592 const DistProxy &operator=(const DistProxy &sp) {
1593 cstat = sp.cstat; index = sp.index; return *this;
1594 }
1595
1596 public:
1597 template <typename U>
1598 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
1599
1600 virtual size_t size() const { return 1; }
1601 virtual bool zero() const {
1602 return data()->zero(cstat->params);
1603 }
1604 virtual void display(std::ostream &stream) const {
1605 std::stringstream name, desc;
1606
1607 if (!(cstat->mysubname(index).empty())) {
1608 name << cstat->myname() << cstat->mysubname(index);
1609 } else {
1610 name << cstat->myname() << "_" << index;
1611 }
1612 if (!(cstat->mysubdesc(index).empty())) {
1613 desc << cstat->mysubdesc(index);
1614 } else {
1615 desc << cstat->mydesc();
1616 }
1617
1618 data()->display(stream, name.str(), desc.str(),
1619 cstat->myprecision(), cstat->myflags(), cstat->params);
1620 }
1621 /**
1622 * Proxy has no state. Nothing to reset.
1623 */
1624 virtual void reset() { }
1625 };
1626
1627 template <typename T, template <typename T> class Storage, class Bin>
1628 inline DistProxy<T, Storage, Bin>
1629 VectorDistBase<T, Storage, Bin>::operator[](int index)
1630 {
1631 assert (index >= 0 && index < size());
1632 return DistProxy<T, Storage, Bin>(*this, index);
1633 }
1634
1635 template <typename T, template <typename T> class Storage, class Bin>
1636 inline const DistProxy<T, Storage, Bin>
1637 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1638 {
1639 assert (index >= 0 && index < size());
1640 return DistProxy<T, Storage, Bin>(*this, index);
1641 }
1642
1643 /**
1644 * @todo Need a way to print Distribution totals across the Vector
1645 */
1646 template <typename T, template <typename T> class Storage, class Bin>
1647 void
1648 VectorDistBase<T, Storage, Bin>::display(std::ostream &stream) const
1649 {
1650 for (int i = 0; i < size(); ++i) {
1651 DistProxy<T, Storage, Bin> proxy(*this, i);
1652 proxy.display(stream);
1653 }
1654 }
1655
1656 #if 0
1657 result_t
1658 VectorDistBase<T, Storage, Bin>::total(int index) const
1659 {
1660 int total = 0;
1661 for (int i=0; i < x_size(); ++i) {
1662 total += data(i)->val(*params);
1663 }
1664 }
1665 #endif
1666
1667 //////////////////////////////////////////////////////////////////////
1668 //
1669 // Formula Details
1670 //
1671 //////////////////////////////////////////////////////////////////////
1672
1673 /**
1674 * Base class for formula statistic node. These nodes are used to build a tree
1675 * that represents the formula.
1676 */
1677 class Node : public RefCounted
1678 {
1679 public:
1680 /**
1681 * Return the number of nodes in the subtree starting at this node.
1682 * @return the number of nodes in this subtree.
1683 */
1684 virtual size_t size() const = 0;
1685 /**
1686 * Return the result vector of this subtree.
1687 * @return The result vector of this subtree.
1688 */
1689 virtual const rvec_t &val() const = 0;
1690 /**
1691 * Return the total of the result vector.
1692 * @return The total of the result vector.
1693 */
1694 virtual result_t total() const = 0;
1695 };
1696
1697 /** Reference counting pointer to a function Node. */
1698 typedef RefCountingPtr<Node> NodePtr;
1699
1700 class ScalarStatNode : public Node
1701 {
1702 private:
1703 const ScalarStat &stat;
1704 mutable rvec_t result;
1705
1706 public:
1707 ScalarStatNode(const ScalarStat &s) : stat(s), result(1) {}
1708 const rvec_t &val() const { result[0] = stat.val(); return result; }
1709 virtual result_t total() const { return stat.val(); };
1710
1711 virtual size_t size() const { return 1; }
1712 };
1713
1714 template <typename T, template <typename T> class Storage, class Bin>
1715 class ScalarProxyNode : public Node
1716 {
1717 private:
1718 const ScalarProxy<T, Storage, Bin> proxy;
1719 mutable rvec_t result;
1720
1721 public:
1722 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1723 : proxy(p), result(1) { }
1724 const rvec_t &val() const { result[0] = proxy.val(); return result; }
1725 virtual result_t total() const { return proxy.val(); };
1726
1727 virtual size_t size() const { return 1; }
1728 };
1729
1730 class VectorStatNode : public Node
1731 {
1732 private:
1733 const VectorStat &stat;
1734
1735 public:
1736 VectorStatNode(const VectorStat &s) : stat(s) {}
1737 const rvec_t &val() const { return stat.val(); }
1738 virtual result_t total() const { return stat.total(); };
1739
1740 virtual size_t size() const { return stat.size(); }
1741 };
1742
1743 template <typename T>
1744 class ConstNode : public Node
1745 {
1746 private:
1747 rvec_t data;
1748
1749 public:
1750 ConstNode(T s) : data(1, (result_t)s) {}
1751 const rvec_t &val() const { return data; }
1752 virtual result_t total() const { return data[0]; };
1753
1754 virtual size_t size() const { return 1; }
1755 };
1756
1757 template <typename T>
1758 class FunctorNode : public Node
1759 {
1760 private:
1761 T &functor;
1762 mutable rvec_t result;
1763
1764 public:
1765 FunctorNode(T &f) : functor(f) { result.resize(1); }
1766 const rvec_t &val() const {
1767 result[0] = (result_t)functor();
1768 return result;
1769 }
1770 virtual result_t total() const { return (result_t)functor(); };
1771
1772 virtual size_t size() const { return 1; }
1773 };
1774
1775 template <typename T>
1776 class ScalarNode : public Node
1777 {
1778 private:
1779 T &scalar;
1780 mutable rvec_t result;
1781
1782 public:
1783 ScalarNode(T &s) : scalar(s) { result.resize(1); }
1784 const rvec_t &val() const {
1785 result[0] = (result_t)scalar;
1786 return result;
1787 }
1788 virtual result_t total() const { return (result_t)scalar; };
1789
1790 virtual size_t size() const { return 1; }
1791 };
1792
1793 template <class Op>
1794 class UnaryNode : public Node
1795 {
1796 public:
1797 NodePtr l;
1798 mutable rvec_t result;
1799
1800 public:
1801 UnaryNode(NodePtr p) : l(p) {}
1802
1803 const rvec_t &val() const {
1804 const rvec_t &lvec = l->val();
1805 int size = lvec.size();
1806
1807 assert(size > 0);
1808
1809 result.resize(size);
1810 Op op;
1811 for (int i = 0; i < size; ++i)
1812 result[i] = op(lvec[i]);
1813
1814 return result;
1815 }
1816
1817 result_t total() const {
1818 Op op;
1819 return op(l->total());
1820 }
1821
1822 virtual size_t size() const { return l->size(); }
1823 };
1824
1825 template <class Op>
1826 class BinaryNode : public Node
1827 {
1828 public:
1829 NodePtr l;
1830 NodePtr r;
1831 mutable rvec_t result;
1832
1833 public:
1834 BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {}
1835
1836 const rvec_t &val() const {
1837 Op op;
1838 const rvec_t &lvec = l->val();
1839 const rvec_t &rvec = r->val();
1840
1841 assert(lvec.size() > 0 && rvec.size() > 0);
1842
1843 if (lvec.size() == 1 && rvec.size() == 1) {
1844 result.resize(1);
1845 result[0] = op(lvec[0], rvec[0]);
1846 } else if (lvec.size() == 1) {
1847 int size = rvec.size();
1848 result.resize(size);
1849 for (int i = 0; i < size; ++i)
1850 result[i] = op(lvec[0], rvec[i]);
1851 } else if (rvec.size() == 1) {
1852 int size = lvec.size();
1853 result.resize(size);
1854 for (int i = 0; i < size; ++i)
1855 result[i] = op(lvec[i], rvec[0]);
1856 } else if (rvec.size() == lvec.size()) {
1857 int size = rvec.size();
1858 result.resize(size);
1859 for (int i = 0; i < size; ++i)
1860 result[i] = op(lvec[i], rvec[i]);
1861 }
1862
1863 return result;
1864 }
1865
1866 result_t total() const {
1867 Op op;
1868 return op(l->total(), r->total());
1869 }
1870
1871 virtual size_t size() const {
1872 int ls = l->size();
1873 int rs = r->size();
1874 if (ls == 1)
1875 return rs;
1876 else if (rs == 1)
1877 return ls;
1878 else {
1879 assert(ls == rs && "Node vector sizes are not equal");
1880 return ls;
1881 }
1882 }
1883 };
1884
1885 template <class Op>
1886 class SumNode : public Node
1887 {
1888 public:
1889 NodePtr l;
1890 mutable rvec_t result;
1891
1892 public:
1893 SumNode(NodePtr p) : l(p), result(1) {}
1894
1895 const rvec_t &val() const {
1896 const rvec_t &lvec = l->val();
1897 int size = lvec.size();
1898 assert(size > 0);
1899
1900 result[0] = 0.0;
1901
1902 Op op;
1903 for (int i = 0; i < size; ++i)
1904 result[0] = op(result[0], lvec[i]);
1905
1906 return result;
1907 }
1908
1909 result_t total() const {
1910 const rvec_t &lvec = l->val();
1911 int size = lvec.size();
1912 assert(size > 0);
1913
1914 result_t result = 0.0;
1915
1916 Op op;
1917 for (int i = 0; i < size; ++i)
1918 result = op(result, lvec[i]);
1919
1920 return result;
1921 }
1922
1923 virtual size_t size() const { return 1; }
1924 };
1925
1926 /**
1927 * Helper class to construct formula node trees.
1928 */
1929 class Temp
1930 {
1931 private:
1932 /**
1933 * Pointer to a Node object.
1934 */
1935 NodePtr node;
1936
1937 public:
1938 /**
1939 * Copy the given pointer to this class.
1940 * @param n A pointer to a Node object to copy.
1941 */
1942 Temp(NodePtr n) : node(n) {}
1943 /**
1944 * Create a new ScalarStatNode.
1945 * @param s The ScalarStat to place in a node.
1946 */
1947 Temp(const ScalarStat &s) : node(new ScalarStatNode(s)) {}
1948 /**
1949 * Create a new ScalarProxyNode.
1950 * @param p The ScalarProxy to place in a node.
1951 */
1952 template <typename T, template <typename T> class Storage, class Bin>
1953 Temp(const ScalarProxy<T, Storage, Bin> &p)
1954 : node(new ScalarProxyNode<T, Storage, Bin>(p)) {}
1955 /**
1956 * Create a new VectorStatNode.
1957 * @param s The VectorStat to place in a node.
1958 */
1959 Temp(const VectorStat &s) : node(new VectorStatNode(s)) {}
1960
1961 /**
1962 * Create a ConstNode
1963 * @param value The value of the const node.
1964 */
1965 Temp(signed char value) : node(new ConstNode<signed char>(value)) {}
1966 /**
1967 * Create a ConstNode
1968 * @param value The value of the const node.
1969 */
1970 Temp(unsigned char value) : node(new ConstNode<unsigned char>(value)) {}
1971 /**
1972 * Create a ConstNode
1973 * @param value The value of the const node.
1974 */
1975 Temp(signed short value) : node(new ConstNode<signed short>(value)) {}
1976 /**
1977 * Create a ConstNode
1978 * @param value The value of the const node.
1979 */
1980 Temp(unsigned short value) : node(new ConstNode<unsigned short>(value)) {}
1981 /**
1982 * Create a ConstNode
1983 * @param value The value of the const node.
1984 */
1985 Temp(signed int value) : node(new ConstNode<signed int>(value)) {}
1986 /**
1987 * Create a ConstNode
1988 * @param value The value of the const node.
1989 */
1990 Temp(unsigned int value) : node(new ConstNode<unsigned int>(value)) {}
1991 /**
1992 * Create a ConstNode
1993 * @param value The value of the const node.
1994 */
1995 Temp(signed long value) : node(new ConstNode<signed long>(value)) {}
1996 /**
1997 * Create a ConstNode
1998 * @param value The value of the const node.
1999 */
2000 Temp(unsigned long value) : node(new ConstNode<unsigned long>(value)) {}
2001 /**
2002 * Create a ConstNode
2003 * @param value The value of the const node.
2004 */
2005 Temp(signed long long value)
2006 : node(new ConstNode<signed long long>(value)) {}
2007 /**
2008 * Create a ConstNode
2009 * @param value The value of the const node.
2010 */
2011 Temp(unsigned long long value)
2012 : node(new ConstNode<unsigned long long>(value)) {}
2013 /**
2014 * Create a ConstNode
2015 * @param value The value of the const node.
2016 */
2017 Temp(float value) : node(new ConstNode<float>(value)) {}
2018 /**
2019 * Create a ConstNode
2020 * @param value The value of the const node.
2021 */
2022 Temp(double value) : node(new ConstNode<double>(value)) {}
2023
2024 /**
2025 * Return the node pointer.
2026 * @return the node pointer.
2027 */
2028 operator NodePtr() { return node;}
2029 };
2030
2031
2032 //////////////////////////////////////////////////////////////////////
2033 //
2034 // Binning Interface
2035 //
2036 //////////////////////////////////////////////////////////////////////
2037
2038 class BinBase
2039 {
2040 private:
2041 off_t memsize;
2042 char *mem;
2043
2044 protected:
2045 off_t size() const { return memsize; }
2046 char *memory();
2047
2048 public:
2049 BinBase(size_t size);
2050 ~BinBase();
2051 };
2052
2053 } // namespace Detail
2054
2055 template <class BinType>
2056 struct StatBin : public Detail::BinBase
2057 {
2058 static StatBin *&curBin() {
2059 static StatBin *current = NULL;
2060 return current;
2061 }
2062
2063 static void setCurBin(StatBin *bin) { curBin() = bin; }
2064 static StatBin *current() { assert(curBin()); return curBin(); }
2065
2066 static off_t &offset() {
2067 static off_t offset = 0;
2068 return offset;
2069 }
2070
2071 static off_t new_offset(size_t size) {
2072 size_t mask = sizeof(u_int64_t) - 1;
2073 off_t off = offset();
2074
2075 // That one is for the last trailing flags byte.
2076 offset() += (size + 1 + mask) & ~mask;
2077
2078 return off;
2079 }
2080
2081 explicit StatBin(size_t size = 1024) : Detail::BinBase(size) {}
2082
2083 char *memory(off_t off) {
2084 assert(offset() <= size());
2085 return Detail::BinBase::memory() + off;
2086 }
2087
2088 static void activate(StatBin &bin) { setCurBin(&bin); }
2089
2090 class BinBase
2091 {
2092 private:
2093 int offset;
2094
2095 public:
2096 BinBase() : offset(-1) {}
2097 void allocate(size_t size) {
2098 offset = new_offset(size);
2099 }
2100 char *access() {
2101 assert(offset != -1);
2102 return current()->memory(offset);
2103 }
2104 };
2105
2106 template <class Storage>
2107 class Bin : public BinBase
2108 {
2109 public:
2110 typedef typename Storage::Params Params;
2111
2112 public:
2113 Bin() { allocate(sizeof(Storage)); }
2114 bool initialized() const { return true; }
2115 void init(const Params &params) { }
2116
2117 int size() const { return 1; }
2118
2119 Storage *data(const Params &params) {
2120 assert(initialized());
2121 char *ptr = access();
2122 char *flags = ptr + sizeof(Storage);
2123 if (!(*flags & 0x1)) {
2124 *flags |= 0x1;
2125 new (ptr) Storage(params);
2126 }
2127 return reinterpret_cast<Storage *>(ptr);
2128 }
2129 void reset()
2130 {
2131 char *ptr = access();
2132 char *flags = ptr + size() * sizeof(Storage);
2133 if (!(*flags & 0x1))
2134 return;
2135
2136 Storage *s = reinterpret_cast<Storage *>(ptr);
2137 s->reset();
2138 }
2139 };
2140
2141 template <class Storage>
2142 class VectorBin : public BinBase
2143 {
2144 public:
2145 typedef typename Storage::Params Params;
2146
2147 private:
2148 int _size;
2149
2150 public:
2151 VectorBin() : _size(0) {}
2152
2153 bool initialized() const { return _size > 0; }
2154 void init(int s, const Params &params) {
2155 assert(!initialized());
2156 assert(s > 0);
2157 _size = s;
2158 allocate(_size * sizeof(Storage));
2159 }
2160
2161 int size() const { return _size; }
2162
2163 Storage *data(int index, const Params &params) {
2164 assert(initialized());
2165 assert(index >= 0 && index < size());
2166 char *ptr = access();
2167 char *flags = ptr + size() * sizeof(Storage);
2168 if (!(*flags & 0x1)) {
2169 *flags |= 0x1;
2170 for (int i = 0; i < size(); ++i)
2171 new (ptr + i * sizeof(Storage)) Storage(params);
2172 }
2173 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2174 }
2175 void reset()
2176 {
2177 char *ptr = access();
2178 char *flags = ptr + size() * sizeof(Storage);
2179 if (!(*flags & 0x1))
2180 return;
2181
2182 for (int i = 0; i < _size; ++i) {
2183 char *p = ptr + i * sizeof(Storage);
2184 Storage *s = reinterpret_cast<Storage *>(p);
2185 s->reset();
2186 }
2187 }
2188 };
2189 };
2190
2191 class MainBinType {};
2192 typedef StatBin<MainBinType> MainBin;
2193
2194 struct NoBin
2195 {
2196 template <class Storage>
2197 struct Bin
2198 {
2199 public:
2200 typedef typename Storage::Params Params;
2201
2202 private:
2203 char ptr[sizeof(Storage)];
2204
2205 public:
2206 ~Bin()
2207 {
2208 reinterpret_cast<Storage *>(ptr)->~Storage();
2209 }
2210
2211 bool initialized() const { return true; }
2212 void init(const Params &params) {
2213 new (ptr) Storage(params);
2214 }
2215 int size() const{ return 1; }
2216 Storage *data(const Params &params) {
2217 assert(initialized());
2218 return reinterpret_cast<Storage *>(ptr);
2219 }
2220 void reset()
2221 {
2222 Storage *s = reinterpret_cast<Storage *>(ptr);
2223 s->reset();
2224 }
2225 };
2226
2227 template <class Storage>
2228 struct VectorBin
2229 {
2230 public:
2231 typedef typename Storage::Params Params;
2232
2233 private:
2234 char *ptr;
2235 int _size;
2236
2237 public:
2238 VectorBin() : ptr(NULL) { }
2239 ~VectorBin()
2240 {
2241 if (!initialized())
2242 return;
2243
2244 for (int i = 0; i < _size; ++i) {
2245 char *p = ptr + i * sizeof(Storage);
2246 reinterpret_cast<Storage *>(p)->~Storage();
2247 }
2248 delete [] ptr;
2249 }
2250
2251 bool initialized() const { return ptr != NULL; }
2252 void init(int s, const Params &params) {
2253 assert(s > 0 && "size must be positive!");
2254 assert(!initialized());
2255 _size = s;
2256 ptr = new char[_size * sizeof(Storage)];
2257 for (int i = 0; i < _size; ++i)
2258 new (ptr + i * sizeof(Storage)) Storage(params);
2259 }
2260
2261 int size() const { return _size; }
2262
2263 Storage *data(int index, const Params &params) {
2264 assert(initialized());
2265 assert(index >= 0 && index < size());
2266 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2267 }
2268 void reset()
2269 {
2270 for (int i = 0; i < _size; ++i) {
2271 char *p = ptr + i * sizeof(Storage);
2272 Storage *s = reinterpret_cast<Storage *>(p);
2273 s->reset();
2274 }
2275 }
2276 };
2277 };
2278
2279 //////////////////////////////////////////////////////////////////////
2280 //
2281 // Visible Statistics Types
2282 //
2283 //////////////////////////////////////////////////////////////////////
2284 /**
2285 * @defgroup VisibleStats "Statistic Types"
2286 * These are the statistics that are used in the simulator. By default these
2287 * store counters and don't use binning, but are templatized to accept any type
2288 * and any Bin class.
2289 * @{
2290 */
2291
2292 /**
2293 * This is a simple scalar statistic, like a counter.
2294 * @sa Stat, ScalarBase, StatStor
2295 */
2296 template <typename T = Counter, class Bin = NoBin>
2297 class Scalar : public Detail::ScalarBase<T, Detail::StatStor, Bin>
2298 {
2299 public:
2300 /** The base implementation. */
2301 typedef Detail::ScalarBase<T, Detail::StatStor, Bin> Base;
2302
2303 /**
2304 * Sets the stat equal to the given value. Calls the base implementation
2305 * of operator=
2306 * @param v The new value.
2307 */
2308 template <typename U>
2309 void operator=(const U& v) { Base::operator=(v); }
2310 };
2311
2312 /**
2313 * A stat that calculates the per cycle average of a value.
2314 * @sa Stat, ScalarBase, AvgStor
2315 */
2316 template <typename T = Counter, class Bin = NoBin>
2317 class Average : public Detail::ScalarBase<T, Detail::AvgStor, Bin>
2318 {
2319 public:
2320 /** The base implementation. */
2321 typedef Detail::ScalarBase<T, Detail::AvgStor, Bin> Base;
2322
2323 /**
2324 * Sets the stat equal to the given value. Calls the base implementation
2325 * of operator=
2326 * @param v The new value.
2327 */
2328 template <typename U>
2329 void operator=(const U& v) { Base::operator=(v); }
2330 };
2331
2332 /**
2333 * A vector of scalar stats.
2334 * @sa Stat, VectorBase, StatStor
2335 */
2336 template <typename T = Counter, class Bin = NoBin>
2337 class Vector : public Detail::VectorBase<T, Detail::StatStor, Bin>
2338 { };
2339
2340 /**
2341 * A vector of Average stats.
2342 * @sa Stat, VectorBase, AvgStor
2343 */
2344 template <typename T = Counter, class Bin = NoBin>
2345 class AverageVector : public Detail::VectorBase<T, Detail::AvgStor, Bin>
2346 { };
2347
2348 /**
2349 * A 2-Dimensional vecto of scalar stats.
2350 * @sa Stat, Vector2dBase, StatStor
2351 */
2352 template <typename T = Counter, class Bin = NoBin>
2353 class Vector2d : public Detail::Vector2dBase<T, Detail::StatStor, Bin>
2354 { };
2355
2356 /**
2357 * A simple distribution stat.
2358 * @sa Stat, DistBase, DistStor
2359 */
2360 template <typename T = Counter, class Bin = NoBin>
2361 class Distribution : public Detail::DistBase<T, Detail::DistStor, Bin>
2362 {
2363 private:
2364 /** Base implementation. */
2365 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2366 /** The Parameter type. */
2367 typedef typename Detail::DistStor<T>::Params Params;
2368
2369 public:
2370 /**
2371 * Set the parameters of this distribution. @sa DistStor::Params
2372 * @param min The minimum value of the distribution.
2373 * @param max The maximum value of the distribution.
2374 * @param bkt The number of values in each bucket.
2375 * @return A reference to this distribution.
2376 */
2377 Distribution &init(T min, T max, int bkt) {
2378 params.min = min;
2379 params.max = max;
2380 params.bucket_size = bkt;
2381 params.size = (max - min) / bkt + 1;
2382 bin.init(params);
2383 setInit();
2384
2385 return *this;
2386 }
2387 };
2388
2389 /**
2390 * Calculates the mean and variance of all the samples.
2391 * @sa Stat, DistBase, FancyStor
2392 */
2393 template <typename T = Counter, class Bin = NoBin>
2394 class StandardDeviation : public Detail::DistBase<T, Detail::FancyStor, Bin>
2395 {
2396 private:
2397 /** The base implementation */
2398 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2399 /** The parameter type. */
2400 typedef typename Detail::DistStor<T>::Params Params;
2401
2402 public:
2403 /**
2404 * Construct and initialize this distribution.
2405 */
2406 StandardDeviation() {
2407 bin.init(params);
2408 setInit();
2409 }
2410 };
2411
2412 /**
2413 * Calculates the per cycle mean and variance of the samples.
2414 * @sa Stat, DistBase, AvgFancy
2415 */
2416 template <typename T = Counter, class Bin = NoBin>
2417 class AverageDeviation : public Detail::DistBase<T, Detail::AvgFancy, Bin>
2418 {
2419 private:
2420 /** The base implementation */
2421 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2422 /** The parameter type. */
2423 typedef typename Detail::DistStor<T>::Params Params;
2424
2425 public:
2426 /**
2427 * Construct and initialize this distribution.
2428 */
2429 AverageDeviation() {
2430 bin.init(params);
2431 setInit();
2432 }
2433 };
2434
2435 /**
2436 * A vector of distributions.
2437 * @sa Stat, VectorDistBase, DistStor
2438 */
2439 template <typename T = Counter, class Bin = NoBin>
2440 class VectorDistribution
2441 : public Detail::VectorDistBase<T, Detail::DistStor, Bin>
2442 {
2443 private:
2444 /** The base implementation */
2445 typedef Detail::VectorDistBase<T, Detail::DistStor, Bin> Base;
2446 /** The parameter type. */
2447 typedef typename Detail::DistStor<T>::Params Params;
2448
2449 public:
2450 /**
2451 * Initialize storage and parameters for this distribution.
2452 * @param size The size of the vector (the number of distributions).
2453 * @param min The minimum value of the distribution.
2454 * @param max The maximum value of the distribution.
2455 * @param bkt The number of values in each bucket.
2456 * @return A reference to this distribution.
2457 */
2458 VectorDistribution &init(int size, T min, T max, int bkt) {
2459 params.min = min;
2460 params.max = max;
2461 params.bucket_size = bkt;
2462 params.size = (max - min) / bkt + 1;
2463 bin.init(size, params);
2464 setInit();
2465
2466 return *this;
2467 }
2468 };
2469
2470 /**
2471 * This is a vector of StandardDeviation stats.
2472 * @sa Stat, VectorDistBase, FancyStor
2473 */
2474 template <typename T = Counter, class Bin = NoBin>
2475 class VectorStandardDeviation
2476 : public Detail::VectorDistBase<T, Detail::FancyStor, Bin>
2477 {
2478 private:
2479 /** The base implementation */
2480 typedef Detail::VectorDistBase<T, Detail::FancyStor, Bin> Base;
2481 /** The parameter type. */
2482 typedef typename Detail::DistStor<T>::Params Params;
2483
2484 public:
2485 /**
2486 * Initialize storage for this distribution.
2487 * @param size The size of the vector.
2488 * @return A reference to this distribution.
2489 */
2490 VectorStandardDeviation &init(int size) {
2491 bin.init(size, params);
2492 setInit();
2493
2494 return *this;
2495 }
2496 };
2497
2498 /**
2499 * This is a vector of AverageDeviation stats.
2500 * @sa Stat, VectorDistBase, AvgFancy
2501 */
2502 template <typename T = Counter, class Bin = NoBin>
2503 class VectorAverageDeviation
2504 : public Detail::VectorDistBase<T, Detail::AvgFancy, Bin>
2505 {
2506 private:
2507 /** The base implementation */
2508 typedef Detail::VectorDistBase<T, Detail::AvgFancy, Bin> Base;
2509 /** The parameter type. */
2510 typedef typename Detail::DistStor<T>::Params Params;
2511
2512 public:
2513 /**
2514 * Initialize storage for this distribution.
2515 * @param size The size of the vector.
2516 * @return A reference to this distribution.
2517 */
2518 VectorAverageDeviation &init(int size) {
2519 bin.init(size, params);
2520 setInit();
2521
2522 return *this;
2523 }
2524 };
2525
2526 /**
2527 * A formula for statistics that is calculated when printed. A formula is
2528 * stored as a tree of Nodes that represent the equation to calculate.
2529 * @sa Stat, ScalarStat, VectorStat, Node, Detail::Temp
2530 */
2531 class Formula : public Detail::VectorStat
2532 {
2533 private:
2534 /** The root of the tree which represents the Formula */
2535 Detail::NodePtr root;
2536 friend class Statistics::Detail::Temp;
2537
2538 public:
2539 /**
2540 * Create and initialize thie formula, and register it with the database.
2541 */
2542 Formula() : VectorStat(true) { setInit(); }
2543 /**
2544 * Create a formula with the given root node, register it with the
2545 * database.
2546 * @param r The root of the expression tree.
2547 */
2548 Formula(Detail::Temp r) : VectorStat(true) {
2549 root = r;
2550 assert(size());
2551 }
2552
2553 /**
2554 * Set an unitialized Formula to the given root.
2555 * @param r The root of the expression tree.
2556 * @return a reference to this formula.
2557 */
2558 const Formula &operator=(Detail::Temp r) {
2559 assert(!root && "Can't change formulas");
2560 root = r;
2561 assert(size());
2562 return *this;
2563 }
2564
2565 /**
2566 * Add the given tree to the existing one.
2567 * @param r The root of the expression tree.
2568 * @return a reference to this formula.
2569 */
2570 const Formula &operator+=(Detail::Temp r) {
2571 using namespace Detail;
2572 if (root)
2573 root = NodePtr(new BinaryNode<std::plus<result_t> >(root, r));
2574 else
2575 root = r;
2576 assert(size());
2577 return *this;
2578 }
2579
2580 /**
2581 * Return the result of the Fomula in a vector. If there were no Vector
2582 * components to the Formula, then the vector is size 1. If there were,
2583 * like x/y with x being a vector of size 3, then the result returned will
2584 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2585 * @return The result vector.
2586 */
2587 const rvec_t &val() const { return root->val(); }
2588 /**
2589 * Return the total Formula result. If there is a Vector component to this
2590 * Formula, then this is the result of the Formula if the formula is applied
2591 * after summing all the components of the Vector. For example, if Formula
2592 * is x/y where x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If there is no
2593 * Vector component, total() returns the same value as the first entry in the rvec_t
2594 * val() returns.
2595 * @return The total of the result vector.
2596 */
2597 result_t total() const { return root->total(); }
2598
2599 /**
2600 * Return the number of elements in the tree.
2601 */
2602 size_t size() const {
2603 if (!root)
2604 return 0;
2605 else
2606 return root->size();
2607 }
2608
2609 /**
2610 * Formulas don't need to be reset
2611 */
2612 virtual void reset() {}
2613 };
2614
2615 /**
2616 * @}
2617 */
2618
2619 void check();
2620 void dump(std::ostream &stream);
2621 void reset();
2622 void regReset(Callback *cb);
2623
2624 inline Detail::Temp
2625 operator+(Detail::Temp l, Detail::Temp r)
2626 {
2627 using namespace Detail;
2628 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
2629 }
2630
2631 inline Detail::Temp
2632 operator-(Detail::Temp l, Detail::Temp r)
2633 {
2634 using namespace Detail;
2635 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
2636 }
2637
2638 inline Detail::Temp
2639 operator*(Detail::Temp l, Detail::Temp r)
2640 {
2641 using namespace Detail;
2642 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
2643 }
2644
2645 inline Detail::Temp
2646 operator/(Detail::Temp l, Detail::Temp r)
2647 {
2648 using namespace Detail;
2649 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
2650 }
2651
2652 inline Detail::Temp
2653 operator%(Detail::Temp l, Detail::Temp r)
2654 {
2655 using namespace Detail;
2656 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
2657 }
2658
2659 inline Detail::Temp
2660 operator-(Detail::Temp l)
2661 {
2662 using namespace Detail;
2663 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
2664 }
2665
2666 template <typename T>
2667 inline Detail::Temp
2668 constant(T val)
2669 {
2670 using namespace Detail;
2671 return NodePtr(new ConstNode<T>(val));
2672 }
2673
2674 template <typename T>
2675 inline Detail::Temp
2676 functor(T &val)
2677 {
2678 using namespace Detail;
2679 return NodePtr(new FunctorNode<T>(val));
2680 }
2681
2682 template <typename T>
2683 inline Detail::Temp
2684 scalar(T &val)
2685 {
2686 using namespace Detail;
2687 return NodePtr(new ScalarNode<T>(val));
2688 }
2689
2690 inline Detail::Temp
2691 sum(Detail::Temp val)
2692 {
2693 using namespace Detail;
2694 return NodePtr(new SumNode<std::plus<result_t> >(val));
2695 }
2696
2697 extern bool PrintDescriptions;
2698
2699 } // namespace statistics
2700
2701 #endif // __STATISTICS_HH__