source: examples/rate/rate-dir.c @ e81d1af

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since e81d1af was e81d1af, checked in by Daniel Lawson <dlawson@…>, 16 years ago

s/stdint.h/inttypes.h/

  • Property mode set to 100644
File size: 4.8 KB
Line 
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
47struct libtrace_t *trace;
48
49#define SCANSIZE 4096
50
51#define ALPHA 0.9
52
53char *buffer[SCANSIZE];
54
55static int docalc = 0;
56
57typedef enum counter_dir {
58        OUT = 0,
59        IN = 1,
60        NEITHER = 2,
61        ERK = 3
62} counter_dir_t;
63
64typedef enum counter_type {
65        BYTES = 0,
66        PACKETS = 1
67} counter_type_t;
68
69typedef enum counter_frame {
70        INSTANT = 0,
71        SMOOTHED = 1
72} counter_frame_t;
73
74#define MAXCOUNTERTYPE (PACKETS + 1)
75#define MAXCOUNTERFRAME (SMOOTHED + 1)
76#define MAXCOUNTERDIR (ERK + 1)
77int32_t counter[MAXCOUNTERDIR][MAXCOUNTERTYPE][MAXCOUNTERFRAME];
78
79struct timeval current,last,diff,total;
80
81void alarmsig(int sig) {
82        docalc++;
83}
84
85static int32_t smoothed(int32_t smoothed, int32_t instant, int32_t alpha) {
86        return alpha * smoothed + (1-alpha) * instant;
87}
88
89void secondreport() {
90        int i;
91        static int hdrcount = 10;
92
93        if (hdrcount >= 10) {
94                printf("\tOUT\t\t\tIN\t\t\tOTHER\n");
95                printf("Bps\tpps\t\tBps\tpps\t\tBps\tpps\n");
96                hdrcount = 0;
97        }
98        hdrcount++;
99
100        for (i = 0; i < MAXCOUNTERDIR; i ++) {
101                counter[i][BYTES][SMOOTHED] = smoothed(counter[i][BYTES][SMOOTHED],counter[i][BYTES][INSTANT],ALPHA);
102                counter[i][PACKETS][SMOOTHED] = smoothed(counter[i][PACKETS][SMOOTHED],counter[i][PACKETS][INSTANT],ALPHA);
103        }
104        for (i = 0; i < 2; i++) {
105                printf("%d\t%d\t\t",
106                                counter[i][BYTES][SMOOTHED], 
107                                counter[i][PACKETS][SMOOTHED]);
108                counter[i][BYTES][INSTANT] = 0;
109                counter[i][PACKETS][INSTANT] = 0;
110        }
111        printf("%d\t%d\t\t",
112                        counter[2][BYTES][INSTANT],
113                        counter[2][PACKETS][INSTANT]);
114        counter[2][BYTES][INSTANT] = 0;
115        counter[2][PACKETS][INSTANT] = 0;
116        printf("\n");
117
118        docalc=0;
119}
120int main(int argc, char *argv[]) {
121
122        char *uri = 0;
123        int psize = 0;
124        int direction = 0;
125        struct sigaction sigact;
126        struct libtrace_ip *ipptr = 0;
127        struct libtrace_packet_t packet;
128
129        struct itimerval itv;
130
131        /*
132         * Set up a timer to expire every second, for reporting
133         */
134        sigact.sa_handler = alarmsig;
135        sigact.sa_flags = SA_RESTART;
136        if(sigaction(SIGALRM, &sigact, NULL) < 0)
137                perror("sigaction");
138        itv.it_interval.tv_sec = 1;
139        itv.it_interval.tv_usec = 0;
140        itv.it_value.tv_sec = 1;
141        itv.it_value.tv_usec = 0;
142        if (setitimer(ITIMER_REAL, &itv, NULL) < 0)
143                perror("setitimer");
144
145        if (argc == 2) {
146                uri = strdup(argv[1]);
147        }
148
149        // create an trace to uri
150        trace = trace_create(uri);
151
152
153        for (;;) {
154                if ((psize = trace_read_packet(trace,&packet)) == -1) {
155                        // terminate
156                        break;
157                }
158                if (psize == 0) {
159                        continue;
160                }
161
162                if((ipptr = trace_get_ip(&packet)) == 0) {
163                        continue;
164                }
165
166                direction = trace_get_direction(&packet);
167
168                counter[direction][BYTES][INSTANT] += ntohs(ipptr->ip_len);
169                counter[direction][PACKETS][INSTANT] ++;
170
171                if(docalc) {
172                        secondreport();
173                }
174
175
176        }
177
178        trace_destroy(trace);
179        return 0;
180}
Note: See TracBrowser for help on using the repository browser.