Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation.
3 : : * Copyright(c) 2016 6WIND S.A.
4 : : * Copyright(c) 2022 SmartShare Systems
5 : : */
6 : :
7 : : #include <stdbool.h>
8 : : #include <stdlib.h>
9 : : #include <stdio.h>
10 : : #include <string.h>
11 : : #include <stdint.h>
12 : : #include <unistd.h>
13 : : #include <inttypes.h>
14 : : #include <errno.h>
15 : : #include <sys/queue.h>
16 : :
17 : : #include <eal_export.h>
18 : : #include <rte_common.h>
19 : : #include <rte_log.h>
20 : : #include <rte_debug.h>
21 : : #include <rte_memory.h>
22 : : #include <rte_memzone.h>
23 : : #include <rte_malloc.h>
24 : : #include <rte_eal.h>
25 : : #include <rte_eal_memconfig.h>
26 : : #include <rte_errno.h>
27 : : #include <rte_string_fns.h>
28 : : #include <rte_tailq.h>
29 : : #include <rte_eal_paging.h>
30 : : #include <rte_telemetry.h>
31 : :
32 : : #include "mempool_trace.h"
33 : : #include "rte_mempool.h"
34 : :
35 : : RTE_EXPORT_SYMBOL(rte_mempool_logtype)
36 [ - + ]: 252 : RTE_LOG_REGISTER_DEFAULT(rte_mempool_logtype, INFO);
37 : :
38 : : TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
39 : :
40 : : static struct rte_tailq_elem rte_mempool_tailq = {
41 : : .name = "RTE_MEMPOOL",
42 : : };
43 [ - + ]: 252 : EAL_REGISTER_TAILQ(rte_mempool_tailq)
44 : :
45 : : TAILQ_HEAD(mempool_callback_tailq, mempool_callback_data);
46 : :
47 : : static struct mempool_callback_tailq callback_tailq =
48 : : TAILQ_HEAD_INITIALIZER(callback_tailq);
49 : :
50 : : /* Invoke all registered mempool event callbacks. */
51 : : static void
52 : : mempool_event_callback_invoke(enum rte_mempool_event event,
53 : : struct rte_mempool *mp);
54 : :
55 : : /* Note: avoid using floating point since that compiler
56 : : * may not think that is constant.
57 : : */
58 : : #define CALC_CACHE_FLUSHTHRESH(c) (((c) * 3) / 2)
59 : :
60 : : #if defined(RTE_ARCH_X86)
61 : : /*
62 : : * return the greatest common divisor between a and b (fast algorithm)
63 : : */
64 : : static unsigned get_gcd(unsigned a, unsigned b)
65 : : {
66 : : unsigned c;
67 : :
68 : 203 : if (0 == a)
69 : : return b;
70 [ + - ]: 203 : if (0 == b)
71 : : return a;
72 : :
73 [ + + ]: 203 : if (a < b) {
74 : : c = a;
75 : : a = b;
76 : : b = c;
77 : : }
78 : :
79 [ + + ]: 537 : while (b != 0) {
80 : 334 : c = a % b;
81 : : a = b;
82 : : b = c;
83 : : }
84 : :
85 : : return a;
86 : : }
87 : :
88 : : /*
89 : : * Depending on memory configuration on x86 arch, objects addresses are spread
90 : : * between channels and ranks in RAM: the pool allocator will add
91 : : * padding between objects. This function return the new size of the
92 : : * object.
93 : : */
94 : : static unsigned int
95 : 121 : arch_mem_object_align(unsigned int obj_size)
96 : : {
97 : : unsigned nrank, nchan;
98 : : unsigned new_obj_size;
99 : :
100 : : /* get number of channels */
101 : 121 : nchan = rte_memory_get_nchannel();
102 [ + - ]: 121 : if (nchan == 0)
103 : : nchan = 4;
104 : :
105 : 121 : nrank = rte_memory_get_nrank();
106 : : if (nrank == 0)
107 : : nrank = 1;
108 : :
109 : : /* process new object size */
110 : 120 : new_obj_size = (obj_size + RTE_MEMPOOL_ALIGN_MASK) / RTE_MEMPOOL_ALIGN;
111 [ + - + + ]: 406 : while (get_gcd(new_obj_size, nrank * nchan) != 1)
112 : 83 : new_obj_size++;
113 : 120 : return new_obj_size * RTE_MEMPOOL_ALIGN;
114 : : }
115 : : #else
116 : : static unsigned int
117 : : arch_mem_object_align(unsigned int obj_size)
118 : : {
119 : : return obj_size;
120 : : }
121 : : #endif
122 : :
123 : : struct pagesz_walk_arg {
124 : : int socket_id;
125 : : size_t min;
126 : : };
127 : :
128 : : static int
129 : 9768 : find_min_pagesz(const struct rte_memseg_list *msl, void *arg)
130 : : {
131 : : struct pagesz_walk_arg *wa = arg;
132 : : bool valid;
133 : :
134 : : /*
135 : : * we need to only look at page sizes available for a particular socket
136 : : * ID. so, we either need an exact match on socket ID (can match both
137 : : * native and external memory), or, if SOCKET_ID_ANY was specified as a
138 : : * socket ID argument, we must only look at native memory and ignore any
139 : : * page sizes associated with external memory.
140 : : */
141 : 9768 : valid = msl->socket_id == wa->socket_id;
142 [ + + - + ]: 9768 : valid |= wa->socket_id == SOCKET_ID_ANY && msl->external == 0;
143 : :
144 [ + + + + ]: 9768 : if (valid && msl->page_sz < wa->min)
145 : 1221 : wa->min = msl->page_sz;
146 : :
147 : 9768 : return 0;
148 : : }
149 : :
150 : : static size_t
151 : 1221 : get_min_page_size(int socket_id)
152 : : {
153 : : struct pagesz_walk_arg wa;
154 : :
155 : 1221 : wa.min = SIZE_MAX;
156 : 1221 : wa.socket_id = socket_id;
157 : :
158 : 1221 : rte_memseg_list_walk(find_min_pagesz, &wa);
159 : :
160 [ - + ]: 1221 : return wa.min == SIZE_MAX ? (size_t) rte_mem_page_size() : wa.min;
161 : : }
162 : :
163 : :
164 : : static void
165 : 44670441 : mempool_add_elem(struct rte_mempool *mp, __rte_unused void *opaque,
166 : : void *obj, rte_iova_t iova)
167 : : {
168 : : struct rte_mempool_objhdr *hdr;
169 : :
170 : : /* set mempool ptr in header */
171 : 44670441 : hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
172 : 44670441 : hdr->mp = mp;
173 : 44670441 : hdr->iova = iova;
174 : 44670441 : STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
175 : 44670441 : mp->populated_size++;
176 : :
177 : : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
178 : : hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
179 : : rte_mempool_get_trailer(obj)->cookie = RTE_MEMPOOL_TRAILER_COOKIE;
180 : : #endif
181 : 44670441 : }
182 : :
183 : : /* call obj_cb() for each mempool element */
184 : : RTE_EXPORT_SYMBOL(rte_mempool_obj_iter)
185 : : uint32_t
186 : 67 : rte_mempool_obj_iter(struct rte_mempool *mp,
187 : : rte_mempool_obj_cb_t *obj_cb, void *obj_cb_arg)
188 : : {
189 : : struct rte_mempool_objhdr *hdr;
190 : : void *obj;
191 : : unsigned n = 0;
192 : :
193 [ + + ]: 101516 : STAILQ_FOREACH(hdr, &mp->elt_list, next) {
194 : 101449 : obj = (char *)hdr + sizeof(*hdr);
195 : 101449 : obj_cb(mp, obj_cb_arg, obj, n);
196 : 101449 : n++;
197 : : }
198 : :
199 : 67 : return n;
200 : : }
201 : :
202 : : /* call mem_cb() for each mempool memory chunk */
203 : : RTE_EXPORT_SYMBOL(rte_mempool_mem_iter)
204 : : uint32_t
205 : 1 : rte_mempool_mem_iter(struct rte_mempool *mp,
206 : : rte_mempool_mem_cb_t *mem_cb, void *mem_cb_arg)
207 : : {
208 : : struct rte_mempool_memhdr *hdr;
209 : : unsigned n = 0;
210 : :
211 [ + + ]: 3 : STAILQ_FOREACH(hdr, &mp->mem_list, next) {
212 : 2 : mem_cb(mp, mem_cb_arg, hdr, n);
213 : 2 : n++;
214 : : }
215 : :
216 : 1 : return n;
217 : : }
218 : :
219 : : /* get the header, trailer and total size of a mempool element. */
220 : : RTE_EXPORT_SYMBOL(rte_mempool_calc_obj_size)
221 : : uint32_t
222 : 123 : rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
223 : : struct rte_mempool_objsz *sz)
224 : : {
225 : : struct rte_mempool_objsz lsz;
226 : :
227 [ + - ]: 123 : sz = (sz != NULL) ? sz : &lsz;
228 : :
229 : 123 : sz->header_size = sizeof(struct rte_mempool_objhdr);
230 [ + + ]: 123 : if ((flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN) == 0)
231 : 121 : sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
232 : : RTE_MEMPOOL_ALIGN);
233 : :
234 : : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
235 : : sz->trailer_size = sizeof(struct rte_mempool_objtlr);
236 : : #else
237 : 123 : sz->trailer_size = 0;
238 : : #endif
239 : :
240 : : /* element size is 8 bytes-aligned at least */
241 : 123 : sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
242 : :
243 : : /* expand trailer to next cache line */
244 [ + + ]: 123 : if ((flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
245 : 121 : sz->total_size = sz->header_size + sz->elt_size +
246 : : sz->trailer_size;
247 : 121 : sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
248 : 121 : (sz->total_size & RTE_MEMPOOL_ALIGN_MASK)) &
249 : : RTE_MEMPOOL_ALIGN_MASK);
250 : : }
251 : :
252 : : /*
253 : : * increase trailer to add padding between objects in order to
254 : : * spread them across memory channels/ranks
255 : : */
256 [ + + ]: 123 : if ((flags & RTE_MEMPOOL_F_NO_SPREAD) == 0) {
257 : : unsigned new_size;
258 : 121 : new_size = arch_mem_object_align
259 : 121 : (sz->header_size + sz->elt_size + sz->trailer_size);
260 : 120 : sz->trailer_size = new_size - sz->header_size - sz->elt_size;
261 : : }
262 : :
263 : : /* this is the size of an object, including header and trailer */
264 : 122 : sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
265 : :
266 : 122 : return sz->total_size;
267 : : }
268 : :
269 : : /* free a memchunk allocated with rte_memzone_reserve() */
270 : : static void
271 : 100 : rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
272 : : void *opaque)
273 : : {
274 : : const struct rte_memzone *mz = opaque;
275 : 100 : rte_memzone_free(mz);
276 : 100 : }
277 : :
278 : : /* Free memory chunks used by a mempool. Objects must be in pool */
279 : : static void
280 : 105 : rte_mempool_free_memchunks(struct rte_mempool *mp)
281 : : {
282 : : struct rte_mempool_memhdr *memhdr;
283 : : void *elt;
284 : :
285 [ + + ]: 44663337 : while (!STAILQ_EMPTY(&mp->elt_list)) {
286 : 44663232 : rte_mempool_ops_dequeue_bulk(mp, &elt, 1);
287 : : (void)elt;
288 [ + + ]: 44663232 : STAILQ_REMOVE_HEAD(&mp->elt_list, next);
289 : 44663232 : mp->populated_size--;
290 : : }
291 : :
292 [ + + ]: 1297 : while (!STAILQ_EMPTY(&mp->mem_list)) {
293 : : memhdr = STAILQ_FIRST(&mp->mem_list);
294 [ + + ]: 1192 : STAILQ_REMOVE_HEAD(&mp->mem_list, next);
295 [ + + ]: 1192 : if (memhdr->free_cb != NULL)
296 : 103 : memhdr->free_cb(memhdr, memhdr->opaque);
297 : 1192 : rte_free(memhdr);
298 : 1192 : mp->nb_mem_chunks--;
299 : : }
300 : 105 : }
301 : :
302 : : static int
303 : 1307 : mempool_ops_alloc_once(struct rte_mempool *mp)
304 : : {
305 : : int ret;
306 : :
307 : : /* create the internal ring if not already done */
308 [ + + ]: 1307 : if ((mp->flags & RTE_MEMPOOL_F_POOL_CREATED) == 0) {
309 : 110 : ret = rte_mempool_ops_alloc(mp);
310 [ + - ]: 110 : if (ret != 0)
311 : : return ret;
312 : 110 : mp->flags |= RTE_MEMPOOL_F_POOL_CREATED;
313 : : }
314 : : return 0;
315 : : }
316 : :
317 : : /* Add objects in the pool, using a physically contiguous memory
318 : : * zone. Return the number of objects added, or a negative value
319 : : * on error.
320 : : */
321 : : RTE_EXPORT_SYMBOL(rte_mempool_populate_iova)
322 : : int
323 : 1199 : rte_mempool_populate_iova(struct rte_mempool *mp, char *vaddr,
324 : : rte_iova_t iova, size_t len, rte_mempool_memchunk_free_cb_t *free_cb,
325 : : void *opaque)
326 : : {
327 : : unsigned i = 0;
328 : : size_t off;
329 : : struct rte_mempool_memhdr *memhdr;
330 : : int ret;
331 : :
332 : 1199 : ret = mempool_ops_alloc_once(mp);
333 [ + - ]: 1199 : if (ret != 0)
334 : : return ret;
335 : :
336 : : /* mempool is already populated */
337 [ + - ]: 1199 : if (mp->populated_size >= mp->size)
338 : : return -ENOSPC;
339 : :
340 : 1199 : memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
341 [ + - ]: 1199 : if (memhdr == NULL)
342 : : return -ENOMEM;
343 : :
344 : 1199 : memhdr->mp = mp;
345 : 1199 : memhdr->addr = vaddr;
346 : 1199 : memhdr->iova = iova;
347 : 1199 : memhdr->len = len;
348 : 1199 : memhdr->free_cb = free_cb;
349 : 1199 : memhdr->opaque = opaque;
350 : :
351 [ + + ]: 1199 : if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
352 : 3 : off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
353 : : else
354 : 1196 : off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN) - vaddr;
355 : :
356 [ - + ]: 1199 : if (off > len) {
357 : : ret = 0;
358 : 0 : goto fail;
359 : : }
360 : :
361 [ + + ]: 1199 : i = rte_mempool_ops_populate(mp, mp->size - mp->populated_size,
362 : 1199 : (char *)vaddr + off,
363 : : (iova == RTE_BAD_IOVA) ? RTE_BAD_IOVA : (iova + off),
364 : : len - off, mempool_add_elem, NULL);
365 : :
366 : : /* not enough room to store one object */
367 [ - + ]: 1199 : if (i == 0) {
368 : : ret = 0;
369 : 0 : goto fail;
370 : : }
371 : :
372 : 1199 : STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
373 : 1199 : mp->nb_mem_chunks++;
374 : :
375 : : /* Check if at least some objects in the pool are now usable for IO. */
376 [ + + + + ]: 1199 : if (!(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG) && iova != RTE_BAD_IOVA)
377 : 1196 : mp->flags &= ~RTE_MEMPOOL_F_NON_IO;
378 : :
379 : : /* Report the mempool as ready only when fully populated. */
380 [ + + ]: 1199 : if (mp->populated_size >= mp->size)
381 : 108 : mempool_event_callback_invoke(RTE_MEMPOOL_EVENT_READY, mp);
382 : :
383 : 1199 : rte_mempool_trace_populate_iova(mp, vaddr, iova, len, free_cb, opaque);
384 : 1199 : return i;
385 : :
386 : 0 : fail:
387 : 0 : rte_free(memhdr);
388 : 0 : return ret;
389 : : }
390 : :
391 : : static rte_iova_t
392 : 2193320 : get_iova(void *addr)
393 : : {
394 : : struct rte_memseg *ms;
395 : :
396 : : /* try registered memory first */
397 : 2193320 : ms = rte_mem_virt2memseg(addr, NULL);
398 [ + + - + ]: 2193320 : if (ms == NULL || ms->iova == RTE_BAD_IOVA)
399 : : /* fall back to actual physical address */
400 : 2124 : return rte_mem_virt2iova(addr);
401 : 2191196 : return ms->iova + RTE_PTR_DIFF(addr, ms->addr);
402 : : }
403 : :
404 : : /* Populate the mempool with a virtual area. Return the number of
405 : : * objects added, or a negative value on error.
406 : : */
407 : : RTE_EXPORT_SYMBOL(rte_mempool_populate_virt)
408 : : int
409 : 108 : rte_mempool_populate_virt(struct rte_mempool *mp, char *addr,
410 : : size_t len, size_t pg_sz, rte_mempool_memchunk_free_cb_t *free_cb,
411 : : void *opaque)
412 : : {
413 : : rte_iova_t iova;
414 : : size_t off, phys_len;
415 : : int ret, cnt = 0;
416 : :
417 [ - + ]: 108 : if (mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG)
418 : 0 : return rte_mempool_populate_iova(mp, addr, RTE_BAD_IOVA,
419 : : len, free_cb, opaque);
420 : :
421 [ + + ]: 1303 : for (off = 0; off < len &&
422 [ + + ]: 1196 : mp->populated_size < mp->size; off += phys_len) {
423 : :
424 : 1195 : iova = get_iova(addr + off);
425 : :
426 : : /* populate with the largest group of contiguous pages */
427 : 1195 : for (phys_len = RTE_MIN(
428 : : (size_t)(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz) -
429 : : (addr + off)),
430 : : len - off);
431 [ + + ]: 2192232 : off + phys_len < len;
432 : 2191037 : phys_len = RTE_MIN(phys_len + pg_sz, len - off)) {
433 : : rte_iova_t iova_tmp;
434 : :
435 : 2192125 : iova_tmp = get_iova(addr + off + phys_len);
436 : :
437 [ + - ]: 2192125 : if (iova_tmp == RTE_BAD_IOVA ||
438 [ + + ]: 2192125 : iova_tmp != iova + phys_len)
439 : : break;
440 : : }
441 : :
442 : 1195 : ret = rte_mempool_populate_iova(mp, addr + off, iova,
443 : : phys_len, free_cb, opaque);
444 [ - + ]: 1195 : if (ret == 0)
445 : 0 : continue;
446 [ - + ]: 1195 : if (ret < 0)
447 : 0 : goto fail;
448 : : /* no need to call the free callback for next chunks */
449 : : free_cb = NULL;
450 : 1195 : cnt += ret;
451 : : }
452 : :
453 : 108 : rte_mempool_trace_populate_virt(mp, addr, len, pg_sz, free_cb, opaque);
454 : 108 : return cnt;
455 : :
456 : : fail:
457 : 0 : rte_mempool_free_memchunks(mp);
458 : 0 : return ret;
459 : : }
460 : :
461 : : /* Get the minimal page size used in a mempool before populating it. */
462 : : RTE_EXPORT_SYMBOL(rte_mempool_get_page_size)
463 : : int
464 : 1304 : rte_mempool_get_page_size(struct rte_mempool *mp, size_t *pg_sz)
465 : : {
466 : : bool need_iova_contig_obj;
467 : : bool alloc_in_ext_mem;
468 : : int ret;
469 : :
470 : : /* check if we can retrieve a valid socket ID */
471 : 1304 : ret = rte_malloc_heap_socket_is_external(mp->socket_id);
472 [ + - ]: 1304 : if (ret < 0)
473 : : return -EINVAL;
474 : : alloc_in_ext_mem = (ret == 1);
475 : 1304 : need_iova_contig_obj = !(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG);
476 : :
477 [ + + ]: 1304 : if (!need_iova_contig_obj)
478 : 1 : *pg_sz = 0;
479 [ + + - + ]: 1303 : else if (rte_eal_has_hugepages() || alloc_in_ext_mem)
480 : 1221 : *pg_sz = get_min_page_size(mp->socket_id);
481 : : else
482 : 82 : *pg_sz = rte_mem_page_size();
483 : :
484 [ - + ]: 1304 : rte_mempool_trace_get_page_size(mp, *pg_sz);
485 : 1304 : return 0;
486 : : }
487 : :
488 : : /* Default function to populate the mempool: allocate memory in memzones,
489 : : * and populate them. Return the number of objects added, or a negative
490 : : * value on error.
491 : : */
492 : : RTE_EXPORT_SYMBOL(rte_mempool_populate_default)
493 : : int
494 : 105 : rte_mempool_populate_default(struct rte_mempool *mp)
495 : : {
496 : : unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
497 : : char mz_name[RTE_MEMZONE_NAMESIZE];
498 : : const struct rte_memzone *mz;
499 : : ssize_t mem_size;
500 : : size_t align, pg_sz, pg_shift = 0;
501 : : rte_iova_t iova;
502 : : unsigned mz_id, n;
503 : : int ret;
504 : : bool need_iova_contig_obj;
505 : : size_t max_alloc_size = SIZE_MAX;
506 : :
507 : 105 : ret = mempool_ops_alloc_once(mp);
508 [ + - ]: 105 : if (ret != 0)
509 : : return ret;
510 : :
511 : : /* mempool must not be populated */
512 [ + - ]: 105 : if (mp->nb_mem_chunks != 0)
513 : : return -EEXIST;
514 : :
515 : : /*
516 : : * the following section calculates page shift and page size values.
517 : : *
518 : : * these values impact the result of calc_mem_size operation, which
519 : : * returns the amount of memory that should be allocated to store the
520 : : * desired number of objects. when not zero, it allocates more memory
521 : : * for the padding between objects, to ensure that an object does not
522 : : * cross a page boundary. in other words, page size/shift are to be set
523 : : * to zero if mempool elements won't care about page boundaries.
524 : : * there are several considerations for page size and page shift here.
525 : : *
526 : : * if we don't need our mempools to have physically contiguous objects,
527 : : * then just set page shift and page size to 0, because the user has
528 : : * indicated that there's no need to care about anything.
529 : : *
530 : : * if we do need contiguous objects (if a mempool driver has its
531 : : * own calc_size() method returning min_chunk_size = mem_size),
532 : : * there is also an option to reserve the entire mempool memory
533 : : * as one contiguous block of memory.
534 : : *
535 : : * if we require contiguous objects, but not necessarily the entire
536 : : * mempool reserved space to be contiguous, pg_sz will be != 0,
537 : : * and the default ops->populate() will take care of not placing
538 : : * objects across pages.
539 : : *
540 : : * if our IO addresses are physical, we may get memory from bigger
541 : : * pages, or we might get memory from smaller pages, and how much of it
542 : : * we require depends on whether we want bigger or smaller pages.
543 : : * However, requesting each and every memory size is too much work, so
544 : : * what we'll do instead is walk through the page sizes available, pick
545 : : * the smallest one and set up page shift to match that one. We will be
546 : : * wasting some space this way, but it's much nicer than looping around
547 : : * trying to reserve each and every page size.
548 : : *
549 : : * If we fail to get enough contiguous memory, then we'll go and
550 : : * reserve space in smaller chunks.
551 : : */
552 : :
553 : 105 : need_iova_contig_obj = !(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG);
554 : 105 : ret = rte_mempool_get_page_size(mp, &pg_sz);
555 [ + - ]: 105 : if (ret < 0)
556 : : return ret;
557 : :
558 [ + - ]: 105 : if (pg_sz != 0)
559 : 105 : pg_shift = rte_bsf32(pg_sz);
560 : :
561 [ + + ]: 210 : for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
562 : : size_t min_chunk_size;
563 : :
564 : 105 : mem_size = rte_mempool_ops_calc_mem_size(
565 : : mp, n, pg_shift, &min_chunk_size, &align);
566 : :
567 [ - + ]: 105 : if (mem_size < 0) {
568 : 0 : ret = mem_size;
569 : 0 : goto fail;
570 : : }
571 : :
572 : : ret = snprintf(mz_name, sizeof(mz_name),
573 [ - + ]: 105 : RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
574 [ - + ]: 105 : if (ret < 0 || ret >= (int)sizeof(mz_name)) {
575 : : ret = -ENAMETOOLONG;
576 : 0 : goto fail;
577 : : }
578 : :
579 : : /* if we're trying to reserve contiguous memory, add appropriate
580 : : * memzone flag.
581 : : */
582 [ - + ]: 105 : if (min_chunk_size == (size_t)mem_size)
583 : : mz_flags |= RTE_MEMZONE_IOVA_CONTIG;
584 : :
585 : : /* Allocate a memzone, retrying with a smaller area on ENOMEM */
586 : : do {
587 : 105 : mz = rte_memzone_reserve_aligned(mz_name,
588 : 105 : RTE_MIN((size_t)mem_size, max_alloc_size),
589 : : mp->socket_id, mz_flags, align);
590 : :
591 [ - + - - ]: 105 : if (mz != NULL || rte_errno != ENOMEM)
592 : : break;
593 : :
594 : 0 : max_alloc_size = RTE_MIN(max_alloc_size,
595 : : (size_t)mem_size) / 2;
596 [ # # ]: 0 : } while (mz == NULL && max_alloc_size >= min_chunk_size);
597 : :
598 [ - + ]: 105 : if (mz == NULL) {
599 : 0 : ret = -rte_errno;
600 : 0 : goto fail;
601 : : }
602 : :
603 [ + - ]: 105 : if (need_iova_contig_obj)
604 : 105 : iova = mz->iova;
605 : : else
606 : : iova = RTE_BAD_IOVA;
607 : :
608 [ + - - + ]: 105 : if (pg_sz == 0 || (mz_flags & RTE_MEMZONE_IOVA_CONTIG))
609 : 0 : ret = rte_mempool_populate_iova(mp, mz->addr,
610 : 0 : iova, mz->len,
611 : : rte_mempool_memchunk_mz_free,
612 : : (void *)(uintptr_t)mz);
613 : : else
614 : 105 : ret = rte_mempool_populate_virt(mp, mz->addr,
615 : 105 : mz->len, pg_sz,
616 : : rte_mempool_memchunk_mz_free,
617 : : (void *)(uintptr_t)mz);
618 [ + - ]: 105 : if (ret == 0) /* should not happen */
619 : : ret = -ENOBUFS;
620 [ - + ]: 105 : if (ret < 0) {
621 : 0 : rte_memzone_free(mz);
622 : 0 : goto fail;
623 : : }
624 : : }
625 : :
626 : 105 : rte_mempool_trace_populate_default(mp);
627 : 105 : return mp->size;
628 : :
629 : : fail:
630 : 0 : rte_mempool_free_memchunks(mp);
631 : 0 : return ret;
632 : : }
633 : :
634 : : /* return the memory size required for mempool objects in anonymous mem */
635 : : static ssize_t
636 : 6 : get_anon_size(const struct rte_mempool *mp)
637 : : {
638 : : ssize_t size;
639 : : size_t pg_sz, pg_shift;
640 : : size_t min_chunk_size;
641 : : size_t align;
642 : :
643 : 6 : pg_sz = rte_mem_page_size();
644 : 6 : pg_shift = rte_bsf32(pg_sz);
645 : 6 : size = rte_mempool_ops_calc_mem_size(mp, mp->size, pg_shift,
646 : : &min_chunk_size, &align);
647 : :
648 : 6 : return size;
649 : : }
650 : :
651 : : /* unmap a memory zone mapped by rte_mempool_populate_anon() */
652 : : static void
653 : 3 : rte_mempool_memchunk_anon_free(struct rte_mempool_memhdr *memhdr,
654 : : void *opaque)
655 : : {
656 : : ssize_t size;
657 : :
658 : : /*
659 : : * Calculate size since memhdr->len has contiguous chunk length
660 : : * which may be smaller if anon map is split into many contiguous
661 : : * chunks. Result must be the same as we calculated on populate.
662 : : */
663 : 3 : size = get_anon_size(memhdr->mp);
664 [ + - ]: 3 : if (size < 0)
665 : : return;
666 : :
667 : 3 : rte_mem_unmap(opaque, size);
668 : : }
669 : :
670 : : /* populate the mempool with an anonymous mapping */
671 : : RTE_EXPORT_SYMBOL(rte_mempool_populate_anon)
672 : : int
673 : 4 : rte_mempool_populate_anon(struct rte_mempool *mp)
674 : : {
675 : : ssize_t size;
676 : : int ret;
677 : : char *addr;
678 : :
679 : : /* mempool is already populated, error */
680 [ + + - + ]: 4 : if ((!STAILQ_EMPTY(&mp->mem_list)) || mp->nb_mem_chunks != 0) {
681 : 1 : rte_errno = EINVAL;
682 : 1 : return 0;
683 : : }
684 : :
685 : 3 : ret = mempool_ops_alloc_once(mp);
686 [ - + ]: 3 : if (ret < 0) {
687 : 0 : rte_errno = -ret;
688 : 0 : return 0;
689 : : }
690 : :
691 : 3 : size = get_anon_size(mp);
692 [ - + ]: 3 : if (size < 0) {
693 : 0 : rte_errno = -size;
694 : 0 : return 0;
695 : : }
696 : :
697 : : /* get chunk of virtually continuous memory */
698 : 3 : addr = rte_mem_map(NULL, size, RTE_PROT_READ | RTE_PROT_WRITE,
699 : : RTE_MAP_SHARED | RTE_MAP_ANONYMOUS, -1, 0);
700 [ + - ]: 3 : if (addr == NULL)
701 : : return 0;
702 : : /* can't use MMAP_LOCKED, it does not exist on BSD */
703 [ - + ]: 3 : if (rte_mem_lock(addr, size) < 0) {
704 : 0 : rte_mem_unmap(addr, size);
705 : 0 : return 0;
706 : : }
707 : :
708 : 3 : ret = rte_mempool_populate_virt(mp, addr, size, rte_mem_page_size(),
709 : : rte_mempool_memchunk_anon_free, addr);
710 [ + - ]: 3 : if (ret == 0) /* should not happen */
711 : : ret = -ENOBUFS;
712 [ - + ]: 3 : if (ret < 0) {
713 : 0 : rte_errno = -ret;
714 : 0 : goto fail;
715 : : }
716 : :
717 : 3 : rte_mempool_trace_populate_anon(mp);
718 : 3 : return mp->populated_size;
719 : :
720 : : fail:
721 : 0 : rte_mempool_free_memchunks(mp);
722 : 0 : return 0;
723 : : }
724 : :
725 : : /* free a mempool */
726 : : RTE_EXPORT_SYMBOL(rte_mempool_free)
727 : : void
728 : 122 : rte_mempool_free(struct rte_mempool *mp)
729 : : {
730 : : struct rte_mempool_list *mempool_list = NULL;
731 : : struct rte_tailq_entry *te;
732 : :
733 [ + + ]: 122 : if (mp == NULL)
734 : : return;
735 : :
736 : 105 : mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
737 : 105 : rte_mcfg_tailq_write_lock();
738 : : /* find out tailq entry */
739 [ + - ]: 300 : TAILQ_FOREACH(te, mempool_list, next) {
740 [ + + ]: 300 : if (te->data == (void *)mp)
741 : : break;
742 : : }
743 : :
744 [ + - ]: 105 : if (te != NULL) {
745 [ + + ]: 105 : TAILQ_REMOVE(mempool_list, te, next);
746 : 105 : rte_free(te);
747 : : }
748 : 105 : rte_mcfg_tailq_write_unlock();
749 : :
750 : 105 : mempool_event_callback_invoke(RTE_MEMPOOL_EVENT_DESTROY, mp);
751 : 105 : rte_mempool_trace_free(mp);
752 : 105 : rte_mempool_free_memchunks(mp);
753 : 105 : rte_mempool_ops_free(mp);
754 : 105 : rte_memzone_free(mp->mz);
755 : : }
756 : :
757 : : static void
758 : : mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)
759 : : {
760 : : /* Check that cache have enough space for flush threshold */
761 : : RTE_BUILD_BUG_ON(CALC_CACHE_FLUSHTHRESH(RTE_MEMPOOL_CACHE_MAX_SIZE) >
762 : : RTE_SIZEOF_FIELD(struct rte_mempool_cache, objs) /
763 : : RTE_SIZEOF_FIELD(struct rte_mempool_cache, objs[0]));
764 : :
765 : 2947 : cache->size = size;
766 : 2947 : cache->flushthresh = CALC_CACHE_FLUSHTHRESH(size);
767 [ - + ]: 3 : cache->len = 0;
768 : : }
769 : :
770 : : /*
771 : : * Create and initialize a cache for objects that are retrieved from and
772 : : * returned to an underlying mempool. This structure is identical to the
773 : : * local_cache[lcore_id] pointed to by the mempool structure.
774 : : */
775 : : RTE_EXPORT_SYMBOL(rte_mempool_cache_create)
776 : : struct rte_mempool_cache *
777 : 3 : rte_mempool_cache_create(uint32_t size, int socket_id)
778 : : {
779 : : struct rte_mempool_cache *cache;
780 : :
781 [ - + ]: 3 : if (size == 0 || size > RTE_MEMPOOL_CACHE_MAX_SIZE) {
782 : 0 : rte_errno = EINVAL;
783 : 0 : return NULL;
784 : : }
785 : :
786 : 3 : cache = rte_zmalloc_socket("MEMPOOL_CACHE", sizeof(*cache),
787 : : RTE_CACHE_LINE_SIZE, socket_id);
788 [ - + ]: 3 : if (cache == NULL) {
789 : 0 : RTE_MEMPOOL_LOG(ERR, "Cannot allocate mempool cache.");
790 : 0 : rte_errno = ENOMEM;
791 : 0 : return NULL;
792 : : }
793 : :
794 : : mempool_cache_init(cache, size);
795 : :
796 : 3 : rte_mempool_trace_cache_create(size, socket_id, cache);
797 : 3 : return cache;
798 : : }
799 : :
800 : : /*
801 : : * Free a cache. It's the responsibility of the user to make sure that any
802 : : * remaining objects in the cache are flushed to the corresponding
803 : : * mempool.
804 : : */
805 : : RTE_EXPORT_SYMBOL(rte_mempool_cache_free)
806 : : void
807 [ - + ]: 3 : rte_mempool_cache_free(struct rte_mempool_cache *cache)
808 : : {
809 : 3 : rte_mempool_trace_cache_free(cache);
810 : 3 : rte_free(cache);
811 : 3 : }
812 : :
813 : : /* create an empty mempool */
814 : : RTE_EXPORT_SYMBOL(rte_mempool_create_empty)
815 : : struct rte_mempool *
816 : 125 : rte_mempool_create_empty(const char *name, unsigned n, unsigned elt_size,
817 : : unsigned cache_size, unsigned private_data_size,
818 : : int socket_id, unsigned flags)
819 : : {
820 : : char mz_name[RTE_MEMZONE_NAMESIZE];
821 : : struct rte_mempool_list *mempool_list;
822 : : struct rte_mempool *mp = NULL;
823 : : struct rte_tailq_entry *te = NULL;
824 : : const struct rte_memzone *mz = NULL;
825 : : size_t mempool_size;
826 : : unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
827 : : struct rte_mempool_objsz objsz;
828 : : unsigned lcore_id;
829 : : int ret;
830 : :
831 : : /* compilation-time checks */
832 : : RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
833 : : RTE_CACHE_LINE_MASK) != 0);
834 : : RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
835 : : RTE_CACHE_LINE_MASK) != 0);
836 : : #ifdef RTE_LIBRTE_MEMPOOL_STATS
837 : : RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
838 : : RTE_CACHE_LINE_MASK) != 0);
839 : : RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
840 : : RTE_CACHE_LINE_MASK) != 0);
841 : : #endif
842 : :
843 : 125 : mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
844 : :
845 : : /* asked for zero items */
846 [ - + ]: 125 : if (n == 0) {
847 : 0 : rte_errno = EINVAL;
848 : 0 : return NULL;
849 : : }
850 : :
851 : : /* asked cache too big */
852 [ + + ]: 125 : if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
853 [ - + ]: 124 : CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
854 : 1 : rte_errno = EINVAL;
855 : 1 : return NULL;
856 : : }
857 : :
858 : : /* enforce only user flags are passed by the application */
859 [ + + ]: 124 : if ((flags & ~RTE_MEMPOOL_VALID_USER_FLAGS) != 0) {
860 : 1 : rte_errno = EINVAL;
861 : 1 : return NULL;
862 : : }
863 : :
864 : : /*
865 : : * No objects in the pool can be used for IO until it's populated
866 : : * with at least some objects with valid IOVA.
867 : : */
868 : 123 : flags |= RTE_MEMPOOL_F_NON_IO;
869 : :
870 : : /* "no cache align" imply "no spread" */
871 [ + + ]: 123 : if (flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
872 : 2 : flags |= RTE_MEMPOOL_F_NO_SPREAD;
873 : :
874 : : /* calculate mempool object sizes. */
875 [ - + ]: 123 : if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
876 : 0 : rte_errno = EINVAL;
877 : 0 : return NULL;
878 : : }
879 : :
880 : 122 : rte_mcfg_mempool_write_lock();
881 : :
882 : : /*
883 : : * reserve a memory zone for this mempool: private data is
884 : : * cache-aligned
885 : : */
886 : 123 : private_data_size = (private_data_size +
887 : : RTE_MEMPOOL_ALIGN_MASK) & (~RTE_MEMPOOL_ALIGN_MASK);
888 : :
889 : :
890 : : /* try to allocate tailq entry */
891 : 123 : te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
892 [ - + ]: 123 : if (te == NULL) {
893 : 0 : RTE_MEMPOOL_LOG(ERR, "Cannot allocate tailq entry!");
894 : 0 : goto exit_unlock;
895 : : }
896 : :
897 [ + + ]: 123 : mempool_size = RTE_MEMPOOL_HEADER_SIZE(mp, cache_size);
898 : 123 : mempool_size += private_data_size;
899 [ - + ]: 123 : mempool_size = RTE_ALIGN_CEIL(mempool_size, RTE_MEMPOOL_ALIGN);
900 : :
901 : : ret = snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
902 [ - + ]: 123 : if (ret < 0 || ret >= (int)sizeof(mz_name)) {
903 : 0 : rte_errno = ENAMETOOLONG;
904 : 0 : goto exit_unlock;
905 : : }
906 : :
907 : 123 : mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
908 [ + + ]: 123 : if (mz == NULL)
909 : 13 : goto exit_unlock;
910 : :
911 : : /* init the mempool structure */
912 [ - + ]: 110 : mp = mz->addr;
913 : : memset(mp, 0, RTE_MEMPOOL_HEADER_SIZE(mp, cache_size));
914 [ - + ]: 110 : ret = strlcpy(mp->name, name, sizeof(mp->name));
915 [ - + ]: 110 : if (ret < 0 || ret >= (int)sizeof(mp->name)) {
916 : 0 : rte_errno = ENAMETOOLONG;
917 : 0 : goto exit_unlock;
918 : : }
919 : 110 : mp->mz = mz;
920 : 110 : mp->size = n;
921 : 110 : mp->flags = flags;
922 : 110 : mp->socket_id = socket_id;
923 : 110 : mp->elt_size = objsz.elt_size;
924 : 110 : mp->header_size = objsz.header_size;
925 : 110 : mp->trailer_size = objsz.trailer_size;
926 : : /* Size of default caches, zero means disabled. */
927 : 110 : mp->cache_size = cache_size;
928 : 110 : mp->private_data_size = private_data_size;
929 : 110 : STAILQ_INIT(&mp->elt_list);
930 : 110 : STAILQ_INIT(&mp->mem_list);
931 : :
932 : : /*
933 : : * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
934 : : * set the correct index into the table of ops structs.
935 : : */
936 [ + + ]: 110 : if ((flags & RTE_MEMPOOL_F_SP_PUT) && (flags & RTE_MEMPOOL_F_SC_GET))
937 : 1 : ret = rte_mempool_set_ops_byname(mp, "ring_sp_sc", NULL);
938 [ + + ]: 109 : else if (flags & RTE_MEMPOOL_F_SP_PUT)
939 : 1 : ret = rte_mempool_set_ops_byname(mp, "ring_sp_mc", NULL);
940 [ - + ]: 108 : else if (flags & RTE_MEMPOOL_F_SC_GET)
941 : 0 : ret = rte_mempool_set_ops_byname(mp, "ring_mp_sc", NULL);
942 : : else
943 : 108 : ret = rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL);
944 : :
945 [ - + ]: 110 : if (ret) {
946 : 0 : rte_errno = -ret;
947 : 0 : goto exit_unlock;
948 : : }
949 : :
950 : : /*
951 : : * local_cache pointer is set even if cache_size is zero.
952 : : * The local_cache points to just past the elt_pa[] array.
953 : : */
954 : 110 : mp->local_cache = (struct rte_mempool_cache *)
955 : 110 : RTE_PTR_ADD(mp, RTE_MEMPOOL_HEADER_SIZE(mp, 0));
956 : :
957 : : /* Init all default caches. */
958 [ + + ]: 110 : if (cache_size != 0) {
959 [ + + ]: 2967 : for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
960 : 2944 : mempool_cache_init(&mp->local_cache[lcore_id],
961 : : cache_size);
962 : : }
963 : :
964 : 110 : te->data = mp;
965 : :
966 : 110 : rte_mcfg_tailq_write_lock();
967 : 110 : TAILQ_INSERT_TAIL(mempool_list, te, next);
968 : 110 : rte_mcfg_tailq_write_unlock();
969 : 110 : rte_mcfg_mempool_write_unlock();
970 : :
971 : 110 : rte_mempool_trace_create_empty(name, n, elt_size, cache_size,
972 : : private_data_size, flags, mp);
973 : 110 : return mp;
974 : :
975 : 13 : exit_unlock:
976 : 13 : rte_mcfg_mempool_write_unlock();
977 : 13 : rte_free(te);
978 : 13 : rte_mempool_free(mp);
979 : 13 : return NULL;
980 : : }
981 : :
982 : : /* create the mempool */
983 : : RTE_EXPORT_SYMBOL(rte_mempool_create)
984 : : struct rte_mempool *
985 : 91 : rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
986 : : unsigned cache_size, unsigned private_data_size,
987 : : rte_mempool_ctor_t *mp_init, void *mp_init_arg,
988 : : rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
989 : : int socket_id, unsigned flags)
990 : : {
991 : : struct rte_mempool *mp;
992 : :
993 : 91 : mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
994 : : private_data_size, socket_id, flags);
995 [ + + ]: 91 : if (mp == NULL)
996 : : return NULL;
997 : :
998 : : /* call the mempool priv initializer */
999 [ + + ]: 76 : if (mp_init)
1000 : 1 : mp_init(mp, mp_init_arg);
1001 : :
1002 [ - + ]: 76 : if (rte_mempool_populate_default(mp) < 0)
1003 : 0 : goto fail;
1004 : :
1005 : : /* call the object initializers */
1006 [ + + ]: 76 : if (obj_init)
1007 : 40 : rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
1008 : :
1009 : 76 : rte_mempool_trace_create(name, n, elt_size, cache_size,
1010 : : private_data_size, mp_init, mp_init_arg, obj_init,
1011 : : obj_init_arg, flags, mp);
1012 : 76 : return mp;
1013 : :
1014 : : fail:
1015 : 0 : rte_mempool_free(mp);
1016 : 0 : return NULL;
1017 : : }
1018 : :
1019 : : /* Return the number of entries in the mempool */
1020 : : RTE_EXPORT_SYMBOL(rte_mempool_avail_count)
1021 : : unsigned int
1022 : 610 : rte_mempool_avail_count(const struct rte_mempool *mp)
1023 : : {
1024 : : unsigned count;
1025 : : unsigned lcore_id;
1026 : :
1027 : 610 : count = rte_mempool_ops_get_count(mp);
1028 : :
1029 [ + + ]: 610 : if (mp->cache_size == 0)
1030 : : return count;
1031 : :
1032 [ + + ]: 56115 : for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1033 : 55680 : count += mp->local_cache[lcore_id].len;
1034 : :
1035 : : /*
1036 : : * due to race condition (access to len is not locked), the
1037 : : * total can be greater than size... so fix the result
1038 : : */
1039 : 435 : if (count > mp->size)
1040 : : return mp->size;
1041 : : return count;
1042 : : }
1043 : :
1044 : : /* return the number of entries allocated from the mempool */
1045 : : RTE_EXPORT_SYMBOL(rte_mempool_in_use_count)
1046 : : unsigned int
1047 : 23 : rte_mempool_in_use_count(const struct rte_mempool *mp)
1048 : : {
1049 : 23 : return mp->size - rte_mempool_avail_count(mp);
1050 : : }
1051 : :
1052 : : /* dump the cache status */
1053 : : static unsigned
1054 : 40 : rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
1055 : : {
1056 : : unsigned lcore_id;
1057 : : unsigned count = 0;
1058 : : unsigned cache_count;
1059 : :
1060 : : fprintf(f, " internal cache infos:\n");
1061 : 40 : fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size);
1062 : :
1063 [ + + ]: 40 : if (mp->cache_size == 0)
1064 : : return count;
1065 : :
1066 [ + + ]: 3225 : for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1067 : 3200 : cache_count = mp->local_cache[lcore_id].len;
1068 : : fprintf(f, " cache_count[%u]=%"PRIu32"\n",
1069 : : lcore_id, cache_count);
1070 : 3200 : count += cache_count;
1071 : : }
1072 : : fprintf(f, " total_cache_count=%u\n", count);
1073 : 25 : return count;
1074 : : }
1075 : :
1076 : : /* check and update cookies or panic (internal) */
1077 : : RTE_EXPORT_SYMBOL(rte_mempool_check_cookies)
1078 : 0 : void rte_mempool_check_cookies(const struct rte_mempool *mp,
1079 : : void * const *obj_table_const, unsigned n, int free)
1080 : : {
1081 : : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1082 : : struct rte_mempool_objhdr *hdr;
1083 : : struct rte_mempool_objtlr *tlr;
1084 : : uint64_t cookie;
1085 : : void *tmp;
1086 : : void *obj;
1087 : : void **obj_table;
1088 : :
1089 : : /* Force to drop the "const" attribute. This is done only when
1090 : : * DEBUG is enabled */
1091 : : tmp = (void *)(uintptr_t)obj_table_const;
1092 : : obj_table = tmp;
1093 : :
1094 : : while (n--) {
1095 : : obj = obj_table[n];
1096 : :
1097 : : if (rte_mempool_from_obj(obj) != mp)
1098 : : rte_panic("MEMPOOL: object is owned by another "
1099 : : "mempool\n");
1100 : :
1101 : : hdr = rte_mempool_get_header(obj);
1102 : : cookie = hdr->cookie;
1103 : :
1104 : : if (free == 0) {
1105 : : if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
1106 : : RTE_MEMPOOL_LOG(CRIT,
1107 : : "obj=%p, mempool=%p, cookie=%" PRIx64,
1108 : : obj, (const void *) mp, cookie);
1109 : : rte_panic("MEMPOOL: bad header cookie (put)\n");
1110 : : }
1111 : : hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
1112 : : } else if (free == 1) {
1113 : : if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1114 : : RTE_MEMPOOL_LOG(CRIT,
1115 : : "obj=%p, mempool=%p, cookie=%" PRIx64,
1116 : : obj, (const void *) mp, cookie);
1117 : : rte_panic("MEMPOOL: bad header cookie (get)\n");
1118 : : }
1119 : : hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
1120 : : } else if (free == 2) {
1121 : : if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
1122 : : cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1123 : : RTE_MEMPOOL_LOG(CRIT,
1124 : : "obj=%p, mempool=%p, cookie=%" PRIx64,
1125 : : obj, (const void *) mp, cookie);
1126 : : rte_panic("MEMPOOL: bad header cookie (audit)\n");
1127 : : }
1128 : : }
1129 : : tlr = rte_mempool_get_trailer(obj);
1130 : : cookie = tlr->cookie;
1131 : : if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
1132 : : RTE_MEMPOOL_LOG(CRIT,
1133 : : "obj=%p, mempool=%p, cookie=%" PRIx64,
1134 : : obj, (const void *) mp, cookie);
1135 : : rte_panic("MEMPOOL: bad trailer cookie\n");
1136 : : }
1137 : : }
1138 : : #else
1139 : : RTE_SET_USED(mp);
1140 : : RTE_SET_USED(obj_table_const);
1141 : : RTE_SET_USED(n);
1142 : : RTE_SET_USED(free);
1143 : : #endif
1144 : 0 : }
1145 : :
1146 : : RTE_EXPORT_SYMBOL(rte_mempool_contig_blocks_check_cookies)
1147 : : void
1148 : 0 : rte_mempool_contig_blocks_check_cookies(const struct rte_mempool *mp,
1149 : : void * const *first_obj_table_const, unsigned int n, int free)
1150 : : {
1151 : : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1152 : : struct rte_mempool_info info;
1153 : : const size_t total_elt_sz =
1154 : : mp->header_size + mp->elt_size + mp->trailer_size;
1155 : : unsigned int i, j;
1156 : :
1157 : : rte_mempool_ops_get_info(mp, &info);
1158 : :
1159 : : for (i = 0; i < n; ++i) {
1160 : : void *first_obj = first_obj_table_const[i];
1161 : :
1162 : : for (j = 0; j < info.contig_block_size; ++j) {
1163 : : void *obj;
1164 : :
1165 : : obj = (void *)((uintptr_t)first_obj + j * total_elt_sz);
1166 : : rte_mempool_check_cookies(mp, &obj, 1, free);
1167 : : }
1168 : : }
1169 : : #else
1170 : : RTE_SET_USED(mp);
1171 : : RTE_SET_USED(first_obj_table_const);
1172 : : RTE_SET_USED(n);
1173 : : RTE_SET_USED(free);
1174 : : #endif
1175 : 0 : }
1176 : :
1177 : : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1178 : : static void
1179 : : mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
1180 : : void *obj, __rte_unused unsigned idx)
1181 : : {
1182 : : RTE_MEMPOOL_CHECK_COOKIES(mp, &obj, 1, 2);
1183 : : }
1184 : :
1185 : : static void
1186 : : mempool_audit_cookies(struct rte_mempool *mp)
1187 : : {
1188 : : unsigned num;
1189 : :
1190 : : num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
1191 : : if (num != mp->size) {
1192 : : rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
1193 : : "iterated only over %u elements\n",
1194 : : mp, mp->size, num);
1195 : : }
1196 : : }
1197 : : #else
1198 : : #define mempool_audit_cookies(mp) do {} while(0)
1199 : : #endif
1200 : :
1201 : : /* check cookies before and after objects */
1202 : : static void
1203 : 40 : mempool_audit_cache(const struct rte_mempool *mp)
1204 : : {
1205 : : /* check cache size consistency */
1206 : : unsigned lcore_id;
1207 : :
1208 [ + + ]: 40 : if (mp->cache_size == 0)
1209 : : return;
1210 : :
1211 [ + + ]: 3225 : for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1212 : : const struct rte_mempool_cache *cache;
1213 : 3200 : cache = &mp->local_cache[lcore_id];
1214 [ - + ]: 3200 : if (cache->len > RTE_DIM(cache->objs)) {
1215 : 0 : RTE_MEMPOOL_LOG(CRIT, "badness on cache[%u]",
1216 : : lcore_id);
1217 : 0 : rte_panic("MEMPOOL: invalid cache len\n");
1218 : : }
1219 : : }
1220 : : }
1221 : :
1222 : : /* check the consistency of mempool (size, cookies, ...) */
1223 : : RTE_EXPORT_SYMBOL(rte_mempool_audit)
1224 : : void
1225 : 40 : rte_mempool_audit(struct rte_mempool *mp)
1226 : : {
1227 : 40 : mempool_audit_cache(mp);
1228 : : mempool_audit_cookies(mp);
1229 : :
1230 : : /* For case where mempool DEBUG is not set, and cache size is 0 */
1231 : : RTE_SET_USED(mp);
1232 : 40 : }
1233 : :
1234 : : /* dump the status of the mempool on the console */
1235 : : RTE_EXPORT_SYMBOL(rte_mempool_dump)
1236 : : void
1237 : 40 : rte_mempool_dump(FILE *f, struct rte_mempool *mp)
1238 : : {
1239 : : #ifdef RTE_LIBRTE_MEMPOOL_STATS
1240 : : struct rte_mempool_info info;
1241 : : struct rte_mempool_debug_stats sum;
1242 : : unsigned lcore_id;
1243 : : #endif
1244 : : struct rte_mempool_memhdr *memhdr;
1245 : : struct rte_mempool_ops *ops;
1246 : : unsigned common_count;
1247 : : unsigned cache_count;
1248 : : size_t mem_len = 0;
1249 : :
1250 : : RTE_ASSERT(f != NULL);
1251 : : RTE_ASSERT(mp != NULL);
1252 : :
1253 : 40 : fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
1254 : 40 : fprintf(f, " flags=%x\n", mp->flags);
1255 : 40 : fprintf(f, " socket_id=%d\n", mp->socket_id);
1256 : 40 : fprintf(f, " pool=%p\n", mp->pool_data);
1257 : 40 : fprintf(f, " iova=0x%" PRIx64 "\n", mp->mz->iova);
1258 : 40 : fprintf(f, " nb_mem_chunks=%u\n", mp->nb_mem_chunks);
1259 : 40 : fprintf(f, " size=%"PRIu32"\n", mp->size);
1260 : 40 : fprintf(f, " populated_size=%"PRIu32"\n", mp->populated_size);
1261 : 40 : fprintf(f, " header_size=%"PRIu32"\n", mp->header_size);
1262 : 40 : fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size);
1263 : 40 : fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size);
1264 : 40 : fprintf(f, " total_obj_size=%"PRIu32"\n",
1265 : 40 : mp->header_size + mp->elt_size + mp->trailer_size);
1266 : :
1267 : 40 : fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size);
1268 : :
1269 : 40 : fprintf(f, " ops_index=%d\n", mp->ops_index);
1270 [ - + ]: 40 : ops = rte_mempool_get_ops(mp->ops_index);
1271 : 40 : fprintf(f, " ops_name: <%s>\n", (ops != NULL) ? ops->name : "NA");
1272 : :
1273 [ + + ]: 2209 : STAILQ_FOREACH(memhdr, &mp->mem_list, next) {
1274 : 2169 : fprintf(f, " memory chunk at %p, addr=%p, iova=0x%" PRIx64 ", len=%zu\n",
1275 : : memhdr, memhdr->addr, memhdr->iova, memhdr->len);
1276 : 2169 : mem_len += memhdr->len;
1277 : : }
1278 [ + - ]: 40 : if (mem_len != 0) {
1279 : 40 : fprintf(f, " avg bytes/object=%#Lf\n",
1280 : 40 : (long double)mem_len / mp->size);
1281 : : }
1282 : :
1283 : 40 : cache_count = rte_mempool_dump_cache(f, mp);
1284 : 40 : common_count = rte_mempool_ops_get_count(mp);
1285 [ - + ]: 40 : if ((cache_count + common_count) > mp->size)
1286 : 0 : common_count = mp->size - cache_count;
1287 : : fprintf(f, " common_pool_count=%u\n", common_count);
1288 : :
1289 : : /* sum and dump statistics */
1290 : : #ifdef RTE_LIBRTE_MEMPOOL_STATS
1291 : : rte_mempool_ops_get_info(mp, &info);
1292 : : memset(&sum, 0, sizeof(sum));
1293 : : for (lcore_id = 0; lcore_id < RTE_MAX_LCORE + 1; lcore_id++) {
1294 : : sum.put_bulk += mp->stats[lcore_id].put_bulk;
1295 : : sum.put_objs += mp->stats[lcore_id].put_objs;
1296 : : sum.put_common_pool_bulk += mp->stats[lcore_id].put_common_pool_bulk;
1297 : : sum.put_common_pool_objs += mp->stats[lcore_id].put_common_pool_objs;
1298 : : sum.get_common_pool_bulk += mp->stats[lcore_id].get_common_pool_bulk;
1299 : : sum.get_common_pool_objs += mp->stats[lcore_id].get_common_pool_objs;
1300 : : sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
1301 : : sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
1302 : : sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
1303 : : sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
1304 : : sum.get_success_blks += mp->stats[lcore_id].get_success_blks;
1305 : : sum.get_fail_blks += mp->stats[lcore_id].get_fail_blks;
1306 : : }
1307 : : if (mp->cache_size != 0) {
1308 : : /* Add the statistics stored in the mempool caches. */
1309 : : for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1310 : : sum.put_bulk += mp->local_cache[lcore_id].stats.put_bulk;
1311 : : sum.put_objs += mp->local_cache[lcore_id].stats.put_objs;
1312 : : sum.get_success_bulk += mp->local_cache[lcore_id].stats.get_success_bulk;
1313 : : sum.get_success_objs += mp->local_cache[lcore_id].stats.get_success_objs;
1314 : : }
1315 : : }
1316 : : fprintf(f, " stats:\n");
1317 : : fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk);
1318 : : fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs);
1319 : : fprintf(f, " put_common_pool_bulk=%"PRIu64"\n", sum.put_common_pool_bulk);
1320 : : fprintf(f, " put_common_pool_objs=%"PRIu64"\n", sum.put_common_pool_objs);
1321 : : fprintf(f, " get_common_pool_bulk=%"PRIu64"\n", sum.get_common_pool_bulk);
1322 : : fprintf(f, " get_common_pool_objs=%"PRIu64"\n", sum.get_common_pool_objs);
1323 : : fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
1324 : : fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs);
1325 : : fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
1326 : : fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
1327 : : if (info.contig_block_size > 0) {
1328 : : fprintf(f, " get_success_blks=%"PRIu64"\n",
1329 : : sum.get_success_blks);
1330 : : fprintf(f, " get_fail_blks=%"PRIu64"\n", sum.get_fail_blks);
1331 : : }
1332 : : #else
1333 : : fprintf(f, " no statistics available\n");
1334 : : #endif
1335 : :
1336 : 40 : rte_mempool_audit(mp);
1337 : 40 : }
1338 : :
1339 : : /* dump the status of all mempools on the console */
1340 : : RTE_EXPORT_SYMBOL(rte_mempool_list_dump)
1341 : : void
1342 : 2 : rte_mempool_list_dump(FILE *f)
1343 : : {
1344 : : struct rte_mempool *mp = NULL;
1345 : : struct rte_tailq_entry *te;
1346 : : struct rte_mempool_list *mempool_list;
1347 : :
1348 : 2 : mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1349 : :
1350 : 2 : rte_mcfg_mempool_read_lock();
1351 : :
1352 [ + + ]: 16 : TAILQ_FOREACH(te, mempool_list, next) {
1353 : 14 : mp = (struct rte_mempool *) te->data;
1354 : 14 : rte_mempool_dump(f, mp);
1355 : : }
1356 : :
1357 : 2 : rte_mcfg_mempool_read_unlock();
1358 : 2 : }
1359 : :
1360 : : /* search a mempool from its name */
1361 : : RTE_EXPORT_SYMBOL(rte_mempool_lookup)
1362 : : struct rte_mempool *
1363 : 107 : rte_mempool_lookup(const char *name)
1364 : : {
1365 : : struct rte_mempool *mp = NULL;
1366 : : struct rte_tailq_entry *te;
1367 : : struct rte_mempool_list *mempool_list;
1368 : :
1369 : 107 : mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1370 : :
1371 : 107 : rte_mcfg_mempool_read_lock();
1372 : :
1373 [ + + ]: 1309 : TAILQ_FOREACH(te, mempool_list, next) {
1374 : 1302 : mp = (struct rte_mempool *) te->data;
1375 [ + + ]: 1302 : if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
1376 : : break;
1377 : : }
1378 : :
1379 : 107 : rte_mcfg_mempool_read_unlock();
1380 : :
1381 [ + + ]: 107 : if (te == NULL) {
1382 : 7 : rte_errno = ENOENT;
1383 : 7 : return NULL;
1384 : : }
1385 : :
1386 : : return mp;
1387 : : }
1388 : :
1389 : : RTE_EXPORT_SYMBOL(rte_mempool_walk)
1390 : 1 : void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1391 : : void *arg)
1392 : : {
1393 : : struct rte_tailq_entry *te = NULL;
1394 : : struct rte_mempool_list *mempool_list;
1395 : : void *tmp_te;
1396 : :
1397 : 1 : mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1398 : :
1399 : 1 : rte_mcfg_mempool_read_lock();
1400 : :
1401 [ + + ]: 8 : RTE_TAILQ_FOREACH_SAFE(te, mempool_list, next, tmp_te) {
1402 : 7 : (*func)((struct rte_mempool *) te->data, arg);
1403 : : }
1404 : :
1405 : 1 : rte_mcfg_mempool_read_unlock();
1406 : 1 : }
1407 : :
1408 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_mempool_get_mem_range, 24.07)
1409 : 3 : int rte_mempool_get_mem_range(const struct rte_mempool *mp,
1410 : : struct rte_mempool_mem_range_info *mem_range)
1411 : : {
1412 : : void *address_low = (void *)UINTPTR_MAX;
1413 : : void *address_high = 0;
1414 : : size_t address_diff = 0;
1415 : : size_t total_size = 0;
1416 : : struct rte_mempool_memhdr *hdr;
1417 : :
1418 [ + + ]: 3 : if (mp == NULL || mem_range == NULL)
1419 : : return -EINVAL;
1420 : :
1421 : : /* go through memory chunks and find the lowest and highest addresses */
1422 [ + + ]: 4 : STAILQ_FOREACH(hdr, &mp->mem_list, next) {
1423 : 2 : if (address_low > hdr->addr)
1424 : : address_low = hdr->addr;
1425 : 2 : if (address_high < RTE_PTR_ADD(hdr->addr, hdr->len))
1426 : : address_high = RTE_PTR_ADD(hdr->addr, hdr->len);
1427 : 2 : total_size += hdr->len;
1428 : : }
1429 : :
1430 : : /* check if mempool was not populated yet (no memory chunks) */
1431 [ + - ]: 2 : if (address_low == (void *)UINTPTR_MAX)
1432 : : return -EINVAL;
1433 : :
1434 : 2 : address_diff = (size_t)RTE_PTR_DIFF(address_high, address_low);
1435 : :
1436 : 2 : mem_range->start = address_low;
1437 : 2 : mem_range->length = address_diff;
1438 : 2 : mem_range->is_contiguous = (total_size == address_diff) ? true : false;
1439 : :
1440 : 2 : return 0;
1441 : : }
1442 : :
1443 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_mempool_get_obj_alignment, 24.07)
1444 : 3 : size_t rte_mempool_get_obj_alignment(const struct rte_mempool *mp)
1445 : : {
1446 [ + + ]: 3 : if (mp == NULL)
1447 : : return 0;
1448 : :
1449 [ + + ]: 2 : if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
1450 : : return sizeof(uint64_t);
1451 : : else
1452 : 1 : return RTE_MEMPOOL_ALIGN;
1453 : : }
1454 : :
1455 : : struct mempool_callback_data {
1456 : : TAILQ_ENTRY(mempool_callback_data) callbacks;
1457 : : rte_mempool_event_callback *func;
1458 : : void *user_data;
1459 : : };
1460 : :
1461 : : static void
1462 : 213 : mempool_event_callback_invoke(enum rte_mempool_event event,
1463 : : struct rte_mempool *mp)
1464 : : {
1465 : : struct mempool_callback_data *cb;
1466 : : void *tmp_te;
1467 : :
1468 : 213 : rte_mcfg_tailq_read_lock();
1469 [ + + ]: 235 : RTE_TAILQ_FOREACH_SAFE(cb, &callback_tailq, callbacks, tmp_te) {
1470 : 22 : rte_mcfg_tailq_read_unlock();
1471 : 22 : cb->func(event, mp, cb->user_data);
1472 : 22 : rte_mcfg_tailq_read_lock();
1473 : : }
1474 : 213 : rte_mcfg_tailq_read_unlock();
1475 : 213 : }
1476 : :
1477 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_mempool_event_callback_register)
1478 : : int
1479 : 10 : rte_mempool_event_callback_register(rte_mempool_event_callback *func,
1480 : : void *user_data)
1481 : : {
1482 : : struct mempool_callback_data *cb;
1483 : : int ret;
1484 : :
1485 [ - + ]: 10 : if (func == NULL) {
1486 : 0 : rte_errno = EINVAL;
1487 : 0 : return -rte_errno;
1488 : : }
1489 : :
1490 : 10 : rte_mcfg_tailq_write_lock();
1491 [ + + ]: 19 : TAILQ_FOREACH(cb, &callback_tailq, callbacks) {
1492 [ + + + + ]: 10 : if (cb->func == func && cb->user_data == user_data) {
1493 : : ret = -EEXIST;
1494 : 1 : goto exit;
1495 : : }
1496 : : }
1497 : :
1498 : 9 : cb = calloc(1, sizeof(*cb));
1499 [ - + ]: 9 : if (cb == NULL) {
1500 : 0 : RTE_MEMPOOL_LOG(ERR, "Cannot allocate event callback!");
1501 : : ret = -ENOMEM;
1502 : 0 : goto exit;
1503 : : }
1504 : :
1505 : 9 : cb->func = func;
1506 : 9 : cb->user_data = user_data;
1507 : 9 : TAILQ_INSERT_TAIL(&callback_tailq, cb, callbacks);
1508 : : ret = 0;
1509 : :
1510 : 10 : exit:
1511 : 10 : rte_mcfg_tailq_write_unlock();
1512 : 10 : rte_errno = -ret;
1513 : 10 : return ret;
1514 : : }
1515 : :
1516 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_mempool_event_callback_unregister)
1517 : : int
1518 : 17 : rte_mempool_event_callback_unregister(rte_mempool_event_callback *func,
1519 : : void *user_data)
1520 : : {
1521 : : struct mempool_callback_data *cb;
1522 : : int ret = -ENOENT;
1523 : :
1524 : 17 : rte_mcfg_tailq_write_lock();
1525 [ + + ]: 31 : TAILQ_FOREACH(cb, &callback_tailq, callbacks) {
1526 [ + + + + ]: 23 : if (cb->func == func && cb->user_data == user_data) {
1527 [ + + ]: 9 : TAILQ_REMOVE(&callback_tailq, cb, callbacks);
1528 : : ret = 0;
1529 : : break;
1530 : : }
1531 : : }
1532 : 17 : rte_mcfg_tailq_write_unlock();
1533 : :
1534 : : if (ret == 0)
1535 : 9 : free(cb);
1536 : 17 : rte_errno = -ret;
1537 : 17 : return ret;
1538 : : }
1539 : :
1540 : : static void
1541 : 0 : mempool_list_cb(struct rte_mempool *mp, void *arg)
1542 : : {
1543 : : struct rte_tel_data *d = (struct rte_tel_data *)arg;
1544 : :
1545 : 0 : rte_tel_data_add_array_string(d, mp->name);
1546 : 0 : }
1547 : :
1548 : : static int
1549 : 0 : mempool_handle_list(const char *cmd __rte_unused,
1550 : : const char *params __rte_unused, struct rte_tel_data *d)
1551 : : {
1552 : 0 : rte_tel_data_start_array(d, RTE_TEL_STRING_VAL);
1553 : 0 : rte_mempool_walk(mempool_list_cb, d);
1554 : 0 : return 0;
1555 : : }
1556 : :
1557 : : struct mempool_info_cb_arg {
1558 : : char *pool_name;
1559 : : struct rte_tel_data *d;
1560 : : };
1561 : :
1562 : : static void
1563 : 0 : mempool_info_cb(struct rte_mempool *mp, void *arg)
1564 : : {
1565 : : struct mempool_info_cb_arg *info = (struct mempool_info_cb_arg *)arg;
1566 : : const struct rte_memzone *mz;
1567 : : uint64_t cache_count, common_count;
1568 : :
1569 [ # # ]: 0 : if (strncmp(mp->name, info->pool_name, RTE_MEMZONE_NAMESIZE))
1570 : : return;
1571 : :
1572 : 0 : rte_tel_data_add_dict_string(info->d, "name", mp->name);
1573 : 0 : rte_tel_data_add_dict_uint(info->d, "pool_id", mp->pool_id);
1574 : 0 : rte_tel_data_add_dict_uint(info->d, "flags", mp->flags);
1575 : 0 : rte_tel_data_add_dict_int(info->d, "socket_id", mp->socket_id);
1576 : 0 : rte_tel_data_add_dict_uint(info->d, "size", mp->size);
1577 : 0 : rte_tel_data_add_dict_uint(info->d, "cache_size", mp->cache_size);
1578 : 0 : rte_tel_data_add_dict_uint(info->d, "elt_size", mp->elt_size);
1579 : 0 : rte_tel_data_add_dict_uint(info->d, "header_size", mp->header_size);
1580 : 0 : rte_tel_data_add_dict_uint(info->d, "trailer_size", mp->trailer_size);
1581 : 0 : rte_tel_data_add_dict_uint(info->d, "private_data_size",
1582 : 0 : mp->private_data_size);
1583 : 0 : rte_tel_data_add_dict_int(info->d, "ops_index", mp->ops_index);
1584 : 0 : rte_tel_data_add_dict_uint(info->d, "populated_size",
1585 : 0 : mp->populated_size);
1586 : :
1587 : : cache_count = 0;
1588 [ # # ]: 0 : if (mp->cache_size > 0) {
1589 : : int lcore_id;
1590 [ # # ]: 0 : for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1591 : 0 : cache_count += mp->local_cache[lcore_id].len;
1592 : : }
1593 : 0 : rte_tel_data_add_dict_uint(info->d, "total_cache_count", cache_count);
1594 : 0 : common_count = rte_mempool_ops_get_count(mp);
1595 [ # # ]: 0 : if ((cache_count + common_count) > mp->size)
1596 : 0 : common_count = mp->size - cache_count;
1597 : 0 : rte_tel_data_add_dict_uint(info->d, "common_pool_count", common_count);
1598 : :
1599 : 0 : mz = mp->mz;
1600 : 0 : rte_tel_data_add_dict_string(info->d, "mz_name", mz->name);
1601 : 0 : rte_tel_data_add_dict_uint(info->d, "mz_len", mz->len);
1602 : 0 : rte_tel_data_add_dict_uint(info->d, "mz_hugepage_sz",
1603 : 0 : mz->hugepage_sz);
1604 : 0 : rte_tel_data_add_dict_int(info->d, "mz_socket_id", mz->socket_id);
1605 : 0 : rte_tel_data_add_dict_uint(info->d, "mz_flags", mz->flags);
1606 : : }
1607 : :
1608 : : static int
1609 : 0 : mempool_handle_info(const char *cmd __rte_unused, const char *params,
1610 : : struct rte_tel_data *d)
1611 : : {
1612 : : struct mempool_info_cb_arg mp_arg;
1613 : : char name[RTE_MEMZONE_NAMESIZE];
1614 : :
1615 [ # # # # ]: 0 : if (!params || strlen(params) == 0)
1616 : : return -EINVAL;
1617 : :
1618 : : rte_strlcpy(name, params, RTE_MEMZONE_NAMESIZE);
1619 : :
1620 : 0 : rte_tel_data_start_dict(d);
1621 : 0 : mp_arg.pool_name = name;
1622 : 0 : mp_arg.d = d;
1623 : 0 : rte_mempool_walk(mempool_info_cb, &mp_arg);
1624 : :
1625 : 0 : return 0;
1626 : : }
1627 : :
1628 : 252 : RTE_INIT(mempool_init_telemetry)
1629 : : {
1630 : 252 : rte_telemetry_register_cmd("/mempool/list", mempool_handle_list,
1631 : : "Returns list of available mempool. Takes no parameters");
1632 : 252 : rte_telemetry_register_cmd("/mempool/info", mempool_handle_info,
1633 : : "Returns mempool info. Parameters: pool_name");
1634 : 252 : }
|