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

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since c988da9 was 0f42351, checked in by Perry Lorier <perry@…>, 15 years ago

Lots more warnings squashed

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007 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 ALPHA 0.9
50
51static int docalc = 0;
52
53typedef enum counter_dir {
54        OUT = 0,
55        IN = 1,
56        NEITHER = 2,
57        ERK = 3
58} counter_dir_t;
59
60typedef enum counter_type {
61        BYTES = 0,
62        PACKETS = 1
63} counter_type_t;
64
65typedef enum counter_frame {
66        INSTANT = 0,
67        SMOOTHED = 1
68} counter_frame_t;
69
70#define MAXCOUNTERTYPE (PACKETS + 1)
71#define MAXCOUNTERFRAME (SMOOTHED + 1)
72#define MAXCOUNTERDIR (ERK + 1)
73int32_t counter[MAXCOUNTERDIR][MAXCOUNTERTYPE][MAXCOUNTERFRAME];
74
75struct timeval current,last,diff,total;
76
77
78static int32_t smoothed(int32_t smoothed, int32_t instant, int32_t alpha) {
79        return alpha * smoothed + (1-alpha) * instant;
80}
81
82static void secondreport() {
83        int i;
84        static int hdrcount = 10;
85
86        if (hdrcount >= 10) {
87                printf("\tOUT\t\t\tIN\t\t\tOTHER\n");
88                printf("bps\tpps\t\tbps\tpps\t\tbps\tpps\n");
89                hdrcount = 0;
90        }
91        hdrcount++;
92
93        for (i = 0; i < MAXCOUNTERDIR; i ++) {
94                counter[i][BYTES][SMOOTHED] = smoothed(counter[i][BYTES][SMOOTHED],counter[i][BYTES][INSTANT],ALPHA);
95                counter[i][PACKETS][SMOOTHED] = smoothed(counter[i][PACKETS][SMOOTHED],counter[i][PACKETS][INSTANT],ALPHA);
96        }
97        for (i = 0; i < 2; i++) {
98                printf("%d\t%d\t\t",
99                                counter[i][BYTES][SMOOTHED]*8, 
100                                counter[i][PACKETS][SMOOTHED]);
101                counter[i][BYTES][INSTANT] = 0;
102                counter[i][PACKETS][INSTANT] = 0;
103        }
104        printf("%d\t%d\t\t",
105                        counter[2][BYTES][INSTANT]*8,
106                        counter[2][PACKETS][INSTANT]);
107        counter[2][BYTES][INSTANT] = 0;
108        counter[2][PACKETS][INSTANT] = 0;
109        printf("\n");
110
111        docalc=0;
112}
113int main(int argc, char *argv[]) {
114
115        char *uri = 0;
116        int psize = 0;
117        int direction = 0;
118        struct libtrace_ip *ipptr = 0;
119        struct libtrace_packet_t *packet = trace_create_packet();
120        libtrace_err_t trace_err;
121       
122        uint32_t last_second = 0;
123        double ts = 0.0;
124       
125        if (argc == 2) {
126                uri = strdup(argv[1]);
127        }
128
129        /* create an trace to uri */
130        trace = trace_create(uri);
131        if (trace_is_err(trace)) {
132                trace_err = trace_get_err(trace);
133                printf("Error in trace_create: %s\n", trace_err.problem);
134                return -1;
135        }
136        trace_start(trace);
137        if (trace_is_err(trace)) {
138                trace_err = trace_get_err(trace);
139                printf("Error in trace_start: %s\n", trace_err.problem);
140                return -1;
141        }
142
143        for (;;) {
144                if ((psize = trace_read_packet(trace,packet)) == -1) {
145                        /* terminate */
146                        break;
147                }
148                if (psize == 0) {
149                        continue;
150                }
151
152                if((ipptr = trace_get_ip(packet)) == 0) {
153                        continue;
154                }
155
156                direction = trace_get_direction(packet);
157
158                counter[direction][BYTES][INSTANT] += ntohs(ipptr->ip_len);
159                counter[direction][PACKETS][INSTANT] ++;
160
161                ts = trace_get_seconds(packet);
162                if(last_second == 0) {
163                        last_second = ts;
164                } else if (last_second < ts) {
165                        last_second = ts;
166                        docalc++;
167                }
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.