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: bab1e00e37c9a30de4b8f73cc7b1e2d91b9e4755 $
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: bab1e00e37c9a30de4b8f73cc7b1e2d91b9e4755 $")
24 
25 #include <freeradius-devel/io/ring_buffer.h>
26 #include <freeradius-devel/util/request.h>
27 #include <freeradius-devel/util/debug.h>
28 #include <freeradius-devel/util/hash.h>
29 #include <freeradius-devel/util/syserror.h>
30 #include <freeradius-devel/util/talloc.h>
31 #include <string.h>
32 
33 #ifdef HAVE_GETOPT_H
34 # include <getopt.h>
35 #endif
36 
37 #define ALLOC_SIZE (8)
38 #define ARRAY_SIZE (4 * ALLOC_SIZE)
39 
40 static size_t used = 0;
41 static size_t array[ARRAY_SIZE];
43 
44 static int debug_lvl = 0;
45 
46 static char const *seed_string = "foo";
47 static size_t seed_string_len = 3;
48 
49 /**********************************************************************/
50 typedef struct request_s request_t;
52 void request_verify(UNUSED char const *file, UNUSED int line, UNUSED request_t *request);
53 
55 {
56  return NULL;
57 }
58 
59 void request_verify(UNUSED char const *file, UNUSED int line, UNUSED request_t *request)
60 {
61 }
62 
63 /**********************************************************************/
64 
65 static void alloc_blocks(fr_ring_buffer_t *rb, uint32_t *seed, UNUSED int *start, int *end)
66 {
67  int i;
68  uint32_t hash;
69 
70  for (i = 0; i < ALLOC_SIZE; i++) {
71  int index;
72  uint8_t *p;
73 
74  index = (*end + i) & (ARRAY_SIZE - 1);
75 
77  *seed = hash;
78 
79  hash &= 0x3ff;
80  hash += 16; /* can't have it zero... */
81 
82  array[index] = hash;
83  p = fr_ring_buffer_reserve(rb, 2048);
84 
85  if (!fr_cond_assert(p != NULL)) fr_exit_now(EXIT_FAILURE);
86 
87  data[index] = fr_ring_buffer_alloc(rb, hash);
88  if (!fr_cond_assert(data[index] == p)) fr_exit_now(EXIT_FAILURE);
89 
90  if (debug_lvl > 1) printf("%08x\t", hash);
91 
92  used += hash;
94  }
95 
96  *end += ALLOC_SIZE;
97 }
98 
99 static void free_blocks(fr_ring_buffer_t *rb, UNUSED uint32_t *seed, int *start, int *end)
100 {
101  int i;
102 
103  for (i = 0; i < ALLOC_SIZE; i++) {
104  int index;
105  int rcode;
106 
107  index = (*start + i) & (ARRAY_SIZE - 1);
108 
109  rcode = fr_ring_buffer_free(rb, array[index]);
110  if (!fr_cond_assert(rcode == 0)) fr_exit_now(EXIT_FAILURE);
111 
112  used -= array[index];
114 
115  array[index] = 0;
116  data[index] = NULL;
117  }
118 
119  *start += ALLOC_SIZE;
120  if (*start > ARRAY_SIZE) {
121  *start -= ARRAY_SIZE;
122  *end -= ARRAY_SIZE;
123  }
124 }
125 
126 static NEVER_RETURNS void usage(void)
127 {
128  fprintf(stderr, "usage: ring_buffer_test [OPTS]\n");
129  fprintf(stderr, " -x Debugging mode.\n");
130  fprintf(stderr, " -s <string> Set random seed to <string>.\n");
131  fprintf(stderr, " -l <length> Set the iteration number to <length>.\n");
132 
133  fr_exit_now(EXIT_SUCCESS);
134 }
135 
136 int main(int argc, char *argv[])
137 {
138  int c;
139 
140  int i, start, end, length = 1000;
142  uint32_t seed;
143 
144  TALLOC_CTX *autofree = talloc_autofree_context();
145 
146  while ((c = getopt(argc, argv, "hl:s:x")) != -1) switch (c) {
147  case 'l':
148  length = strtol(optarg, NULL, 10);
149  break;
150  case 's':
151  seed_string = optarg;
152  seed_string_len = strlen(optarg);
153  break;
154 
155  case 'x':
156  debug_lvl++;
157  break;
158 
159  case 'h':
160  default:
161  usage();
162  }
163 #if 0
164  argc -= (optind - 1);
165  argv += (optind - 1);
166 #endif
167 
169  if (!rb) {
170  fprintf(stderr, "Failed creating ring buffer\n");
171  fr_exit_now(EXIT_FAILURE);
172  }
173 
174  seed = 0xabcdef;
175  start = 0;
176  end = 0;
177 
178  /*
179  * Allocate the first set of blocks.
180  */
181  alloc_blocks(rb, &seed, &start, &end);
182 
183  /*
184  * Do 1000 rounds of alloc / free.
185  */
186  for (i = 0; i < length; i++) {
187  if (debug_lvl) printf("Loop %d (used %zu) \n", i, used);
188  alloc_blocks(rb, &seed, &start, &end);
189 
190  free_blocks(rb, &seed, &start, &end);
191  }
192 
193  free_blocks(rb, &seed, &start, &end);
194 
195  fr_assert(used == 0);
197 
198  fr_exit_now(EXIT_SUCCESS);
199 }
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:481
#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:139
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition: debug.h:234
uint32_t fr_hash_update(void const *data, size_t size, uint32_t hash)
Definition: hash.c:846
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:107
rlm_rcode_t rcode
Last rcode returned by a module.
Definition: request.h:233
Optional arguments for initialising requests.
Definition: request.h:254
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