SimTracer.cc

00001 /* -*- mode:c++ -*- ********************************************************
00002  * file:        SimTracer.cc
00003  *
00004  * author:      Jerome Rousselot
00005  *
00006  * copyright:   (C) 2007-2009 CSEM SA, Neuchatel, Switzerland
00007  *
00008  *              This program is free software; you can redistribute it
00009  *              and/or modify it under the terms of the GNU General Public
00010  *              License as published by the Free Software Foundation; either
00011  *              version 2 of the License, or (at your option) any later
00012  *              version.
00013  *              For further information see file COPYING
00014  *              in the top level directory
00015  *
00016  * Funding: This work was partially financed by the European Commission under the
00017  * Framework 6 IST Project "Wirelessly Accessible Sensor Populations"
00018  * (WASP) under contract IST-034963.
00019  ***************************************************************************
00020  * part of:    Modifications to the MF-2 framework by CSEM
00021  **************************************************************************/
00022 
00023 
00024 #include "SimTracer.h"
00025 
00026 
00027 Define_Module(SimTracer);
00028 
00029 /*
00030  * Open some log files and write some static initializations stuff.
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     // retrieve pointer to BaseWorldUtility module
00055     world = check_and_cast<BaseWorldUtility*>(cSimulation::getActiveSimulation()->getModuleByPath("sim.world"));
00056     catPacket = world->subscribe(this, &packet, -1);
00057 
00058 //  } else if(stage == 1) {  // it seems that we are initialized only once. Why ?
00059   }
00060 }
00061 
00062 // compute current average sensor power consumption
00063 double SimTracer::getAvgSensorPowerConsumption() {
00064   double sensorAvgP = 0;
00065   int nbSensors = 0;
00066   map < unsigned long, double >::iterator iter = powerConsumptions.begin(); // address, powerConsumption
00067   for (; iter != powerConsumptions.end(); iter++) { // iterate over all nodes power consumptions
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  * Close the nam log file.
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  * Record a line into the nam log file.
00112  */
00113 void SimTracer::namLog(string namString)
00114 {
00115   //Enter_Method_Silent();
00116   //namFile << namString << endl;
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   radioEnergyFile << mac << "\t" << state << "\t" << duration << "\t" << power
00125     << endl;
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   //  nbApplPacketsSent = nbApplPacketsSent + packet.getNbPacketsSent();
00157   //  nbApplPacketsReceived = nbApplPacketsReceived + packet.getNbPacketsReceived();
00158     if(packet.isSent()) {
00159       nbApplPacketsSent = nbApplPacketsSent + 1;
00160     } else {
00161       nbApplPacketsReceived = nbApplPacketsReceived + 1;
00162     }
00163     goodputVec.record(((double) nbApplPacketsReceived)/nbApplPacketsSent);
00164   }
00165 }
00166