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: 06e453ce1a72b99e5b8da080893a40017cb7c1bf $
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: 06e453ce1a72b99e5b8da080893a40017cb7c1bf $")
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
111static NEVER_RETURNS void usage(void)
112{
113 fprintf(stderr, "usage: ring_buffer_test [OPTS]\n");
114 fprintf(stderr, " -x Debugging mode.\n");
115 fprintf(stderr, " -s <string> Set random seed to <string>.\n");
116 fprintf(stderr, " -l <length> Set the iteration number to <length>.\n");
117
118 fr_exit_now(EXIT_SUCCESS);
119}
120
121int main(int argc, char *argv[])
122{
123 int c;
124
125 int i, start, end, length = 1000;
127 uint32_t seed;
128
129 TALLOC_CTX *autofree = talloc_autofree_context();
130
131 while ((c = getopt(argc, argv, "hl:s:x")) != -1) switch (c) {
132 case 'l':
133 length = strtol(optarg, NULL, 10);
134 break;
135 case 's':
136 seed_string = optarg;
137 seed_string_len = strlen(optarg);
138 break;
139
140 case 'x':
141 debug_lvl++;
142 break;
143
144 case 'h':
145 default:
146 usage();
147 }
148#if 0
149 argc -= (optind - 1);
150 argv += (optind - 1);
151#endif
152
154 if (!rb) {
155 fprintf(stderr, "Failed creating ring buffer\n");
156 fr_exit_now(EXIT_FAILURE);
157 }
158
159 seed = 0xabcdef;
160 start = 0;
161 end = 0;
162
163 /*
164 * Allocate the first set of blocks.
165 */
166 alloc_blocks(rb, &seed, &start, &end);
167
168 /*
169 * Do 1000 rounds of alloc / free.
170 */
171 for (i = 0; i < length; i++) {
172 if (debug_lvl) printf("Loop %d (used %zu) \n", i, used);
173 alloc_blocks(rb, &seed, &start, &end);
174
175 free_blocks(rb, &seed, &start, &end);
176 }
177
178 free_blocks(rb, &seed, &start, &end);
179
180 fr_assert(used == 0);
182
183 fr_exit_now(EXIT_SUCCESS);
184}
static TALLOC_CTX * autofree
Definition fuzzer.c:45
#define RCSID(id)
Definition build.h:485
#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:846
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.
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 size_t seed_string_len
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