statistics.hh:
[gem5.git] / base / statistics.hh
1 /*
2 * Copyright (c) 2003 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /** @file
30 * Declaration of Statistics objects.
31 */
32
33 /**
34 * @todo
35 *
36 * Generalized N-dimensinal vector
37 * documentation
38 * key stats
39 * interval stats
40 * -- these both can use the same function that prints out a
41 * specific set of stats
42 * VectorStandardDeviation totals
43 * Document Namespaces
44 */
45 #ifndef __STATISTICS_HH__
46 #define __STATISTICS_HH__
47
48 #include <algorithm>
49 #include <functional>
50 #include <iosfwd>
51 #include <sstream>
52 #include <string>
53 #include <vector>
54
55 #include <assert.h>
56
57 #include "base/refcnt.hh"
58 #include "base/str.hh"
59
60 #include "sim/host.hh"
61
62 //
63 // Un-comment this to enable wierdo-stat debugging
64 //
65 // #define STAT_DEBUG
66
67
68 #ifndef NAN
69 float __nan();
70 /** Define Not a number. */
71 #define NAN (__nan())
72 /** Need to define __nan() */
73 #define __M5_NAN
74 #endif
75
76 /** Print stats out in SS format. */
77 #define STAT_DISPLAY_COMPAT
78
79 /** 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 this class with the associated scalar proxy. */
721 friend class ScalarProxy<T, Storage, Bin>;
722
723 /**
724 * Return a reference (ScalarProxy) to the stat at the given index.
725 * @param index The vector index to access.
726 * @return A reference of the stat.
727 */
728 ScalarProxy<T, Storage, Bin> operator[](int index);
729
730 /**
731 * Return the number of elements in this vector.
732 * @return The size of the vector.
733 */
734 virtual size_t size() const { return bin.size(); }
735 };
736
737 /**
738 * A proxy class to access the stat at a given index in a VectorBase stat.
739 * Behaves like a ScalarBase.
740 */
741 template <typename T, template <typename T> class Storage, class Bin>
742 class ScalarProxy : public ScalarStat
743 {
744 protected:
745 /** Define the type of the storage class. */
746 typedef Storage<T> storage_t;
747 /** Define the params of the storage class. */
748 typedef typename storage_t::Params params_t;
749 /** Define the bin type. */
750 typedef typename Bin::VectorBin<storage_t> bin_t;
751
752 private:
753 /** Pointer to the bin in the parent VectorBase. */
754 bin_t *bin;
755 /** Pointer to the params in the parent VectorBase. */
756 params_t *params;
757 /** The index to access in the parent VectorBase. */
758 int index;
759
760 protected:
761 /**
762 * Retrieve the storage from the bin.
763 * @return The storage from the bin for this stat.
764 */
765 storage_t *data() { return bin->data(index, *params); }
766 /**
767 * Retrieve a const pointer to the storage from the bin.
768 * @return A const pointer to the storage for this stat.
769 */
770 const storage_t *data() const { return bin->data(index, *params); }
771
772 public:
773 /**
774 * Return the current value of this statas a result type.
775 * @return The current value.
776 */
777 result_t val() const { return data()->val(*params); }
778 /**
779 * Return the current value of this stat as its base type.
780 * @return The current value.
781 */
782 T value() const { return data()->value(*params); }
783
784 public:
785 /**
786 * Create and initialize this proxy, do not register it with the database.
787 * @param b The bin to use.
788 * @param p The params to use.
789 * @param i The index to access.
790 */
791 ScalarProxy(bin_t &b, params_t &p, int i)
792 : ScalarStat(false), bin(&b), params(&p), index(i) {}
793 /**
794 * Create a copy of the provided ScalarProxy.
795 * @param sp The proxy to copy.
796 */
797 ScalarProxy(const ScalarProxy &sp)
798 : ScalarStat(false), bin(sp.bin), params(sp.params), index(sp.index) {}
799 /**
800 * Set this proxy equal to the provided one.
801 * @param sp The proxy to copy.
802 * @return A reference to this proxy.
803 */
804 const ScalarProxy &operator=(const ScalarProxy &sp) {
805 bin = sp.bin;
806 params = sp.params;
807 index = sp.index;
808 return *this;
809 }
810
811 public:
812 // Common operators for stats
813 /**
814 * Increment the stat by 1. This calls the associated storage object inc
815 * function.
816 */
817 void operator++() { data()->inc(1, *params); }
818 /**
819 * Decrement the stat by 1. This calls the associated storage object dec
820 * function.
821 */
822 void operator--() { data()->dec(1, *params); }
823
824 /** Increment the stat by 1. */
825 void operator++(int) { ++*this; }
826 /** Decrement the stat by 1. */
827 void operator--(int) { --*this; }
828
829 /**
830 * Set the data value to the given value. This calls the associated storage
831 * object set function.
832 * @param v The new value.
833 */
834 template <typename U>
835 void operator=(const U& v) { data()->set(v, *params); }
836
837 /**
838 * Increment the stat by the given value. This calls the associated
839 * storage object inc function.
840 * @param v The value to add.
841 */
842 template <typename U>
843 void operator+=(const U& v) { data()->inc(v, *params); }
844
845 /**
846 * Decrement the stat by the given value. This calls the associated
847 * storage object dec function.
848 * @param v The value to substract.
849 */
850 template <typename U>
851 void operator-=(const U& v) { data()->dec(v, *params); }
852
853 /**
854 * Return the number of elements, always 1 for a scalar.
855 * @return 1.
856 */
857 virtual size_t size() const { return 1; }
858 };
859
860 template <typename T, template <typename T> class Storage, class Bin>
861 inline ScalarProxy<T, Storage, Bin>
862 VectorBase<T, Storage, Bin>::operator[](int index)
863 {
864 assert (index >= 0 && index < size());
865 return ScalarProxy<T, Storage, Bin>(bin, params, index);
866 }
867
868 template <typename T, template <typename T> class Storage, class Bin>
869 class VectorProxy;
870
871 template <typename T, template <typename T> class Storage, class Bin>
872 class Vector2dBase : public Stat
873 {
874 protected:
875 typedef Storage<T> storage_t;
876 typedef typename storage_t::Params params_t;
877 typedef typename Bin::VectorBin<storage_t> bin_t;
878
879 protected:
880 size_t x;
881 size_t y;
882 bin_t bin;
883 params_t params;
884 std::vector<std::string> *y_subnames;
885
886 protected:
887 storage_t *data(int index) { return bin.data(index, params); }
888 const storage_t *data(int index) const {
889 return (const_cast<bin_t *>(&bin))->data(index, params);
890 }
891
892 protected:
893 // Copying stats is not allowed
894 Vector2dBase(const Vector2dBase &stat);
895 const Vector2dBase &operator=(const Vector2dBase &);
896
897 public:
898 Vector2dBase() : Stat(true) {}
899 ~Vector2dBase() { }
900
901 Vector2dBase &init(size_t _x, size_t _y) {
902 x = _x;
903 y = _y;
904 bin.init(x * y, params);
905 setInit();
906 y_subnames = new std::vector<std::string>(y);
907
908 return *this;
909 }
910
911 /**
912 * @warning This makes the assumption that if you're gonna subnames a 2d
913 * vector, you're subnaming across all y
914 */
915 Vector2dBase &ysubnames(const char **names)
916 {
917 for (int i=0; i < y; ++i) {
918 (*y_subnames)[i] = names[i];
919 }
920 return *this;
921 }
922 Vector2dBase &ysubname(int index, const std::string subname)
923 {
924 (*y_subnames)[i] = subname.c_str();
925 return *this;
926 }
927 std::string ysubname(int i) const { return (*y_subnames)[i]; }
928
929 friend class VectorProxy<T, Storage, Bin>;
930 VectorProxy<T, Storage, Bin> operator[](int index);
931
932 virtual size_t size() const { return bin.size(); }
933 virtual bool zero() const { return data(0)->value(params) == 0.0; }
934
935 virtual void
936 display(std::ostream &out) const
937 {
938 bool have_subname = false;
939 for (int i = 0; i < x; ++i) {
940 if (!mysubname(i).empty())
941 have_subname = true;
942 }
943
944 rvec_t tot_vec(y);
945 result_t super_total = 0.0;
946 for (int i = 0; i < x; ++i) {
947 std::string subname;
948 if (have_subname) {
949 subname = mysubname(i);
950 if (subname.empty())
951 continue;
952 } else
953 subname = to_string(i);
954
955 int iy = i * y;
956 rvec_t vec(y);
957
958 result_t total = 0.0;
959 for (int j = 0; j < y; ++j) {
960 vec[j] = data(iy + j)->val(params);
961 tot_vec[j] += vec[j];
962 total += vec[j];
963 super_total += vec[j];
964 }
965
966 std::string desc;
967 if (mysubdesc(i).empty()) {
968 desc = mydesc();
969 } else {
970 desc = mysubdesc(i);
971 }
972
973 VectorDisplay(out, myname() + "_" + subname, y_subnames, desc, 0,
974 myprecision(), myflags(), vec, total);
975
976 }
977 if ((myflags() & ::Statistics::total) && (x > 1)) {
978 VectorDisplay(out, myname(), y_subnames, mydesc(), 0,
979 myprecision(), myflags(), tot_vec, super_total);
980
981 }
982 }
983 };
984
985 template <typename T, template <typename T> class Storage, class Bin>
986 class VectorProxy : public VectorStat
987 {
988 protected:
989 typedef Storage<T> storage_t;
990 typedef typename storage_t::Params params_t;
991 typedef typename Bin::VectorBin<storage_t> bin_t;
992
993 private:
994 bin_t *bin;
995 params_t *params;
996 int offset;
997 int len;
998
999 private:
1000 mutable rvec_t *vec;
1001
1002 storage_t *data(int index) {
1003 assert(index < len);
1004 return bin->data(offset + index, *params);
1005 }
1006
1007 const storage_t *data(int index) const {
1008 return (const_cast<bin_t *>(bin))->data(offset + index, *params);
1009 }
1010
1011 public:
1012 const rvec_t &val() const {
1013 if (vec)
1014 vec->resize(size());
1015 else
1016 vec = new rvec_t(size());
1017
1018 for (int i = 0; i < size(); ++i)
1019 (*vec)[i] = data(i)->val(*params);
1020
1021 return *vec;
1022 }
1023
1024 result_t total() const {
1025 result_t total = 0.0;
1026 for (int i = 0; i < size(); ++i)
1027 total += data(i)->val(*params);
1028 return total;
1029 }
1030
1031 public:
1032 VectorProxy(bin_t &b, params_t &p, int o, int l)
1033 : VectorStat(false), bin(&b), params(&p), offset(o), len(l), vec(NULL)
1034 { }
1035 VectorProxy(const VectorProxy &sp)
1036 : VectorStat(false), bin(sp.bin), params(sp.params), offset(sp.offset),
1037 len(sp.len), vec(NULL)
1038 { }
1039 ~VectorProxy() {
1040 if (vec)
1041 delete vec;
1042 }
1043
1044 const VectorProxy &operator=(const VectorProxy &sp) {
1045 bin = sp.bin;
1046 params = sp.params;
1047 offset = sp.offset;
1048 len = sp.len;
1049 if (vec)
1050 delete vec;
1051 vec = NULL;
1052 return *this;
1053 }
1054
1055 virtual size_t size() const { return len; }
1056
1057 ScalarProxy<T, Storage, Bin> operator[](int index) {
1058 assert (index >= 0 && index < size());
1059 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index);
1060 }
1061 };
1062
1063 template <typename T, template <typename T> class Storage, class Bin>
1064 inline VectorProxy<T, Storage, Bin>
1065 Vector2dBase<T, Storage, Bin>::operator[](int index)
1066 {
1067 int offset = index * y;
1068 assert (index >= 0 && offset < size());
1069 return VectorProxy<T, Storage, Bin>(bin, params, offset, y);
1070 }
1071
1072 //////////////////////////////////////////////////////////////////////
1073 //
1074 // Non formula statistics
1075 //
1076 //////////////////////////////////////////////////////////////////////
1077
1078 void DistDisplay(std::ostream &stream, const std::string &name,
1079 const std::string &desc, int precision, FormatFlags flags,
1080 result_t min_val, result_t max_val,
1081 result_t underflow, result_t overflow,
1082 const rvec_t &vec, int min, int max, int bucket_size,
1083 int size);
1084 /**
1085 * Templatized storage and interface for a distrbution stat.
1086 */
1087 template <typename T>
1088 struct DistStor
1089 {
1090 public:
1091 /** The parameters for a distribution stat. */
1092 struct Params
1093 {
1094 /** The minimum value to track. */
1095 int min;
1096 /** The maximum value to track. */
1097 int max;
1098 /** The number of entries in each bucket. */
1099 int bucket_size;
1100 /** The number of buckets. Equal to (max-min)/bucket_size. */
1101 int size;
1102 };
1103
1104 private:
1105 /** The smallest value sampled. */
1106 T min_val;
1107 /** The largest value sampled. */
1108 T max_val;
1109 /** The number of values sampled less than min. */
1110 T underflow;
1111 /** The number of values sampled more than max. */
1112 T overflow;
1113 /** Counter for each bucket. */
1114 std::vector<T> vec;
1115
1116 public:
1117 /**
1118 * Construct this storage with the supplied params.
1119 * @param params The parameters.
1120 */
1121 DistStor(const Params &params)
1122 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
1123 vec(params.size) {
1124 }
1125 /**
1126 * Add a value to the distribution for the given number of times.
1127 * @param val The value to add.
1128 * @param number The number of times to add the value.
1129 * @param params The paramters of the distribution.
1130 */
1131 void sample(T val, int number, const Params &params) {
1132 if (val < params.min)
1133 underflow += number;
1134 else if (val > params.max)
1135 overflow += number;
1136 else {
1137 int index = (val - params.min) / params.bucket_size;
1138 assert(index < size(params));
1139 vec[index] += number;
1140 }
1141
1142 if (val < min_val)
1143 min_val = val;
1144
1145 if (val > max_val)
1146 max_val = val;
1147 }
1148
1149 /**
1150 * Return the number of buckets in this distribution.
1151 * @return the number of buckets.
1152 * @todo Is it faster to return the size from the parameters?
1153 */
1154 size_t size(const Params &) const { return vec.size(); }
1155
1156 /**
1157 * Returns true if any calls to sample have been made.
1158 * @param params The paramters of the distribution.
1159 * @return True if any values have been sampled.
1160 */
1161 bool zero(const Params &params) const {
1162 if (underflow != 0 || overflow != 0)
1163 return false;
1164
1165 int s = size(params);
1166 for (int i = 0; i < s; i++)
1167 if (vec[i] != 0)
1168 return false;
1169
1170 return true;
1171 }
1172
1173 /**
1174 * Print this distribution and the given print data to the given ostream.
1175 * @param stream The output stream.
1176 * @param name The name of this stat (from StatData).
1177 * @param desc The description of this stat (from StatData).
1178 * @param precision The print precision (from StatData).
1179 * @param flags The format flags (from StatData).
1180 * @param params The paramters of this distribution.
1181 */
1182 void display(std::ostream &stream, const std::string &name,
1183 const std::string &desc, int precision, FormatFlags flags,
1184 const Params &params) const {
1185
1186 #ifdef STAT_DISPLAY_COMPAT
1187 result_t min = params.min;
1188 #else
1189 result_t min = (min_val == INT_MAX) ? params.min : min_val;
1190 #endif
1191 result_t max = (max_val == INT_MIN) ? 0 : max_val;
1192
1193 rvec_t rvec(params.size);
1194 for (int i = 0; i < params.size; ++i)
1195 rvec[i] = vec[i];
1196
1197 DistDisplay(stream, name, desc, precision, flags,
1198 (result_t)min, (result_t)max,
1199 (result_t)underflow, (result_t)overflow,
1200 rvec, params.min, params.max, params.bucket_size,
1201 params.size);
1202 }
1203 };
1204
1205 void FancyDisplay(std::ostream &stream, const std::string &name,
1206 const std::string &desc, int precision, FormatFlags flags,
1207 result_t mean, result_t variance);
1208
1209 /**
1210 * Templatized storage and interface for a distribution that calculates mean
1211 * and variance.
1212 */
1213 template <typename T>
1214 struct FancyStor
1215 {
1216 public:
1217 /**
1218 * No paramters for this storage.
1219 */
1220 struct Params {};
1221
1222 private:
1223 /** The current sum. */
1224 T sum;
1225 /** The sum of squares. */
1226 T squares;
1227 /** The total number of samples. */
1228 int total;
1229
1230 public:
1231 /**
1232 * Create and initialize this storage.
1233 */
1234 FancyStor(const Params &) : sum(0), squares(0), total(0) {}
1235
1236 /**
1237 * Add a value the given number of times to this running average.
1238 * Update the running sum and sum of squares, increment the number of
1239 * values seen by the given number.
1240 * @param val The value to add.
1241 * @param number The number of times to add the value.
1242 * @param p The parameters of this stat.
1243 */
1244 void sample(T val, int number, const Params &p) {
1245 T value = val * number;
1246 sum += value;
1247 squares += value * value;
1248 total += number;
1249 }
1250
1251 /**
1252 * Print this distribution and the given print data to the given ostream.
1253 * @param stream The output stream.
1254 * @param name The name of this stat (from StatData).
1255 * @param desc The description of this stat (from StatData).
1256 * @param precision The print precision (from StatData).
1257 * @param flags The format flags (from StatData).
1258 * @param params The paramters of this distribution.
1259 */
1260 void display(std::ostream &stream, const std::string &name,
1261 const std::string &desc, int precision, FormatFlags flags,
1262 const Params &params) const {
1263
1264 result_t mean = NAN;
1265 result_t variance = NAN;
1266
1267 if (total != 0) {
1268 result_t fsum = sum;
1269 result_t fsq = squares;
1270 result_t ftot = total;
1271
1272 mean = fsum / ftot;
1273 variance = (ftot * fsq - (fsum * fsum)) / (ftot * (ftot - 1.0));
1274 }
1275
1276 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1277 }
1278
1279 /**
1280 * Return the number of entries in this stat, 1
1281 * @return 1.
1282 */
1283 size_t size(const Params &) const { return 1; }
1284 /**
1285 * Return true if no samples have been added.
1286 * @return True if no samples have been added.
1287 */
1288 bool zero(const Params &) const { return total == 0; }
1289 };
1290
1291 /**
1292 * Templatized storage for distribution that calculates per cycle mean and
1293 * variance.
1294 */
1295 template <typename T>
1296 struct AvgFancy
1297 {
1298 public:
1299 /** No parameters for this storage. */
1300 struct Params {};
1301
1302 private:
1303 /** Current total. */
1304 T sum;
1305 /** Current sum of squares. */
1306 T squares;
1307
1308 public:
1309 /**
1310 * Create and initialize this storage.
1311 */
1312 AvgFancy(const Params &) : sum(0), squares(0) {}
1313
1314 /**
1315 * Add a value to the distribution for the given number of times.
1316 * Update the running sum and sum of squares.
1317 * @param val The value to add.
1318 * @param number The number of times to add the value.
1319 * @param p The paramters of the distribution.
1320 */
1321 void sample(T val, int number, const Params& p) {
1322 T value = val * number;
1323 sum += value;
1324 squares += value * value;
1325 }
1326
1327 /**
1328 * Print this distribution and the given print data to the given ostream.
1329 * @param stream The output stream.
1330 * @param name The name of this stat (from StatData).
1331 * @param desc The description of this stat (from StatData).
1332 * @param precision The print precision (from StatData).
1333 * @param flags The format flags (from StatData).
1334 * @param params The paramters of this distribution.
1335 */
1336 void display(std::ostream &stream, const std::string &name,
1337 const std::string &desc, int precision, FormatFlags flags,
1338 const Params &params) const {
1339 result_t mean = sum / curTick;
1340 result_t variance = (squares - sum * sum) / curTick;
1341
1342 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1343 }
1344
1345 /**
1346 * Return the number of entries, in this case 1.
1347 * @return 1.
1348 */
1349 size_t size(const Params &params) const { return 1; }
1350 /**
1351 * Return true if no samples have been added.
1352 * @return True if the sum is zero.
1353 */
1354 bool zero(const Params &params) const { return sum == 0; }
1355 };
1356
1357 /**
1358 * Implementation of a distribution stat. The type of distribution is
1359 * determined by the Storage template. @sa ScalarBase
1360 */
1361 template <typename T, template <typename T> class Storage, class Bin>
1362 class DistBase : public Stat
1363 {
1364 protected:
1365 /** Define the type of the storage class. */
1366 typedef Storage<T> storage_t;
1367 /** Define the params of the storage class. */
1368 typedef typename storage_t::Params params_t;
1369 /** Define the bin type. */
1370 typedef typename Bin::Bin<storage_t> bin_t;
1371
1372 protected:
1373 /** The bin of this stat. */
1374 bin_t bin;
1375 /** The parameters for this stat. */
1376 params_t params;
1377
1378 protected:
1379 /**
1380 * Retrieve the storage from the bin.
1381 * @return The storage object for this stat.
1382 */
1383 storage_t *data() { return bin.data(params); }
1384 /**
1385 * Retrieve a const pointer to the storage from the bin.
1386 * @return A const pointer to the storage object for this stat.
1387 */
1388 const storage_t *data() const {
1389 return (const_cast<bin_t *>(&bin))->data(params);
1390 }
1391
1392 protected:
1393 // Copying stats is not allowed
1394 /** Copies are not allowed. */
1395 DistBase(const DistBase &stat);
1396 /** Copies are not allowed. */
1397 const DistBase &operator=(const DistBase &);
1398
1399 public:
1400 /**
1401 * Create this distrubition and register it with the database.
1402 */
1403 DistBase() : Stat(true) { }
1404 /**
1405 * Destructor.
1406 */
1407 ~DistBase() { }
1408
1409 /**
1410 * Add a value to the distribtion n times. Calls sample on the storage
1411 * class.
1412 * @param v The value to add.
1413 * @param n The number of times to add it, defaults to 1.
1414 */
1415 template <typename U>
1416 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
1417
1418 /**
1419 * Return the number of entries in this stat.
1420 * @return The number of entries.
1421 */
1422 virtual size_t size() const { return data()->size(params); }
1423 /**
1424 * Return true if no samples have been added.
1425 * @return True if there haven't been any samples.
1426 */
1427 virtual bool zero() const { return data()->zero(params); }
1428 /**
1429 * Print this distribution to the given ostream.
1430 * @param stream The output stream.
1431 */
1432 virtual void display(std::ostream &stream) const {
1433 data()->display(stream, myname(), mydesc(), myprecision(), myflags(),
1434 params);
1435 }
1436 };
1437
1438 template <typename T, template <typename T> class Storage, class Bin>
1439 class DistProxy;
1440
1441 template <typename T, template <typename T> class Storage, class Bin>
1442 class VectorDistBase : public Stat
1443 {
1444 protected:
1445 typedef Storage<T> storage_t;
1446 typedef typename storage_t::Params params_t;
1447 typedef typename Bin::VectorBin<storage_t> bin_t;
1448
1449 protected:
1450 bin_t bin;
1451 params_t params;
1452
1453 protected:
1454 storage_t *data(int index) { return bin.data(index, params); }
1455 const storage_t *data(int index) const {
1456 return (const_cast<bin_t *>(&bin))->data(index, params);
1457 }
1458
1459 protected:
1460 // Copying stats is not allowed
1461 VectorDistBase(const VectorDistBase &stat);
1462 const VectorDistBase &operator=(const VectorDistBase &);
1463
1464 public:
1465 VectorDistBase() : Stat(true) { }
1466 ~VectorDistBase() { }
1467
1468 friend class DistProxy<T, Storage, Bin>;
1469 DistProxy<T, Storage, Bin> operator[](int index);
1470 const DistProxy<T, Storage, Bin> operator[](int index) const;
1471
1472 virtual size_t size() const { return bin.size(); }
1473 virtual bool zero() const { return false; }
1474 virtual void display(std::ostream &stream) const;
1475 };
1476
1477 template <typename T, template <typename T> class Storage, class Bin>
1478 class DistProxy : public Stat
1479 {
1480 protected:
1481 typedef Storage<T> storage_t;
1482 typedef typename storage_t::Params params_t;
1483 typedef typename Bin::Bin<storage_t> bin_t;
1484 typedef VectorDistBase<T, Storage, Bin> base_t;
1485
1486 private:
1487 union {
1488 base_t *stat;
1489 const base_t *cstat;
1490 };
1491 int index;
1492
1493 protected:
1494 storage_t *data() { return stat->data(index); }
1495 const storage_t *data() const { return cstat->data(index); }
1496
1497 public:
1498 DistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1499 : Stat(false), cstat(&s), index(i) {}
1500 DistProxy(const DistProxy &sp)
1501 : Stat(false), cstat(sp.cstat), index(sp.index) {}
1502 const DistProxy &operator=(const DistProxy &sp) {
1503 cstat = sp.cstat; index = sp.index; return *this;
1504 }
1505
1506 public:
1507 template <typename U>
1508 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
1509
1510 virtual size_t size() const { return 1; }
1511 virtual bool zero() const {
1512 return data()->zero(cstat->params);
1513 }
1514 virtual void display(std::ostream &stream) const {
1515 std::stringstream name, desc;
1516
1517 if (!(cstat->mysubname(index).empty())) {
1518 name << cstat->myname() << cstat->mysubname(index);
1519 } else {
1520 name << cstat->myname() << "_" << index;
1521 }
1522 if (!(cstat->mysubdesc(index).empty())) {
1523 desc << cstat->mysubdesc(index);
1524 } else {
1525 desc << cstat->mydesc();
1526 }
1527
1528 data()->display(stream, name.str(), desc.str(),
1529 cstat->myprecision(), cstat->myflags(), cstat->params);
1530 }
1531 };
1532
1533 template <typename T, template <typename T> class Storage, class Bin>
1534 inline DistProxy<T, Storage, Bin>
1535 VectorDistBase<T, Storage, Bin>::operator[](int index)
1536 {
1537 assert (index >= 0 && index < size());
1538 return DistProxy<T, Storage, Bin>(*this, index);
1539 }
1540
1541 template <typename T, template <typename T> class Storage, class Bin>
1542 inline const DistProxy<T, Storage, Bin>
1543 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1544 {
1545 assert (index >= 0 && index < size());
1546 return DistProxy<T, Storage, Bin>(*this, index);
1547 }
1548
1549 /**
1550 * @todo Need a way to print Distribution totals across the Vector
1551 */
1552 template <typename T, template <typename T> class Storage, class Bin>
1553 void
1554 VectorDistBase<T, Storage, Bin>::display(std::ostream &stream) const
1555 {
1556 for (int i = 0; i < size(); ++i) {
1557 DistProxy<T, Storage, Bin> proxy(*this, i);
1558 proxy.display(stream);
1559 }
1560 }
1561
1562 #if 0
1563 result_t
1564 VectorDistBase<T, Storage, Bin>::total(int index) const
1565 {
1566 int total = 0;
1567 for (int i=0; i < x_size(); ++i) {
1568 total += data(i)->val(*params);
1569 }
1570 }
1571 #endif
1572
1573 //////////////////////////////////////////////////////////////////////
1574 //
1575 // Formula Details
1576 //
1577 //////////////////////////////////////////////////////////////////////
1578
1579 /**
1580 * Base class for formula statistic node. These nodes are used to build a tree
1581 * that represents the formula.
1582 */
1583 class Node : public RefCounted
1584 {
1585 public:
1586 /**
1587 * Return the number of nodes in the subtree starting at this node.
1588 * @return the number of nodes in this subtree.
1589 */
1590 virtual size_t size() const = 0;
1591 /**
1592 * Return the result vector of this subtree.
1593 * @return The result vector of this subtree.
1594 */
1595 virtual const rvec_t &val() const = 0;
1596 /**
1597 * Return the total of the result vector.
1598 * @return The total of the result vector.
1599 */
1600 virtual result_t total() const = 0;
1601 };
1602
1603 /** Reference counting pointer to a function Node. */
1604 typedef RefCountingPtr<Node> NodePtr;
1605
1606 class ScalarStatNode : public Node
1607 {
1608 private:
1609 const ScalarStat &stat;
1610 mutable rvec_t result;
1611
1612 public:
1613 ScalarStatNode(const ScalarStat &s) : stat(s), result(1) {}
1614 const rvec_t &val() const { result[0] = stat.val(); return result; }
1615 virtual result_t total() const { return stat.val(); };
1616
1617 virtual size_t size() const { return 1; }
1618 };
1619
1620 template <typename T, template <typename T> class Storage, class Bin>
1621 class ScalarProxyNode : public Node
1622 {
1623 private:
1624 const ScalarProxy<T, Storage, Bin> proxy;
1625 mutable rvec_t result;
1626
1627 public:
1628 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1629 : proxy(p), result(1) { }
1630 const rvec_t &val() const { result[0] = proxy.val(); return result; }
1631 virtual result_t total() const { return proxy.val(); };
1632
1633 virtual size_t size() const { return 1; }
1634 };
1635
1636 class VectorStatNode : public Node
1637 {
1638 private:
1639 const VectorStat &stat;
1640
1641 public:
1642 VectorStatNode(const VectorStat &s) : stat(s) {}
1643 const rvec_t &val() const { return stat.val(); }
1644 virtual result_t total() const { return stat.total(); };
1645
1646 virtual size_t size() const { return stat.size(); }
1647 };
1648
1649 template <typename T>
1650 class ConstNode : public Node
1651 {
1652 private:
1653 rvec_t data;
1654
1655 public:
1656 ConstNode(T s) : data(1, (result_t)s) {}
1657 const rvec_t &val() const { return data; }
1658 virtual result_t total() const { return data[0]; };
1659
1660 virtual size_t size() const { return 1; }
1661 };
1662
1663 template <typename T>
1664 class FunctorNode : public Node
1665 {
1666 private:
1667 T &functor;
1668 mutable rvec_t result;
1669
1670 public:
1671 FunctorNode(T &f) : functor(f) { result.resize(1); }
1672 const rvec_t &val() const {
1673 result[0] = (result_t)functor();
1674 return result;
1675 }
1676 virtual result_t total() const { return (result_t)functor(); };
1677
1678 virtual size_t size() const { return 1; }
1679 };
1680
1681 template <typename T>
1682 class ScalarNode : public Node
1683 {
1684 private:
1685 T &scalar;
1686 mutable rvec_t result;
1687
1688 public:
1689 ScalarNode(T &s) : scalar(s) { result.resize(1); }
1690 const rvec_t &val() const {
1691 result[0] = (result_t)scalar;
1692 return result;
1693 }
1694 virtual result_t total() const { return (result_t)scalar; };
1695
1696 virtual size_t size() const { return 1; }
1697 };
1698
1699 template <class Op>
1700 class UnaryNode : public Node
1701 {
1702 public:
1703 NodePtr l;
1704 mutable rvec_t result;
1705
1706 public:
1707 UnaryNode(NodePtr p) : l(p) {}
1708
1709 const rvec_t &val() const {
1710 const rvec_t &lvec = l->val();
1711 int size = lvec.size();
1712
1713 assert(size > 0);
1714
1715 result.resize(size);
1716 Op op;
1717 for (int i = 0; i < size; ++i)
1718 result[i] = op(lvec[i]);
1719
1720 return result;
1721 }
1722
1723 result_t total() const {
1724 Op op;
1725 return op(l->total());
1726 }
1727
1728 virtual size_t size() const { return l->size(); }
1729 };
1730
1731 template <class Op>
1732 class BinaryNode : public Node
1733 {
1734 public:
1735 NodePtr l;
1736 NodePtr r;
1737 mutable rvec_t result;
1738
1739 public:
1740 BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {}
1741
1742 const rvec_t &val() const {
1743 Op op;
1744 const rvec_t &lvec = l->val();
1745 const rvec_t &rvec = r->val();
1746
1747 assert(lvec.size() > 0 && rvec.size() > 0);
1748
1749 if (lvec.size() == 1 && rvec.size() == 1) {
1750 result.resize(1);
1751 result[0] = op(lvec[0], rvec[0]);
1752 } else if (lvec.size() == 1) {
1753 int size = rvec.size();
1754 result.resize(size);
1755 for (int i = 0; i < size; ++i)
1756 result[i] = op(lvec[0], rvec[i]);
1757 } else if (rvec.size() == 1) {
1758 int size = lvec.size();
1759 result.resize(size);
1760 for (int i = 0; i < size; ++i)
1761 result[i] = op(lvec[i], rvec[0]);
1762 } else if (rvec.size() == lvec.size()) {
1763 int size = rvec.size();
1764 result.resize(size);
1765 for (int i = 0; i < size; ++i)
1766 result[i] = op(lvec[i], rvec[i]);
1767 }
1768
1769 return result;
1770 }
1771
1772 result_t total() const {
1773 Op op;
1774 return op(l->total(), r->total());
1775 }
1776
1777 virtual size_t size() const {
1778 int ls = l->size();
1779 int rs = r->size();
1780 if (ls == 1)
1781 return rs;
1782 else if (rs == 1)
1783 return ls;
1784 else {
1785 assert(ls == rs && "Node vector sizes are not equal");
1786 return ls;
1787 }
1788 }
1789 };
1790
1791 template <class Op>
1792 class SumNode : public Node
1793 {
1794 public:
1795 NodePtr l;
1796 mutable rvec_t result;
1797
1798 public:
1799 SumNode(NodePtr p) : l(p), result(1) {}
1800
1801 const rvec_t &val() const {
1802 const rvec_t &lvec = l->val();
1803 int size = lvec.size();
1804 assert(size > 0);
1805
1806 result[0] = 0.0;
1807
1808 Op op;
1809 for (int i = 0; i < size; ++i)
1810 result[0] = op(result[0], lvec[i]);
1811
1812 return result;
1813 }
1814
1815 result_t total() const {
1816 const rvec_t &lvec = l->val();
1817 int size = lvec.size();
1818 assert(size > 0);
1819
1820 result_t result = 0.0;
1821
1822 Op op;
1823 for (int i = 0; i < size; ++i)
1824 result = op(result, lvec[i]);
1825
1826 return result;
1827 }
1828
1829 virtual size_t size() const { return 1; }
1830 };
1831
1832 /**
1833 * Helper class to construct formula node trees.
1834 */
1835 class Temp
1836 {
1837 private:
1838 /**
1839 * Pointer to a Node object.
1840 */
1841 NodePtr node;
1842
1843 public:
1844 /**
1845 * Copy the given pointer to this class.
1846 * @param n A pointer to a Node object to copy.
1847 */
1848 Temp(NodePtr n) : node(n) {}
1849 /**
1850 * Create a new ScalarStatNode.
1851 * @param s The ScalarStat to place in a node.
1852 */
1853 Temp(const ScalarStat &s) : node(new ScalarStatNode(s)) {}
1854 /**
1855 * Create a new ScalarProxyNode.
1856 * @param p The ScalarProxy to place in a node.
1857 */
1858 template <typename T, template <typename T> class Storage, class Bin>
1859 Temp(const ScalarProxy<T, Storage, Bin> &p)
1860 : node(new ScalarProxyNode<T, Storage, Bin>(p)) {}
1861 /**
1862 * Create a new VectorStatNode.
1863 * @param s The VectorStat to place in a node.
1864 */
1865 Temp(const VectorStat &s) : node(new VectorStatNode(s)) {}
1866
1867 /**
1868 * Create a ConstNode
1869 * @param value The value of the const node.
1870 */
1871 Temp(signed char value) : node(new ConstNode<signed char>(value)) {}
1872 /**
1873 * Create a ConstNode
1874 * @param value The value of the const node.
1875 */
1876 Temp(unsigned char value) : node(new ConstNode<unsigned char>(value)) {}
1877 /**
1878 * Create a ConstNode
1879 * @param value The value of the const node.
1880 */
1881 Temp(signed short value) : node(new ConstNode<signed short>(value)) {}
1882 /**
1883 * Create a ConstNode
1884 * @param value The value of the const node.
1885 */
1886 Temp(unsigned short value) : node(new ConstNode<unsigned short>(value)) {}
1887 /**
1888 * Create a ConstNode
1889 * @param value The value of the const node.
1890 */
1891 Temp(signed int value) : node(new ConstNode<signed int>(value)) {}
1892 /**
1893 * Create a ConstNode
1894 * @param value The value of the const node.
1895 */
1896 Temp(unsigned int value) : node(new ConstNode<unsigned int>(value)) {}
1897 /**
1898 * Create a ConstNode
1899 * @param value The value of the const node.
1900 */
1901 Temp(signed long value) : node(new ConstNode<signed long>(value)) {}
1902 /**
1903 * Create a ConstNode
1904 * @param value The value of the const node.
1905 */
1906 Temp(unsigned long value) : node(new ConstNode<unsigned long>(value)) {}
1907 /**
1908 * Create a ConstNode
1909 * @param value The value of the const node.
1910 */
1911 Temp(signed long long value)
1912 : node(new ConstNode<signed long long>(value)) {}
1913 /**
1914 * Create a ConstNode
1915 * @param value The value of the const node.
1916 */
1917 Temp(unsigned long long value)
1918 : node(new ConstNode<unsigned long long>(value)) {}
1919 /**
1920 * Create a ConstNode
1921 * @param value The value of the const node.
1922 */
1923 Temp(float value) : node(new ConstNode<float>(value)) {}
1924 /**
1925 * Create a ConstNode
1926 * @param value The value of the const node.
1927 */
1928 Temp(double value) : node(new ConstNode<double>(value)) {}
1929
1930 /**
1931 * Return the node pointer.
1932 * @return the node pointer.
1933 */
1934 operator NodePtr() { return node;}
1935 };
1936
1937
1938 //////////////////////////////////////////////////////////////////////
1939 //
1940 // Binning Interface
1941 //
1942 //////////////////////////////////////////////////////////////////////
1943
1944 class BinBase
1945 {
1946 private:
1947 off_t memsize;
1948 char *mem;
1949
1950 protected:
1951 off_t size() const { return memsize; }
1952 char *memory();
1953
1954 public:
1955 BinBase(size_t size);
1956 ~BinBase();
1957 };
1958
1959 } // namespace Detail
1960
1961 template <class BinType>
1962 struct StatBin : public Detail::BinBase
1963 {
1964 static StatBin *&curBin() {
1965 static StatBin *current = NULL;
1966 return current;
1967 }
1968
1969 static void setCurBin(StatBin *bin) { curBin() = bin; }
1970 static StatBin *current() { assert(curBin()); return curBin(); }
1971
1972 static off_t &offset() {
1973 static off_t offset = 0;
1974 return offset;
1975 }
1976
1977 static off_t new_offset(size_t size) {
1978 size_t mask = sizeof(u_int64_t) - 1;
1979 off_t off = offset();
1980
1981 // That one is for the last trailing flags byte.
1982 offset() += (size + 1 + mask) & ~mask;
1983
1984 return off;
1985 }
1986
1987 explicit StatBin(size_t size = 1024) : Detail::BinBase(size) {}
1988
1989 char *memory(off_t off) {
1990 assert(offset() <= size());
1991 return Detail::BinBase::memory() + off;
1992 }
1993
1994 static void activate(StatBin &bin) { setCurBin(&bin); }
1995
1996 class BinBase
1997 {
1998 private:
1999 int offset;
2000
2001 public:
2002 BinBase() : offset(-1) {}
2003 void allocate(size_t size) {
2004 offset = new_offset(size);
2005 }
2006 char *access() {
2007 assert(offset != -1);
2008 return current()->memory(offset);
2009 }
2010 };
2011
2012 template <class Storage>
2013 class Bin : public BinBase
2014 {
2015 public:
2016 typedef typename Storage::Params Params;
2017
2018 public:
2019 Bin() { allocate(sizeof(Storage)); }
2020 bool initialized() const { return true; }
2021 void init(const Params &params) { }
2022
2023 int size() const { return 1; }
2024
2025 Storage *data(const Params &params) {
2026 assert(initialized());
2027 char *ptr = access();
2028 char *flags = ptr + sizeof(Storage);
2029 if (!(*flags & 0x1)) {
2030 *flags |= 0x1;
2031 new (ptr) Storage(params);
2032 }
2033 return reinterpret_cast<Storage *>(ptr);
2034 }
2035 };
2036
2037 template <class Storage>
2038 class VectorBin : public BinBase
2039 {
2040 public:
2041 typedef typename Storage::Params Params;
2042
2043 private:
2044 int _size;
2045
2046 public:
2047 VectorBin() : _size(0) {}
2048
2049 bool initialized() const { return _size > 0; }
2050 void init(int s, const Params &params) {
2051 assert(!initialized());
2052 assert(s > 0);
2053 _size = s;
2054 allocate(_size * sizeof(Storage));
2055 }
2056
2057 int size() const { return _size; }
2058
2059 Storage *data(int index, const Params &params) {
2060 assert(initialized());
2061 assert(index >= 0 && index < size());
2062 char *ptr = access();
2063 char *flags = ptr + size() * sizeof(Storage);
2064 if (!(*flags & 0x1)) {
2065 *flags |= 0x1;
2066 for (int i = 0; i < size(); ++i)
2067 new (ptr + i * sizeof(Storage)) Storage(params);
2068 }
2069 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2070 }
2071 };
2072 };
2073
2074 class MainBinType {};
2075 typedef StatBin<MainBinType> MainBin;
2076
2077 struct NoBin
2078 {
2079 template <class Storage>
2080 struct Bin
2081 {
2082 public:
2083 typedef typename Storage::Params Params;
2084
2085 private:
2086 char ptr[sizeof(Storage)];
2087
2088 public:
2089 bool initialized() const { return true; }
2090 void init(const Params &params) {
2091 new (ptr) Storage(params);
2092 }
2093 int size() const{ return 1; }
2094 Storage *data(const Params &params) {
2095 assert(initialized());
2096 return reinterpret_cast<Storage *>(ptr);
2097 }
2098 };
2099
2100 template <class Storage>
2101 struct VectorBin
2102 {
2103 public:
2104 typedef typename Storage::Params Params;
2105
2106 private:
2107 char *ptr;
2108 int _size;
2109
2110 public:
2111 VectorBin() : ptr(NULL) { }
2112 ~VectorBin() {
2113 if (initialized())
2114 delete [] ptr;
2115 }
2116 bool initialized() const { return ptr != NULL; }
2117 void init(int s, const Params &params) {
2118 assert(s > 0 && "size must be positive!");
2119 assert(!initialized());
2120 _size = s;
2121 ptr = new char[_size * sizeof(Storage)];
2122 for (int i = 0; i < _size; ++i)
2123 new (ptr + i * sizeof(Storage)) Storage(params);
2124 }
2125
2126 int size() const { return _size; }
2127
2128 Storage *data(int index, const Params &params) {
2129 assert(initialized());
2130 assert(index >= 0 && index < size());
2131 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2132 }
2133 };
2134 };
2135
2136 //////////////////////////////////////////////////////////////////////
2137 //
2138 // Visible Statistics Types
2139 //
2140 //////////////////////////////////////////////////////////////////////
2141 /**
2142 * @defgroup VisibleStats "Statistic Types"
2143 * These are the statistics that are used in the simulator. By default these
2144 * store counters and don't use binning, but are templatized to accept any type
2145 * and any Bin class.
2146 * @{
2147 */
2148
2149 /**
2150 * This is a simple scalar statistic, like a counter.
2151 * @sa Stat, ScalarBase, StatStor
2152 */
2153 template <typename T = Counter, class Bin = NoBin>
2154 class Scalar : public Detail::ScalarBase<T, Detail::StatStor, Bin>
2155 {
2156 public:
2157 /** The base implementation. */
2158 typedef Detail::ScalarBase<T, Detail::StatStor, Bin> Base;
2159
2160 /**
2161 * Sets the stat equal to the given value. Calls the base implementation
2162 * of operator=
2163 * @param v The new value.
2164 */
2165 template <typename U>
2166 void operator=(const U& v) { Base::operator=(v); }
2167 };
2168
2169 /**
2170 * A stat that calculates the per cycle average of a value.
2171 * @sa Stat, ScalarBase, AvgStor
2172 */
2173 template <typename T = Counter, class Bin = NoBin>
2174 class Average : public Detail::ScalarBase<T, Detail::AvgStor, Bin>
2175 {
2176 public:
2177 /** The base implementation. */
2178 typedef Detail::ScalarBase<T, Detail::AvgStor, Bin> Base;
2179
2180 /**
2181 * Sets the stat equal to the given value. Calls the base implementation
2182 * of operator=
2183 * @param v The new value.
2184 */
2185 template <typename U>
2186 void operator=(const U& v) { Base::operator=(v); }
2187 };
2188
2189 /**
2190 * A vector of scalar stats.
2191 * @sa Stat, VectorBase, StatStor
2192 */
2193 template <typename T = Counter, class Bin = NoBin>
2194 class Vector : public Detail::VectorBase<T, Detail::StatStor, Bin>
2195 { };
2196
2197 /**
2198 * A vector of Average stats.
2199 * @sa Stat, VectorBase, AvgStor
2200 */
2201 template <typename T = Counter, class Bin = NoBin>
2202 class AverageVector : public Detail::VectorBase<T, Detail::AvgStor, Bin>
2203 { };
2204
2205 /**
2206 * A 2-Dimensional vecto of scalar stats.
2207 * @sa Stat, Vector2dBase, StatStor
2208 */
2209 template <typename T = Counter, class Bin = NoBin>
2210 class Vector2d : public Detail::Vector2dBase<T, Detail::StatStor, Bin>
2211 { };
2212
2213 /**
2214 * A simple distribution stat.
2215 * @sa Stat, DistBase, DistStor
2216 */
2217 template <typename T = Counter, class Bin = NoBin>
2218 class Distribution : public Detail::DistBase<T, Detail::DistStor, Bin>
2219 {
2220 private:
2221 /** Base implementation. */
2222 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2223 /** The Parameter type. */
2224 typedef typename Detail::DistStor<T>::Params Params;
2225
2226 public:
2227 /**
2228 * Set the parameters of this distribution. @sa DistStor::Params
2229 * @param min The minimum value of the distribution.
2230 * @param max The maximum value of the distribution.
2231 * @param bkt The number of values in each bucket.
2232 * @return A reference to this distribution.
2233 */
2234 Distribution &init(T min, T max, int bkt) {
2235 params.min = min;
2236 params.max = max;
2237 params.bucket_size = bkt;
2238 params.size = (max - min) / bkt + 1;
2239 bin.init(params);
2240 setInit();
2241
2242 return *this;
2243 }
2244 };
2245
2246 /**
2247 * Calculates the mean and variance of all the samples.
2248 * @sa Stat, DistBase, FancyStor
2249 */
2250 template <typename T = Counter, class Bin = NoBin>
2251 class StandardDeviation : public Detail::DistBase<T, Detail::FancyStor, Bin>
2252 {
2253 private:
2254 /** The base implementation */
2255 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2256 /** The parameter type. */
2257 typedef typename Detail::DistStor<T>::Params Params;
2258
2259 public:
2260 /**
2261 * Construct and initialize this distribution.
2262 */
2263 StandardDeviation() {
2264 bin.init(params);
2265 setInit();
2266 }
2267 };
2268
2269 /**
2270 * Calculates the per cycle mean and variance of the samples.
2271 * @sa Stat, DistBase, AvgFancy
2272 */
2273 template <typename T = Counter, class Bin = NoBin>
2274 class AverageDeviation : public Detail::DistBase<T, Detail::AvgFancy, Bin>
2275 {
2276 private:
2277 /** The base implementation */
2278 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2279 /** The parameter type. */
2280 typedef typename Detail::DistStor<T>::Params Params;
2281
2282 public:
2283 /**
2284 * Construct and initialize this distribution.
2285 */
2286 AverageDeviation() {
2287 bin.init(params);
2288 setInit();
2289 }
2290 };
2291
2292 /**
2293 * A vector of distributions.
2294 * @sa Stat, VectorDistBase, DistStor
2295 */
2296 template <typename T = Counter, class Bin = NoBin>
2297 class VectorDistribution
2298 : public Detail::VectorDistBase<T, Detail::DistStor, Bin>
2299 {
2300 private:
2301 /** The base implementation */
2302 typedef Detail::VectorDistBase<T, Detail::DistStor, Bin> Base;
2303 /** The parameter type. */
2304 typedef typename Detail::DistStor<T>::Params Params;
2305
2306 public:
2307 /**
2308 * Initialize storage and parameters for this distribution.
2309 * @param size The size of the vector (the number of distributions).
2310 * @param min The minimum value of the distribution.
2311 * @param max The maximum value of the distribution.
2312 * @param bkt The number of values in each bucket.
2313 * @return A reference to this distribution.
2314 */
2315 VectorDistribution &init(int size, T min, T max, int bkt) {
2316 params.min = min;
2317 params.max = max;
2318 params.bucket_size = bkt;
2319 params.size = (max - min) / bkt + 1;
2320 bin.init(size, params);
2321 setInit();
2322
2323 return *this;
2324 }
2325 };
2326
2327 /**
2328 * This is a vector of StandardDeviation stats.
2329 * @sa Stat, VectorDistBase, FancyStor
2330 */
2331 template <typename T = Counter, class Bin = NoBin>
2332 class VectorStandardDeviation
2333 : public Detail::VectorDistBase<T, Detail::FancyStor, Bin>
2334 {
2335 private:
2336 /** The base implementation */
2337 typedef Detail::VectorDistBase<T, Detail::FancyStor, Bin> Base;
2338 /** The parameter type. */
2339 typedef typename Detail::DistStor<T>::Params Params;
2340
2341 public:
2342 /**
2343 * Initialize storage for this distribution.
2344 * @param size The size of the vector.
2345 * @return A reference to this distribution.
2346 */
2347 VectorStandardDeviation &init(int size) {
2348 bin.init(size, params);
2349 setInit();
2350
2351 return *this;
2352 }
2353 };
2354
2355 /**
2356 * This is a vector of AverageDeviation stats.
2357 * @sa Stat, VectorDistBase, AvgFancy
2358 */
2359 template <typename T = Counter, class Bin = NoBin>
2360 class VectorAverageDeviation
2361 : public Detail::VectorDistBase<T, Detail::AvgFancy, Bin>
2362 {
2363 private:
2364 /** The base implementation */
2365 typedef Detail::VectorDistBase<T, Detail::AvgFancy, Bin> Base;
2366 /** The parameter type. */
2367 typedef typename Detail::DistStor<T>::Params Params;
2368
2369 public:
2370 /**
2371 * Initialize storage for this distribution.
2372 * @param size The size of the vector.
2373 * @return A reference to this distribution.
2374 */
2375 VectorAverageDeviation &init(int size) {
2376 bin.init(size, params);
2377 setInit();
2378
2379 return *this;
2380 }
2381 };
2382
2383 /**
2384 * A formula for statistics that is calculated when printed. A formula is
2385 * stored as a tree of Nodes that represent the equation to calculate.
2386 * @sa Stat, ScalarStat, VectorStat, Node, Detail::Temp
2387 */
2388 class Formula : public Detail::VectorStat
2389 {
2390 private:
2391 /** The root of the tree which represents the Formula */
2392 Detail::NodePtr root;
2393 friend class Statistics::Detail::Temp;
2394
2395 public:
2396 /**
2397 * Create and initialize thie formula, and register it with the database.
2398 */
2399 Formula() : VectorStat(true) { setInit(); }
2400 /**
2401 * Create a formula with the given root node, register it with the
2402 * database.
2403 * @param r The root of the expression tree.
2404 */
2405 Formula(Detail::Temp r) : VectorStat(true) {
2406 root = r;
2407 assert(size());
2408 }
2409
2410 /**
2411 * Set an unitialized Formula to the given root.
2412 * @param r The root of the expression tree.
2413 * @return a reference to this formula.
2414 */
2415 const Formula &operator=(Detail::Temp r) {
2416 assert(!root && "Can't change formulas");
2417 root = r;
2418 assert(size());
2419 return *this;
2420 }
2421
2422 /**
2423 * Add the given tree to the existing one.
2424 * @param r The root of the expression tree.
2425 * @return a reference to this formula.
2426 */
2427 const Formula &operator+=(Detail::Temp r) {
2428 using namespace Detail;
2429 if (root)
2430 root = NodePtr(new BinaryNode<std::plus<result_t> >(root, r));
2431 else
2432 root = r;
2433 assert(size());
2434 return *this;
2435 }
2436
2437 /**
2438 * Return the vector of values of this formula.
2439 * @return The result vector.
2440 */
2441 const rvec_t &val() const { return root->val(); }
2442 /**
2443 * Return the total of the result vector.
2444 * @return The total of the result vector.
2445 */
2446 result_t total() const { return root->total(); }
2447
2448 /**
2449 * Return the number of elements in the tree.
2450 */
2451 size_t size() const {
2452 if (!root)
2453 return 0;
2454 else
2455 return root->size();
2456 }
2457 };
2458
2459 /**
2460 * @}
2461 */
2462
2463 void check();
2464 void dump(std::ostream &stream);
2465
2466 inline Detail::Temp
2467 operator+(Detail::Temp l, Detail::Temp r)
2468 {
2469 using namespace Detail;
2470 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
2471 }
2472
2473 inline Detail::Temp
2474 operator-(Detail::Temp l, Detail::Temp r)
2475 {
2476 using namespace Detail;
2477 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
2478 }
2479
2480 inline Detail::Temp
2481 operator*(Detail::Temp l, Detail::Temp r)
2482 {
2483 using namespace Detail;
2484 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
2485 }
2486
2487 inline Detail::Temp
2488 operator/(Detail::Temp l, Detail::Temp r)
2489 {
2490 using namespace Detail;
2491 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
2492 }
2493
2494 inline Detail::Temp
2495 operator%(Detail::Temp l, Detail::Temp r)
2496 {
2497 using namespace Detail;
2498 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
2499 }
2500
2501 inline Detail::Temp
2502 operator-(Detail::Temp l)
2503 {
2504 using namespace Detail;
2505 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
2506 }
2507
2508 template <typename T>
2509 inline Detail::Temp
2510 constant(T val)
2511 {
2512 using namespace Detail;
2513 return NodePtr(new ConstNode<T>(val));
2514 }
2515
2516 template <typename T>
2517 inline Detail::Temp
2518 functor(T &val)
2519 {
2520 using namespace Detail;
2521 return NodePtr(new FunctorNode<T>(val));
2522 }
2523
2524 template <typename T>
2525 inline Detail::Temp
2526 scalar(T &val)
2527 {
2528 using namespace Detail;
2529 return NodePtr(new ScalarNode<T>(val));
2530 }
2531
2532 inline Detail::Temp
2533 sum(Detail::Temp val)
2534 {
2535 using namespace Detail;
2536 return NodePtr(new SumNode<std::plus<result_t> >(val));
2537 }
2538
2539 extern bool PrintDescriptions;
2540
2541 } // namespace statistics
2542
2543 #endif // __STATISTICS_HH__