source: examples/rate/rate.c @ 3840760

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

updated the examples/ and tools/ directories to correctly use trace_packet_create() now.

fixed a typo in format_wag (shouldn't use pcap_get_framing_length for wag!)

  • Property mode set to 100644
File size: 3.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 <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_type {
54        BYTES = 0,
55        PACKETS = 1
56} counter_type_t;
57
58typedef 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
66int32_t counter[MAXCOUNTERTYPE][MAXCOUNTERFRAME];
67
68struct timeval current,last,diff,total;
69
70
71void 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}
92int 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_packet_create();
98
99        uint32_t last_second = 0;
100        double ts = 0.0;
101
102
103        if (argc == 2) {
104                uri = strdup(argv[1]);
105        }
106
107        // create an trace to uri
108        trace = trace_create(uri);
109
110
111        for (;;) {
112                if ((psize = trace_read_packet(trace,&packet)) < 1) {
113                        // terminate
114                        break;
115                }
116                if (psize == 0) {
117                        continue;
118                }
119
120                if((ipptr = trace_get_ip(&packet)) == 0) {
121                        continue;
122                }
123               
124                counter[BYTES][INSTANT] += ntohs(ipptr->ip_len);
125                counter[PACKETS][INSTANT] ++;
126
127                ts = trace_get_seconds(&packet);
128                if(last_second == 0) {
129                        last_second = (int)ts;
130                } else if (last_second < (int)ts) {
131                        last_second = (int)ts;
132                        docalc++;
133                }
134
135                if(docalc) {
136                        secondreport();
137                }
138
139
140        }
141
142        trace_destroy(trace);
143        return 0;
144}
Note: See TracBrowser for help on using the repository browser.