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/format.h>
17 #include <vppinfra/pmalloc.h>
18 #include <vppinfra/random.h>
34 clib_pmalloc_main_t pmalloc_main;
37 test_main_t test_main;
40 test_palloc (test_main_t * tm)
42 clib_pmalloc_main_t *pm = &tm->pmalloc_main;
47 if (clib_pmalloc_init (pm, 0, 0) != 0)
48 return clib_error_return (0, "pmalloc init failure");
50 fformat (stdout, "Allocate %d items...\n", tm->nitems);
52 for (i = 0; i < tm->nitems; i++)
54 u32 size = tm->item_size ? tm->item_size : 64 + 64 * (i % 8);
55 u32 align = tm->align ? tm->align : 64 << (i % 5);
56 u32 numa = i % (tm->max_numa + 1);
57 va = clib_pmalloc_alloc_aligned_on_numa (pm, size, align, numa);
60 clib_error ("Failed to alloc %u byte chunk with align %u on numa %u,"
61 "\nerror: %U", size, align, numa, format_clib_error,
62 clib_pmalloc_last_error (pm));
64 if ((pointer_to_uword (va) & (align - 1)) != 0)
65 clib_error (0, "Alignment error: %p not aligned with %u", va, align);
67 vec_add1 (tm->vas, pointer_to_uword (va));
69 fformat (stdout, "%U\n", format_pmalloc, pm, tm->verbose);
71 /* alloc from arena */
74 fformat (stdout, "Allocate %d items from arena ...\n", tm->arena_items);
75 arena = clib_pmalloc_create_shared_arena (pm, "test arena",
76 tm->arena_pages << 21,
80 clib_error ("Failed to alloc shared arena: %U", format_clib_error,
81 clib_pmalloc_last_error (pm));
83 for (i = 0; i < tm->arena_items; i++)
85 u32 size = tm->item_size ? tm->item_size : 64 + 64 * (i % 8);
86 u32 align = tm->align ? tm->align : 64 << (i % 5);
87 va = clib_pmalloc_alloc_from_arena (pm, arena, size, align);
88 vec_add1 (tm->vas, pointer_to_uword (va));
90 fformat (stdout, "\n%U\n", format_pmalloc, pm, tm->verbose);
94 fformat (stdout, "Freeing %d items ...\n", vec_len (tm->vas));
95 for (i = 0; i < vec_len (tm->vas); i++)
96 clib_pmalloc_free (pm, (void *) tm->vas[i]);
98 fformat (stdout, "\n%U\n", format_pmalloc, pm, tm->verbose);
103 test_palloc_main (unformat_input_t * i)
105 test_main_t *tm = &test_main;
110 tm->arena_numa = CLIB_PMALLOC_NUMA_LOCAL;
112 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
114 if (unformat (i, "nitems %u", &tm->nitems))
116 else if (unformat (i, "max-numa %u", &tm->max_numa))
118 else if (unformat (i, "item-size %u", &tm->item_size))
120 else if (unformat (i, "align %u", &tm->align))
122 else if (unformat (i, "verbose %d", &tm->verbose))
124 else if (unformat (i, "arena-pages %u", &tm->arena_pages))
126 else if (unformat (i, "arena-numa %u", &tm->arena_numa))
128 else if (unformat (i, "arena-items %u", &tm->arena_items))
130 else if (unformat (i, "arena-log2-page-size %u", &tm->arena_log2_pg_sz))
132 else if (unformat (i, "verbose"))
135 return clib_error_return (0, "unknown input '%U'",
136 format_unformat_error, i);
139 error = test_palloc (tm);
146 main (int argc, char *argv[])
152 clib_mem_init (0, 3ULL << 30);
154 unformat_init_command_line (&i, argv);
155 error = test_palloc_main (&i);
158 clib_error_report (error);
165 #endif /* CLIB_UNIX */
168 * fd.io coding-style-patch-verification: ON
171 * eval: (c-set-style "gnu")