source: examples/event/rate-tracetime.c @ e81d1af

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

s/stdint.h/inttypes.h/

  • Property mode set to 100644
File size: 4.6 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 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
72int32_t event_read_packet(struct libtrace_t *trace, struct libtrace_packet_t *packet);
73
74struct timeval current,last,diff,total;
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 libtrace_ip *ipptr = 0;
102        struct libtrace_packet_t packet;
103        uint32_t last_second = 0;
104        double ts = 0.0;
105
106
107        if (argc == 2) {
108                uri = strdup(argv[1]);
109        }
110
111        // create an trace to uri
112        trace = trace_create(uri);
113
114        if(!trace) {
115                fprintf(stderr,"Bad trace, check your input uri (%s)\n",uri);
116                exit(0);
117        }
118
119
120        for (;;) {
121                if ((psize = event_read_packet(trace,&packet)) < 0) {
122                        break;
123                }
124                if (psize == 0) {
125                        continue;
126                }
127
128                if((ipptr = trace_get_ip(&packet)) == 0) {
129                        continue;
130                }
131               
132                counter[BYTES][INSTANT] += ntohs(ipptr->ip_len);
133                counter[PACKETS][INSTANT] ++;
134
135                ts = trace_get_seconds(&packet);
136                if(last_second == 0) {
137                        last_second = (int)ts;
138                } else if (last_second < (int)ts) {
139                        last_second = (int)ts;
140                        docalc++;
141                }
142
143                if(docalc) {
144                        secondreport();
145                }
146
147
148        }
149        trace_destroy(trace);
150        return 0;
151}
152
153
154int32_t event_read_packet(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
155        struct libtrace_eventobj_t obj;
156        struct timeval etv;
157        int32_t sleepusec = 0;
158        fd_set rfds;
159
160        FD_ZERO(&rfds);
161
162        for (;;) {
163                obj = trace_event(trace,packet);
164                if (obj.size <= 0) {
165                        return obj.size;
166                }
167                if (obj.type == TRACE_EVENT_IOWAIT) {
168                        // select() on fd
169                        FD_ZERO(&rfds);
170                        FD_SET(obj.fd,&rfds);
171                        select(obj.fd + 1, &rfds,NULL,NULL,0);
172                        continue;
173                } else if (obj.type == TRACE_EVENT_SLEEP) {
174                        // sleep on seconds
175                        // Use select for better precision.
176                        etv.tv_sec = (int) obj.seconds;
177                        etv.tv_usec = (int) ((obj.seconds - etv.tv_sec) * 1000000.0);
178                        select(0,NULL,NULL,NULL,&etv);
179                        continue;
180                } else if (obj.type == TRACE_EVENT_PACKET) {
181                        return packet->size;
182                } else {
183                        fprintf(stderr,"Unknown event type %d\n",obj.type);
184                        return -1;
185                }
186        }
187
188}
Note: See TracBrowser for help on using the repository browser.