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