source: lib/trace_sliding_window.c @ 29ba7c2

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 29ba7c2 was 29bbef0, checked in by Richard Sanger <rsangerarj@…>, 7 years ago

My work from over summer, with a few things tidied up and updated to include recent commits/patches to bring this up to date. Still very much work in progress.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1#include "trace_sliding_window.h"
2#include <stdlib.h>
3#include <assert.h>
4#include <string.h>
5
6/**
7 * Implements a sliding window via a ring buffer, this is a fixed size.
8 *
9 * @param rb A pointer to a ringbuffer structure.
10 * @param size The maximum size of the ring buffer, note 1 of these slots are unusable.
11 * @param mode The mode allows selection to use semaphores to signal when data
12 *                              becomes available. LIBTRACE_RINGBUFFER_BLOCKING or LIBTRACE_RINGBUFFER_POLLING.
13 *                              NOTE: this mainly applies to the blocking functions
14 */
15inline void libtrace_slidingwindow_init(libtrace_slidingwindow_t *sw, int size, uint64_t start_number) {
16        sw->size = size; // All of this size can be used
17        sw->start = 0;
18        sw->elements = calloc(sw->size, sizeof(void*));
19        assert(sw->elements);
20        memset(sw->elements, 0, sizeof(void*) * sw->size);
21        sw->start_number = start_number;
22}
23
24/**
25 * Destroys the ring buffer along with any memory allocated to it
26 * @param rb The ringbuffer to destroy
27 */
28inline void libtrace_slidingwindow_destroy(libtrace_slidingwindow_t *sw) {
29        sw->size = 0;
30        sw->start = 0;
31        sw->start_number = 0;
32        free((void *)sw->elements);
33        sw->elements = NULL;
34}
35
36
37/**
38 * Performs a non-blocking write to the buffer, if their is no space
39 * or the list is locked by another thread this will return immediately
40 * without writing the value. Assumes that only one thread is writing.
41 * Otherwise use libtrace_ringbuffer_try_swrite.
42 *
43 * @param rb a pointer to libtrace_ringbuffer structure
44 * @param value the value to store
45 * @return 1 if a object was written otherwise 0.
46 */
47inline int libtrace_slidingwindow_try_write(libtrace_slidingwindow_t *sw, uint64_t number, void* value) {
48        uint64_t adjusted_number = number - sw->start_number;
49        if (adjusted_number < sw->size) {
50                // Add it
51                sw->elements[(adjusted_number + sw->start) % sw->size] = value;
52                return 1;
53        } else {
54                // Out of range don't add it
55                return 0;
56        }
57}
58
59static inline uint64_t libtrace_slidingwindow_get_min_number(libtrace_slidingwindow_t *sw) {
60        return sw->start_number;
61}
62
63inline uint64_t libtrace_slidingwindow_read_ready(libtrace_slidingwindow_t *sw) {
64        return sw->elements[sw->start] != NULL;
65}
66
67/**
68 * Tries to read from the supplied buffer if it fails this and returns
69 * 0 to indicate nothing was read.
70 *
71 * @param rb a pointer to libtrace_ringbuffer structure
72 * @param out a pointer to a memory address where the returned item would be placed
73 * @return 1 if a object was received otherwise 0, in this case out remains unchanged
74 */
75inline int libtrace_slidingwindow_try_read(libtrace_slidingwindow_t *sw, void ** value, uint64_t *number) {
76        if (sw->elements[sw->start]) {
77                *value = sw->elements[sw->start];
78                sw->elements[sw->start] = NULL;
79                if (number)
80                        *number = sw->start_number;
81                ++sw->start_number;
82                sw->start = (sw->start + 1) % sw->size;
83                return 1;
84        } else {
85                return 0;
86        }
87}
88
89inline void libtrace_zero_slidingwindow(libtrace_slidingwindow_t * sw)
90{
91        sw->start = 0;
92        sw->start_number = 0;
93        sw->size = 0;
94        sw->elements = NULL;
95}
Note: See TracBrowser for help on using the repository browser.