| 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/types.h> |
|---|
| 36 | #include <netinet/in.h> |
|---|
| 37 | #include <netinet/in_systm.h> |
|---|
| 38 | #include <netinet/ip.h> |
|---|
| 39 | #include <signal.h> |
|---|
| 40 | #include <setjmp.h> |
|---|
| 41 | #include <unistd.h> |
|---|
| 42 | #include <sys/time.h> |
|---|
| 43 | #include <inttypes.h> |
|---|
| 44 | #include "libtrace.h" |
|---|
| 45 | #include "dagformat.h" |
|---|
| 46 | |
|---|
| 47 | struct libtrace_t *trace; |
|---|
| 48 | |
|---|
| 49 | #define SCANSIZE 4096 |
|---|
| 50 | |
|---|
| 51 | #define ALPHA 0.9 |
|---|
| 52 | |
|---|
| 53 | char *buffer[SCANSIZE]; |
|---|
| 54 | |
|---|
| 55 | static int docalc = 0; |
|---|
| 56 | |
|---|
| 57 | typedef enum counter_type { |
|---|
| 58 | BYTES = 0, |
|---|
| 59 | PACKETS = 1 |
|---|
| 60 | } counter_type_t; |
|---|
| 61 | |
|---|
| 62 | typedef enum counter_frame { |
|---|
| 63 | INSTANT = 0, |
|---|
| 64 | SMOOTHED = 1 |
|---|
| 65 | } counter_frame_t; |
|---|
| 66 | |
|---|
| 67 | #define MAXCOUNTERTYPE (PACKETS + 1) |
|---|
| 68 | #define MAXCOUNTERFRAME (SMOOTHED + 1) |
|---|
| 69 | |
|---|
| 70 | int32_t counter[MAXCOUNTERTYPE][MAXCOUNTERFRAME]; |
|---|
| 71 | |
|---|
| 72 | int32_t event_read_packet(struct libtrace_t *trace, struct libtrace_packet_t *packet); |
|---|
| 73 | |
|---|
| 74 | struct timeval current,last,diff,total; |
|---|
| 75 | |
|---|
| 76 | void secondreport() { |
|---|
| 77 | |
|---|
| 78 | static int hdrcount = 10; |
|---|
| 79 | |
|---|
| 80 | if (hdrcount >= 10) { |
|---|
| 81 | printf("Byte count: smoothed[instant] Packet count: smoothed[instant]\n"); |
|---|
| 82 | hdrcount = 0; |
|---|
| 83 | } |
|---|
| 84 | hdrcount++; |
|---|
| 85 | counter[BYTES][SMOOTHED] = ALPHA * counter[BYTES][SMOOTHED] + (1 - ALPHA) * counter[BYTES][INSTANT]; |
|---|
| 86 | counter[PACKETS][SMOOTHED] = ALPHA * counter[PACKETS][SMOOTHED] + (1 - ALPHA) * counter[PACKETS][INSTANT]; |
|---|
| 87 | |
|---|
| 88 | printf("\t\t%d[%d]\t\t\t%d[%d] \n", |
|---|
| 89 | counter[BYTES][SMOOTHED], |
|---|
| 90 | counter[BYTES][INSTANT], |
|---|
| 91 | counter[PACKETS][SMOOTHED], |
|---|
| 92 | counter[PACKETS][INSTANT]); |
|---|
| 93 | counter[BYTES][INSTANT] = 0; |
|---|
| 94 | counter[PACKETS][INSTANT] = 0; |
|---|
| 95 | docalc=0; |
|---|
| 96 | } |
|---|
| 97 | int main(int argc, char *argv[]) { |
|---|
| 98 | |
|---|
| 99 | char *uri = 0; |
|---|
| 100 | int psize = 0; |
|---|
| 101 | struct libtrace_ip *ipptr = 0; |
|---|
| 102 | struct libtrace_packet_t *packet = trace_create_packet(); |
|---|
| 103 | uint32_t last_second = 0; |
|---|
| 104 | double ts = 0.0; |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | if (argc == 2) { |
|---|
| 108 | uri = strdup(argv[1]); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | // create an trace to uri |
|---|
| 112 | trace = trace_create(uri); |
|---|
| 113 | |
|---|
| 114 | if(!trace) { |
|---|
| 115 | trace_perror("trace_create"); |
|---|
| 116 | //fprintf(stderr,"Bad trace, check your input uri (%s)\n",uri); |
|---|
| 117 | exit(0); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | for (;;) { |
|---|
| 122 | if ((psize = event_read_packet(trace,&packet)) < 0) { |
|---|
| 123 | break; |
|---|
| 124 | } |
|---|
| 125 | if (psize == 0) { |
|---|
| 126 | continue; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | if((ipptr = trace_get_ip(&packet)) == 0) { |
|---|
| 130 | continue; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | counter[BYTES][INSTANT] += ntohs(ipptr->ip_len); |
|---|
| 134 | counter[PACKETS][INSTANT] ++; |
|---|
| 135 | |
|---|
| 136 | ts = trace_get_seconds(&packet); |
|---|
| 137 | if(last_second == 0) { |
|---|
| 138 | last_second = (int)ts; |
|---|
| 139 | } else if (last_second < (int)ts) { |
|---|
| 140 | last_second = (int)ts; |
|---|
| 141 | docalc++; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | if(docalc) { |
|---|
| 145 | secondreport(); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | } |
|---|
| 150 | trace_destroy(trace); |
|---|
| 151 | return 0; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | int32_t event_read_packet(struct libtrace_t *trace, struct libtrace_packet_t *packet) { |
|---|
| 156 | struct libtrace_eventobj_t obj; |
|---|
| 157 | struct timeval etv; |
|---|
| 158 | int32_t sleepusec = 0; |
|---|
| 159 | fd_set rfds; |
|---|
| 160 | |
|---|
| 161 | FD_ZERO(&rfds); |
|---|
| 162 | |
|---|
| 163 | for (;;) { |
|---|
| 164 | obj = trace_event(trace,packet); |
|---|
| 165 | if (obj.size <= 0) { |
|---|
| 166 | return obj.size; |
|---|
| 167 | } |
|---|
| 168 | if (obj.type == TRACE_EVENT_IOWAIT) { |
|---|
| 169 | // select() on fd |
|---|
| 170 | FD_ZERO(&rfds); |
|---|
| 171 | FD_SET(obj.fd,&rfds); |
|---|
| 172 | select(obj.fd + 1, &rfds,NULL,NULL,0); |
|---|
| 173 | continue; |
|---|
| 174 | } else if (obj.type == TRACE_EVENT_SLEEP) { |
|---|
| 175 | // sleep on seconds |
|---|
| 176 | // Use select for better precision. |
|---|
| 177 | etv.tv_sec = (int) obj.seconds; |
|---|
| 178 | etv.tv_usec = (int) ((obj.seconds - etv.tv_sec) * 1000000.0); |
|---|
| 179 | select(0,NULL,NULL,NULL,&etv); |
|---|
| 180 | continue; |
|---|
| 181 | } else if (obj.type == TRACE_EVENT_PACKET) { |
|---|
| 182 | return packet->size; |
|---|
| 183 | } else { |
|---|
| 184 | fprintf(stderr,"Unknown event type %d\n",obj.type); |
|---|
| 185 | return -1; |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | } |
|---|