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 alarmsig(int sig) { |
---|
77 | docalc++; |
---|
78 | } |
---|
79 | |
---|
80 | void secondreport() { |
---|
81 | |
---|
82 | static int hdrcount = 10; |
---|
83 | |
---|
84 | if (hdrcount >= 10) { |
---|
85 | printf("Byte count: smoothed[instant] Packet count: smoothed[instant]\n"); |
---|
86 | hdrcount = 0; |
---|
87 | } |
---|
88 | hdrcount++; |
---|
89 | counter[BYTES][SMOOTHED] = ALPHA * counter[BYTES][SMOOTHED] + (1 - ALPHA) * counter[BYTES][INSTANT]; |
---|
90 | counter[PACKETS][SMOOTHED] = ALPHA * counter[PACKETS][SMOOTHED] + (1 - ALPHA) * counter[PACKETS][INSTANT]; |
---|
91 | |
---|
92 | printf("\t\t%d[%d]\t\t\t%d[%d] \n", |
---|
93 | counter[BYTES][SMOOTHED], |
---|
94 | counter[BYTES][INSTANT], |
---|
95 | counter[PACKETS][SMOOTHED], |
---|
96 | counter[PACKETS][INSTANT]); |
---|
97 | counter[BYTES][INSTANT] = 0; |
---|
98 | counter[PACKETS][INSTANT] = 0; |
---|
99 | docalc=0; |
---|
100 | } |
---|
101 | int main(int argc, char *argv[]) { |
---|
102 | |
---|
103 | char *uri = 0; |
---|
104 | int psize = 0; |
---|
105 | struct sigaction sigact; |
---|
106 | struct libtrace_ip *ipptr = 0; |
---|
107 | struct libtrace_packet_t packet; |
---|
108 | |
---|
109 | struct itimerval itv; |
---|
110 | |
---|
111 | /* |
---|
112 | * Set up a timer to expire every second, for reporting |
---|
113 | */ |
---|
114 | sigact.sa_handler = alarmsig; |
---|
115 | sigact.sa_flags = SA_RESTART; |
---|
116 | if(sigaction(SIGALRM, &sigact, NULL) < 0) |
---|
117 | perror("sigaction"); |
---|
118 | itv.it_interval.tv_sec = 1; |
---|
119 | itv.it_interval.tv_usec = 0; |
---|
120 | itv.it_value.tv_sec = 1; |
---|
121 | itv.it_value.tv_usec = 0; |
---|
122 | if (setitimer(ITIMER_REAL, &itv, NULL) < 0) |
---|
123 | perror("setitimer"); |
---|
124 | |
---|
125 | if (argc == 2) { |
---|
126 | uri = strdup(argv[1]); |
---|
127 | } |
---|
128 | |
---|
129 | // create an trace to uri |
---|
130 | trace = trace_create(uri); |
---|
131 | |
---|
132 | if(!trace) { |
---|
133 | fprintf(stderr,"Bad trace, check your input uri (%s)\n",uri); |
---|
134 | exit(0); |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | for (;;) { |
---|
139 | if ((psize = event_read_packet(trace,&packet)) < 0) { |
---|
140 | break; |
---|
141 | } |
---|
142 | if (psize == 0) { |
---|
143 | continue; |
---|
144 | } |
---|
145 | |
---|
146 | if((ipptr = trace_get_ip(&packet)) == 0) { |
---|
147 | continue; |
---|
148 | } |
---|
149 | |
---|
150 | counter[BYTES][INSTANT] += ntohs(ipptr->ip_len); |
---|
151 | counter[PACKETS][INSTANT] ++; |
---|
152 | |
---|
153 | if(docalc) { |
---|
154 | secondreport(); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | } |
---|
159 | |
---|
160 | trace_destroy(trace); |
---|
161 | return 0; |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | int32_t event_read_packet(struct libtrace_t *trace, struct libtrace_packet_t *packet) { |
---|
166 | struct libtrace_eventobj_t obj; |
---|
167 | struct timeval etv; |
---|
168 | int32_t sleepusec = 0; |
---|
169 | fd_set rfds; |
---|
170 | |
---|
171 | FD_ZERO(&rfds); |
---|
172 | |
---|
173 | for (;;) { |
---|
174 | obj = trace_event(trace,packet); |
---|
175 | if (obj.size <= 0) { |
---|
176 | return obj.size; |
---|
177 | } |
---|
178 | if (obj.type == TRACE_EVENT_IOWAIT) { |
---|
179 | // select() on fd |
---|
180 | FD_ZERO(&rfds); |
---|
181 | FD_SET(obj.fd,&rfds); |
---|
182 | select(obj.fd + 1, &rfds,NULL,NULL,0); |
---|
183 | continue; |
---|
184 | } else if (obj.type == TRACE_EVENT_SLEEP) { |
---|
185 | // sleep on seconds |
---|
186 | // Use select for better precision. |
---|
187 | etv.tv_sec = (int) obj.seconds; |
---|
188 | etv.tv_usec = (int) ((obj.seconds - etv.tv_sec) * 1000000.0); |
---|
189 | select(0,NULL,NULL,NULL,&etv); |
---|
190 | continue; |
---|
191 | } else if (obj.type == TRACE_EVENT_PACKET) { |
---|
192 | return packet->size; |
---|
193 | } else { |
---|
194 | fprintf(stderr,"Unknown event type %d\n",obj.type); |
---|
195 | return -1; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | } |
---|