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