ThruputMeter.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2005 Andras Varga
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 
00019 #include "ThruputMeter.h"
00020 
00021 Define_Module(ThruputMeter);
00022 
00023 
00024 void ThruputMeter::initialize()
00025 {
00026     startTime = par("startTime");
00027     long _batchSize = par("batchSize");
00028     if((_batchSize < 0) || (((long)(unsigned int)_batchSize) != _batchSize))
00029         throw cRuntimeError("invalid 'batchSize=%ld' parameter at '%s' module",_batchSize, getFullPath().c_str());
00030     batchSize = (unsigned int)_batchSize;
00031     maxInterval = par("maxInterval");
00032 
00033     numPackets = numBits = 0;
00034     intvlStartTime = intvlLastPkTime = 0;
00035     intvlNumPackets = intvlNumBits = 0;
00036 
00037     WATCH(numPackets);
00038     WATCH(numBits);
00039     WATCH(intvlStartTime);
00040     WATCH(intvlNumPackets);
00041     WATCH(intvlNumBits);
00042 
00043     bitpersecVector.setName("thruput (bit/sec)");
00044     pkpersecVector.setName("packet/sec");
00045 }
00046 
00047 void ThruputMeter::handleMessage(cMessage *msg)
00048 {
00049     updateStats(simTime(), PK(msg)->getBitLength());
00050     send(msg, "out");
00051 }
00052 
00053 void ThruputMeter::updateStats(simtime_t now, unsigned long bits)
00054 {
00055     numPackets++;
00056     numBits += bits;
00057 
00058     // packet should be counted to new interval
00059     if (intvlNumPackets >= batchSize || now-intvlStartTime >= maxInterval)
00060         beginNewInterval(now);
00061 
00062     intvlNumPackets++;
00063     intvlNumBits += bits;
00064     intvlLastPkTime = now;
00065 }
00066 
00067 void ThruputMeter::beginNewInterval(simtime_t now)
00068 {
00069     simtime_t duration = now - intvlStartTime;
00070 
00071     // record measurements
00072     double bitpersec = intvlNumBits/duration.dbl();
00073     double pkpersec = intvlNumPackets/duration.dbl();
00074 
00075     bitpersecVector.recordWithTimestamp(intvlStartTime, bitpersec);
00076     pkpersecVector.recordWithTimestamp(intvlStartTime, pkpersec);
00077 
00078     // restart counters
00079     intvlStartTime = now;  // FIXME this should be *beginning* of tx of this packet, not end!
00080     intvlNumPackets = intvlNumBits = 0;
00081 }
00082 
00083 void ThruputMeter::finish()
00084 {
00085     simtime_t duration = simTime() - startTime;
00086 
00087     recordScalar("duration", duration);
00088     recordScalar("total packets", numPackets);
00089     recordScalar("total bits", numBits);
00090 
00091     recordScalar("avg throughput (bit/s)", numBits/duration.dbl());
00092     recordScalar("avg packets/s", numPackets/duration.dbl());
00093 }
00094 
00095