estimator.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) Xerox Corporation 1997. All rights reserved.
00004  *
00005  * This program is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU General Public License as published by the
00007  * Free Software Foundation; either version 2 of the License, or (at your
00008  * option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, write to the Free Software Foundation, Inc.,
00017  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  * Linking this file statically or dynamically with other modules is making
00020  * a combined work based on this file.  Thus, the terms and conditions of
00021  * the GNU General Public License cover the whole combination.
00022  *
00023  * In addition, as a special exception, the copyright holders of this file
00024  * give you permission to combine this file with free software programs or
00025  * libraries that are released under the GNU LGPL and with code included in
00026  * the standard release of ns-2 under the Apache 2.0 license or under
00027  * otherwise-compatible licenses with advertising requirements (or modified
00028  * versions of such code, with unchanged license).  You may copy and
00029  * distribute such a system following the terms of the GNU GPL for this
00030  * file and the licenses of the other code concerned, provided that you
00031  * include the source code of that other code when and as the GNU GPL
00032  * requires distribution of source code.
00033  *
00034  * Note that people who make modified versions of this file are not
00035  * obligated to grant this special exception for their modified versions;
00036  * it is their choice whether to do so.  The GNU General Public License
00037  * gives permission to release a modified version without this exception;
00038  * this exception also makes it possible to release a modified version
00039  * which carries forward this exception.
00040  */
00041 #ifndef lint
00042 static const char rcsid[] =
00043     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/adc/estimator.cc,v 1.8 2005/08/26 05:05:27 tomh Exp $";
00044 #endif
00045 
00046 #include "estimator.h"
00047 
00048 Estimator::Estimator() : meas_mod_(0),avload_(0.0),est_timer_(this), measload_(0.0), tchan_(0), omeasload_(0), oavload_(0)
00049 {
00050     bind("period_",&period_);
00051     bind("src_", &src_);
00052     bind("dst_", &dst_);
00053 
00054     avload_.tracer(this);
00055     avload_.name("\"Estimated Util.\"");
00056     measload_.tracer(this);
00057     measload_.name("\"Measured Util.\"");
00058 }
00059 
00060 int Estimator::command(int argc, const char*const* argv)
00061 {
00062     Tcl& tcl = Tcl::instance();
00063     if (argc==2) {
00064         if (strcmp(argv[1],"load-est") == 0) {
00065             tcl.resultf("%.3f",double(avload_));
00066             return(TCL_OK);
00067         } else if (strcmp(argv[1],"link-utlzn") == 0) {
00068             tcl.resultf("%.3f",meas_mod_->bitcnt()/period_);
00069             return(TCL_OK);
00070         }
00071     }
00072     if (argc == 3) {
00073         if (strcmp(argv[1], "attach") == 0) {
00074             int mode;
00075             const char* id = argv[2];
00076             tchan_ = Tcl_GetChannel(tcl.interp(), (char*)id, &mode);
00077             if (tchan_ == 0) {
00078                 tcl.resultf("Estimator: trace: can't attach %s for writing", id);
00079                 return (TCL_ERROR);
00080             }
00081             return (TCL_OK);
00082         }
00083         if (strcmp(argv[1], "setbuf") == 0) {
00084             /* some sub classes actually do something here */
00085             return(TCL_OK);
00086         }
00087     }
00088     return NsObject::command(argc,argv);
00089 }
00090 
00091 void Estimator::setmeasmod (MeasureMod *measmod)
00092 {
00093     meas_mod_=measmod;
00094 }
00095 
00096 void Estimator::start()
00097 {
00098     avload_=0;
00099     measload_ = 0;
00100     est_timer_.resched(period_);
00101 }
00102 
00103 void Estimator::stop()
00104 {
00105     est_timer_.cancel();
00106 }
00107 
00108 void Estimator::timeout(int)
00109 {
00110     estimate();
00111     est_timer_.resched(period_);
00112 }
00113 
00114 void Estimator_Timer::expire(Event* /*e*/) 
00115 {
00116     est_->timeout(0);
00117 }
00118 
00119 void Estimator::trace(TracedVar* v)
00120 {
00121     char wrk[500];
00122     double *p, newval;
00123 
00124     /* check for right variable */
00125     if (strcmp(v->name(), "\"Estimated Util.\"") == 0) {
00126         p = &oavload_;
00127     }
00128     else if (strcmp(v->name(), "\"Measured Util.\"") == 0) {
00129         p = &omeasload_;
00130     }
00131     else {
00132         fprintf(stderr, "Estimator: unknown trace var %s\n", v->name());
00133         return;
00134     }
00135 
00136     newval = double(*((TracedDouble*)v));
00137 
00138     if (tchan_) {
00139         int n;
00140         double t = Scheduler::instance().clock();
00141         /* f -t 0.0 -s 1 -a SA -T v -n Num -v 0 -o 0 */
00142         sprintf(wrk, "f -t %g -s %d -a %s:%d-%d -T v -n %s -v %g -o %g",
00143             t, src_, actype_, src_, dst_, v->name(), newval, *p);
00144         n = strlen(wrk);
00145         wrk[n] = '\n';
00146         wrk[n+1] = 0;
00147         (void)Tcl_Write(tchan_, wrk, n+1);
00148         
00149     }
00150 
00151     *p = newval;
00152 
00153     return;
00154 
00155 
00156 
00157 }
00158 
00159 void Estimator::setactype(const char* type)
00160 {
00161     actype_ = new char[strlen(type)+1];
00162     strcpy(actype_, type);
00163     return;
00164 }

Generated on Tue Mar 6 16:47:45 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6