GlobalStatistics.cc
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
00024 #include <omnetpp.h>
00025
00026 #include "GlobalStatistics.h"
00027
00028 Define_Module(GlobalStatistics);
00029
00030 using namespace std;
00031
00032 void GlobalStatistics::initialize()
00033 {
00034 sentKBRTestAppMessages = 0;
00035 deliveredKBRTestAppMessages = 0;
00036
00037 nodesInitialized = nodesFinished = 0;
00038
00039 measuring = par("measureNetwInitPhase");
00040 measureStartTime = 0;
00041
00042 currentDeliveryVector.setName("Current Delivery Ratio");
00043
00044
00045 globalStatTimerInterval = par("globalStatTimerInterval");
00046
00047 if (globalStatTimerInterval > 0) {
00048 globalStatTimer = new cMessage("globalStatTimer");
00049 scheduleAt(simTime() + globalStatTimerInterval, globalStatTimer);
00050 }
00051
00052 WATCH(measuring);
00053 WATCH(measureStartTime);
00054 WATCH(currentDeliveryVector);
00055 }
00056
00057 void GlobalStatistics::startMeasuring()
00058 {
00059 if (!measuring) {
00060 measuring = true;
00061 measureStartTime = simTime();
00062 }
00063 }
00064
00065
00066 void GlobalStatistics::handleMessage(cMessage* msg)
00067 {
00068 if (msg == globalStatTimer) {
00069
00070 scheduleAt(simTime() + globalStatTimerInterval, msg);
00071
00072 double ratio;
00073
00074
00075 if (sentKBRTestAppMessages == 0) {
00076 ratio = 0;
00077 } else {
00078 ratio = (double)deliveredKBRTestAppMessages /
00079 (double)sentKBRTestAppMessages;
00080 }
00081
00082 if (ratio > 1) ratio = 1;
00083
00084 currentDeliveryVector.record(ratio);
00085 sentKBRTestAppMessages = 0;
00086 deliveredKBRTestAppMessages = 0;
00087
00088 return;
00089 }
00090
00091 error("GlobalStatistics::handleMessage(): Unknown message type!");
00092 }
00093
00094 void GlobalStatistics::finish()
00095 {
00096 doFinish();
00097 }
00098
00099 void GlobalStatistics::doFinish()
00100 {
00101 if (nodesInitialized != nodesFinished) return;
00102
00103 recordScalar("GlobalStatistics: Simulation Time", simTime());
00104
00105 bool outputMinMax = par("outputMinMax");
00106 bool outputStdDev = par("outputStdDev");
00107
00108
00109 for (map<std::string, cStdDev*>::iterator iter = stdDevMap.begin();
00110 iter != stdDevMap.end(); iter++) {
00111
00112 const std::string& n = iter->first;
00113 const cStatistic& stat = *(iter->second);
00114
00115 recordScalar((n + ".mean").c_str(), stat.getMean());
00116
00117 if (outputStdDev)
00118 recordScalar((n + ".stddev").c_str(), stat.getStddev());
00119
00120 if (outputMinMax) {
00121 recordScalar((n + ".min").c_str(), stat.getMin());
00122 recordScalar((n + ".max").c_str(), stat.getMax());
00123 }
00124 }
00125
00126 for (map<std::string, OutVector*>::iterator iter = outVectorMap.begin();
00127 iter != outVectorMap.end(); iter++) {
00128
00129 const OutVector& ov = *(iter->second);
00130
00131 double mean = ov.count > 0 ? ov.value / ov.count : 0;
00132
00133 recordScalar(("Vector: " + iter->first + ".mean").c_str(), mean);
00134 }
00135 }
00136
00137 void GlobalStatistics::addStdDev(const std::string& name, double value)
00138 {
00139 if (!measuring) {
00140 return;
00141 }
00142
00143 std::map<std::string, cStdDev*>::iterator sdPos = stdDevMap.find(name);
00144 cStdDev* sd = NULL;
00145
00146 if (sdPos == stdDevMap.end()) {
00147 Enter_Method_Silent();
00148 sd = new cStdDev(name.c_str());
00149 stdDevMap.insert(pair<std::string, cStdDev*>(name, sd));
00150 } else {
00151 sd = sdPos->second;
00152 }
00153
00154 sd->collect(value);
00155 }
00156
00157 void GlobalStatistics::recordOutVector(const std::string& name, double value
00158 )
00159 {
00160 if (!measuring) {
00161 return;
00162 }
00163
00164 std::map<std::string, OutVector*>::iterator ovPos =
00165 outVectorMap.find(name);
00166 OutVector* ov = NULL;
00167
00168 if (ovPos == outVectorMap.end()) {
00169 Enter_Method_Silent();
00170 ov = new OutVector(name);
00171 outVectorMap.insert(pair<std::string, OutVector*>(name, ov));
00172 } else {
00173 ov = ovPos->second;
00174 }
00175
00176 ov->vector.record(value);
00177 ov->value += value;
00178 ov->count++;
00179
00180 #if 0
00181
00182 if (avg != -1) {
00183 std::string name_avg = name + "_avg";
00184 std::map<std::string, OutVector*>::iterator ovPos_avg =
00185 outVectorMap.find(name_avg);
00186 OutVector* ov_avg = NULL;
00187
00188 if (ovPos_avg == outVectorMap.end()) {
00189 Enter_Method_Silent();
00190 ov_avg = new OutVector(name_avg);
00191 outVectorMap.insert(pair<std::string, OutVector*>(name_avg, ov_avg));
00192 } else {
00193 ov_avg = ovPos_avg->second;
00194 }
00195 int div = ((ov_avg->count >= avg) ? (avg - 1) : ov_avg->count);
00196 if (div <= 0) div = 1;
00197
00198 double newValue = (ov_avg->avg * div + value) / (div + 1);
00199 ov_avg->vector.record(newValue);
00200 ov_avg->avg = newValue;
00201 ov_avg->value += newValue;
00202 ov_avg->count++;
00203 }
00204 #endif
00205
00206 }
00207
00208 simtime_t GlobalStatistics::calcMeasuredLifetime(simtime_t creationTime)
00209 {
00210 return simTime() - ((creationTime > measureStartTime)
00211 ? creationTime : measureStartTime);
00212 }
00213
00214 GlobalStatistics::~GlobalStatistics()
00215 {
00216
00217 for (map<std::string, cStdDev*>::iterator iter = stdDevMap.begin();
00218 iter != stdDevMap.end(); iter++) {
00219 delete iter->second;
00220 }
00221 stdDevMap.clear();
00222
00223 for (map<std::string, OutVector*>::iterator iter = outVectorMap.begin();
00224 iter != outVectorMap.end(); iter++) {
00225 delete iter->second;
00226 }
00227 outVectorMap.clear();
00228 }
00229