00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef lint
00021 static const char rcsid[] =
00022 "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/adc/estimator.cc,v 1.7 1999/03/13 03:52:47 haoboy Exp $";
00023 #endif
00024
00025 #include "estimator.h"
00026
00027 Estimator::Estimator() : meas_mod_(0),avload_(0.0),est_timer_(this), measload_(0.0), tchan_(0), omeasload_(0), oavload_(0)
00028 {
00029 bind("period_",&period_);
00030 bind("src_", &src_);
00031 bind("dst_", &dst_);
00032
00033 avload_.tracer(this);
00034 avload_.name("\"Estimated Util.\"");
00035 measload_.tracer(this);
00036 measload_.name("\"Measured Util.\"");
00037 }
00038
00039 int Estimator::command(int argc, const char*const* argv)
00040 {
00041 Tcl& tcl = Tcl::instance();
00042 if (argc==2) {
00043 if (strcmp(argv[1],"load-est") == 0) {
00044 tcl.resultf("%.3f",double(avload_));
00045 return(TCL_OK);
00046 } else if (strcmp(argv[1],"link-utlzn") == 0) {
00047 tcl.resultf("%.3f",meas_mod_->bitcnt()/period_);
00048 return(TCL_OK);
00049 }
00050 }
00051 if (argc == 3) {
00052 if (strcmp(argv[1], "attach") == 0) {
00053 int mode;
00054 const char* id = argv[2];
00055 tchan_ = Tcl_GetChannel(tcl.interp(), (char*)id, &mode);
00056 if (tchan_ == 0) {
00057 tcl.resultf("Estimator: trace: can't attach %s for writing", id);
00058 return (TCL_ERROR);
00059 }
00060 return (TCL_OK);
00061 }
00062 if (strcmp(argv[1], "setbuf") == 0) {
00063
00064 return(TCL_OK);
00065 }
00066 }
00067 return NsObject::command(argc,argv);
00068 }
00069
00070 void Estimator::setmeasmod (MeasureMod *measmod)
00071 {
00072 meas_mod_=measmod;
00073 }
00074
00075 void Estimator::start()
00076 {
00077 avload_=0;
00078 measload_ = 0;
00079 est_timer_.resched(period_);
00080 }
00081
00082 void Estimator::stop()
00083 {
00084 est_timer_.cancel();
00085 }
00086
00087 void Estimator::timeout(int)
00088 {
00089 estimate();
00090 est_timer_.resched(period_);
00091 }
00092
00093 void Estimator_Timer::expire(Event* )
00094 {
00095 est_->timeout(0);
00096 }
00097
00098 void Estimator::trace(TracedVar* v)
00099 {
00100 char wrk[500];
00101 double *p, newval;
00102
00103
00104 if (strcmp(v->name(), "\"Estimated Util.\"") == 0) {
00105 p = &oavload_;
00106 }
00107 else if (strcmp(v->name(), "\"Measured Util.\"") == 0) {
00108 p = &omeasload_;
00109 }
00110 else {
00111 fprintf(stderr, "Estimator: unknown trace var %s\n", v->name());
00112 return;
00113 }
00114
00115 newval = double(*((TracedDouble*)v));
00116
00117 if (tchan_) {
00118 int n;
00119 double t = Scheduler::instance().clock();
00120
00121 sprintf(wrk, "f -t %g -s %d -a %s:%d-%d -T v -n %s -v %g -o %g",
00122 t, src_, actype_, src_, dst_, v->name(), newval, *p);
00123 n = strlen(wrk);
00124 wrk[n] = '\n';
00125 wrk[n+1] = 0;
00126 (void)Tcl_Write(tchan_, wrk, n+1);
00127
00128 }
00129
00130 *p = newval;
00131
00132 return;
00133
00134
00135
00136 }
00137
00138 void Estimator::setactype(const char* type)
00139 {
00140 actype_ = new char[strlen(type)+1];
00141 strcpy(actype_, type);
00142 return;
00143 }