source: lib/fifo.c @ 5b91b48

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

Abstract away opening up traces into a single trace_open_trace that's more
simple and reliable.

  • 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        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                trace_set_err(errno,"malloc failed");
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(fifo->base);
101        free(fifo);
102}
103
104static void increment_pointer(struct tracefifo_t *fifo, enum which_t which, int amount) {
105        assert(fifo);
106        assert(which == IN || which == OUT || which == ACK);
107        assert(amount >= 0);
108
109        if ((fifo->datamap[which] + amount) >= fifo->length) {
110                fifo->datamap[which] = (fifo->datamap[which] + amount - fifo->length);
111        } else {
112                fifo->datamap[which] += amount;
113        }
114}
115
116void tracefifo_flush(struct tracefifo_t *fifo __attribute__((unused))) {
117        /* do nothing */
118        return;
119}
120
121static void set_pointer(struct tracefifo_t *fifo, enum which_t which, unsigned int location) {
122        assert(fifo);
123        assert(which == IN || which == OUT || which == ACK);
124
125        assert(location <= fifo->length);
126
127        fifo->datamap[which] = location;
128}
129
130static size_t tracefifo_compare(struct tracefifo_t *fifo, enum which_t first, enum which_t second) {
131        assert(fifo);
132        assert(first == IN || first == OUT || first == ACK);
133        assert(second == IN || second == OUT || second == ACK);
134
135        if (fifo->datamap[first] == fifo->datamap[second]) {
136                return 0;
137        }
138        if (fifo->datamap[first] > fifo->datamap[second]) {
139                return fifo->datamap[first] - fifo->datamap[second];
140        } else {
141                return fifo->length - (fifo->datamap[second] - fifo->datamap[first]);
142        }
143}
144
145size_t tracefifo_free(struct tracefifo_t *fifo) {
146        assert(fifo);
147        return (fifo->length - tracefifo_compare(fifo,IN,ACK));
148} 
149
150size_t tracefifo_length(struct tracefifo_t *fifo) {
151        assert(fifo);
152        return fifo->length;
153}
154
155size_t tracefifo_out_available(struct tracefifo_t *fifo) {
156        assert(fifo);
157        return tracefifo_compare(fifo,IN,OUT);
158}
159
160size_t tracefifo_ack_available(struct tracefifo_t *fifo) {
161        assert(fifo);
162        return tracefifo_compare(fifo,OUT,ACK);
163}
164
165void tracefifo_stat_int(struct tracefifo_t *fifo, tracefifo_state_t *state)
166{
167        assert(fifo);
168        assert(state);
169
170        state->in = fifo->datamap[IN];
171        state->out = fifo->datamap[OUT];
172        state->ack = fifo->datamap[ACK];
173        state->length = fifo->length;
174        state->used = tracefifo_compare(fifo,IN,ACK);
175
176}
177char *tracefifo_stat_str(struct tracefifo_t *fifo, char *desc, int delta)
178{
179        char *scan = 0;
180        assert(fifo);
181
182        bzero(tracefifo_stat_buffer,513);
183        scan = tracefifo_stat_buffer;
184        if (desc)
185                scan += sprintf(scan,"%s\t",desc);
186        scan += sprintf(scan,"in:   %d \t",fifo->datamap[IN]);
187        scan += sprintf(scan,"sent: %d\t", fifo->datamap[OUT]);
188        scan += sprintf(scan,"ack:  %d\t", fifo->datamap[ACK]);
189        if (delta > 0)
190                scan += sprintf(scan,"delta: %d\t", delta);
191        scan += sprintf(scan,"Size: %d", tracefifo_compare(fifo,IN,ACK));
192        scan += sprintf(scan,"\n");
193        return tracefifo_stat_buffer;
194}
195void tracefifo_stat(struct tracefifo_t *fifo, char *desc, int delta)
196{
197        assert(fifo);
198
199        printf("%s",tracefifo_stat_str(fifo,desc,delta));
200}
201
202/* Read a portion from the given section of the fifo. Note that it is the responsibility
203 * of the caller to ensure that there is something to read! This will return len bytes
204 * starting at the pointer corresponding to which - if thats bogus data then its not
205 * the fault of this function */
206static int tracefifo_read_generic(struct tracefifo_t *fifo, void *buffer, size_t len, enum which_t which, char update) {
207        size_t oldptr;
208        int lenleft;
209        int size;
210        assert(fifo);
211        assert(buffer);
212
213        oldptr = fifo->datamap[which];
214        lenleft = len;
215        while (lenleft > 0) {
216                size = MIN( ( fifo->length - fifo->datamap[which]), lenleft);
217                memcpy(buffer, 
218                                (char *)((ptrdiff_t)fifo->base + fifo->datamap[which]), 
219                                size);
220                increment_pointer(fifo,which,size);
221                buffer = (char*)buffer+size;
222                lenleft -= size;
223        }
224
225        if (update == 0) {
226                set_pointer(fifo,which,oldptr);
227        }
228        return len;
229}
230
231int tracefifo_write(struct tracefifo_t *fifo, void *buffer, size_t len) {
232        int lenleft;
233        int size;
234        assert(fifo);
235        assert(buffer);
236
237        if (tracefifo_free(fifo) < len) {
238                return 0;
239        }
240
241        lenleft = len;
242        while (lenleft > 0) {
243                size = MIN((fifo->length - fifo->datamap[IN]), lenleft );
244                memcpy((char *)((ptrdiff_t)fifo->base + fifo->datamap[IN]), 
245                                buffer, 
246                                size);
247                increment_pointer(fifo,IN,size);
248                buffer += size;
249                lenleft -= size;
250        }
251        return len;
252}
253
254
255int tracefifo_out_read(struct tracefifo_t *fifo, void *buffer, size_t len) {
256        assert(fifo);
257        assert(buffer);
258        if (tracefifo_compare(fifo,IN,OUT) < len) {
259                return 0;
260        }
261        return tracefifo_read_generic(fifo,buffer,len,OUT,0);
262}
263
264int tracefifo_ack_read(struct tracefifo_t *fifo, void *buffer, size_t len) {
265        assert(fifo);
266        assert(buffer);
267        if (tracefifo_compare(fifo,OUT,ACK) < len) {
268                return 0;
269        }
270        return tracefifo_read_generic(fifo,buffer,len,ACK,0);
271}
272
273int tracefifo_out_update(struct tracefifo_t *fifo, size_t len){
274        assert(fifo);
275        if (tracefifo_compare(fifo,IN,OUT) < len) {
276                return 0;
277        }
278        increment_pointer(fifo,OUT,len);
279        return len;
280}
281
282int tracefifo_ack_update(struct tracefifo_t *fifo, size_t len){
283        assert(fifo);
284        if (tracefifo_compare(fifo,OUT,ACK) < len) {
285                return 0;
286        }
287        increment_pointer(fifo,ACK,len);
288        return len;
289}
290
291void tracefifo_out_reset(struct tracefifo_t *fifo) {
292        /*
293         * This will reset the sent pointer back to the ack pointer. This
294         * is called from the application when it realises that any data it
295         * has sent but not acked will probably have died for some reason
296         */
297        assert(fifo);
298        fifo->datamap[OUT] = fifo->datamap[ACK];
299}
300
Note: See TracBrowser for help on using the repository browser.