c9ae01fc9365b4ecf3a666f9da81761b43e4cc11
[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   int verbose;
33   clib_pmalloc_main_t pmalloc_main;
34 } test_main_t;
35
36 test_main_t test_main;
37
38 clib_error_t *
39 test_palloc (test_main_t * tm)
40 {
41   clib_pmalloc_main_t *pm = &tm->pmalloc_main;
42   void *arena;
43   int i;
44   uword *va;
45
46   if (clib_pmalloc_init (pm, 0) != 0)
47     return clib_error_return (0, "pmalloc init failure");
48
49   fformat (stdout, "Allocate %d items...\n", tm->nitems);
50
51   for (i = 0; i < tm->nitems; i++)
52     {
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);
57
58       if (va == 0)
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));
62
63       if ((pointer_to_uword (va) & (align - 1)) != 0)
64         clib_error (0, "Alignment error: %p not aligned with %u", va, align);
65
66       vec_add1 (tm->vas, pointer_to_uword (va));
67     }
68   fformat (stdout, "%U\n", format_pmalloc, pm, tm->verbose);
69
70   /* alloc from arena */
71   if (tm->arena_items)
72     {
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,
76                                                 tm->arena_numa);
77       if (arena == 0)
78         clib_error ("Failed to alloc shared arena: %U", format_clib_error,
79                     clib_pmalloc_last_error (pm));
80
81       for (i = 0; i < tm->arena_items; i++)
82         {
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));
87         }
88       fformat (stdout, "\n%U\n", format_pmalloc, pm, tm->verbose);
89     }
90
91
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]);
95
96   fformat (stdout, "\n%U\n", format_pmalloc, pm, tm->verbose);
97   return 0;
98 }
99
100 clib_error_t *
101 test_palloc_main (unformat_input_t * i)
102 {
103   test_main_t *tm = &test_main;
104   clib_error_t *error;
105
106   tm->nitems = 5;
107   tm->arena_pages = 2;
108   tm->arena_numa = CLIB_PMALLOC_NUMA_LOCAL;
109
110   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
111     {
112       if (unformat (i, "nitems %u", &tm->nitems))
113         ;
114       else if (unformat (i, "max-numa %u", &tm->max_numa))
115         ;
116       else if (unformat (i, "item-size %u", &tm->item_size))
117         ;
118       else if (unformat (i, "align %u", &tm->align))
119         ;
120       else if (unformat (i, "verbose %d", &tm->verbose))
121         ;
122       else if (unformat (i, "arena-pages %u", &tm->arena_pages))
123         ;
124       else if (unformat (i, "arena-numa %u", &tm->arena_numa))
125         ;
126       else if (unformat (i, "arena-items %u", &tm->arena_items))
127         ;
128       else if (unformat (i, "verbose"))
129         tm->verbose = 1;
130       else
131         return clib_error_return (0, "unknown input '%U'",
132                                   format_unformat_error, i);
133     }
134
135   error = test_palloc (tm);
136
137   return error;
138 }
139
140 #ifdef CLIB_UNIX
141 int
142 main (int argc, char *argv[])
143 {
144   unformat_input_t i;
145   int rv = 0;
146   clib_error_t *error;
147
148   clib_mem_init (0, 3ULL << 30);
149
150   unformat_init_command_line (&i, argv);
151   error = test_palloc_main (&i);
152   if (error)
153     {
154       clib_error_report (error);
155       rv = 1;
156     }
157   unformat_free (&i);
158
159   return rv;
160 }
161 #endif /* CLIB_UNIX */
162
163 /*
164  * fd.io coding-style-patch-verification: ON
165  *
166  * Local Variables:
167  * eval: (c-set-style "gnu")
168  * End:
169  */