source: lib/fifo.c @ 7c8eacf

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

cleaned up after compiling with -Wall -W

  • Property mode set to 100644
File size: 9.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 "common.h"
32#include "config.h"
33#include <errno.h>
34#include <stdlib.h> /* free */
35#include <stdio.h> /* perror, sprintf, printf */
36#include <assert.h> /* assert */
37#include <string.h> /* bzero */
38#include "fifo.h"
39
40#include <sys/types.h>
41
42#ifdef HAVE_INTTYPES_H
43# include <inttypes.h>
44#else
45# error "Can't find inttypes.h, this needs to be fixed"
46#endif
47
48#ifdef HAVE_STDDEF_H
49# include <stddef.h>
50#else
51# error "Can't find stddef.h - do you define ptrdiff_t elsewhere"
52#endif
53
54#include <netinet/in.h>
55
56
57enum which_t { IN, OUT, ACK };
58
59struct tracefifo_t {
60        size_t length;
61        size_t datamap[3];
62        void *base;
63};
64
65#define MIN(a,b) ({ int _a = a; int _b = b; (_a < _b) ? _a : _b; })
66
67
68static char *tracefifo_stat_buffer = 0;
69
70static void increment_pointer(struct tracefifo_t *fifo, enum which_t which, int amount);
71static void set_pointer(struct tracefifo_t *fifo, enum which_t which, unsigned int location);
72static size_t tracefifo_compare(struct tracefifo_t *fifo, enum which_t first, enum which_t second);
73static int tracefifo_read_generic(struct tracefifo_t *fifo, void *buffer, size_t len, enum which_t which, char update);
74
75struct tracefifo_t *create_tracefifo(size_t size) 
76{
77        /* Set up our fifo
78        */
79        struct tracefifo_t *fifo = malloc(sizeof(struct tracefifo_t));
80        assert(size > 0);
81
82        if (tracefifo_stat_buffer == 0) 
83                tracefifo_stat_buffer = (char *)malloc(513);
84        fifo->length = size;
85
86        if ((fifo->base = malloc(fifo->length)) == 0) {
87                perror("malloc");
88                return 0;
89        }
90
91        fifo->datamap[IN] = 0;
92        fifo->datamap[OUT] = 0;
93        fifo->datamap[ACK] = 0;
94        return fifo;
95}
96
97void destroy_tracefifo(struct tracefifo_t *fifo)
98{
99        assert(fifo);
100        //free(tracefifo_stat_buffer);
101        free(fifo->base);
102        free(fifo);
103}
104
105static void increment_pointer(struct tracefifo_t *fifo, enum which_t which, int amount) {
106        assert(fifo);
107        assert(which == IN || which == OUT || which == ACK);
108        assert(amount >= 0);
109
110        if ((fifo->datamap[which] + amount) >= fifo->length) {
111                fifo->datamap[which] = (fifo->datamap[which] + amount - fifo->length);
112        } else {
113                fifo->datamap[which] += amount;
114        }
115}
116
117void tracefifo_flush(struct tracefifo_t *fifo __attribute__((unused))) {
118        // do nothing
119        return;
120}
121
122static void set_pointer(struct tracefifo_t *fifo, enum which_t which, unsigned int location) {
123        assert(fifo);
124        assert(which == IN || which == OUT || which == ACK);
125
126        assert(location <= fifo->length);
127
128        fifo->datamap[which] = location;
129}
130
131static size_t tracefifo_compare(struct tracefifo_t *fifo, enum which_t first, enum which_t second) {
132        assert(fifo);
133        assert(first == IN || first == OUT || first == ACK);
134        assert(second == IN || second == OUT || second == ACK);
135
136        if (fifo->datamap[first] == fifo->datamap[second]) {
137                return 0;
138        }
139        if (fifo->datamap[first] > fifo->datamap[second]) {
140                return fifo->datamap[first] - fifo->datamap[second];
141        } else {
142                return fifo->length - (fifo->datamap[second] - fifo->datamap[first]);
143        }
144}
145
146size_t tracefifo_free(struct tracefifo_t *fifo) {
147        assert(fifo);
148        return (fifo->length - tracefifo_compare(fifo,IN,ACK));
149} 
150
151size_t tracefifo_length(struct tracefifo_t *fifo) {
152        assert(fifo);
153        return fifo->length;
154}
155
156size_t tracefifo_out_available(struct tracefifo_t *fifo) {
157        assert(fifo);
158        return tracefifo_compare(fifo,IN,OUT);
159}
160
161size_t tracefifo_ack_available(struct tracefifo_t *fifo) {
162        assert(fifo);
163        return tracefifo_compare(fifo,OUT,ACK);
164}
165
166void tracefifo_stat_int(struct tracefifo_t *fifo, tracefifo_state_t *state)
167{
168        assert(fifo);
169        assert(state);
170
171        state->in = fifo->datamap[IN];
172        state->out = fifo->datamap[OUT];
173        state->ack = fifo->datamap[ACK];
174        state->length = fifo->length;
175        state->used = tracefifo_compare(fifo,IN,ACK);
176
177}
178char *tracefifo_stat_str(struct tracefifo_t *fifo, char *desc, int delta)
179{
180        char *scan = 0;
181        assert(fifo);
182
183        bzero(tracefifo_stat_buffer,513);
184        scan = tracefifo_stat_buffer;
185        if (desc)
186                scan += sprintf(scan,"%s\t",desc);
187        scan += sprintf(scan,"in:   %d \t",fifo->datamap[IN]);
188        scan += sprintf(scan,"sent: %d\t", fifo->datamap[OUT]);
189        scan += sprintf(scan,"ack:  %d\t", fifo->datamap[ACK]);
190        if (delta > 0)
191                scan += sprintf(scan,"delta: %d\t", delta);
192        scan += sprintf(scan,"Size: %d", tracefifo_compare(fifo,IN,ACK));
193        scan += sprintf(scan,"\n");
194        return tracefifo_stat_buffer;
195}
196void tracefifo_stat(struct tracefifo_t *fifo, char *desc, int delta)
197{
198        assert(fifo);
199
200        printf("%s",tracefifo_stat_str(fifo,desc,delta));
201}
202
203/* Read a portion from the given section of the fifo. Note that it is the responsibility
204 * of the caller to ensure that there is something to read! This will return len bytes
205 * starting at the pointer corresponding to which - if thats bogus data then its not
206 * the fault of this function */
207static int tracefifo_read_generic(struct tracefifo_t *fifo, void *buffer, size_t len, enum which_t which, char update) {
208        size_t oldptr;
209        int lenleft;
210        int size;
211        assert(fifo);
212        assert(buffer);
213
214        oldptr = fifo->datamap[which];
215        lenleft = len;
216        while (lenleft > 0) {
217                size = MIN( ( fifo->length - fifo->datamap[which]), lenleft);
218                memcpy(buffer, 
219                                (char *)((ptrdiff_t)fifo->base + fifo->datamap[which]), 
220                                size);
221                increment_pointer(fifo,which,size);
222                buffer += size;
223                lenleft -= size;
224        }
225
226        if (update == 0) {
227                set_pointer(fifo,which,oldptr);
228        }
229        return len;
230}
231
232int tracefifo_write(struct tracefifo_t *fifo, void *buffer, size_t len) {
233        int lenleft;
234        int size;
235        assert(fifo);
236        assert(buffer);
237
238        if (tracefifo_free(fifo) < len) {
239                return 0;
240        }
241
242        lenleft = len;
243        while (lenleft > 0) {
244                size = MIN((fifo->length - fifo->datamap[IN]), lenleft );
245                memcpy((char *)((ptrdiff_t)fifo->base + fifo->datamap[IN]), 
246                                buffer, 
247                                size);
248                increment_pointer(fifo,IN,size);
249                buffer += size;
250                lenleft -= size;
251        }
252        return len;
253}
254
255
256int tracefifo_out_read(struct tracefifo_t *fifo, void *buffer, size_t len) {
257        assert(fifo);
258        assert(buffer);
259        if (tracefifo_compare(fifo,IN,OUT) < len) {
260                return 0;
261        }
262        return tracefifo_read_generic(fifo,buffer,len,OUT,0);
263}
264
265int tracefifo_ack_read(struct tracefifo_t *fifo, void *buffer, size_t len) {
266        assert(fifo);
267        assert(buffer);
268        if (tracefifo_compare(fifo,OUT,ACK) < len) {
269                return 0;
270        }
271        return tracefifo_read_generic(fifo,buffer,len,ACK,0);
272}
273
274int tracefifo_out_update(struct tracefifo_t *fifo, size_t len){
275        assert(fifo);
276        if (tracefifo_compare(fifo,IN,OUT) < len) {
277                return 0;
278        }
279        increment_pointer(fifo,OUT,len);
280        return len;
281}
282
283int tracefifo_ack_update(struct tracefifo_t *fifo, size_t len){
284        assert(fifo);
285        if (tracefifo_compare(fifo,OUT,ACK) < len) {
286                return 0;
287        }
288        increment_pointer(fifo,ACK,len);
289        return len;
290}
291
292void tracefifo_out_reset(struct tracefifo_t *fifo) {
293        /*
294         * This will reset the sent pointer back to the ack pointer. This
295         * is called from the application when it realises that any data it
296         * has sent but not acked will probably have died for some reason
297         */
298        assert(fifo);
299        fifo->datamap[OUT] = fifo->datamap[ACK];
300}
301
Note: See TracBrowser for help on using the repository browser.