The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
ring_buffer_test.c
Go to the documentation of this file.
1/*
2 * ring_buffer_test.c Tests for ring buffers
3 *
4 * Version: $Id: 525ba2eddf4a01efe5991b46cd8c338c348dd2ab $
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * @copyright 2016 Alan DeKok (aland@freeradius.org)
21 */
22
23RCSID("$Id: 525ba2eddf4a01efe5991b46cd8c338c348dd2ab $")
24
25#include <freeradius-devel/io/ring_buffer.h>
26#include <freeradius-devel/util/debug.h>
27#include <freeradius-devel/util/hash.h>
28#include <freeradius-devel/util/syserror.h>
29#include <freeradius-devel/util/talloc.h>
30#include <string.h>
31
32#ifdef HAVE_GETOPT_H
33# include <getopt.h>
34#endif
35
36#define ALLOC_SIZE (8)
37#define ARRAY_SIZE (4 * ALLOC_SIZE)
38
39static size_t used = 0;
40static size_t array[ARRAY_SIZE];
42
43static int debug_lvl = 0;
44
45static char const *seed_string = "foo";
46static size_t seed_string_len = 3;
47
48/**********************************************************************/
49
50static void alloc_blocks(fr_ring_buffer_t *rb, uint32_t *seed, UNUSED int *start, int *end)
51{
52 int i;
54
55 for (i = 0; i < ALLOC_SIZE; i++) {
56 int index;
57 uint8_t *p;
58
59 index = (*end + i) & (ARRAY_SIZE - 1);
60
62 *seed = hash;
63
64 hash &= 0x3ff;
65 hash += 16; /* can't have it zero... */
66
67 array[index] = hash;
68 p = fr_ring_buffer_reserve(rb, 2048);
69
70 if (!fr_cond_assert(p != NULL)) fr_exit_now(EXIT_FAILURE);
71
72 data[index] = fr_ring_buffer_alloc(rb, hash);
73 if (!fr_cond_assert(data[index] == p)) fr_exit_now(EXIT_FAILURE);
74
75 if (debug_lvl > 1) printf("%08x\t", hash);
76
77 used += hash;
79 }
80
81 *end += ALLOC_SIZE;
82}
83
84static void free_blocks(fr_ring_buffer_t *rb, UNUSED uint32_t *seed, int *start, int *end)
85{
86 int i;
87
88 for (i = 0; i < ALLOC_SIZE; i++) {
89 int index;
90 int rcode;
91
92 index = (*start + i) & (ARRAY_SIZE - 1);
93
94 rcode = fr_ring_buffer_free(rb, array[index]);
95 if (!fr_cond_assert(rcode == 0)) fr_exit_now(EXIT_FAILURE);
96
97 used -= array[index];
99
100 array[index] = 0;
101 data[index] = NULL;
102 }
103
104 *start += ALLOC_SIZE;
105 if (*start >= ARRAY_SIZE) {
106 *start -= ARRAY_SIZE;
107 *end -= ARRAY_SIZE;
108 }
109}
110
111/*
112 * @todo - mover to acutest framework.
113 */
114static void verify_start(fr_ring_buffer_t *rb, int start_idx)
115{
116 uint8_t *p_start;
117 size_t p_size;
118 int idx = start_idx & (ARRAY_SIZE - 1);
119
120 if (!fr_cond_assert(fr_ring_buffer_start(rb, &p_start, &p_size) == 0)) fr_exit_now(EXIT_FAILURE);
121
122 if (used == 0) {
123 if (!fr_cond_assert(p_size == 0)) fr_exit_now(EXIT_FAILURE);
124 return;
125 }
126
127 /*
128 * The contiguous block at the start can never exceed
129 * the total used.
130 */
131 if (!fr_cond_assert(p_size > 0)) fr_exit_now(EXIT_FAILURE);
132 if (!fr_cond_assert(p_size <= fr_ring_buffer_used(rb))) fr_exit_now(EXIT_FAILURE);
133
134 /*
135 * The start pointer must match the first un-freed
136 * block's data pointer.
137 */
138 if (data[idx] && !fr_cond_assert(p_start == data[idx])) fr_exit_now(EXIT_FAILURE);
139}
140
141static void test_start_basic(TALLOC_CTX *ctx)
142{
144 uint8_t *p, *p2, *p_start;
145 size_t p_size;
146
147 rb = fr_ring_buffer_create(ctx, 1024);
148 if (!fr_cond_assert(rb != NULL)) fr_exit_now(EXIT_FAILURE);
149
150 /*
151 * Empty buffer: size must be 0.
152 */
153 if (!fr_cond_assert(fr_ring_buffer_start(rb, &p_start, &p_size) == 0)) fr_exit_now(EXIT_FAILURE);
154 if (!fr_cond_assert(p_size == 0)) fr_exit_now(EXIT_FAILURE);
155
156 /*
157 * Single allocation: start points to it, size matches.
158 */
159 p = fr_ring_buffer_reserve(rb, 100);
160 if (!fr_cond_assert(p != NULL)) fr_exit_now(EXIT_FAILURE);
161 p = fr_ring_buffer_alloc(rb, 100);
162 if (!fr_cond_assert(p != NULL)) fr_exit_now(EXIT_FAILURE);
163
164 if (!fr_cond_assert(fr_ring_buffer_start(rb, &p_start, &p_size) == 0)) fr_exit_now(EXIT_FAILURE);
165 if (!fr_cond_assert(p_start == p)) fr_exit_now(EXIT_FAILURE);
166 if (!fr_cond_assert(p_size == 100)) fr_exit_now(EXIT_FAILURE);
167
168 /*
169 * Second allocation: start still points to first block,
170 * size covers both contiguous blocks.
171 */
172 p2 = fr_ring_buffer_reserve(rb, 50);
173 if (!fr_cond_assert(p2 != NULL)) fr_exit_now(EXIT_FAILURE);
174 p2 = fr_ring_buffer_alloc(rb, 50);
175 if (!fr_cond_assert(p2 != NULL)) fr_exit_now(EXIT_FAILURE);
176
177 if (!fr_cond_assert(fr_ring_buffer_start(rb, &p_start, &p_size) == 0)) fr_exit_now(EXIT_FAILURE);
178 if (!fr_cond_assert(p_start == p)) fr_exit_now(EXIT_FAILURE);
179 if (!fr_cond_assert(p_size == 150)) fr_exit_now(EXIT_FAILURE);
180
181 /*
182 * Free the first block: start advances to second block.
183 */
184 if (!fr_cond_assert(fr_ring_buffer_free(rb, 100) == 0)) fr_exit_now(EXIT_FAILURE);
185
186 if (!fr_cond_assert(fr_ring_buffer_start(rb, &p_start, &p_size) == 0)) fr_exit_now(EXIT_FAILURE);
187 if (!fr_cond_assert(p_start == p2)) fr_exit_now(EXIT_FAILURE);
188 if (!fr_cond_assert(p_size == 50)) fr_exit_now(EXIT_FAILURE);
189
190 /*
191 * Free everything: size must return to 0.
192 */
193 if (!fr_cond_assert(fr_ring_buffer_free(rb, 50) == 0)) fr_exit_now(EXIT_FAILURE);
194
195 if (!fr_cond_assert(fr_ring_buffer_start(rb, &p_start, &p_size) == 0)) fr_exit_now(EXIT_FAILURE);
196 if (!fr_cond_assert(p_size == 0)) fr_exit_now(EXIT_FAILURE);
197
198 talloc_free(rb);
199
200 if (debug_lvl) printf("test_start_basic: OK\n");
201}
202
203static void test_start_wrapped(TALLOC_CTX *ctx)
204{
206 uint8_t *first, *wrapped, *p_start;
207 size_t p_size, rb_size;
208 size_t tail_size, total_used;
209
210 rb = fr_ring_buffer_create(ctx, 1024);
211 if (!fr_cond_assert(rb != NULL)) fr_exit_now(EXIT_FAILURE);
212
213 rb_size = fr_ring_buffer_size(rb);
214
215 /*
216 * Allocate most of the buffer, leaving a small gap
217 * at the end.
218 */
219 first = fr_ring_buffer_reserve(rb, rb_size - 64);
220 if (!fr_cond_assert(first != NULL)) fr_exit_now(EXIT_FAILURE);
221 first = fr_ring_buffer_alloc(rb, rb_size - 64);
222 if (!fr_cond_assert(first != NULL)) fr_exit_now(EXIT_FAILURE);
223
224 /*
225 * State: |S*************WE.......|
226 * data_start=0, data_end=rb_size-64, write_offset=rb_size-64
227 */
228
229 /*
230 * Free the first half, advancing data_start.
231 */
232 if (!fr_cond_assert(fr_ring_buffer_free(rb, (rb_size - 64) / 2) == 0)) fr_exit_now(EXIT_FAILURE);
233
234 /*
235 * State: |.....S********WE.......|
236 * data_start=(rb_size-64)/2, data_end=rb_size-64, write_offset=rb_size-64
237 */
238
239 /*
240 * Allocate 128 bytes. This is larger than the 64-byte gap at the end, so it wraps to the start
241 * of the buffer.
242 */
243 wrapped = fr_ring_buffer_reserve(rb, 128);
244 if (!fr_cond_assert(wrapped != NULL)) fr_exit_now(EXIT_FAILURE);
245 wrapped = fr_ring_buffer_alloc(rb, 128);
246 if (!fr_cond_assert(wrapped != NULL)) fr_exit_now(EXIT_FAILURE);
247
248 /*
249 * State: |***W.....S****E........|
250 * write_offset=128, data_start=(rb_size-64)/2, data_end=rb_size-64
251 * Buffer is wrapped.
252 */
253 tail_size = (rb_size - 64) - (rb_size - 64) / 2;
254 total_used = tail_size + 128;
255
256 if (!fr_cond_assert(fr_ring_buffer_used(rb) == total_used)) fr_exit_now(EXIT_FAILURE);
257
258 if (!fr_cond_assert(fr_ring_buffer_start(rb, &p_start, &p_size) == 0)) fr_exit_now(EXIT_FAILURE);
259
260 /*
261 * Start points to the tail block (from data_start to data_end), NOT the wrapped block at offset
262 * 0.
263 */
264 if (!fr_cond_assert(p_start == first + (rb_size - 64) / 2)) fr_exit_now(EXIT_FAILURE);
265 if (!fr_cond_assert(p_size == tail_size)) fr_exit_now(EXIT_FAILURE);
266
267 /*
268 * The contiguous block at start is strictly less than total_used when wrapped.
269 */
270 if (!fr_cond_assert(p_size < total_used)) fr_exit_now(EXIT_FAILURE);
271
272 /*
273 * Free the tail block. The buffer unwraps:
274 * data_start=0, data_end=128, write_offset=128
275 *
276 * Now start should point to the wrapped block at offset 0.
277 */
278 tail_size = (rb_size - 64) - (rb_size - 64) / 2;
279 if (!fr_cond_assert(fr_ring_buffer_free(rb, tail_size) == 0)) fr_exit_now(EXIT_FAILURE);
280
281 if (!fr_cond_assert(fr_ring_buffer_start(rb, &p_start, &p_size) == 0)) fr_exit_now(EXIT_FAILURE);
282 if (!fr_cond_assert(p_start == wrapped)) fr_exit_now(EXIT_FAILURE);
283 if (!fr_cond_assert(p_size == 128)) fr_exit_now(EXIT_FAILURE);
284
285 /*
286 * Free the last block: empty.
287 */
288 if (!fr_cond_assert(fr_ring_buffer_free(rb, 128) == 0)) fr_exit_now(EXIT_FAILURE);
289
290 if (!fr_cond_assert(fr_ring_buffer_start(rb, &p_start, &p_size) == 0)) fr_exit_now(EXIT_FAILURE);
291 if (!fr_cond_assert(p_size == 0)) fr_exit_now(EXIT_FAILURE);
292
293 talloc_free(rb);
294
295 if (debug_lvl) printf("test_start_wrapped: OK\n");
296}
297
298static NEVER_RETURNS void usage(void)
299{
300 fprintf(stderr, "usage: ring_buffer_test [OPTS]\n");
301 fprintf(stderr, " -x Debugging mode.\n");
302 fprintf(stderr, " -s <string> Set random seed to <string>.\n");
303 fprintf(stderr, " -l <length> Set the iteration number to <length>.\n");
304
305 fr_exit_now(EXIT_SUCCESS);
306}
307
308int main(int argc, char *argv[])
309{
310 int c;
311
312 int i, start, end, length = 1000;
314 uint32_t seed;
315
316 TALLOC_CTX *autofree = talloc_autofree_context();
317
318 while ((c = getopt(argc, argv, "hl:s:x")) != -1) switch (c) {
319 case 'l':
320 length = strtol(optarg, NULL, 10);
321 break;
322 case 's':
323 seed_string = optarg;
324 seed_string_len = strlen(optarg);
325 break;
326
327 case 'x':
328 debug_lvl++;
329 break;
330
331 case 'h':
332 default:
333 usage();
334 }
335#if 0
336 argc -= (optind - 1);
337 argv += (optind - 1);
338#endif
339
340 /*
341 * Run targeted fr_ring_buffer_start() tests first.
342 */
345
347 if (!rb) {
348 fprintf(stderr, "Failed creating ring buffer\n");
349 fr_exit_now(EXIT_FAILURE);
350 }
351
352 seed = 0xabcdef;
353 start = 0;
354 end = 0;
355
356 /*
357 * Allocate the first set of blocks.
358 */
359 alloc_blocks(rb, &seed, &start, &end);
360 verify_start(rb, start);
361
362 /*
363 * Do 1000 rounds of alloc / free.
364 */
365 for (i = 0; i < length; i++) {
366 if (debug_lvl) printf("Loop %d (used %zu) \n", i, used);
367 alloc_blocks(rb, &seed, &start, &end);
368 verify_start(rb, start);
369
370 free_blocks(rb, &seed, &start, &end);
371 verify_start(rb, start);
372 }
373
374 free_blocks(rb, &seed, &start, &end);
375 verify_start(rb, start);
376
377 fr_assert(used == 0);
379
380 fr_exit_now(EXIT_SUCCESS);
381}
static TALLOC_CTX * autofree
Definition fuzzer.c:46
#define RCSID(id)
Definition build.h:487
#define NEVER_RETURNS
Should be placed before the function return type.
Definition build.h:315
#define UNUSED
Definition build.h:317
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:131
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition debug.h:226
uint32_t fr_hash_update(void const *data, size_t size, uint32_t hash)
Definition hash.c:881
talloc_free(hp)
unsigned int uint32_t
unsigned char uint8_t
#define fr_assert(_expr)
Definition rad_assert.h:38
fr_ring_buffer_t * fr_ring_buffer_create(TALLOC_CTX *ctx, size_t size)
Create a ring buffer.
Definition ring_buffer.c:64
uint8_t * fr_ring_buffer_alloc(fr_ring_buffer_t *rb, size_t size)
Mark data as allocated.
uint8_t * fr_ring_buffer_reserve(fr_ring_buffer_t *rb, size_t size)
Reserve room in the ring buffer.
int fr_ring_buffer_free(fr_ring_buffer_t *rb, size_t size_to_free)
Mark data as free,.
size_t fr_ring_buffer_used(fr_ring_buffer_t *rb)
Get the amount of data used in a ring buffer.
size_t fr_ring_buffer_size(fr_ring_buffer_t *rb)
Get the size of the ring buffer.
int fr_ring_buffer_start(fr_ring_buffer_t *rb, uint8_t **p_start, size_t *p_size)
Get a pointer to the data at the start of the ring buffer.
static void test_start_basic(TALLOC_CTX *ctx)
int main(int argc, char *argv[])
static uint8_t * data[ARRAY_SIZE]
static void free_blocks(fr_ring_buffer_t *rb, UNUSED uint32_t *seed, int *start, int *end)
#define ARRAY_SIZE
static size_t array[ARRAY_SIZE]
static void verify_start(fr_ring_buffer_t *rb, int start_idx)
static size_t seed_string_len
static void test_start_wrapped(TALLOC_CTX *ctx)
static char const * seed_string
static size_t used
static NEVER_RETURNS void usage(void)
#define ALLOC_SIZE
static void alloc_blocks(fr_ring_buffer_t *rb, uint32_t *seed, UNUSED int *start, int *end)
static int debug_lvl
static unsigned int hash(char const *username, unsigned int tablesize)
Definition rlm_passwd.c:132
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
Definition talloc.h:51