source: tools/tracestats/tracestats.c @ 5ab626a

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 5ab626a was 5ab626a, checked in by Richard Sanger <rsangerarj@…>, 6 years ago

Deprecate trace_get_filtered/accepted/recevied/dropped() in favour of a single function

Adds the single trace_get_statistics function. This allows the structure to be filled
at a point in time, rather than making multiple calls to the library during which state
might have changed.

This has been designed such that the structure can be added to in the future without
breaking old code.

The old internal get_captured_packets was removed from the formats as it was never used.
Eventually we should completely remove get_filtered and received from the formats and replace
them with get_statistics.

In additon some extra fields have added, such as error and captured and the pre-existing
fields are better defined.

The linux formats have been updated to use this new API, which combined with reading
/proc/net/dev returns a full set of statistics.

  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[130a0e4]1/*
2 * This file is part of libtrace
3 *
[d5a27e8]4 * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand.
[130a0e4]5 * Authors: Daniel Lawson
6 *          Perry Lorier
7 *         
8 * All rights reserved.
9 *
10 * This code has been developed by the University of Waikato WAND
11 * research group. For further information please see http://www.wand.net.nz/
12 *
13 * libtrace is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * libtrace is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with libtrace; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 *
27 * $Id$
28 *
29 */
30
[853603a]31/*
32 * This program takes a series of traces and bpf filters and outputs how many
33 * bytes/packets
34 */
[130a0e4]35
36#include <stdio.h>
37#include <stdlib.h>
38#include <assert.h>
39#include <string.h>
40#include <sys/time.h>
41#include <sys/types.h>
42#include <time.h>
43
44#include <netinet/in.h>
45#include <netinet/in_systm.h>
46#include <netinet/tcp.h>
47#include <netinet/ip.h>
48#include <netinet/ip_icmp.h>
49#include <arpa/inet.h>
50#include <sys/socket.h>
51#include <getopt.h>
52#include <inttypes.h>
[2cf30f6]53#include <signal.h>
[130a0e4]54
55#include "libtrace.h"
[dd1217b]56#include "lt_inttypes.h"
[4e65f42]57
[130a0e4]58struct libtrace_t *trace;
59
[fbc4342]60static volatile int done=0;
[2cf30f6]61
62static void cleanup_signal(int signal)
63{
[a8f2692]64        (void)signal;
[2cf30f6]65        done=1;
[c0ccccd]66        trace_interrupt();
[2cf30f6]67}
68
[130a0e4]69struct filter_t {
70        char *expr;
71        struct libtrace_filter_t *filter;
72        uint64_t count;
73        uint64_t bytes;
74} *filters = NULL;
75int filter_count=0;
76uint64_t totcount;
77uint64_t totbytes;
78
79/* Process a trace, counting packets that match filter(s) */
[a7282fb]80static void run_trace(char *uri) 
[130a0e4]81{
[d56089a]82        struct libtrace_packet_t *packet = trace_create_packet();
[130a0e4]83        int i;
84        uint64_t count = 0;
85        uint64_t bytes = 0;
[5ab626a]86        libtrace_stat_t *stats;
[130a0e4]87
88        fprintf(stderr,"%s:\n",uri);
89
90        trace = trace_create(uri);
91
[e49ec63]92        if (trace_is_err(trace)) {
[a8f2692]93                trace_perror(trace,"Failed to create trace");
[e49ec63]94                return;
95        }
96
97        if (trace_start(trace)==-1) {
[a8f2692]98                trace_perror(trace,"Failed to start trace");
[e49ec63]99                return;
100        }
101
102
[130a0e4]103        for (;;) {
104                int psize;
[c0ccccd]105                int wlen;
[3840760]106                if ((psize = trace_read_packet(trace, packet)) <1) {
[130a0e4]107                        break;
108                }
[2cf30f6]109               
110                if (done)
111                        break;
[c0ccccd]112                wlen = trace_get_wire_length(packet);
[130a0e4]113
114                for(i=0;i<filter_count;++i) {
[d242ea8]115                        if (filters[i].filter == NULL)
116                                continue;
117                        if(trace_apply_filter(filters[i].filter,packet) > 0) {
[130a0e4]118                                ++filters[i].count;
[c0ccccd]119                                filters[i].bytes+=wlen;
[130a0e4]120                        }
[d242ea8]121                        if (trace_is_err(trace)) {
122                                trace_perror(trace, "trace_apply_filter");
123                                fprintf(stderr, "Removing filter from filterlist\n");
124                                filters[i].filter = NULL;
125                        }
[130a0e4]126                }
127
128                ++count;
[c0ccccd]129                bytes+=wlen;
[5ab626a]130        }
131
132        stats = trace_get_statistics(trace, NULL);
[130a0e4]133
[d242ea8]134        printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%");
[130a0e4]135        for(i=0;i<filter_count;++i) {
136                printf("%30s:\t%12"PRIu64"\t%12"PRIu64"\t%7.03f\n",filters[i].expr,filters[i].count,filters[i].bytes,filters[i].count*100.0/count);
137                filters[i].bytes=0;
138                filters[i].count=0;
139        }
[5ab626a]140        if (stats->received_valid)
[4423dc7]141                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
[5ab626a]142                                "Input packets", stats->received);
143        if (stats->filtered_valid)
[4423dc7]144                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
[5ab626a]145                                "Filtered packets", stats->filtered);
146        if (stats->dropped_valid)
[4423dc7]147                fprintf(stderr,"%30s:\t%12" PRIu64"\n",
[5ab626a]148                                "Dropped packets",stats->dropped);
149        if (stats->accepted_valid)
150                fprintf(stderr,"%30s:\t%12" PRIu64 "\n",
151                                "Accepted packets", stats->accepted);
152        if (stats->errors_valid)
[4423dc7]153                fprintf(stderr,"%30s:\t%12" PRIu64 "\n",
[5ab626a]154                                "Erred packets", stats->errors);
[130a0e4]155        printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes);
156        totcount+=count;
157        totbytes+=bytes;
158
[ec0f8f1]159        if (trace_is_err(trace))
[3c54095]160                trace_perror(trace,"Processing trace");
[ec0f8f1]161
[130a0e4]162        trace_destroy(trace);
163}
164
[a7282fb]165static void usage(char *argv0)
[130a0e4]166{
[66caf4b]167        fprintf(stderr,"Usage: %s [-H|--libtrace-help] [--filter|-f bpf ]... libtraceuri...\n",argv0);
[130a0e4]168}
169
170int main(int argc, char *argv[]) {
171
172        int i;
[2cf30f6]173        struct sigaction sigact;
[130a0e4]174
175        while(1) {
176                int option_index;
177                struct option long_options[] = {
[66caf4b]178                        { "filter",        1, 0, 'f' },
179                        { "libtrace-help", 0, 0, 'H' },
180                        { NULL,            0, 0, 0   },
[130a0e4]181                };
182
[66caf4b]183                int c=getopt_long(argc, argv, "f:H",
[130a0e4]184                                long_options, &option_index);
185
186                if (c==-1)
187                        break;
188
189                switch (c) {
190                        case 'f':
191                                ++filter_count;
192                                filters=realloc(filters,filter_count*sizeof(struct filter_t));
193                                filters[filter_count-1].expr=strdup(optarg);
[853603a]194                                filters[filter_count-1].filter=trace_create_filter(optarg);
[130a0e4]195                                filters[filter_count-1].count=0;
196                                filters[filter_count-1].bytes=0;
197                                break;
[66caf4b]198                        case 'H':
199                                trace_help();
200                                exit(1);
201                                break;
[130a0e4]202                        default:
203                                fprintf(stderr,"Unknown option: %c\n",c);
204                                usage(argv[0]);
205                                return 1;
206                }
207        }
208
[2cf30f6]209        sigact.sa_handler = cleanup_signal;
210        sigemptyset(&sigact.sa_mask);
211        sigact.sa_flags = SA_RESTART;
212
213        sigaction(SIGINT, &sigact, NULL);
214        sigaction(SIGTERM, &sigact, NULL);
215
[130a0e4]216        for(i=optind;i<argc;++i) {
217                run_trace(argv[i]);
218        }
219        if (optind+1<argc) {
220                printf("Grand total:\n");
221                printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",totcount,totbytes);
222        }
223
224
225        return 0;
226}
Note: See TracBrowser for help on using the repository browser.