Changeset acffeb8
- Timestamp:
- 11/09/18 11:21:17 (2 years ago)
- Branches:
- develop
- Children:
- 4d5e6b3, dc62550
- Parents:
- 2f07199
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
examples/tutorial/ipdist-parallel.c
r2f07199 racffeb8 10 10 /* Structure to hold the counters each thread has its own one of these */ 11 11 struct addr_local { 12 uint64_t src[256]; 13 uint64_t dst[256]; 14 uint64_t lastkey; 15 uint64_t packets; 16 }; 17 /* Structure to hold the result from a processing thread */ 18 struct addr_result { 19 uint64_t src[256]; 20 uint64_t dst[256]; 21 uint64_t packets; 22 }; 23 /* Structure to hold counters the report has one of these, it combines 24 * the the counters of each threads local storage used by the reporter thread */ 25 struct addr_tally { 26 uint64_t src[256]; 27 uint64_t dst[256]; 12 uint64_t src[4][256]; 13 uint64_t dst[4][256]; 28 14 uint64_t lastkey; 29 15 uint64_t packets; … … 43 29 /* interval between outputs in seconds */ 44 30 uint64_t tickrate; 45 /* The octet which to plot results for */46 int octet = 0;47 31 48 32 static void per_tick(libtrace_t *trace, libtrace_thread_t *thread, void *global, void *tls, uint64_t tick) { 49 33 50 struct addr_ result *result = (struct addr_result *)malloc(sizeof(struct addr_result));34 struct addr_local *result = (struct addr_local *)malloc(sizeof(struct addr_local)); 51 35 /* Proccessing thread local storage */ 52 36 struct addr_local *local = (struct addr_local *)tls; 53 37 54 38 /* Populate the result structure from the threads local storage and clear threads local storage*/ 55 int i; 56 for(i=0;i<256;i++) { 57 /* Populate results */ 58 result->src[i] = local->src[i]; 59 result->dst[i] = local->dst[i]; 60 /* Clear local storage */ 61 local->src[i] = 0; 62 local->dst[i] = 0; 39 int i, j; 40 for(i=0;i<4;i++) { 41 for(j=0;j<256;j++) { 42 result->src[i][j] = local->src[i][j]; 43 result->dst[i][j] = local->dst[i][j]; 44 /* clear local storage */ 45 local->src[i][j] = 0; 46 local->dst[i][j] = 0; 47 } 63 48 } 64 49 result->packets = local->packets; … … 74 59 /* Create and initialize the local counter struct */ 75 60 struct addr_local *local = (struct addr_local *)malloc(sizeof(struct addr_local)); 76 int i; 77 for(i=0;i<256;i++) { 78 local->src[i] = 0; 79 local->dst[i] = 0; 61 int i, j; 62 for(i=0;i<4;i++) { 63 for(j=0;j<256;j++) { 64 local->src[i][j] = 0; 65 local->dst[i][j] = 0; 66 } 80 67 } 81 68 local->lastkey = 0; … … 121 108 122 109 /* Split the IPv4 address into each octet */ 123 uint8_t octet s[4];124 octet s[0] = (address & 0xff000000) >> 24;125 octet s[1] = (address & 0x00ff0000) >> 16;126 octet s[2] = (address & 0x0000ff00) >> 8;127 octet s[3] = (address & 0x000000ff);110 uint8_t octet[4]; 111 octet[0] = (address & 0xff000000) >> 24; 112 octet[1] = (address & 0x00ff0000) >> 16; 113 octet[2] = (address & 0x0000ff00) >> 8; 114 octet[3] = (address & 0x000000ff); 128 115 129 116 /* check if the supplied address was a source or destination, 130 117 increment the correct one */ 131 118 if(srcaddr) { 132 local->src[octets[octet]]++; 119 int i; 120 for(i=0;i<4;i++) { 121 local->src[i][octet[i]] += 1; 122 } 133 123 } else { 134 local->dst[octets[octet]]++; 124 int i; 125 for(i=0;i<4;i++) { 126 local->dst[i][octet[i]] += 1; 127 } 135 128 } 136 129 } … … 196 189 struct addr_local *local = (struct addr_local *)tls; 197 190 /* Create structure to store the result */ 198 struct addr_ result *result = (struct addr_result *)malloc(sizeof(struct addr_result));191 struct addr_local *result = (struct addr_local *)malloc(sizeof(struct addr_local)); 199 192 200 193 /* Populate the result */ 201 int i; 202 for(i=0;i<256;i++) { 203 result->src[i] = local->src[i]; 204 result->dst[i] = local->dst[i]; 194 int i, j; 195 for(i=0;i<4;i++) { 196 for(j=0;j<256;j++) { 197 result->src[i][j] = local->src[i][j]; 198 result->dst[i][j] = local->dst[i][j]; 199 } 205 200 } 206 201 result->packets = local->packets; … … 216 211 static void *start_reporter(libtrace_t *trace, libtrace_thread_t *thread, void *global) { 217 212 /* Create tally structure */ 218 struct addr_ tally *tally = (struct addr_tally *)malloc(sizeof(struct addr_tally));213 struct addr_local *tally = (struct addr_local *)malloc(sizeof(struct addr_local)); 219 214 220 215 /* Initialize the tally structure */ 221 int i; 222 for(i=0;i<256;i++) { 223 tally->src[i] = 0; 224 tally->dst[i] = 0; 216 int i, j; 217 for(i=0;i<4;i++) { 218 for(j=0;j<256;j++) { 219 tally->src[i][j] = 0; 220 tally->dst[i][j] = 0; 221 } 225 222 } 226 223 tally->lastkey = 0; … … 230 227 } 231 228 232 static void plot_results(struct addr_ tally*tally, uint64_t tick) {229 static void plot_results(struct addr_local *tally, uint64_t tick) { 233 230 234 231 /* Get the current time */ … … 242 239 /* Push all data into data file */ 243 240 FILE *tmp = fopen(outputfile, "w"); 244 int i; 245 for(i=0;i<255;i++) { 246 fprintf(tmp, "%d %d %d\n", i, tally->src[i], tally->dst[i]); 241 int i, j; 242 fprintf(tmp, "#num\toctet1\t\toctet2\t\toctet3\t\toctet4\n"); 243 fprintf(tmp, "#\tsrc\tdst\tsrc\tdst\tsrc\tdst\tsrc\tdst\n"); 244 for(i=0;i<256;i++) { 245 fprintf(tmp, "%d", i); 246 for(j=0;j<4;j++) { 247 fprintf(tmp, "\t%d\t%d", tally->src[j][i], tally->dst[j][i]); 248 } 249 fprintf(tmp, "\n"); 247 250 } 248 251 fclose(tmp); … … 258 261 fprintf(gnuplot, "set ylabel 'Hits'\n"); 259 262 fprintf(gnuplot, "set xtics 0,10,255\n"); 263 //fprintf(gnuplot, "set datafile commentschars '#!%'"); 260 264 fprintf(gnuplot, "set output '%s'\n", outputplot); 261 265 fprintf(gnuplot, "plot '%s' using 1:2 title 'Source address' with boxes, '%s' using 1:3 title 'Destination address' with boxes\n", outputfile, outputfile); … … 269 273 void *tls, libtrace_result_t *result) { 270 274 271 struct addr_ result*results;272 struct addr_ tally*tally;275 struct addr_local *results; 276 struct addr_local *tally; 273 277 uint64_t key; 274 278 … … 283 287 284 288 /* result->value is a libtrace_generic_t that was passed into trace_publish_results() */ 285 results = (struct addr_ result*)result->value.ptr;289 results = (struct addr_local *)result->value.ptr; 286 290 287 291 /* Grab our tally out of thread local storage */ 288 tally = (struct addr_ tally*)tls;292 tally = (struct addr_local *)tls; 289 293 290 294 /* Add all the results to the tally */ 291 int i; 292 for(i=0;i<256;i++) { 293 tally->src[i] += results->src[i]; 294 tally->dst[i] += results->dst[i]; 295 int i, j; 296 for(i=0;i<4;i++) { 297 for(j=0;j<256;j++) { 298 tally->src[i][j] += results->src[i][j]; 299 tally->dst[i][j] += results->dst[i][j]; 300 } 295 301 } 296 302 tally->packets += results->packets; … … 306 312 307 313 /* clear the tally */ 308 for(i=0;i<256;i++) { 309 tally->src[i] = 0; 310 tally->dst[i] = 0; 314 for(i=0;i<4;i++) { 315 for(j=0;j<256;j++) { 316 tally->src[i][j] = 0; 317 tally->dst[i][j] = 0; 318 } 311 319 } 312 320 tally->packets = 0; … … 321 329 322 330 /* Get the tally from the thread local storage */ 323 struct addr_ tally *tally = (struct addr_tally*)tls;331 struct addr_local *tally = (struct addr_local *)tls; 324 332 325 333 /* If there is any remaining data in the tally plot it */ … … 380 388 libtrace_callback_set_t *processing, *reporter; 381 389 390 382 391 /* Ensure the input URI was supplied */ 383 392 if(argc < 3) { … … 388 397 /* Convert tick into an int */ 389 398 tickrate = atoi(argv[2]); 399 390 400 391 401 /* Create the trace */ … … 434 444 } 435 445 exclude->count = 0; 436 437 446 int i; 438 447 for(i=0;i<argc-3;i++) { 439 448 /* convert the network string into a network structure */ 440 get_network(argv[i+3], &exclude->networks[i]); 449 if(get_network(argv[i+3], &exclude->networks[i]) != 0) { 450 fprintf(stderr, "Error creating excluded network"); 451 return 1; 452 } 441 453 /* increment the count of excluded networks */ 442 454 exclude->count += 1; 443 455 } 456 444 457 445 458 /* Start the trace, if it errors return */
Note: See TracChangeset
for help on using the changeset viewer.