source: tools/tracestats/tracestats.c @ a8f2692

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

Tidy up a bazillion tiny warnings

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