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
|
//* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
* Copyright (c) 2000 International Computer Science Institute
* 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 ACIRI, the AT&T
* Center for Internet Research at ICSI (the International Computer
* Science Institute).
* 4. Neither the name of ACIRI nor of ICSI may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ICSI 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 ICSI 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.
*
*/
#include "ident-tree.h"
#include "rate-limit.h"
// ############################ PrefixTree Methods ####################
PrefixTree::PrefixTree() {
for (int i=0; i<=getLastIndex(); i++) {
countArray[i]=0;
}
}
void
PrefixTree::reset() {
for (int i=0; i<=getLastIndex(); i++) {
countArray[i]=0;
}
}
void
PrefixTree::traverse() {
printf("Traversal \n");
for (int i=0; i<=getLastIndex(); i++) {
printf("%d/%d %d\n",getPrefixFromIndex(i), getNoBitsFromIndex(i), countArray[i]);
}
}
void
PrefixTree::registerDrop(int address, int size) {
if (address > getMaxAddress()) {
printf("ERROR: Address out of Range\n");
exit(EXIT_FAILURE);
}
for (int i=0; i<=NO_BITS; i++) {
int index = getIndexFromAddress(address, i);
countArray[index]+=size;
}
}
AggReturn *
PrefixTree::calculateLowerBound() {
//bulk of this code is taken from identifyAggregate.
// bad idea - but quick.
// better way - to make the common code into a separate function.
int sum = 0; int count=0; int i;
for (i=getFirstIndexOfBit(NO_BITS); i<=getLastIndexOfBit(NO_BITS); i++) {
if (countArray[i]!=0) {
sum+=countArray[i];
count++;
}
}
// printf("CLB: sum: %d count: %d\n",sum, count);
if (count < 2) return NULL;
cluster *clusterList = (cluster *)malloc(sizeof(cluster)*MAX_CLUSTER);
for (i=0; i < MAX_CLUSTER; i++) {
clusterList[i].prefix_=-1;
clusterList[i].count_=0;
}
double mean = sum/count;
for (i=getFirstIndexOfBit(NO_BITS); i<=getLastIndexOfBit(NO_BITS); i++) {
if (countArray[i] >= mean/2) { //using mean/2 helps in trivial simulations.
insertCluster(clusterList, i, countArray[i], CLUSTER_LEVEL);
}
}
for (i=0; i<MAX_CLUSTER; i++) {
if (clusterList[i].prefix_==-1) {
break;
}
goDownCluster(clusterList, i);
}
int lastIndex = i-1;
sortCluster(clusterList, lastIndex);
return new AggReturn(clusterList, 0, lastIndex, countArray[0]);
}
AggReturn *
PrefixTree::identifyAggregate(double arrRate, double linkBW) {
int sum = 0; int count=0; int i;
for (i=getFirstIndexOfBit(NO_BITS); i<=getLastIndexOfBit(NO_BITS); i++) {
if (countArray[i]!=0) {
sum+=countArray[i];
count++;
}
}
if (count == 0) return NULL;
cluster *clusterList = (cluster *)malloc(sizeof(cluster)*MAX_CLUSTER);
for (i=0; i < MAX_CLUSTER; i++) {
clusterList[i].prefix_=-1;
clusterList[i].count_=0;
}
double mean = sum/count;
for (i=getFirstIndexOfBit(NO_BITS); i<=getLastIndexOfBit(NO_BITS); i++) {
if (countArray[i] >= mean/2) { //using mean/2 helps in trivial simulations.
insertCluster(clusterList, i, countArray[i], CLUSTER_LEVEL);
}
}
for (i=0; i<MAX_CLUSTER; i++) {
if (clusterList[i].prefix_==-1) {
break;
}
goDownCluster(clusterList, i);
}
int lastIndex = i-1;
sortCluster(clusterList, lastIndex);
//check for natural rifts here, if you want to.
double targetRate = linkBW/(1 - TARGET_DROPRATE);
double excessRate = arrRate - targetRate;
////////////////
//printf("arrRate: %5.2f targetRate: %5.2f excessRate %5.2f\n",
// arrRate, targetRate, excessRate);
/////////////////
double rateTillNow = 0;
double requiredBottom;
int id=0;
for (; id<=lastIndex; id++) {
rateTillNow+=clusterList[id].count_*(arrRate/countArray[0]);
requiredBottom = (rateTillNow - excessRate)/(id+1);
//printf("id: %d excessRate: %5.2f rateTillNow: %5.2f requiredBottom: %5.2f\n",
//id, excessRate, rateTillNow, requiredBottom);
if (clusterList[id+1].prefix_==-1) {
// I think that this means that no viable set of aggregates was found.
// Shouldn't it return failure in this case? - Sally
break;
}
if (clusterList[id+1].count_* (arrRate/countArray[0]) < requiredBottom) break;
}
return new AggReturn(clusterList, requiredBottom, id, countArray[0]);
}
void
PrefixTree::insertCluster(cluster * clusterList, int index, int count, int noBits) {
int address = getPrefixFromIndex(index);
int prefix = (address >> (NO_BITS - noBits)) << (NO_BITS - noBits);
int breakCode=0;
for (int i=0;i<MAX_CLUSTER; i++) {
if (clusterList[i].prefix_ == prefix && clusterList[i].bits_ == noBits) {
clusterList[i].count_+=count;
breakCode=1;
break;
}
if (clusterList[i].prefix_ == -1) {
clusterList[i].prefix_ = prefix;
clusterList[i].bits_ = noBits;
clusterList[i].count_=count;
breakCode=2;
break;
}
}
if (breakCode==0) {
printf("Error: Too Small MAX_CLUSTER. Increase and Recompile\n");
exit(-1);
}
}
void
PrefixTree::goDownCluster(cluster * clusterList, int index) {
int noBits = clusterList[index].bits_;
int prefix = clusterList[index].prefix_;
int treeIndex = getIndexFromPrefix(prefix, noBits);
int maxIndex = treeIndex;
while (1) {
int leftIndex = 2*maxIndex+1;
if (leftIndex > getLastIndex()) break;
int leftCount = countArray[leftIndex];
int rightCount = countArray[leftIndex+1];
if (leftCount > 9*rightCount) {
maxIndex = leftIndex;
}
else if (rightCount > 9*leftCount) {
maxIndex = leftIndex+1;
}
else {
break;
}
}
clusterList[index].prefix_=getPrefixFromIndex(maxIndex);
clusterList[index].bits_=getNoBitsFromIndex(maxIndex);
clusterList[index].count_=countArray[maxIndex];
}
void PrefixTree::sortCluster(cluster * clusterList, int lastIndex)
{
int i, j;
for (i=0; i<=lastIndex; i++) {
for (j=i+1; j<=lastIndex; j++) {
if (clusterList[i].count_ < clusterList[j].count_) {
swapCluster(clusterList, i, j);
}
}
}
}
void
PrefixTree::swapCluster(cluster * clusterList, int id1, int id2) {
int tempP = clusterList[id1].prefix_;
int tempB = clusterList[id1].bits_;
int tempC = clusterList[id1].count_;
clusterList[id1].prefix_ = clusterList[id2].prefix_;
clusterList[id1].bits_ = clusterList[id2].bits_;
clusterList[id1].count_ = clusterList[id2].count_;
clusterList[id2].prefix_ = tempP;
clusterList[id2].bits_ = tempB;
clusterList[id2].count_ = tempC;
}
int
PrefixTree::getMaxAddress() {
return (1 << NO_BITS) - 1;
}
int
PrefixTree::getBitI(int address, int i) {
int andAgent = 1 << (NO_BITS - i);
int bitI = address & andAgent;
if (bitI)
return 1;
else
return 0;
}
int
PrefixTree::getIndexFromPrefix(int prefix, int noBits) {
int base = (1 << noBits) - 1;
return base + (prefix >> (NO_BITS - noBits));
}
int
PrefixTree::getIndexFromAddress(int address, int noBits) {
int base = (1 << noBits) - 1;
// int andAgent = address >> (NO_BITS - noBits);
// int additive = base & andAgent;
int additive = address >> (NO_BITS - noBits);
return base + additive;
}
int
PrefixTree::getPrefixFromIndex(int index) {
int noBits = getNoBitsFromIndex(index);
int base = (1 << noBits) - 1;
int prefix = (index - base) << (NO_BITS - noBits);
return prefix;
}
int
PrefixTree::getPrefixBits(int prefix, int noBits) {
return (prefix >> (NO_BITS - noBits)) << (NO_BITS - noBits);
}
int
PrefixTree::getNoBitsFromIndex(int index) {
//using 1.2 is an ugly hack to get precise floating point calculation.
int noBits = (int) floor(log(index+1.2)/log(2));
return noBits;
}
int
PrefixTree::getFirstIndexOfBit(int noBits) {
return ( 1 << noBits) - 1;
}
int
PrefixTree::getLastIndexOfBit(int noBits) {
return ( 1 << (noBits+1)) - 2;
}
// ######################## IdentStruct Methods ########################
IdentStruct::IdentStruct() {
dstTree_ = new PrefixTree();
srcTree_ = new PrefixTree();
dropHash_ = new DropHashTable();
lowerBound_ = 0;
}
void
IdentStruct::reset() {
dstTree_->reset();
// srcTree_->reset();
// dropHash_->reset();
}
void
IdentStruct::traverse() {
dstTree_->traverse();
// srcTree_->traverse();
// dropHash_->traverse();
}
void
IdentStruct::registerDrop(Packet *p) {
hdr_ip * iph = hdr_ip::access(p);
// ns_addr_t src = iph->src();
ns_addr_t dst = iph->dst();
int fid = iph->flowid();
hdr_cmn* hdr = HDR_CMN(p);
int size = hdr->size();
if (AGGREGATE_CLASSIFICATION_MODE_FID)
dstTree_->registerDrop(fid, size);
else
dstTree_->registerDrop(dst.addr_, size);
// srcTree_->registerDrop(src.addr_, size);
// dropHash_->insert(p, size);
}
AggReturn *
IdentStruct::identifyAggregate(double arrRate, double linkBW) {
return dstTree_->identifyAggregate(arrRate, linkBW);
}
AggReturn *
IdentStruct::calculateLowerBound() {
return dstTree_->calculateLowerBound();
}
void
IdentStruct::setLowerBound(double bound, int averageIt) {
double alpha = 0.5;
if (lowerBound_ == 0)
lowerBound_ = bound;
else if (averageIt == 0) {
if (bound < lowerBound_)
lowerBound_ = bound;
else
lowerBound_ = alpha * lowerBound_ + (1 - alpha) * bound;
}
else {
lowerBound_ = alpha * lowerBound_ + (1 - alpha) * bound;
}
//printf("lower bound: new = %g avg = %g\n", bound, lowerBound_);
//fflush(stdout);
}
|