source: tools/tracestats/tracestats.c @ dd1217b

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since dd1217b was dd1217b, checked in by Shane Alcock <salcock@…>, 13 years ago
  • Tools that use PRIu64 or similar for output need to include lt_inttypes.h otherwise they won't compile on systems that don't define them - e.g. OpenBSD 3.5
  • Also fixed a variable in ecn_report that wasn't declared at the start of the code block
  • Property mode set to 100644
File size: 5.1 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#include "lt_inttypes.h"
57
58struct libtrace_t *trace;
59
60static volatile int done=0;
61
62static void cleanup_signal(int signal)
63{
64        done=1;
65}
66
67struct filter_t {
68        char *expr;
69        struct libtrace_filter_t *filter;
70        uint64_t count;
71        uint64_t bytes;
72} *filters = NULL;
73int filter_count=0;
74uint64_t totcount;
75uint64_t totbytes;
76
77/* Process a trace, counting packets that match filter(s) */
78static void run_trace(char *uri) 
79{
80        struct libtrace_packet_t *packet = trace_create_packet();
81        int i;
82        uint64_t count = 0;
83        uint64_t bytes = 0;
84        uint64_t packets;
85
86        fprintf(stderr,"%s:\n",uri);
87
88        trace = trace_create(uri);
89
90        if (trace_is_err(trace)) {
91                trace_perror(trace,"");
92                return;
93        }
94
95        if (trace_start(trace)==-1) {
96                trace_perror(trace,"");
97                return;
98        }
99
100
101        for (;;) {
102                int psize;
103                if ((psize = trace_read_packet(trace, packet)) <1) {
104                        break;
105                }
106               
107                if (done)
108                        break;
109
110                for(i=0;i<filter_count;++i) {
111                        if (filters[i].filter == NULL)
112                                continue;
113                        if(trace_apply_filter(filters[i].filter,packet) > 0) {
114                                ++filters[i].count;
115                                filters[i].bytes+=trace_get_wire_length(packet);
116                        }
117                        if (trace_is_err(trace)) {
118                                trace_perror(trace, "trace_apply_filter");
119                                fprintf(stderr, "Removing filter from filterlist\n");
120                                filters[i].filter = NULL;
121                        }
122                }
123
124                ++count;
125                bytes+=trace_get_wire_length(packet);
126        }
127
128        printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%");
129        for(i=0;i<filter_count;++i) {
130                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);
131                filters[i].bytes=0;
132                filters[i].count=0;
133        }
134        packets=trace_get_received_packets(trace);
135        if (packets!=UINT64_MAX)
136                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
137                                "Input packets", packets);
138        packets=trace_get_filtered_packets(trace);
139        if (packets!=UINT64_MAX)
140                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
141                                "Filtered packets", packets);
142        packets=trace_get_dropped_packets(trace);
143        if (packets!=UINT64_MAX)
144                fprintf(stderr,"%30s:\t%12" PRIu64"\n",
145                                "Dropped packets",packets);
146        packets=trace_get_accepted_packets(trace);
147        if (packets!=UINT64_MAX)
148                fprintf(stderr,"%30s:\t%12" PRIu64 "\n",
149                                "Accepted Packets",packets);
150        printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes);
151        totcount+=count;
152        totbytes+=bytes;
153
154        trace_destroy(trace);
155}
156
157static void usage(char *argv0)
158{
159        fprintf(stderr,"Usage: %s [-H|--libtrace-help] [--filter|-f bpf ]... libtraceuri...\n",argv0);
160}
161
162int main(int argc, char *argv[]) {
163
164        int i;
165        struct sigaction sigact;
166
167        while(1) {
168                int option_index;
169                struct option long_options[] = {
170                        { "filter",        1, 0, 'f' },
171                        { "libtrace-help", 0, 0, 'H' },
172                        { NULL,            0, 0, 0   },
173                };
174
175                int c=getopt_long(argc, argv, "f:H",
176                                long_options, &option_index);
177
178                if (c==-1)
179                        break;
180
181                switch (c) {
182                        case 'f':
183                                ++filter_count;
184                                filters=realloc(filters,filter_count*sizeof(struct filter_t));
185                                filters[filter_count-1].expr=strdup(optarg);
186                                filters[filter_count-1].filter=trace_create_filter(optarg);
187                                filters[filter_count-1].count=0;
188                                filters[filter_count-1].bytes=0;
189                                break;
190                        case 'H':
191                                trace_help();
192                                exit(1);
193                                break;
194                        default:
195                                fprintf(stderr,"Unknown option: %c\n",c);
196                                usage(argv[0]);
197                                return 1;
198                }
199        }
200
201        sigact.sa_handler = cleanup_signal;
202        sigemptyset(&sigact.sa_mask);
203        sigact.sa_flags = SA_RESTART;
204
205        sigaction(SIGINT, &sigact, NULL);
206        sigaction(SIGTERM, &sigact, NULL);
207
208        for(i=optind;i<argc;++i) {
209                run_trace(argv[i]);
210        }
211        if (optind+1<argc) {
212                printf("Grand total:\n");
213                printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",totcount,totbytes);
214        }
215
216
217        return 0;
218}
Note: See TracBrowser for help on using the repository browser.