| 1 |
/* |
| 2 |
* This file is part of the rsyslog runtime library. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* -or- |
| 10 |
* see COPYING.ASL20 in the source distribution |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <stdint.h> |
| 22 |
#include <stdbool.h> |
| 23 |
#include <math.h> |
| 24 |
#include <assert.h> |
| 25 |
#include "perctile_ringbuf.h" |
| 26 |
|
| 27 |
static uint64_t min(uint64_t a, uint64_t b) { |
| 28 |
return a < b ? a : b; |
| 29 |
} |
| 30 |
|
| 31 |
/* |
| 32 |
* circ buf macros derived from linux/circ_buf.h |
| 33 |
*/ |
| 34 |
|
| 35 |
/* Return count in buffer. */ |
| 36 |
#define CIRC_CNT(head, tail, size) (((head) - (tail)) & ((size) - 1)) |
| 37 |
|
| 38 |
/* Return space available, 0..size-1. We always leave one free char |
| 39 |
as a completely full buffer has head == tail, which is the same as |
| 40 |
empty. */ |
| 41 |
#define CIRC_SPACE(head, tail, size) CIRC_CNT((tail), ((head) + 1), (size)) |
| 42 |
|
| 43 |
/* Return count up to the end of the buffer. Carefully avoid |
| 44 |
accessing head and tail more than once, so they can change |
| 45 |
underneath us without returning inconsistent results. */ |
| 46 |
#define CIRC_CNT_TO_END(head, tail, size) \ |
| 47 |
({ \ |
| 48 |
int end = (size) - (tail); \ |
| 49 |
int n = ((head) + end) & ((size) - 1); \ |
| 50 |
n < end ? n : end; \ |
| 51 |
}) |
| 52 |
|
| 53 |
/* Return space available up to the end of the buffer. */ |
| 54 |
#define CIRC_SPACE_TO_END(head, tail, size) \ |
| 55 |
({ \ |
| 56 |
int end = (size) - 1 - (head); \ |
| 57 |
int n = (end + (tail)) & ((size) - 1); \ |
| 58 |
n <= end ? n : end + 1; \ |
| 59 |
}) |
| 60 |
|
| 61 |
/* Move head by size. */ |
| 62 |
#define CIRC_ADD(idx, size, offset) (((idx) + (offset)) & ((size) - 1)) |
| 63 |
|
| 64 |
// simple use of the linux defined circular buffer. |
| 65 |
|
| 66 |
ringbuf_t *ringbuf_new(size_t count) { |
| 67 |
// use nearest power of 2 |
| 68 |
double x = ceil(log2(count)); |
| 69 |
size_t bufsize = pow(2, x); |
| 70 |
|
| 71 |
ringbuf_t *rb = calloc(1, sizeof(ringbuf_t)); |
| 72 |
// Note: count needs to be a power of 2, otherwise our macros won't work. |
| 73 |
ITEM *pbuf = calloc(bufsize, sizeof(ITEM)); |
| 74 |
rb->cb.buf = pbuf; |
| 75 |
rb->cb.head = rb->cb.tail = 0; |
| 76 |
rb->size = bufsize; |
| 77 |
|
| 78 |
return rb; |
| 79 |
} |
| 80 |
|
| 81 |
void ringbuf_del(ringbuf_t *rb) { |
| 82 |
if (rb) { |
| 83 |
if (rb->cb.buf) { |
| 84 |
free(rb->cb.buf); |
| 85 |
} |
| 86 |
free(rb); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
int ringbuf_append(ringbuf_t *rb, ITEM item) { |
| 91 |
// lock it and add |
| 92 |
int head = rb->cb.head, tail = rb->cb.tail; |
| 93 |
|
| 94 |
if (!CIRC_SPACE(head, tail, rb->size)) { |
| 95 |
return -1; |
| 96 |
} else { |
| 97 |
/* insert item into buffer */ |
| 98 |
rb->cb.buf[head] = item; |
| 99 |
// move head |
| 100 |
rb->cb.head = CIRC_ADD(head, rb->size, 1); |
| 101 |
} |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
int ringbuf_append_with_overwrite(ringbuf_t *rb, ITEM item) { |
| 106 |
int head = rb->cb.head, tail = rb->cb.tail; |
| 107 |
int ret = 0; |
| 108 |
|
| 109 |
if (!CIRC_SPACE(head, tail, rb->size)) { |
| 110 |
rb->cb.tail = CIRC_ADD(tail, rb->size, 1); |
| 111 |
} |
| 112 |
ret = ringbuf_append(rb, item); |
| 113 |
assert(ret == 0); // we shouldn't fail due to no space. |
| 114 |
return ret; |
| 115 |
} |
| 116 |
|
| 117 |
int ringbuf_read(ringbuf_t *rb, ITEM *buf, size_t count) { |
| 118 |
int head = rb->cb.head, tail = rb->cb.tail; |
| 119 |
size_t copy_size = 0; |
| 120 |
|
| 121 |
if (!CIRC_CNT(head, tail, rb->size)) { |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
// copy to end of buffer |
| 126 |
copy_size = min((size_t)CIRC_CNT_TO_END(head, tail, rb->size), count); |
| 127 |
memcpy(buf, rb->cb.buf + tail, copy_size * sizeof(ITEM)); |
| 128 |
|
| 129 |
rb->cb.tail = CIRC_ADD(rb->cb.tail, rb->size, copy_size); |
| 130 |
return copy_size; |
| 131 |
} |
| 132 |
|
| 133 |
size_t ringbuf_read_to_end(ringbuf_t *rb, ITEM *buf, size_t count) { |
| 134 |
size_t nread = 0; |
| 135 |
nread += ringbuf_read(rb, buf, count); |
| 136 |
if (nread == 0) { |
| 137 |
return nread; |
| 138 |
} |
| 139 |
// read the rest if buf circled around |
| 140 |
nread += ringbuf_read(rb, buf + nread, count - nread); |
| 141 |
assert(nread <= count); |
| 142 |
return nread; |
| 143 |
} |
| 144 |
|
| 145 |
bool ringbuf_peek(ringbuf_t *rb, ITEM *item) { |
| 146 |
if (CIRC_CNT(rb->cb.head, rb->cb.tail, rb->size) == 0) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
*item = rb->cb.buf[rb->cb.tail]; |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
size_t ringbuf_capacity(ringbuf_t *rb) { |
| 155 |
return rb->size; |
| 156 |
} |
| 157 |
|
| 158 |
/* ---- RINGBUFFER TESTS ---- */ |
| 159 |
void ringbuf_init_test(void) { |
| 160 |
size_t count = 4; |
| 161 |
ringbuf_t *rb = ringbuf_new(count); |
| 162 |
// verify ringbuf empty state |
| 163 |
assert(rb->cb.head == 0); |
| 164 |
assert(rb->cb.tail == 0); |
| 165 |
assert(rb->size == 4); |
| 166 |
ringbuf_del(rb); |
| 167 |
} |
| 168 |
|
| 169 |
void ringbuf_simple_test(void) { |
| 170 |
size_t count = 3; |
| 171 |
ITEM item = 0; |
| 172 |
ringbuf_t *rb = ringbuf_new(count); |
| 173 |
assert(ringbuf_append(rb, 1) == 0); |
| 174 |
assert(ringbuf_append(rb, 2) == 0); |
| 175 |
assert(ringbuf_append(rb, 3) == 0); |
| 176 |
|
| 177 |
item = 0; |
| 178 |
assert(ringbuf_peek(rb, &item) == 0); |
| 179 |
assert(item == 1); |
| 180 |
} |
| 181 |
|
| 182 |
void ringbuf_append_test(void) { |
| 183 |
size_t count = 4; |
| 184 |
ringbuf_t *rb = ringbuf_new(count); |
| 185 |
|
| 186 |
assert(ringbuf_append(rb, 1) == 0); |
| 187 |
assert(ringbuf_append(rb, 2) == 0); |
| 188 |
assert(ringbuf_append(rb, 3) == 0); |
| 189 |
|
| 190 |
// check state of ringbuffer: |
| 191 |
// {1, 2, 3, X} |
| 192 |
// T H |
| 193 |
assert(rb->cb.buf[0] == 1); |
| 194 |
assert(rb->cb.buf[1] == 2); |
| 195 |
assert(rb->cb.buf[2] == 3); |
| 196 |
assert(rb->cb.buf[3] == 0); |
| 197 |
assert(rb->cb.head == 3); |
| 198 |
assert(rb->cb.tail == 0); |
| 199 |
|
| 200 |
ringbuf_del(rb); |
| 201 |
} |
| 202 |
|
| 203 |
void ringbuf_append_wrap_test(void) { |
| 204 |
size_t count = 4; |
| 205 |
ITEM item = 0; |
| 206 |
|
| 207 |
ringbuf_t *rb = ringbuf_new(count); |
| 208 |
assert(ringbuf_append(rb, 1) == 0); |
| 209 |
assert(ringbuf_append(rb, 2) == 0); |
| 210 |
assert(ringbuf_append(rb, 3) == 0); |
| 211 |
assert(ringbuf_append(rb, 4) != 0); |
| 212 |
|
| 213 |
// check state of ringbuffer: |
| 214 |
// {1, 2, 3, X} |
| 215 |
// T H |
| 216 |
assert(rb->cb.buf[0] == 1); |
| 217 |
assert(rb->cb.buf[1] == 2); |
| 218 |
assert(rb->cb.buf[2] == 3); |
| 219 |
assert(rb->cb.head == 3); |
| 220 |
assert(rb->cb.tail == 0); |
| 221 |
|
| 222 |
item = 0; |
| 223 |
assert(ringbuf_read(rb, &item, 1) == 1); |
| 224 |
assert(item == 1); |
| 225 |
|
| 226 |
assert(ringbuf_append(rb, 4) == 0); |
| 227 |
|
| 228 |
// state of ringbuffer: |
| 229 |
// {1, 2, 3, 4} |
| 230 |
// H T |
| 231 |
assert(rb->cb.buf[0] == 1); |
| 232 |
assert(rb->cb.buf[1] == 2); |
| 233 |
assert(rb->cb.buf[2] == 3); |
| 234 |
assert(rb->cb.buf[3] == 4); |
| 235 |
assert(rb->cb.head == 0); |
| 236 |
assert(rb->cb.tail == 1); |
| 237 |
|
| 238 |
item = 0; |
| 239 |
assert(ringbuf_read(rb, &item, 1) == 1); |
| 240 |
assert(item == 2); |
| 241 |
|
| 242 |
// test wraparound |
| 243 |
assert(ringbuf_append(rb, 5) == 0); |
| 244 |
|
| 245 |
// state of ringbuffer: |
| 246 |
// {1, 2, 3, 4} |
| 247 |
// H T |
| 248 |
assert(rb->cb.buf[0] == 5); |
| 249 |
assert(rb->cb.buf[1] == 2); |
| 250 |
assert(rb->cb.buf[2] == 3); |
| 251 |
assert(rb->cb.buf[3] == 4); |
| 252 |
assert(rb->cb.head == 1); |
| 253 |
assert(rb->cb.tail == 2); |
| 254 |
|
| 255 |
ringbuf_del(rb); |
| 256 |
} |
| 257 |
|
| 258 |
void ringbuf_append_overwrite_test(void) { |
| 259 |
size_t count = 4; |
| 260 |
ringbuf_t *rb = ringbuf_new(count); |
| 261 |
assert(ringbuf_append_with_overwrite(rb, 1) == 0); |
| 262 |
assert(ringbuf_append_with_overwrite(rb, 2) == 0); |
| 263 |
assert(ringbuf_append_with_overwrite(rb, 3) == 0); |
| 264 |
assert(ringbuf_append_with_overwrite(rb, 4) == 0); |
| 265 |
|
| 266 |
// check state of ringbuffer: |
| 267 |
// {1, 2, 3, 4} |
| 268 |
// H T |
| 269 |
assert(rb->cb.buf[0] == 1); |
| 270 |
assert(rb->cb.buf[1] == 2); |
| 271 |
assert(rb->cb.buf[2] == 3); |
| 272 |
assert(rb->cb.buf[3] == 4); |
| 273 |
assert(rb->cb.head == 0); |
| 274 |
assert(rb->cb.tail == 1); |
| 275 |
|
| 276 |
assert(ringbuf_append_with_overwrite(rb, 5) == 0); |
| 277 |
// check state of ringbuffer: |
| 278 |
// {5, 2, 3, 4} |
| 279 |
// H T |
| 280 |
assert(rb->cb.buf[0] == 5); |
| 281 |
assert(rb->cb.buf[1] == 2); |
| 282 |
assert(rb->cb.buf[2] == 3); |
| 283 |
assert(rb->cb.buf[3] == 4); |
| 284 |
assert(rb->cb.head == 1); |
| 285 |
assert(rb->cb.tail == 2); |
| 286 |
|
| 287 |
ringbuf_del(rb); |
| 288 |
} |
| 289 |
|
| 290 |
void ringbuf_read_test(void) { |
| 291 |
size_t count = 4; |
| 292 |
ringbuf_t *rb = ringbuf_new(count); |
| 293 |
ITEM item_array[3]; |
| 294 |
ITEM expects[] = {1, 2, 3}; |
| 295 |
ITEM expects2[] = {4}; |
| 296 |
|
| 297 |
assert(ringbuf_append_with_overwrite(rb, 1) == 0); |
| 298 |
assert(ringbuf_append_with_overwrite(rb, 2) == 0); |
| 299 |
assert(ringbuf_append_with_overwrite(rb, 3) == 0); |
| 300 |
|
| 301 |
assert(ringbuf_read(rb, item_array, count) == 3); |
| 302 |
assert(memcmp(item_array, expects, 3) == 0); |
| 303 |
|
| 304 |
// check state of ringbuffer: |
| 305 |
// {X, X, X, X} |
| 306 |
// H |
| 307 |
// T |
| 308 |
assert(rb->cb.head == 3); |
| 309 |
assert(rb->cb.tail == 3); |
| 310 |
|
| 311 |
assert(ringbuf_append_with_overwrite(rb, 4) == 0); |
| 312 |
assert(ringbuf_read(rb, item_array, count) == 1); |
| 313 |
assert(memcmp(item_array, expects2, 1) == 0); |
| 314 |
assert(rb->cb.head == 0); |
| 315 |
assert(rb->cb.tail == 0); |
| 316 |
|
| 317 |
ringbuf_del(rb); |
| 318 |
} |
| 319 |
|
| 320 |
void ringbuf_read_to_end_test(void) { |
| 321 |
size_t count = 4; |
| 322 |
ringbuf_t *rb = ringbuf_new(count); |
| 323 |
ITEM item_array[3]; |
| 324 |
ITEM expects[] = {1, 2, 3}; |
| 325 |
ITEM expects2[] = {4}; |
| 326 |
|
| 327 |
assert(ringbuf_append_with_overwrite(rb, 1) == 0); |
| 328 |
assert(ringbuf_append_with_overwrite(rb, 2) == 0); |
| 329 |
assert(ringbuf_append_with_overwrite(rb, 3) == 0); |
| 330 |
|
| 331 |
// state of ringbuffer: |
| 332 |
// {1, 2, 3, X} |
| 333 |
// T H |
| 334 |
assert(ringbuf_read_to_end(rb, item_array, 3) == 3); |
| 335 |
assert(memcmp(item_array, expects, 3) == 0); |
| 336 |
|
| 337 |
// check state of ringbuffer: |
| 338 |
// {1, 2, 3, X} |
| 339 |
// H |
| 340 |
// T |
| 341 |
assert(rb->cb.head == 3); |
| 342 |
assert(rb->cb.tail == 3); |
| 343 |
|
| 344 |
assert(ringbuf_append_with_overwrite(rb, 4) == 0); |
| 345 |
// state of ringbuffer: |
| 346 |
// {1, 2, 3, 4} |
| 347 |
// H T |
| 348 |
assert(ringbuf_read_to_end(rb, item_array, count) == 1); |
| 349 |
|
| 350 |
// check state of ringbuffer (empty): |
| 351 |
// {1, 2, 3, 4} |
| 352 |
// H |
| 353 |
// T |
| 354 |
assert(memcmp(item_array, expects2, 1) == 0); |
| 355 |
assert(rb->cb.head == 0); |
| 356 |
assert(rb->cb.tail == 0); |
| 357 |
|
| 358 |
ringbuf_del(rb); |
| 359 |
} |
| 360 |
/* ---- END RINGBUFFER TESTS ---- */ |