source: lib/fifo.c @ d5879cc

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since d5879cc was d5879cc, checked in by Perry Lorier <perry@…>, 16 years ago

Fix various issues

  • 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> /* 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        fifo->length = size;
83
84        if ((fifo->base = malloc(fifo->length)) == 0) {
85                trace_set_err(errno,"malloc failed");
86                return 0;
87        }
88
89        fifo->datamap[IN] = 0;
90        fifo->datamap[OUT] = 0;
91        fifo->datamap[ACK] = 0;
92        return fifo;
93}
94
95void destroy_tracefifo(struct tracefifo_t *fifo)
96{
97        assert(fifo);
98        free(fifo->base);
99        free(fifo);
100}
101
102static void increment_pointer(struct tracefifo_t *fifo, enum which_t which, int amount) {
103        assert(fifo);
104        assert(which == IN || which == OUT || which == ACK);
105        assert(amount >= 0);
106
107        if ((fifo->datamap[which] + amount) >= fifo->length) {
108                fifo->datamap[which] = (fifo->datamap[which] + amount - fifo->length);
109        } else {
110                fifo->datamap[which] += amount;
111        }
112}
113
114void tracefifo_flush(struct tracefifo_t *fifo __attribute__((unused))) {
115        /* do nothing */
116        return;
117}
118
119static void set_pointer(struct tracefifo_t *fifo, enum which_t which, unsigned int location) {
120        assert(fifo);
121        assert(which == IN || which == OUT || which == ACK);
122
123        assert(location <= fifo->length);
124
125        fifo->datamap[which] = location;
126}
127
128static size_t tracefifo_compare(struct tracefifo_t *fifo, enum which_t first, enum which_t second) {
129        assert(fifo);
130        assert(first == IN || first == OUT || first == ACK);
131        assert(second == IN || second == OUT || second == ACK);
132
133        if (fifo->datamap[first] == fifo->datamap[second]) {
134                return 0;
135        }
136        if (fifo->datamap[first] > fifo->datamap[second]) {
137                return fifo->datamap[first] - fifo->datamap[second];
138        } else {
139                return fifo->length - (fifo->datamap[second] - fifo->datamap[first]);
140        }
141}
142
143size_t tracefifo_free(struct tracefifo_t *fifo) {
144        assert(fifo);
145        return (fifo->length - tracefifo_compare(fifo,IN,ACK));
146} 
147
148size_t tracefifo_length(struct tracefifo_t *fifo) {
149        assert(fifo);
150        return fifo->length;
151}
152
153size_t tracefifo_out_available(struct tracefifo_t *fifo) {
154        assert(fifo);
155        return tracefifo_compare(fifo,IN,OUT);
156}
157
158size_t tracefifo_ack_available(struct tracefifo_t *fifo) {
159        assert(fifo);
160        return tracefifo_compare(fifo,OUT,ACK);
161}
162
163void tracefifo_stat_int(struct tracefifo_t *fifo, tracefifo_state_t *state)
164{
165        assert(fifo);
166        assert(state);
167
168        state->in = fifo->datamap[IN];
169        state->out = fifo->datamap[OUT];
170        state->ack = fifo->datamap[ACK];
171        state->length = fifo->length;
172        state->used = tracefifo_compare(fifo,IN,ACK);
173
174}
175char *tracefifo_stat_str(struct tracefifo_t *fifo, char *desc, int delta)
176{
177        char *scan = 0;
178        assert(fifo);
179
180        if (tracefifo_stat_buffer == 0) 
181                tracefifo_stat_buffer = (char *)malloc(513);
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 = (char*)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 = (char*)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.