stats.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "common/h/headers.h"
00037 #include "common/h/stats.h"
00038 #include "dynutil/h/util.h"
00039 #include <sstream>
00040 #include <string>
00041 #if 0
00042 #include "dyninstAPI/src/stats.h"
00043
00044 CntStatistic trampBytes;
00045 CntStatistic pointsUsed;
00046 CntStatistic insnGenerated;
00047 CntStatistic totalMiniTramps;
00048 double timeCostLastChanged=0;
00049
00050
00051 CntStatistic ptraceOtherOps, ptraceOps, ptraceBytes;
00052
00053 void printDyninstStats()
00054 {
00055 sprintf(errorLine, " %ld total points used\n", pointsUsed.value());
00056 logLine(errorLine);
00057 sprintf(errorLine, " %ld mini-tramps used\n", totalMiniTramps.value());
00058 logLine(errorLine);
00059 sprintf(errorLine, " %ld tramp bytes\n", trampBytes.value());
00060 logLine(errorLine);
00061 sprintf(errorLine, " %ld ptrace other calls\n", ptraceOtherOps.value());
00062 logLine(errorLine);
00063 sprintf(errorLine, " %ld ptrace write calls\n",
00064 ptraceOps.value()-ptraceOtherOps.value());
00065 logLine(errorLine);
00066 sprintf(errorLine, " %ld ptrace bytes written\n", ptraceBytes.value());
00067 logLine(errorLine);
00068 sprintf(errorLine, " %ld instructions generated\n",
00069 insnGenerated.value());
00070 logLine(errorLine);
00071 }
00072 #endif
00073
00074 StatContainer::StatContainer()
00075
00076 {
00077 }
00078
00079 Statistic*
00080 StatContainer::operator[](std::string &name)
00081 {
00082 if (stats_.find(name) != stats_.end()) {
00083 return stats_[name];
00084 } else {
00085 return NULL;
00086 }
00087 }
00088
00089 void
00090 StatContainer::add(std::string name, StatType type)
00091 {
00092 Statistic *s = NULL;
00093
00094 switch(type) {
00095 case CountStat:
00096 s = (Statistic *)(new CntStatistic(this));
00097 break;
00098 case TimerStat:
00099 s = (Statistic *)(new TimeStatistic(this));
00100 break;
00101 default:
00102 fprintf(stderr,"Error adding statistic %s: type %d not recognized\n",
00103 name.c_str(), type);
00104 return;
00105 }
00106
00107 stats_[name] = s;
00108 }
00109
00110 void StatContainer::startTimer(std::string name)
00111 {
00112 TimeStatistic *timer = dynamic_cast<TimeStatistic *>(stats_[name]);
00113 if (!timer) return;
00114 timer->start();
00115 }
00116
00117 void StatContainer::stopTimer(std::string name) {
00118 TimeStatistic *timer = dynamic_cast<TimeStatistic *>(stats_[name]);
00119 if (!timer) return;
00120 timer->stop();
00121 }
00122
00123 void StatContainer::incrementCounter(std::string name) {
00124 CntStatistic *counter = dynamic_cast<CntStatistic *>(stats_[name]);
00125 if (!counter) return;
00126 (*counter)++;
00127 }
00128
00129 void StatContainer::decrementCounter(std::string name) {
00130 CntStatistic *counter = dynamic_cast<CntStatistic *>(stats_[name]);
00131 if (!counter) return;
00132 (*counter)--;
00133 }
00134
00135 void StatContainer::addCounter(std::string name, int val) {
00136 CntStatistic *counter = dynamic_cast<CntStatistic *>(stats_[name]);
00137 if (!counter) return;
00138 (*counter) += val;
00139 }
00140
00141
00142 CntStatistic
00143 CntStatistic::operator++( int )
00144 {
00145 CntStatistic ret = *this;
00146 ++(*this);
00147 return ret;
00148 }
00149
00150
00151 CntStatistic&
00152 CntStatistic::operator++()
00153 {
00154 cnt_++;
00155 return *this;
00156 }
00157
00158 CntStatistic
00159 CntStatistic::operator--( int )
00160 {
00161 CntStatistic ret = *this;
00162 --(*this);
00163 return ret;
00164 }
00165
00166 CntStatistic&
00167 CntStatistic::operator--()
00168 {
00169 cnt_--;
00170 return *this;
00171 }
00172
00173 CntStatistic&
00174 CntStatistic::operator=( long int v )
00175 {
00176 cnt_ = v;
00177 return *this;
00178 }
00179
00180 CntStatistic&
00181 CntStatistic::operator=(CntStatistic & v )
00182 {
00183 if( this != &v ) {
00184 cnt_ = v.cnt_;
00185 }
00186 return *this;
00187 }
00188
00189 CntStatistic&
00190 CntStatistic::operator+=( long int v )
00191 {
00192 cnt_ += v;
00193 return *this;
00194 }
00195
00196 CntStatistic&
00197 CntStatistic::operator+=( CntStatistic & v )
00198 {
00199 cnt_ += v.cnt_;
00200 return *this;
00201 }
00202
00203
00204 CntStatistic&
00205 CntStatistic::operator-=( long int v )
00206 {
00207 cnt_ -= v;
00208 return *this;
00209 }
00210
00211 CntStatistic&
00212 CntStatistic::operator-=( CntStatistic & v)
00213 {
00214 cnt_ -= v.cnt_;
00215 return *this;
00216 }
00217
00218 inline long int
00219 CntStatistic::operator*()
00220 {
00221 return cnt_;
00222 }
00223
00224 long int
00225 CntStatistic::value()
00226 {
00227 return cnt_;
00228 }
00229
00230
00231 TimeStatistic&
00232 TimeStatistic::operator=(TimeStatistic & t)
00233 {
00234 if( this != &t ) {
00235 t_ = t.t_;
00236 }
00237 return *this;
00238 }
00239
00240 TimeStatistic&
00241 TimeStatistic::operator+=(TimeStatistic & t)
00242 {
00243 t_ += t.t_;
00244 return *this;
00245 }
00246
00247 TimeStatistic&
00248 TimeStatistic::operator+(TimeStatistic & t)
00249 {
00250 TimeStatistic * ret = new TimeStatistic;
00251 *ret = *this;
00252 *ret += t;
00253 return *ret;
00254 }
00255
00256 void
00257 TimeStatistic::clear()
00258 {
00259 t_.clear();
00260 }
00261
00262 void
00263 TimeStatistic::start()
00264 {
00265 t_.start();
00266 }
00267
00268 void
00269 TimeStatistic::stop()
00270 {
00271 t_.stop();
00272 }
00273
00274 double
00275 TimeStatistic::usecs()
00276 {
00277 return t_.usecs();
00278 }
00279
00280 double
00281 TimeStatistic::ssecs()
00282 {
00283 return t_.ssecs();
00284 }
00285
00286 double
00287 TimeStatistic::wsecs()
00288 {
00289 return t_.wsecs();
00290 }
00291
00292 bool
00293 TimeStatistic::is_running()
00294 {
00295 return t_.is_running();
00296 }
00297