source: tools/tracestats/tracestats.c @ 4e65f42

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 4e65f42 was 4e65f42, checked in by Shane Alcock <salcock@…>, 15 years ago

Fixed segmentation faults when trace_destroy is called on a trace that wasn't successfully created
libdl is now checked for and only linked against if available (BSD's don't have a libdl)
Fixed the libpacketdump .so's to use libtrace's generic protocol headers as well as trace_ether_ntoa
Added missing check for strlcpy
Added a couple of #defines to various tools that needed them under FreeBSD
Removed some needless #includes from the libpacketdump .so's

  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2004 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#include <stdio.h>
36#include <stdlib.h>
37#include <assert.h>
38#include <string.h>
39#include <sys/time.h>
40#include <sys/types.h>
41#include <time.h>
42
43#include <netinet/in.h>
44#include <netinet/in_systm.h>
45#include <netinet/tcp.h>
46#include <netinet/ip.h>
47#include <netinet/ip_icmp.h>
48#include <arpa/inet.h>
49#include <sys/socket.h>
50#include <getopt.h>
51#include <inttypes.h>
52
53#include "libtrace.h"
54
55#ifndef PRIu64
56        #define PRIu64 "llu"
57#endif
58
59struct libtrace_t *trace;
60
61struct filter_t {
62        char *expr;
63        struct libtrace_filter_t *filter;
64        uint64_t count;
65        uint64_t bytes;
66} *filters = NULL;
67int filter_count=0;
68uint64_t totcount;
69uint64_t totbytes;
70
71/* Process a trace, counting packets that match filter(s) */
72void run_trace(char *uri) 
73{
74        struct libtrace_packet_t *packet = trace_create_packet();
75        int i;
76        uint64_t count = 0;
77        uint64_t bytes = 0;
78
79        fprintf(stderr,"%s:\n",uri);
80
81        trace = trace_create(uri);
82
83        for (;;) {
84                int psize;
85                if ((psize = trace_read_packet(trace, packet)) <1) {
86                        break;
87                }
88
89                for(i=0;i<filter_count;++i) {
90                        if(trace_bpf_filter(filters[i].filter,packet)) {
91                                ++filters[i].count;
92                                filters[i].bytes+=trace_get_wire_length(packet);
93                        }
94                }
95
96                ++count;
97                bytes+=trace_get_wire_length(packet);
98        }
99
100        for(i=0;i<filter_count;++i) {
101                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);
102                filters[i].bytes=0;
103                filters[i].count=0;
104        }
105        printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes);
106        totcount+=count;
107        totbytes+=bytes;
108
109        trace_destroy(trace);
110}
111
112void usage(char *argv0)
113{
114        fprintf(stderr,"Usage: %s [--filter|-f bpf ]... libtraceuri...\n",argv0);
115}
116
117int main(int argc, char *argv[]) {
118
119        int i;
120
121        while(1) {
122                int option_index;
123                struct option long_options[] = {
124                        { "filter",     1, 0, 'f' },
125                        { NULL,         0, 0, 0   },
126                };
127
128                int c=getopt_long(argc, argv, "f:",
129                                long_options, &option_index);
130
131                if (c==-1)
132                        break;
133
134                switch (c) {
135                        case 'f':
136                                ++filter_count;
137                                filters=realloc(filters,filter_count*sizeof(struct filter_t));
138                                filters[filter_count-1].expr=strdup(optarg);
139                                filters[filter_count-1].filter=trace_bpf_setfilter(optarg);
140                                filters[filter_count-1].count=0;
141                                filters[filter_count-1].bytes=0;
142                                break;
143                        default:
144                                fprintf(stderr,"Unknown option: %c\n",c);
145                                usage(argv[0]);
146                                return 1;
147                }
148        }
149
150        printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%");
151        for(i=optind;i<argc;++i) {
152                run_trace(argv[i]);
153        }
154        if (optind+1<argc) {
155                printf("Grand total:\n");
156                printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",totcount,totbytes);
157        }
158
159
160        return 0;
161}
Note: See TracBrowser for help on using the repository browser.