source: examples/rate/rate.c @ c837874

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

added license information

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