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 ALPHA 0.9 |
---|
50 | |
---|
51 | static int docalc = 0; |
---|
52 | |
---|
53 | typedef enum counter_type { |
---|
54 | BYTES = 0, |
---|
55 | PACKETS = 1 |
---|
56 | } counter_type_t; |
---|
57 | |
---|
58 | typedef enum counter_frame { |
---|
59 | INSTANT = 0, |
---|
60 | SMOOTHED = 1 |
---|
61 | } counter_frame_t; |
---|
62 | |
---|
63 | #define MAXCOUNTERTYPE (PACKETS + 1) |
---|
64 | #define MAXCOUNTERFRAME (SMOOTHED + 1) |
---|
65 | |
---|
66 | int32_t counter[MAXCOUNTERTYPE][MAXCOUNTERFRAME]; |
---|
67 | |
---|
68 | struct timeval current,last,diff,total; |
---|
69 | |
---|
70 | |
---|
71 | void secondreport() { |
---|
72 | |
---|
73 | static int hdrcount = 10; |
---|
74 | |
---|
75 | if (hdrcount >= 10) { |
---|
76 | printf("Byte count: smoothed[instant] Packet count: smoothed[instant]\n"); |
---|
77 | hdrcount = 0; |
---|
78 | } |
---|
79 | hdrcount++; |
---|
80 | counter[BYTES][SMOOTHED] = ALPHA * counter[BYTES][SMOOTHED] + (1 - ALPHA) * counter[BYTES][INSTANT]; |
---|
81 | counter[PACKETS][SMOOTHED] = ALPHA * counter[PACKETS][SMOOTHED] + (1 - ALPHA) * counter[PACKETS][INSTANT]; |
---|
82 | |
---|
83 | printf("\t\t%d[%d]\t\t\t%d[%d] \n", |
---|
84 | counter[BYTES][SMOOTHED], |
---|
85 | counter[BYTES][INSTANT], |
---|
86 | counter[PACKETS][SMOOTHED], |
---|
87 | counter[PACKETS][INSTANT]); |
---|
88 | counter[BYTES][INSTANT] = 0; |
---|
89 | counter[PACKETS][INSTANT] = 0; |
---|
90 | docalc=0; |
---|
91 | } |
---|
92 | int main(int argc, char *argv[]) { |
---|
93 | |
---|
94 | char *uri = 0; |
---|
95 | int psize = 0; |
---|
96 | struct libtrace_ip *ipptr = 0; |
---|
97 | struct libtrace_packet_t *packet = trace_create_packet(); |
---|
98 | libtrace_err_t trace_err; |
---|
99 | |
---|
100 | uint32_t last_second = 0; |
---|
101 | double ts = 0.0; |
---|
102 | |
---|
103 | |
---|
104 | if (argc == 2) { |
---|
105 | uri = strdup(argv[1]); |
---|
106 | } |
---|
107 | |
---|
108 | // create an trace to uri |
---|
109 | trace = trace_create(uri); |
---|
110 | if (trace_is_err(trace)) { |
---|
111 | trace_err = trace_get_err(trace); |
---|
112 | printf("Error in trace_create: %s\n", trace_err.problem); |
---|
113 | return -1; |
---|
114 | } |
---|
115 | trace_start(trace); |
---|
116 | if (trace_is_err(trace)) { |
---|
117 | trace_err = trace_get_err(trace); |
---|
118 | printf("Error in trace_start: %s\n", trace_err.problem); |
---|
119 | return -1; |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | for (;;) { |
---|
124 | if ((psize = trace_read_packet(trace,packet)) < 1) { |
---|
125 | // terminate |
---|
126 | break; |
---|
127 | } |
---|
128 | if (psize == 0) { |
---|
129 | continue; |
---|
130 | } |
---|
131 | |
---|
132 | if((ipptr = trace_get_ip(packet)) == 0) { |
---|
133 | continue; |
---|
134 | } |
---|
135 | |
---|
136 | counter[BYTES][INSTANT] += ntohs(ipptr->ip_len); |
---|
137 | counter[PACKETS][INSTANT] ++; |
---|
138 | |
---|
139 | ts = trace_get_seconds(packet); |
---|
140 | if(last_second == 0) { |
---|
141 | last_second = (int)ts; |
---|
142 | } else if (last_second < (int)ts) { |
---|
143 | last_second = (int)ts; |
---|
144 | docalc++; |
---|
145 | } |
---|
146 | |
---|
147 | if(docalc) { |
---|
148 | secondreport(); |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | trace_destroy(trace); |
---|
155 | return 0; |
---|
156 | } |
---|