source: examples/rate/rate.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 5e9d62d, checked in by Daniel Lawson <dlawson@…>, 16 years ago

poink

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