00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "SimTracer.h"
00025
00026
00027 Define_Module(SimTracer);
00028
00029
00030
00031
00032 void SimTracer::initialize(int stage)
00033 {
00034 cSimpleModule::initialize(stage);
00035 if (stage == 0) {
00036
00037 char treeName[250];
00038 int n;
00039 n = sprintf(treeName, "results/tree-%d.txt",
00040 cSimulation::getActiveSimulation()->getEnvir()->getConfigEx()->getActiveRunNumber());
00041 treeFile.open(treeName);
00042 if (!treeFile) {
00043 EV << "Error opening output stream for routing tree statistics."
00044 << endl;
00045 } else {
00046 treeFile << "graph aRoutingTree " << endl << "{" << endl;
00047 }
00048 goodputVec.setName("goodput");
00049 pSinkVec.setName("sinkPowerConsumption");
00050 pSensorVec.setName("sensorPowerConsumption");
00051 nbApplPacketsSent = 0;
00052 nbApplPacketsReceived = 0;
00053
00054
00055 world = check_and_cast<BaseWorldUtility*>(cSimulation::getActiveSimulation()->getModuleByPath("sim.world"));
00056 catPacket = world->subscribe(this, &packet, -1);
00057
00058
00059 }
00060 }
00061
00062
00063 double SimTracer::getAvgSensorPowerConsumption() {
00064 double sensorAvgP = 0;
00065 int nbSensors = 0;
00066 map < unsigned long, double >::iterator iter = powerConsumptions.begin();
00067 for (; iter != powerConsumptions.end(); iter++) {
00068 double eval = iter->second;
00069 eval = eval + (simTime()-lastUpdates[iter->first]).dbl()*currPower[iter->first];
00070 eval = eval * 1000 / simTime();
00071 if(iter->first != 0) {
00072 nbSensors++;
00073 sensorAvgP += eval;
00074 }
00075 }
00076 sensorAvgP = sensorAvgP / nbSensors;
00077 return sensorAvgP;
00078 }
00079
00080 double SimTracer::getSinkPowerConsumption() {
00081 double sinkP = powerConsumptions[0];
00082 sinkP = sinkP + (simTime() - lastUpdates[0]).dbl() * currPower[0];
00083 sinkP = sinkP * 1000 / simTime();
00084 return sinkP;
00085 }
00086
00087
00088
00089
00090 void SimTracer::finish()
00091 {
00092 double goodput = 0;
00093 if(nbApplPacketsSent > 0) {
00094 goodput = ((double) nbApplPacketsReceived)/nbApplPacketsSent;
00095 } else {
00096 goodput = 0;
00097 }
00098 recordScalar("Application Packet Success Rate", goodput);
00099 recordScalar("Application packets received", nbApplPacketsReceived);
00100 recordScalar("Application packets sent", nbApplPacketsSent);
00101 recordScalar("Sink power consumption", getSinkPowerConsumption());
00102 recordScalar("Sensor average power consumption", getAvgSensorPowerConsumption());
00103 if (treeFile) {
00104 treeFile << "}" << endl;
00105 treeFile.close();
00106 }
00107
00108 }
00109
00110
00111
00112
00113 void SimTracer::namLog(string namString)
00114 {
00115
00116
00117 }
00118
00119 void SimTracer::radioEnergyLog(unsigned long mac, int state,
00120 simtime_t duration, double power, double newPower)
00121 {
00122 Enter_Method_Silent();
00123
00124
00125
00126
00127 if (powerConsumptions.count(mac) == 0) {
00128 powerConsumptions[mac] = 0;
00129 }
00130 powerConsumptions[mac] = powerConsumptions[mac] + power * duration.dbl();
00131 currPower[mac] = newPower;
00132 lastUpdates[mac] = simTime();
00133 if(mac != 0) {
00134 pSensorVec.record(getAvgSensorPowerConsumption());
00135 } else {
00136 pSinkVec.record(getSinkPowerConsumption());
00137 }
00138 }
00139
00140
00141
00142 void SimTracer::logLink(int parent, int child)
00143 {
00144 treeFile << " " << parent << " -- " << child << " ;" << endl;
00145 }
00146
00147 void SimTracer::logPosition(int node, double x, double y)
00148 {
00149 treeFile << node << "[pos=\""<< x << ", " << y << "!\"];" << endl;
00150 }
00151
00152 void SimTracer::receiveBBItem(int category, const BBItem * details,
00153 int scopeModuleId) {
00154 if (category == catPacket) {
00155 packet = *(static_cast<const Packet*>(details));
00156
00157
00158 if(packet.isSent()) {
00159 nbApplPacketsSent = nbApplPacketsSent + 1;
00160 } else {
00161 nbApplPacketsReceived = nbApplPacketsReceived + 1;
00162 }
00163 goodputVec.record(((double) nbApplPacketsReceived)/nbApplPacketsSent);
00164 }
00165 }
00166