pmalloc: support for 4K pages
[vpp.git] / src / vppinfra / test_pmalloc.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vppinfra/format.h>
17 #include <vppinfra/pmalloc.h>
18 #include <vppinfra/random.h>
19
20 typedef struct
21 {
22   uword baseva;
23   uword size;
24   uword *vas;
25   u32 nitems;
26   u32 item_size;
27   u32 align;
28   int max_numa;
29   u32 arena_pages;
30   u32 arena_numa;
31   u32 arena_items;
32   u32 arena_log2_pg_sz;
33   int verbose;
34   clib_pmalloc_main_t pmalloc_main;
35 } test_main_t;
36
37 test_main_t test_main;
38
39 clib_error_t *
40 test_palloc (test_main_t * tm)
41 {
42   clib_pmalloc_main_t *pm = &tm->pmalloc_main;
43   void *arena;
44   int i;
45   uword *va;
46
47   if (clib_pmalloc_init (pm, 0) != 0)
48     return clib_error_return (0, "pmalloc init failure");
49
50   fformat (stdout, "Allocate %d items...\n", tm->nitems);
51
52   for (i = 0; i < tm->nitems; i++)
53     {
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);
58
59       if (va == 0)
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));
63
64       if ((pointer_to_uword (va) & (align - 1)) != 0)
65         clib_error (0, "Alignment error: %p not aligned with %u", va, align);
66
67       vec_add1 (tm->vas, pointer_to_uword (va));
68     }
69   fformat (stdout, "%U\n", format_pmalloc, pm, tm->verbose);
70
71   /* alloc from arena */
72   if (tm->arena_items)
73     {
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,
77                                                 tm->arena_log2_pg_sz,
78                                                 tm->arena_numa);
79       if (arena == 0)
80         clib_error ("Failed to alloc shared arena: %U", format_clib_error,
81                     clib_pmalloc_last_error (pm));
82
83       for (i = 0; i < tm->arena_items; i++)
84         {
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));
89         }
90       fformat (stdout, "\n%U\n", format_pmalloc, pm, tm->verbose);
91     }
92
93
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]);
97
98   fformat (stdout, "\n%U\n", format_pmalloc, pm, tm->verbose);
99   return 0;
100 }
101
102 clib_error_t *
103 test_palloc_main (unformat_input_t * i)
104 {
105   test_main_t *tm = &test_main;
106   clib_error_t *error;
107
108   tm->nitems = 5;
109   tm->arena_pages = 2;
110   tm->arena_numa = CLIB_PMALLOC_NUMA_LOCAL;
111
112   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
113     {
114       if (unformat (i, "nitems %u", &tm->nitems))
115         ;
116       else if (unformat (i, "max-numa %u", &tm->max_numa))
117         ;
118       else if (unformat (i, "item-size %u", &tm->item_size))
119         ;
120       else if (unformat (i, "align %u", &tm->align))
121         ;
122       else if (unformat (i, "verbose %d", &tm->verbose))
123         ;
124       else if (unformat (i, "arena-pages %u", &tm->arena_pages))
125         ;
126       else if (unformat (i, "arena-numa %u", &tm->arena_numa))
127         ;
128       else if (unformat (i, "arena-items %u", &tm->arena_items))
129         ;
130       else if (unformat (i, "arena-log2-page-size %u", &tm->arena_log2_pg_sz))
131         ;
132       else if (unformat (i, "verbose"))
133         tm->verbose = 1;
134       else
135         return clib_error_return (0, "unknown input '%U'",
136                                   format_unformat_error, i);
137     }
138
139   error = test_palloc (tm);
140
141   return error;
142 }
143
144 #ifdef CLIB_UNIX
145 int
146 main (int argc, char *argv[])
147 {
148   unformat_input_t i;
149   int rv = 0;
150   clib_error_t *error;
151
152   clib_mem_init (0, 3ULL << 30);
153
154   unformat_init_command_line (&i, argv);
155   error = test_palloc_main (&i);
156   if (error)
157     {
158       clib_error_report (error);
159       rv = 1;
160     }
161   unformat_free (&i);
162
163   return rv;
164 }
165 #endif /* CLIB_UNIX */
166
167 /*
168  * fd.io coding-style-patch-verification: ON
169  *
170  * Local Variables:
171  * eval: (c-set-style "gnu")
172  * End:
173  */