GlobalStatistics.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2007 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #include <omnetpp.h>
00025 
00026 #include "GlobalStatistics.h"
00027 
00028 Define_Module(GlobalStatistics);
00029 
00030 using namespace std;
00031 
00032 const double GlobalStatistics::MIN_MEASURED = 0.1;
00033 
00034 void GlobalStatistics::initialize()
00035 {
00036     sentKBRTestAppMessages = 0;
00037     deliveredKBRTestAppMessages = 0;
00038 
00039     measuring = par("measureNetwInitPhase");
00040     measureStartTime = 0;
00041 
00042     currentDeliveryVector.setName("Current Delivery Ratio");
00043 
00044     // start periodic globalStatTimer
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         // schedule next timer event
00070         scheduleAt(simTime() + globalStatTimerInterval, msg);
00071 
00072         double ratio;
00073 
00074         // quick hack for live display of the current KBR delivery ratio
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     // Here, the FinisherModule is created which will get destroyed at last.
00097     // This way, all other modules have sent their statistical data to the
00098     // GobalStatisticModule before GlobalStatistics::finalizeStatistics()
00099     // is called by FinisherModule::finish()
00100     cModuleType* moduleType = cModuleType::get("oversim.common.FinisherModule");
00101     moduleType->create("finisherModule", getParentModule()->getParentModule());
00102 }
00103 
00104 
00105 void GlobalStatistics::finalizeStatistics()
00106 {
00107     recordScalar("GlobalStatistics: Simulation Time", simTime());
00108 
00109     bool outputMinMax = par("outputMinMax");
00110     bool outputStdDev = par("outputStdDev");
00111 
00112     // record stats from other modules
00113     for (map<std::string, cStdDev*>::iterator iter = stdDevMap.begin();
00114             iter != stdDevMap.end(); iter++) {
00115 
00116         const std::string& n = iter->first;
00117         const cStatistic& stat = *(iter->second);
00118 
00119         recordScalar((n + ".mean").c_str(), stat.getMean());
00120 
00121         if (outputStdDev)
00122             recordScalar((n + ".stddev").c_str(), stat.getStddev());
00123 
00124         if (outputMinMax) {
00125             recordScalar((n + ".min").c_str(), stat.getMin());
00126             recordScalar((n + ".max").c_str(), stat.getMax());
00127         }
00128     }
00129 
00130     for (map<std::string, cHistogram*>::iterator iter = histogramMap.begin();
00131             iter != histogramMap.end(); iter++) {
00132         const std::string& n = iter->first;
00133         recordStatistic(n.c_str(), iter->second);
00134     }
00135 
00136     for (map<std::string, OutVector*>::iterator iter = outVectorMap.begin();
00137     iter != outVectorMap.end(); iter++) {
00138         const OutVector& ov = *(iter->second);
00139         double mean = ov.count > 0 ? ov.value / ov.count : 0;
00140         recordScalar(("Vector: " + iter->first + ".mean").c_str(), mean);
00141     }
00142 }
00143 
00144 void GlobalStatistics::addStdDev(const std::string& name, double value)
00145 {
00146     if (!measuring) {
00147         return;
00148     }
00149 
00150     std::map<std::string, cStdDev*>::iterator sdPos = stdDevMap.find(name);
00151     cStdDev* sd = NULL;
00152 
00153     if (sdPos == stdDevMap.end()) {
00154         Enter_Method_Silent();
00155         sd = new cStdDev(name.c_str());
00156         stdDevMap.insert(pair<std::string, cStdDev*>(name, sd));
00157     } else {
00158         sd = sdPos->second;
00159     }
00160 
00161     sd->collect(value);
00162 }
00163 
00164 void GlobalStatistics::recordHistogram(const std::string& name, double value)
00165 {
00166     if (!measuring) {
00167         return;
00168     }
00169 
00170     std::map<std::string, cHistogram*>::iterator hPos = histogramMap.find(name);
00171     cHistogram* h = NULL;
00172 
00173     if (hPos == histogramMap.end()) {
00174         Enter_Method_Silent();
00175         h = new cHistogram(name.c_str());
00176         histogramMap.insert(pair<std::string, cHistogram*>(name, h));
00177     } else {
00178         h = hPos->second;
00179     }
00180 
00181     h->collect(value);
00182 }
00183 
00184 void GlobalStatistics::recordOutVector(const std::string& name, double value)
00185 {
00186     if (!measuring) {
00187         return;
00188     }
00189 
00190     std::map<std::string, OutVector*>::iterator ovPos =
00191         outVectorMap.find(name);
00192     OutVector* ov = NULL;
00193 
00194     if (ovPos == outVectorMap.end()) {
00195         Enter_Method_Silent();
00196         ov = new OutVector(name);
00197         outVectorMap.insert(pair<std::string, OutVector*>(name, ov));
00198     } else {
00199         ov = ovPos->second;
00200     }
00201 
00202     ov->vector.record(value);
00203     ov->value += value;
00204     ov->count++;
00205 }
00206 
00207 simtime_t GlobalStatistics::calcMeasuredLifetime(simtime_t creationTime)
00208 {
00209     return simTime() - ((creationTime > measureStartTime)
00210             ? creationTime : measureStartTime);
00211 }
00212 
00213 GlobalStatistics::~GlobalStatistics()
00214 {
00215     // deallocate vectors
00216     for (map<std::string, cStdDev*>::iterator it = stdDevMap.begin();
00217             it != stdDevMap.end(); it++) {
00218         delete it->second;
00219     }
00220     stdDevMap.clear();
00221 
00222     for (map<std::string, OutVector*>::iterator it = outVectorMap.begin();
00223             it != outVectorMap.end(); it++) {
00224         delete it->second;
00225     }
00226     outVectorMap.clear();
00227 
00228     for (map<std::string, cHistogram*>::iterator iter = histogramMap.begin();
00229             iter != histogramMap.end(); iter++) {
00230         delete iter->second;
00231     }
00232     histogramMap.clear();
00233 }
00234