[c9caf07] | 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 | #include <stdio.h> |
---|
| 32 | #include <stdlib.h> |
---|
| 33 | #include <assert.h> |
---|
| 34 | #include <string.h> |
---|
| 35 | #include <sys/time.h> |
---|
| 36 | #include <sys/types.h> |
---|
| 37 | #include <time.h> |
---|
| 38 | |
---|
| 39 | #include <netinet/in.h> |
---|
| 40 | #include <netinet/in_systm.h> |
---|
| 41 | #include <netinet/tcp.h> |
---|
| 42 | #include <netinet/ip.h> |
---|
| 43 | #include <netinet/ip_icmp.h> |
---|
| 44 | #include <arpa/inet.h> |
---|
| 45 | #include <sys/socket.h> |
---|
| 46 | #include <getopt.h> |
---|
| 47 | #include <inttypes.h> |
---|
[edca0b8] | 48 | #include <sys/resource.h> |
---|
| 49 | #include <unistd.h> |
---|
[c9caf07] | 50 | |
---|
| 51 | #include "libtrace.h" |
---|
| 52 | |
---|
| 53 | struct libtrace_t *trace; |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | uint64_t tot=0; |
---|
| 57 | /* Process a trace, counting packets that match filter(s) */ |
---|
| 58 | void run_trace(char *uri) |
---|
| 59 | { |
---|
[d56089a] | 60 | struct libtrace_packet_t *packet = trace_create_packet(); |
---|
[c9caf07] | 61 | uint64_t count = 0; |
---|
| 62 | uint64_t bytes = 0; |
---|
| 63 | uint64_t nontcp_count = 0; |
---|
| 64 | uint64_t nontcp_bytes = 0; |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | trace = trace_create(uri); |
---|
| 69 | |
---|
| 70 | for (;;) { |
---|
| 71 | int psize; |
---|
| 72 | if ((psize = trace_read_packet(trace, &packet)) <1) { |
---|
| 73 | break; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | if (trace_get_tcp(&packet)) { |
---|
| 77 | ++nontcp_count; |
---|
| 78 | nontcp_bytes+=trace_get_wire_length(&packet); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | ++count; |
---|
| 82 | bytes+=trace_get_wire_length(&packet); |
---|
| 83 | ++tot; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | trace_destroy(trace); |
---|
| 88 | } |
---|
| 89 | |
---|
[edca0b8] | 90 | #define TIMER_SUB(res,a,b) \ |
---|
| 91 | do { \ |
---|
| 92 | res.tv_sec = a.tv_sec - b.tv_sec; \ |
---|
| 93 | res.tv_usec = a.tv_usec - b.tv_usec; \ |
---|
| 94 | if (res.tv_usec < 0) { \ |
---|
| 95 | --res.tv_sec; \ |
---|
| 96 | res.tv_usec += 1000000; \ |
---|
| 97 | } \ |
---|
| 98 | } while(0) |
---|
| 99 | |
---|
[c9caf07] | 100 | int main(int argc, char *argv[]) { |
---|
| 101 | |
---|
| 102 | int i; |
---|
| 103 | struct timeval start,end; |
---|
| 104 | struct timeval interval; |
---|
[edca0b8] | 105 | struct rusage start_usage,end_usage; |
---|
[c9caf07] | 106 | |
---|
[edca0b8] | 107 | getrusage(RUSAGE_SELF,&start_usage); |
---|
[c9caf07] | 108 | gettimeofday(&start,NULL); |
---|
| 109 | for(i=optind;i<argc;++i) { |
---|
| 110 | run_trace(argv[i]); |
---|
| 111 | } |
---|
| 112 | gettimeofday(&end,NULL); |
---|
[edca0b8] | 113 | getrusage(RUSAGE_SELF,&end_usage); |
---|
| 114 | |
---|
| 115 | TIMER_SUB(interval,end,start); |
---|
[c9caf07] | 116 | |
---|
| 117 | printf("Tracemarks: %.02f\n",((double)tot)/(interval.tv_sec+interval.tv_usec/100000)); |
---|
[edca0b8] | 118 | |
---|
| 119 | printf("Real: %i.%05is\n",interval.tv_sec,interval.tv_usec); |
---|
| 120 | TIMER_SUB(interval,end_usage.ru_utime,start_usage.ru_utime); |
---|
| 121 | printf("User: %i.%05is\n",interval.tv_sec,interval.tv_usec); |
---|
| 122 | TIMER_SUB(interval,end_usage.ru_stime,start_usage.ru_stime); |
---|
| 123 | printf("System: %i.%06is\n",interval.tv_sec,interval.tv_usec); |
---|
| 124 | printf("I/O: %li/%li\n",end_usage.ru_inblock-start_usage.ru_inblock, |
---|
| 125 | end_usage.ru_oublock-start_usage.ru_oublock); |
---|
[c9caf07] | 126 | return 0; |
---|
| 127 | } |
---|