source: tools/tracestats/tracestats_parallel.c @ 29bbef0

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

My work from over summer, with a few things tidied up and updated to include recent commits/patches to bring this up to date. Still very much work in progress.

  • Property mode set to 100644
File size: 7.9 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#include "trace_vector.h"
58#include <pthread.h>
59
60struct libtrace_t *trace;
61
62static void cleanup_signal(int signal)
63{
64        (void)signal;
65        //trace_interrupt();
66        // trace_pstop isn't really signal safe because its got lots of locks in it
67        trace_pstop(trace);
68}
69
70struct filter_t {
71        char *expr;
72        struct libtrace_filter_t *filter;
73        uint64_t count;
74        uint64_t bytes;
75} *filters = NULL;
76int filter_count=0;
77volatile uint64_t totcount = 0;
78volatile uint64_t totbytes = 0;
79
80
81typedef struct global_blob {
82        uint64_t * totcount;
83        uint64_t * totbytes;
84} global_blob_t;
85
86typedef struct statistics {
87        uint64_t count;
88        uint64_t bytes;
89} statistics_t;
90
91
92static void* per_packet(libtrace_t *trace, libtrace_packet_t *pkt, libtrace_message_t *mesg, libtrace_thread_t *t)
93{
94        // Using first entry as total and those after for filter counts
95        static __thread statistics_t * results = NULL;
96        int i;
97       
98        if (pkt) {
99                int wlen = trace_get_wire_length(pkt);
100                for(i=0;i<filter_count;++i) {
101                        if (filters[i].filter == NULL)
102                                continue;
103                        if(trace_apply_filter(filters[i].filter,pkt) > 0) {
104                                results[i+1].count++;
105                                results[i+1].bytes+=wlen;
106                        }
107                        if (trace_is_err(trace)) {
108                                trace_perror(trace, "trace_apply_filter");
109                                fprintf(stderr, "Removing filter from filterlist\n");
110                                // XXX might be a problem across threads below
111                                filters[i].filter = NULL;
112                        }
113                }
114                results[0].count++;
115                results[0].bytes +=wlen;
116        }
117        if (mesg) {
118                // printf ("%d.%06d READ #%"PRIu64"\n", tv.tv_sec, tv.tv_usec, trace_packet_get(packet));
119                switch (mesg->code) {
120                        case MESSAGE_STOPPED:
121                                trace_publish_result(trace, 0, results); // Only ever using a single key 0
122                                printf("Thread published resuslts WOWW \n");
123                                break;
124                        case MESSAGE_STARTED:
125                                results = calloc(1, sizeof(statistics_t) * (filter_count + 1));
126                                break;
127                        case MESSAGE_PAUSE:
128                                printf("GOT Asked to pause ahh\n");
129                                break;
130                }
131        }
132        return pkt;
133}
134
135static int reduce(libtrace_t* trace, void* global_blob)
136{
137        int i,j;
138        uint64_t count=0, bytes=0;
139        libtrace_vector_t results;
140        libtrace_vector_init(&results, sizeof(libtrace_result_t));
141        trace_get_results(trace, &results);
142        uint64_t packets;
143       
144        /* Get the results from each core and sum 'em up */
145        for (i = 0 ; i < libtrace_vector_get_size(&results) ; i++) {
146                libtrace_result_t result;
147                assert(libtrace_vector_get(&results, i, (void *) &result) == 1);
148                assert(libtrace_result_get_key(&result) == 0);
149                statistics_t * res = libtrace_result_get_value(&result);
150                count += res[0].count;
151                bytes += res[0].bytes;
152                for (j = 0; j < filter_count; j++) {
153                        filters[j].count += res[j+1].count;
154                        filters[j].bytes += res[j+1].bytes;
155                }
156                free(res);
157        }
158        // Done with these results - Free internally and externally
159        libtrace_vector_destroy(&results);
160
161    printf("%-30s\t%12s\t%12s\t%7s\n","filter","count","bytes","%");
162        for(i=0;i<filter_count;++i) {
163                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);
164                filters[i].bytes=0;
165                filters[i].count=0;
166        }
167        packets=trace_get_received_packets(trace);
168        if (packets!=UINT64_MAX)
169                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
170                                "Input packets", packets);
171        packets=trace_get_filtered_packets(trace);
172        if (packets!=UINT64_MAX)
173                fprintf(stderr,"%30s:\t%12" PRIu64"\n", 
174                                "Filtered packets", packets);
175        packets=trace_get_dropped_packets(trace);
176        if (packets!=UINT64_MAX)
177                fprintf(stderr,"%30s:\t%12" PRIu64"\n",
178                                "Dropped packets",packets);
179        packets=trace_get_accepted_packets(trace);
180        if (packets!=UINT64_MAX)
181                fprintf(stderr,"%30s:\t%12" PRIu64 "\n",
182                                "Accepted Packets",packets);
183        printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",count,bytes);
184        totcount+=count;
185        totbytes+=bytes;
186       
187        return 0;
188}
189
190static uint64_t rand_hash(libtrace_packet_t * pkt) {
191        return rand();
192}
193
194static uint64_t bad_hash(libtrace_packet_t * pkt) {
195        return 0;
196}
197
198/* Process a trace, counting packets that match filter(s) */
199static void run_trace(char *uri) 
200{
201
202        fprintf(stderr,"%s:\n",uri);
203
204        trace = trace_create(uri);
205
206        if (trace_is_err(trace)) {
207                trace_perror(trace,"Failed to create trace");
208                return;
209        }
210       
211        int option = 2;
212        //option = 10000;
213        //trace_parallel_config(trace, TRACE_OPTION_USE_DEDICATED_HASHER, &option);
214        //trace_parallel_config(trace, TRACE_OPTION_USE_SLIDING_WINDOW_BUFFER, &option);
215        option = 2;
216        trace_parallel_config(trace, TRACE_OPTION_SET_MAPPER_THREAD_COUNT, &option);
217        //trace_parallel_config(trace, TRACE_OPTION_SET_MAPPER_BUFFER_SIZE, &option);
218
219        /* OPTIONALLY SETUP CORES HERE BUT WE DON'T CARE ABOUT THAT YET XXX */
220
221        /*if (trace_start(trace)==-1) {
222        trace_perror(trace,"Failed to start trace");
223        return;
224        }*/
225        global_blob_t blob;
226
227
228        if (trace_pstart(trace, (void *)&blob, &per_packet, NULL)==-1) {
229                trace_perror(trace,"Failed to start trace");
230                return;
231        }
232
233        // Wait for all threads to stop
234        trace_join(trace);
235        reduce(trace, NULL);
236
237        //map_pair_iterator_t * results = NULL;
238        //trace_get_results(trace, &results);
239
240        //if (results != NULL) {
241        //      reduce(trace, global_blob, results);
242        //}
243        if (trace_is_err(trace))
244                trace_perror(trace,"%s",uri);
245
246        print_contention_stats(trace);
247        trace_destroy(trace);
248}
249
250static void usage(char *argv0)
251{
252        fprintf(stderr,"Usage: %s [-H|--libtrace-help] [--filter|-f bpf ]... libtraceuri...\n",argv0);
253}
254
255int main(int argc, char *argv[]) {
256
257        int i;
258        struct sigaction sigact;
259
260        while(1) {
261                int option_index;
262                struct option long_options[] = {
263                        { "filter",        1, 0, 'f' },
264                        { "libtrace-help", 0, 0, 'H' },
265                        { NULL,            0, 0, 0   },
266                };
267
268                int c=getopt_long(argc, argv, "f:H",
269                                long_options, &option_index);
270
271                if (c==-1)
272                        break;
273
274                switch (c) {
275                        case 'f':
276                                ++filter_count;
277                                filters=realloc(filters,filter_count*sizeof(struct filter_t));
278                                filters[filter_count-1].expr=strdup(optarg);
279                                filters[filter_count-1].filter=trace_create_filter(optarg);
280                                filters[filter_count-1].count=0;
281                                filters[filter_count-1].bytes=0;
282                                break;
283                        case 'H':
284                                trace_help();
285                                exit(1);
286                                break;
287                        default:
288                                fprintf(stderr,"Unknown option: %c\n",c);
289                                usage(argv[0]);
290                                return 1;
291                }
292        }
293
294        sigact.sa_handler = cleanup_signal;
295        sigemptyset(&sigact.sa_mask);
296        sigact.sa_flags = SA_RESTART;
297
298        sigaction(SIGINT, &sigact, NULL);
299        sigaction(SIGTERM, &sigact, NULL);
300       
301        for(i=optind;i<argc;++i) {
302                run_trace(argv[i]);
303        }
304        if (optind+1<argc) {
305                printf("Grand total:\n");
306                printf("%30s:\t%12"PRIu64"\t%12" PRIu64 "\n","Total",totcount,totbytes);
307        }
308       
309        return 0;
310}
Note: See TracBrowser for help on using the repository browser.