source: lib/fifo.c @ 689c220

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

More portability fixes

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