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