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