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