source: examples/rate/rate.c @ 78b916a

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

* empty log message *

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