Changeset 8865858
 Timestamp:
 11/13/18 17:45:23 (2 years ago)
 Branches:
 develop
 Children:
 3b206d8
 Parents:
 3d01ef7
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

examples/tutorial/ipdistparallel.c
r3d01ef7 r8865858 7 7 #include <arpa/inet.h> 8 8 #include <time.h> 9 #include <math.h> 9 10 10 11 /* Structure to hold the counters each thread has its own one of these */ … … 23 24 /* Pointer to stats structure */ 24 25 struct addr_stats *stats; 25 26 26 }; 27 27 struct addr_stats { … … 29 29 float src[4][256]; 30 30 float dst[4][256]; 31 double mean_src[4]; 32 double mean_dst[4]; 33 double stdev_src[4]; 34 double stdev_dst[4]; 35 double variance_src[4]; 36 double variance_dst[4]; 37 double variation_src[4]; 38 double variation_dst[4]; 31 39 struct addr_rank *rank_src[4]; 32 40 struct addr_rank *rank_dst[4]; … … 54 62 uint64_t tickrate; 55 63 56 char *stats_outputdir = " ";64 char *stats_outputdir = "/home/jcv9/output/"; 57 65 /* Calculate and plot the percentage change from the previous plot */ 58 int stats_percentage_change = 1;66 int stats_percentage_change = 0; 59 67 int stats_ranking = 1; 60 68 … … 72 80 } 73 81 static uint8_t peak(struct addr_rank **head) { 74 return (*head)>addr; 82 return (*head)>addr; 83 } 84 static uint64_t peak_count(struct addr_rank **head) { 85 return (*head)>count; 75 86 } 76 87 static void pop(struct addr_rank **head) { … … 101 112 102 113 static void compute_stats(struct addr_local *tally) { 103 int i, j ;114 int i, j, k; 104 115 105 116 /* Calculates the percentage change from the last output. NEED TO MAKE THIS WEIGHTED */ … … 133 144 } 134 145 } 146 147 148 /* This will all result in overflows, needs to be a rolling average?? stdev etc */ 149 /* Calculate mean */ 150 // for(i=0;i<4;i++) { 151 // uint64_t count_src = 0; 152 // uint64_t count_dst = 0; 153 // uint64_t tmp_src = 0; 154 // uint64_t tmp_dst = 0; 155 // for(j=0;j<256;j++) { 156 // tmp_src += (j * tally>src[i][j]); 157 // count_src += tally>src[i][j]; 158 // tmp_dst += (j * tally>dst[i][j]); 159 // count_dst += tally>dst[i][j]; 160 // } 161 // 162 // tally>stats>mean_src[i] = tmp_src / count_src; 163 // tally>stats>mean_dst[i] = tmp_dst / count_dst; 164 // } 165 // printf("mean: %f\n", tally>stats>mean_src[0]); 166 // 167 // 168 // /* Calculate variance, standard deviation and variation*/ 169 // for(i=0;i<4;i++) { 170 // uint64_t count_src = 0; 171 // uint64_t count_dst = 0; 172 // uint64_t tmp_src = 0; 173 // uint64_t tmp_dst = 0; 174 // for(j=0;j<256;j++) { 175 // tmp_src += (j * pow((tally>src[i][j]  tally>stats>mean_src[i]), 2)); 176 // count_src += tally>src[i][j]; 177 // tmp_dst += (j * pow((tally>dst[i][j]  tally>stats>mean_dst[i]), 2)); 178 // count_dst += tally>dst[i][j]; 179 // } 180 // //printf("total: %u count: %u dd: %f\n", tmp_src, count_src, tmp_src/count_src); 181 // tally>stats>variance_src[i] = (double)tmp_src / (double)count_src; 182 // tally>stats>variance_dst[i] = (double)tmp_dst / (double)count_dst; 183 // tally>stats>stdev_src[i] = sqrt(tally>stats>variance_src[i]); 184 // tally>stats>stdev_dst[i] = sqrt(tally>stats>variance_dst[i]); 185 // 186 // /* Calculate coefficient of variation */ 187 // tally>stats>variation_src[i] = tally>stats>stdev_src[i] / tally>stats>mean_src[i]; 188 // tally>stats>variation_dst[i] = tally>stats>stdev_dst[i] / tally>stats>mean_dst[i]; 189 // } 190 // 191 // 192 // printf("stdev: %f variance: %f variation: %f\n", tally>stats>stdev_src[0], tally>stats>variance_src[0], tally>stats>variation_src[0]); 193 135 194 } 136 195 … … 330 389 tally>stats>dst[i][j] = 0; 331 390 } 391 /* Stats related varibles */ 392 tally>stats>mean_src[i] = 0; 393 tally>stats>mean_dst[i] = 0; 394 tally>stats>stdev_src[i] = 0; 395 tally>stats>stdev_dst[i] = 0; 396 tally>stats>variance_src[i] = 0; 397 tally>stats>variance_dst[i] = 0; 398 tally>stats>variation_src[i] = 0; 399 tally>stats>variation_dst[i] = 0; 332 400 } 333 401 tally>lastkey = 0; … … 340 408 static void plot_results(struct addr_local *tally, uint64_t tick) { 341 409 342 int i, j ;410 int i, j, k; 343 411 344 412 /* Calculations before reporting the results */ … … 363 431 snprintf(outputfile, sizeof(outputfile), "%sipdist%u.data", stats_outputdir, tick); 364 432 FILE *tmp = fopen(outputfile, "w"); 365 fprintf(tmp, "#\tHits"); 366 if(stats_percentage_change) { 367 fprintf(tmp, "\t\t\t\t\t\t\t\tPercentage"); 368 } 369 if(stats_ranking) { 370 fprintf(tmp, "\t\t\t\t\t\t\t\tRanking"); 371 } 372 fprintf(tmp, "\n"); 373 fprintf(tmp, "#num\toctet1\t\toctet2\t\toctet3\t\toctet4"); 374 if(stats_percentage_change) { 375 fprintf(tmp, "\t\toctet1\t\toctet2\t\toctet3\t\toctet4"); 376 } 377 if(stats_ranking) { 378 fprintf(tmp, "\t\toctet1\t\toctet2\t\toctet3\t\toctet4"); 379 } 380 fprintf(tmp, "\n"); 381 fprintf(tmp, "#\tsrc\tdst\tsrc\tdst\tsrc\tdst\tsrc\tdst"); 382 if(stats_percentage_change) { 383 fprintf(tmp, "\tsrc\tdst\tsrc\tdst\tsrc\tdst\tsrc\tdst"); 384 } 385 if(stats_ranking) { 386 fprintf(tmp, "\tsrc\tdst\tsrc\tdst\tsrc\tdst\tsrc\tdst"); 387 } 388 fprintf(tmp, "\n"); 389 for(i=0;i<256;i++) { 390 fprintf(tmp, "%d", i); 433 fprintf(tmp, "#time\t\trank\toctet1\t\t\t\toctet2\t\t\t\toctet3\t\t\t\toctet4\n"); 434 fprintf(tmp, "#\t\t\tsrc\thits\tdst\thits\tsrc\thits\tdst\thits\tsrc\thits\tdst\thits\tsrc\thits\tdst\thits\n"); 435 for(i=0;i<256;i++) { 436 fprintf(tmp, "%d\t%d", tick, i+1); 391 437 for(j=0;j<4;j++) { 392 fprintf(tmp, "\t%d\t%d", tally>src[j][i], tally>dst[j][i]); 393 } 394 if(stats_percentage_change) { 395 for(j=0;j<4;j++) { 396 fprintf(tmp, "\t%.0f\t%.0f", tally>stats>src[j][i], tally>stats>dst[j][i]); 438 /* Get the highest ranking to lowest ranking octets */ 439 fprintf(tmp, "\t%d", peak(&tally>stats>rank_src[j])); 440 fprintf(tmp, "\t%d", peak_count(&tally>stats>rank_src[j])); 441 fprintf(tmp, "\t%d", peak(&tally>stats>rank_dst[j])); 442 fprintf(tmp, "\t%d", peak_count(&tally>stats>rank_dst[j])); 443 pop(&tally>stats>rank_src[j]); 444 pop(&tally>stats>rank_dst[j]); 445 } 446 fprintf(tmp, "\n"); 447 } 448 fclose(tmp); 449 450 /* Puts data into timeseries files that gnuplot likes */ 451 char outputfile2[255]; 452 for(k=0;k<2;k++) { 453 for(j=0;j<4;j++) { 454 /* If k is 0 we are doing src else dst */ 455 if(k) { 456 snprintf(outputfile2, sizeof(outputfile2), "%sipdisttimeseriesdstoctet%d.data", stats_outputdir, j+1); 457 } else { 458 snprintf(outputfile2, sizeof(outputfile2), "%sipdisttimeseriessrcoctet%d.data", stats_outputdir, j+1); 397 459 } 398 } 399 if(stats_ranking) { 400 for(j=0;j<4;j++) { 401 /* Get the highest ranking to lowest ranking octets */ 402 fprintf(tmp, "\t%d", peak(&tally>stats>rank_src[j])); 403 fprintf(tmp, "\t%d", peak(&tally>stats>rank_dst[j])); 404 pop(&tally>stats>rank_src[j]); 405 pop(&tally>stats>rank_dst[j]); 460 if(tally>output_count == 0) { 461 tmp = fopen(outputfile2, "w"); 462 fprintf(tmp, "prefix\t"); 463 for(i=0;i<256;i++) { 464 fprintf(tmp, "%d\t", i); 465 } 466 fprintf(tmp, "\n"); 467 } else { 468 tmp = fopen(outputfile2, "a"); 406 469 } 407 } 408 fprintf(tmp, "\n"); 409 } 410 fclose(tmp); 411 412 if(stats_ranking) { 413 for(i=0;i<4;i++) { 414 free(tally>stats>rank_src[i]); 415 free(tally>stats>rank_dst[i]); 416 } 470 fprintf(tmp, "%d\t", tick); 471 for(i=0;i<256;i++) { 472 if(k) { 473 fprintf(tmp, "%d\t", tally>dst[j][i]); 474 } else { 475 fprintf(tmp, "%d\t", tally>src[j][i]); 476 } 477 } 478 fprintf(tmp, "\n"); 479 fclose(tmp); 480 } 417 481 } 418 482 … … 421 485 char outputplot[255]; 422 486 snprintf(outputplot, sizeof(outputplot), "%sipdist%uoctet%d.png", stats_outputdir, tick, i+1); 423 424 /* Open pipe to gnuplot */ 425 FILE *gnuplot = popen("gnuplot persistent", "w"); 487 /* Open pipe to gnuplot */ 488 FILE *gnuplot = popen("gnuplot persistent", "w"); 426 489 /* send all commands to gnuplot */ 427 fprintf(gnuplot, "set term png size 1280,960 \n");490 fprintf(gnuplot, "set term pngcairo size 1280,960 \n"); 428 491 fprintf(gnuplot, "set title 'IP Distribution  Octet %d'\n", i+1); 429 492 fprintf(gnuplot, "set xrange[0:255]\n"); … … 431 494 fprintf(gnuplot, "set ylabel 'Hits'\n"); 432 495 fprintf(gnuplot, "set xtics 0,10,255\n"); 496 //fprintf(gnuplot, "set y2tics\n"); 433 497 fprintf(gnuplot, "set output '%s'\n", outputplot); 434 if(stats_percentage_change) { 435 fprintf(gnuplot, "set y2label 'Percentage Change'\n"); 436 fprintf(gnuplot, "set y2range[100:100]\n"); 437 fprintf(gnuplot, "set ytics nomirror\n"); 438 fprintf(gnuplot, "plot '%s' using 1:%d title 'Source octet %d' axes x1y1 with boxes,", outputfile, i+2, i+1); 439 fprintf(gnuplot, "'%s' using 1:%d title 'Destination octet %d' axes x1y1 with boxes,", outputfile, i+3, i+1); 440 fprintf(gnuplot, "'%s' using 1:%d title 'Octet %d source change' axes x1y2 with lines,", outputfile, i+10, i+1); 441 fprintf(gnuplot, "'%s' using 1:%d title 'Octet %d destination change' axes x1y2 with lines\n", outputfile, i+11, i+1); 442 } else { 443 fprintf(gnuplot, "plot '%s' using 1:%d title 'Source octet %d' axes x1y1 with boxes,", outputfile, i+2, i+1); 444 fprintf(gnuplot, "'%s' using 1:%d title 'Destination octet %d' axes x1y1 with boxes\n", outputfile, i+3, i+1); 445 } 498 //if(stats_percentage_change) { 499 // fprintf(gnuplot, "set y2label 'Percentage Change'\n"); 500 // fprintf(gnuplot, "set y2range[100:100]\n"); 501 // fprintf(gnuplot, "set ytics nomirror\n"); 502 // fprintf(gnuplot, "set y2tics\n"); 503 // fprintf(gnuplot, "plot '%s' using 1:%d title 'Source octet %d' axes x1y1 with boxes,", outputfile, i+2, i+1); 504 // fprintf(gnuplot, "'%s' using 1:%d title 'Destination octet %d' axes x1y1 with boxes,", outputfile, i+3, i+1); 505 // fprintf(gnuplot, "'%s' using 1:%d title 'Octet %d source change' axes x1y2 with lines,", outputfile, i+10, i+1); 506 // fprintf(gnuplot, "'%s' using 1:%d title 'Octet %d destination change' axes x1y2 with lines\n", outputfile, i+11, i+1); 507 508 //} else { 509 fprintf(gnuplot, "plot '%s' using %d:%d title 'Source octet %d' smooth unique with boxes,", outputfile, (i*4)+3,(i*4)+4, i+1); 510 fprintf(gnuplot, "'%s' using %d:%d title 'Destination octet %d' smooth unique with boxes\n", outputfile, (i*4)+5, (i*4)+6, i+1); 511 //} 446 512 fprintf(gnuplot, "replot"); 447 513 pclose(gnuplot); 514 } 515 516 /* Plot time series */ 517 for(k=0;k<2;k++) { 518 for(i=0;i<4;i++) { 519 char outputplot2[255]; 520 if(k) { 521 snprintf(outputplot2, sizeof(outputplot2), "%sipdisttimeseriesdstoctet%i.png", stats_outputdir, i+1); 522 } else { 523 snprintf(outputplot2, sizeof(outputplot2), "%sipdisttimeseriessrcoctet%i.png", stats_outputdir, i+1); 524 } 525 FILE *gnuplot = popen("gnuplot persistent", "w"); 526 fprintf(gnuplot, "set term pngcairo size 1280,960 \n"); 527 fprintf(gnuplot, "set output '%s'\n", outputplot2); 528 fprintf(gnuplot, "set key off\n"); 529 //fprintf(gnuplot, "set xdata time\n"); 530 //fprintf(gnuplot, "set timefmt '%s'\n"); 531 if(k) { 532 fprintf(gnuplot, "plot '%sipdisttimeseriesdstoctet%d.data' using 2:xtic(1) with lines title columnheader(2), for[i=2:257] '' using i with lines title columnheader(i)\n", stats_outputdir, i+1); 533 } else { 534 fprintf(gnuplot, "plot '%sipdisttimeseriessrcoctet%d.data' using 2:xtic(1) with lines title columnheader(2), for[i=2:257] '' using i with lines title columnheader(i)\n", stats_outputdir, i+1); 535 } 536 fprintf(gnuplot, "replot"); 537 pclose(gnuplot); 538 } 448 539 } 449 540 } … … 503 594 tally>dst[i][j] = 0; 504 595 } 596 /* Clear all stats related data */ 597 tally>stats>mean_src[i] = 0; 598 tally>stats>mean_dst[i] = 0; 599 tally>stats>stdev_src[i] = 0; 600 tally>stats>stdev_dst[i] = 0; 601 tally>stats>variance_src[i] = 0; 602 tally>stats>variance_dst[i] = 0; 603 tally>stats>variation_src[i] = 0; 604 tally>stats>variation_dst[i] = 0; 605 505 606 } 607 /* free the priority queue */ 608 for(i=0;i<4;i++) { 609 free(tally>stats>rank_src[i]); 610 free(tally>stats>rank_dst[i]); 611 } 612 506 613 tally>packets = 0; 507 614
Note: See TracChangeset
for help on using the changeset viewer.