-
Notifications
You must be signed in to change notification settings - Fork 0
/
bprobe.c
701 lines (601 loc) · 18.9 KB
/
bprobe.c
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
main routine for bprobe tool
calls rprobe to do actuall packet transmissions
save results and does analysis
*/
#include "bprobe.h"
static char rcsid[] = "$Id: bprobe.c,v 1.5 1996/07/10 18:56:46 carter Exp $";
char hostname[MAXHOSTNAMELEN];
void usage(){fprintf(stderr,"Usage: bprobe hostname \n");}
/* forward declarations */
int *freeBins(int *bins);
float applyMethod(int occupiedBinCount, float binSize,
float smallGap, int *bins,
int *fullBins);
float applyNewGraphicalMethod(float **bws, float *probeSizes,
int rows, int cols,
int minPeak, int minNumSets);
void histogramGaps(float smallGap, float bigGap, float gapRange,
int *gaps, int gapCount,
float *RETURNbinSize, int *RETURNoccupiedBinCount,
int **RETURNoccupiedBins, int **RETURNbins);
float reportBW(float estimate, int probeSize);
void reportBandwidthForGaps(int count, int gaps[], int Dgaps[], int size,
float *bwEsts, float * interDepartureTimes,
float *packetSizes);
void sortGaps(int gapCount, int *gaps, int *departGaps,
int *sortedGaps, int *sortedDGaps);
void gapStats(int gapCount, int *gaps,
float *minGap, float *maxGap,
float *gapRange, float *medianGap);
float probeLoop(char * hostName, int packetCount);
void calculateUtilization(float **BwEstimateMatrix,
float **interDepartureMatrix,
float **packetSizeMatrix,
int rows, int cols,
float baseBW,
float *returnAvg, float *returnMedian);
#ifndef NOMAIN
/* conditional compilation for use as stanalone probe
and as part of SONAR server */
int main(int argc, char *argv[])
{
int numPaths = 0; /* how many paths did the packets take ? */
int returnCodes = 0;
hw_time_t depart[MAX_PROBES]; /* departure time of each packet */
hw_time_t arrive[MAX_PROBES]; /* arrival time of each packet */
int packetSize[MAX_PROBES];
int bytes[MAX_PROBES]; /* size of each packet */
int npackets;
float bandwidth;
int retry;
char *cp;
char *ap;
int fp;
int i;
struct protoent *proto;
hw_time_t start_time, end_time;
/* for high-res timer */
unsigned hw_clock, raddr;
int fd;
if (argc != 2)
{
usage();
exit(1);
}
if (argc > (2 + MAX_PROBES))
{
fprintf(stderr, "Too many probes requested; only doing first %d\n",
MAX_PROBES);
argc = 2 + MAX_PROBES;
}
npackets = argc - 2;
setuid(getuid());
/* initialize Arrival vector */
for (i = 0; i< npackets; i++) {
arrive[i] = -1;
}
printf("BRPROBE (%s) %s\n",
VERSION, argv[1]);
bandwidth = -1.0;
retry = 4; /* DISABLE RETRIES */
while ((bandwidth < 0.0) && (retry++ < 5)) {
bandwidth = (float)bprobe(argv[1]);
}
if (bandwidth > 0.0) {
printf("Final Bandwidth: %10.5e\n",
bandwidth);
}
else {
printf("Final Bandwidth: 0.0\n");
}
exit(0);
}
#endif
int bprobe(char *host) {
float result;
result = probeLoop(host, 10);
return((int) result);
}
/* print stats for each received packet */
void reportResults(int npackets,
hw_time_t depart[], hw_time_t arrive[], int bytes[],
int orderOK[],
int returnCodes, int numPaths) {
int i = 0;
printf("* ");
for (i=0; i< npackets; i++){
printf("%lu %lu %lu ", bytes[i], depart[i], arrive[i]);
}
if (returnCodes & PacketsInOrder) {
printf(" SEQOK ");
}
else {
printf(" NOTSEQOK ");
}
if (returnCodes & RecordRouteData) {
printf(" RR");
}
else {
printf(" NORR");
}
if (returnCodes & DuplicatePackets) {
/* tack this string onto the end of the record route info flag
that way, it doesn't disrupt the processing by other scripts */
printf("_DUP ");
}
else {
printf(" ");
}
if (numPaths > 1) {
printf("multipath: %d", numPaths);
}
printf("\n");
}
float probeLoop(char * hostName, int packetCount) {
int probeSize = 124;
int numPaths = 0; /* how many paths did the packets take ? */
int returnCodes = 0;
hw_time_t depart[MAX_PROBES]; /* dparture time of each packet */
hw_time_t arrive[MAX_PROBES]; /* arrival time of each packet */
int packetSize[MAX_PROBES];
int bytes[MAX_PROBES]; /* size of each packet */
int orderOK[MAX_PROBES]; /* is this packet in order? */
int gaps[MAX_PROBES];
int departGaps[MAX_PROBES];
int sortedGaps[MAX_PROBES];
int sortedDGaps[MAX_PROBES];
int occupiedBinCount = 0;
int *occupiedBins;
int *bins;
int gapCount = 0;
float binSize = 0;
float minGap = 0.0;
float maxGap = 0.0;
float gapRange = 0.0;
float medianGap = 0.0;
float estimate1 = 0.0, estimate6 = 0.0;
float avgUtil6 = 0.0;
float medUtil6 = 0.0;
float bandwidth = 0.0;
float gapVector[NUM_ROUNDS];
float bandwidthVector[NUM_ROUNDS];
float probeSizeVector[NUM_ROUNDS];
float **bwsToPass;
float **interDepartureMatrix;
float **packetSizeMatrix;
int pass = 0;
int i = 0, j = 0;
int validObservations = 0;
int badIteration = 0;
packetCount = 10;
bwsToPass = matrix(0,NUM_ROUNDS-1,0,MAX_PROBES-1);
interDepartureMatrix = matrix(0,NUM_ROUNDS-1,0,MAX_PROBES-1);
packetSizeMatrix = matrix(0,NUM_ROUNDS-1,0,MAX_PROBES-1);
for(i=0;i<NUM_ROUNDS;i++)
for(j=0;j<MAX_PROBES;j++) {
bwsToPass[i][j] = 0.0;
interDepartureMatrix[i][j] = 0.0;
packetSizeMatrix[i][j] = 0.0;
gapVector[j] = bandwidthVector[j] = probeSizeVector[j] = 0.0;
}
//while (probeSize < 8192) {
while (probeSize < 1400) {
/*
printf("\n\nTry probing with %d byte probes...\n", probeSize);
*/
badIteration = 0; /* clear trouble flag */
/* initialize Arrival vector */
for (i = 0; i< packetCount; i++) {
arrive[i] = -1;
}
/* marshal the packet size parameters for the call */
for (i=0; i<packetCount; i++)
{
packetSize[i] = probeSize;
}
/* do the call */
if (!rprobe(hostName, packetCount, packetSize,
depart, arrive, bytes, orderOK,
&returnCodes, &numPaths,
/* for now, hard code so it works the old way
may want to adjust based on packet size eventually */
30)) {
#ifdef DEBUG
/* successful probe, report results */
reportResults(packetCount, depart, arrive, bytes,
orderOK,
returnCodes, numPaths);
#endif
}
else {
printf("bprobe: rprobe failure.\n");
exit(-1);
}
if (!(returnCodes & PacketsInOrder))
{
printf("LOGME Packets out of order. Cannot continue\n");
badIteration = 1;
}
if (returnCodes & DuplicatePackets) {
printf("LOGME Duplicate Packets received. Cannot continue\n");
badIteration = 1;
}
gapCount = computeInterArrivalGaps(packetCount, arrive, depart,
orderOK,
gaps, departGaps);
if (gapCount < 2) {
printf("not enough valid gaps (%d), going on to next iteration\n",
gapCount);
badIteration = 1;
}
if (badIteration) {
estimate1 = 0.0;
bandwidth = 0.0;
}
else {
/* we have enough gaps to be getting on with */
#ifdef DEBUG
displayBothGaps(gapCount, gaps, departGaps);
#endif
sortGaps(gapCount, gaps, departGaps, sortedGaps, sortedDGaps);
#ifdef DEBUG
displayBothGaps(gapCount, gaps, sortedGaps, sortedDGaps);
#endif
reportBandwidthForGaps(gapCount, sortedGaps, sortedDGaps, probeSize,
bwsToPass[pass], interDepartureMatrix[pass],
packetSizeMatrix[pass]);
gapStats(gapCount, sortedGaps, &minGap, &maxGap,
&gapRange, &medianGap);
histogramGaps(minGap, maxGap, gapRange,
gaps, gapCount,
&binSize, &occupiedBinCount,
&occupiedBins, &bins);
/* this is just shoehorned in */
estimate1 = applyMethod(occupiedBinCount, binSize,
minGap,
bins,
occupiedBins);
bandwidth = reportBW(estimate1, probeSize);
bins = freeBins(bins);
occupiedBins = freeBins(occupiedBins);
}/* end procesing of sufficient gaps */
/* double the probe size for the next iteration */
gapVector[pass] = estimate1;
bandwidthVector[pass] = bandwidth;
probeSizeVector[pass] = (float)probeSize;
pass++;
/* new probe size is 1.5 times old on odd passes
ans 2.5 times old on even passes */
if ((pass % 2) == 1) {
//int halfProbe = probeSize >> 1;
//probeSize = probeSize + halfProbe;
// Changing probesize = 1.3 times old for odd passes
probeSize = 1.3*probeSize;
}
else {
//int halfProbe = probeSize >> 1;
//probeSize = 2 * probeSize + halfProbe;
// Changing probesize = 1.7 times old for even passes
probeSize = 1.7*probeSize;
}
/* round up to next even number */
if ((probeSize % 2) == 1)
probeSize++;
} /* end loop over varied probe sizes */
validObservations = cleanData(probeSizeVector, gapVector, bwsToPass,
MAX_PROBES);
estimate6 = applyNewGraphicalMethod(bwsToPass, probeSizeVector,
validObservations, MAX_PROBES,
7, 2);
calculateUtilization(bwsToPass, interDepartureMatrix, packetSizeMatrix,
validObservations, MAX_PROBES,
estimate6, &avgUtil6,&medUtil6);
free_matrix(bwsToPass,0,NUM_ROUNDS-1,0,MAX_PROBES-1);
free_matrix(interDepartureMatrix,0,NUM_ROUNDS-1,0,MAX_PROBES-1);
free_matrix(packetSizeMatrix,0,NUM_ROUNDS-1,0,MAX_PROBES-1);
return(estimate6);
} /* end probeLoop */
int cleanData(float *probeSizes, float *gapSizes, float **bwsToPass,
int numberOfProbeRounds) {
int i, j, k;
int valid = numberOfProbeRounds; /* initially, assume all are valid */
for (i=0; i<numberOfProbeRounds; i++) {
if (gapSizes[i] == 0.0) {
valid--; /* remove this entry from the count of valid data */
for (j = i+1; j<numberOfProbeRounds; j++) {
/* move the rest of the data down one position */
probeSizes[j-1] = probeSizes[j];
gapSizes[j-1] = gapSizes[j];
for (k=0; k<MAX_PROBES; k++) {
bwsToPass[j-1][k] = bwsToPass[j][k];
}
}
i--; /* want to look again at the *new* i'th entry */
numberOfProbeRounds--; /* one fewer we need to look at at the end */
}
}
return(valid);
}
int *freeBins(int *bins) {
free(bins);
return(NULL);
}
float applyMethod(int occupiedBinCount, float binSize,
float smallGap, int *bins,
int *fullBins) {
float sum = 0.0;
float count = 0.0;
int index = 0;
float estimate = 0.0;
if (occupiedBinCount > 3) {
/* throw out the low and high bins */
/* take average of remainder */
sum = 0.0;
count = 0.0;
for(index = 1; index < occupiedBinCount - 1; index++) {
/* compute the contribution of this bin my multiplying
the value of the bin by the number of items in it */
sum += (smallGap + binSize * fullBins[index]) *
bins[fullBins[index]];
/* count the number of items in the running sum */
count += bins[fullBins[index]];
}
estimate = sum / count;
}
else {
if (occupiedBinCount == 3) {
/* take the median bin size as the estimate */
estimate = smallGap + binSize * fullBins[1];
}
else {
if (occupiedBinCount > 0) {
/* take average of remainder */
sum = 0.0;
count = 0.0;
for(index = 0; index < occupiedBinCount ; index++) {
/* compute the contribution of this bin my multiplying
the value of the bin by the number of items in it */
sum += (smallGap + binSize * fullBins[index]) *
bins[fullBins[index]];
/* count the number of items in the running sum */
count += bins[fullBins[index]];
}
estimate = sum / count;
}
else {
printf("ERROR - no bins occupied\n");
estimate = 1.0;
}
}
}
return(estimate);
} /* end applyMethod */
void gapStats(int gapCount, int *gaps,
float *minGap, float *maxGap,
float *gapRange, float *medianGap) {
/* # find the range of the gaps */
int smallGapIndex = 0;
int bigGapIndex = gapCount - 1;
*minGap = gaps[0];
*maxGap = gaps[bigGapIndex];
*gapRange = *maxGap - *minGap;
#ifdef DEBUG
printf("minGap: %10.2f, maxGap: %10.2f, gapRange: %10.2f\n",
*minGap, *maxGap, *gapRange);
#endif
/* # find the median gap */
if ((bigGapIndex % 2) == 0) {
*medianGap = gaps[(bigGapIndex >>= 1)];
}
else {
int leftOfCenter = (int)(bigGapIndex/2.0);
int rightOfCenter = leftOfCenter++;
*medianGap = ((gaps[leftOfCenter] + gaps[rightOfCenter]) / 2.0);
}
#ifdef DEBUG
printf( "medianGap: %f\n", *medianGap);
#endif
}/* end gapStats */
void histogramGaps(float smallGap, float bigGap, float gapRange,
int *gaps, int gapCount,
float *RETURNbinSize, int *RETURNoccupiedBinCount,
int **RETURNoccupiedBins, int **RETURNbins) {
int numberOfBins = 100;
int index = 0;
int *occupiedBins, *bins;
float binSize;
int occupiedBinCount;
binSize = gapRange/(float)numberOfBins;
/* don't want too fine a grain */
while ((binSize < 0.1)) {
binSize = binSize * 2.0;
}
occupiedBinCount = 10;
while (occupiedBinCount > 7) {
occupiedBinCount = 0;
numberOfBins = (int)(gapRange/binSize + 0.5);
numberOfBins++; /* safety margin. think about this someday */
/* allocate space for bins and occupiedBins */
bins = (int *)malloc(numberOfBins * sizeof(int));
occupiedBins = (int *) malloc(numberOfBins * sizeof(int));
/* zero out these arrays */
/* can thse be done faster. bzero?? */
for (index = 0; index < numberOfBins; index++) {
bins[index] = occupiedBins[index] = 0;
}
/* # assign the gaps to bins */
for (index = 0; index < gapCount; index++) {
int binNumber = (int)((gaps[index]-smallGap)/binSize + 0.5);
bins[binNumber]++;
}
/* figure out how many bins are occupied and which ones are */
for(index = 0; index < numberOfBins; index++) {
if (bins[index]) {
occupiedBins[occupiedBinCount++] = index;
#ifdef DEBUG
printf("bin[%d] : %d\n",index, bins[index]);
#endif
}
}
#ifdef DEBUG
printf("full bins: %d\n",occupiedBinCount);
for(index = 0; index < occupiedBinCount; index++) {
printf("occupied bin[%d] : %d\n",index, occupiedBins[index]);
}
#endif
if (occupiedBinCount > 7) {
/* free storage for next round */
free(bins);
free(occupiedBins);
/* undef(@bin); */
#ifdef DEBUG
printf("too many bins, another round\n");
#endif
binSize = binSize * 2;
} /* cleanup for next iteration */
} /* # loop to compress bins */
/* return these values */
*RETURNbinSize = binSize;
*RETURNoccupiedBinCount = occupiedBinCount;
*RETURNoccupiedBins = occupiedBins;
*RETURNbins = bins;
} /* end histogramGaps */
void displayGaps(int gapCount, int *gaps) {
int index = 0 ;
for (index = 0; index < gapCount; index++) {
printf("gaps[%d] = %d\n", index, gaps[index]);
}
}
void displayBothGaps(int gapCount, int *gaps, int *Dgaps) {
int index = 0 ;
for (index = 0; index < gapCount; index++) {
printf("gaps[%d] = %d\t%d\n", index, gaps[index], Dgaps[index]);
}
}
int computeInterArrivalGaps(
int packetCount, hw_time_t *arrive, hw_time_t *depart,
int *orderOK,
int *gaps, int *departGaps) {
int index = 0;
int gapIndex = -1;
for (index = 0; index < packetCount; index++) {
/* check that no rollover or timeout occurred */
if ((arrive[index] > 0 ) &&
(arrive[index+1] > 0) &&
(arrive[index+1] > arrive[index]) &&
/* new scheme, let packets com in out of order but only
measure the pairs that ar in order */
/* check that the next packet is in order relative to this one */
orderOK[index+1]
) {
gaps[++gapIndex] = arrive[index+1] - arrive[index];
departGaps[gapIndex] = depart[index+1] - depart[index];
}
} /* end loop computing inter-arrival gaps */
return(gapIndex + 1); /* return a count of valid gaps */
}
void sortGaps(int gapCount, int *gaps, int *Dgaps,
int *sortedGaps, int *sortedDgaps) {
/* do a stupid sort since this is so small a list */
int i, j;
int position;
#if DEBUG
printf("sorting %d gaps\n", gapCount);
#endif
for (i = 0; i < gapCount; i++) {
/* pick up an item */
/* initially assume it belongs in the first position in the
sorted list */
position = 0;
/* compare it to each other item.
if the other item is less than incrment the position
of the item being held to make room for the smaller item
*/
for (j = 0; j < gapCount; j++) {
/* if the item is less than the current, current moves over one */
if (gaps[j] < gaps[i])
position++;
/* if the item equals the current, only move over equals to the right */
if ((gaps[j] == gaps[i]) && j > i) {
position++;
}
}
/* printf("put %d in position %d\n", gaps[i], position); */
sortedGaps[position] = gaps[i];
/* the inter-departure gap goes along for the ride */
sortedDgaps[position] = Dgaps[i];
}
} /* end sortGaps */
float reportBW(float estimate, int probeSize) {
float bandwidth;
/*
printf("estimated interarrivaL GAPOSIS: %10.2f probe size %d\n",estimate,
probeSize);
*/
probeSize += 82; /* add ethernet penalty */
if (estimate > 0.0) {
bandwidth = (float)(probeSize * 8000000.0) / estimate;
}
else {
bandwidth = 0.0;
}
/*
printf("estimated Bandwidth: %10.2f bps\n",bandwidth);
*/
return(bandwidth);
} /* end reportBW */
void reportBandwidthForGaps(int count, int gaps[], int Dgaps[],
int size,
float *bwEsts,
float *interDepartureTimes,
float *packetSizes) {
/*
takes vectors of interArrival gaps and interDeparture gaps
and the packet size from one phase of the probe
given a set of inter-arrival gaps and the packet sizes
compute the bw estimates
returns rows of three matrices: bw estimates, interdeparture
times and packet sizes
also dumps a table of this infomation to the stdout.
*/
int i, validBWIndex;
float diff, ratio;
float bw;
validBWIndex = 0;
#ifdef DEBUG
printf("Size\tDelArr\tDelDep\t Diff\t Ratio\tBW EST\n");
#endif
for (i=0; i<count; i++) {
bw = reportBW((float)gaps[i], size);
diff = (float)(gaps[i] - Dgaps[i]);
ratio = (float)gaps[i]/(float)Dgaps[i];
#ifdef DEBUG
printf("%d\t%6.0f\t%6.0f\t%6.0f\t%6.2f\t%10.4e",
size, (float)gaps[i], (float)Dgaps[i],
diff, ratio, bw);
#endif
/* weed out bogus entries */
if ((diff > 0.0) && (ratio <= 10000.0)) {
interDepartureTimes[validBWIndex] = (float)Dgaps[i];
packetSizes[validBWIndex] = (float)size;
bwEsts[validBWIndex++] = bw;
/* terminate table row */
#ifdef DEBUG
printf("\n");
#endif
}
else {
/* terminate table row (with extreme prejudice ;-)*/
#ifdef DEBUG
printf(" <discarded>\n");
#endif
}
} /* end loop over all valid gaps */
/* pad the rest of the bw row for this probe size with zeros */
for (i=validBWIndex; i<MAX_PROBES; i++) {
interDepartureTimes[i] = 0.0;
packetSizes[i] = 0.0;
bwEsts[i] = 0.0;
}
} /* end reportBandwidthForGaps */