00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
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
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* )
00115 {
00116 est_->timeout(0);
00117 }
00118
00119 void Estimator::trace(TracedVar* v)
00120 {
00121 char wrk[500];
00122 double *p, newval;
00123
00124
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
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 }