2 * Copyright (c) 2008, 2009, 2010, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* A non-exhaustive test for some of the functions and macros declared in
24 #include "command-line.h"
25 #include "fat-rwlock.h"
29 #include "ovs-thread.h"
37 /* Sample cmap element. */
40 struct cmap_node node;
43 typedef size_t hash_func(int value);
46 compare_ints(const void *a_, const void *b_)
50 return *a < *b ? -1 : *a > *b;
53 /* Verifies that 'cmap' contains exactly the 'n' values in 'values'. */
55 check_cmap(struct cmap *cmap, const int values[], size_t n,
58 int *sort_values, *cmap_values;
59 struct cmap_cursor cursor;
63 /* Check that all the values are there in iteration. */
64 sort_values = xmalloc(sizeof *sort_values * n);
65 cmap_values = xmalloc(sizeof *sort_values * n);
68 CMAP_FOR_EACH (e, node, &cursor, cmap) {
70 cmap_values[i++] = e->value;
74 memcpy(sort_values, values, sizeof *sort_values * n);
75 qsort(sort_values, n, sizeof *sort_values, compare_ints);
76 qsort(cmap_values, n, sizeof *cmap_values, compare_ints);
78 for (i = 0; i < n; i++) {
79 assert(sort_values[i] == cmap_values[i]);
85 /* Check that all the values are there in lookup. */
86 for (i = 0; i < n; i++) {
89 CMAP_FOR_EACH_WITH_HASH (e, node, hash(values[i]), cmap) {
90 count += e->value == values[i];
96 assert(cmap_is_empty(cmap) == !n);
97 assert(cmap_count(cmap) == n);
101 shuffle(int *p, size_t n)
103 for (; n > 1; n--, p++) {
104 int *q = &p[random_range(n)];
111 /* Prints the values in 'cmap', plus 'name' as a title. */
112 static void OVS_UNUSED
113 print_cmap(const char *name, struct cmap *cmap)
115 struct cmap_cursor cursor;
119 CMAP_FOR_EACH (e, node, &cursor, cmap) {
120 printf(" %d", e->value);
125 /* Prints the 'n' values in 'values', plus 'name' as a title. */
126 static void OVS_UNUSED
127 print_ints(const char *name, const int *values, size_t n)
132 for (i = 0; i < n; i++) {
133 printf(" %d", values[i]);
139 identity_hash(int value)
147 return hash_int(value, 0x1234abcd);
151 constant_hash(int value OVS_UNUSED)
156 /* Tests basic cmap insertion and deletion. */
158 test_cmap_insert_delete(hash_func *hash)
160 enum { N_ELEMS = 1000 };
162 struct element elements[N_ELEMS];
168 for (i = 0; i < N_ELEMS; i++) {
169 elements[i].value = i;
170 cmap_insert(&cmap, &elements[i].node, hash(i));
172 check_cmap(&cmap, values, i + 1, hash);
174 shuffle(values, N_ELEMS);
175 for (i = 0; i < N_ELEMS; i++) {
176 cmap_remove(&cmap, &elements[values[i]].node, hash(values[i]));
177 check_cmap(&cmap, values + (i + 1), N_ELEMS - (i + 1), hash);
183 run_test(void (*function)(hash_func *))
185 hash_func *hash_funcs[] = { identity_hash, good_hash, constant_hash };
188 for (i = 0; i < ARRAY_SIZE(hash_funcs); i++) {
189 function(hash_funcs[i]);
196 run_tests(int argc, char *argv[])
201 n = argc >= 2 ? atoi(argv[1]) : 100;
202 for (i = 0; i < n; i++) {
203 run_test(test_cmap_insert_delete);
208 static int n_elems; /* Number of elements to insert. */
209 static int n_threads; /* Number of threads to search and mutate. */
210 static uint32_t mutation_frac; /* % mutations, as fraction of UINT32_MAX. */
212 static void benchmark_cmap(void);
213 static void benchmark_hmap(void);
216 elapsed(const struct timeval *start)
221 return timeval_to_msec(&end) - timeval_to_msec(start);
225 run_benchmarks(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
227 n_elems = strtol(argv[1], NULL, 10);
228 n_threads = strtol(argv[2], NULL, 10);
229 mutation_frac = strtod(argv[3], NULL) / 100.0 * UINT32_MAX;
231 printf("Benchmarking with n=%d, %d threads, %.2f%% mutations:\n",
232 n_elems, n_threads, (double) mutation_frac / UINT32_MAX * 100.);
239 /* cmap benchmark. */
241 static struct element *
242 find(const struct cmap *cmap, int value)
246 CMAP_FOR_EACH_WITH_HASH (e, node, hash_int(value, 0), cmap) {
247 if (e->value == value) {
255 struct ovs_mutex mutex;
260 search_cmap(void *aux_)
262 struct cmap_aux *aux = aux_;
265 for (i = 0; i < n_elems; i++) {
268 if (random_uint32() < mutation_frac) {
269 ovs_mutex_lock(&aux->mutex);
270 e = find(aux->cmap, i);
272 cmap_remove(aux->cmap, &e->node, hash_int(e->value, 0));
274 ovs_mutex_unlock(&aux->mutex);
276 ignore(find(aux->cmap, i));
285 struct element *elements;
288 struct cmap_cursor cursor;
289 struct timeval start;
294 elements = xmalloc(n_elems * sizeof *elements);
297 xgettimeofday(&start);
299 for (i = 0; i < n_elems; i++) {
300 elements[i].value = i;
301 cmap_insert(&cmap, &elements[i].node, hash_int(i, 0));
303 printf("cmap insert: %5d ms\n", elapsed(&start));
306 xgettimeofday(&start);
307 CMAP_FOR_EACH (e, node, &cursor, &cmap) {
310 printf("cmap iterate: %5d ms\n", elapsed(&start));
312 /* Search and mutation. */
313 xgettimeofday(&start);
315 ovs_mutex_init(&aux.mutex);
316 threads = xmalloc(n_threads * sizeof *threads);
317 for (i = 0; i < n_threads; i++) {
318 threads[i] = ovs_thread_create("search", search_cmap, &aux);
320 for (i = 0; i < n_threads; i++) {
321 xpthread_join(threads[i], NULL);
324 printf("cmap search: %5d ms\n", elapsed(&start));
327 xgettimeofday(&start);
328 CMAP_FOR_EACH (e, node, &cursor, &cmap) {
329 cmap_remove(&cmap, &e->node, hash_int(e->value, 0));
332 printf("cmap destroy: %5d ms\n", elapsed(&start));
337 /* hmap benchmark. */
340 struct hmap_node node;
343 static struct helement *
344 hfind(const struct hmap *hmap, int value)
348 HMAP_FOR_EACH_WITH_HASH (e, node, hash_int(value, 0), hmap) {
349 if (e->value == value) {
358 struct fat_rwlock fatlock;
362 search_hmap(void *aux_)
364 struct hmap_aux *aux = aux_;
367 for (i = 0; i < n_elems; i++) {
369 if (random_uint32() < mutation_frac) {
372 fat_rwlock_wrlock(&aux->fatlock);
373 e = hfind(aux->hmap, i);
375 hmap_remove(aux->hmap, &e->node);
377 fat_rwlock_unlock(&aux->fatlock);
379 fat_rwlock_rdlock(&aux->fatlock);
380 ignore(hfind(aux->hmap, i));
381 fat_rwlock_unlock(&aux->fatlock);
393 struct helement *elements;
395 struct helement *e, *next;
396 struct timeval start;
401 elements = xmalloc(n_elems * sizeof *elements);
403 xgettimeofday(&start);
405 for (i = 0; i < n_elems; i++) {
406 elements[i].value = i;
407 hmap_insert(&hmap, &elements[i].node, hash_int(i, 0));
410 printf("hmap insert: %5d ms\n", elapsed(&start));
412 xgettimeofday(&start);
413 HMAP_FOR_EACH (e, node, &hmap) {
416 printf("hmap iterate: %5d ms\n", elapsed(&start));
418 xgettimeofday(&start);
420 fat_rwlock_init(&aux.fatlock);
421 threads = xmalloc(n_threads * sizeof *threads);
422 for (i = 0; i < n_threads; i++) {
423 threads[i] = ovs_thread_create("search", search_hmap, &aux);
425 for (i = 0; i < n_threads; i++) {
426 xpthread_join(threads[i], NULL);
429 printf("hmap search: %5d ms\n", elapsed(&start));
432 xgettimeofday(&start);
433 HMAP_FOR_EACH_SAFE (e, next, node, &hmap) {
434 hmap_remove(&hmap, &e->node);
437 printf("hmap destroy: %5d ms\n", elapsed(&start));
442 static const struct command commands[] = {
443 {"check", 0, 1, run_tests},
444 {"benchmark", 3, 3, run_benchmarks},
449 test_cmap_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
451 set_program_name(argv[0]);
452 run_command(argc - optind, argv + optind, commands);
455 OVSTEST_REGISTER("test-cmap", test_cmap_main);