source: tools/tracestats/tracestats.c @ fed9152

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since fed9152 was 3c54095, checked in by Shane Alcock <salcock@…>, 7 years ago

Fix horrible error reporting in tracestats

The URI seems to get printed about three times in some error messages,
which isn't exactly helpful.

  • 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;
[4423dc7]86        uint64_t packets;
[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;
[130a0e4]130        }
131
[d242ea8]132        printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%");
[130a0e4]133        for(i=0;i<filter_count;++i) {
134                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);
135                filters[i].bytes=0;
136                filters[i].count=0;
137        }
[4423dc7]138        packets=trace_get_received_packets(trace);
139        if (packets!=UINT64_MAX)
140                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
141                                "Input packets", packets);
142        packets=trace_get_filtered_packets(trace);
143        if (packets!=UINT64_MAX)
144                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
145                                "Filtered packets", packets);
146        packets=trace_get_dropped_packets(trace);
147        if (packets!=UINT64_MAX)
148                fprintf(stderr,"%30s:\t%12" PRIu64"\n",
149                                "Dropped packets",packets);
150        packets=trace_get_accepted_packets(trace);
151        if (packets!=UINT64_MAX)
152                fprintf(stderr,"%30s:\t%12" PRIu64 "\n",
153                                "Accepted Packets",packets);
[130a0e4]154        printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes);
155        totcount+=count;
156        totbytes+=bytes;
157
[ec0f8f1]158        if (trace_is_err(trace))
[3c54095]159                trace_perror(trace,"Processing trace");
[ec0f8f1]160
[130a0e4]161        trace_destroy(trace);
162}
163
[a7282fb]164static void usage(char *argv0)
[130a0e4]165{
[66caf4b]166        fprintf(stderr,"Usage: %s [-H|--libtrace-help] [--filter|-f bpf ]... libtraceuri...\n",argv0);
[130a0e4]167}
168
169int main(int argc, char *argv[]) {
170
171        int i;
[2cf30f6]172        struct sigaction sigact;
[130a0e4]173
174        while(1) {
175                int option_index;
176                struct option long_options[] = {
[66caf4b]177                        { "filter",        1, 0, 'f' },
178                        { "libtrace-help", 0, 0, 'H' },
179                        { NULL,            0, 0, 0   },
[130a0e4]180                };
181
[66caf4b]182                int c=getopt_long(argc, argv, "f:H",
[130a0e4]183                                long_options, &option_index);
184
185                if (c==-1)
186                        break;
187
188                switch (c) {
189                        case 'f':
190                                ++filter_count;
191                                filters=realloc(filters,filter_count*sizeof(struct filter_t));
192                                filters[filter_count-1].expr=strdup(optarg);
[853603a]193                                filters[filter_count-1].filter=trace_create_filter(optarg);
[130a0e4]194                                filters[filter_count-1].count=0;
195                                filters[filter_count-1].bytes=0;
196                                break;
[66caf4b]197                        case 'H':
198                                trace_help();
199                                exit(1);
200                                break;
[130a0e4]201                        default:
202                                fprintf(stderr,"Unknown option: %c\n",c);
203                                usage(argv[0]);
204                                return 1;
205                }
206        }
207
[2cf30f6]208        sigact.sa_handler = cleanup_signal;
209        sigemptyset(&sigact.sa_mask);
210        sigact.sa_flags = SA_RESTART;
211
212        sigaction(SIGINT, &sigact, NULL);
213        sigaction(SIGTERM, &sigact, NULL);
214
[130a0e4]215        for(i=optind;i<argc;++i) {
216                run_trace(argv[i]);
217        }
218        if (optind+1<argc) {
219                printf("Grand total:\n");
220                printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",totcount,totbytes);
221        }
222
223
224        return 0;
225}
Note: See TracBrowser for help on using the repository browser.