base: Fix build errors with gcc 10.x
[gem5.git] / src / base / statistics.hh
1 /*
2 * Copyright (c) 2019-2020 Arm Limited
3 * All rights reserved.
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * Copyright (c) 2017, Centre National de la Recherche Scientifique
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 /** @file
43 * Declaration of Statistics objects.
44 */
45
46 /**
47 * @todo
48 *
49 * Generalized N-dimensinal vector
50 * documentation
51 * key stats
52 * interval stats
53 * -- these both can use the same function that prints out a
54 * specific set of stats
55 * VectorStandardDeviation totals
56 * Document Namespaces
57 */
58 #ifndef __BASE_STATISTICS_HH__
59 #define __BASE_STATISTICS_HH__
60
61 #include <algorithm>
62 #include <cassert>
63 #ifdef __SUNPRO_CC
64 #include <math.h>
65 #endif
66 #include <cmath>
67 #include <functional>
68 #include <iosfwd>
69 #include <list>
70 #include <map>
71 #include <memory>
72 #include <string>
73 #include <vector>
74
75 #include "base/stats/group.hh"
76 #include "base/stats/info.hh"
77 #include "base/stats/output.hh"
78 #include "base/stats/types.hh"
79 #include "base/cast.hh"
80 #include "base/cprintf.hh"
81 #include "base/intmath.hh"
82 #include "base/str.hh"
83 #include "base/types.hh"
84
85 class Callback;
86
87 /** The current simulated tick. */
88 extern Tick curTick();
89
90 /* A namespace for all of the Statistics */
91 namespace Stats {
92
93 template <class Stat, class Base>
94 class InfoProxy : public Base
95 {
96 protected:
97 Stat &s;
98
99 public:
100 InfoProxy(Stat &stat) : s(stat) {}
101
102 bool check() const { return s.check(); }
103 void prepare() { s.prepare(); }
104 void reset() { s.reset(); }
105 void
106 visit(Output &visitor)
107 {
108 visitor.visit(*static_cast<Base *>(this));
109 }
110 bool zero() const { return s.zero(); }
111 };
112
113 template <class Stat>
114 class ScalarInfoProxy : public InfoProxy<Stat, ScalarInfo>
115 {
116 public:
117 ScalarInfoProxy(Stat &stat) : InfoProxy<Stat, ScalarInfo>(stat) {}
118
119 Counter value() const { return this->s.value(); }
120 Result result() const { return this->s.result(); }
121 Result total() const { return this->s.total(); }
122 };
123
124 template <class Stat>
125 class VectorInfoProxy : public InfoProxy<Stat, VectorInfo>
126 {
127 protected:
128 mutable VCounter cvec;
129 mutable VResult rvec;
130
131 public:
132 VectorInfoProxy(Stat &stat) : InfoProxy<Stat, VectorInfo>(stat) {}
133
134 size_type size() const { return this->s.size(); }
135
136 VCounter &
137 value() const
138 {
139 this->s.value(cvec);
140 return cvec;
141 }
142
143 const VResult &
144 result() const
145 {
146 this->s.result(rvec);
147 return rvec;
148 }
149
150 Result total() const { return this->s.total(); }
151 };
152
153 template <class Stat>
154 class DistInfoProxy : public InfoProxy<Stat, DistInfo>
155 {
156 public:
157 DistInfoProxy(Stat &stat) : InfoProxy<Stat, DistInfo>(stat) {}
158 };
159
160 template <class Stat>
161 class VectorDistInfoProxy : public InfoProxy<Stat, VectorDistInfo>
162 {
163 public:
164 VectorDistInfoProxy(Stat &stat) : InfoProxy<Stat, VectorDistInfo>(stat) {}
165
166 size_type size() const { return this->s.size(); }
167 };
168
169 template <class Stat>
170 class Vector2dInfoProxy : public InfoProxy<Stat, Vector2dInfo>
171 {
172 public:
173 Vector2dInfoProxy(Stat &stat) : InfoProxy<Stat, Vector2dInfo>(stat) {}
174
175 Result total() const { return this->s.total(); }
176 };
177
178 struct StorageParams
179 {
180 virtual ~StorageParams();
181 };
182
183 class InfoAccess
184 {
185 private:
186 Info *_info;
187
188 protected:
189 /** Set up an info class for this statistic */
190 void setInfo(Group *parent, Info *info);
191 /** Save Storage class parameters if any */
192 void setParams(const StorageParams *params);
193 /** Save Storage class parameters if any */
194 void setInit();
195
196 /** Grab the information class for this statistic */
197 Info *info();
198 /** Grab the information class for this statistic */
199 const Info *info() const;
200
201 public:
202 InfoAccess()
203 : _info(nullptr) {};
204
205 /**
206 * Reset the stat to the default state.
207 */
208 void reset() { }
209
210 /**
211 * @return true if this stat has a value and satisfies its
212 * requirement as a prereq
213 */
214 bool zero() const { return true; }
215
216 /**
217 * Check that this stat has been set up properly and is ready for
218 * use
219 * @return true for success
220 */
221 bool check() const { return true; }
222 };
223
224 template <class Derived, template <class> class InfoProxyType>
225 class DataWrap : public InfoAccess
226 {
227 public:
228 typedef InfoProxyType<Derived> Info;
229
230 protected:
231 Derived &self() { return *static_cast<Derived *>(this); }
232
233 protected:
234 Info *
235 info()
236 {
237 return safe_cast<Info *>(InfoAccess::info());
238 }
239
240 public:
241 const Info *
242 info() const
243 {
244 return safe_cast<const Info *>(InfoAccess::info());
245 }
246
247 public:
248 DataWrap() = delete;
249 DataWrap(const DataWrap &) = delete;
250 DataWrap &operator=(const DataWrap &) = delete;
251
252
253 DataWrap(Group *parent, const char *name, const char *desc)
254 {
255 auto info = new Info(self());
256 this->setInfo(parent, info);
257
258 if (parent)
259 parent->addStat(info);
260
261 if (name) {
262 info->setName(parent, name);
263 info->flags.set(display);
264 }
265
266 if (desc)
267 info->desc = desc;
268 }
269
270 /**
271 * Set the name and marks this stat to print at the end of simulation.
272 * @param name The new name.
273 * @return A reference to this stat.
274 */
275 Derived &
276 name(const std::string &name)
277 {
278 Info *info = this->info();
279 info->setName(name);
280 info->flags.set(display);
281 return this->self();
282 }
283 const std::string &name() const { return this->info()->name; }
284
285 /**
286 * Set the character(s) used between the name and vector number
287 * on vectors, dist, etc.
288 * @param _sep The new separator string
289 * @return A reference to this stat.
290 */
291 Derived &
292 setSeparator(const std::string &_sep)
293 {
294 this->info()->setSeparator(_sep);
295 return this->self();
296 }
297 const std::string &setSeparator() const
298 {
299 return this->info()->separatorString;
300 }
301
302 /**
303 * Set the description and marks this stat to print at the end of
304 * simulation.
305 * @param desc The new description.
306 * @return A reference to this stat.
307 */
308 Derived &
309 desc(const std::string &_desc)
310 {
311 this->info()->desc = _desc;
312 return this->self();
313 }
314
315 /**
316 * Set the precision and marks this stat to print at the end of simulation.
317 * @param _precision The new precision
318 * @return A reference to this stat.
319 */
320 Derived &
321 precision(int _precision)
322 {
323 this->info()->precision = _precision;
324 return this->self();
325 }
326
327 /**
328 * Set the flags and marks this stat to print at the end of simulation.
329 * @param f The new flags.
330 * @return A reference to this stat.
331 */
332 Derived &
333 flags(Flags _flags)
334 {
335 this->info()->flags.set(_flags);
336 return this->self();
337 }
338
339 /**
340 * Set the prerequisite stat and marks this stat to print at the end of
341 * simulation.
342 * @param prereq The prerequisite stat.
343 * @return A reference to this stat.
344 */
345 template <class Stat>
346 Derived &
347 prereq(const Stat &prereq)
348 {
349 this->info()->prereq = prereq.info();
350 return this->self();
351 }
352 };
353
354 template <class Derived, template <class> class InfoProxyType>
355 class DataWrapVec : public DataWrap<Derived, InfoProxyType>
356 {
357 public:
358 typedef InfoProxyType<Derived> Info;
359
360 DataWrapVec(Group *parent = nullptr, const char *name = nullptr,
361 const char *desc = nullptr)
362 : DataWrap<Derived, InfoProxyType>(parent, name, desc)
363 {}
364
365 // The following functions are specific to vectors. If you use them
366 // in a non vector context, you will get a nice compiler error!
367
368 /**
369 * Set the subfield name for the given index, and marks this stat to print
370 * at the end of simulation.
371 * @param index The subfield index.
372 * @param name The new name of the subfield.
373 * @return A reference to this stat.
374 */
375 Derived &
376 subname(off_type index, const std::string &name)
377 {
378 Derived &self = this->self();
379 Info *info = self.info();
380
381 std::vector<std::string> &subn = info->subnames;
382 if (subn.size() <= index)
383 subn.resize(index + 1);
384 subn[index] = name;
385 return self;
386 }
387
388 // The following functions are specific to 2d vectors. If you use
389 // them in a non vector context, you will get a nice compiler
390 // error because info doesn't have the right variables.
391
392 /**
393 * Set the subfield description for the given index and marks this stat to
394 * print at the end of simulation.
395 * @param index The subfield index.
396 * @param desc The new description of the subfield
397 * @return A reference to this stat.
398 */
399 Derived &
400 subdesc(off_type index, const std::string &desc)
401 {
402 Info *info = this->info();
403
404 std::vector<std::string> &subd = info->subdescs;
405 if (subd.size() <= index)
406 subd.resize(index + 1);
407 subd[index] = desc;
408
409 return this->self();
410 }
411
412 void
413 prepare()
414 {
415 Derived &self = this->self();
416 Info *info = this->info();
417
418 size_t size = self.size();
419 for (off_type i = 0; i < size; ++i)
420 self.data(i)->prepare(info);
421 }
422
423 void
424 reset()
425 {
426 Derived &self = this->self();
427 Info *info = this->info();
428
429 size_t size = self.size();
430 for (off_type i = 0; i < size; ++i)
431 self.data(i)->reset(info);
432 }
433 };
434
435 template <class Derived, template <class> class InfoProxyType>
436 class DataWrapVec2d : public DataWrapVec<Derived, InfoProxyType>
437 {
438 public:
439 typedef InfoProxyType<Derived> Info;
440
441 DataWrapVec2d(Group *parent, const char *name, const char *desc)
442 : DataWrapVec<Derived, InfoProxyType>(parent, name, desc)
443 {
444 }
445
446 /**
447 * @warning This makes the assumption that if you're gonna subnames a 2d
448 * vector, you're subnaming across all y
449 */
450 Derived &
451 ysubnames(const char **names)
452 {
453 Derived &self = this->self();
454 Info *info = this->info();
455
456 info->y_subnames.resize(self.y);
457 for (off_type i = 0; i < self.y; ++i)
458 info->y_subnames[i] = names[i];
459 return self;
460 }
461
462 Derived &
463 ysubname(off_type index, const std::string &subname)
464 {
465 Derived &self = this->self();
466 Info *info = this->info();
467
468 assert(index < self.y);
469 info->y_subnames.resize(self.y);
470 info->y_subnames[index] = subname.c_str();
471 return self;
472 }
473
474 std::string
475 ysubname(off_type i) const
476 {
477 return this->info()->y_subnames[i];
478 }
479
480 };
481
482 //////////////////////////////////////////////////////////////////////
483 //
484 // Simple Statistics
485 //
486 //////////////////////////////////////////////////////////////////////
487
488 /**
489 * Templatized storage and interface for a simple scalar stat.
490 */
491 class StatStor
492 {
493 private:
494 /** The statistic value. */
495 Counter data;
496
497 public:
498 struct Params : public StorageParams {};
499
500 public:
501 /**
502 * Builds this storage element and calls the base constructor of the
503 * datatype.
504 */
505 StatStor(Info *info)
506 : data(Counter())
507 { }
508
509 /**
510 * The the stat to the given value.
511 * @param val The new value.
512 */
513 void set(Counter val) { data = val; }
514 /**
515 * Increment the stat by the given value.
516 * @param val The new value.
517 */
518 void inc(Counter val) { data += val; }
519 /**
520 * Decrement the stat by the given value.
521 * @param val The new value.
522 */
523 void dec(Counter val) { data -= val; }
524 /**
525 * Return the value of this stat as its base type.
526 * @return The value of this stat.
527 */
528 Counter value() const { return data; }
529 /**
530 * Return the value of this stat as a result type.
531 * @return The value of this stat.
532 */
533 Result result() const { return (Result)data; }
534 /**
535 * Prepare stat data for dumping or serialization
536 */
537 void prepare(Info *info) { }
538 /**
539 * Reset stat value to default
540 */
541 void reset(Info *info) { data = Counter(); }
542
543 /**
544 * @return true if zero value
545 */
546 bool zero() const { return data == Counter(); }
547 };
548
549 /**
550 * Templatized storage and interface to a per-tick average stat. This keeps
551 * a current count and updates a total (count * ticks) when this count
552 * changes. This allows the quick calculation of a per tick count of the item
553 * being watched. This is good for keeping track of residencies in structures
554 * among other things.
555 */
556 class AvgStor
557 {
558 private:
559 /** The current count. */
560 Counter current;
561 /** The tick of the last reset */
562 Tick lastReset;
563 /** The total count for all tick. */
564 mutable Result total;
565 /** The tick that current last changed. */
566 mutable Tick last;
567
568 public:
569 struct Params : public StorageParams {};
570
571 public:
572 /**
573 * Build and initializes this stat storage.
574 */
575 AvgStor(Info *info)
576 : current(0), lastReset(0), total(0), last(0)
577 { }
578
579 /**
580 * Set the current count to the one provided, update the total and last
581 * set values.
582 * @param val The new count.
583 */
584 void
585 set(Counter val)
586 {
587 total += current * (curTick() - last);
588 last = curTick();
589 current = val;
590 }
591
592 /**
593 * Increment the current count by the provided value, calls set.
594 * @param val The amount to increment.
595 */
596 void inc(Counter val) { set(current + val); }
597
598 /**
599 * Deccrement the current count by the provided value, calls set.
600 * @param val The amount to decrement.
601 */
602 void dec(Counter val) { set(current - val); }
603
604 /**
605 * Return the current count.
606 * @return The current count.
607 */
608 Counter value() const { return current; }
609
610 /**
611 * Return the current average.
612 * @return The current average.
613 */
614 Result
615 result() const
616 {
617 assert(last == curTick());
618 return (Result)(total + current) / (Result)(curTick() - lastReset + 1);
619 }
620
621 /**
622 * @return true if zero value
623 */
624 bool zero() const { return total == 0.0; }
625
626 /**
627 * Prepare stat data for dumping or serialization
628 */
629 void
630 prepare(Info *info)
631 {
632 total += current * (curTick() - last);
633 last = curTick();
634 }
635
636 /**
637 * Reset stat value to default
638 */
639 void
640 reset(Info *info)
641 {
642 total = 0.0;
643 last = curTick();
644 lastReset = curTick();
645 }
646
647 };
648
649 /**
650 * Implementation of a scalar stat. The type of stat is determined by the
651 * Storage template.
652 */
653 template <class Derived, class Stor>
654 class ScalarBase : public DataWrap<Derived, ScalarInfoProxy>
655 {
656 public:
657 typedef Stor Storage;
658 typedef typename Stor::Params Params;
659
660 protected:
661 /** The storage of this stat. */
662 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
663
664 protected:
665 /**
666 * Retrieve the storage.
667 * @param index The vector index to access.
668 * @return The storage object at the given index.
669 */
670 Storage *
671 data()
672 {
673 return reinterpret_cast<Storage *>(storage);
674 }
675
676 /**
677 * Retrieve a const pointer to the storage.
678 * for the given index.
679 * @param index The vector index to access.
680 * @return A const pointer to the storage object at the given index.
681 */
682 const Storage *
683 data() const
684 {
685 return reinterpret_cast<const Storage *>(storage);
686 }
687
688 void
689 doInit()
690 {
691 new (storage) Storage(this->info());
692 this->setInit();
693 }
694
695 public:
696 /**
697 * Return the current value of this stat as its base type.
698 * @return The current value.
699 */
700 Counter value() const { return data()->value(); }
701
702 public:
703 ScalarBase(Group *parent = nullptr, const char *name = nullptr,
704 const char *desc = nullptr)
705 : DataWrap<Derived, ScalarInfoProxy>(parent, name, desc)
706 {
707 this->doInit();
708 }
709
710 public:
711 // Common operators for stats
712 /**
713 * Increment the stat by 1. This calls the associated storage object inc
714 * function.
715 */
716 void operator++() { data()->inc(1); }
717 /**
718 * Decrement the stat by 1. This calls the associated storage object dec
719 * function.
720 */
721 void operator--() { data()->dec(1); }
722
723 /** Increment the stat by 1. */
724 void operator++(int) { ++*this; }
725 /** Decrement the stat by 1. */
726 void operator--(int) { --*this; }
727
728 /**
729 * Set the data value to the given value. This calls the associated storage
730 * object set function.
731 * @param v The new value.
732 */
733 template <typename U>
734 void operator=(const U &v) { data()->set(v); }
735
736 /**
737 * Increment the stat by the given value. This calls the associated
738 * storage object inc function.
739 * @param v The value to add.
740 */
741 template <typename U>
742 void operator+=(const U &v) { data()->inc(v); }
743
744 /**
745 * Decrement the stat by the given value. This calls the associated
746 * storage object dec function.
747 * @param v The value to substract.
748 */
749 template <typename U>
750 void operator-=(const U &v) { data()->dec(v); }
751
752 /**
753 * Return the number of elements, always 1 for a scalar.
754 * @return 1.
755 */
756 size_type size() const { return 1; }
757
758 Counter value() { return data()->value(); }
759
760 Result result() { return data()->result(); }
761
762 Result total() { return result(); }
763
764 bool zero() { return result() == 0.0; }
765
766 void reset() { data()->reset(this->info()); }
767 void prepare() { data()->prepare(this->info()); }
768 };
769
770 class ProxyInfo : public ScalarInfo
771 {
772 public:
773 std::string str() const { return std::to_string(value()); }
774 size_type size() const { return 1; }
775 bool check() const { return true; }
776 void prepare() { }
777 void reset() { }
778 bool zero() const { return value() == 0; }
779
780 void visit(Output &visitor) { visitor.visit(*this); }
781 };
782
783 template <class T>
784 class ValueProxy : public ProxyInfo
785 {
786 private:
787 T *scalar;
788
789 public:
790 ValueProxy(T &val) : scalar(&val) {}
791 Counter value() const { return *scalar; }
792 Result result() const { return *scalar; }
793 Result total() const { return *scalar; }
794 };
795
796 template <class T>
797 class FunctorProxy : public ProxyInfo
798 {
799 private:
800 T *functor;
801
802 public:
803 FunctorProxy(T &func) : functor(&func) {}
804 Counter value() const { return (*functor)(); }
805 Result result() const { return (*functor)(); }
806 Result total() const { return (*functor)(); }
807 };
808
809 /**
810 * A proxy similar to the FunctorProxy, but allows calling a method of a bound
811 * object, instead of a global free-standing function.
812 */
813 template <class T, class V>
814 class MethodProxy : public ProxyInfo
815 {
816 private:
817 T *object;
818 typedef V (T::*MethodPointer) () const;
819 MethodPointer method;
820
821 public:
822 MethodProxy(T *obj, MethodPointer meth) : object(obj), method(meth) {}
823 Counter value() const { return (object->*method)(); }
824 Result result() const { return (object->*method)(); }
825 Result total() const { return (object->*method)(); }
826 };
827
828 template <class Derived>
829 class ValueBase : public DataWrap<Derived, ScalarInfoProxy>
830 {
831 private:
832 ProxyInfo *proxy;
833
834 public:
835 ValueBase(Group *parent, const char *name, const char *desc)
836 : DataWrap<Derived, ScalarInfoProxy>(parent, name, desc),
837 proxy(NULL)
838 {
839 }
840
841 ~ValueBase() { if (proxy) delete proxy; }
842
843 template <class T>
844 Derived &
845 scalar(T &value)
846 {
847 proxy = new ValueProxy<T>(value);
848 this->setInit();
849 return this->self();
850 }
851
852 template <class T>
853 Derived &
854 functor(T &func)
855 {
856 proxy = new FunctorProxy<T>(func);
857 this->setInit();
858 return this->self();
859 }
860
861 /**
862 * Extended functor that calls the specified method of the provided object.
863 *
864 * @param obj Pointer to the object whose method should be called.
865 * @param method Pointer of the function / method of the object.
866 * @return Updated stats item.
867 */
868 template <class T, class V>
869 Derived &
870 method(T *obj, V (T::*method)() const)
871 {
872 proxy = new MethodProxy<T,V>(obj, method);
873 this->setInit();
874 return this->self();
875 }
876
877 Counter value() { return proxy->value(); }
878 Result result() const { return proxy->result(); }
879 Result total() const { return proxy->total(); };
880 size_type size() const { return proxy->size(); }
881
882 std::string str() const { return proxy->str(); }
883 bool zero() const { return proxy->zero(); }
884 bool check() const { return proxy != NULL; }
885 void prepare() { }
886 void reset() { }
887 };
888
889 //////////////////////////////////////////////////////////////////////
890 //
891 // Vector Statistics
892 //
893 //////////////////////////////////////////////////////////////////////
894
895 /**
896 * A proxy class to access the stat at a given index in a VectorBase stat.
897 * Behaves like a ScalarBase.
898 */
899 template <class Stat>
900 class ScalarProxy
901 {
902 private:
903 /** Pointer to the parent Vector. */
904 Stat &stat;
905
906 /** The index to access in the parent VectorBase. */
907 off_type index;
908
909 public:
910 /**
911 * Return the current value of this stat as its base type.
912 * @return The current value.
913 */
914 Counter value() const { return stat.data(index)->value(); }
915
916 /**
917 * Return the current value of this statas a result type.
918 * @return The current value.
919 */
920 Result result() const { return stat.data(index)->result(); }
921
922 public:
923 /**
924 * Create and initialize this proxy, do not register it with the database.
925 * @param i The index to access.
926 */
927 ScalarProxy(Stat &s, off_type i)
928 : stat(s), index(i)
929 {
930 }
931
932 /**
933 * Create a copy of the provided ScalarProxy.
934 * @param sp The proxy to copy.
935 */
936 ScalarProxy(const ScalarProxy &sp)
937 : stat(sp.stat), index(sp.index)
938 {}
939
940 /**
941 * Set this proxy equal to the provided one.
942 * @param sp The proxy to copy.
943 * @return A reference to this proxy.
944 */
945 const ScalarProxy &
946 operator=(const ScalarProxy &sp)
947 {
948 stat = sp.stat;
949 index = sp.index;
950 return *this;
951 }
952
953 public:
954 // Common operators for stats
955 /**
956 * Increment the stat by 1. This calls the associated storage object inc
957 * function.
958 */
959 void operator++() { stat.data(index)->inc(1); }
960 /**
961 * Decrement the stat by 1. This calls the associated storage object dec
962 * function.
963 */
964 void operator--() { stat.data(index)->dec(1); }
965
966 /** Increment the stat by 1. */
967 void operator++(int) { ++*this; }
968 /** Decrement the stat by 1. */
969 void operator--(int) { --*this; }
970
971 /**
972 * Set the data value to the given value. This calls the associated storage
973 * object set function.
974 * @param v The new value.
975 */
976 template <typename U>
977 void
978 operator=(const U &v)
979 {
980 stat.data(index)->set(v);
981 }
982
983 /**
984 * Increment the stat by the given value. This calls the associated
985 * storage object inc function.
986 * @param v The value to add.
987 */
988 template <typename U>
989 void
990 operator+=(const U &v)
991 {
992 stat.data(index)->inc(v);
993 }
994
995 /**
996 * Decrement the stat by the given value. This calls the associated
997 * storage object dec function.
998 * @param v The value to substract.
999 */
1000 template <typename U>
1001 void
1002 operator-=(const U &v)
1003 {
1004 stat.data(index)->dec(v);
1005 }
1006
1007 /**
1008 * Return the number of elements, always 1 for a scalar.
1009 * @return 1.
1010 */
1011 size_type size() const { return 1; }
1012
1013 public:
1014 std::string
1015 str() const
1016 {
1017 return csprintf("%s[%d]", stat.info()->name, index);
1018 }
1019 };
1020
1021 /**
1022 * Implementation of a vector of stats. The type of stat is determined by the
1023 * Storage class. @sa ScalarBase
1024 */
1025 template <class Derived, class Stor>
1026 class VectorBase : public DataWrapVec<Derived, VectorInfoProxy>
1027 {
1028 public:
1029 typedef Stor Storage;
1030 typedef typename Stor::Params Params;
1031
1032 /** Proxy type */
1033 typedef ScalarProxy<Derived> Proxy;
1034 friend class ScalarProxy<Derived>;
1035 friend class DataWrapVec<Derived, VectorInfoProxy>;
1036
1037 protected:
1038 /** The storage of this stat. */
1039 Storage *storage;
1040 size_type _size;
1041
1042 protected:
1043 /**
1044 * Retrieve the storage.
1045 * @param index The vector index to access.
1046 * @return The storage object at the given index.
1047 */
1048 Storage *data(off_type index) { return &storage[index]; }
1049
1050 /**
1051 * Retrieve a const pointer to the storage.
1052 * @param index The vector index to access.
1053 * @return A const pointer to the storage object at the given index.
1054 */
1055 const Storage *data(off_type index) const { return &storage[index]; }
1056
1057 void
1058 doInit(size_type s)
1059 {
1060 assert(s > 0 && "size must be positive!");
1061 assert(!storage && "already initialized");
1062 _size = s;
1063
1064 char *ptr = new char[_size * sizeof(Storage)];
1065 storage = reinterpret_cast<Storage *>(ptr);
1066
1067 for (off_type i = 0; i < _size; ++i)
1068 new (&storage[i]) Storage(this->info());
1069
1070 this->setInit();
1071 }
1072
1073 public:
1074 void
1075 value(VCounter &vec) const
1076 {
1077 vec.resize(size());
1078 for (off_type i = 0; i < size(); ++i)
1079 vec[i] = data(i)->value();
1080 }
1081
1082 /**
1083 * Copy the values to a local vector and return a reference to it.
1084 * @return A reference to a vector of the stat values.
1085 */
1086 void
1087 result(VResult &vec) const
1088 {
1089 vec.resize(size());
1090 for (off_type i = 0; i < size(); ++i)
1091 vec[i] = data(i)->result();
1092 }
1093
1094 /**
1095 * Return a total of all entries in this vector.
1096 * @return The total of all vector entries.
1097 */
1098 Result
1099 total() const
1100 {
1101 Result total = 0.0;
1102 for (off_type i = 0; i < size(); ++i)
1103 total += data(i)->result();
1104 return total;
1105 }
1106
1107 /**
1108 * @return the number of elements in this vector.
1109 */
1110 size_type size() const { return _size; }
1111
1112 bool
1113 zero() const
1114 {
1115 for (off_type i = 0; i < size(); ++i)
1116 if (data(i)->zero())
1117 return false;
1118 return true;
1119 }
1120
1121 bool
1122 check() const
1123 {
1124 return storage != NULL;
1125 }
1126
1127 public:
1128 VectorBase(Group *parent, const char *name, const char *desc)
1129 : DataWrapVec<Derived, VectorInfoProxy>(parent, name, desc),
1130 storage(nullptr), _size(0)
1131 {}
1132
1133 ~VectorBase()
1134 {
1135 if (!storage)
1136 return;
1137
1138 for (off_type i = 0; i < _size; ++i)
1139 data(i)->~Storage();
1140 delete [] reinterpret_cast<char *>(storage);
1141 }
1142
1143 /**
1144 * Set this vector to have the given size.
1145 * @param size The new size.
1146 * @return A reference to this stat.
1147 */
1148 Derived &
1149 init(size_type size)
1150 {
1151 Derived &self = this->self();
1152 self.doInit(size);
1153 return self;
1154 }
1155
1156 /**
1157 * Return a reference (ScalarProxy) to the stat at the given index.
1158 * @param index The vector index to access.
1159 * @return A reference of the stat.
1160 */
1161 Proxy
1162 operator[](off_type index)
1163 {
1164 assert (index < size());
1165 return Proxy(this->self(), index);
1166 }
1167 };
1168
1169 template <class Stat>
1170 class VectorProxy
1171 {
1172 private:
1173 Stat &stat;
1174 off_type offset;
1175 size_type len;
1176
1177 private:
1178 mutable VResult vec;
1179
1180 typename Stat::Storage *
1181 data(off_type index)
1182 {
1183 assert(index < len);
1184 return stat.data(offset + index);
1185 }
1186
1187 const typename Stat::Storage *
1188 data(off_type index) const
1189 {
1190 assert(index < len);
1191 return stat.data(offset + index);
1192 }
1193
1194 public:
1195 const VResult &
1196 result() const
1197 {
1198 vec.resize(size());
1199
1200 for (off_type i = 0; i < size(); ++i)
1201 vec[i] = data(i)->result();
1202
1203 return vec;
1204 }
1205
1206 Result
1207 total() const
1208 {
1209 Result total = 0.0;
1210 for (off_type i = 0; i < size(); ++i)
1211 total += data(i)->result();
1212 return total;
1213 }
1214
1215 public:
1216 VectorProxy(Stat &s, off_type o, size_type l)
1217 : stat(s), offset(o), len(l)
1218 {
1219 }
1220
1221 VectorProxy(const VectorProxy &sp)
1222 : stat(sp.stat), offset(sp.offset), len(sp.len)
1223 {
1224 }
1225
1226 const VectorProxy &
1227 operator=(const VectorProxy &sp)
1228 {
1229 stat = sp.stat;
1230 offset = sp.offset;
1231 len = sp.len;
1232 return *this;
1233 }
1234
1235 ScalarProxy<Stat>
1236 operator[](off_type index)
1237 {
1238 assert (index < size());
1239 return ScalarProxy<Stat>(stat, offset + index);
1240 }
1241
1242 size_type size() const { return len; }
1243 };
1244
1245 template <class Derived, class Stor>
1246 class Vector2dBase : public DataWrapVec2d<Derived, Vector2dInfoProxy>
1247 {
1248 public:
1249 typedef Vector2dInfoProxy<Derived> Info;
1250 typedef Stor Storage;
1251 typedef typename Stor::Params Params;
1252 typedef VectorProxy<Derived> Proxy;
1253 friend class ScalarProxy<Derived>;
1254 friend class VectorProxy<Derived>;
1255 friend class DataWrapVec<Derived, Vector2dInfoProxy>;
1256 friend class DataWrapVec2d<Derived, Vector2dInfoProxy>;
1257
1258 protected:
1259 size_type x;
1260 size_type y;
1261 size_type _size;
1262 Storage *storage;
1263
1264 protected:
1265 Storage *data(off_type index) { return &storage[index]; }
1266 const Storage *data(off_type index) const { return &storage[index]; }
1267
1268 public:
1269 Vector2dBase(Group *parent, const char *name, const char *desc)
1270 : DataWrapVec2d<Derived, Vector2dInfoProxy>(parent, name, desc),
1271 x(0), y(0), _size(0), storage(nullptr)
1272 {}
1273
1274 ~Vector2dBase()
1275 {
1276 if (!storage)
1277 return;
1278
1279 for (off_type i = 0; i < _size; ++i)
1280 data(i)->~Storage();
1281 delete [] reinterpret_cast<char *>(storage);
1282 }
1283
1284 Derived &
1285 init(size_type _x, size_type _y)
1286 {
1287 assert(_x > 0 && _y > 0 && "sizes must be positive!");
1288 assert(!storage && "already initialized");
1289
1290 Derived &self = this->self();
1291 Info *info = this->info();
1292
1293 x = _x;
1294 y = _y;
1295 info->x = _x;
1296 info->y = _y;
1297 _size = x * y;
1298
1299 char *ptr = new char[_size * sizeof(Storage)];
1300 storage = reinterpret_cast<Storage *>(ptr);
1301
1302 for (off_type i = 0; i < _size; ++i)
1303 new (&storage[i]) Storage(info);
1304
1305 this->setInit();
1306
1307 return self;
1308 }
1309
1310 Proxy
1311 operator[](off_type index)
1312 {
1313 off_type offset = index * y;
1314 assert (offset + y <= size());
1315 return Proxy(this->self(), offset, y);
1316 }
1317
1318
1319 size_type
1320 size() const
1321 {
1322 return _size;
1323 }
1324
1325 bool
1326 zero() const
1327 {
1328 return data(0)->zero();
1329 }
1330
1331 /**
1332 * Return a total of all entries in this vector.
1333 * @return The total of all vector entries.
1334 */
1335 Result
1336 total() const
1337 {
1338 Result total = 0.0;
1339 for (off_type i = 0; i < size(); ++i)
1340 total += data(i)->result();
1341 return total;
1342 }
1343
1344 void
1345 prepare()
1346 {
1347 Info *info = this->info();
1348 size_type size = this->size();
1349
1350 for (off_type i = 0; i < size; ++i)
1351 data(i)->prepare(info);
1352
1353 info->cvec.resize(size);
1354 for (off_type i = 0; i < size; ++i)
1355 info->cvec[i] = data(i)->value();
1356 }
1357
1358 /**
1359 * Reset stat value to default
1360 */
1361 void
1362 reset()
1363 {
1364 Info *info = this->info();
1365 size_type size = this->size();
1366 for (off_type i = 0; i < size; ++i)
1367 data(i)->reset(info);
1368 }
1369
1370 bool
1371 check() const
1372 {
1373 return storage != NULL;
1374 }
1375 };
1376
1377 //////////////////////////////////////////////////////////////////////
1378 //
1379 // Non formula statistics
1380 //
1381 //////////////////////////////////////////////////////////////////////
1382 /** The parameters for a distribution stat. */
1383 struct DistParams : public StorageParams
1384 {
1385 const DistType type;
1386 DistParams(DistType t) : type(t) {}
1387 };
1388
1389 /**
1390 * Templatized storage and interface for a distribution stat.
1391 */
1392 class DistStor
1393 {
1394 public:
1395 /** The parameters for a distribution stat. */
1396 struct Params : public DistParams
1397 {
1398 /** The minimum value to track. */
1399 Counter min;
1400 /** The maximum value to track. */
1401 Counter max;
1402 /** The number of entries in each bucket. */
1403 Counter bucket_size;
1404 /** The number of buckets. Equal to (max-min)/bucket_size. */
1405 size_type buckets;
1406
1407 Params() : DistParams(Dist), min(0), max(0), bucket_size(0),
1408 buckets(0) {}
1409 };
1410
1411 private:
1412 /** The minimum value to track. */
1413 Counter min_track;
1414 /** The maximum value to track. */
1415 Counter max_track;
1416 /** The number of entries in each bucket. */
1417 Counter bucket_size;
1418
1419 /** The smallest value sampled. */
1420 Counter min_val;
1421 /** The largest value sampled. */
1422 Counter max_val;
1423 /** The number of values sampled less than min. */
1424 Counter underflow;
1425 /** The number of values sampled more than max. */
1426 Counter overflow;
1427 /** The current sum. */
1428 Counter sum;
1429 /** The sum of squares. */
1430 Counter squares;
1431 /** The number of samples. */
1432 Counter samples;
1433 /** Counter for each bucket. */
1434 VCounter cvec;
1435
1436 public:
1437 DistStor(Info *info)
1438 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1439 {
1440 reset(info);
1441 }
1442
1443 /**
1444 * Add a value to the distribution for the given number of times.
1445 * @param val The value to add.
1446 * @param number The number of times to add the value.
1447 */
1448 void
1449 sample(Counter val, int number)
1450 {
1451 if (val < min_track)
1452 underflow += number;
1453 else if (val > max_track)
1454 overflow += number;
1455 else {
1456 size_type index =
1457 (size_type)std::floor((val - min_track) / bucket_size);
1458 assert(index < size());
1459 cvec[index] += number;
1460 }
1461
1462 if (val < min_val)
1463 min_val = val;
1464
1465 if (val > max_val)
1466 max_val = val;
1467
1468 sum += val * number;
1469 squares += val * val * number;
1470 samples += number;
1471 }
1472
1473 /**
1474 * Return the number of buckets in this distribution.
1475 * @return the number of buckets.
1476 */
1477 size_type size() const { return cvec.size(); }
1478
1479 /**
1480 * Returns true if any calls to sample have been made.
1481 * @return True if any values have been sampled.
1482 */
1483 bool
1484 zero() const
1485 {
1486 return samples == Counter();
1487 }
1488
1489 void
1490 prepare(Info *info, DistData &data)
1491 {
1492 const Params *params = safe_cast<const Params *>(info->storageParams);
1493
1494 assert(params->type == Dist);
1495 data.type = params->type;
1496 data.min = params->min;
1497 data.max = params->max;
1498 data.bucket_size = params->bucket_size;
1499
1500 data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
1501 data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
1502 data.underflow = underflow;
1503 data.overflow = overflow;
1504
1505 data.cvec.resize(params->buckets);
1506 for (off_type i = 0; i < params->buckets; ++i)
1507 data.cvec[i] = cvec[i];
1508
1509 data.sum = sum;
1510 data.squares = squares;
1511 data.samples = samples;
1512 }
1513
1514 /**
1515 * Reset stat value to default
1516 */
1517 void
1518 reset(Info *info)
1519 {
1520 const Params *params = safe_cast<const Params *>(info->storageParams);
1521 min_track = params->min;
1522 max_track = params->max;
1523 bucket_size = params->bucket_size;
1524
1525 min_val = CounterLimits::max();
1526 max_val = CounterLimits::min();
1527 underflow = Counter();
1528 overflow = Counter();
1529
1530 size_type size = cvec.size();
1531 for (off_type i = 0; i < size; ++i)
1532 cvec[i] = Counter();
1533
1534 sum = Counter();
1535 squares = Counter();
1536 samples = Counter();
1537 }
1538 };
1539
1540 /**
1541 * Templatized storage and interface for a histogram stat.
1542 */
1543 class HistStor
1544 {
1545 public:
1546 /** The parameters for a distribution stat. */
1547 struct Params : public DistParams
1548 {
1549 /** The number of buckets.. */
1550 size_type buckets;
1551
1552 Params() : DistParams(Hist), buckets(0) {}
1553 };
1554
1555 private:
1556 /** The minimum value to track. */
1557 Counter min_bucket;
1558 /** The maximum value to track. */
1559 Counter max_bucket;
1560 /** The number of entries in each bucket. */
1561 Counter bucket_size;
1562
1563 /** The current sum. */
1564 Counter sum;
1565 /** The sum of logarithm of each sample, used to compute geometric mean. */
1566 Counter logs;
1567 /** The sum of squares. */
1568 Counter squares;
1569 /** The number of samples. */
1570 Counter samples;
1571 /** Counter for each bucket. */
1572 VCounter cvec;
1573
1574 public:
1575 HistStor(Info *info)
1576 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1577 {
1578 reset(info);
1579 }
1580
1581 void grow_up();
1582 void grow_out();
1583 void grow_convert();
1584 void add(HistStor *);
1585
1586 /**
1587 * Add a value to the distribution for the given number of times.
1588 * @param val The value to add.
1589 * @param number The number of times to add the value.
1590 */
1591 void
1592 sample(Counter val, int number)
1593 {
1594 assert(min_bucket < max_bucket);
1595 if (val < min_bucket) {
1596 if (min_bucket == 0)
1597 grow_convert();
1598
1599 while (val < min_bucket)
1600 grow_out();
1601 } else if (val >= max_bucket + bucket_size) {
1602 if (min_bucket == 0) {
1603 while (val >= max_bucket + bucket_size)
1604 grow_up();
1605 } else {
1606 while (val >= max_bucket + bucket_size)
1607 grow_out();
1608 }
1609 }
1610
1611 size_type index =
1612 (int64_t)std::floor((val - min_bucket) / bucket_size);
1613
1614 assert(index < size());
1615 cvec[index] += number;
1616
1617 sum += val * number;
1618 squares += val * val * number;
1619 logs += log(val) * number;
1620 samples += number;
1621 }
1622
1623 /**
1624 * Return the number of buckets in this distribution.
1625 * @return the number of buckets.
1626 */
1627 size_type size() const { return cvec.size(); }
1628
1629 /**
1630 * Returns true if any calls to sample have been made.
1631 * @return True if any values have been sampled.
1632 */
1633 bool
1634 zero() const
1635 {
1636 return samples == Counter();
1637 }
1638
1639 void
1640 prepare(Info *info, DistData &data)
1641 {
1642 const Params *params = safe_cast<const Params *>(info->storageParams);
1643
1644 assert(params->type == Hist);
1645 data.type = params->type;
1646 data.min = min_bucket;
1647 data.max = max_bucket + bucket_size - 1;
1648 data.bucket_size = bucket_size;
1649
1650 data.min_val = min_bucket;
1651 data.max_val = max_bucket;
1652
1653 int buckets = params->buckets;
1654 data.cvec.resize(buckets);
1655 for (off_type i = 0; i < buckets; ++i)
1656 data.cvec[i] = cvec[i];
1657
1658 data.sum = sum;
1659 data.logs = logs;
1660 data.squares = squares;
1661 data.samples = samples;
1662 }
1663
1664 /**
1665 * Reset stat value to default
1666 */
1667 void
1668 reset(Info *info)
1669 {
1670 const Params *params = safe_cast<const Params *>(info->storageParams);
1671 min_bucket = 0;
1672 max_bucket = params->buckets - 1;
1673 bucket_size = 1;
1674
1675 size_type size = cvec.size();
1676 for (off_type i = 0; i < size; ++i)
1677 cvec[i] = Counter();
1678
1679 sum = Counter();
1680 squares = Counter();
1681 samples = Counter();
1682 logs = Counter();
1683 }
1684 };
1685
1686 /**
1687 * Templatized storage and interface for a distribution that calculates mean
1688 * and variance.
1689 */
1690 class SampleStor
1691 {
1692 public:
1693 struct Params : public DistParams
1694 {
1695 Params() : DistParams(Deviation) {}
1696 };
1697
1698 private:
1699 /** The current sum. */
1700 Counter sum;
1701 /** The sum of squares. */
1702 Counter squares;
1703 /** The number of samples. */
1704 Counter samples;
1705
1706 public:
1707 /**
1708 * Create and initialize this storage.
1709 */
1710 SampleStor(Info *info)
1711 : sum(Counter()), squares(Counter()), samples(Counter())
1712 { }
1713
1714 /**
1715 * Add a value the given number of times to this running average.
1716 * Update the running sum and sum of squares, increment the number of
1717 * values seen by the given number.
1718 * @param val The value to add.
1719 * @param number The number of times to add the value.
1720 */
1721 void
1722 sample(Counter val, int number)
1723 {
1724 sum += val * number;
1725 squares += val * val * number;
1726 samples += number;
1727 }
1728
1729 /**
1730 * Return the number of entries in this stat, 1
1731 * @return 1.
1732 */
1733 size_type size() const { return 1; }
1734
1735 /**
1736 * Return true if no samples have been added.
1737 * @return True if no samples have been added.
1738 */
1739 bool zero() const { return samples == Counter(); }
1740
1741 void
1742 prepare(Info *info, DistData &data)
1743 {
1744 const Params *params = safe_cast<const Params *>(info->storageParams);
1745
1746 assert(params->type == Deviation);
1747 data.type = params->type;
1748 data.sum = sum;
1749 data.squares = squares;
1750 data.samples = samples;
1751 }
1752
1753 /**
1754 * Reset stat value to default
1755 */
1756 void
1757 reset(Info *info)
1758 {
1759 sum = Counter();
1760 squares = Counter();
1761 samples = Counter();
1762 }
1763 };
1764
1765 /**
1766 * Templatized storage for distribution that calculates per tick mean and
1767 * variance.
1768 */
1769 class AvgSampleStor
1770 {
1771 public:
1772 struct Params : public DistParams
1773 {
1774 Params() : DistParams(Deviation) {}
1775 };
1776
1777 private:
1778 /** Current total. */
1779 Counter sum;
1780 /** Current sum of squares. */
1781 Counter squares;
1782
1783 public:
1784 /**
1785 * Create and initialize this storage.
1786 */
1787 AvgSampleStor(Info *info)
1788 : sum(Counter()), squares(Counter())
1789 {}
1790
1791 /**
1792 * Add a value to the distribution for the given number of times.
1793 * Update the running sum and sum of squares.
1794 * @param val The value to add.
1795 * @param number The number of times to add the value.
1796 */
1797 void
1798 sample(Counter val, int number)
1799 {
1800 sum += val * number;
1801 squares += val * val * number;
1802 }
1803
1804 /**
1805 * Return the number of entries, in this case 1.
1806 * @return 1.
1807 */
1808 size_type size() const { return 1; }
1809
1810 /**
1811 * Return true if no samples have been added.
1812 * @return True if the sum is zero.
1813 */
1814 bool zero() const { return sum == Counter(); }
1815
1816 void
1817 prepare(Info *info, DistData &data)
1818 {
1819 const Params *params = safe_cast<const Params *>(info->storageParams);
1820
1821 assert(params->type == Deviation);
1822 data.type = params->type;
1823 data.sum = sum;
1824 data.squares = squares;
1825 data.samples = curTick();
1826 }
1827
1828 /**
1829 * Reset stat value to default
1830 */
1831 void
1832 reset(Info *info)
1833 {
1834 sum = Counter();
1835 squares = Counter();
1836 }
1837 };
1838
1839 /**
1840 * Implementation of a distribution stat. The type of distribution is
1841 * determined by the Storage template. @sa ScalarBase
1842 */
1843 template <class Derived, class Stor>
1844 class DistBase : public DataWrap<Derived, DistInfoProxy>
1845 {
1846 public:
1847 typedef DistInfoProxy<Derived> Info;
1848 typedef Stor Storage;
1849 typedef typename Stor::Params Params;
1850
1851 protected:
1852 /** The storage for this stat. */
1853 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
1854
1855 protected:
1856 /**
1857 * Retrieve the storage.
1858 * @return The storage object for this stat.
1859 */
1860 Storage *
1861 data()
1862 {
1863 return reinterpret_cast<Storage *>(storage);
1864 }
1865
1866 /**
1867 * Retrieve a const pointer to the storage.
1868 * @return A const pointer to the storage object for this stat.
1869 */
1870 const Storage *
1871 data() const
1872 {
1873 return reinterpret_cast<const Storage *>(storage);
1874 }
1875
1876 void
1877 doInit()
1878 {
1879 new (storage) Storage(this->info());
1880 this->setInit();
1881 }
1882
1883 public:
1884 DistBase(Group *parent, const char *name, const char *desc)
1885 : DataWrap<Derived, DistInfoProxy>(parent, name, desc)
1886 {
1887 }
1888
1889 /**
1890 * Add a value to the distribtion n times. Calls sample on the storage
1891 * class.
1892 * @param v The value to add.
1893 * @param n The number of times to add it, defaults to 1.
1894 */
1895 template <typename U>
1896 void sample(const U &v, int n = 1) { data()->sample(v, n); }
1897
1898 /**
1899 * Return the number of entries in this stat.
1900 * @return The number of entries.
1901 */
1902 size_type size() const { return data()->size(); }
1903 /**
1904 * Return true if no samples have been added.
1905 * @return True if there haven't been any samples.
1906 */
1907 bool zero() const { return data()->zero(); }
1908
1909 void
1910 prepare()
1911 {
1912 Info *info = this->info();
1913 data()->prepare(info, info->data);
1914 }
1915
1916 /**
1917 * Reset stat value to default
1918 */
1919 void
1920 reset()
1921 {
1922 data()->reset(this->info());
1923 }
1924
1925 /**
1926 * Add the argument distribution to the this distribution.
1927 */
1928 void add(DistBase &d) { data()->add(d.data()); }
1929
1930 };
1931
1932 template <class Stat>
1933 class DistProxy;
1934
1935 template <class Derived, class Stor>
1936 class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy>
1937 {
1938 public:
1939 typedef VectorDistInfoProxy<Derived> Info;
1940 typedef Stor Storage;
1941 typedef typename Stor::Params Params;
1942 typedef DistProxy<Derived> Proxy;
1943 friend class DistProxy<Derived>;
1944 friend class DataWrapVec<Derived, VectorDistInfoProxy>;
1945
1946 protected:
1947 Storage *storage;
1948 size_type _size;
1949
1950 protected:
1951 Storage *
1952 data(off_type index)
1953 {
1954 return &storage[index];
1955 }
1956
1957 const Storage *
1958 data(off_type index) const
1959 {
1960 return &storage[index];
1961 }
1962
1963 void
1964 doInit(size_type s)
1965 {
1966 assert(s > 0 && "size must be positive!");
1967 assert(!storage && "already initialized");
1968 _size = s;
1969
1970 char *ptr = new char[_size * sizeof(Storage)];
1971 storage = reinterpret_cast<Storage *>(ptr);
1972
1973 Info *info = this->info();
1974 for (off_type i = 0; i < _size; ++i)
1975 new (&storage[i]) Storage(info);
1976
1977 this->setInit();
1978 }
1979
1980 public:
1981 VectorDistBase(Group *parent, const char *name, const char *desc)
1982 : DataWrapVec<Derived, VectorDistInfoProxy>(parent, name, desc),
1983 storage(NULL)
1984 {}
1985
1986 ~VectorDistBase()
1987 {
1988 if (!storage)
1989 return ;
1990
1991 for (off_type i = 0; i < _size; ++i)
1992 data(i)->~Storage();
1993 delete [] reinterpret_cast<char *>(storage);
1994 }
1995
1996 Proxy operator[](off_type index)
1997 {
1998 assert(index < size());
1999 return Proxy(this->self(), index);
2000 }
2001
2002 size_type
2003 size() const
2004 {
2005 return _size;
2006 }
2007
2008 bool
2009 zero() const
2010 {
2011 for (off_type i = 0; i < size(); ++i)
2012 if (!data(i)->zero())
2013 return false;
2014 return true;
2015 }
2016
2017 void
2018 prepare()
2019 {
2020 Info *info = this->info();
2021 size_type size = this->size();
2022 info->data.resize(size);
2023 for (off_type i = 0; i < size; ++i)
2024 data(i)->prepare(info, info->data[i]);
2025 }
2026
2027 bool
2028 check() const
2029 {
2030 return storage != NULL;
2031 }
2032 };
2033
2034 template <class Stat>
2035 class DistProxy
2036 {
2037 private:
2038 Stat &stat;
2039 off_type index;
2040
2041 protected:
2042 typename Stat::Storage *data() { return stat.data(index); }
2043 const typename Stat::Storage *data() const { return stat.data(index); }
2044
2045 public:
2046 DistProxy(Stat &s, off_type i)
2047 : stat(s), index(i)
2048 {}
2049
2050 DistProxy(const DistProxy &sp)
2051 : stat(sp.stat), index(sp.index)
2052 {}
2053
2054 const DistProxy &
2055 operator=(const DistProxy &sp)
2056 {
2057 stat = sp.stat;
2058 index = sp.index;
2059 return *this;
2060 }
2061
2062 public:
2063 template <typename U>
2064 void
2065 sample(const U &v, int n = 1)
2066 {
2067 data()->sample(v, n);
2068 }
2069
2070 size_type
2071 size() const
2072 {
2073 return 1;
2074 }
2075
2076 bool
2077 zero() const
2078 {
2079 return data()->zero();
2080 }
2081
2082 /**
2083 * Proxy has no state. Nothing to reset.
2084 */
2085 void reset() { }
2086 };
2087
2088 //////////////////////////////////////////////////////////////////////
2089 //
2090 // Formula Details
2091 //
2092 //////////////////////////////////////////////////////////////////////
2093
2094 /**
2095 * Base class for formula statistic node. These nodes are used to build a tree
2096 * that represents the formula.
2097 */
2098 class Node
2099 {
2100 public:
2101 /**
2102 * Return the number of nodes in the subtree starting at this node.
2103 * @return the number of nodes in this subtree.
2104 */
2105 virtual size_type size() const = 0;
2106 /**
2107 * Return the result vector of this subtree.
2108 * @return The result vector of this subtree.
2109 */
2110 virtual const VResult &result() const = 0;
2111 /**
2112 * Return the total of the result vector.
2113 * @return The total of the result vector.
2114 */
2115 virtual Result total() const = 0;
2116
2117 /**
2118 *
2119 */
2120 virtual std::string str() const = 0;
2121
2122 virtual ~Node() {};
2123 };
2124
2125 /** Shared pointer to a function Node. */
2126 typedef std::shared_ptr<Node> NodePtr;
2127
2128 class ScalarStatNode : public Node
2129 {
2130 private:
2131 const ScalarInfo *data;
2132 mutable VResult vresult;
2133
2134 public:
2135 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {}
2136
2137 const VResult &
2138 result() const
2139 {
2140 vresult[0] = data->result();
2141 return vresult;
2142 }
2143
2144 Result total() const { return data->result(); };
2145
2146 size_type size() const { return 1; }
2147
2148 /**
2149 *
2150 */
2151 std::string str() const { return data->name; }
2152 };
2153
2154 template <class Stat>
2155 class ScalarProxyNode : public Node
2156 {
2157 private:
2158 const ScalarProxy<Stat> proxy;
2159 mutable VResult vresult;
2160
2161 public:
2162 ScalarProxyNode(const ScalarProxy<Stat> &p)
2163 : proxy(p), vresult(1)
2164 { }
2165
2166 const VResult &
2167 result() const
2168 {
2169 vresult[0] = proxy.result();
2170 return vresult;
2171 }
2172
2173 Result
2174 total() const
2175 {
2176 return proxy.result();
2177 }
2178
2179 size_type
2180 size() const
2181 {
2182 return 1;
2183 }
2184
2185 /**
2186 *
2187 */
2188 std::string
2189 str() const
2190 {
2191 return proxy.str();
2192 }
2193 };
2194
2195 class VectorStatNode : public Node
2196 {
2197 private:
2198 const VectorInfo *data;
2199
2200 public:
2201 VectorStatNode(const VectorInfo *d) : data(d) { }
2202 const VResult &result() const { return data->result(); }
2203 Result total() const { return data->total(); };
2204
2205 size_type size() const { return data->size(); }
2206
2207 std::string str() const { return data->name; }
2208 };
2209
2210 template <class T>
2211 class ConstNode : public Node
2212 {
2213 private:
2214 VResult vresult;
2215
2216 public:
2217 ConstNode(T s) : vresult(1, (Result)s) {}
2218 const VResult &result() const { return vresult; }
2219 Result total() const { return vresult[0]; };
2220 size_type size() const { return 1; }
2221 std::string str() const { return std::to_string(vresult[0]); }
2222 };
2223
2224 template <class T>
2225 class ConstVectorNode : public Node
2226 {
2227 private:
2228 VResult vresult;
2229
2230 public:
2231 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {}
2232 const VResult &result() const { return vresult; }
2233
2234 Result
2235 total() const
2236 {
2237 size_type size = this->size();
2238 Result tmp = 0;
2239 for (off_type i = 0; i < size; i++)
2240 tmp += vresult[i];
2241 return tmp;
2242 }
2243
2244 size_type size() const { return vresult.size(); }
2245 std::string
2246 str() const
2247 {
2248 size_type size = this->size();
2249 std::string tmp = "(";
2250 for (off_type i = 0; i < size; i++)
2251 tmp += csprintf("%s ", std::to_string(vresult[i]));
2252 tmp += ")";
2253 return tmp;
2254 }
2255 };
2256
2257 template <class Op>
2258 struct OpString;
2259
2260 template<>
2261 struct OpString<std::plus<Result> >
2262 {
2263 static std::string str() { return "+"; }
2264 };
2265
2266 template<>
2267 struct OpString<std::minus<Result> >
2268 {
2269 static std::string str() { return "-"; }
2270 };
2271
2272 template<>
2273 struct OpString<std::multiplies<Result> >
2274 {
2275 static std::string str() { return "*"; }
2276 };
2277
2278 template<>
2279 struct OpString<std::divides<Result> >
2280 {
2281 static std::string str() { return "/"; }
2282 };
2283
2284 template<>
2285 struct OpString<std::modulus<Result> >
2286 {
2287 static std::string str() { return "%"; }
2288 };
2289
2290 template<>
2291 struct OpString<std::negate<Result> >
2292 {
2293 static std::string str() { return "-"; }
2294 };
2295
2296 template <class Op>
2297 class UnaryNode : public Node
2298 {
2299 public:
2300 NodePtr l;
2301 mutable VResult vresult;
2302
2303 public:
2304 UnaryNode(NodePtr &p) : l(p) {}
2305
2306 const VResult &
2307 result() const
2308 {
2309 const VResult &lvec = l->result();
2310 size_type size = lvec.size();
2311
2312 assert(size > 0);
2313
2314 vresult.resize(size);
2315 Op op;
2316 for (off_type i = 0; i < size; ++i)
2317 vresult[i] = op(lvec[i]);
2318
2319 return vresult;
2320 }
2321
2322 Result
2323 total() const
2324 {
2325 const VResult &vec = this->result();
2326 Result total = 0.0;
2327 for (off_type i = 0; i < size(); i++)
2328 total += vec[i];
2329 return total;
2330 }
2331
2332 size_type size() const { return l->size(); }
2333
2334 std::string
2335 str() const
2336 {
2337 return OpString<Op>::str() + l->str();
2338 }
2339 };
2340
2341 template <class Op>
2342 class BinaryNode : public Node
2343 {
2344 public:
2345 NodePtr l;
2346 NodePtr r;
2347 mutable VResult vresult;
2348
2349 public:
2350 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2351
2352 const VResult &
2353 result() const override
2354 {
2355 Op op;
2356 const VResult &lvec = l->result();
2357 const VResult &rvec = r->result();
2358
2359 assert(lvec.size() > 0 && rvec.size() > 0);
2360
2361 if (lvec.size() == 1 && rvec.size() == 1) {
2362 vresult.resize(1);
2363 vresult[0] = op(lvec[0], rvec[0]);
2364 } else if (lvec.size() == 1) {
2365 size_type size = rvec.size();
2366 vresult.resize(size);
2367 for (off_type i = 0; i < size; ++i)
2368 vresult[i] = op(lvec[0], rvec[i]);
2369 } else if (rvec.size() == 1) {
2370 size_type size = lvec.size();
2371 vresult.resize(size);
2372 for (off_type i = 0; i < size; ++i)
2373 vresult[i] = op(lvec[i], rvec[0]);
2374 } else if (rvec.size() == lvec.size()) {
2375 size_type size = rvec.size();
2376 vresult.resize(size);
2377 for (off_type i = 0; i < size; ++i)
2378 vresult[i] = op(lvec[i], rvec[i]);
2379 }
2380
2381 return vresult;
2382 }
2383
2384 Result
2385 total() const override
2386 {
2387 const VResult &vec = this->result();
2388 const VResult &lvec = l->result();
2389 const VResult &rvec = r->result();
2390 Result total = 0.0;
2391 Result lsum = 0.0;
2392 Result rsum = 0.0;
2393 Op op;
2394
2395 assert(lvec.size() > 0 && rvec.size() > 0);
2396 assert(lvec.size() == rvec.size() ||
2397 lvec.size() == 1 || rvec.size() == 1);
2398
2399 /** If vectors are the same divide their sums (x0+x1)/(y0+y1) */
2400 if (lvec.size() == rvec.size() && lvec.size() > 1) {
2401 for (off_type i = 0; i < size(); ++i) {
2402 lsum += lvec[i];
2403 rsum += rvec[i];
2404 }
2405 return op(lsum, rsum);
2406 }
2407
2408 /** Otherwise divide each item by the divisor */
2409 for (off_type i = 0; i < size(); ++i) {
2410 total += vec[i];
2411 }
2412
2413 return total;
2414 }
2415
2416 size_type
2417 size() const override
2418 {
2419 size_type ls = l->size();
2420 size_type rs = r->size();
2421 if (ls == 1) {
2422 return rs;
2423 } else if (rs == 1) {
2424 return ls;
2425 } else {
2426 assert(ls == rs && "Node vector sizes are not equal");
2427 return ls;
2428 }
2429 }
2430
2431 std::string
2432 str() const override
2433 {
2434 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2435 }
2436 };
2437
2438 template <class Op>
2439 class SumNode : public Node
2440 {
2441 public:
2442 NodePtr l;
2443 mutable VResult vresult;
2444
2445 public:
2446 SumNode(NodePtr &p) : l(p), vresult(1) {}
2447
2448 const VResult &
2449 result() const
2450 {
2451 const VResult &lvec = l->result();
2452 size_type size = lvec.size();
2453 assert(size > 0);
2454
2455 vresult[0] = 0.0;
2456
2457 Op op;
2458 for (off_type i = 0; i < size; ++i)
2459 vresult[0] = op(vresult[0], lvec[i]);
2460
2461 return vresult;
2462 }
2463
2464 Result
2465 total() const
2466 {
2467 const VResult &lvec = l->result();
2468 size_type size = lvec.size();
2469 assert(size > 0);
2470
2471 Result result = 0.0;
2472
2473 Op op;
2474 for (off_type i = 0; i < size; ++i)
2475 result = op(result, lvec[i]);
2476
2477 return result;
2478 }
2479
2480 size_type size() const { return 1; }
2481
2482 std::string
2483 str() const
2484 {
2485 return csprintf("total(%s)", l->str());
2486 }
2487 };
2488
2489
2490 //////////////////////////////////////////////////////////////////////
2491 //
2492 // Visible Statistics Types
2493 //
2494 //////////////////////////////////////////////////////////////////////
2495 /**
2496 * @defgroup VisibleStats "Statistic Types"
2497 * These are the statistics that are used in the simulator.
2498 * @{
2499 */
2500
2501 /**
2502 * This is a simple scalar statistic, like a counter.
2503 * @sa Stat, ScalarBase, StatStor
2504 */
2505 class Scalar : public ScalarBase<Scalar, StatStor>
2506 {
2507 public:
2508 using ScalarBase<Scalar, StatStor>::operator=;
2509
2510 Scalar(Group *parent = nullptr, const char *name = nullptr,
2511 const char *desc = nullptr)
2512 : ScalarBase<Scalar, StatStor>(parent, name, desc)
2513 {
2514 }
2515 };
2516
2517 /**
2518 * A stat that calculates the per tick average of a value.
2519 * @sa Stat, ScalarBase, AvgStor
2520 */
2521 class Average : public ScalarBase<Average, AvgStor>
2522 {
2523 public:
2524 using ScalarBase<Average, AvgStor>::operator=;
2525
2526 Average(Group *parent = nullptr, const char *name = nullptr,
2527 const char *desc = nullptr)
2528 : ScalarBase<Average, AvgStor>(parent, name, desc)
2529 {
2530 }
2531 };
2532
2533 class Value : public ValueBase<Value>
2534 {
2535 public:
2536 Value(Group *parent = nullptr, const char *name = nullptr,
2537 const char *desc = nullptr)
2538 : ValueBase<Value>(parent, name, desc)
2539 {
2540 }
2541 };
2542
2543 /**
2544 * A vector of scalar stats.
2545 * @sa Stat, VectorBase, StatStor
2546 */
2547 class Vector : public VectorBase<Vector, StatStor>
2548 {
2549 public:
2550 Vector(Group *parent = nullptr, const char *name = nullptr,
2551 const char *desc = nullptr)
2552 : VectorBase<Vector, StatStor>(parent, name, desc)
2553 {
2554 }
2555 };
2556
2557 /**
2558 * A vector of Average stats.
2559 * @sa Stat, VectorBase, AvgStor
2560 */
2561 class AverageVector : public VectorBase<AverageVector, AvgStor>
2562 {
2563 public:
2564 AverageVector(Group *parent = nullptr, const char *name = nullptr,
2565 const char *desc = nullptr)
2566 : VectorBase<AverageVector, AvgStor>(parent, name, desc)
2567 {
2568 }
2569 };
2570
2571 /**
2572 * A 2-Dimensional vecto of scalar stats.
2573 * @sa Stat, Vector2dBase, StatStor
2574 */
2575 class Vector2d : public Vector2dBase<Vector2d, StatStor>
2576 {
2577 public:
2578 Vector2d(Group *parent = nullptr, const char *name = nullptr,
2579 const char *desc = nullptr)
2580 : Vector2dBase<Vector2d, StatStor>(parent, name, desc)
2581 {
2582 }
2583 };
2584
2585 /**
2586 * A simple distribution stat.
2587 * @sa Stat, DistBase, DistStor
2588 */
2589 class Distribution : public DistBase<Distribution, DistStor>
2590 {
2591 public:
2592 Distribution(Group *parent = nullptr, const char *name = nullptr,
2593 const char *desc = nullptr)
2594 : DistBase<Distribution, DistStor>(parent, name, desc)
2595 {
2596 }
2597
2598 /**
2599 * Set the parameters of this distribution. @sa DistStor::Params
2600 * @param min The minimum value of the distribution.
2601 * @param max The maximum value of the distribution.
2602 * @param bkt The number of values in each bucket.
2603 * @return A reference to this distribution.
2604 */
2605 Distribution &
2606 init(Counter min, Counter max, Counter bkt)
2607 {
2608 DistStor::Params *params = new DistStor::Params;
2609 params->min = min;
2610 params->max = max;
2611 params->bucket_size = bkt;
2612 // Division by zero is especially serious in an Aarch64 host,
2613 // where it gets rounded to allocate 32GiB RAM.
2614 assert(bkt > 0);
2615 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2616 this->setParams(params);
2617 this->doInit();
2618 return this->self();
2619 }
2620 };
2621
2622 /**
2623 * A simple histogram stat.
2624 * @sa Stat, DistBase, HistStor
2625 */
2626 class Histogram : public DistBase<Histogram, HistStor>
2627 {
2628 public:
2629 Histogram(Group *parent = nullptr, const char *name = nullptr,
2630 const char *desc = nullptr)
2631 : DistBase<Histogram, HistStor>(parent, name, desc)
2632 {
2633 }
2634
2635 /**
2636 * Set the parameters of this histogram. @sa HistStor::Params
2637 * @param size The number of buckets in the histogram
2638 * @return A reference to this histogram.
2639 */
2640 Histogram &
2641 init(size_type size)
2642 {
2643 HistStor::Params *params = new HistStor::Params;
2644 params->buckets = size;
2645 this->setParams(params);
2646 this->doInit();
2647 return this->self();
2648 }
2649 };
2650
2651 /**
2652 * Calculates the mean and variance of all the samples.
2653 * @sa DistBase, SampleStor
2654 */
2655 class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2656 {
2657 public:
2658 /**
2659 * Construct and initialize this distribution.
2660 */
2661 StandardDeviation(Group *parent = nullptr, const char *name = nullptr,
2662 const char *desc = nullptr)
2663 : DistBase<StandardDeviation, SampleStor>(parent, name, desc)
2664 {
2665 SampleStor::Params *params = new SampleStor::Params;
2666 this->doInit();
2667 this->setParams(params);
2668 }
2669 };
2670
2671 /**
2672 * Calculates the per tick mean and variance of the samples.
2673 * @sa DistBase, AvgSampleStor
2674 */
2675 class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor>
2676 {
2677 public:
2678 /**
2679 * Construct and initialize this distribution.
2680 */
2681 AverageDeviation(Group *parent = nullptr, const char *name = nullptr,
2682 const char *desc = nullptr)
2683 : DistBase<AverageDeviation, AvgSampleStor>(parent, name, desc)
2684 {
2685 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2686 this->doInit();
2687 this->setParams(params);
2688 }
2689 };
2690
2691 /**
2692 * A vector of distributions.
2693 * @sa VectorDistBase, DistStor
2694 */
2695 class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor>
2696 {
2697 public:
2698 VectorDistribution(Group *parent = nullptr, const char *name = nullptr,
2699 const char *desc = nullptr)
2700 : VectorDistBase<VectorDistribution, DistStor>(parent, name, desc)
2701 {
2702 }
2703
2704 /**
2705 * Initialize storage and parameters for this distribution.
2706 * @param size The size of the vector (the number of distributions).
2707 * @param min The minimum value of the distribution.
2708 * @param max The maximum value of the distribution.
2709 * @param bkt The number of values in each bucket.
2710 * @return A reference to this distribution.
2711 */
2712 VectorDistribution &
2713 init(size_type size, Counter min, Counter max, Counter bkt)
2714 {
2715 DistStor::Params *params = new DistStor::Params;
2716 params->min = min;
2717 params->max = max;
2718 params->bucket_size = bkt;
2719 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2720 this->setParams(params);
2721 this->doInit(size);
2722 return this->self();
2723 }
2724 };
2725
2726 /**
2727 * This is a vector of StandardDeviation stats.
2728 * @sa VectorDistBase, SampleStor
2729 */
2730 class VectorStandardDeviation
2731 : public VectorDistBase<VectorStandardDeviation, SampleStor>
2732 {
2733 public:
2734 VectorStandardDeviation(Group *parent = nullptr, const char *name = nullptr,
2735 const char *desc = nullptr)
2736 : VectorDistBase<VectorStandardDeviation, SampleStor>(parent, name,
2737 desc)
2738 {
2739 }
2740
2741 /**
2742 * Initialize storage for this distribution.
2743 * @param size The size of the vector.
2744 * @return A reference to this distribution.
2745 */
2746 VectorStandardDeviation &
2747 init(size_type size)
2748 {
2749 SampleStor::Params *params = new SampleStor::Params;
2750 this->doInit(size);
2751 this->setParams(params);
2752 return this->self();
2753 }
2754 };
2755
2756 /**
2757 * This is a vector of AverageDeviation stats.
2758 * @sa VectorDistBase, AvgSampleStor
2759 */
2760 class VectorAverageDeviation
2761 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor>
2762 {
2763 public:
2764 VectorAverageDeviation(Group *parent = nullptr, const char *name = nullptr,
2765 const char *desc = nullptr)
2766 : VectorDistBase<VectorAverageDeviation, AvgSampleStor>(parent, name,
2767 desc)
2768 {
2769 }
2770
2771 /**
2772 * Initialize storage for this distribution.
2773 * @param size The size of the vector.
2774 * @return A reference to this distribution.
2775 */
2776 VectorAverageDeviation &
2777 init(size_type size)
2778 {
2779 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2780 this->doInit(size);
2781 this->setParams(params);
2782 return this->self();
2783 }
2784 };
2785
2786 template <class Stat>
2787 class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo>
2788 {
2789 protected:
2790 mutable VResult vec;
2791 mutable VCounter cvec;
2792
2793 public:
2794 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {}
2795
2796 size_type size() const { return this->s.size(); }
2797
2798 const VResult &
2799 result() const
2800 {
2801 this->s.result(vec);
2802 return vec;
2803 }
2804 Result total() const { return this->s.total(); }
2805 VCounter &value() const { return cvec; }
2806
2807 std::string str() const { return this->s.str(); }
2808 };
2809
2810 template <class Stat>
2811 class SparseHistInfoProxy : public InfoProxy<Stat, SparseHistInfo>
2812 {
2813 public:
2814 SparseHistInfoProxy(Stat &stat) : InfoProxy<Stat, SparseHistInfo>(stat) {}
2815 };
2816
2817 /**
2818 * Implementation of a sparse histogram stat. The storage class is
2819 * determined by the Storage template.
2820 */
2821 template <class Derived, class Stor>
2822 class SparseHistBase : public DataWrap<Derived, SparseHistInfoProxy>
2823 {
2824 public:
2825 typedef SparseHistInfoProxy<Derived> Info;
2826 typedef Stor Storage;
2827 typedef typename Stor::Params Params;
2828
2829 protected:
2830 /** The storage for this stat. */
2831 char storage[sizeof(Storage)];
2832
2833 protected:
2834 /**
2835 * Retrieve the storage.
2836 * @return The storage object for this stat.
2837 */
2838 Storage *
2839 data()
2840 {
2841 return reinterpret_cast<Storage *>(storage);
2842 }
2843
2844 /**
2845 * Retrieve a const pointer to the storage.
2846 * @return A const pointer to the storage object for this stat.
2847 */
2848 const Storage *
2849 data() const
2850 {
2851 return reinterpret_cast<const Storage *>(storage);
2852 }
2853
2854 void
2855 doInit()
2856 {
2857 new (storage) Storage(this->info());
2858 this->setInit();
2859 }
2860
2861 public:
2862 SparseHistBase(Group *parent, const char *name, const char *desc)
2863 : DataWrap<Derived, SparseHistInfoProxy>(parent, name, desc)
2864 {
2865 }
2866
2867 /**
2868 * Add a value to the distribtion n times. Calls sample on the storage
2869 * class.
2870 * @param v The value to add.
2871 * @param n The number of times to add it, defaults to 1.
2872 */
2873 template <typename U>
2874 void sample(const U &v, int n = 1) { data()->sample(v, n); }
2875
2876 /**
2877 * Return the number of entries in this stat.
2878 * @return The number of entries.
2879 */
2880 size_type size() const { return data()->size(); }
2881 /**
2882 * Return true if no samples have been added.
2883 * @return True if there haven't been any samples.
2884 */
2885 bool zero() const { return data()->zero(); }
2886
2887 void
2888 prepare()
2889 {
2890 Info *info = this->info();
2891 data()->prepare(info, info->data);
2892 }
2893
2894 /**
2895 * Reset stat value to default
2896 */
2897 void
2898 reset()
2899 {
2900 data()->reset(this->info());
2901 }
2902 };
2903
2904 /**
2905 * Templatized storage and interface for a sparse histogram stat.
2906 */
2907 class SparseHistStor
2908 {
2909 public:
2910 /** The parameters for a sparse histogram stat. */
2911 struct Params : public DistParams
2912 {
2913 Params() : DistParams(Hist) {}
2914 };
2915
2916 private:
2917 /** Counter for number of samples */
2918 Counter samples;
2919 /** Counter for each bucket. */
2920 MCounter cmap;
2921
2922 public:
2923 SparseHistStor(Info *info)
2924 {
2925 reset(info);
2926 }
2927
2928 /**
2929 * Add a value to the distribution for the given number of times.
2930 * @param val The value to add.
2931 * @param number The number of times to add the value.
2932 */
2933 void
2934 sample(Counter val, int number)
2935 {
2936 cmap[val] += number;
2937 samples += number;
2938 }
2939
2940 /**
2941 * Return the number of buckets in this distribution.
2942 * @return the number of buckets.
2943 */
2944 size_type size() const { return cmap.size(); }
2945
2946 /**
2947 * Returns true if any calls to sample have been made.
2948 * @return True if any values have been sampled.
2949 */
2950 bool
2951 zero() const
2952 {
2953 return samples == Counter();
2954 }
2955
2956 void
2957 prepare(Info *info, SparseHistData &data)
2958 {
2959 MCounter::iterator it;
2960 data.cmap.clear();
2961 for (it = cmap.begin(); it != cmap.end(); it++) {
2962 data.cmap[(*it).first] = (*it).second;
2963 }
2964
2965 data.samples = samples;
2966 }
2967
2968 /**
2969 * Reset stat value to default
2970 */
2971 void
2972 reset(Info *info)
2973 {
2974 cmap.clear();
2975 samples = 0;
2976 }
2977 };
2978
2979 class SparseHistogram : public SparseHistBase<SparseHistogram, SparseHistStor>
2980 {
2981 public:
2982 SparseHistogram(Group *parent = nullptr, const char *name = nullptr,
2983 const char *desc = nullptr)
2984 : SparseHistBase<SparseHistogram, SparseHistStor>(parent, name, desc)
2985 {
2986 }
2987
2988 /**
2989 * Set the parameters of this histogram. @sa HistStor::Params
2990 * @param size The number of buckets in the histogram
2991 * @return A reference to this histogram.
2992 */
2993 SparseHistogram &
2994 init(size_type size)
2995 {
2996 SparseHistStor::Params *params = new SparseHistStor::Params;
2997 this->setParams(params);
2998 this->doInit();
2999 return this->self();
3000 }
3001 };
3002
3003 class Temp;
3004 /**
3005 * A formula for statistics that is calculated when printed. A formula is
3006 * stored as a tree of Nodes that represent the equation to calculate.
3007 * @sa Stat, ScalarStat, VectorStat, Node, Temp
3008 */
3009 class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
3010 {
3011 protected:
3012 /** The root of the tree which represents the Formula */
3013 NodePtr root;
3014 friend class Temp;
3015
3016 public:
3017 /**
3018 * Create and initialize thie formula, and register it with the database.
3019 */
3020 Formula(Group *parent = nullptr, const char *name = nullptr,
3021 const char *desc = nullptr);
3022
3023 Formula(Group *parent, const char *name, const char *desc,
3024 const Temp &r);
3025
3026 /**
3027 * Set an unitialized Formula to the given root.
3028 * @param r The root of the expression tree.
3029 * @return a reference to this formula.
3030 */
3031 const Formula &operator=(const Temp &r);
3032
3033 template<typename T>
3034 const Formula &operator=(const T &v)
3035 {
3036 *this = Temp(v);
3037 return *this;
3038 }
3039
3040 /**
3041 * Add the given tree to the existing one.
3042 * @param r The root of the expression tree.
3043 * @return a reference to this formula.
3044 */
3045 const Formula &operator+=(Temp r);
3046
3047 /**
3048 * Divide the existing tree by the given one.
3049 * @param r The root of the expression tree.
3050 * @return a reference to this formula.
3051 */
3052 const Formula &operator/=(Temp r);
3053
3054 /**
3055 * Return the result of the Fomula in a vector. If there were no Vector
3056 * components to the Formula, then the vector is size 1. If there were,
3057 * like x/y with x being a vector of size 3, then the result returned will
3058 * be x[0]/y, x[1]/y, x[2]/y, respectively.
3059 * @return The result vector.
3060 */
3061 void result(VResult &vec) const;
3062
3063 /**
3064 * Return the total Formula result. If there is a Vector
3065 * component to this Formula, then this is the result of the
3066 * Formula if the formula is applied after summing all the
3067 * components of the Vector. For example, if Formula is x/y where
3068 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
3069 * there is no Vector component, total() returns the same value as
3070 * the first entry in the VResult val() returns.
3071 * @return The total of the result vector.
3072 */
3073 Result total() const;
3074
3075 /**
3076 * Return the number of elements in the tree.
3077 */
3078 size_type size() const;
3079
3080 void prepare() { }
3081
3082 /**
3083 * Formulas don't need to be reset
3084 */
3085 void reset();
3086
3087 /**
3088 *
3089 */
3090 bool zero() const;
3091
3092 std::string str() const;
3093 };
3094
3095 class FormulaNode : public Node
3096 {
3097 private:
3098 const Formula &formula;
3099 mutable VResult vec;
3100
3101 public:
3102 FormulaNode(const Formula &f) : formula(f) {}
3103
3104 size_type size() const { return formula.size(); }
3105 const VResult &result() const { formula.result(vec); return vec; }
3106 Result total() const { return formula.total(); }
3107
3108 std::string str() const { return formula.str(); }
3109 };
3110
3111 /**
3112 * Helper class to construct formula node trees.
3113 */
3114 class Temp
3115 {
3116 protected:
3117 /**
3118 * Pointer to a Node object.
3119 */
3120 NodePtr node;
3121
3122 public:
3123 /**
3124 * Copy the given pointer to this class.
3125 * @param n A pointer to a Node object to copy.
3126 */
3127 Temp(const NodePtr &n) : node(n) { }
3128
3129 Temp(NodePtr &&n) : node(std::move(n)) { }
3130
3131 /**
3132 * Return the node pointer.
3133 * @return the node pointer.
3134 */
3135 operator NodePtr&() { return node; }
3136
3137 /**
3138 * Makde gcc < 4.6.3 happy and explicitly get the underlying node.
3139 */
3140 NodePtr getNodePtr() const { return node; }
3141
3142 public:
3143 /**
3144 * Create a new ScalarStatNode.
3145 * @param s The ScalarStat to place in a node.
3146 */
3147 Temp(const Scalar &s)
3148 : node(new ScalarStatNode(s.info()))
3149 { }
3150
3151 /**
3152 * Create a new ScalarStatNode.
3153 * @param s The ScalarStat to place in a node.
3154 */
3155 Temp(const Value &s)
3156 : node(new ScalarStatNode(s.info()))
3157 { }
3158
3159 /**
3160 * Create a new ScalarStatNode.
3161 * @param s The ScalarStat to place in a node.
3162 */
3163 Temp(const Average &s)
3164 : node(new ScalarStatNode(s.info()))
3165 { }
3166
3167 /**
3168 * Create a new VectorStatNode.
3169 * @param s The VectorStat to place in a node.
3170 */
3171 Temp(const Vector &s)
3172 : node(new VectorStatNode(s.info()))
3173 { }
3174
3175 Temp(const AverageVector &s)
3176 : node(new VectorStatNode(s.info()))
3177 { }
3178
3179 /**
3180 *
3181 */
3182 Temp(const Formula &f)
3183 : node(new FormulaNode(f))
3184 { }
3185
3186 /**
3187 * Create a new ScalarProxyNode.
3188 * @param p The ScalarProxy to place in a node.
3189 */
3190 template <class Stat>
3191 Temp(const ScalarProxy<Stat> &p)
3192 : node(new ScalarProxyNode<Stat>(p))
3193 { }
3194
3195 /**
3196 * Create a ConstNode
3197 * @param value The value of the const node.
3198 */
3199 Temp(signed char value)
3200 : node(new ConstNode<signed char>(value))
3201 { }
3202
3203 /**
3204 * Create a ConstNode
3205 * @param value The value of the const node.
3206 */
3207 Temp(unsigned char value)
3208 : node(new ConstNode<unsigned char>(value))
3209 { }
3210
3211 /**
3212 * Create a ConstNode
3213 * @param value The value of the const node.
3214 */
3215 Temp(signed short value)
3216 : node(new ConstNode<signed short>(value))
3217 { }
3218
3219 /**
3220 * Create a ConstNode
3221 * @param value The value of the const node.
3222 */
3223 Temp(unsigned short value)
3224 : node(new ConstNode<unsigned short>(value))
3225 { }
3226
3227 /**
3228 * Create a ConstNode
3229 * @param value The value of the const node.
3230 */
3231 Temp(signed int value)
3232 : node(new ConstNode<signed int>(value))
3233 { }
3234
3235 /**
3236 * Create a ConstNode
3237 * @param value The value of the const node.
3238 */
3239 Temp(unsigned int value)
3240 : node(new ConstNode<unsigned int>(value))
3241 { }
3242
3243 /**
3244 * Create a ConstNode
3245 * @param value The value of the const node.
3246 */
3247 Temp(signed long value)
3248 : node(new ConstNode<signed long>(value))
3249 { }
3250
3251 /**
3252 * Create a ConstNode
3253 * @param value The value of the const node.
3254 */
3255 Temp(unsigned long value)
3256 : node(new ConstNode<unsigned long>(value))
3257 { }
3258
3259 /**
3260 * Create a ConstNode
3261 * @param value The value of the const node.
3262 */
3263 Temp(signed long long value)
3264 : node(new ConstNode<signed long long>(value))
3265 { }
3266
3267 /**
3268 * Create a ConstNode
3269 * @param value The value of the const node.
3270 */
3271 Temp(unsigned long long value)
3272 : node(new ConstNode<unsigned long long>(value))
3273 { }
3274
3275 /**
3276 * Create a ConstNode
3277 * @param value The value of the const node.
3278 */
3279 Temp(float value)
3280 : node(new ConstNode<float>(value))
3281 { }
3282
3283 /**
3284 * Create a ConstNode
3285 * @param value The value of the const node.
3286 */
3287 Temp(double value)
3288 : node(new ConstNode<double>(value))
3289 { }
3290 };
3291
3292
3293 /**
3294 * @}
3295 */
3296
3297 inline Temp
3298 operator+(Temp l, Temp r)
3299 {
3300 return Temp(std::make_shared<BinaryNode<std::plus<Result> > >(l, r));
3301 }
3302
3303 inline Temp
3304 operator-(Temp l, Temp r)
3305 {
3306 return Temp(std::make_shared<BinaryNode<std::minus<Result> > >(l, r));
3307 }
3308
3309 inline Temp
3310 operator*(Temp l, Temp r)
3311 {
3312 return Temp(std::make_shared<BinaryNode<std::multiplies<Result> > >(l, r));
3313 }
3314
3315 inline Temp
3316 operator/(Temp l, Temp r)
3317 {
3318 return Temp(std::make_shared<BinaryNode<std::divides<Result> > >(l, r));
3319 }
3320
3321 inline Temp
3322 operator-(Temp l)
3323 {
3324 return Temp(std::make_shared<UnaryNode<std::negate<Result> > >(l));
3325 }
3326
3327 template <typename T>
3328 inline Temp
3329 constant(T val)
3330 {
3331 return Temp(std::make_shared<ConstNode<T> >(val));
3332 }
3333
3334 template <typename T>
3335 inline Temp
3336 constantVector(T val)
3337 {
3338 return Temp(std::make_shared<ConstVectorNode<T> >(val));
3339 }
3340
3341 inline Temp
3342 sum(Temp val)
3343 {
3344 return Temp(std::make_shared<SumNode<std::plus<Result> > >(val));
3345 }
3346
3347 /** Dump all statistics data to the registered outputs */
3348 void dump();
3349 void reset();
3350 void enable();
3351 bool enabled();
3352 const Info* resolve(const std::string &name);
3353
3354 /**
3355 * Register reset and dump handlers. These are the functions which
3356 * will actually perform the whole statistics reset/dump actions
3357 * including processing the reset/dump callbacks
3358 */
3359 typedef void (*Handler)();
3360
3361 void registerHandlers(Handler reset_handler, Handler dump_handler);
3362
3363 /**
3364 * Register a callback that should be called whenever statistics are
3365 * reset
3366 */
3367 void registerResetCallback(Callback *cb);
3368
3369 /**
3370 * Register a callback that should be called whenever statistics are
3371 * about to be dumped
3372 */
3373 void registerDumpCallback(Callback *cb);
3374
3375 /**
3376 * Process all the callbacks in the reset callbacks queue
3377 */
3378 void processResetQueue();
3379
3380 /**
3381 * Process all the callbacks in the dump callbacks queue
3382 */
3383 void processDumpQueue();
3384
3385 std::list<Info *> &statsList();
3386
3387 typedef std::map<const void *, Info *> MapType;
3388 MapType &statsMap();
3389
3390 typedef std::map<std::string, Info *> NameMapType;
3391 NameMapType &nameMap();
3392
3393 bool validateStatName(const std::string &name);
3394
3395 } // namespace Stats
3396
3397 void debugDumpStats();
3398
3399 #endif // __BASE_STATISTICS_HH__