summaryrefslogtreecommitdiffstats
path: root/runtime/perctile_ringbuf.c
blob: 6eb3b662d550411f725f2b23fccc8339ad7290a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * This file is part of the rsyslog runtime library.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *       -or-
 *       see COPYING.ASL20 in the source distribution
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <assert.h>
#include "perctile_ringbuf.h"

static uint64_t min(uint64_t a, uint64_t b) {
	return a < b ? a : b;
}

/*
 * circ buf macros derived from linux/circ_buf.h
 */

/* Return count in buffer.  */
#define CIRC_CNT(head,tail,size) (((head) - (tail)) & ((size)-1))

/* Return space available, 0..size-1.  We always leave one free char
	 as a completely full buffer has head == tail, which is the same as
	 empty.  */
#define CIRC_SPACE(head,tail,size) CIRC_CNT((tail),((head)+1),(size))

/* Return count up to the end of the buffer.  Carefully avoid
	 accessing head and tail more than once, so they can change
	 underneath us without returning inconsistent results.  */
#define CIRC_CNT_TO_END(head,tail,size) \
	({int end = (size) - (tail); \
	 int n = ((head) + end) & ((size)-1); \
	 n < end ? n : end;})

/* Return space available up to the end of the buffer.  */
#define CIRC_SPACE_TO_END(head,tail,size) \
	({int end = (size) - 1 - (head); \
	 int n = (end + (tail)) & ((size)-1); \
	 n <= end ? n : end+1;})

/* Move head by size. */
#define CIRC_ADD(idx, size, offset)	(((idx) + (offset)) & ((size) - 1))

// simple use of the linux defined circular buffer.

ringbuf_t* ringbuf_new(size_t count) {
	// use nearest power of 2
	double x = ceil(log2(count));
	size_t bufsize = pow(2, x);

	ringbuf_t *rb = calloc(1, sizeof(ringbuf_t));
	// Note: count needs to be a power of 2, otherwise our macros won't work.
	ITEM *pbuf = calloc(bufsize, sizeof(ITEM));
	rb->cb.buf = pbuf;
	rb->cb.head = rb->cb.tail = 0;
	rb->size = bufsize;

	return rb;
}

void ringbuf_del(ringbuf_t *rb) {
	if (rb) {
		if (rb->cb.buf) {
			free(rb->cb.buf);
		}
		free(rb);
	}
}

int ringbuf_append(ringbuf_t *rb, ITEM item) {
	// lock it and add
	int head = rb->cb.head,
			tail = rb->cb.tail;

	if (!CIRC_SPACE(head, tail, rb->size)) {
		return -1;
	} else {
		/* insert item into buffer */
		rb->cb.buf[head] = item;
		// move head
		rb->cb.head = CIRC_ADD(head, rb->size, 1);
	}
	return 0;
}

int ringbuf_append_with_overwrite(ringbuf_t *rb, ITEM item) {
	int head = rb->cb.head,
			tail = rb->cb.tail;
	int ret = 0;

	if (!CIRC_SPACE(head, tail, rb->size)) {
		rb->cb.tail = CIRC_ADD(tail, rb->size, 1);
	}
	ret = ringbuf_append(rb, item);
	assert(ret == 0); // we shouldn't fail due to no space.
	return ret;
}

int ringbuf_read(ringbuf_t *rb, ITEM *buf, size_t count) {
	int head = rb->cb.head,
			tail = rb->cb.tail;
	size_t copy_size = 0;

	if (!CIRC_CNT(head, tail, rb->size)) {
		return 0;
	}

	// copy to end of buffer
	copy_size = min((size_t)CIRC_CNT_TO_END(head, tail, rb->size), count);
	memcpy(buf, rb->cb.buf+tail, copy_size*sizeof(ITEM));

	rb->cb.tail = CIRC_ADD(rb->cb.tail, rb->size, copy_size);
	return copy_size;
}

size_t ringbuf_read_to_end(ringbuf_t *rb, ITEM *buf, size_t count) {
	size_t nread = 0;
	nread += ringbuf_read(rb, buf, count);
	if (nread == 0) {
		return nread;
	}
	// read the rest if buf circled around
	nread += ringbuf_read(rb, buf + nread, count - nread);
	assert(nread <= count);
	return nread;
}

bool ringbuf_peek(ringbuf_t *rb, ITEM *item) {
	if (CIRC_CNT(rb->cb.head, rb->cb.tail, rb->size) == 0) {
		return false;
	}

	*item = rb->cb.buf[rb->cb.tail];
	return true;
}

size_t ringbuf_capacity(ringbuf_t *rb) {
	return rb->size;
}

/* ---- RINGBUFFER TESTS ---- */
void ringbuf_init_test(void) {
	size_t count = 4;
	ringbuf_t *rb = ringbuf_new(count);
	// verify ringbuf empty state
	assert(rb->cb.head == 0);
	assert(rb->cb.tail == 0);
	assert(rb->size == 4);
	ringbuf_del(rb);
}

void ringbuf_simple_test(void) {
	size_t count = 3;
	ITEM item = 0;
	ringbuf_t *rb = ringbuf_new(count);
	assert(ringbuf_append(rb, 1) == 0);
	assert(ringbuf_append(rb, 2) == 0);
	assert(ringbuf_append(rb, 3) == 0);

	item = 0;
	assert(ringbuf_peek(rb, &item) == 0);
	assert(item == 1);
}

void ringbuf_append_test(void) {
	size_t count = 4;
	ringbuf_t *rb = ringbuf_new(count);

	assert(ringbuf_append(rb, 1) == 0);
	assert(ringbuf_append(rb, 2) == 0);
	assert(ringbuf_append(rb, 3) == 0);

	// check state of ringbuffer:
	// {1, 2, 3, X}
	//  T        H
	assert(rb->cb.buf[0] == 1);
	assert(rb->cb.buf[1] == 2);
	assert(rb->cb.buf[2] == 3);
	assert(rb->cb.buf[3] == 0);
	assert(rb->cb.head == 3);
	assert(rb->cb.tail == 0);

	ringbuf_del(rb);
}

void ringbuf_append_wrap_test(void) {
	size_t count = 4;
	ITEM item = 0;

	ringbuf_t *rb = ringbuf_new(count);
	assert(ringbuf_append(rb, 1) == 0);
	assert(ringbuf_append(rb, 2) == 0);
	assert(ringbuf_append(rb, 3) == 0);
	assert(ringbuf_append(rb, 4) != 0);

	// check state of ringbuffer:
	// {1, 2, 3, X}
	//  T        H
	assert(rb->cb.buf[0] == 1);
	assert(rb->cb.buf[1] == 2);
	assert(rb->cb.buf[2] == 3);
	assert(rb->cb.head == 3);
	assert(rb->cb.tail == 0);

	item = 0;
	assert(ringbuf_read(rb, &item, 1) == 1);
	assert(item == 1);

	assert(ringbuf_append(rb, 4) == 0);

	// state of ringbuffer:
	// {1, 2, 3, 4}
	//  H  T
	assert(rb->cb.buf[0] == 1);
	assert(rb->cb.buf[1] == 2);
	assert(rb->cb.buf[2] == 3);
	assert(rb->cb.buf[3] == 4);
	assert(rb->cb.head == 0);
	assert(rb->cb.tail == 1);

	item = 0;
	assert(ringbuf_read(rb, &item, 1) == 1);
	assert(item == 2);

	// test wraparound
	assert(ringbuf_append(rb, 5) == 0);

	// state of ringbuffer:
	// {1, 2, 3, 4}
	//     H  T
	assert(rb->cb.buf[0] == 5);
	assert(rb->cb.buf[1] == 2);
	assert(rb->cb.buf[2] == 3);
	assert(rb->cb.buf[3] == 4);
	assert(rb->cb.head == 1);
	assert(rb->cb.tail == 2);

	ringbuf_del(rb);
}

void ringbuf_append_overwrite_test(void) {
	size_t count = 4;
	ringbuf_t *rb = ringbuf_new(count);
	assert(ringbuf_append_with_overwrite(rb, 1) == 0);
	assert(ringbuf_append_with_overwrite(rb, 2) == 0);
	assert(ringbuf_append_with_overwrite(rb, 3) == 0);
	assert(ringbuf_append_with_overwrite(rb, 4) == 0);

	// check state of ringbuffer:
	// {1, 2, 3, 4}
	//  H  T
	assert(rb->cb.buf[0] == 1);
	assert(rb->cb.buf[1] == 2);
	assert(rb->cb.buf[2] == 3);
	assert(rb->cb.buf[3] == 4);
	assert(rb->cb.head == 0);
	assert(rb->cb.tail == 1);

	assert(ringbuf_append_with_overwrite(rb, 5) == 0);
	// check state of ringbuffer:
	// {5, 2, 3, 4}
	//     H  T
	assert(rb->cb.buf[0] == 5);
	assert(rb->cb.buf[1] == 2);
	assert(rb->cb.buf[2] == 3);
	assert(rb->cb.buf[3] == 4);
	assert(rb->cb.head == 1);
	assert(rb->cb.tail == 2);

	ringbuf_del(rb);
}

void ringbuf_read_test(void) {
	size_t count = 4;
	ringbuf_t *rb = ringbuf_new(count);
	ITEM item_array[3];
	ITEM expects[] = {1, 2, 3};
	ITEM expects2[] = {4};

	assert(ringbuf_append_with_overwrite(rb, 1) == 0);
	assert(ringbuf_append_with_overwrite(rb, 2) == 0);
	assert(ringbuf_append_with_overwrite(rb, 3) == 0);

	assert(ringbuf_read(rb, item_array, count) == 3);
	assert(memcmp(item_array, expects, 3) == 0);

	// check state of ringbuffer:
	// {X, X, X, X}
	//           H
	//           T
	assert(rb->cb.head == 3);
	assert(rb->cb.tail == 3);

	assert(ringbuf_append_with_overwrite(rb, 4) == 0);
	assert(ringbuf_read(rb, item_array, count) == 1);
	assert(memcmp(item_array, expects2, 1) == 0);
	assert(rb->cb.head == 0);
	assert(rb->cb.tail == 0);

	ringbuf_del(rb);
}

void ringbuf_read_to_end_test(void) {
	size_t count = 4;
	ringbuf_t *rb = ringbuf_new(count);
	ITEM item_array[3];
	ITEM expects[] = {1, 2, 3};
	ITEM expects2[] = {4};

	assert(ringbuf_append_with_overwrite(rb, 1) == 0);
	assert(ringbuf_append_with_overwrite(rb, 2) == 0);
	assert(ringbuf_append_with_overwrite(rb, 3) == 0);

	// state of ringbuffer:
	// {1, 2, 3, X}
	//  T        H
	assert(ringbuf_read_to_end(rb, item_array, 3) == 3);
	assert(memcmp(item_array, expects, 3) == 0);

	// check state of ringbuffer:
	// {1, 2, 3, X}
	//           H
	//           T
	assert(rb->cb.head == 3);
	assert(rb->cb.tail == 3);

	assert(ringbuf_append_with_overwrite(rb, 4) == 0);
	// state of ringbuffer:
	// {1, 2, 3, 4}
	//  H        T
	assert(ringbuf_read_to_end(rb, item_array, count) == 1);

	// check state of ringbuffer (empty):
	// {1, 2, 3, 4}
	//  H
	//  T
	assert(memcmp(item_array, expects2, 1) == 0);
	assert(rb->cb.head == 0);
	assert(rb->cb.tail == 0);

	ringbuf_del(rb);
}
/* ---- END RINGBUFFER TESTS ---- */