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