source: tools/tracestats/tracestats_single.c @ 2d8a045

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 2d8a045 was 322c516, checked in by Shane Alcock <salcock@…>, 6 years ago

Port tracestats to new parallel API

Old single-threaded tracestats has been moved to tracestats_single.c, which
won't be built or distributed.

  • Property mode set to 100644
File size: 5.3 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        trace_interrupt();
67}
68
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) */
80static void run_trace(char *uri) 
81{
82        struct libtrace_packet_t *packet = trace_create_packet();
83        int i;
84        uint64_t count = 0;
85        uint64_t bytes = 0;
86        libtrace_stat_t *stats;
87
88        fprintf(stderr,"%s:\n",uri);
89
90        trace = trace_create(uri);
91
92        if (trace_is_err(trace)) {
93                trace_perror(trace,"Failed to create trace");
94                return;
95        }
96
97        if (trace_start(trace)==-1) {
98                trace_perror(trace,"Failed to start trace");
99                return;
100        }
101
102
103        for (;;) {
104                int psize;
105                int wlen;
106                if ((psize = trace_read_packet(trace, packet)) <1) {
107                        break;
108                }
109               
110                if (done)
111                        break;
112                wlen = trace_get_wire_length(packet);
113
114                for(i=0;i<filter_count;++i) {
115                        if (filters[i].filter == NULL)
116                                continue;
117                        if(trace_apply_filter(filters[i].filter,packet) > 0) {
118                                ++filters[i].count;
119                                filters[i].bytes+=wlen;
120                        }
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                        }
126                }
127
128                ++count;
129                bytes+=wlen;
130        }
131        trace_destroy_packet(packet);
132
133        stats = trace_get_statistics(trace, NULL);
134
135        printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%");
136        for(i=0;i<filter_count;++i) {
137                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);
138                filters[i].bytes=0;
139                filters[i].count=0;
140        }
141        if (stats->received_valid)
142                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
143                                "Input packets", stats->received);
144        if (stats->filtered_valid)
145                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
146                                "Filtered packets", stats->filtered);
147        if (stats->dropped_valid)
148                fprintf(stderr,"%30s:\t%12" PRIu64"\n",
149                                "Dropped packets",stats->dropped);
150        if (stats->accepted_valid)
151                fprintf(stderr,"%30s:\t%12" PRIu64 "\n",
152                                "Accepted packets", stats->accepted);
153        if (stats->errors_valid)
154                fprintf(stderr,"%30s:\t%12" PRIu64 "\n",
155                                "Erred packets", stats->errors);
156        printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes);
157        totcount+=count;
158        totbytes+=bytes;
159
160        if (trace_is_err(trace))
161                trace_perror(trace,"Processing trace");
162
163        trace_destroy(trace);
164}
165
166static void usage(char *argv0)
167{
168        fprintf(stderr,"Usage: %s [-H|--libtrace-help] [--filter|-f bpf ]... libtraceuri...\n",argv0);
169}
170
171int main(int argc, char *argv[]) {
172
173        int i;
174        struct sigaction sigact;
175
176        while(1) {
177                int option_index;
178                struct option long_options[] = {
179                        { "filter",        1, 0, 'f' },
180                        { "libtrace-help", 0, 0, 'H' },
181                        { NULL,            0, 0, 0   },
182                };
183
184                int c=getopt_long(argc, argv, "f:H",
185                                long_options, &option_index);
186
187                if (c==-1)
188                        break;
189
190                switch (c) {
191                        case 'f':
192                                ++filter_count;
193                                filters=realloc(filters,filter_count*sizeof(struct filter_t));
194                                filters[filter_count-1].expr=strdup(optarg);
195                                filters[filter_count-1].filter=trace_create_filter(optarg);
196                                filters[filter_count-1].count=0;
197                                filters[filter_count-1].bytes=0;
198                                break;
199                        case 'H':
200                                trace_help();
201                                exit(1);
202                                break;
203                        default:
204                                fprintf(stderr,"Unknown option: %c\n",c);
205                                usage(argv[0]);
206                                return 1;
207                }
208        }
209
210        sigact.sa_handler = cleanup_signal;
211        sigemptyset(&sigact.sa_mask);
212        sigact.sa_flags = SA_RESTART;
213
214        sigaction(SIGINT, &sigact, NULL);
215        sigaction(SIGTERM, &sigact, NULL);
216
217        for(i=optind;i<argc;++i) {
218                run_trace(argv[i]);
219        }
220        if (optind+1<argc) {
221                printf("Grand total:\n");
222                printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",totcount,totbytes);
223        }
224
225
226        return 0;
227}
Note: See TracBrowser for help on using the repository browser.