The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: d76389cb2cb1dc1555b0d31a5b08f441b769c8ab $
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 
23 RCSID("$Id: d76389cb2cb1dc1555b0d31a5b08f441b769c8ab $")
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 
39 static size_t used = 0;
40 static size_t array[ARRAY_SIZE];
42 
43 static int debug_lvl = 0;
44 
45 static char const *seed_string = "foo";
46 static size_t seed_string_len = 3;
47 
48 /**********************************************************************/
49 typedef struct request_s request_t;
51 void request_verify(UNUSED char const *file, UNUSED int line, UNUSED request_t *request);
52 
54 {
55  return NULL;
56 }
57 
58 void request_verify(UNUSED char const *file, UNUSED int line, UNUSED request_t *request)
59 {
60 }
61 
62 /**********************************************************************/
63 
64 static void alloc_blocks(fr_ring_buffer_t *rb, uint32_t *seed, UNUSED int *start, int *end)
65 {
66  int i;
67  uint32_t hash;
68 
69  for (i = 0; i < ALLOC_SIZE; i++) {
70  int index;
71  uint8_t *p;
72 
73  index = (*end + i) & (ARRAY_SIZE - 1);
74 
76  *seed = hash;
77 
78  hash &= 0x3ff;
79  hash += 16; /* can't have it zero... */
80 
81  array[index] = hash;
82  p = fr_ring_buffer_reserve(rb, 2048);
83 
84  if (!fr_cond_assert(p != NULL)) fr_exit_now(EXIT_FAILURE);
85 
86  data[index] = fr_ring_buffer_alloc(rb, hash);
87  if (!fr_cond_assert(data[index] == p)) fr_exit_now(EXIT_FAILURE);
88 
89  if (debug_lvl > 1) printf("%08x\t", hash);
90 
91  used += hash;
93  }
94 
95  *end += ALLOC_SIZE;
96 }
97 
98 static void free_blocks(fr_ring_buffer_t *rb, UNUSED uint32_t *seed, int *start, int *end)
99 {
100  int i;
101 
102  for (i = 0; i < ALLOC_SIZE; i++) {
103  int index;
104  int rcode;
105 
106  index = (*start + i) & (ARRAY_SIZE - 1);
107 
108  rcode = fr_ring_buffer_free(rb, array[index]);
109  if (!fr_cond_assert(rcode == 0)) fr_exit_now(EXIT_FAILURE);
110 
111  used -= array[index];
113 
114  array[index] = 0;
115  data[index] = NULL;
116  }
117 
118  *start += ALLOC_SIZE;
119  if (*start > ARRAY_SIZE) {
120  *start -= ARRAY_SIZE;
121  *end -= ARRAY_SIZE;
122  }
123 }
124 
125 static NEVER_RETURNS void usage(void)
126 {
127  fprintf(stderr, "usage: ring_buffer_test [OPTS]\n");
128  fprintf(stderr, " -x Debugging mode.\n");
129  fprintf(stderr, " -s <string> Set random seed to <string>.\n");
130  fprintf(stderr, " -l <length> Set the iteration number to <length>.\n");
131 
132  fr_exit_now(EXIT_SUCCESS);
133 }
134 
135 int main(int argc, char *argv[])
136 {
137  int c;
138 
139  int i, start, end, length = 1000;
141  uint32_t seed;
142 
143  TALLOC_CTX *autofree = talloc_autofree_context();
144 
145  while ((c = getopt(argc, argv, "hl:s:x")) != -1) switch (c) {
146  case 'l':
147  length = strtol(optarg, NULL, 10);
148  break;
149  case 's':
150  seed_string = optarg;
151  seed_string_len = strlen(optarg);
152  break;
153 
154  case 'x':
155  debug_lvl++;
156  break;
157 
158  case 'h':
159  default:
160  usage();
161  }
162 #if 0
163  argc -= (optind - 1);
164  argv += (optind - 1);
165 #endif
166 
168  if (!rb) {
169  fprintf(stderr, "Failed creating ring buffer\n");
170  fr_exit_now(EXIT_FAILURE);
171  }
172 
173  seed = 0xabcdef;
174  start = 0;
175  end = 0;
176 
177  /*
178  * Allocate the first set of blocks.
179  */
180  alloc_blocks(rb, &seed, &start, &end);
181 
182  /*
183  * Do 1000 rounds of alloc / free.
184  */
185  for (i = 0; i < length; i++) {
186  if (debug_lvl) printf("Loop %d (used %zu) \n", i, used);
187  alloc_blocks(rb, &seed, &start, &end);
188 
189  free_blocks(rb, &seed, &start, &end);
190  }
191 
192  free_blocks(rb, &seed, &start, &end);
193 
194  fr_assert(used == 0);
196 
197  fr_exit_now(EXIT_SUCCESS);
198 }
int const char * file
Definition: acutest.h:702
va_list args
Definition: acutest.h:770
int const char int line
Definition: acutest.h:702
#define RCSID(id)
Definition: build.h:444
#define NEVER_RETURNS
Should be placed before the function return type.
Definition: build.h:311
#define UNUSED
Definition: build.h:313
static fr_ring_buffer_t * rb
Definition: control_test.c:51
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition: debug.h:137
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition: debug.h:232
uint32_t fr_hash_update(void const *data, size_t size, uint32_t hash)
Definition: hash.c:840
unsigned int uint32_t
Definition: merged_model.c:33
unsigned char uint8_t
Definition: merged_model.c:30
static TALLOC_CTX * autofree
Definition: radclient-ng.c:104
rlm_rcode_t rcode
Last rcode returned by a module.
Definition: request.h:232
Optional arguments for initialising requests.
Definition: request.h:253
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_reserve(fr_ring_buffer_t *rb, size_t size)
Reserve room in the ring buffer.
Definition: ring_buffer.c:119
int fr_ring_buffer_free(fr_ring_buffer_t *rb, size_t size_to_free)
Mark data as free,.
Definition: ring_buffer.c:304
size_t fr_ring_buffer_used(fr_ring_buffer_t *rb)
Get the amount of data used in a ring buffer.
Definition: ring_buffer.c:437
uint8_t * fr_ring_buffer_alloc(fr_ring_buffer_t *rb, size_t size)
Mark data as allocated.
Definition: ring_buffer.c:196
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)
request_t * request_alloc(UNUSED TALLOC_CTX *ctx, UNUSED request_init_args_t const *args)
#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)
void request_verify(UNUSED char const *file, UNUSED int line, UNUSED request_t *request)
static int debug_lvl
static unsigned int hash(char const *username, unsigned int tablesize)
Definition: rlm_passwd.c:132
fr_assert(0)
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
Definition: talloc.h:51