source: tools/tracestats/tracestats.c @ 341c38f

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 341c38f was 6b98325, checked in by Richard Sanger <rsangerarj@…>, 6 years ago

Removes pconfig_input() and replaces trace_config() with trace_set_<option>

  • Removes trace_pconfig completely - only trace_set_hasher is left now in trace_config
  • Rework user configuration so this is no longer modified directly. Now we just apply it directly to a trace.
  • Many minor documentation fixes and additions
  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[130a0e4]1/*
2 * This file is part of libtrace
3 *
[d5a27e8]4 * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand.
[130a0e4]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
[853603a]31/*
32 * This program takes a series of traces and bpf filters and outputs how many
33 * bytes/packets
34 */
[130a0e4]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>
[2cf30f6]53#include <signal.h>
[130a0e4]54
55#include "libtrace.h"
[dd1217b]56#include "lt_inttypes.h"
[4e65f42]57
[130a0e4]58struct libtrace_t *trace;
59
[fbc4342]60static volatile int done=0;
[2cf30f6]61
62static void cleanup_signal(int signal)
63{
[a8f2692]64        (void)signal;
[2cf30f6]65        done=1;
[c0ccccd]66        trace_interrupt();
[2cf30f6]67}
68
[130a0e4]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) */
[a7282fb]80static void run_trace(char *uri) 
[130a0e4]81{
[d56089a]82        struct libtrace_packet_t *packet = trace_create_packet();
[130a0e4]83        int i;
84        uint64_t count = 0;
85        uint64_t bytes = 0;
[5ab626a]86        libtrace_stat_t *stats;
[130a0e4]87
88        fprintf(stderr,"%s:\n",uri);
89
90        trace = trace_create(uri);
91
[e49ec63]92        if (trace_is_err(trace)) {
[a8f2692]93                trace_perror(trace,"Failed to create trace");
[e49ec63]94                return;
95        }
96
97        if (trace_start(trace)==-1) {
[a8f2692]98                trace_perror(trace,"Failed to start trace");
[e49ec63]99                return;
100        }
101
102
[130a0e4]103        for (;;) {
104                int psize;
[c0ccccd]105                int wlen;
[3840760]106                if ((psize = trace_read_packet(trace, packet)) <1) {
[130a0e4]107                        break;
108                }
[2cf30f6]109               
110                if (done)
111                        break;
[c0ccccd]112                wlen = trace_get_wire_length(packet);
[130a0e4]113
114                for(i=0;i<filter_count;++i) {
[d242ea8]115                        if (filters[i].filter == NULL)
116                                continue;
117                        if(trace_apply_filter(filters[i].filter,packet) > 0) {
[130a0e4]118                                ++filters[i].count;
[c0ccccd]119                                filters[i].bytes+=wlen;
[130a0e4]120                        }
[d242ea8]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                        }
[130a0e4]126                }
127
128                ++count;
[c0ccccd]129                bytes+=wlen;
[5ab626a]130        }
[6b98325]131        trace_destroy_packet(packet);
[5ab626a]132
133        stats = trace_get_statistics(trace, NULL);
[130a0e4]134
[d242ea8]135        printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%");
[130a0e4]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        }
[5ab626a]141        if (stats->received_valid)
[4423dc7]142                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
[5ab626a]143                                "Input packets", stats->received);
144        if (stats->filtered_valid)
[4423dc7]145                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
[5ab626a]146                                "Filtered packets", stats->filtered);
147        if (stats->dropped_valid)
[4423dc7]148                fprintf(stderr,"%30s:\t%12" PRIu64"\n",
[5ab626a]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)
[4423dc7]154                fprintf(stderr,"%30s:\t%12" PRIu64 "\n",
[5ab626a]155                                "Erred packets", stats->errors);
[130a0e4]156        printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes);
157        totcount+=count;
158        totbytes+=bytes;
159
[ec0f8f1]160        if (trace_is_err(trace))
[3c54095]161                trace_perror(trace,"Processing trace");
[ec0f8f1]162
[130a0e4]163        trace_destroy(trace);
164}
165
[a7282fb]166static void usage(char *argv0)
[130a0e4]167{
[66caf4b]168        fprintf(stderr,"Usage: %s [-H|--libtrace-help] [--filter|-f bpf ]... libtraceuri...\n",argv0);
[130a0e4]169}
170
171int main(int argc, char *argv[]) {
172
173        int i;
[2cf30f6]174        struct sigaction sigact;
[130a0e4]175
176        while(1) {
177                int option_index;
178                struct option long_options[] = {
[66caf4b]179                        { "filter",        1, 0, 'f' },
180                        { "libtrace-help", 0, 0, 'H' },
181                        { NULL,            0, 0, 0   },
[130a0e4]182                };
183
[66caf4b]184                int c=getopt_long(argc, argv, "f:H",
[130a0e4]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);
[853603a]195                                filters[filter_count-1].filter=trace_create_filter(optarg);
[130a0e4]196                                filters[filter_count-1].count=0;
197                                filters[filter_count-1].bytes=0;
198                                break;
[66caf4b]199                        case 'H':
200                                trace_help();
201                                exit(1);
202                                break;
[130a0e4]203                        default:
204                                fprintf(stderr,"Unknown option: %c\n",c);
205                                usage(argv[0]);
206                                return 1;
207                }
208        }
209
[2cf30f6]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
[130a0e4]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.