source: tools/tracestats/tracestats_single.c @ 02d13cb

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivendag_formatrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 02d13cb was ee6e802, checked in by Shane Alcock <salcock@…>, 5 years ago

Updated copyright blurb on all source files

In some cases, this meant adding copyright blurbs to files that
had never had them before.

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