OverSim
GlobalStatistics.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2007 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #include <omnetpp.h>
25 
26 #include "GlobalStatistics.h"
27 
29 
30 using namespace std;
31 
32 const double GlobalStatistics::MIN_MEASURED = 0.1;
33 
35 {
36  sentKBRTestAppMessages = 0;
37  deliveredKBRTestAppMessages = 0;
38 
39  measuring = measureNetwInitPhase = par("measureNetwInitPhase");
40  measureStartTime = 0;
41 
42  currentDeliveryVector.setName("Current Delivery Ratio");
43 
44  // start periodic globalStatTimer
45  globalStatTimerInterval = par("globalStatTimerInterval");
46 
47  if (globalStatTimerInterval > 0) {
48  globalStatTimer = new cMessage("globalStatTimer");
49  scheduleAt(simTime() + globalStatTimerInterval, globalStatTimer);
50  }
51 
52  WATCH(measuring);
53  WATCH(measureStartTime);
54  WATCH(currentDeliveryVector);
55 }
56 
58 {
59  if (!measuring) {
60  measuring = true;
61  measureStartTime = simTime();
62  }
63 }
64 
65 
67 {
68  if (msg == globalStatTimer) {
69  // schedule next timer event
70  scheduleAt(simTime() + globalStatTimerInterval, msg);
71 
72  double ratio;
73 
74  // quick hack for live display of the current KBR delivery ratio
75  if (sentKBRTestAppMessages == 0) {
76  ratio = 0;
77  } else {
78  ratio = (double)deliveredKBRTestAppMessages /
79  (double)sentKBRTestAppMessages;
80  }
81 
82  if (ratio > 1) ratio = 1;
83 
84  currentDeliveryVector.record(ratio);
85  sentKBRTestAppMessages = 0;
86  deliveredKBRTestAppMessages = 0;
87 
88  return;
89  }
90 
91  error("GlobalStatistics::handleMessage(): Unknown message type!");
92 }
93 
95 {
96  // Here, the FinisherModule is created which will get destroyed at last.
97  // This way, all other modules have sent their statistical data to the
98  // GobalStatisticModule before GlobalStatistics::finalizeStatistics()
99  // is called by FinisherModule::finish()
100  cModuleType* moduleType = cModuleType::get("oversim.common.FinisherModule");
101  moduleType->create("finisherModule", getParentModule()->getParentModule());
102 }
103 
104 
106 {
107  recordScalar("GlobalStatistics: Simulation Time", simTime());
108 
109  bool outputMinMax = par("outputMinMax");
110  bool outputStdDev = par("outputStdDev");
111 
112  // record stats from other modules
113  for (map<std::string, cStdDev*>::iterator iter = stdDevMap.begin();
114  iter != stdDevMap.end(); iter++) {
115 
116  const std::string& n = iter->first;
117  const cStatistic& stat = *(iter->second);
118 
119  recordScalar((n + ".mean").c_str(), stat.getMean());
120 
121  if (outputStdDev)
122  recordScalar((n + ".stddev").c_str(), stat.getStddev());
123 
124  if (outputMinMax) {
125  recordScalar((n + ".min").c_str(), stat.getMin());
126  recordScalar((n + ".max").c_str(), stat.getMax());
127  }
128  }
129 
130  for (map<std::string, cHistogram*>::iterator iter = histogramMap.begin();
131  iter != histogramMap.end(); iter++) {
132  const std::string& n = iter->first;
133  recordStatistic(n.c_str(), iter->second);
134  }
135 
136  for (map<std::string, OutVector*>::iterator iter = outVectorMap.begin();
137  iter != outVectorMap.end(); iter++) {
138  const OutVector& ov = *(iter->second);
139  double mean = ov.count > 0 ? ov.value / ov.count : 0;
140  recordScalar(("Vector: " + iter->first + ".mean").c_str(), mean);
141  }
142 }
143 
144 void GlobalStatistics::addStdDev(const std::string& name, double value)
145 {
146  if (!measuring) {
147  return;
148  }
149 
150  std::map<std::string, cStdDev*>::iterator sdPos = stdDevMap.find(name);
151  cStdDev* sd = NULL;
152 
153  if (sdPos == stdDevMap.end()) {
154  Enter_Method_Silent();
155  sd = new cStdDev(name.c_str());
156  stdDevMap.insert(pair<std::string, cStdDev*>(name, sd));
157  } else {
158  sd = sdPos->second;
159  }
160 
161  sd->collect(value);
162 }
163 
164 void GlobalStatistics::recordHistogram(const std::string& name, double value)
165 {
166  if (!measuring) {
167  return;
168  }
169 
170  std::map<std::string, cHistogram*>::iterator hPos = histogramMap.find(name);
171  cHistogram* h = NULL;
172 
173  if (hPos == histogramMap.end()) {
174  Enter_Method_Silent();
175  h = new cHistogram(name.c_str());
176  histogramMap.insert(pair<std::string, cHistogram*>(name, h));
177  } else {
178  h = hPos->second;
179  }
180 
181  h->collect(value);
182 }
183 
184 void GlobalStatistics::recordOutVector(const std::string& name, double value)
185 {
186  if (!measuring) {
187  return;
188  }
189 
190  std::map<std::string, OutVector*>::iterator ovPos =
191  outVectorMap.find(name);
192  OutVector* ov = NULL;
193 
194  if (ovPos == outVectorMap.end()) {
195  Enter_Method_Silent();
196  ov = new OutVector(name);
197  outVectorMap.insert(pair<std::string, OutVector*>(name, ov));
198  } else {
199  ov = ovPos->second;
200  }
201 
202  ov->vector.record(value);
203  ov->value += value;
204  ov->count++;
205 }
206 
207 simtime_t GlobalStatistics::calcMeasuredLifetime(simtime_t creationTime)
208 {
209  return simTime() - ((creationTime > measureStartTime)
210  ? creationTime : measureStartTime);
211 }
212 
214 {
215  // deallocate vectors
216  for (map<std::string, cStdDev*>::iterator it = stdDevMap.begin();
217  it != stdDevMap.end(); it++) {
218  delete it->second;
219  }
220  stdDevMap.clear();
221 
222  for (map<std::string, OutVector*>::iterator it = outVectorMap.begin();
223  it != outVectorMap.end(); it++) {
224  delete it->second;
225  }
226  outVectorMap.clear();
227 
228  for (map<std::string, cHistogram*>::iterator iter = histogramMap.begin();
229  iter != histogramMap.end(); iter++) {
230  delete iter->second;
231  }
232  histogramMap.clear();
233 }
234