source: tools/tracestats/tracestats.c @ b8bb4c5

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since b8bb4c5 was 3840760, checked in by Daniel Lawson <dlawson@…>, 16 years ago

updated the examples/ and tools/ directories to correctly use trace_packet_create() now.

fixed a typo in format_wag (shouldn't use pcap_get_framing_length for wag!)

  • Property mode set to 100644
File size: 3.7 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
55struct libtrace_t *trace;
56
57struct filter_t {
58        char *expr;
59        struct libtrace_filter_t *filter;
60        uint64_t count;
61        uint64_t bytes;
62} *filters = NULL;
63int filter_count=0;
64uint64_t totcount;
65uint64_t totbytes;
66
67/* Process a trace, counting packets that match filter(s) */
68void run_trace(char *uri) 
69{
70        struct libtrace_packet_t *packet = trace_packet_create();
71        int i;
72        uint64_t count = 0;
73        uint64_t bytes = 0;
74
75        fprintf(stderr,"%s:\n",uri);
76
77        trace = trace_create(uri);
78
79        for (;;) {
80                int psize;
81                if ((psize = trace_read_packet(trace, packet)) <1) {
82                        break;
83                }
84
85                for(i=0;i<filter_count;++i) {
86                        if(trace_bpf_filter(filters[i].filter,packet)) {
87                                ++filters[i].count;
88                                filters[i].bytes+=trace_get_wire_length(packet);
89                        }
90                }
91
92                ++count;
93                bytes+=trace_get_wire_length(packet);
94        }
95
96        for(i=0;i<filter_count;++i) {
97                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);
98                filters[i].bytes=0;
99                filters[i].count=0;
100        }
101        printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes);
102        totcount+=count;
103        totbytes+=bytes;
104
105        trace_destroy(trace);
106}
107
108void usage(char *argv0)
109{
110        fprintf(stderr,"Usage: %s [--filter|-f bpf ]... libtraceuri...\n",argv0);
111}
112
113int main(int argc, char *argv[]) {
114
115        int i;
116
117        while(1) {
118                int option_index;
119                struct option long_options[] = {
120                        { "filter",     1, 0, 'f' },
121                        { NULL,         0, 0, 0   },
122                };
123
124                int c=getopt_long(argc, argv, "f:",
125                                long_options, &option_index);
126
127                if (c==-1)
128                        break;
129
130                switch (c) {
131                        case 'f':
132                                ++filter_count;
133                                filters=realloc(filters,filter_count*sizeof(struct filter_t));
134                                filters[filter_count-1].expr=strdup(optarg);
135                                filters[filter_count-1].filter=trace_bpf_setfilter(optarg);
136                                filters[filter_count-1].count=0;
137                                filters[filter_count-1].bytes=0;
138                                break;
139                        default:
140                                fprintf(stderr,"Unknown option: %c\n",c);
141                                usage(argv[0]);
142                                return 1;
143                }
144        }
145
146        printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%");
147        for(i=optind;i<argc;++i) {
148                run_trace(argv[i]);
149        }
150        if (optind+1<argc) {
151                printf("Grand total:\n");
152                printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",totcount,totbytes);
153        }
154
155
156        return 0;
157}
Note: See TracBrowser for help on using the repository browser.