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>
33 clib_pmalloc_main_t pmalloc_main;
36 test_main_t test_main;
39 test_palloc (test_main_t * tm)
41 clib_pmalloc_main_t *pm = &tm->pmalloc_main;
46 if (clib_pmalloc_init (pm, 0) != 0)
47 return clib_error_return (0, "pmalloc init failure");
49 fformat (stdout, "Allocate %d items...\n", tm->nitems);
51 for (i = 0; i < tm->nitems; i++)
53 u32 size = tm->item_size ? tm->item_size : 64 + 64 * (i % 8);
54 u32 align = tm->align ? tm->align : 64 << (i % 5);
55 u32 numa = i % (tm->max_numa + 1);
56 va = clib_pmalloc_alloc_aligned_on_numa (pm, size, align, numa);
59 clib_error ("Failed to alloc %u byte chunk with align %u on numa %u,"
60 "\nerror: %U", size, align, numa, format_clib_error,
61 clib_pmalloc_last_error (pm));
63 if ((pointer_to_uword (va) & (align - 1)) != 0)
64 clib_error (0, "Alignment error: %p not aligned with %u", va, align);
66 vec_add1 (tm->vas, pointer_to_uword (va));
68 fformat (stdout, "%U\n", format_pmalloc, pm, tm->verbose);
70 /* alloc from arena */
73 fformat (stdout, "Allocate %d items from arena ...\n", tm->arena_items);
74 arena = clib_pmalloc_create_shared_arena (pm, "test arena",
75 tm->arena_pages << 21,
78 clib_error ("Failed to alloc shared arena: %U", format_clib_error,
79 clib_pmalloc_last_error (pm));
81 for (i = 0; i < tm->arena_items; i++)
83 u32 size = tm->item_size ? tm->item_size : 64 + 64 * (i % 8);
84 u32 align = tm->align ? tm->align : 64 << (i % 5);
85 va = clib_pmalloc_alloc_from_arena (pm, arena, size, align);
86 vec_add1 (tm->vas, pointer_to_uword (va));
88 fformat (stdout, "\n%U\n", format_pmalloc, pm, tm->verbose);
92 fformat (stdout, "Freeing %d items ...\n", vec_len (tm->vas));
93 for (i = 0; i < vec_len (tm->vas); i++)
94 clib_pmalloc_free (pm, (void *) tm->vas[i]);
96 fformat (stdout, "\n%U\n", format_pmalloc, pm, tm->verbose);
101 test_palloc_main (unformat_input_t * i)
103 test_main_t *tm = &test_main;
108 tm->arena_numa = CLIB_PMALLOC_NUMA_LOCAL;
110 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
112 if (unformat (i, "nitems %u", &tm->nitems))
114 else if (unformat (i, "max-numa %u", &tm->max_numa))
116 else if (unformat (i, "item-size %u", &tm->item_size))
118 else if (unformat (i, "align %u", &tm->align))
120 else if (unformat (i, "verbose %d", &tm->verbose))
122 else if (unformat (i, "arena-pages %u", &tm->arena_pages))
124 else if (unformat (i, "arena-numa %u", &tm->arena_numa))
126 else if (unformat (i, "arena-items %u", &tm->arena_items))
128 else if (unformat (i, "verbose"))
131 return clib_error_return (0, "unknown input '%U'",
132 format_unformat_error, i);
135 error = test_palloc (tm);
142 main (int argc, char *argv[])
148 clib_mem_init (0, 3ULL << 30);
150 unformat_init_command_line (&i, argv);
151 error = test_palloc_main (&i);
154 clib_error_report (error);
161 #endif /* CLIB_UNIX */
164 * fd.io coding-style-patch-verification: ON
167 * eval: (c-set-style "gnu")