source: examples/rate/rate-dir.c @ 5e9d62d

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

added rate-dir

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