- Timestamp:
- 02/18/15 17:41:48 (6 years ago)
- Branches:
- 4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
- Children:
- e4f27d1
- Parents:
- 8bcc925
- Location:
- lib
- Files:
-
- 20 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/format_atmhdr.c
rb13b939 r5ab626a 227 227 NULL, /* get_filtered_packets */ 228 228 NULL, /* get_dropped_packets */ 229 NULL, /* get_ captured_packets */229 NULL, /* get_statistics */ 230 230 NULL, /* get_fd */ 231 231 trace_event_trace, /* trace_event */ -
lib/format_bpf.c
r9e429e8 r5ab626a 610 610 NULL, /* get_filtered_packets */ 611 611 bpf_get_dropped_packets,/* get_dropped_packets */ 612 NULL, /* get_ captured_packets */612 NULL, /* get_statistics */ 613 613 bpf_get_fd, /* get_fd */ 614 614 trace_event_device, /* trace_event */ … … 657 657 bpf_get_framing_length, /* get_framing_length */ 658 658 NULL, /* set_capture_length */ 659 NULL, /* get_received_packets */659 NULL, /* get_received_packets */ 660 660 NULL, /* get_filtered_packets */ 661 NULL, /* get_dropped_packets */662 NULL, /* get_ captured_packets */661 NULL, /* get_dropped_packets */ 662 NULL, /* get_statistics */ 663 663 NULL, /* get_fd */ 664 664 NULL, /* trace_event */ -
lib/format_dag24.c
r9e429e8 r5ab626a 554 554 NULL, /* get_filtered_packets */ 555 555 dag_get_dropped_packets, /* get_dropped_packets */ 556 NULL, /* get_ captured_packets */556 NULL, /* get_statistics */ 557 557 NULL, /* get_fd */ 558 558 trace_event_dag, /* trace_event */ -
lib/format_dag25.c
rde74f88 r5ab626a 1595 1595 NULL, /* get_filtered_packets */ 1596 1596 dag_get_dropped_packets, /* get_dropped_packets */ 1597 NULL, /* get_ captured_packets */1597 NULL, /* get_statistics */ 1598 1598 NULL, /* get_fd */ 1599 1599 trace_event_dag, /* trace_event */ … … 1607 1607 dag_pconfig_input, 1608 1608 dag_pregister_thread, 1609 NULL 1609 NULL, 1610 NULL /* get thread stats */ 1610 1611 }; 1611 1612 -
lib/format_dpdk.c
r4631115 r5ab626a 2166 2166 /* Get the drop counter */ 2167 2167 return (uint64_t) stats.ierrors; 2168 }2169 2170 static uint64_t dpdk_get_captured_packets (libtrace_t *trace) {2171 struct rte_eth_stats stats = {0};2172 2173 if (trace->format_data == NULL || FORMAT(trace)->port == 0xFF)2174 return UINT64_MAX;2175 /* Grab the current stats */2176 rte_eth_stats_get(FORMAT(trace)->port, &stats);2177 2178 /* Get the drop counter */2179 return (uint64_t) stats.ipackets;2180 2168 } 2181 2169 … … 2315 2303 dpdk_get_filtered_packets,/* get_filtered_packets */ 2316 2304 dpdk_get_dropped_packets,/* get_dropped_packets */ 2317 dpdk_get_captured_packets,/* get_captured_packets */2305 NULL, /* get_statistics */ 2318 2306 NULL, /* get_fd */ 2319 2307 dpdk_trace_event, /* trace_event */ 2320 2321 2322 2323 2308 dpdk_help, /* help */ 2309 NULL, /* next pointer */ 2310 {true, 8}, /* Live, NICs typically have 8 threads */ 2311 dpdk_pstart_input, /* pstart_input */ 2324 2312 dpdk_pread_packets, /* pread_packets */ 2325 2313 dpdk_pause_input, /* ppause */ 2326 2314 dpdk_fin_input, /* p_fin */ 2327 2315 dpdk_pconfig_input, /* pconfig_input */ 2328 dpdk_pregister_thread, /* pregister_thread */ 2329 dpdk_punregister_thread /* unpregister_thread */ 2316 dpdk_pregister_thread, /* pregister_thread */ 2317 dpdk_punregister_thread, /* punregister_thread */ 2318 NULL /* get thread stats */ 2330 2319 }; 2331 2320 -
lib/format_duck.c
r4631115 r5ab626a 362 362 NULL, /* get_filtered_packets */ 363 363 NULL, /* get_dropped_packets */ 364 NULL, /* get_ captured_packets */364 NULL, /* get_statistics */ 365 365 NULL, /* get_fd */ 366 366 NULL, /* trace_event */ -
lib/format_erf.c
r97d170d r5ab626a 833 833 NULL, /* get_filtered_packets */ 834 834 erf_get_dropped_packets, /* get_dropped_packets */ 835 NULL, /* get_ captured_packets */835 NULL, /* get_statistics */ 836 836 NULL, /* get_fd */ 837 837 erf_event, /* trace_event */ … … 877 877 NULL, /* get_filtered_packets */ 878 878 erf_get_dropped_packets, /* get_dropped_packets */ 879 NULL, /* get_ captured_packets */879 NULL, /* get_statistics */ 880 880 NULL, /* get_fd */ 881 881 erf_event, /* trace_event */ -
lib/format_legacy.c
rb13b939 r5ab626a 548 548 NULL, /* get_filtered_packets */ 549 549 NULL, /* get_dropped_packets */ 550 NULL, /* get_ captured_packets */550 NULL, /* get_statistics */ 551 551 NULL, /* get_fd */ 552 552 trace_event_trace, /* trace_event */ … … 592 592 NULL, /* get_filtered_packets */ 593 593 NULL, /* get_dropped_packets */ 594 NULL, /* get_ captured_packets */594 NULL, /* get_statistics */ 595 595 NULL, /* get_fd */ 596 596 trace_event_trace, /* trace_event */ … … 636 636 NULL, /* get_filtered_packets */ 637 637 NULL, /* get_dropped_packets */ 638 NULL, /* get_ captured_packets */638 NULL, /* get_statistics */ 639 639 NULL, /* get_fd */ 640 640 trace_event_trace, /* trace_event */ … … 680 680 NULL, /* get_filtered_packets */ 681 681 NULL, /* get_dropped_packets */ 682 NULL, /* get_ captured_packets */682 NULL, /* get_statistics */ 683 683 NULL, /* get_fd */ 684 684 trace_event_trace, /* trace_event */ -
lib/format_linux_common.c
red6304c5 r5ab626a 186 186 FORMAT_DATA->filter = NULL; 187 187 FORMAT_DATA->stats_valid = 0; 188 FORMAT_DATA->stats.tp_drops = 0; 189 FORMAT_DATA->stats.tp_packets = 0; 188 190 FORMAT_DATA->max_order = MAX_ORDER; 189 191 FORMAT_DATA->fanout_flags = PACKET_FANOUT_LB; … … 253 255 tmp_stats.if_name, 254 256 &tmp_stats.rx_bytes, 255 &tmp_stats.rx_ bytes,257 &tmp_stats.rx_packets, 256 258 &tmp_stats.rx_errors, 257 259 &tmp_stats.rx_drops, … … 402 404 sizeof(filter->filter)) == -1) { 403 405 perror("setsockopt(SO_ATTACH_FILTER)"); 404 } else { 405 /* The socket accepted the filter, so we need to 406 * consume any buffered packets that were received 407 * between opening the socket and applying the filter. 408 */ 409 void *buf = malloc((size_t)LIBTRACE_PACKET_BUFSIZE); 410 while(recv(stream->fd, 411 buf, 412 (size_t)LIBTRACE_PACKET_BUFSIZE, 413 MSG_DONTWAIT) != -1) { } 414 free(buf); 415 } 416 } 417 418 FORMAT_DATA->stats_valid = 0; 406 } 407 } 408 409 /* Consume any buffered packets that were received before the socket 410 * was properly setup, including those which missed the filter and 411 * bind()ing to an interface. 412 * 413 * If packet rate is high this could therotically hang forever. 4K 414 * should be a large enough limit. 415 */ 416 int count = 0; 417 void *buf = malloc((size_t)LIBTRACE_PACKET_BUFSIZE); 418 while(count < 4096 && 419 recv(stream->fd, 420 buf, 421 (size_t)LIBTRACE_PACKET_BUFSIZE, 422 MSG_DONTWAIT) != -1) { count++; } 423 free(buf); 424 fprintf(stderr, "set offset %d", count); 425 426 /* Mark that the stats are valid and apply an offset */ 427 FORMAT_DATA->stats_valid = 1; 428 /* Offset by number we ate for each stream */ 429 FORMAT_DATA->stats.tp_packets -= count; 430 431 419 432 if (linuxcommon_get_dev_statisitics(libtrace, &FORMAT_DATA->dev_stats) != 0) { 420 433 /* Mark this as bad */ … … 561 574 } 562 575 563 /* Number of packets that passed filtering */ 564 uint64_t linuxcommon_get_captured_packets(libtrace_t *libtrace) { 576 #define DEV_DIFF(x) (dev_stats.x - FORMAT_DATA->dev_stats.x) 577 /* Note these statistics come from two different sources, the socket itself and 578 * the linux device. As such this means it is highly likely that their is some 579 * margin of error in the returned statisitics, we perform basic sanitising so 580 * that these are not too noticable. 581 */ 582 void linuxcommon_get_statistics(libtrace_t *libtrace, libtrace_stat_t *stat) { 583 struct linux_dev_stats dev_stats; 565 584 if (libtrace->format_data == NULL) 566 return UINT64_MAX; 585 return; 586 /* Do we need to consider the case after the trace is closed? */ 567 587 if (FORMAT_DATA_FIRST->fd == -1) { 568 588 /* This is probably a 'dead' trace so obviously we can't query 569 589 * the socket for capture counts, can we? */ 570 return UINT64_MAX; 590 return; 591 } 592 593 dev_stats.if_name[0] = 0; /* This will be set if we retrive valid stats */ 594 /* Do we have starting stats to compare to? */ 595 if (FORMAT_DATA->dev_stats.if_name[0] != 0) { 596 linuxcommon_get_dev_statisitics(libtrace, &dev_stats); 571 597 } 572 598 linuxcommon_update_socket_statistics(libtrace); 573 if (FORMAT_DATA->stats_valid) 574 return FORMAT_DATA->stats.tp_packets; 575 else 576 return UINT64_MAX; 577 } 578 579 /* Number of packets that got past filtering and were then dropped because 580 * of lack of space. 581 * 582 * We could also try read from /sys/class/net/ethX/statistics/ to get 583 * real drop counters and stuff. 584 */ 585 uint64_t linuxcommon_get_dropped_packets(libtrace_t *libtrace) { 586 struct linux_dev_stats dev_stats; 587 uint64_t adjustment = 0; 588 if (libtrace->format_data == NULL) 589 return UINT64_MAX; 590 if (FORMAT_DATA_FIRST->fd == -1) { 591 /* This is probably a 'dead' trace so obviously we can't query 592 * the socket for drop counts, can we? */ 593 return UINT64_MAX; 594 } 595 // Do we have starting stats to compare to? 596 if (FORMAT_DATA->dev_stats.if_name[0] != 0) { 597 if (linuxcommon_get_dev_statisitics(libtrace, &dev_stats) == 0) { 598 adjustment = dev_stats.rx_drops - FORMAT_DATA->dev_stats.rx_drops; 599 } 600 } 601 linuxcommon_update_socket_statistics(libtrace); 602 if (FORMAT_DATA->stats_valid) 603 return FORMAT_DATA->stats.tp_drops + adjustment; 604 else 605 return UINT64_MAX; 606 } 607 608 /* Linux doesn't keep track how many packets were seen before filtering 609 * so we can't tell how many packets were filtered. Bugger. So annoying. 610 * 611 * Since we tell libtrace that we do support filtering, if we don't declare 612 * this here as failing, libtrace will happily report for us that it didn't 613 * filter any packets, so don't lie -- return that we don't know. 614 */ 615 uint64_t linuxcommon_get_filtered_packets(libtrace_t *trace UNUSED) { 616 return UINT64_MAX; 599 600 /* filtered count == dev received - socket received */ 601 if (FORMAT_DATA->filter == NULL) { 602 stat->filtered_valid = 1; 603 stat->filtered = 0; 604 } else if (FORMAT_DATA->stats_valid && dev_stats.if_name[0]) { 605 stat->filtered_valid = 1; 606 stat->filtered = DEV_DIFF(rx_packets) - 607 FORMAT_DATA->stats.tp_packets; 608 if (stat->filtered > UINT64_MAX - 100000) { 609 stat->filtered = 0; 610 } 611 } 612 613 /* dropped count == socket dropped + dev dropped */ 614 if (FORMAT_DATA->stats_valid) { 615 stat->dropped_valid = 1; 616 stat->dropped = FORMAT_DATA->stats.tp_drops; 617 if (dev_stats.if_name[0]) { 618 stat->dropped += DEV_DIFF(rx_drops); 619 } 620 } 621 622 /* received count - All good packets even those dropped or filtered */ 623 if (dev_stats.if_name[0]) { 624 stat->received_valid = 1; 625 stat->received = DEV_DIFF(rx_packets) + DEV_DIFF(rx_drops); 626 } 627 628 /* captured count - received and but not dropped */ 629 if (dev_stats.if_name[0] && FORMAT_DATA->stats_valid) { 630 stat->captured_valid = 1; 631 stat->captured = DEV_DIFF(rx_packets) - FORMAT_DATA->stats.tp_drops; 632 } 633 634 /* errors */ 635 if (dev_stats.if_name[0]) { 636 stat->errors_valid = 1; 637 stat->errors = DEV_DIFF(rx_errors); 638 } 639 617 640 } 618 641 … … 671 694 672 695 /* No NETPACKET - So this format is not live */ 673 uint64_t linuxcommon_get_filtered_packets(libtrace_t *trace UNUSED) { 674 return UINT64_MAX; 675 } 676 677 uint64_t linuxcommon_get_captured_packets(libtrace_t *trace UNUSED) { 678 return UINT64_MAX; 679 } 680 681 uint64_t linuxcommon_get_dropped_packets(libtrace_t *trace UNUSED) { 682 return UINT64_MAX; 696 void linuxcommon_get_statistics(libtrace_t *libtrace UNUSED, 697 libtrace_stat_t *stat UNUSED) { 698 return; 683 699 } 684 700 -
lib/format_linux_common.h
red6304c5 r5ab626a 325 325 #endif /* HAVE_NETPACKET_PACKET_H */ 326 326 327 uint64_t linuxcommon_get_captured_packets(libtrace_t *libtrace); 328 uint64_t linuxcommon_get_filtered_packets(libtrace_t *libtrace); 329 uint64_t linuxcommon_get_dropped_packets(libtrace_t *libtrace); 327 void linuxcommon_get_statistics(libtrace_t *libtrace, libtrace_stat_t *stat); 330 328 inline libtrace_direction_t linuxcommon_get_direction(uint8_t pkttype); 331 329 inline libtrace_direction_t linuxcommon_set_direction(struct sockaddr_ll * skadr, -
lib/format_linux_int.c
r6cf3ca0 r5ab626a 508 508 linuxnative_set_capture_length, /* set_capture_length */ 509 509 NULL, /* get_received_packets */ 510 linuxcommon_get_filtered_packets,/* get_filtered_packets */511 linuxcommon_get_dropped_packets,/* get_dropped_packets */512 linuxcommon_get_ captured_packets,/* get_captured_packets */510 NULL, /* get_filtered_packets */ 511 NULL, /* get_dropped_packets */ 512 linuxcommon_get_statistics, /* get_statistics */ 513 513 linuxcommon_get_fd, /* get_fd */ 514 514 trace_event_device, /* trace_event */ 515 515 linuxnative_help, /* help */ 516 NULL, /* next pointer */ 517 {true, -1}, /* Live, no thread limit */ 518 linuxnative_pstart_input, /* pstart_input */ 519 linuxnative_pread_packets, /* pread_packets */ 520 linuxcommon_pause_input, /* ppause */ 521 linuxcommon_fin_input, /* p_fin */ 522 linuxcommon_pconfig_input, /* pconfig input */ 523 linuxcommon_pregister_thread, 524 NULL 516 NULL, /* next pointer */ 517 {true, -1}, /* Live, no thread limit */ 518 linuxnative_pstart_input, /* pstart_input */ 519 linuxnative_pread_packets, /* pread_packets */ 520 linuxcommon_pause_input, /* ppause */ 521 linuxcommon_fin_input, /* p_fin */ 522 linuxcommon_pconfig_input, /* pconfig input */ 523 linuxcommon_pregister_thread, /* register thread */ 524 NULL, /* unregister thread */ 525 NULL /* get thread stats */ 525 526 }; 526 527 #else … … 564 565 linuxnative_set_capture_length, /* set_capture_length */ 565 566 NULL, /* get_received_packets */ 566 linuxnative_get_filtered_packets,/* get_filtered_packets */567 linuxnative_get_dropped_packets,/* get_dropped_packets */568 linux native_get_captured_packets,/* get_captured_packets */567 NULL, /* get_filtered_packets */ 568 NULL, /* get_dropped_packets */ 569 linuxcommon_get_statistics, /* get_statistics */ 569 570 linuxnative_get_fd, /* get_fd */ 570 571 trace_event_device, /* trace_event */ -
lib/format_linux_ring.c
r3e89670 r5ab626a 745 745 linuxring_set_capture_length, /* set_capture_length */ 746 746 NULL, /* get_received_packets */ 747 linuxcommon_get_filtered_packets,/* get_filtered_packets */748 linuxcommon_get_dropped_packets,/* get_dropped_packets */749 linuxcommon_get_ captured_packets,/* get_captured_packets */747 NULL, /* get_filtered_packets */ 748 NULL, /* get_dropped_packets */ 749 linuxcommon_get_statistics, /* get_statistics */ 750 750 linuxcommon_get_fd, /* get_fd */ 751 751 linuxring_event, /* trace_event */ … … 757 757 linuxcommon_pause_input, /* ppause */ 758 758 linuxcommon_fin_input, /* p_fin */ 759 linuxcommon_pconfig_input, /* pconfig input */ 760 linuxcommon_pregister_thread, 761 NULL 759 linuxcommon_pconfig_input, /* pconfig input */ 760 linuxcommon_pregister_thread, /* register thread */ 761 NULL, /* unregister thread */ 762 NULL /* get thread stats */ 762 763 }; 763 764 #else … … 805 806 NULL, /* get_filtered_packets */ 806 807 NULL, /* get_dropped_packets */ 807 NULL, /* get_captured_packets */808 linuxcommon_get_statistics, /* get_statistics */ 808 809 NULL, /* get_fd */ 809 810 NULL, /* trace_event */ -
lib/format_pcap.c
r9e429e8 r5ab626a 830 830 NULL, /* get_filtered_packets */ 831 831 NULL, /* get_dropped_packets */ 832 NULL, /* get_ captured_packets */832 NULL, /* get_statistics */ 833 833 NULL, /* get_fd */ 834 834 trace_event_trace, /* trace_event */ … … 874 874 NULL, /* get_filtered_packets */ 875 875 pcap_get_dropped_packets, /* get_dropped_packets */ 876 NULL, /* get_ captured_packets */876 NULL, /* get_statistics */ 877 877 pcap_get_fd, /* get_fd */ 878 878 trace_event_device, /* trace_event */ -
lib/format_pcapfile.c
r97d170d r5ab626a 782 782 NULL, /* get_filtered_packets */ 783 783 NULL, /* get_dropped_packets */ 784 NULL, /* get_ captured_packets */784 NULL, /* get_statistics */ 785 785 NULL, /* get_fd */ 786 786 pcapfile_event, /* trace_event */ -
lib/format_rt.c
r4631115 r5ab626a 859 859 NULL, /* get_filtered_packets */ 860 860 NULL, /* get_dropped_packets */ 861 NULL, /* get_ captured_packets */861 NULL, /* get_statistics */ 862 862 rt_get_fd, /* get_fd */ 863 863 trace_event_rt, /* trace_event */ -
lib/format_tsh.c
r97d170d r5ab626a 270 270 NULL, /* get_filtered_packets */ 271 271 NULL, /* get_dropped_packets */ 272 NULL, /* get_ captured_packets */272 NULL, /* get_statistics */ 273 273 NULL, /* get_fd */ 274 274 trace_event_trace, /* trace_event */ … … 319 319 NULL, /* get_filtered_packets */ 320 320 NULL, /* get_dropped_packets */ 321 NULL, /* get_ captured_packets */321 NULL, /* get_statistics */ 322 322 NULL, /* get_fd */ 323 323 trace_event_trace, /* trace_event */ -
lib/libtrace.h.in
r4631115 r5ab626a 63 63 64 64 #include <sys/types.h> 65 #include <stddef.h> 66 #include <stdio.h> 67 /* Compile time assertion. Sourced from: 68 * http://www.pixelbeat.org/programming/gcc/static_assert.html */ 69 #define ct_assert(e) extern char (*ct_assert(void)) [sizeof(char[1 - 2*!(e)])] 70 65 71 #ifndef WIN32 66 72 #include <sys/time.h> … … 1308 1314 } trace_option_output_t; 1309 1315 1316 /* To add a new stat field update this list, and the relevent places in 1317 * libtrace_stat_t structure. 1318 */ 1319 /** An X Macro set for libtrace stat fields */ 1320 #define LIBTRACE_STAT_FIELDS \ 1321 X(accepted) \ 1322 X(filtered) \ 1323 X(received) \ 1324 X(dropped) \ 1325 X(captured) \ 1326 X(errors) 1327 1328 /** 1329 * Statistic counters are cumulative from the time the trace is started. 1330 * Trace pause will reset the counters, to zero. 1331 * Always check \a{field}_valid is set before attempting to read the stored value. 1332 * 1333 * @note Always allocate this structure using trace_create_statistics(). 1334 * This allows us to extend the structure in the future. 1335 */ 1336 typedef struct libtrace_stat_t { 1337 // Align to 64 bits, because the compiler is likely to do this anyway. 1338 #if __BYTE_ORDER == __LITTLE_ENDIAN 1339 #define X(name) LT_BITFIELD64 name ##_valid : 1; 1340 LIBTRACE_STAT_FIELDS 1341 #undef X 1342 /* We use the remaining space as magic to ensure the structure 1343 * was alloc'd by us. We can easily decrease the no. bits without 1344 * problems as long as we update any asserts as needed */ 1345 LT_BITFIELD64 reserved1: 26; /**< Bits reserved for future fields */ 1346 LT_BITFIELD64 reserved2: 24; /**< Bits reserved for future fields */ 1347 LT_BITFIELD64 magic: 8; /**< A number stored against the format to 1348 ensure the struct was allocated correctly */ 1349 #else 1350 LT_BITFIELD64 magic: 8; 1351 LT_BITFIELD64 reserved2: 24; 1352 LT_BITFIELD64 reserved1: 26; 1353 LT_BITFIELD64 dropped_valid: 1; 1354 LT_BITFIELD64 received_valid: 1; 1355 LT_BITFIELD64 filtered_valid: 1; 1356 LT_BITFIELD64 accepted_valid: 1; 1357 #endif 1358 /* These must all be uint64_t's, match this order with the X macro */ 1359 uint64_t accepted; 1360 /**< The number of packets that have been read from the input trace 1361 * using trace_read_packet(). In the parallel framework this may include 1362 * some packets waiting in a batch yet to be seen by the user's 1363 * application. 1364 * 1365 * This does not include filtered packets because these do not make it 1366 * to trace_read_packet(). 1367 * 1368 * @note This field replaces trace_get_accepted_packets() 1369 */ 1370 uint64_t filtered; 1371 /**< The number of packets that were captured, but discarded for not 1372 * matching a provided filter. 1373 * 1374 * @note This field replaces trace_get_filtered_packets() 1375 */ 1376 uint64_t received; 1377 /**< The total number of good packets which have been recevied. Including 1378 * those which are dropped and filtered. This does not include errors. 1379 * 1380 * @note This may include a small number of buffered packets 1381 * not yet included in accepted. 1382 * 1383 * @note This field replaces trace_get_received_packets() 1384 */ 1385 uint64_t dropped; 1386 /**< The number of packets that have been dropped on an input trace 1387 * due to lack of buffer space on the capturing device. This does not 1388 * included errored packets which are dropped directly by the card. 1389 * 1390 * @note This field replaces trace_get_dropped_packets() 1391 */ 1392 uint64_t captured; 1393 /**< The number of received packets that have not been dropped. This 1394 * includes filtered packets. 1395 * 1396 * This is equivalent to received-dropped. 1397 * 1398 * @note This may include a small number of buffered packets 1399 * not yet included in accepted. 1400 */ 1401 uint64_t errors; 1402 /**< The number of packets that have been discarded by the network card 1403 * because they are invalid. That includes FCS mismatches, incorrect 1404 * packet lengths etc. 1405 */ 1406 } libtrace_stat_t; 1407 1408 ct_assert(offsetof(libtrace_stat_t, accepted) == 8); 1409 1310 1410 /** Sets an output config option 1311 1411 * … … 1399 1499 * 1400 1500 * If the number is not known, this function will return UINT64_MAX 1401 */ 1402 DLLEXPORT 1501 * 1502 * @deprecated This function is deprecated: Use trace_get_statistics(), this 1503 * allows all statistic counters to be read in an atomic manner. 1504 */ 1505 DLLEXPORT DEPRECATED 1403 1506 uint64_t trace_get_received_packets(libtrace_t *trace); 1404 1507 … … 1410 1513 * 1411 1514 * If the number is not known, this function will return UINT64_MAX 1412 */ 1413 DLLEXPORT 1515 * 1516 * @deprecated This function is deprecated: Use trace_get_statistics(), this 1517 * allows all statistic counters to be read in an atomic manner. 1518 */ 1519 DLLEXPORT DEPRECATED 1414 1520 uint64_t trace_get_filtered_packets(libtrace_t *trace); 1415 1521 … … 1421 1527 * 1422 1528 * If the number is not known, this function will return UINT64_MAX 1423 */ 1424 DLLEXPORT 1529 * 1530 * @deprecated This function is deprecated: Use trace_get_statistics(), this 1531 * allows all statistic counters to be read in an atomic manner. 1532 */ 1533 DLLEXPORT DEPRECATED 1425 1534 uint64_t trace_get_dropped_packets(libtrace_t *trace); 1426 1535 … … 1432 1541 * 1433 1542 * If the number is not known, this function will return UINT64_MAX 1543 * 1544 * @deprecated This function is deprecated: Use trace_get_statistics(), this 1545 * allows all statistic counters to be read in an atomic manner. 1546 */ 1547 DLLEXPORT DEPRECATED 1548 uint64_t trace_get_accepted_packets(libtrace_t *trace); 1549 1550 /** 1551 * Returns statistic counters for a trace, for a parallel trace this is a 1552 * combined total. 1553 * Where possible these are retrived atomically, however this behaviour depends 1554 * on the underlying trace format. 1555 * 1556 * @param trace The input trace to examine. 1557 * @param stats Optional, Filled upon return with statistics about the trace, 1558 * check the flags included to see if a given statistic is valid. 1559 * If NULL a statistic structure owned by libtrace is returned, this 1560 * should not be free'd and will become invalid if the trace is 1561 * destroyed. 1562 * 1563 * @return A pointer to the statistics structure. 1564 * @note Use trace_create_stat() to create the stats object, this way future 1565 * versions of libtrace can add to the structure without breaking existing 1566 * code. 1434 1567 */ 1435 1568 DLLEXPORT 1436 uint64_t trace_get_accepted_packets(libtrace_t *trace); 1569 libtrace_stat_t *trace_get_statistics(libtrace_t *trace, libtrace_stat_t *stats); 1570 1571 1572 /** 1573 * Returns statistic counters for a single thread of a trace. 1574 * Where possible these are retrived atomically, however this behaviour depends 1575 * on the underlying trace format. 1576 * 1577 * @param trace The input trace to examine. 1578 * @param t An optional thread to received stats for or NULL to retrive stats 1579 * for the current thread 1580 * @param stats Filled upon return with statistics about the trace, check the 1581 * flags included to see if a given statistic is valid. 1582 * 1583 * @note Use trace_create_stat() to create the stats object, this way future 1584 * versions of libtrace can add to the structure without breaking existing 1585 * code. 1586 */ 1587 DLLEXPORT 1588 void trace_get_thread_statistics(libtrace_t *trace, libtrace_thread_t *t, 1589 libtrace_stat_t *stats); 1590 1591 /** 1592 * Creates and returns a zeroed libtrace_stat_t structure. 1593 * 1594 * This allows us to add extra fields increasing the size of the structure 1595 * without breaking existing libtrace applications. 1596 * 1597 * This structure should be free'd using free(). 1598 * 1599 * @return A valid pointer to a libtrace_stat_t struct otherwise NULL if 1600 * memory was not available. 1601 */ 1602 DLLEXPORT libtrace_stat_t* trace_create_statistics(void); 1603 1604 /** 1605 * Performs the operation c=a-b accounting for valid fields. 1606 * c is allowed to be a or b. 1607 * 1608 * @param a The minuend 1609 * @param b The subtrahend 1610 * @param c The resulting difference 1611 */ 1612 DLLEXPORT 1613 void trace_subtract_statistics(const libtrace_stat_t *a, 1614 const libtrace_stat_t *b, libtrace_stat_t *c); 1615 1616 /** 1617 * Performs operation c=a+b accounting for valid fields. 1618 * c is allowed to be a or b. 1619 * 1620 * @param a The first operand 1621 * @param b The second operand 1622 * @param c The result 1623 */ 1624 DLLEXPORT 1625 void trace_add_statistics(const libtrace_stat_t *a, 1626 const libtrace_stat_t *b, libtrace_stat_t *c); 1627 1628 /** 1629 * Prints all valid stats to a file stream, (which could be stdout/err). 1630 * By default the format "name: value" is used. 1631 * 1632 * @param s The statistic structure to print 1633 * @param f The output file stream 1634 * @param format An optional format string such as the default ("%s: %"PRIu64"\n") 1635 * This is passed to fprintf and called with two arguments 1636 * first a char* (%s) and second a uint64_t (%PRIu64). 1637 * 1638 * @return -1 if an error occurs when writing to the file stream, check errno. 1639 * Otherwise 0. 1640 */ 1641 DLLEXPORT 1642 int trace_print_statistics(const libtrace_stat_t *s, FILE *f, 1643 const char *format); 1437 1644 1438 1645 … … 3266 3473 /* C99 Integer types */ 3267 3474 /* NOTE: Standard doesn't require 64-bit 3268 3475 * but x32 and x64 gcc does */ 3269 3476 int64_t sint64; 3270 3477 uint64_t uint64; -
lib/libtrace_int.h
r0b01fea r5ab626a 325 325 * paused or stopped before this counter is taken 326 326 */ 327 uint64_t dropped_packets; 328 uint64_t received_packets; 327 libtrace_stat_t *stats; 329 328 struct user_configuration config; 330 329 libtrace_combine_t combiner; 331 330 }; 331 332 #define LIBTRACE_STAT_MAGIC 0x41 332 333 333 334 void trace_fin_packet(libtrace_packet_t *packet); … … 457 458 458 459 /** Given a filename, return if this is the most likely capture format 459 460 * (used for devices). Used to "guess" the capture format when the 460 461 * URI is not fully specified. 461 462 * 462 463 * @param fname The name of the device or file to examine 463 464 * @return 1 if the name matches the capture format, 0 otherwise 464 465 */ 465 466 int (*probe_filename)(const char *fname); 466 467 … … 834 835 */ 835 836 uint64_t (*get_dropped_packets)(libtrace_t *trace); 836 837 /** Returns the number of packets captured and returned by an input 838 * trace. 839 * 840 * @param trace The input trace to get the capture count for 841 * @return The number of packets returned to the libtrace user, or 842 * UINT64_MAX if the number is unknown 843 * 844 * This is the number of packets that have been successfully returned 845 * to the libtrace user via the read_packet() function. 846 * 847 */ 848 uint64_t (*get_captured_packets)(libtrace_t *trace); 837 838 /** Returns statistics about a trace. Flags are all set to 0 when 839 * invoked. 840 * 841 * @param trace The libtrace object 842 * @param stat A zeroed structure ready to be filled. 843 */ 844 void (*get_statistics)(libtrace_t *trace, libtrace_stat_t *stat); 849 845 850 846 /** Returns the file descriptor used by the input trace. … … 958 954 */ 959 955 void (*punregister_thread)(libtrace_t *libtrace, libtrace_thread_t *t); 956 957 /** 958 * Return statistics for a single thread. 959 */ 960 void (*get_thread_statistics)(libtrace_t *libtrace, 961 libtrace_thread_t *t, 962 libtrace_stat_t *stat); 960 963 }; 961 964 962 965 /** Macro to zero out a single thread format */ 963 966 #define NON_PARALLEL(live) \ 964 {live, 1}, /* trace info */ \ 965 NULL, /* pstart_input */ \ 966 NULL, /* pread_packet */ \ 967 NULL, /* ppause_input */ \ 968 NULL, /* pfin_input */ \ 969 NULL, /* pconfig_input */ \ 970 NULL, /* pregister_thread */ \ 971 NULL /* punregister_thread */ 967 {live, 1}, /* trace info */ \ 968 NULL, /* pstart_input */ \ 969 NULL, /* pread_packet */ \ 970 NULL, /* ppause_input */ \ 971 NULL, /* pfin_input */ \ 972 NULL, /* pconfig_input */ \ 973 NULL, /* pregister_thread */ \ 974 NULL, /* punregister_thread */ \ 975 NULL, /* get_thread_statistics */ 972 976 973 977 /** The list of registered capture formats */ -
lib/trace.c
r4631115 r5ab626a 280 280 libtrace->first_packets.count = 0; 281 281 libtrace->first_packets.packets = NULL; 282 libtrace->dropped_packets = UINT64_MAX; 283 libtrace->received_packets = UINT64_MAX; 282 libtrace->stats = NULL; 284 283 libtrace->pread = NULL; 285 284 ZERO_USER_CONFIG(libtrace->config); … … 397 396 libtrace->perpkt_threads = NULL; 398 397 libtrace->tracetime = 0; 398 libtrace->stats = NULL; 399 399 libtrace->pread = NULL; 400 400 ZERO_USER_CONFIG(libtrace->config); … … 660 660 if (libtrace->uridata) 661 661 free(libtrace->uridata); 662 663 if (libtrace->stats) 664 free(libtrace->stats); 662 665 663 666 /* Empty any packet memory */ … … 1951 1954 if ((ret = trace->format->get_received_packets(trace)) != UINT64_MAX) 1952 1955 return ret; 1953 } 1954 // Read this cached value taken before the trace was closed 1955 return trace->received_packets; 1956 } else if (trace->format->get_statistics) { 1957 struct libtrace_stat_t stat; 1958 stat.magic = LIBTRACE_STAT_MAGIC; 1959 trace_get_statistics(trace, &stat); 1960 if (stat.received_valid) 1961 return stat.received; 1962 } 1963 1964 // Read the cached value taken before the trace was paused/closed 1965 if(trace->stats && trace->stats->received_valid) 1966 return trace->stats->received; 1967 else 1968 return UINT64_MAX; 1956 1969 } 1957 1970 … … 1960 1973 assert(trace); 1961 1974 int i = 0; 1962 uint64_t ret= trace->filtered_packets;1975 uint64_t lib_filtered = trace->filtered_packets; 1963 1976 for (i = 0; i < trace->perpkt_thread_count; i++) { 1964 ret+= trace->perpkt_threads[i].filtered_packets;1977 lib_filtered += trace->perpkt_threads[i].filtered_packets; 1965 1978 } 1966 1979 if (trace->format->get_filtered_packets) { 1967 return trace->format->get_filtered_packets(trace)+ 1968 ret; 1969 } 1970 return ret; 1980 uint64_t trace_filtered = trace->format->get_filtered_packets(trace); 1981 if (trace_filtered == UINT64_MAX) 1982 return UINT64_MAX; 1983 else 1984 return trace_filtered + lib_filtered; 1985 } else if (trace->format->get_statistics) { 1986 struct libtrace_stat_t stat; 1987 stat.magic = LIBTRACE_STAT_MAGIC; 1988 trace_get_statistics(trace, &stat); 1989 if (stat.filtered_valid) 1990 return lib_filtered + stat.filtered; 1991 else 1992 return UINT64_MAX; 1993 } 1994 1995 // Read the cached value taken before the trace was paused/closed 1996 if(trace->stats && trace->stats->filtered_valid) 1997 return trace->stats->filtered + lib_filtered; 1998 else 1999 return lib_filtered; 1971 2000 } 1972 2001 … … 1979 2008 if ((ret = trace->format->get_dropped_packets(trace)) != UINT64_MAX) 1980 2009 return ret; 1981 } 1982 // Read this cached value taken before the trace was closed 1983 return trace->dropped_packets; 2010 } else if (trace->format->get_statistics) { 2011 struct libtrace_stat_t stat; 2012 stat.magic = LIBTRACE_STAT_MAGIC; 2013 trace_get_statistics(trace, &stat); 2014 if (stat.dropped_valid) 2015 return stat.dropped; 2016 } 2017 2018 // Read the cached value taken before the trace was paused/closed 2019 if(trace->stats && trace->stats->dropped_valid) 2020 return trace->stats->dropped; 2021 else 2022 return UINT64_MAX; 1984 2023 } 1985 2024 … … 1994 2033 return ret; 1995 2034 } 2035 2036 libtrace_stat_t *trace_get_statistics(libtrace_t *trace, libtrace_stat_t *stat) 2037 { 2038 uint64_t ret; 2039 int i; 2040 assert(trace); 2041 if (stat == NULL) { 2042 if (trace->stats == NULL) 2043 trace->stats = trace_create_statistics(); 2044 stat = trace->stats; 2045 } 2046 assert(stat->magic == LIBTRACE_STAT_MAGIC && "Please use" 2047 "trace_create_statistics() to allocate statistics"); 2048 stat->reserved1 = 0; 2049 stat->reserved2 = 0; 2050 #define X(x) stat->x ##_valid = 0; 2051 LIBTRACE_STAT_FIELDS; 2052 #undef X 2053 if (trace->state == STATE_PAUSED || 2054 trace->state == STATE_FINSHED || 2055 trace->state == STATE_JOINED) { 2056 if (trace->stats && trace->stats != stat) 2057 *stat = *trace->stats; 2058 return stat; 2059 } else if (trace->format->get_statistics) { 2060 trace->format->get_statistics(trace, stat); 2061 ret = trace_get_accepted_packets(trace); 2062 if (ret != UINT64_MAX) { 2063 stat->accepted_valid = 1; 2064 stat->accepted = ret; 2065 } 2066 /* Now add on any library filtered packets */ 2067 if (stat->filtered_valid) { 2068 stat->filtered += trace->filtered_packets; 2069 for (i = 0; i < trace->perpkt_thread_count; i++) { 2070 stat->filtered += trace->perpkt_threads[i].filtered_packets; 2071 } 2072 } 2073 return stat; 2074 } 2075 ret = trace_get_accepted_packets(trace); 2076 if (ret != UINT64_MAX) { 2077 stat->accepted_valid = 1; 2078 stat->accepted = ret; 2079 } 2080 ret = trace_get_received_packets(trace); 2081 if (ret != UINT64_MAX) { 2082 stat->received_valid = 1; 2083 stat->received = ret; 2084 } 2085 /* Fallback to the old way */ 2086 ret = trace_get_dropped_packets(trace); 2087 if (ret != UINT64_MAX) { 2088 stat->dropped_valid = 1; 2089 stat->dropped = ret; 2090 } 2091 ret = trace_get_filtered_packets(trace); 2092 if (ret != UINT64_MAX) { 2093 stat->filtered_valid = 1; 2094 stat->filtered = ret; 2095 } 2096 return stat; 2097 } 2098 2099 void trace_get_thread_statistics(libtrace_t *trace, libtrace_thread_t *t, 2100 libtrace_stat_t *stat) 2101 { 2102 assert(trace && stat); 2103 assert(stat->magic == LIBTRACE_STAT_MAGIC && "Please use" 2104 "trace_create_statistics() to allocate statistics"); 2105 stat->reserved1 = 0; 2106 stat->reserved2 = 0; 2107 #define X(x) stat->x ##_valid= 0; 2108 LIBTRACE_STAT_FIELDS; 2109 #undef X 2110 if (trace->format->get_thread_statistics) { 2111 trace->format->get_thread_statistics(trace, t, stat); 2112 } 2113 if (t->accepted_packets != UINT64_MAX) { 2114 stat->accepted_valid = 1; 2115 stat->accepted = t->accepted_packets; 2116 } 2117 /* Now add on any library filtered packets */ 2118 if (stat->filtered_valid) { 2119 stat->filtered += t->filtered_packets; 2120 } 2121 return; 2122 } 2123 2124 libtrace_stat_t *trace_create_statistics(void) { 2125 libtrace_stat_t *ret; 2126 ret = malloc(sizeof(libtrace_stat_t)); 2127 if (ret) { 2128 memset(ret, 0, sizeof(libtrace_stat_t)); 2129 ret->magic = LIBTRACE_STAT_MAGIC; 2130 } 2131 return ret; 2132 } 2133 2134 void trace_subtract_statistics(const libtrace_stat_t *a, const libtrace_stat_t *b, 2135 libtrace_stat_t *c) { 2136 assert(a->magic == LIBTRACE_STAT_MAGIC && "Please use" 2137 "trace_create_statistics() to allocate statistics"); 2138 assert(b->magic == LIBTRACE_STAT_MAGIC && "Please use" 2139 "trace_create_statistics() to allocate statistics"); 2140 assert(c->magic == LIBTRACE_STAT_MAGIC && "Please use" 2141 "trace_create_statistics() to allocate statistics"); 2142 2143 #define X(x) \ 2144 if (a->x ##_valid && b->x ##_valid) { \ 2145 c->x ##_valid = 1; \ 2146 c->x = a->x - b->x; \ 2147 } else {\ 2148 c->x ##_valid = 0;\ 2149 } 2150 LIBTRACE_STAT_FIELDS 2151 #undef X 2152 } 2153 2154 void trace_add_statistics(const libtrace_stat_t *a, const libtrace_stat_t *b, 2155 libtrace_stat_t *c) { 2156 assert(a->magic == LIBTRACE_STAT_MAGIC && "Please use" 2157 "trace_create_statistics() to allocate statistics"); 2158 assert(b->magic == LIBTRACE_STAT_MAGIC && "Please use" 2159 "trace_create_statistics() to allocate statistics"); 2160 assert(c->magic == LIBTRACE_STAT_MAGIC && "Please use" 2161 "trace_create_statistics() to allocate statistics"); 2162 2163 #define X(x) \ 2164 if (a->x ##_valid&& b->x ##_valid) { \ 2165 c->x ##_valid = 1; \ 2166 c->x = a->x + b->x; \ 2167 } else {\ 2168 c->x ##_valid = 0;\ 2169 } 2170 LIBTRACE_STAT_FIELDS 2171 #undef X 2172 } 2173 2174 int trace_print_statistics(const libtrace_stat_t *s, FILE *f, const char *format) { 2175 assert(s->magic == LIBTRACE_STAT_MAGIC && "Please use" 2176 "trace_create_statistics() to allocate statistics"); 2177 if (format == NULL) 2178 format = "%s: %"PRIu64"\n"; 2179 #define xstr(s) str(s) 2180 #define str(s) #s 2181 #define X(x) \ 2182 if (s->x ##_valid) { \ 2183 if (fprintf(f, format, xstr(x), s->x) < 0) \ 2184 return -1; \ 2185 } 2186 LIBTRACE_STAT_FIELDS 2187 #undef X 2188 return 0; 2189 } 2190 1996 2191 1997 2192 void trace_clear_cache(libtrace_packet_t *packet) { -
lib/trace_parallel.c
r8bcc925 r5ab626a 1906 1906 1907 1907 /* Cache values before we pause */ 1908 libtrace->dropped_packets = trace_get_dropped_packets(libtrace); 1909 libtrace->received_packets = trace_get_received_packets(libtrace); 1910 uint64_t tmp_stats; 1911 if (libtrace->format->get_filtered_packets) { 1912 if ((tmp_stats = libtrace->format->get_filtered_packets(libtrace)) != UINT64_MAX) { 1913 libtrace->filtered_packets += tmp_stats; 1914 } 1915 } 1908 if (libtrace->stats == NULL) 1909 libtrace->stats = trace_create_statistics(); 1910 // Save the statistics against the trace 1911 trace_get_statistics(libtrace, NULL); 1916 1912 if (trace_supports_parallel(libtrace) && !trace_has_dedicated_hasher(libtrace) && libtrace->perpkt_thread_count > 1) { 1917 1913 libtrace->started = false;
Note: See TracChangeset
for help on using the changeset viewer.