00001 // Copyright (C) 2001, Compaq Computer Corporation 00002 // 00003 // This file is part of Vesta. 00004 // 00005 // Vesta is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU Lesser General Public 00007 // License as published by the Free Software Foundation; either 00008 // version 2.1 of the License, or (at your option) any later version. 00009 // 00010 // Vesta is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public 00016 // License along with Vesta; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 // Last modified on Thu Aug 5 00:41:36 EDT 2004 by ken@xorian.net 00020 // modified on Tue Dec 3 08:29:41 PST 1996 by heydon 00021 00022 #ifndef _STAT_COUNT_H 00023 #define _STAT_COUNT_H 00024 00025 /* A "StatCount" is a statistical container for a set of integers. 00026 There is a method for adding a number to the container, and for 00027 retrieving various statistical properties of the numbers, such 00028 as their minimum, maximum, and mean. There is also a method for 00029 retrieving a histogram of the numbers classified according to 00030 an overridable mapping function. 00031 00032 The methods of a "StatCount" are unmonitored. */ 00033 00034 #include <Basics.H> 00035 #include <BasicsAllocator.H> 00036 #include <Table.H> 00037 #include <Generics.H> 00038 #include <list> 00039 #include <RegExp.H> 00040 #include "StatNames.H" 00041 #include "StatLocation.H" 00042 00043 #include <Sequence.H> 00044 00045 // a sequence of pointers to RegExp objects 00046 typedef Sequence<Basics::RegExp*> RegExpSeq; 00047 00048 class StatCount { 00049 public: 00050 StatCount() throw (); 00051 /* Initialize to count of zero items. */ 00052 00053 void SetHistoMap(const char *name, int (*map)(int)) throw (); 00054 /* A StatCount maintains a histogram of the values placed in 00055 it via the "AddVal" method. By default, the histogram is 00056 of the values themselves. However, if the values are sparse, 00057 it may be more meaningful to keep a histogram of some mapping 00058 of the values. This function sets the mapping function to 00059 "map"; the name "name" will be printed as part of printing 00060 the histogram. */ 00061 00062 void SetReportCount(unsigned int reportCount) 00063 { this->reportCount = reportCount; this->reportCount_set = true; } 00064 /* If desired, while recording the total, min, max, and histogram 00065 of this statistic, a certain number of "troublesome" functions 00066 (sourceFuncs) can be saved and listed after statistics 00067 gathering. This can be helpful in trying to track down 00068 functions that need attention. */ 00069 00070 enum troubleEnd_t { troubleHigh, troubleLow }; 00071 void SetTroubleEnd(troubleEnd_t end) 00072 { this->troubleEnd = end; } 00073 /* The "trouble end" of a statistic determines whether we report 00074 about functions at the high or low end of the scale. */ 00075 00076 void AddReportMask(Basics::RegExp *mask) 00077 { this->troubleMasks.addhi(mask); } 00078 /* Keep any functions matching the regular expression passed from 00079 being included in the report of "troublesome" functions. */ 00080 00081 void AddVal(int val, const Stat::Location *loc) throw (); 00082 /* Add "val" to the set of numbers in this container. If this value 00083 is a new minimum and/or maximum, "loc" is recorded as the associated 00084 location that produced the value. */ 00085 00086 void Merge(const StatCount &sc) throw (); 00087 /* Merge the values in "sc" into this container. */ 00088 00089 bool IsEmpty() const throw () { return this->numVals == 0; } 00090 /* Return "true" iff the container does not contain any values. */ 00091 00092 int MinVal() const throw (); 00093 int MaxVal() const throw (); 00094 float MeanVal() const throw (); 00095 /* Return the minimum, maximum, and mean of the numbers added 00096 to the container so far. These methods are all checked 00097 run-time errors if "IsEmpty()". */ 00098 00099 Basics::int64 SumTotal() const throw () { return this->total; } 00100 /* Return the sum total of all the values added to this container 00101 so far. */ 00102 00103 void Print(std::ostream &os, int indent, bool printHisto, bool printMinLoc, 00104 bool printMaxLoc, bool printMean = true) const throw (); 00105 /* Print the stats for this container to "os", indented by 00106 "indent" spaces on each line. If "printHisto" is true, the 00107 histogram is printed. If "printMinLoc"/"printMaxLoc" is true, 00108 the location associated with the minimum/maximum value is 00109 printed. If "printMean" is false and all the values added to 00110 this StatCount were 1, only the number of values added is 00111 printed; otherwise, the minimum, mean, and maximum values are 00112 printed, as well as any information requested by the 00113 "printHisto", "printMinLoc", and "printMaxLoc" arguments. 00114 Finally, if this statistic has a non-zero "reportCount", 00115 PrintTrouble is called. */ 00116 00117 void PrintHisto(std::ostream &os, int indent) const throw (); 00118 /* Print the container's histogram, with each line indented 00119 by "indent" spaces. */ 00120 00121 void PrintTrouble(std::ostream &os, int indent) const throw (); 00122 /* Print out information about "troublesome" functions. */ 00123 00124 private: 00125 // data fields 00126 int numVals; 00127 int minVal; 00128 int maxVal; 00129 Basics::int64 total; 00130 IntIntTbl tbl; 00131 const Stat::Location *minLoc, *maxLoc; 00132 00133 // histogram map 00134 const char *histoMapName; 00135 int (*histoMap)(int); 00136 00137 // Parameters controlling the search for "troublesome" areas. 00138 troubleEnd_t troubleEnd; 00139 unsigned int reportCount; 00140 bool reportCount_set; 00141 00142 // Data structures used to store information about "troublesome" 00143 // entries. 00144 struct toubleLoc_t 00145 { 00146 int val; 00147 const Stat::Location *loc; 00148 toubleLoc_t() : val(-1), loc(0) { } 00149 toubleLoc_t(int val, const Stat::Location *loc) 00150 : val(val), loc(loc) { } 00151 toubleLoc_t(const toubleLoc_t &other) 00152 : val(other.val), loc(other.loc) { } 00153 toubleLoc_t &operator=(const toubleLoc_t &other) 00154 { val = other.val; loc = other.loc; return *this; } 00155 bool operator==(const toubleLoc_t &other) 00156 { return (val == other.val) && (loc == other.loc); } 00157 bool operator<(const toubleLoc_t &other) 00158 { return (val < other.val); } 00159 }; 00160 typedef std::list<toubleLoc_t, 00161 Basics::Allocator<toubleLoc_t> > troubleSet_t; 00162 troubleSet_t troubleSet; 00163 TextIntTbl troubleCounts; 00164 00165 // Regular espressions used to exclude some functions from the 00166 // trouble reporting. 00167 RegExpSeq troubleMasks; 00168 00169 // hide the copy constructor from clients 00170 StatCount(const StatCount &); 00171 00172 bool MoreTrouble(int val1, int val2); 00173 00174 // Insert a new entry into our troubleSet. 00175 void AddTroubleEntry(int val, const Stat::Location *loc); 00176 00177 // Prune entries from our troubleSet. 00178 void PruneTroubleEntries(); 00179 }; 00180 00181 #endif // _STAT_COUNT_H