source: lib/fifo.c @ 85a79b0

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

Fixed lots of warnings

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