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