source: tools/tracestats/tracestats.c @ 4423dc7

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 4423dc7 was 4423dc7, checked in by Perry Lorier <perry@…>, 14 years ago

Add support to various tools for outputting the number of dropped packets
they've seen

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand.
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
31/*
32 * This program takes a series of traces and bpf filters and outputs how many
33 * bytes/packets
34 */
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>
53#include <signal.h>
54
55#include "libtrace.h"
56
57#ifndef PRIu64
58        #define PRIu64 "llu"
59#endif
60
61struct libtrace_t *trace;
62
63static volatile int done=0;
64
65static void cleanup_signal(int signal)
66{
67        done=1;
68}
69
70struct filter_t {
71        char *expr;
72        struct libtrace_filter_t *filter;
73        uint64_t count;
74        uint64_t bytes;
75} *filters = NULL;
76int filter_count=0;
77uint64_t totcount;
78uint64_t totbytes;
79
80/* Process a trace, counting packets that match filter(s) */
81static void run_trace(char *uri) 
82{
83        struct libtrace_packet_t *packet = trace_create_packet();
84        int i;
85        uint64_t count = 0;
86        uint64_t bytes = 0;
87        uint64_t packets;
88
89        fprintf(stderr,"%s:\n",uri);
90
91        trace = trace_create(uri);
92
93        if (trace_is_err(trace)) {
94                trace_perror(trace,"");
95                return;
96        }
97
98        if (trace_start(trace)==-1) {
99                trace_perror(trace,"");
100                return;
101        }
102
103
104        for (;;) {
105                int psize;
106                if ((psize = trace_read_packet(trace, packet)) <1) {
107                        break;
108                }
109               
110                if (done)
111                        break;
112
113                for(i=0;i<filter_count;++i) {
114                        if (filters[i].filter == NULL)
115                                continue;
116                        if(trace_apply_filter(filters[i].filter,packet) > 0) {
117                                ++filters[i].count;
118                                filters[i].bytes+=trace_get_wire_length(packet);
119                        }
120                        if (trace_is_err(trace)) {
121                                trace_perror(trace, "trace_apply_filter");
122                                fprintf(stderr, "Removing filter from filterlist\n");
123                                filters[i].filter = NULL;
124                        }
125                }
126
127                ++count;
128                bytes+=trace_get_wire_length(packet);
129        }
130
131        printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%");
132        for(i=0;i<filter_count;++i) {
133                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);
134                filters[i].bytes=0;
135                filters[i].count=0;
136        }
137        packets=trace_get_received_packets(trace);
138        if (packets!=UINT64_MAX)
139                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
140                                "Input packets", packets);
141        packets=trace_get_filtered_packets(trace);
142        if (packets!=UINT64_MAX)
143                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
144                                "Filtered packets", packets);
145        packets=trace_get_dropped_packets(trace);
146        if (packets!=UINT64_MAX)
147                fprintf(stderr,"%30s:\t%12" PRIu64"\n",
148                                "Dropped packets",packets);
149        packets=trace_get_accepted_packets(trace);
150        if (packets!=UINT64_MAX)
151                fprintf(stderr,"%30s:\t%12" PRIu64 "\n",
152                                "Accepted Packets",packets);
153        printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes);
154        totcount+=count;
155        totbytes+=bytes;
156
157        trace_destroy(trace);
158}
159
160static void usage(char *argv0)
161{
162        fprintf(stderr,"Usage: %s [-H|--libtrace-help] [--filter|-f bpf ]... libtraceuri...\n",argv0);
163}
164
165int main(int argc, char *argv[]) {
166
167        int i;
168        struct sigaction sigact;
169
170        while(1) {
171                int option_index;
172                struct option long_options[] = {
173                        { "filter",        1, 0, 'f' },
174                        { "libtrace-help", 0, 0, 'H' },
175                        { NULL,            0, 0, 0   },
176                };
177
178                int c=getopt_long(argc, argv, "f:H",
179                                long_options, &option_index);
180
181                if (c==-1)
182                        break;
183
184                switch (c) {
185                        case 'f':
186                                ++filter_count;
187                                filters=realloc(filters,filter_count*sizeof(struct filter_t));
188                                filters[filter_count-1].expr=strdup(optarg);
189                                filters[filter_count-1].filter=trace_create_filter(optarg);
190                                filters[filter_count-1].count=0;
191                                filters[filter_count-1].bytes=0;
192                                break;
193                        case 'H':
194                                trace_help();
195                                exit(1);
196                                break;
197                        default:
198                                fprintf(stderr,"Unknown option: %c\n",c);
199                                usage(argv[0]);
200                                return 1;
201                }
202        }
203
204        sigact.sa_handler = cleanup_signal;
205        sigemptyset(&sigact.sa_mask);
206        sigact.sa_flags = SA_RESTART;
207
208        sigaction(SIGINT, &sigact, NULL);
209        sigaction(SIGTERM, &sigact, NULL);
210
211        for(i=optind;i<argc;++i) {
212                run_trace(argv[i]);
213        }
214        if (optind+1<argc) {
215                printf("Grand total:\n");
216                printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",totcount,totbytes);
217        }
218
219
220        return 0;
221}
Note: See TracBrowser for help on using the repository browser.