2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vppinfra/valloc.h>
25 * GDB callable function: pe - call pool_elts - number of elements in a pool
30 return (pool_elts (v));
43 int check_every_add_del;
44 clib_valloc_main_t valloc_main;
48 test_main_t test_main;
51 test_valloc (test_main_t * tm)
53 clib_valloc_chunk_t _ip, *ip = &_ip;
57 u32 currently_in_table;
60 ip->baseva = 0x20000000;
63 clib_valloc_init (&tm->valloc_main, ip, 1 /* lock */ );
65 ip->baseva = 0x20000000 + 1024;
66 ip->size = 1024 * 1024 * 1024 - 1024;
67 clib_valloc_add_chunk (&tm->valloc_main, ip);
69 fformat (stdout, "Allocate %d items...\n", tm->nitems);
70 for (i = 0; i < tm->nitems; i++)
72 baseva = clib_valloc_alloc (&tm->valloc_main, 1024,
73 1 /* fail:os_out_of_memory */ );
74 vec_add1 (tm->basevas, baseva);
75 vec_add1 (tm->item_in_table, 1);
78 fformat (stdout, "Perform %d random add/delete operations...\n", tm->niter);
80 for (i = 0; i < tm->niter; i++)
82 index = random_u32 (&tm->seed) % tm->nitems;
83 /* Swap state of random entry */
84 if (tm->item_in_table[index])
87 fformat (stdout, "free [%d] %llx\n", index, tm->basevas[index]);
88 clib_valloc_free (&tm->valloc_main, tm->basevas[index]);
89 tm->item_in_table[index] = 0;
90 tm->basevas[index] = ~0;
94 baseva = clib_valloc_alloc (&tm->valloc_main, 1024,
95 1 /* fail:os_out_of_memory */ );
96 tm->basevas[index] = baseva;
97 tm->item_in_table[index] = 1;
99 fformat (stdout, "alloc [%d] %llx\n", index, tm->basevas[index]);
102 /* Check our work... */
103 if (tm->check_every_add_del)
105 for (j = 0; j < tm->nitems; j++)
107 if (tm->item_in_table[j])
109 p = hash_get ((&tm->valloc_main)->chunk_index_by_baseva,
114 pool_elt_at_index ((&tm->valloc_main)->chunks, p[0]);
115 ASSERT (ip->baseva == tm->basevas[j]);
116 ASSERT (ip->flags & CLIB_VALLOC_BUSY);
121 p = hash_get ((&tm->valloc_main)->chunk_index_by_baseva,
123 /* Have to check, it's OK for the block to have been fused */
127 pool_elt_at_index ((&tm->valloc_main)->chunks, p[0]);
128 if ((ip->flags & CLIB_VALLOC_BUSY))
130 fformat (stdout, "BUG: baseva %llx chunk %d busy\n",
131 tm->basevas[j], p[0]);
132 fformat (stdout, "%U\n", format_valloc,
133 &tm->valloc_main, 1 /* verbose */ );
134 ASSERT ((ip->flags & CLIB_VALLOC_BUSY) == 0);
142 currently_in_table = 0;
144 for (i = 0; i < tm->nitems; i++)
146 currently_in_table += tm->item_in_table[i];
149 fformat (stdout, "Check that %d items in table can be found...\n",
154 for (i = 0; i < tm->nitems; i++)
156 if (tm->item_in_table[i])
158 p = hash_get ((&tm->valloc_main)->chunk_index_by_baseva,
162 ip = pool_elt_at_index ((&tm->valloc_main)->chunks, p[0]);
163 ASSERT (ip->baseva == tm->basevas[i]);
164 ASSERT (ip->flags & CLIB_VALLOC_BUSY);
170 p = hash_get ((&tm->valloc_main)->chunk_index_by_baseva,
172 /* Have to check, it's OK for the block to have been fused */
175 ip = pool_elt_at_index ((&tm->valloc_main)->chunks, p[0]);
176 if ((ip->flags & CLIB_VALLOC_BUSY))
178 fformat (stdout, "BUG: baseva %llx chunk %d busy\n",
179 tm->basevas[i], p[0]);
180 fformat (stdout, "%U\n", format_valloc,
181 &tm->valloc_main, 1 /* verbose */ );
182 ASSERT ((ip->flags & CLIB_VALLOC_BUSY) == 0);
188 fformat (stdout, "Found %d items in table...\n", found);
190 for (i = 0; i < tm->nitems; i++)
192 if (tm->item_in_table[i])
193 clib_valloc_free (&tm->valloc_main, tm->basevas[i]);
196 fformat (stdout, "%U", format_valloc, &tm->valloc_main, 1 /* verbose */ );
202 test_valloc_main (unformat_input_t * i)
204 test_main_t *tm = &test_main;
207 tm->seed = 0xdeaddabe;
210 tm->item_size = 1024;
212 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
214 if (unformat (i, "seed %u", &tm->seed))
216 else if (unformat (i, "nitems %u", &tm->nitems))
218 else if (unformat (i, "niter %u", &tm->niter))
220 else if (unformat (i, "item-size %u", &tm->item_size))
222 else if (unformat (i, "check-every-add-del"))
223 tm->check_every_add_del = 1;
224 else if (unformat (i, "verbose %d", &tm->verbose))
226 else if (unformat (i, "verbose"))
229 return clib_error_return (0, "unknown input '%U'",
230 format_unformat_error, i);
233 error = test_valloc (tm);
240 main (int argc, char *argv[])
246 clib_mem_init (0, 3ULL << 30);
248 unformat_init_command_line (&i, argv);
249 error = test_valloc_main (&i);
252 clib_error_report (error);
259 #endif /* CLIB_UNIX */
262 * fd.io coding-style-patch-verification: ON
265 * eval: (c-set-style "gnu")