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 void GlobalStatistics::initialize()
00033 {
00034     sentKBRTestAppMessages = 0;
00035     deliveredKBRTestAppMessages = 0;
00036 
00037     measuring = par("measureNetwInitPhase");
00038     measureStartTime = 0;
00039 
00040     currentDeliveryVector.setName("Current Delivery Ratio");
00041 
00042     // start periodic globalStatTimer
00043     globalStatTimerInterval = par("globalStatTimerInterval");
00044 
00045     if (globalStatTimerInterval > 0) {
00046         globalStatTimer = new cMessage("globalStatTimer");
00047         scheduleAt(simTime() + globalStatTimerInterval, globalStatTimer);
00048     }
00049 
00050     WATCH(measuring);
00051     WATCH(measureStartTime);
00052     WATCH(currentDeliveryVector);
00053 }
00054 
00055 void GlobalStatistics::startMeasuring()
00056 {
00057     if (!measuring) {
00058         measuring = true;
00059         measureStartTime = simTime();
00060     }
00061 }
00062 
00063 
00064 void GlobalStatistics::handleMessage(cMessage* msg)
00065 {
00066     if (msg == globalStatTimer) {
00067         // schedule next timer event
00068         scheduleAt(simTime() + globalStatTimerInterval, msg);
00069 
00070         double ratio;
00071 
00072         // quick hack for live display of the current KBR delivery ratio
00073         if (sentKBRTestAppMessages == 0) {
00074             ratio = 0;
00075         } else {
00076             ratio = (double)deliveredKBRTestAppMessages /
00077             (double)sentKBRTestAppMessages;
00078         }
00079 
00080         if (ratio > 1) ratio = 1;
00081 
00082         currentDeliveryVector.record(ratio);
00083         sentKBRTestAppMessages = 0;
00084         deliveredKBRTestAppMessages = 0;
00085 
00086         return;
00087     }
00088 
00089     error("GlobalStatistics::handleMessage(): Unknown message type!");
00090 }
00091 
00092 void GlobalStatistics::finish()
00093 {
00094     // Here, the FinisherModule is created which will get destroyed at last.
00095     // This way, all other modules have sent their statistical data to the
00096     // GobalStatisticModule before GlobalStatistics::finalizeStatistics()
00097     // is called by FinisherModule::finish()
00098     cModuleType* moduleType = cModuleType::get("oversim.common.FinisherModule");
00099     moduleType->create("finisherModule", getParentModule()->getParentModule());
00100 }
00101 
00102 
00103 void GlobalStatistics::finalizeStatistics()
00104 {
00105     recordScalar("GlobalStatistics: Simulation Time", simTime());
00106 
00107     bool outputMinMax = par("outputMinMax");
00108     bool outputStdDev = par("outputStdDev");
00109 
00110     // record stats from other modules
00111     for (map<std::string, cStdDev*>::iterator iter = stdDevMap.begin();
00112     iter != stdDevMap.end(); iter++) {
00113 
00114         const std::string& n = iter->first;
00115         const cStatistic& stat = *(iter->second);
00116 
00117         recordScalar((n + ".mean").c_str(), stat.getMean());
00118 
00119         if (outputStdDev)
00120             recordScalar((n + ".stddev").c_str(), stat.getStddev());
00121 
00122         if (outputMinMax) {
00123             recordScalar((n + ".min").c_str(), stat.getMin());
00124             recordScalar((n + ".max").c_str(), stat.getMax());
00125         }
00126     }
00127 
00128     for (map<std::string, OutVector*>::iterator iter = outVectorMap.begin();
00129     iter != outVectorMap.end(); iter++) {
00130         const OutVector& ov = *(iter->second);
00131         double mean = ov.count > 0 ? ov.value / ov.count : 0;
00132         recordScalar(("Vector: " + iter->first + ".mean").c_str(), mean);
00133     }
00134 }
00135 
00136 void GlobalStatistics::addStdDev(const std::string& name, double value)
00137 {
00138     if (!measuring) {
00139         return;
00140     }
00141 
00142     std::map<std::string, cStdDev*>::iterator sdPos = stdDevMap.find(name);
00143     cStdDev* sd = NULL;
00144 
00145     if (sdPos == stdDevMap.end()) {
00146         Enter_Method_Silent();
00147         sd = new cStdDev(name.c_str());
00148         stdDevMap.insert(pair<std::string, cStdDev*>(name, sd));
00149     } else {
00150         sd = sdPos->second;
00151     }
00152 
00153     sd->collect(value);
00154 }
00155 
00156 void GlobalStatistics::recordOutVector(const std::string& name, double value
00157                                        /*, int avg*/)
00158 {
00159     if (!measuring) {
00160         return;
00161     }
00162 
00163     std::map<std::string, OutVector*>::iterator ovPos =
00164         outVectorMap.find(name);
00165     OutVector* ov = NULL;
00166 
00167     if (ovPos == outVectorMap.end()) {
00168         Enter_Method_Silent();
00169         ov = new OutVector(name);
00170         outVectorMap.insert(pair<std::string, OutVector*>(name, ov));
00171     } else {
00172         ov = ovPos->second;
00173     }
00174 
00175     ov->vector.record(value);
00176     ov->value += value;
00177     ov->count++;
00178 
00179 #if 0
00180     // avg vector
00181     if (avg != -1) {
00182         std::string name_avg = name + "_avg";
00183         std::map<std::string, OutVector*>::iterator ovPos_avg =
00184             outVectorMap.find(name_avg);
00185         OutVector* ov_avg = NULL;
00186 
00187         if (ovPos_avg == outVectorMap.end()) {
00188             Enter_Method_Silent();
00189             ov_avg = new OutVector(name_avg);
00190             outVectorMap.insert(pair<std::string, OutVector*>(name_avg, ov_avg));
00191         } else {
00192             ov_avg = ovPos_avg->second;
00193         }
00194         int div = ((ov_avg->count >= avg) ? (avg - 1) : ov_avg->count);
00195         if (div <= 0) div = 1;
00196 
00197         double newValue = (ov_avg->avg * div + value) / (div + 1);
00198         ov_avg->vector.record(newValue);
00199         ov_avg->avg = newValue;
00200         ov_avg->value += newValue;
00201         ov_avg->count++;
00202     }
00203 #endif
00204 
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 iter = stdDevMap.begin();
00217     iter != stdDevMap.end(); iter++) {
00218         delete iter->second;
00219     }
00220     stdDevMap.clear();
00221 
00222     for (map<std::string, OutVector*>::iterator iter = outVectorMap.begin();
00223     iter != outVectorMap.end(); iter++) {
00224         delete iter->second;
00225     }
00226     outVectorMap.clear();
00227 }
00228 
Generated on Wed May 26 16:21:14 2010 for OverSim by  doxygen 1.6.3