statistics.hh revision 8514:57c96df312a1
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 + index < 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 squares. */ 1481 Counter squares; 1482 /** The number of samples. */ 1483 Counter samples; 1484 /** Counter for each bucket. */ 1485 VCounter cvec; 1486 1487 public: 1488 HistStor(Info *info) 1489 : cvec(safe_cast<const Params *>(info->storageParams)->buckets) 1490 { 1491 reset(info); 1492 } 1493 1494 void grow_up(); 1495 void grow_out(); 1496 void grow_convert(); 1497 1498 /** 1499 * Add a value to the distribution for the given number of times. 1500 * @param val The value to add. 1501 * @param number The number of times to add the value. 1502 */ 1503 void 1504 sample(Counter val, int number) 1505 { 1506 assert(min_bucket < max_bucket); 1507 if (val < min_bucket) { 1508 if (min_bucket == 0) 1509 grow_convert(); 1510 1511 while (val < min_bucket) 1512 grow_out(); 1513 } else if (val >= max_bucket + bucket_size) { 1514 if (min_bucket == 0) { 1515 while (val >= max_bucket + bucket_size) 1516 grow_up(); 1517 } else { 1518 while (val >= max_bucket + bucket_size) 1519 grow_out(); 1520 } 1521 } 1522 1523 size_type index = 1524 (int64_t)std::floor((val - min_bucket) / bucket_size); 1525 1526 assert(index >= 0 && index < size()); 1527 cvec[index] += number; 1528 1529 sum += val * number; 1530 squares += val * val * number; 1531 samples += number; 1532 } 1533 1534 /** 1535 * Return the number of buckets in this distribution. 1536 * @return the number of buckets. 1537 */ 1538 size_type size() const { return cvec.size(); } 1539 1540 /** 1541 * Returns true if any calls to sample have been made. 1542 * @return True if any values have been sampled. 1543 */ 1544 bool 1545 zero() const 1546 { 1547 return samples == Counter(); 1548 } 1549 1550 void 1551 prepare(Info *info, DistData &data) 1552 { 1553 const Params *params = safe_cast<const Params *>(info->storageParams); 1554 1555 assert(params->type == Hist); 1556 data.type = params->type; 1557 data.min = min_bucket; 1558 data.max = max_bucket + bucket_size - 1; 1559 data.bucket_size = bucket_size; 1560 1561 data.min_val = min_bucket; 1562 data.max_val = max_bucket; 1563 1564 int buckets = params->buckets; 1565 data.cvec.resize(buckets); 1566 for (off_type i = 0; i < buckets; ++i) 1567 data.cvec[i] = cvec[i]; 1568 1569 data.sum = sum; 1570 data.squares = squares; 1571 data.samples = samples; 1572 } 1573 1574 /** 1575 * Reset stat value to default 1576 */ 1577 void 1578 reset(Info *info) 1579 { 1580 const Params *params = safe_cast<const Params *>(info->storageParams); 1581 min_bucket = 0; 1582 max_bucket = params->buckets - 1; 1583 bucket_size = 1; 1584 1585 size_type size = cvec.size(); 1586 for (off_type i = 0; i < size; ++i) 1587 cvec[i] = Counter(); 1588 1589 sum = Counter(); 1590 squares = Counter(); 1591 samples = Counter(); 1592 } 1593}; 1594 1595/** 1596 * Templatized storage and interface for a distribution that calculates mean 1597 * and variance. 1598 */ 1599class SampleStor 1600{ 1601 public: 1602 struct Params : public DistParams 1603 { 1604 Params() : DistParams(Deviation) {} 1605 }; 1606 1607 private: 1608 /** The current sum. */ 1609 Counter sum; 1610 /** The sum of squares. */ 1611 Counter squares; 1612 /** The number of samples. */ 1613 Counter samples; 1614 1615 public: 1616 /** 1617 * Create and initialize this storage. 1618 */ 1619 SampleStor(Info *info) 1620 : sum(Counter()), squares(Counter()), samples(Counter()) 1621 { } 1622 1623 /** 1624 * Add a value the given number of times to this running average. 1625 * Update the running sum and sum of squares, increment the number of 1626 * values seen by the given number. 1627 * @param val The value to add. 1628 * @param number The number of times to add the value. 1629 */ 1630 void 1631 sample(Counter val, int number) 1632 { 1633 Counter value = val * number; 1634 sum += value; 1635 squares += value * value; 1636 samples += number; 1637 } 1638 1639 /** 1640 * Return the number of entries in this stat, 1 1641 * @return 1. 1642 */ 1643 size_type size() const { return 1; } 1644 1645 /** 1646 * Return true if no samples have been added. 1647 * @return True if no samples have been added. 1648 */ 1649 bool zero() const { return samples == Counter(); } 1650 1651 void 1652 prepare(Info *info, DistData &data) 1653 { 1654 const Params *params = safe_cast<const Params *>(info->storageParams); 1655 1656 assert(params->type == Deviation); 1657 data.type = params->type; 1658 data.sum = sum; 1659 data.squares = squares; 1660 data.samples = samples; 1661 } 1662 1663 /** 1664 * Reset stat value to default 1665 */ 1666 void 1667 reset(Info *info) 1668 { 1669 sum = Counter(); 1670 squares = Counter(); 1671 samples = Counter(); 1672 } 1673}; 1674 1675/** 1676 * Templatized storage for distribution that calculates per tick mean and 1677 * variance. 1678 */ 1679class AvgSampleStor 1680{ 1681 public: 1682 struct Params : public DistParams 1683 { 1684 Params() : DistParams(Deviation) {} 1685 }; 1686 1687 private: 1688 /** Current total. */ 1689 Counter sum; 1690 /** Current sum of squares. */ 1691 Counter squares; 1692 1693 public: 1694 /** 1695 * Create and initialize this storage. 1696 */ 1697 AvgSampleStor(Info *info) 1698 : sum(Counter()), squares(Counter()) 1699 {} 1700 1701 /** 1702 * Add a value to the distribution for the given number of times. 1703 * Update the running sum and sum of squares. 1704 * @param val The value to add. 1705 * @param number The number of times to add the value. 1706 */ 1707 void 1708 sample(Counter val, int number) 1709 { 1710 Counter value = val * number; 1711 sum += value; 1712 squares += value * value; 1713 } 1714 1715 /** 1716 * Return the number of entries, in this case 1. 1717 * @return 1. 1718 */ 1719 size_type size() const { return 1; } 1720 1721 /** 1722 * Return true if no samples have been added. 1723 * @return True if the sum is zero. 1724 */ 1725 bool zero() const { return sum == Counter(); } 1726 1727 void 1728 prepare(Info *info, DistData &data) 1729 { 1730 const Params *params = safe_cast<const Params *>(info->storageParams); 1731 1732 assert(params->type == Deviation); 1733 data.type = params->type; 1734 data.sum = sum; 1735 data.squares = squares; 1736 data.samples = curTick(); 1737 } 1738 1739 /** 1740 * Reset stat value to default 1741 */ 1742 void 1743 reset(Info *info) 1744 { 1745 sum = Counter(); 1746 squares = Counter(); 1747 } 1748}; 1749 1750/** 1751 * Implementation of a distribution stat. The type of distribution is 1752 * determined by the Storage template. @sa ScalarBase 1753 */ 1754template <class Derived, class Stor> 1755class DistBase : public DataWrap<Derived, DistInfoProxy> 1756{ 1757 public: 1758 typedef DistInfoProxy<Derived> Info; 1759 typedef Stor Storage; 1760 typedef typename Stor::Params Params; 1761 1762 protected: 1763 /** The storage for this stat. */ 1764 char storage[sizeof(Storage)] __attribute__ ((aligned (8))); 1765 1766 protected: 1767 /** 1768 * Retrieve the storage. 1769 * @return The storage object for this stat. 1770 */ 1771 Storage * 1772 data() 1773 { 1774 return reinterpret_cast<Storage *>(storage); 1775 } 1776 1777 /** 1778 * Retrieve a const pointer to the storage. 1779 * @return A const pointer to the storage object for this stat. 1780 */ 1781 const Storage * 1782 data() const 1783 { 1784 return reinterpret_cast<const Storage *>(storage); 1785 } 1786 1787 void 1788 doInit() 1789 { 1790 new (storage) Storage(this->info()); 1791 this->setInit(); 1792 } 1793 1794 public: 1795 DistBase() { } 1796 1797 /** 1798 * Add a value to the distribtion n times. Calls sample on the storage 1799 * class. 1800 * @param v The value to add. 1801 * @param n The number of times to add it, defaults to 1. 1802 */ 1803 template <typename U> 1804 void sample(const U &v, int n = 1) { data()->sample(v, n); } 1805 1806 /** 1807 * Return the number of entries in this stat. 1808 * @return The number of entries. 1809 */ 1810 size_type size() const { return data()->size(); } 1811 /** 1812 * Return true if no samples have been added. 1813 * @return True if there haven't been any samples. 1814 */ 1815 bool zero() const { return data()->zero(); } 1816 1817 void 1818 prepare() 1819 { 1820 Info *info = this->info(); 1821 data()->prepare(info, info->data); 1822 } 1823 1824 /** 1825 * Reset stat value to default 1826 */ 1827 void 1828 reset() 1829 { 1830 data()->reset(this->info()); 1831 } 1832}; 1833 1834template <class Stat> 1835class DistProxy; 1836 1837template <class Derived, class Stor> 1838class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy> 1839{ 1840 public: 1841 typedef VectorDistInfoProxy<Derived> Info; 1842 typedef Stor Storage; 1843 typedef typename Stor::Params Params; 1844 typedef DistProxy<Derived> Proxy; 1845 friend class DistProxy<Derived>; 1846 friend class DataWrapVec<Derived, VectorDistInfoProxy>; 1847 1848 protected: 1849 Storage *storage; 1850 size_type _size; 1851 1852 protected: 1853 Storage * 1854 data(off_type index) 1855 { 1856 return &storage[index]; 1857 } 1858 1859 const Storage * 1860 data(off_type index) const 1861 { 1862 return &storage[index]; 1863 } 1864 1865 void 1866 doInit(size_type s) 1867 { 1868 assert(s > 0 && "size must be positive!"); 1869 assert(!storage && "already initialized"); 1870 _size = s; 1871 1872 char *ptr = new char[_size * sizeof(Storage)]; 1873 storage = reinterpret_cast<Storage *>(ptr); 1874 1875 Info *info = this->info(); 1876 for (off_type i = 0; i < _size; ++i) 1877 new (&storage[i]) Storage(info); 1878 1879 this->setInit(); 1880 } 1881 1882 public: 1883 VectorDistBase() 1884 : storage(NULL) 1885 {} 1886 1887 ~VectorDistBase() 1888 { 1889 if (!storage) 1890 return ; 1891 1892 for (off_type i = 0; i < _size; ++i) 1893 data(i)->~Storage(); 1894 delete [] reinterpret_cast<char *>(storage); 1895 } 1896 1897 Proxy operator[](off_type index) 1898 { 1899 assert(index >= 0 && index < size()); 1900 return Proxy(this->self(), index); 1901 } 1902 1903 size_type 1904 size() const 1905 { 1906 return _size; 1907 } 1908 1909 bool 1910 zero() const 1911 { 1912 for (off_type i = 0; i < size(); ++i) 1913 if (!data(i)->zero()) 1914 return false; 1915 return true; 1916 } 1917 1918 void 1919 prepare() 1920 { 1921 Info *info = this->info(); 1922 size_type size = this->size(); 1923 info->data.resize(size); 1924 for (off_type i = 0; i < size; ++i) 1925 data(i)->prepare(info, info->data[i]); 1926 } 1927 1928 bool 1929 check() const 1930 { 1931 return storage != NULL; 1932 } 1933}; 1934 1935template <class Stat> 1936class DistProxy 1937{ 1938 private: 1939 Stat &stat; 1940 off_type index; 1941 1942 protected: 1943 typename Stat::Storage *data() { return stat.data(index); } 1944 const typename Stat::Storage *data() const { return stat.data(index); } 1945 1946 public: 1947 DistProxy(Stat &s, off_type i) 1948 : stat(s), index(i) 1949 {} 1950 1951 DistProxy(const DistProxy &sp) 1952 : stat(sp.stat), index(sp.index) 1953 {} 1954 1955 const DistProxy & 1956 operator=(const DistProxy &sp) 1957 { 1958 stat = sp.stat; 1959 index = sp.index; 1960 return *this; 1961 } 1962 1963 public: 1964 template <typename U> 1965 void 1966 sample(const U &v, int n = 1) 1967 { 1968 data()->sample(v, n); 1969 } 1970 1971 size_type 1972 size() const 1973 { 1974 return 1; 1975 } 1976 1977 bool 1978 zero() const 1979 { 1980 return data()->zero(); 1981 } 1982 1983 /** 1984 * Proxy has no state. Nothing to reset. 1985 */ 1986 void reset() { } 1987}; 1988 1989////////////////////////////////////////////////////////////////////// 1990// 1991// Formula Details 1992// 1993////////////////////////////////////////////////////////////////////// 1994 1995/** 1996 * Base class for formula statistic node. These nodes are used to build a tree 1997 * that represents the formula. 1998 */ 1999class Node : public RefCounted 2000{ 2001 public: 2002 /** 2003 * Return the number of nodes in the subtree starting at this node. 2004 * @return the number of nodes in this subtree. 2005 */ 2006 virtual size_type size() const = 0; 2007 /** 2008 * Return the result vector of this subtree. 2009 * @return The result vector of this subtree. 2010 */ 2011 virtual const VResult &result() const = 0; 2012 /** 2013 * Return the total of the result vector. 2014 * @return The total of the result vector. 2015 */ 2016 virtual Result total() const = 0; 2017 2018 /** 2019 * 2020 */ 2021 virtual std::string str() const = 0; 2022}; 2023 2024/** Reference counting pointer to a function Node. */ 2025typedef RefCountingPtr<Node> NodePtr; 2026 2027class ScalarStatNode : public Node 2028{ 2029 private: 2030 const ScalarInfo *data; 2031 mutable VResult vresult; 2032 2033 public: 2034 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {} 2035 2036 const VResult & 2037 result() const 2038 { 2039 vresult[0] = data->result(); 2040 return vresult; 2041 } 2042 2043 Result total() const { return data->result(); }; 2044 2045 size_type size() const { return 1; } 2046 2047 /** 2048 * 2049 */ 2050 std::string str() const { return data->name; } 2051}; 2052 2053template <class Stat> 2054class ScalarProxyNode : public Node 2055{ 2056 private: 2057 const ScalarProxy<Stat> proxy; 2058 mutable VResult vresult; 2059 2060 public: 2061 ScalarProxyNode(const ScalarProxy<Stat> &p) 2062 : proxy(p), vresult(1) 2063 { } 2064 2065 const VResult & 2066 result() const 2067 { 2068 vresult[0] = proxy.result(); 2069 return vresult; 2070 } 2071 2072 Result 2073 total() const 2074 { 2075 return proxy.result(); 2076 } 2077 2078 size_type 2079 size() const 2080 { 2081 return 1; 2082 } 2083 2084 /** 2085 * 2086 */ 2087 std::string 2088 str() const 2089 { 2090 return proxy.str(); 2091 } 2092}; 2093 2094class VectorStatNode : public Node 2095{ 2096 private: 2097 const VectorInfo *data; 2098 2099 public: 2100 VectorStatNode(const VectorInfo *d) : data(d) { } 2101 const VResult &result() const { return data->result(); } 2102 Result total() const { return data->total(); }; 2103 2104 size_type size() const { return data->size(); } 2105 2106 std::string str() const { return data->name; } 2107}; 2108 2109template <class T> 2110class ConstNode : public Node 2111{ 2112 private: 2113 VResult vresult; 2114 2115 public: 2116 ConstNode(T s) : vresult(1, (Result)s) {} 2117 const VResult &result() const { return vresult; } 2118 Result total() const { return vresult[0]; }; 2119 size_type size() const { return 1; } 2120 std::string str() const { return to_string(vresult[0]); } 2121}; 2122 2123template <class T> 2124class ConstVectorNode : public Node 2125{ 2126 private: 2127 VResult vresult; 2128 2129 public: 2130 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {} 2131 const VResult &result() const { return vresult; } 2132 2133 Result 2134 total() const 2135 { 2136 size_type size = this->size(); 2137 Result tmp = 0; 2138 for (off_type i = 0; i < size; i++) 2139 tmp += vresult[i]; 2140 return tmp; 2141 } 2142 2143 size_type size() const { return vresult.size(); } 2144 std::string 2145 str() const 2146 { 2147 size_type size = this->size(); 2148 std::string tmp = "("; 2149 for (off_type i = 0; i < size; i++) 2150 tmp += csprintf("%s ",to_string(vresult[i])); 2151 tmp += ")"; 2152 return tmp; 2153 } 2154}; 2155 2156template <class Op> 2157struct OpString; 2158 2159template<> 2160struct OpString<std::plus<Result> > 2161{ 2162 static std::string str() { return "+"; } 2163}; 2164 2165template<> 2166struct OpString<std::minus<Result> > 2167{ 2168 static std::string str() { return "-"; } 2169}; 2170 2171template<> 2172struct OpString<std::multiplies<Result> > 2173{ 2174 static std::string str() { return "*"; } 2175}; 2176 2177template<> 2178struct OpString<std::divides<Result> > 2179{ 2180 static std::string str() { return "/"; } 2181}; 2182 2183template<> 2184struct OpString<std::modulus<Result> > 2185{ 2186 static std::string str() { return "%"; } 2187}; 2188 2189template<> 2190struct OpString<std::negate<Result> > 2191{ 2192 static std::string str() { return "-"; } 2193}; 2194 2195template <class Op> 2196class UnaryNode : public Node 2197{ 2198 public: 2199 NodePtr l; 2200 mutable VResult vresult; 2201 2202 public: 2203 UnaryNode(NodePtr &p) : l(p) {} 2204 2205 const VResult & 2206 result() const 2207 { 2208 const VResult &lvec = l->result(); 2209 size_type size = lvec.size(); 2210 2211 assert(size > 0); 2212 2213 vresult.resize(size); 2214 Op op; 2215 for (off_type i = 0; i < size; ++i) 2216 vresult[i] = op(lvec[i]); 2217 2218 return vresult; 2219 } 2220 2221 Result 2222 total() const 2223 { 2224 const VResult &vec = this->result(); 2225 Result total = 0.0; 2226 for (off_type i = 0; i < size(); i++) 2227 total += vec[i]; 2228 return total; 2229 } 2230 2231 size_type size() const { return l->size(); } 2232 2233 std::string 2234 str() const 2235 { 2236 return OpString<Op>::str() + l->str(); 2237 } 2238}; 2239 2240template <class Op> 2241class BinaryNode : public Node 2242{ 2243 public: 2244 NodePtr l; 2245 NodePtr r; 2246 mutable VResult vresult; 2247 2248 public: 2249 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {} 2250 2251 const VResult & 2252 result() const 2253 { 2254 Op op; 2255 const VResult &lvec = l->result(); 2256 const VResult &rvec = r->result(); 2257 2258 assert(lvec.size() > 0 && rvec.size() > 0); 2259 2260 if (lvec.size() == 1 && rvec.size() == 1) { 2261 vresult.resize(1); 2262 vresult[0] = op(lvec[0], rvec[0]); 2263 } else if (lvec.size() == 1) { 2264 size_type size = rvec.size(); 2265 vresult.resize(size); 2266 for (off_type i = 0; i < size; ++i) 2267 vresult[i] = op(lvec[0], rvec[i]); 2268 } else if (rvec.size() == 1) { 2269 size_type size = lvec.size(); 2270 vresult.resize(size); 2271 for (off_type i = 0; i < size; ++i) 2272 vresult[i] = op(lvec[i], rvec[0]); 2273 } else if (rvec.size() == lvec.size()) { 2274 size_type size = rvec.size(); 2275 vresult.resize(size); 2276 for (off_type i = 0; i < size; ++i) 2277 vresult[i] = op(lvec[i], rvec[i]); 2278 } 2279 2280 return vresult; 2281 } 2282 2283 Result 2284 total() const 2285 { 2286 const VResult &vec = this->result(); 2287 Result total = 0.0; 2288 for (off_type i = 0; i < size(); i++) 2289 total += vec[i]; 2290 return total; 2291 } 2292 2293 size_type 2294 size() const 2295 { 2296 size_type ls = l->size(); 2297 size_type rs = r->size(); 2298 if (ls == 1) { 2299 return rs; 2300 } else if (rs == 1) { 2301 return ls; 2302 } else { 2303 assert(ls == rs && "Node vector sizes are not equal"); 2304 return ls; 2305 } 2306 } 2307 2308 std::string 2309 str() const 2310 { 2311 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str()); 2312 } 2313}; 2314 2315template <class Op> 2316class SumNode : public Node 2317{ 2318 public: 2319 NodePtr l; 2320 mutable VResult vresult; 2321 2322 public: 2323 SumNode(NodePtr &p) : l(p), vresult(1) {} 2324 2325 const VResult & 2326 result() const 2327 { 2328 const VResult &lvec = l->result(); 2329 size_type size = lvec.size(); 2330 assert(size > 0); 2331 2332 vresult[0] = 0.0; 2333 2334 Op op; 2335 for (off_type i = 0; i < size; ++i) 2336 vresult[0] = op(vresult[0], lvec[i]); 2337 2338 return vresult; 2339 } 2340 2341 Result 2342 total() const 2343 { 2344 const VResult &lvec = l->result(); 2345 size_type size = lvec.size(); 2346 assert(size > 0); 2347 2348 Result vresult = 0.0; 2349 2350 Op op; 2351 for (off_type i = 0; i < size; ++i) 2352 vresult = op(vresult, lvec[i]); 2353 2354 return vresult; 2355 } 2356 2357 size_type size() const { return 1; } 2358 2359 std::string 2360 str() const 2361 { 2362 return csprintf("total(%s)", l->str()); 2363 } 2364}; 2365 2366 2367////////////////////////////////////////////////////////////////////// 2368// 2369// Visible Statistics Types 2370// 2371////////////////////////////////////////////////////////////////////// 2372/** 2373 * @defgroup VisibleStats "Statistic Types" 2374 * These are the statistics that are used in the simulator. 2375 * @{ 2376 */ 2377 2378/** 2379 * This is a simple scalar statistic, like a counter. 2380 * @sa Stat, ScalarBase, StatStor 2381 */ 2382class Scalar : public ScalarBase<Scalar, StatStor> 2383{ 2384 public: 2385 using ScalarBase<Scalar, StatStor>::operator=; 2386}; 2387 2388/** 2389 * A stat that calculates the per tick average of a value. 2390 * @sa Stat, ScalarBase, AvgStor 2391 */ 2392class Average : public ScalarBase<Average, AvgStor> 2393{ 2394 public: 2395 using ScalarBase<Average, AvgStor>::operator=; 2396}; 2397 2398class Value : public ValueBase<Value> 2399{ 2400}; 2401 2402/** 2403 * A vector of scalar stats. 2404 * @sa Stat, VectorBase, StatStor 2405 */ 2406class Vector : public VectorBase<Vector, StatStor> 2407{ 2408}; 2409 2410/** 2411 * A vector of Average stats. 2412 * @sa Stat, VectorBase, AvgStor 2413 */ 2414class AverageVector : public VectorBase<AverageVector, AvgStor> 2415{ 2416}; 2417 2418/** 2419 * A 2-Dimensional vecto of scalar stats. 2420 * @sa Stat, Vector2dBase, StatStor 2421 */ 2422class Vector2d : public Vector2dBase<Vector2d, StatStor> 2423{ 2424}; 2425 2426/** 2427 * A simple distribution stat. 2428 * @sa Stat, DistBase, DistStor 2429 */ 2430class Distribution : public DistBase<Distribution, DistStor> 2431{ 2432 public: 2433 /** 2434 * Set the parameters of this distribution. @sa DistStor::Params 2435 * @param min The minimum value of the distribution. 2436 * @param max The maximum value of the distribution. 2437 * @param bkt The number of values in each bucket. 2438 * @return A reference to this distribution. 2439 */ 2440 Distribution & 2441 init(Counter min, Counter max, Counter bkt) 2442 { 2443 DistStor::Params *params = new DistStor::Params; 2444 params->min = min; 2445 params->max = max; 2446 params->bucket_size = bkt; 2447 params->buckets = (size_type)ceil((max - min + 1.0) / bkt); 2448 this->setParams(params); 2449 this->doInit(); 2450 return this->self(); 2451 } 2452}; 2453 2454/** 2455 * A simple histogram stat. 2456 * @sa Stat, DistBase, HistStor 2457 */ 2458class Histogram : public DistBase<Histogram, HistStor> 2459{ 2460 public: 2461 /** 2462 * Set the parameters of this histogram. @sa HistStor::Params 2463 * @param size The number of buckets in the histogram 2464 * @return A reference to this histogram. 2465 */ 2466 Histogram & 2467 init(size_type size) 2468 { 2469 HistStor::Params *params = new HistStor::Params; 2470 params->buckets = size; 2471 this->setParams(params); 2472 this->doInit(); 2473 return this->self(); 2474 } 2475}; 2476 2477/** 2478 * Calculates the mean and variance of all the samples. 2479 * @sa DistBase, SampleStor 2480 */ 2481class StandardDeviation : public DistBase<StandardDeviation, SampleStor> 2482{ 2483 public: 2484 /** 2485 * Construct and initialize this distribution. 2486 */ 2487 StandardDeviation() 2488 { 2489 SampleStor::Params *params = new SampleStor::Params; 2490 this->doInit(); 2491 this->setParams(params); 2492 } 2493}; 2494 2495/** 2496 * Calculates the per tick mean and variance of the samples. 2497 * @sa DistBase, AvgSampleStor 2498 */ 2499class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor> 2500{ 2501 public: 2502 /** 2503 * Construct and initialize this distribution. 2504 */ 2505 AverageDeviation() 2506 { 2507 AvgSampleStor::Params *params = new AvgSampleStor::Params; 2508 this->doInit(); 2509 this->setParams(params); 2510 } 2511}; 2512 2513/** 2514 * A vector of distributions. 2515 * @sa VectorDistBase, DistStor 2516 */ 2517class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor> 2518{ 2519 public: 2520 /** 2521 * Initialize storage and parameters for this distribution. 2522 * @param size The size of the vector (the number of distributions). 2523 * @param min The minimum value of the distribution. 2524 * @param max The maximum value of the distribution. 2525 * @param bkt The number of values in each bucket. 2526 * @return A reference to this distribution. 2527 */ 2528 VectorDistribution & 2529 init(size_type size, Counter min, Counter max, Counter bkt) 2530 { 2531 DistStor::Params *params = new DistStor::Params; 2532 params->min = min; 2533 params->max = max; 2534 params->bucket_size = bkt; 2535 params->buckets = (size_type)ceil((max - min + 1.0) / bkt); 2536 this->setParams(params); 2537 this->doInit(size); 2538 return this->self(); 2539 } 2540}; 2541 2542/** 2543 * This is a vector of StandardDeviation stats. 2544 * @sa VectorDistBase, SampleStor 2545 */ 2546class VectorStandardDeviation 2547 : public VectorDistBase<VectorStandardDeviation, SampleStor> 2548{ 2549 public: 2550 /** 2551 * Initialize storage for this distribution. 2552 * @param size The size of the vector. 2553 * @return A reference to this distribution. 2554 */ 2555 VectorStandardDeviation & 2556 init(size_type size) 2557 { 2558 SampleStor::Params *params = new SampleStor::Params; 2559 this->doInit(size); 2560 this->setParams(params); 2561 return this->self(); 2562 } 2563}; 2564 2565/** 2566 * This is a vector of AverageDeviation stats. 2567 * @sa VectorDistBase, AvgSampleStor 2568 */ 2569class VectorAverageDeviation 2570 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor> 2571{ 2572 public: 2573 /** 2574 * Initialize storage for this distribution. 2575 * @param size The size of the vector. 2576 * @return A reference to this distribution. 2577 */ 2578 VectorAverageDeviation & 2579 init(size_type size) 2580 { 2581 AvgSampleStor::Params *params = new AvgSampleStor::Params; 2582 this->doInit(size); 2583 this->setParams(params); 2584 return this->self(); 2585 } 2586}; 2587 2588template <class Stat> 2589class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo> 2590{ 2591 protected: 2592 mutable VResult vec; 2593 mutable VCounter cvec; 2594 2595 public: 2596 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {} 2597 2598 size_type size() const { return this->s.size(); } 2599 2600 const VResult & 2601 result() const 2602 { 2603 this->s.result(vec); 2604 return vec; 2605 } 2606 Result total() const { return this->s.total(); } 2607 VCounter &value() const { return cvec; } 2608 2609 std::string str() const { return this->s.str(); } 2610}; 2611 2612template <class Stat> 2613class SparseHistInfoProxy : public InfoProxy<Stat, SparseHistInfo> 2614{ 2615 public: 2616 SparseHistInfoProxy(Stat &stat) : InfoProxy<Stat, SparseHistInfo>(stat) {} 2617}; 2618 2619/** 2620 * Implementation of a sparse histogram stat. The storage class is 2621 * determined by the Storage template. 2622 */ 2623template <class Derived, class Stor> 2624class SparseHistBase : public DataWrap<Derived, SparseHistInfoProxy> 2625{ 2626 public: 2627 typedef SparseHistInfoProxy<Derived> Info; 2628 typedef Stor Storage; 2629 typedef typename Stor::Params Params; 2630 2631 protected: 2632 /** The storage for this stat. */ 2633 char storage[sizeof(Storage)]; 2634 2635 protected: 2636 /** 2637 * Retrieve the storage. 2638 * @return The storage object for this stat. 2639 */ 2640 Storage * 2641 data() 2642 { 2643 return reinterpret_cast<Storage *>(storage); 2644 } 2645 2646 /** 2647 * Retrieve a const pointer to the storage. 2648 * @return A const pointer to the storage object for this stat. 2649 */ 2650 const Storage * 2651 data() const 2652 { 2653 return reinterpret_cast<const Storage *>(storage); 2654 } 2655 2656 void 2657 doInit() 2658 { 2659 new (storage) Storage(this->info()); 2660 this->setInit(); 2661 } 2662 2663 public: 2664 SparseHistBase() { } 2665 2666 /** 2667 * Add a value to the distribtion n times. Calls sample on the storage 2668 * class. 2669 * @param v The value to add. 2670 * @param n The number of times to add it, defaults to 1. 2671 */ 2672 template <typename U> 2673 void sample(const U &v, int n = 1) { data()->sample(v, n); } 2674 2675 /** 2676 * Return the number of entries in this stat. 2677 * @return The number of entries. 2678 */ 2679 size_type size() const { return data()->size(); } 2680 /** 2681 * Return true if no samples have been added. 2682 * @return True if there haven't been any samples. 2683 */ 2684 bool zero() const { return data()->zero(); } 2685 2686 void 2687 prepare() 2688 { 2689 Info *info = this->info(); 2690 data()->prepare(info, info->data); 2691 } 2692 2693 /** 2694 * Reset stat value to default 2695 */ 2696 void 2697 reset() 2698 { 2699 data()->reset(this->info()); 2700 } 2701}; 2702 2703/** 2704 * Templatized storage and interface for a sparse histogram stat. 2705 */ 2706class SparseHistStor 2707{ 2708 public: 2709 /** The parameters for a sparse histogram stat. */ 2710 struct Params : public DistParams 2711 { 2712 Params() : DistParams(Hist) {} 2713 }; 2714 2715 private: 2716 /** Counter for number of samples */ 2717 Counter samples; 2718 /** Counter for each bucket. */ 2719 MCounter cmap; 2720 2721 public: 2722 SparseHistStor(Info *info) 2723 { 2724 reset(info); 2725 } 2726 2727 /** 2728 * Add a value to the distribution for the given number of times. 2729 * @param val The value to add. 2730 * @param number The number of times to add the value. 2731 */ 2732 void 2733 sample(Counter val, int number) 2734 { 2735 cmap[val] += number; 2736 samples += number; 2737 } 2738 2739 /** 2740 * Return the number of buckets in this distribution. 2741 * @return the number of buckets. 2742 */ 2743 size_type size() const { return cmap.size(); } 2744 2745 /** 2746 * Returns true if any calls to sample have been made. 2747 * @return True if any values have been sampled. 2748 */ 2749 bool 2750 zero() const 2751 { 2752 return samples == Counter(); 2753 } 2754 2755 void 2756 prepare(Info *info, SparseHistData &data) 2757 { 2758 MCounter::iterator it; 2759 data.cmap.clear(); 2760 for (it = cmap.begin(); it != cmap.end(); it++) { 2761 data.cmap[(*it).first] = (*it).second; 2762 } 2763 2764 data.samples = samples; 2765 } 2766 2767 /** 2768 * Reset stat value to default 2769 */ 2770 void 2771 reset(Info *info) 2772 { 2773 cmap.clear(); 2774 samples = 0; 2775 } 2776}; 2777 2778class SparseHistogram : public SparseHistBase<SparseHistogram, SparseHistStor> 2779{ 2780 public: 2781 /** 2782 * Set the parameters of this histogram. @sa HistStor::Params 2783 * @param size The number of buckets in the histogram 2784 * @return A reference to this histogram. 2785 */ 2786 SparseHistogram & 2787 init(size_type size) 2788 { 2789 SparseHistStor::Params *params = new SparseHistStor::Params; 2790 this->setParams(params); 2791 this->doInit(); 2792 return this->self(); 2793 } 2794}; 2795 2796class Temp; 2797/** 2798 * A formula for statistics that is calculated when printed. A formula is 2799 * stored as a tree of Nodes that represent the equation to calculate. 2800 * @sa Stat, ScalarStat, VectorStat, Node, Temp 2801 */ 2802class Formula : public DataWrapVec<Formula, FormulaInfoProxy> 2803{ 2804 protected: 2805 /** The root of the tree which represents the Formula */ 2806 NodePtr root; 2807 friend class Temp; 2808 2809 public: 2810 /** 2811 * Create and initialize thie formula, and register it with the database. 2812 */ 2813 Formula(); 2814 2815 /** 2816 * Create a formula with the given root node, register it with the 2817 * database. 2818 * @param r The root of the expression tree. 2819 */ 2820 Formula(Temp r); 2821 2822 /** 2823 * Set an unitialized Formula to the given root. 2824 * @param r The root of the expression tree. 2825 * @return a reference to this formula. 2826 */ 2827 const Formula &operator=(Temp r); 2828 2829 /** 2830 * Add the given tree to the existing one. 2831 * @param r The root of the expression tree. 2832 * @return a reference to this formula. 2833 */ 2834 const Formula &operator+=(Temp r); 2835 /** 2836 * Return the result of the Fomula in a vector. If there were no Vector 2837 * components to the Formula, then the vector is size 1. If there were, 2838 * like x/y with x being a vector of size 3, then the result returned will 2839 * be x[0]/y, x[1]/y, x[2]/y, respectively. 2840 * @return The result vector. 2841 */ 2842 void result(VResult &vec) const; 2843 2844 /** 2845 * Return the total Formula result. If there is a Vector 2846 * component to this Formula, then this is the result of the 2847 * Formula if the formula is applied after summing all the 2848 * components of the Vector. For example, if Formula is x/y where 2849 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If 2850 * there is no Vector component, total() returns the same value as 2851 * the first entry in the VResult val() returns. 2852 * @return The total of the result vector. 2853 */ 2854 Result total() const; 2855 2856 /** 2857 * Return the number of elements in the tree. 2858 */ 2859 size_type size() const; 2860 2861 void prepare() { } 2862 2863 /** 2864 * Formulas don't need to be reset 2865 */ 2866 void reset(); 2867 2868 /** 2869 * 2870 */ 2871 bool zero() const; 2872 2873 std::string str() const; 2874}; 2875 2876class FormulaNode : public Node 2877{ 2878 private: 2879 const Formula &formula; 2880 mutable VResult vec; 2881 2882 public: 2883 FormulaNode(const Formula &f) : formula(f) {} 2884 2885 size_type size() const { return formula.size(); } 2886 const VResult &result() const { formula.result(vec); return vec; } 2887 Result total() const { return formula.total(); } 2888 2889 std::string str() const { return formula.str(); } 2890}; 2891 2892/** 2893 * Helper class to construct formula node trees. 2894 */ 2895class Temp 2896{ 2897 protected: 2898 /** 2899 * Pointer to a Node object. 2900 */ 2901 NodePtr node; 2902 2903 public: 2904 /** 2905 * Copy the given pointer to this class. 2906 * @param n A pointer to a Node object to copy. 2907 */ 2908 Temp(NodePtr n) : node(n) { } 2909 2910 /** 2911 * Return the node pointer. 2912 * @return the node pointer. 2913 */ 2914 operator NodePtr&() { return node; } 2915 2916 public: 2917 /** 2918 * Create a new ScalarStatNode. 2919 * @param s The ScalarStat to place in a node. 2920 */ 2921 Temp(const Scalar &s) 2922 : node(new ScalarStatNode(s.info())) 2923 { } 2924 2925 /** 2926 * Create a new ScalarStatNode. 2927 * @param s The ScalarStat to place in a node. 2928 */ 2929 Temp(const Value &s) 2930 : node(new ScalarStatNode(s.info())) 2931 { } 2932 2933 /** 2934 * Create a new ScalarStatNode. 2935 * @param s The ScalarStat to place in a node. 2936 */ 2937 Temp(const Average &s) 2938 : node(new ScalarStatNode(s.info())) 2939 { } 2940 2941 /** 2942 * Create a new VectorStatNode. 2943 * @param s The VectorStat to place in a node. 2944 */ 2945 Temp(const Vector &s) 2946 : node(new VectorStatNode(s.info())) 2947 { } 2948 2949 Temp(const AverageVector &s) 2950 : node(new VectorStatNode(s.info())) 2951 { } 2952 2953 /** 2954 * 2955 */ 2956 Temp(const Formula &f) 2957 : node(new FormulaNode(f)) 2958 { } 2959 2960 /** 2961 * Create a new ScalarProxyNode. 2962 * @param p The ScalarProxy to place in a node. 2963 */ 2964 template <class Stat> 2965 Temp(const ScalarProxy<Stat> &p) 2966 : node(new ScalarProxyNode<Stat>(p)) 2967 { } 2968 2969 /** 2970 * Create a ConstNode 2971 * @param value The value of the const node. 2972 */ 2973 Temp(signed char value) 2974 : node(new ConstNode<signed char>(value)) 2975 { } 2976 2977 /** 2978 * Create a ConstNode 2979 * @param value The value of the const node. 2980 */ 2981 Temp(unsigned char value) 2982 : node(new ConstNode<unsigned char>(value)) 2983 { } 2984 2985 /** 2986 * Create a ConstNode 2987 * @param value The value of the const node. 2988 */ 2989 Temp(signed short value) 2990 : node(new ConstNode<signed short>(value)) 2991 { } 2992 2993 /** 2994 * Create a ConstNode 2995 * @param value The value of the const node. 2996 */ 2997 Temp(unsigned short value) 2998 : node(new ConstNode<unsigned short>(value)) 2999 { } 3000 3001 /** 3002 * Create a ConstNode 3003 * @param value The value of the const node. 3004 */ 3005 Temp(signed int value) 3006 : node(new ConstNode<signed int>(value)) 3007 { } 3008 3009 /** 3010 * Create a ConstNode 3011 * @param value The value of the const node. 3012 */ 3013 Temp(unsigned int value) 3014 : node(new ConstNode<unsigned int>(value)) 3015 { } 3016 3017 /** 3018 * Create a ConstNode 3019 * @param value The value of the const node. 3020 */ 3021 Temp(signed long value) 3022 : node(new ConstNode<signed long>(value)) 3023 { } 3024 3025 /** 3026 * Create a ConstNode 3027 * @param value The value of the const node. 3028 */ 3029 Temp(unsigned long value) 3030 : node(new ConstNode<unsigned long>(value)) 3031 { } 3032 3033 /** 3034 * Create a ConstNode 3035 * @param value The value of the const node. 3036 */ 3037 Temp(signed long long value) 3038 : node(new ConstNode<signed long long>(value)) 3039 { } 3040 3041 /** 3042 * Create a ConstNode 3043 * @param value The value of the const node. 3044 */ 3045 Temp(unsigned long long value) 3046 : node(new ConstNode<unsigned long long>(value)) 3047 { } 3048 3049 /** 3050 * Create a ConstNode 3051 * @param value The value of the const node. 3052 */ 3053 Temp(float value) 3054 : node(new ConstNode<float>(value)) 3055 { } 3056 3057 /** 3058 * Create a ConstNode 3059 * @param value The value of the const node. 3060 */ 3061 Temp(double value) 3062 : node(new ConstNode<double>(value)) 3063 { } 3064}; 3065 3066 3067/** 3068 * @} 3069 */ 3070 3071inline Temp 3072operator+(Temp l, Temp r) 3073{ 3074 return NodePtr(new BinaryNode<std::plus<Result> >(l, r)); 3075} 3076 3077inline Temp 3078operator-(Temp l, Temp r) 3079{ 3080 return NodePtr(new BinaryNode<std::minus<Result> >(l, r)); 3081} 3082 3083inline Temp 3084operator*(Temp l, Temp r) 3085{ 3086 return NodePtr(new BinaryNode<std::multiplies<Result> >(l, r)); 3087} 3088 3089inline Temp 3090operator/(Temp l, Temp r) 3091{ 3092 return NodePtr(new BinaryNode<std::divides<Result> >(l, r)); 3093} 3094 3095inline Temp 3096operator-(Temp l) 3097{ 3098 return NodePtr(new UnaryNode<std::negate<Result> >(l)); 3099} 3100 3101template <typename T> 3102inline Temp 3103constant(T val) 3104{ 3105 return NodePtr(new ConstNode<T>(val)); 3106} 3107 3108template <typename T> 3109inline Temp 3110constantVector(T val) 3111{ 3112 return NodePtr(new ConstVectorNode<T>(val)); 3113} 3114 3115inline Temp 3116sum(Temp val) 3117{ 3118 return NodePtr(new SumNode<std::plus<Result> >(val)); 3119} 3120 3121/** Dump all statistics data to the registered outputs */ 3122void dump(); 3123void reset(); 3124 3125/** 3126 * Register a callback that should be called whenever statistics are 3127 * reset 3128 */ 3129void registerResetCallback(Callback *cb); 3130 3131std::list<Info *> &statsList(); 3132 3133} // namespace Stats 3134 3135#endif // __BASE_STATISTICS_HH__ 3136