source: examples/event/rate.c @ 001fbb6

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

version of rate which uses the event api

  • Property mode set to 100644
File size: 4.9 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 <stdint.h>
44#include "libtrace.h"
45#include "dagformat.h"
46
47struct libtrace_t *trace;
48
49#define SCANSIZE 4096
50
51#define ALPHA 0.9
52
53char *buffer[SCANSIZE];
54
55static int docalc = 0;
56
57typedef enum counter_type {
58        BYTES = 0,
59        PACKETS = 1
60} counter_type_t;
61
62typedef enum counter_frame {
63        INSTANT = 0,
64        SMOOTHED = 1
65} counter_frame_t;
66
67#define MAXCOUNTERTYPE (PACKETS + 1)
68#define MAXCOUNTERFRAME (SMOOTHED + 1)
69
70int32_t counter[MAXCOUNTERTYPE][MAXCOUNTERFRAME];
71
72struct timeval current,last,diff,total;
73
74void alarmsig(int sig) {
75        docalc++;
76}
77
78void secondreport() {
79
80        static int hdrcount = 10;
81
82        if (hdrcount >= 10) {
83                printf("Byte count: smoothed[instant]       Packet count: smoothed[instant]\n");
84                hdrcount = 0;
85        }
86        hdrcount++;
87        counter[BYTES][SMOOTHED] = ALPHA * counter[BYTES][SMOOTHED] + (1 - ALPHA) * counter[BYTES][INSTANT];
88        counter[PACKETS][SMOOTHED] = ALPHA * counter[PACKETS][SMOOTHED] + (1 - ALPHA) * counter[PACKETS][INSTANT];
89
90        printf("\t\t%d[%d]\t\t\t%d[%d] \n", 
91                        counter[BYTES][SMOOTHED], 
92                        counter[BYTES][INSTANT],
93                        counter[PACKETS][SMOOTHED],
94                        counter[PACKETS][INSTANT]);
95        counter[BYTES][INSTANT] = 0;
96        counter[PACKETS][INSTANT] = 0;
97        docalc=0;
98}
99int main(int argc, char *argv[]) {
100
101        char *uri = 0;
102        int psize = 0;
103        struct sigaction sigact;
104        struct libtrace_ip *ipptr = 0;
105        struct libtrace_packet_t packet;
106        struct libtrace_eventobj_t obj;
107
108        struct itimerval itv;
109        struct timeval etv;
110        uint32_t sleepsec = 0;
111        fd_set rfds;
112
113        FD_ZERO(&rfds);
114
115        /*
116         * Set up a timer to expire every second, for reporting
117         */
118        sigact.sa_handler = alarmsig;
119        sigact.sa_flags = SA_RESTART;
120        if(sigaction(SIGALRM, &sigact, NULL) < 0)
121                perror("sigaction");
122        itv.it_interval.tv_sec = 1;
123        itv.it_interval.tv_usec = 0;
124        itv.it_value.tv_sec = 1;
125        itv.it_value.tv_usec = 0;
126        if (setitimer(ITIMER_REAL, &itv, NULL) < 0)
127                perror("setitimer");
128
129        if (argc == 2) {
130                uri = strdup(argv[1]);
131        }
132
133        // create an trace to uri
134        trace = trace_create(uri);
135
136        if(!trace) {
137                fprintf(stderr,"Bad trace, check your input uri (%s)\n",uri);
138                exit(0);
139        }
140
141
142        for (;;) {
143                obj = trace_event(trace,&packet);
144        //      printf("event.type = %d\n",obj.type);   
145               
146                if (obj.type == TRACE_EVENT_TERMINATE) {
147                        break;
148                }
149
150                if (obj.type == TRACE_EVENT_IOWAIT) {
151                        // sleep on fd
152                        FD_ZERO(&rfds);
153                        FD_SET(obj.fd,&rfds);
154                        select(obj.fd + 1, &rfds,NULL,NULL,0);
155                        continue;
156                } else if (obj.type == TRACE_EVENT_SLEEP) {
157                        // sleep on seconds
158                        //
159                        etv.tv_sec = (int) obj.seconds;
160                        etv.tv_usec = (int) (obj.seconds - etv.tv_sec) * 1000000;
161
162        /*              printf("waiting for %ld.%ld (%f)usec\n",
163                                        etv.tv_sec,
164                                        etv.tv_usec,
165                                        obj.seconds);
166                                        */
167                        select(0,NULL,NULL,NULL,&etv);
168                        continue;
169
170
171
172                               
173                } else if (obj.type == TRACE_EVENT_PACKET) {
174                        if (packet.size < 0) {
175                                break;
176                        } else if (packet.size == 0) {
177                                continue;
178                        }
179                } else {
180                        fprintf(stderr,"Unknown event type %d\n",obj.type);
181                        break;
182                }
183                //printf("packet.size = %d\n",packet.size);     
184                //printf("foo\n");
185
186                if((ipptr = trace_get_ip(&packet)) == 0) {
187                        continue;
188                }
189               
190                counter[BYTES][INSTANT] += ntohs(ipptr->ip_len);
191                counter[PACKETS][INSTANT] ++;
192
193                if(docalc) {
194                        secondreport();
195                }
196
197
198        }
199
200        trace_destroy(trace);
201        return 0;
202}
Note: See TracBrowser for help on using the repository browser.