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