1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
|
/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
* Copyright (c) 1997 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the MASH Research
* Group at the University of California Berkeley.
* 4. Neither the name of the University nor of the Research Group may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
static const char rcsid[] =
"@(#) $Header: /cvsroot/nsnam/ns-2/tools/queue-monitor.cc,v 1.29 2004/10/28 01:21:41 sfloyd Exp $";
#endif
#include "queue-monitor.h"
#include "trace.h"
#include <math.h>
int QueueMonitor::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
if (argc == 2) {
if (strcmp(argv[1], "get-bytes-integrator") == 0) {
if (bytesInt_)
tcl.resultf("%s", bytesInt_->name());
else
tcl.resultf("");
return (TCL_OK);
}
if (strcmp(argv[1], "get-pkts-integrator") == 0) {
if (pktsInt_)
tcl.resultf("%s", pktsInt_->name());
else
tcl.resultf("");
return (TCL_OK);
}
if (strcmp(argv[1], "get-delay-samples") == 0) {
if (delaySamp_)
tcl.resultf("%s", delaySamp_->name());
else
tcl.resultf("");
return (TCL_OK);
}
if (strcmp(argv[1], "printRTTs") == 0) {
if (keepRTTstats_ && channel1_) {
printRTTs();
}
return (TCL_OK);
}
if (strcmp(argv[1], "printSeqnos") == 0) {
if (keepSeqnoStats_ && channel1_) {
printSeqnos();
}
return (TCL_OK);
}
}
if (argc == 3) {
if (strcmp(argv[1], "set-bytes-integrator") == 0) {
bytesInt_ = (Integrator *)
TclObject::lookup(argv[2]);
if (bytesInt_ == NULL)
return (TCL_ERROR);
return (TCL_OK);
}
if (strcmp(argv[1], "set-pkts-integrator") == 0) {
pktsInt_ = (Integrator *)
TclObject::lookup(argv[2]);
if (pktsInt_ == NULL)
return (TCL_ERROR);
return (TCL_OK);
}
if (strcmp(argv[1], "set-delay-samples") == 0) {
delaySamp_ = (Samples*)
TclObject::lookup(argv[2]);
if (delaySamp_ == NULL)
return (TCL_ERROR);
return (TCL_OK);
}
if (strcmp(argv[1], "trace") == 0) {
// for printStats
int mode;
const char* id = argv[2];
channel_ = Tcl_GetChannel(tcl.interp(), (char*)id, &mode);
if (channel_ == 0) {
tcl.resultf("trace: can't attach %s for writing", id);
return (TCL_ERROR);
}
return (TCL_OK);
}
if (strcmp(argv[1], "traceDist") == 0) {
// for printRTTs and printSeqnos distributions
int mode;
const char* id = argv[2];
channel1_ = Tcl_GetChannel(tcl.interp(), (char*)id, &mode);
if (channel1_ == 0) {
tcl.resultf("trace: can't attach %s for writing", id);
return (TCL_ERROR);
}
return (TCL_OK);
}
}
if (argc == 4) {
if (strcmp(argv[1], "set-src-dst") == 0) {
srcId_ = atoi(argv[2]);
dstId_ = atoi(argv[3]);
return (TCL_OK);
}
}
return TclObject::command(argc, argv); // else control reaches end of
// non-void function, see? :-)
}
static class QueueMonitorClass : public TclClass {
public:
QueueMonitorClass() : TclClass("QueueMonitor") {}
TclObject* create(int, const char*const*) {
return (new QueueMonitor());
}
} queue_monitor_class;
void
QueueMonitor::printRTTs() {
int i, n, topBin, MsPerBin;
char wrk[500];
topBin = maxRTT_ * binsPerSec_;
MsPerBin = int(1000/binsPerSec_);
double now = Scheduler::instance().clock();
sprintf(wrk, "Distribution of RTTs, %d ms bins, time %4.2f\n", MsPerBin, now);
n = strlen(wrk); wrk[n] = 0;
(void)Tcl_Write(channel1_, wrk, n);
for (i = 0; i < topBin; i++) {
if (RTTbins_[i] > 0) {
sprintf(wrk, "%d to %d ms: frac %5.3f num %d time %4.2f\n",
i*MsPerBin, (i+1)*MsPerBin,
(double)RTTbins_[i]/numRTTs_,
RTTbins_[i], now);
n = strlen(wrk); wrk[n] = 0;
(void)Tcl_Write(channel1_, wrk, n);
}
}
i = topBin - 1;
if (RTTbins_[i] > 0) {
sprintf(wrk, "The last bin might also contain RTTs >= %d ms.\n",
(i+1)*MsPerBin);
n = strlen(wrk); wrk[n] = 0;
(void)Tcl_Write(channel1_, wrk, n);
}
}
void
QueueMonitor::printSeqnos() {
int i, n, topBin;
char wrk[500];
topBin = int(maxSeqno_ / SeqnoBinSize_);
double now = Scheduler::instance().clock();
sprintf(wrk, "Distribution of Seqnos, %d seqnos per bin, time %4.2f\n",
SeqnoBinSize_, now);
n = strlen(wrk); wrk[n] = 0;
(void)Tcl_Write(channel1_, wrk, n);
for (i = 0; i < topBin; i++) {
if (SeqnoBins_[i] > 0) {
sprintf(wrk, "%d to %d seqnos: frac %5.3f num %d time %4.2f\n",
i*SeqnoBinSize_, (i+1)*SeqnoBinSize_ - 1,
(double)SeqnoBins_[i]/numSeqnos_,
SeqnoBins_[i], now);
n = strlen(wrk); wrk[n] = 0;
(void)Tcl_Write(channel1_, wrk, n);
}
}
i = topBin - 1;
if (SeqnoBins_[i] > 0) {
sprintf(wrk, "The last bin might also contain Seqnos >= %d. \n",
(i+1)*SeqnoBinSize_);
n = strlen(wrk); wrk[n] = 0;
(void)Tcl_Write(channel1_, wrk, n);
}
}
void
QueueMonitor::printStats() {
char wrk[500];
int n;
double now = Scheduler::instance().clock();
sprintf(wrk, "q -t "TIME_FORMAT" -s %d -d %d -l %d -p %d", now, srcId_, dstId_, size_, pkts_);
n = strlen(wrk);
wrk[n] = '\n';
wrk[n+1] = 0;
(void)Tcl_Write(channel_, wrk, n+1);
wrk[n] = 0;
}
// packet arrival to a queue
void QueueMonitor::in(Packet* p)
{
hdr_cmn* hdr = hdr_cmn::access(p);
double now = Scheduler::instance().clock();
int pktsz = hdr->size();
hdr_flags* pf = hdr_flags::access(p);
last_pkt_ = Scheduler::instance().clock();
if (parrivals_ == 0) {
first_pkt_ = last_pkt_;
}
if (pf->qs()) {
qs_pkts_++;
qs_bytes_ += pktsz;
}
//if enabled estimate rate now
if (estimate_rate_) {
estimateRate(p);
}
else {
prevTime_ = now;
}
barrivals_ += pktsz;
parrivals_++;
size_ += pktsz;
pkts_++;
if (bytesInt_)
bytesInt_->newPoint(now, double(size_));
if (pktsInt_)
pktsInt_->newPoint(now, double(pkts_));
if (delaySamp_)
hdr->timestamp() = now;
if (channel_)
printStats();
}
void QueueMonitor::out(Packet* p)
{
hdr_cmn* hdr = hdr_cmn::access(p);
hdr_flags* pf = hdr_flags::access(p);
double now = Scheduler::instance().clock();
int pktsz = hdr->size();
if (pf->ce() && pf->ect())
pmarks_++;
size_ -= pktsz;
pkts_--;
bdepartures_ += pktsz;
pdepartures_++;
if (bytesInt_)
bytesInt_->newPoint(now, double(size_));
if (pktsInt_)
pktsInt_->newPoint(now, double(pkts_));
if (delaySamp_)
delaySamp_->newPoint(now - hdr->timestamp());
if (keepRTTstats_) {
keepRTTstats(p);
}
if (keepSeqnoStats_) {
keepSeqnoStats(p);
}
if (channel_)
printStats();
}
void QueueMonitor::drop(Packet* p)
{
hdr_cmn* hdr = hdr_cmn::access(p);
double now = Scheduler::instance().clock();
int pktsz = hdr->size();
hdr_flags* pf = hdr_flags::access(p);
size_ -= pktsz;
pkts_--;
bdrops_ += pktsz;
pdrops_++;
if (pf->qs())
qs_drops_++;
if (bytesInt_)
bytesInt_->newPoint(now, double(size_));
if (pktsInt_)
pktsInt_->newPoint(now, double(pkts_));
if (channel_)
printStats();
}
// The procedure to estimate the rate of the incoming traffic
void QueueMonitor::estimateRate(Packet *pkt) {
hdr_cmn* hdr = hdr_cmn::access(pkt);
int pktSize = hdr->size() << 3; /* length of the packet in bits */
double now = Scheduler::instance().clock();
double timeGap = ( now - prevTime_);
if (timeGap == 0) {
temp_size_ += pktSize;
return;
}
else {
pktSize+= temp_size_;
temp_size_ = 0;
}
prevTime_ = now;
estRate_ = (1 - exp(-timeGap/k_))*((double)pktSize)/timeGap + exp(-timeGap/k_)*estRate_;
}
//The procedure to keep RTT statistics.
void QueueMonitor::keepRTTstats(Packet *pkt) {
int i, j, topBin, rttInMs, MsPerBin;
hdr_cmn* hdr = hdr_cmn::access(pkt);
packet_t t = hdr->ptype();
if (t == PT_TCP || t == PT_HTTP || t == PT_FTP || t == PT_TELNET) {
hdr_tcp *tcph = hdr_tcp::access(pkt);
rttInMs = tcph->last_rtt();
if (rttInMs < 0) rttInMs = 0;
topBin = maxRTT_ * binsPerSec_;
if (numRTTs_ == 0) {
RTTbins_ = (int *)malloc(sizeof(int)*topBin);
for (i = 0; i < topBin; i++) {
RTTbins_[i] = 0;
}
}
MsPerBin = int(1000/binsPerSec_);
j = (int)(rttInMs/MsPerBin);
if (j < 0) j = 0;
if (j >= topBin) j = topBin - 1;
++ RTTbins_[j];
++ numRTTs_;
}
}
//The procedure to keep Seqno (sequence number) statistics.
void QueueMonitor::keepSeqnoStats(Packet *pkt) {
int i, j, topBin, seqno;
hdr_cmn* hdr = hdr_cmn::access(pkt);
packet_t t = hdr->ptype();
if (t == PT_TCP || t == PT_HTTP || t == PT_FTP || t == PT_TELNET) {
hdr_tcp *tcph = hdr_tcp::access(pkt);
seqno = tcph->seqno();
if (seqno < 0) seqno = 0;
topBin = int(maxSeqno_ / SeqnoBinSize_);
if (numSeqnos_ == 0) {
SeqnoBins_ = (int *)malloc(sizeof(int)*topBin);
for (i = 0; i < topBin; i++) {
SeqnoBins_[i] = 0;
}
}
j = (int)(seqno/SeqnoBinSize_);
if (j < 0) j = 0;
if (j >= topBin) j = topBin - 1;
++ SeqnoBins_[j];
++ numSeqnos_;
}
}
/* ##############
* Tcl Stuff
* ##############
*/
static class SnoopQueueInClass : public TclClass {
public:
SnoopQueueInClass() : TclClass("SnoopQueue/In") {}
TclObject* create(int, const char*const*) {
return (new SnoopQueueIn());
}
} snoopq_in_class;
static class SnoopQueueOutClass : public TclClass {
public:
SnoopQueueOutClass() : TclClass("SnoopQueue/Out") {}
TclObject* create(int, const char*const*) {
return (new SnoopQueueOut());
}
} snoopq_out_class;
static class SnoopQueueDropClass : public TclClass {
public:
SnoopQueueDropClass() : TclClass("SnoopQueue/Drop") {}
TclObject* create(int, const char*const*) {
return (new SnoopQueueDrop());
}
} snoopq_drop_class;
static class SnoopQueueEDropClass : public TclClass {
public:
SnoopQueueEDropClass() : TclClass("SnoopQueue/EDrop") {}
TclObject* create(int, const char*const*) {
return (new SnoopQueueEDrop);
}
} snoopq_edrop_class;
/* Added by Yun Wang, for use of In/Out tagger */
static class SnoopQueueTaggerClass : public TclClass {
public:
SnoopQueueTaggerClass() : TclClass("SnoopQueue/Tagger") {}
TclObject* create(int, const char*const*) {
return (new SnoopQueueTagger);
}
} snoopq_tagger_class;
static class QueueMonitorEDClass : public TclClass {
public:
QueueMonitorEDClass() : TclClass("QueueMonitor/ED") {}
TclObject* create(int, const char*const*) {
return (new EDQueueMonitor);
}
} queue_monitor_ed_class;
/* ############################################################
* a 'QueueMonitorCompat', which is used by the compat
* code to produce the link statistics used available in ns-1
*
* in ns-1, the counters are the number of departures
* ############################################################
*/
#include "ip.h"
QueueMonitorCompat::QueueMonitorCompat()
{
memset(pkts_, 0, sizeof(pkts_));
memset(bytes_, 0, sizeof(bytes_));
memset(drops_, 0, sizeof(drops_));
memset(flowstats_, 0, sizeof(flowstats_));
}
/*
* create an entry in the flowstats_ array.
*/
void
QueueMonitorCompat::flowstats(int flowid)
{
Tcl& tcl = Tcl::instance();
/*
* here is the deal. we are in C code. we'd like to do
* flowstats_[flowid] = new Samples;
* but, we want to create an object that can be
* referenced via tcl. (in particular, we want ->name_
* to be valid.)
*
* so, how do we do this?
*
* well, the answer is, call tcl to create it. then,
* do a lookup on the result from tcl!
*/
tcl.evalf("new Samples");
flowstats_[flowid] = (Samples*)TclObject::lookup(tcl.result());
if (flowstats_[flowid] == 0) {
abort();
/*NOTREACHED*/
}
}
void QueueMonitorCompat::out(Packet* pkt)
{
hdr_cmn* hdr = hdr_cmn::access(pkt);
hdr_ip* iph = hdr_ip::access(pkt);
double now = Scheduler::instance().clock();
int fid = iph->flowid();
if (fid >= MAXFLOW) {
abort();
/*NOTREACHED*/
}
// printf("QueueMonitorCompat::out(), fid=%d\n", fid);
bytes_[fid] += hdr_cmn::access(pkt)->size();
pkts_[fid]++;
if (flowstats_[fid] == 0) {
flowstats(fid);
}
flowstats_[fid]->newPoint(now - hdr->timestamp());
QueueMonitor::out(pkt);
}
void QueueMonitorCompat::in(Packet* pkt)
{
hdr_cmn* hdr = hdr_cmn::access(pkt);
double now = Scheduler::instance().clock();
// QueueMonitor::in() *may* do this, but we always need it...
hdr->timestamp() = now;
QueueMonitor::in(pkt);
}
void QueueMonitorCompat::drop(Packet* pkt)
{
hdr_ip* iph = hdr_ip::access(pkt);
int fid = iph->flowid();
if (fid >= MAXFLOW) {
abort();
/*NOTREACHED*/
}
++drops_[fid];
QueueMonitor::drop(pkt);
}
int QueueMonitorCompat::command(int argc, const char*const* argv)
{
Tcl& tcl = Tcl::instance();
int fid;
if (argc == 3) {
fid = atoi(argv[2]);
if (strcmp(argv[1], "bytes") == 0) {
if (fid >= MAXFLOW) {
abort();
/*NOTREACHED*/
}
tcl.resultf("%d", bytes_[fid]);
return TCL_OK;
} else if (strcmp(argv[1], "pkts") == 0) {
if (fid >= MAXFLOW) {
abort();
/*NOTREACHED*/
}
tcl.resultf("%d", pkts_[fid]);
return TCL_OK;
} else if (strcmp(argv[1], "drops") == 0) {
if (fid >= MAXFLOW) {
abort();
/*NOTREACHED*/
}
tcl.resultf("%d", drops_[fid]);
return TCL_OK;
} else if (strcmp(argv[1], "get-class-delay-samples") == 0) {
if (fid >= MAXFLOW) {
abort();
/*NOTREACHED*/
}
if (flowstats_[fid] == 0) {
/*
* instantiate one if user actually
* cares enough to ask for it!
*
* (otherwise, need to return "",
* and then special-case caller to
* handle this null return.)
*/
flowstats(fid);
}
tcl.resultf("%s", flowstats_[fid]->name());
return TCL_OK;
}
}
return (QueueMonitor::command(argc, argv));
}
static class QueueMonitorCompatClass : public TclClass {
public:
QueueMonitorCompatClass() : TclClass("QueueMonitor/Compat") {}
TclObject* create(int, const char*const*) {
return (new QueueMonitorCompat);
}
} queue_monitor_compat_class;
|