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 [ - + ]: 305 : 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 [ - + ]: 305 : 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 : 233 : if (0 == a)
64 : : return b;
65 [ + - ]: 233 : if (0 == b)
66 : : return a;
67 : :
68 [ + + ]: 233 : if (a < b) {
69 : : c = a;
70 : : a = b;
71 : : b = c;
72 : : }
73 : :
74 [ + + ]: 624 : while (b != 0) {
75 : 391 : 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 : 139 : 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 : 139 : nchan = rte_memory_get_nchannel();
97 [ + - ]: 139 : if (nchan == 0)
98 : : nchan = 4;
99 : :
100 : 139 : nrank = rte_memory_get_nrank();
101 : : if (nrank == 0)
102 : : nrank = 1;
103 : :
104 : : /* process new object size */
105 : 139 : new_obj_size = (obj_size + RTE_MEMPOOL_ALIGN_MASK) / RTE_MEMPOOL_ALIGN;
106 [ + - + + ]: 466 : while (get_gcd(new_obj_size, nrank * nchan) != 1)
107 : 94 : new_obj_size++;
108 : 139 : 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 : 2180 : 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 : 2180 : valid = msl->socket_id == wa->socket_id;
137 [ + + - + ]: 2180 : valid |= wa->socket_id == SOCKET_ID_ANY && msl->external == 0;
138 : :
139 [ + + + + ]: 2180 : if (valid && msl->page_sz < wa->min)
140 : 1090 : wa->min = msl->page_sz;
141 : :
142 : 2180 : return 0;
143 : : }
144 : :
145 : : static size_t
146 : 1090 : get_min_page_size(int socket_id)
147 : : {
148 : : struct pagesz_walk_arg wa;
149 : :
150 : 1090 : wa.min = SIZE_MAX;
151 : 1090 : wa.socket_id = socket_id;
152 : :
153 : 1090 : rte_memseg_list_walk(find_min_pagesz, &wa);
154 : :
155 [ - + ]: 1090 : return wa.min == SIZE_MAX ? (size_t) rte_mem_page_size() : wa.min;
156 : : }
157 : :
158 : :
159 : : static void
160 : 45049097 : 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 : 45049097 : hdr = RTE_PTR_SUB(obj, sizeof(*hdr));
167 : 45049097 : hdr->mp = mp;
168 : 45049097 : hdr->iova = iova;
169 : 45049097 : STAILQ_INSERT_TAIL(&mp->elt_list, hdr, next);
170 : 45049097 : 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 : 45049097 : }
177 : :
178 : : /* call obj_cb() for each mempool element */
179 : : RTE_EXPORT_SYMBOL(rte_mempool_obj_iter)
180 : : uint32_t
181 : 77 : 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 [ + + ]: 80662 : STAILQ_FOREACH(hdr, &mp->elt_list, next) {
189 : 80585 : obj = (char *)hdr + sizeof(*hdr);
190 : 80585 : obj_cb(mp, obj_cb_arg, obj, n);
191 : 80585 : n++;
192 : : }
193 : :
194 : 77 : 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 : 141 : 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 [ + - ]: 141 : sz = (sz != NULL) ? sz : &lsz;
223 : :
224 : 141 : sz->header_size = sizeof(struct rte_mempool_objhdr);
225 [ + + ]: 141 : if ((flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN) == 0)
226 : 139 : 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 : 141 : sz->trailer_size = 0;
233 : : #endif
234 : :
235 : : /* element size is 8 bytes-aligned at least */
236 : 141 : sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
237 : :
238 : : /* expand trailer to next cache line */
239 [ + + ]: 141 : if ((flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
240 : 139 : sz->total_size = sz->header_size + sz->elt_size +
241 : : sz->trailer_size;
242 : 139 : sz->trailer_size += ((RTE_MEMPOOL_ALIGN -
243 : 139 : (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 [ + + ]: 141 : if ((flags & RTE_MEMPOOL_F_NO_SPREAD) == 0) {
252 : : unsigned new_size;
253 : 139 : new_size = arch_mem_object_align
254 : 139 : (sz->header_size + sz->elt_size + sz->trailer_size);
255 : 139 : 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 : 141 : sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
260 : :
261 : 141 : return sz->total_size;
262 : : }
263 : :
264 : : /* free a memchunk allocated with rte_memzone_reserve() */
265 : : static void
266 : 118 : rte_mempool_memchunk_mz_free(__rte_unused struct rte_mempool_memhdr *memhdr,
267 : : void *opaque)
268 : : {
269 : : const struct rte_memzone *mz = opaque;
270 : 118 : rte_memzone_free(mz);
271 : 118 : }
272 : :
273 : : /* Free memory chunks used by a mempool. Objects must be in pool */
274 : : static void
275 : 123 : rte_mempool_free_memchunks(struct rte_mempool *mp)
276 : : {
277 : : struct rte_mempool_memhdr *memhdr;
278 : : void *elt;
279 : :
280 [ + + ]: 45042011 : while (!STAILQ_EMPTY(&mp->elt_list)) {
281 : : rte_mempool_ops_dequeue_bulk(mp, &elt, 1);
282 : : (void)elt;
283 [ + + ]: 45041888 : STAILQ_REMOVE_HEAD(&mp->elt_list, next);
284 : 45041888 : mp->populated_size--;
285 : : }
286 : :
287 [ + + ]: 1203 : while (!STAILQ_EMPTY(&mp->mem_list)) {
288 : : memhdr = STAILQ_FIRST(&mp->mem_list);
289 [ + + ]: 1080 : STAILQ_REMOVE_HEAD(&mp->mem_list, next);
290 [ + + ]: 1080 : if (memhdr->free_cb != NULL)
291 : 121 : memhdr->free_cb(memhdr, memhdr->opaque);
292 : 1080 : rte_free(memhdr);
293 : 1080 : mp->nb_mem_chunks--;
294 : : }
295 : 123 : }
296 : :
297 : : static int
298 : 1212 : mempool_ops_alloc_once(struct rte_mempool *mp)
299 : : {
300 : : int ret;
301 : :
302 : : /* create the internal ring if not already done */
303 [ + + ]: 1212 : if ((mp->flags & RTE_MEMPOOL_F_POOL_CREATED) == 0) {
304 : 128 : ret = rte_mempool_ops_alloc(mp);
305 [ + - ]: 128 : if (ret != 0)
306 : : return ret;
307 : 128 : 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 : 1086 : 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 : 1086 : ret = mempool_ops_alloc_once(mp);
328 [ + - ]: 1086 : if (ret != 0)
329 : : return ret;
330 : :
331 : : /* mempool is already populated */
332 [ + - ]: 1086 : if (mp->populated_size >= mp->size)
333 : : return -ENOSPC;
334 : :
335 : 1086 : memhdr = rte_zmalloc("MEMPOOL_MEMHDR", sizeof(*memhdr), 0);
336 [ + - ]: 1086 : if (memhdr == NULL)
337 : : return -ENOMEM;
338 : :
339 : 1086 : memhdr->mp = mp;
340 : 1086 : memhdr->addr = vaddr;
341 : 1086 : memhdr->iova = iova;
342 : 1086 : memhdr->len = len;
343 : 1086 : memhdr->free_cb = free_cb;
344 : 1086 : memhdr->opaque = opaque;
345 : :
346 [ + + ]: 1086 : if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
347 : 2 : off = RTE_PTR_ALIGN_CEIL(vaddr, 8) - vaddr;
348 : : else
349 : 1084 : off = RTE_PTR_ALIGN_CEIL(vaddr, RTE_MEMPOOL_ALIGN) - vaddr;
350 : :
351 [ - + ]: 1086 : if (off > len) {
352 : : ret = 0;
353 : 0 : goto fail;
354 : : }
355 : :
356 [ + + ]: 1086 : i = rte_mempool_ops_populate(mp, mp->size - mp->populated_size,
357 : 1086 : (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 [ - + ]: 1086 : if (i == 0) {
363 : : ret = 0;
364 : 0 : goto fail;
365 : : }
366 : :
367 : 1086 : STAILQ_INSERT_TAIL(&mp->mem_list, memhdr, next);
368 : 1086 : mp->nb_mem_chunks++;
369 : :
370 : : /* Check if at least some objects in the pool are now usable for IO. */
371 [ + + + + ]: 1086 : if (!(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG) && iova != RTE_BAD_IOVA)
372 : 1083 : mp->flags &= ~RTE_MEMPOOL_F_NON_IO;
373 : :
374 : : /* Report the mempool as ready only when fully populated. */
375 [ + + ]: 1086 : if (mp->populated_size >= mp->size)
376 : 126 : mempool_event_callback_invoke(RTE_MEMPOOL_EVENT_READY, mp);
377 : :
378 : 1086 : rte_mempool_trace_populate_iova(mp, vaddr, iova, len, free_cb, opaque);
379 : 1086 : return i;
380 : :
381 : 0 : fail:
382 : 0 : rte_free(memhdr);
383 : 0 : return ret;
384 : : }
385 : :
386 : : static rte_iova_t
387 : 2191061 : get_iova(void *addr)
388 : : {
389 : : struct rte_memseg *ms;
390 : :
391 : : /* try registered memory first */
392 : 2191061 : ms = rte_mem_virt2memseg(addr, NULL);
393 [ + + - + ]: 2191061 : if (ms == NULL || ms->iova == RTE_BAD_IOVA)
394 : : /* fall back to actual physical address */
395 : 1935 : return rte_mem_virt2iova(addr);
396 : 2189126 : 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 : 126 : 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 [ - + ]: 126 : 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 [ + + ]: 1208 : for (off = 0; off < len &&
417 [ + + ]: 1083 : mp->populated_size < mp->size; off += phys_len) {
418 : :
419 : 1082 : iova = get_iova(addr + off);
420 : :
421 : : /* populate with the largest group of contiguous pages */
422 : 1082 : for (phys_len = RTE_MIN(
423 : : (size_t)(RTE_PTR_ALIGN_CEIL(addr + off + 1, pg_sz) -
424 : : (addr + off)),
425 : : len - off);
426 [ + + ]: 2190104 : off + phys_len < len;
427 : 2189022 : phys_len = RTE_MIN(phys_len + pg_sz, len - off)) {
428 : : rte_iova_t iova_tmp;
429 : :
430 : 2189979 : iova_tmp = get_iova(addr + off + phys_len);
431 : :
432 [ + - ]: 2189979 : if (iova_tmp == RTE_BAD_IOVA ||
433 [ + + ]: 2189979 : iova_tmp != iova + phys_len)
434 : : break;
435 : : }
436 : :
437 : 1082 : ret = rte_mempool_populate_iova(mp, addr + off, iova,
438 : : phys_len, free_cb, opaque);
439 [ - + ]: 1082 : if (ret == 0)
440 : 0 : continue;
441 [ - + ]: 1082 : if (ret < 0)
442 : 0 : goto fail;
443 : : /* no need to call the free callback for next chunks */
444 : : free_cb = NULL;
445 : 1082 : cnt += ret;
446 : : }
447 : :
448 : 126 : rte_mempool_trace_populate_virt(mp, addr, len, pg_sz, free_cb, opaque);
449 : 126 : 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 : 1209 : 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 : 1209 : ret = rte_malloc_heap_socket_is_external(mp->socket_id);
467 [ + - ]: 1209 : if (ret < 0)
468 : : return -EINVAL;
469 : : alloc_in_ext_mem = (ret == 1);
470 : 1209 : need_iova_contig_obj = !(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG);
471 : :
472 [ + + ]: 1209 : if (!need_iova_contig_obj)
473 : 1 : *pg_sz = 0;
474 [ + + - + ]: 1208 : else if (rte_eal_has_hugepages() || alloc_in_ext_mem)
475 : 1090 : *pg_sz = get_min_page_size(mp->socket_id);
476 : : else
477 : 118 : *pg_sz = rte_mem_page_size();
478 : :
479 [ - + ]: 1209 : rte_mempool_trace_get_page_size(mp, *pg_sz);
480 : 1209 : 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 : 123 : 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 : 123 : ret = mempool_ops_alloc_once(mp);
503 [ + - ]: 123 : if (ret != 0)
504 : : return ret;
505 : :
506 : : /* mempool must not be populated */
507 [ + - ]: 123 : 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 : 123 : need_iova_contig_obj = !(mp->flags & RTE_MEMPOOL_F_NO_IOVA_CONTIG);
549 : 123 : ret = rte_mempool_get_page_size(mp, &pg_sz);
550 [ + - ]: 123 : if (ret < 0)
551 : : return ret;
552 : :
553 [ + - ]: 123 : if (pg_sz != 0)
554 : 123 : pg_shift = rte_bsf32(pg_sz);
555 : :
556 [ + + ]: 246 : for (mz_id = 0, n = mp->size; n > 0; mz_id++, n -= ret) {
557 : : size_t min_chunk_size;
558 : :
559 : 123 : mem_size = rte_mempool_ops_calc_mem_size(
560 : : mp, n, pg_shift, &min_chunk_size, &align);
561 : :
562 [ - + ]: 123 : if (mem_size < 0) {
563 : 0 : ret = mem_size;
564 : 0 : goto fail;
565 : : }
566 : :
567 : : ret = snprintf(mz_name, sizeof(mz_name),
568 [ - + ]: 123 : RTE_MEMPOOL_MZ_FORMAT "_%d", mp->name, mz_id);
569 [ - + ]: 123 : 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 [ - + ]: 123 : 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 : 123 : mz = rte_memzone_reserve_aligned(mz_name,
583 : 123 : RTE_MIN((size_t)mem_size, max_alloc_size),
584 : : mp->socket_id, mz_flags, align);
585 : :
586 [ - + - - ]: 123 : 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 [ - + ]: 123 : if (mz == NULL) {
594 : 0 : ret = -rte_errno;
595 : 0 : goto fail;
596 : : }
597 : :
598 [ + - ]: 123 : if (need_iova_contig_obj)
599 : 123 : iova = mz->iova;
600 : : else
601 : : iova = RTE_BAD_IOVA;
602 : :
603 [ + - - + ]: 123 : 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 : 123 : ret = rte_mempool_populate_virt(mp, mz->addr,
610 : 123 : mz->len, pg_sz,
611 : : rte_mempool_memchunk_mz_free,
612 : : (void *)(uintptr_t)mz);
613 [ + - ]: 123 : if (ret == 0) /* should not happen */
614 : : ret = -ENOBUFS;
615 [ - + ]: 123 : if (ret < 0) {
616 : 0 : rte_memzone_free(mz);
617 : 0 : goto fail;
618 : : }
619 : : }
620 : :
621 : 123 : rte_mempool_trace_populate_default(mp);
622 : 123 : 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 : 140 : rte_mempool_free(struct rte_mempool *mp)
724 : : {
725 : : struct rte_mempool_list *mempool_list = NULL;
726 : : struct rte_tailq_entry *te;
727 : :
728 [ + + ]: 140 : if (mp == NULL)
729 : : return;
730 : :
731 : 123 : mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
732 : 123 : rte_mcfg_tailq_write_lock();
733 : : /* find out tailq entry */
734 [ + - ]: 345 : TAILQ_FOREACH(te, mempool_list, next) {
735 [ + + ]: 345 : if (te->data == (void *)mp)
736 : : break;
737 : : }
738 : :
739 [ + - ]: 123 : if (te != NULL) {
740 [ + + ]: 123 : TAILQ_REMOVE(mempool_list, te, next);
741 : 123 : rte_free(te);
742 : : }
743 : 123 : rte_mcfg_tailq_write_unlock();
744 : :
745 : 123 : mempool_event_callback_invoke(RTE_MEMPOOL_EVENT_DESTROY, mp);
746 : 123 : rte_mempool_trace_free(mp);
747 : 123 : rte_mempool_free_memchunks(mp);
748 : 123 : rte_mempool_ops_free(mp);
749 : 123 : rte_memzone_free(mp->mz);
750 : : }
751 : :
752 : : static void
753 : : mempool_cache_init(struct rte_mempool_cache *cache, uint32_t size)
754 : : {
755 : 3843 : cache->size = size;
756 [ - + ]: 3 : cache->len = 0;
757 : : }
758 : :
759 : : /*
760 : : * Create and initialize a cache for objects that are retrieved from and
761 : : * returned to an underlying mempool. This structure is identical to the
762 : : * local_cache entry pointed to by the mempool structure.
763 : : */
764 : : RTE_EXPORT_SYMBOL(rte_mempool_cache_create)
765 : : struct rte_mempool_cache *
766 : 3 : rte_mempool_cache_create(uint32_t size, int socket_id)
767 : : {
768 : : struct rte_mempool_cache *cache;
769 : 3 : size_t sizeof_cache = sizeof(struct rte_mempool_cache) + size * sizeof(void *);
770 : :
771 [ - + ]: 3 : if (size == 0 || sizeof_cache > UINT32_MAX) {
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 : 143 : 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, sizeof_cache_per_lcore;
816 : : unsigned int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
817 : : struct rte_mempool_objsz objsz;
818 : : int ret;
819 : :
820 : : /* compilation-time checks */
821 : : RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
822 : : RTE_CACHE_LINE_MASK) != 0);
823 : : RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
824 : : RTE_CACHE_LINE_MASK) != 0);
825 : : RTE_BUILD_BUG_ON(offsetof(struct rte_mempool_cache, objs) !=
826 : : sizeof(struct rte_mempool_cache));
827 : : #ifdef RTE_LIBRTE_MEMPOOL_STATS
828 : : RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
829 : : RTE_CACHE_LINE_MASK) != 0);
830 : : RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
831 : : RTE_CACHE_LINE_MASK) != 0);
832 : : #endif
833 : :
834 : 143 : mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
835 : :
836 : : /* asked for zero items */
837 [ - + ]: 143 : if (n == 0) {
838 : 0 : rte_errno = EINVAL;
839 : 0 : return NULL;
840 : : }
841 : :
842 : : /* asked cache too big */
843 : : sizeof_cache_per_lcore = 0;
844 [ + + ]: 143 : if (cache_size != 0) {
845 : : sizeof_cache_per_lcore = sizeof(struct rte_mempool_cache);
846 : 32 : sizeof_cache_per_lcore += RTE_CACHE_LINE_ROUNDUP(cache_size * sizeof(void *));
847 : : /*
848 : : * Add padding, to guard against false sharing-like effects
849 : : * on systems with a next-N-lines hardware prefetcher, when
850 : : * accessing objects at the end of the cache.
851 : : */
852 : 32 : sizeof_cache_per_lcore += RTE_CACHE_GUARD_LINES * RTE_CACHE_LINE_SIZE;
853 : : }
854 : 143 : if (sizeof_cache_per_lcore > UINT32_MAX ||
855 [ + + ]: 143 : cache_size > n) {
856 : 1 : rte_errno = EINVAL;
857 : 1 : return NULL;
858 : : }
859 : :
860 : : /* enforce only user flags are passed by the application */
861 [ + + ]: 142 : if ((flags & ~RTE_MEMPOOL_VALID_USER_FLAGS) != 0) {
862 : 1 : rte_errno = EINVAL;
863 : 1 : return NULL;
864 : : }
865 : :
866 : : /*
867 : : * No objects in the pool can be used for IO until it's populated
868 : : * with at least some objects with valid IOVA.
869 : : */
870 : 141 : flags |= RTE_MEMPOOL_F_NON_IO;
871 : :
872 : : /* "no cache align" imply "no spread" */
873 [ + + ]: 141 : if (flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
874 : 2 : flags |= RTE_MEMPOOL_F_NO_SPREAD;
875 : :
876 : : /* calculate mempool object sizes. */
877 [ - + ]: 141 : if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
878 : 0 : rte_errno = EINVAL;
879 : 0 : return NULL;
880 : : }
881 : :
882 : 141 : rte_mcfg_mempool_write_lock();
883 : :
884 : : /*
885 : : * reserve a memory zone for this mempool: private data is
886 : : * cache-aligned
887 : : */
888 : 141 : private_data_size = RTE_CACHE_LINE_ROUNDUP(private_data_size);
889 : : /*
890 : : * If any private data, add padding, to guard against false sharing-like
891 : : * effects on systems with a next-N-lines hardware prefetcher, when
892 : : * accessing private data.
893 : : */
894 [ + + ]: 141 : if (private_data_size != 0)
895 : 40 : private_data_size += RTE_CACHE_GUARD_LINES * RTE_CACHE_LINE_SIZE;
896 : :
897 : : /* try to allocate tailq entry */
898 : 141 : te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
899 [ - + ]: 141 : if (te == NULL) {
900 : 0 : RTE_MEMPOOL_LOG(ERR, "Cannot allocate tailq entry!");
901 : 0 : goto exit_unlock;
902 : : }
903 : :
904 : : mempool_size = sizeof(struct rte_mempool);
905 : 141 : mempool_size += private_data_size;
906 [ - + ]: 141 : mempool_size += RTE_MAX_LCORE * sizeof_cache_per_lcore;
907 : :
908 : : ret = snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
909 [ - + ]: 141 : if (ret < 0 || ret >= (int)sizeof(mz_name)) {
910 : 0 : rte_errno = ENAMETOOLONG;
911 : 0 : goto exit_unlock;
912 : : }
913 : :
914 : 141 : mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
915 [ + + ]: 141 : if (mz == NULL)
916 : 13 : goto exit_unlock;
917 : :
918 : : /* init the mempool structure */
919 [ - + ]: 128 : mp = mz->addr;
920 : : memset(mp, 0, mempool_size);
921 [ - + ]: 128 : ret = strlcpy(mp->name, name, sizeof(mp->name));
922 [ - + ]: 128 : if (ret < 0 || ret >= (int)sizeof(mp->name)) {
923 : 0 : rte_errno = ENAMETOOLONG;
924 : 0 : goto exit_unlock;
925 : : }
926 : 128 : mp->mz = mz;
927 : 128 : mp->size = n;
928 : 128 : mp->flags = flags;
929 : 128 : mp->socket_id = socket_id;
930 : 128 : mp->elt_size = objsz.elt_size;
931 : 128 : mp->header_size = objsz.header_size;
932 : 128 : mp->trailer_size = objsz.trailer_size;
933 : 128 : mp->cache_size = cache_size;
934 : 128 : mp->private_data_size = private_data_size;
935 : 128 : STAILQ_INIT(&mp->elt_list);
936 : 128 : STAILQ_INIT(&mp->mem_list);
937 : :
938 : : /*
939 : : * Since we have 4 combinations of the SP/SC/MP/MC examine the flags to
940 : : * set the correct index into the table of ops structs.
941 : : */
942 [ + + ]: 128 : if ((flags & RTE_MEMPOOL_F_SP_PUT) && (flags & RTE_MEMPOOL_F_SC_GET))
943 : 1 : ret = rte_mempool_set_ops_byname(mp, "ring_sp_sc", NULL);
944 [ + + ]: 127 : else if (flags & RTE_MEMPOOL_F_SP_PUT)
945 : 1 : ret = rte_mempool_set_ops_byname(mp, "ring_sp_mc", NULL);
946 [ - + ]: 126 : else if (flags & RTE_MEMPOOL_F_SC_GET)
947 : 0 : ret = rte_mempool_set_ops_byname(mp, "ring_mp_sc", NULL);
948 : : else
949 : 126 : ret = rte_mempool_set_ops_byname(mp, "ring_mp_mc", NULL);
950 : :
951 [ - + ]: 128 : if (ret) {
952 : 0 : rte_errno = -ret;
953 : 0 : goto exit_unlock;
954 : : }
955 : :
956 : : /* local_cache pointer is only set if per-lcore local cache is present */
957 [ + + ]: 128 : if (cache_size != 0) {
958 : 30 : mp->local_cache = (struct rte_mempool_cache *)
959 : 30 : RTE_PTR_ADD(mp, sizeof(struct rte_mempool) + private_data_size);
960 : 30 : mp->sizeof_cache_per_lcore = sizeof_cache_per_lcore;
961 : :
962 : : /* Init all default caches. */
963 : : struct rte_mempool_cache *cache = mp->local_cache;
964 [ + + ]: 3870 : for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE;
965 : 3840 : lcore_id++, cache = RTE_PTR_ADD(cache, sizeof_cache_per_lcore))
966 : : mempool_cache_init(cache, cache_size);
967 : : }
968 : :
969 : 128 : te->data = mp;
970 : :
971 : 128 : rte_mcfg_tailq_write_lock();
972 : 128 : TAILQ_INSERT_TAIL(mempool_list, te, next);
973 : 128 : rte_mcfg_tailq_write_unlock();
974 : 128 : rte_mcfg_mempool_write_unlock();
975 : :
976 : 128 : rte_mempool_trace_create_empty(name, n, elt_size, cache_size,
977 : : private_data_size, flags, mp);
978 : 128 : return mp;
979 : :
980 : 13 : exit_unlock:
981 : 13 : rte_mcfg_mempool_write_unlock();
982 : 13 : rte_free(te);
983 : 13 : rte_mempool_free(mp);
984 : 13 : return NULL;
985 : : }
986 : :
987 : : /* create the mempool */
988 : : RTE_EXPORT_SYMBOL(rte_mempool_create)
989 : : struct rte_mempool *
990 : 99 : rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
991 : : unsigned cache_size, unsigned private_data_size,
992 : : rte_mempool_ctor_t *mp_init, void *mp_init_arg,
993 : : rte_mempool_obj_cb_t *obj_init, void *obj_init_arg,
994 : : int socket_id, unsigned flags)
995 : : {
996 : : struct rte_mempool *mp;
997 : :
998 : 99 : mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
999 : : private_data_size, socket_id, flags);
1000 [ + + ]: 99 : if (mp == NULL)
1001 : : return NULL;
1002 : :
1003 : : /* call the mempool priv initializer */
1004 [ + + ]: 84 : if (mp_init)
1005 : 1 : mp_init(mp, mp_init_arg);
1006 : :
1007 [ - + ]: 84 : if (rte_mempool_populate_default(mp) < 0)
1008 : 0 : goto fail;
1009 : :
1010 : : /* call the object initializers */
1011 [ + + ]: 84 : if (obj_init)
1012 : 40 : rte_mempool_obj_iter(mp, obj_init, obj_init_arg);
1013 : :
1014 : 84 : rte_mempool_trace_create(name, n, elt_size, cache_size,
1015 : : private_data_size, mp_init, mp_init_arg, obj_init,
1016 : : obj_init_arg, flags, mp);
1017 : 84 : return mp;
1018 : :
1019 : : fail:
1020 : 0 : rte_mempool_free(mp);
1021 : 0 : return NULL;
1022 : : }
1023 : :
1024 : : /* Return the number of entries in the mempool */
1025 : : RTE_EXPORT_SYMBOL(rte_mempool_avail_count)
1026 : : unsigned int
1027 : 633 : rte_mempool_avail_count(const struct rte_mempool *mp)
1028 : : {
1029 : : unsigned int count;
1030 : :
1031 : 633 : count = rte_mempool_ops_get_count(mp);
1032 : :
1033 [ + + ]: 633 : if (mp->local_cache == NULL)
1034 : : return count;
1035 : :
1036 : : const struct rte_mempool_cache *cache = mp->local_cache;
1037 [ + + ]: 56373 : for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE;
1038 : 55936 : lcore_id++, cache = RTE_PTR_ADD(cache, mp->sizeof_cache_per_lcore))
1039 : 55936 : count += cache->len;
1040 : :
1041 : : /*
1042 : : * due to race condition (access to len is not locked), the
1043 : : * total can be greater than size... so fix the result
1044 : : */
1045 : 437 : if (count > mp->size)
1046 : : return mp->size;
1047 : : return count;
1048 : : }
1049 : :
1050 : : /* return the number of entries allocated from the mempool */
1051 : : RTE_EXPORT_SYMBOL(rte_mempool_in_use_count)
1052 : : unsigned int
1053 : 23 : rte_mempool_in_use_count(const struct rte_mempool *mp)
1054 : : {
1055 : 23 : return mp->size - rte_mempool_avail_count(mp);
1056 : : }
1057 : :
1058 : : /* Reset the statistics of a mempool. */
1059 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_mempool_stats_reset, 26.07)
1060 : : void
1061 : 0 : rte_mempool_stats_reset(struct rte_mempool *mp)
1062 : : {
1063 : : RTE_ASSERT(mp != NULL);
1064 : :
1065 : : #ifdef RTE_LIBRTE_MEMPOOL_STATS
1066 : : memset(&mp->stats, 0, sizeof(mp->stats));
1067 : : if (mp->local_cache != NULL) {
1068 : : struct rte_mempool_cache *cache = mp->local_cache;
1069 : : for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE;
1070 : : lcore_id++, cache = RTE_PTR_ADD(cache, mp->sizeof_cache_per_lcore))
1071 : : memset(&cache->stats, 0, sizeof(cache->stats));
1072 : : }
1073 : :
1074 : : RTE_MEMPOOL_LOG(DEBUG, "<%s>@%p: statistics reset", mp->name, mp);
1075 : : rte_mempool_trace_stats_reset(mp);
1076 : : #else
1077 : : RTE_SET_USED(mp);
1078 : : #endif
1079 : 0 : }
1080 : :
1081 : : /* dump the cache status */
1082 : : static unsigned
1083 : 40 : rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
1084 : : {
1085 : : unsigned int count = 0;
1086 : :
1087 : : fprintf(f, " internal cache infos (hide zero value items):\n");
1088 : 40 : fprintf(f, " cache_size=%"PRIu32"\n", mp->cache_size);
1089 : :
1090 [ + + ]: 40 : if (mp->local_cache == NULL)
1091 : : return count;
1092 : :
1093 : : const struct rte_mempool_cache *cache = mp->local_cache;
1094 [ + + ]: 3225 : for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE;
1095 : 3200 : lcore_id++, cache = RTE_PTR_ADD(cache, mp->sizeof_cache_per_lcore)) {
1096 : 3200 : unsigned int cache_count = cache->len;
1097 [ + + ]: 3200 : if (cache_count == 0)
1098 : 3195 : continue;
1099 : : fprintf(f, " cache_count[%u]=%"PRIu32"\n",
1100 : : lcore_id, cache_count);
1101 : 5 : count += cache_count;
1102 : : }
1103 : : fprintf(f, " total_cache_count=%u\n", count);
1104 : 25 : return count;
1105 : : }
1106 : :
1107 : : /* check and update cookies or panic (internal) */
1108 : : RTE_EXPORT_SYMBOL(rte_mempool_check_cookies)
1109 : 0 : void rte_mempool_check_cookies(const struct rte_mempool *mp,
1110 : : void * const *obj_table_const, unsigned n, int free)
1111 : : {
1112 : : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1113 : : struct rte_mempool_objhdr *hdr;
1114 : : struct rte_mempool_objtlr *tlr;
1115 : : uint64_t cookie;
1116 : : void *tmp;
1117 : : void *obj;
1118 : : void **obj_table;
1119 : :
1120 : : /* Force to drop the "const" attribute. This is done only when
1121 : : * DEBUG is enabled */
1122 : : tmp = (void *)(uintptr_t)obj_table_const;
1123 : : obj_table = tmp;
1124 : :
1125 : : while (n--) {
1126 : : obj = obj_table[n];
1127 : :
1128 : : if (rte_mempool_from_obj(obj) != mp)
1129 : : rte_panic("MEMPOOL: object is owned by another "
1130 : : "mempool\n");
1131 : :
1132 : : hdr = rte_mempool_get_header(obj);
1133 : : cookie = hdr->cookie;
1134 : :
1135 : : if (free == 0) {
1136 : : if (cookie != RTE_MEMPOOL_HEADER_COOKIE1) {
1137 : : RTE_MEMPOOL_LOG(CRIT,
1138 : : "obj=%p, mempool=%p, cookie=%" PRIx64,
1139 : : obj, (const void *) mp, cookie);
1140 : : rte_panic("MEMPOOL: bad header cookie (put)\n");
1141 : : }
1142 : : hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
1143 : : } else if (free == 1) {
1144 : : if (cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1145 : : RTE_MEMPOOL_LOG(CRIT,
1146 : : "obj=%p, mempool=%p, cookie=%" PRIx64,
1147 : : obj, (const void *) mp, cookie);
1148 : : rte_panic("MEMPOOL: bad header cookie (get)\n");
1149 : : }
1150 : : hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE1;
1151 : : } else if (free == 2) {
1152 : : if (cookie != RTE_MEMPOOL_HEADER_COOKIE1 &&
1153 : : cookie != RTE_MEMPOOL_HEADER_COOKIE2) {
1154 : : RTE_MEMPOOL_LOG(CRIT,
1155 : : "obj=%p, mempool=%p, cookie=%" PRIx64,
1156 : : obj, (const void *) mp, cookie);
1157 : : rte_panic("MEMPOOL: bad header cookie (audit)\n");
1158 : : }
1159 : : }
1160 : : tlr = rte_mempool_get_trailer(obj);
1161 : : cookie = tlr->cookie;
1162 : : if (cookie != RTE_MEMPOOL_TRAILER_COOKIE) {
1163 : : RTE_MEMPOOL_LOG(CRIT,
1164 : : "obj=%p, mempool=%p, cookie=%" PRIx64,
1165 : : obj, (const void *) mp, cookie);
1166 : : rte_panic("MEMPOOL: bad trailer cookie\n");
1167 : : }
1168 : : }
1169 : : #else
1170 : : RTE_SET_USED(mp);
1171 : : RTE_SET_USED(obj_table_const);
1172 : : RTE_SET_USED(n);
1173 : : RTE_SET_USED(free);
1174 : : #endif
1175 : 0 : }
1176 : :
1177 : : RTE_EXPORT_SYMBOL(rte_mempool_contig_blocks_check_cookies)
1178 : : void
1179 : 0 : rte_mempool_contig_blocks_check_cookies(const struct rte_mempool *mp,
1180 : : void * const *first_obj_table_const, unsigned int n, int free)
1181 : : {
1182 : : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1183 : : struct rte_mempool_info info;
1184 : : const size_t total_elt_sz =
1185 : : mp->header_size + mp->elt_size + mp->trailer_size;
1186 : : unsigned int i, j;
1187 : :
1188 : : rte_mempool_ops_get_info(mp, &info);
1189 : :
1190 : : for (i = 0; i < n; ++i) {
1191 : : void *first_obj = first_obj_table_const[i];
1192 : :
1193 : : for (j = 0; j < info.contig_block_size; ++j) {
1194 : : void *obj;
1195 : :
1196 : : obj = (void *)((uintptr_t)first_obj + j * total_elt_sz);
1197 : : rte_mempool_check_cookies(mp, &obj, 1, free);
1198 : : }
1199 : : }
1200 : : #else
1201 : : RTE_SET_USED(mp);
1202 : : RTE_SET_USED(first_obj_table_const);
1203 : : RTE_SET_USED(n);
1204 : : RTE_SET_USED(free);
1205 : : #endif
1206 : 0 : }
1207 : :
1208 : : #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
1209 : : static void
1210 : : mempool_obj_audit(struct rte_mempool *mp, __rte_unused void *opaque,
1211 : : void *obj, __rte_unused unsigned idx)
1212 : : {
1213 : : RTE_MEMPOOL_CHECK_COOKIES(mp, &obj, 1, 2);
1214 : : }
1215 : :
1216 : : /* check cookies before and after objects */
1217 : : static void
1218 : : mempool_audit_cookies(struct rte_mempool *mp)
1219 : : {
1220 : : unsigned num;
1221 : :
1222 : : num = rte_mempool_obj_iter(mp, mempool_obj_audit, NULL);
1223 : : if (num != mp->size) {
1224 : : rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
1225 : : "iterated only over %u elements\n",
1226 : : mp, mp->size, num);
1227 : : }
1228 : : }
1229 : : #else
1230 : : #define mempool_audit_cookies(mp) do {} while(0)
1231 : : #endif
1232 : :
1233 : : /* check cache size consistency */
1234 : : static void
1235 : 0 : mempool_audit_cache(const struct rte_mempool *mp)
1236 : : {
1237 [ # # ]: 0 : if (mp->local_cache == NULL)
1238 : : return;
1239 : :
1240 : : const struct rte_mempool_cache *cache = mp->local_cache;
1241 [ # # ]: 0 : for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE;
1242 : 0 : lcore_id++, cache = RTE_PTR_ADD(cache, mp->sizeof_cache_per_lcore)) {
1243 [ # # ]: 0 : if (cache->size > mp->size) {
1244 : 0 : RTE_MEMPOOL_LOG(CRIT, "badness on cache[%u] size", lcore_id);
1245 : 0 : rte_panic("MEMPOOL: invalid cache[%u] size\n", lcore_id);
1246 : : }
1247 [ # # ]: 0 : if (cache->len > cache->size) {
1248 : 0 : RTE_MEMPOOL_LOG(CRIT, "badness on cache[%u] len", lcore_id);
1249 : 0 : rte_panic("MEMPOOL: invalid cache[%u] len\n", lcore_id);
1250 : : }
1251 : : }
1252 : : }
1253 : :
1254 : : /* check the consistency of mempool (size, cookies, ...) */
1255 : : RTE_EXPORT_SYMBOL(rte_mempool_audit)
1256 : : void
1257 : 0 : rte_mempool_audit(struct rte_mempool *mp)
1258 : : {
1259 : 0 : mempool_audit_cache(mp);
1260 : : mempool_audit_cookies(mp);
1261 : 0 : }
1262 : :
1263 : : /* dump the status of the mempool on the console */
1264 : : RTE_EXPORT_SYMBOL(rte_mempool_dump)
1265 : : void
1266 : 40 : rte_mempool_dump(FILE *f, struct rte_mempool *mp)
1267 : : {
1268 : : #ifdef RTE_LIBRTE_MEMPOOL_STATS
1269 : : struct rte_mempool_info info;
1270 : : struct rte_mempool_debug_stats sum;
1271 : : unsigned lcore_id;
1272 : : #endif
1273 : : struct rte_mempool_memhdr *memhdr;
1274 : : struct rte_mempool_ops *ops;
1275 : : unsigned common_count;
1276 : : unsigned cache_count;
1277 : : size_t mem_len = 0;
1278 : :
1279 : : RTE_ASSERT(f != NULL);
1280 : : RTE_ASSERT(mp != NULL);
1281 : :
1282 : 40 : fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
1283 : 40 : fprintf(f, " flags=%x\n", mp->flags);
1284 : 40 : fprintf(f, " socket_id=%d\n", mp->socket_id);
1285 : 40 : fprintf(f, " pool=%p\n", mp->pool_data);
1286 : 40 : fprintf(f, " iova=0x%" PRIx64 "\n", mp->mz->iova);
1287 : 40 : fprintf(f, " nb_mem_chunks=%u\n", mp->nb_mem_chunks);
1288 : 40 : fprintf(f, " size=%"PRIu32"\n", mp->size);
1289 : 40 : fprintf(f, " populated_size=%"PRIu32"\n", mp->populated_size);
1290 : 40 : fprintf(f, " header_size=%"PRIu32"\n", mp->header_size);
1291 : 40 : fprintf(f, " elt_size=%"PRIu32"\n", mp->elt_size);
1292 : 40 : fprintf(f, " trailer_size=%"PRIu32"\n", mp->trailer_size);
1293 : 40 : fprintf(f, " total_obj_size=%"PRIu32"\n",
1294 : 40 : mp->header_size + mp->elt_size + mp->trailer_size);
1295 : :
1296 : 40 : fprintf(f, " private_data_size=%"PRIu32"\n", mp->private_data_size);
1297 : :
1298 : 40 : fprintf(f, " ops_index=%d\n", mp->ops_index);
1299 : 40 : ops = rte_mempool_get_ops(mp->ops_index);
1300 : 40 : fprintf(f, " ops_name: <%s>\n", (ops != NULL) ? ops->name : "NA");
1301 : :
1302 [ + + ]: 1957 : STAILQ_FOREACH(memhdr, &mp->mem_list, next) {
1303 : 1917 : fprintf(f, " memory chunk at %p, addr=%p, iova=0x%" PRIx64 ", len=%zu\n",
1304 : : memhdr, memhdr->addr, memhdr->iova, memhdr->len);
1305 : 1917 : mem_len += memhdr->len;
1306 : : }
1307 [ + - ]: 40 : if (mem_len != 0) {
1308 : 40 : fprintf(f, " avg bytes/object=%#Lf\n",
1309 : 40 : (long double)mem_len / mp->size);
1310 : : }
1311 : :
1312 : 40 : cache_count = rte_mempool_dump_cache(f, mp);
1313 : 40 : common_count = rte_mempool_ops_get_count(mp);
1314 [ - + ]: 40 : if ((cache_count + common_count) > mp->size)
1315 : 0 : common_count = mp->size - cache_count;
1316 : : fprintf(f, " common_pool_count=%u\n", common_count);
1317 : :
1318 : : /* sum and dump statistics */
1319 : : #ifdef RTE_LIBRTE_MEMPOOL_STATS
1320 : : rte_mempool_ops_get_info(mp, &info);
1321 : : memset(&sum, 0, sizeof(sum));
1322 : : for (lcore_id = 0; lcore_id < RTE_MAX_LCORE + 1; lcore_id++) {
1323 : : sum.put_bulk += mp->stats[lcore_id].put_bulk;
1324 : : sum.put_objs += mp->stats[lcore_id].put_objs;
1325 : : sum.put_common_pool_bulk += mp->stats[lcore_id].put_common_pool_bulk;
1326 : : sum.put_common_pool_objs += mp->stats[lcore_id].put_common_pool_objs;
1327 : : sum.get_common_pool_bulk += mp->stats[lcore_id].get_common_pool_bulk;
1328 : : sum.get_common_pool_objs += mp->stats[lcore_id].get_common_pool_objs;
1329 : : sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
1330 : : sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
1331 : : sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
1332 : : sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
1333 : : sum.get_success_blks += mp->stats[lcore_id].get_success_blks;
1334 : : sum.get_fail_blks += mp->stats[lcore_id].get_fail_blks;
1335 : : }
1336 : : if (mp->local_cache != NULL) {
1337 : : /* Add the statistics stored in the mempool caches. */
1338 : : const struct rte_mempool_cache *cache = mp->local_cache;
1339 : : for (lcore_id = 0; lcore_id < RTE_MAX_LCORE;
1340 : : lcore_id++, cache = RTE_PTR_ADD(cache, mp->sizeof_cache_per_lcore)) {
1341 : : sum.put_bulk += cache->stats.put_bulk;
1342 : : sum.put_objs += cache->stats.put_objs;
1343 : : sum.get_success_bulk += cache->stats.get_success_bulk;
1344 : : sum.get_success_objs += cache->stats.get_success_objs;
1345 : : }
1346 : : }
1347 : : fprintf(f, " stats:\n");
1348 : : fprintf(f, " put_bulk=%"PRIu64"\n", sum.put_bulk);
1349 : : fprintf(f, " put_objs=%"PRIu64"\n", sum.put_objs);
1350 : : fprintf(f, " put_common_pool_bulk=%"PRIu64"\n", sum.put_common_pool_bulk);
1351 : : fprintf(f, " put_common_pool_objs=%"PRIu64"\n", sum.put_common_pool_objs);
1352 : : fprintf(f, " get_common_pool_bulk=%"PRIu64"\n", sum.get_common_pool_bulk);
1353 : : fprintf(f, " get_common_pool_objs=%"PRIu64"\n", sum.get_common_pool_objs);
1354 : : fprintf(f, " get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
1355 : : fprintf(f, " get_success_objs=%"PRIu64"\n", sum.get_success_objs);
1356 : : fprintf(f, " get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
1357 : : fprintf(f, " get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
1358 : : if (info.contig_block_size > 0) {
1359 : : fprintf(f, " get_success_blks=%"PRIu64"\n", sum.get_success_blks);
1360 : : fprintf(f, " get_fail_blks=%"PRIu64"\n", sum.get_fail_blks);
1361 : : }
1362 : : fprintf(f, " avg objs/bulk put=%#Lf, get=%#Lf, get_fail=%#Lf\n",
1363 : : sum.put_bulk != 0 ? (long double)sum.put_objs / sum.put_bulk : 0,
1364 : : sum.get_success_bulk != 0 ?
1365 : : (long double)sum.get_success_objs / sum.get_success_bulk : 0,
1366 : : sum.get_fail_bulk != 0 ?
1367 : : (long double)sum.get_fail_objs / sum.get_fail_bulk : 0);
1368 : : fprintf(f, " avg common_pool objs/bulk put=%#Lf, get=%#Lf\n",
1369 : : sum.put_common_pool_bulk != 0 ?
1370 : : (long double)sum.put_common_pool_objs / sum.put_common_pool_bulk : 0,
1371 : : sum.get_common_pool_bulk != 0 ?
1372 : : (long double)sum.get_common_pool_objs / sum.get_common_pool_bulk : 0);
1373 : : fprintf(f, " avg cache miss rate put_objs=%s%#Lf, get_objs=%s%#Lf\n",
1374 : : sum.put_common_pool_objs != 0 ? "1/" : "",
1375 : : sum.put_common_pool_objs != 0 ?
1376 : : (long double)sum.put_objs / sum.put_common_pool_objs : 0,
1377 : : sum.get_common_pool_objs != 0 ? "1/" : "",
1378 : : sum.get_common_pool_objs != 0 ?
1379 : : (long double)sum.get_success_objs / sum.get_common_pool_objs : 0);
1380 : : fprintf(f, " avg cache miss rate put_bulk=%s%#Lf, get_bulk=%s%#Lf\n",
1381 : : sum.put_common_pool_bulk != 0 ? "1/" : "",
1382 : : sum.put_common_pool_bulk != 0 ?
1383 : : (long double)sum.put_bulk / sum.put_common_pool_bulk : 0,
1384 : : sum.get_common_pool_bulk != 0 ? "1/" : "",
1385 : : sum.get_common_pool_bulk != 0 ?
1386 : : (long double)sum.get_success_bulk / sum.get_common_pool_bulk : 0);
1387 : : #else
1388 : : fprintf(f, " no statistics available\n");
1389 : : #endif
1390 : 40 : }
1391 : :
1392 : : /* dump the status of all mempools on the console */
1393 : : RTE_EXPORT_SYMBOL(rte_mempool_list_dump)
1394 : : void
1395 : 2 : rte_mempool_list_dump(FILE *f)
1396 : : {
1397 : : struct rte_mempool *mp = NULL;
1398 : : struct rte_tailq_entry *te;
1399 : : struct rte_mempool_list *mempool_list;
1400 : :
1401 : 2 : mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1402 : :
1403 : 2 : rte_mcfg_mempool_read_lock();
1404 : :
1405 [ + + ]: 16 : TAILQ_FOREACH(te, mempool_list, next) {
1406 : 14 : mp = (struct rte_mempool *) te->data;
1407 : 14 : rte_mempool_dump(f, mp);
1408 : : }
1409 : :
1410 : 2 : rte_mcfg_mempool_read_unlock();
1411 : 2 : }
1412 : :
1413 : : /* search a mempool from its name */
1414 : : RTE_EXPORT_SYMBOL(rte_mempool_lookup)
1415 : : struct rte_mempool *
1416 : 106 : rte_mempool_lookup(const char *name)
1417 : : {
1418 : : struct rte_mempool *mp = NULL;
1419 : : struct rte_tailq_entry *te;
1420 : : struct rte_mempool_list *mempool_list;
1421 : :
1422 : 106 : mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1423 : :
1424 : 106 : rte_mcfg_mempool_read_lock();
1425 : :
1426 [ + + ]: 1320 : TAILQ_FOREACH(te, mempool_list, next) {
1427 : 1314 : mp = (struct rte_mempool *) te->data;
1428 [ + + ]: 1314 : if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
1429 : : break;
1430 : : }
1431 : :
1432 : 106 : rte_mcfg_mempool_read_unlock();
1433 : :
1434 [ + + ]: 106 : if (te == NULL) {
1435 : 6 : rte_errno = ENOENT;
1436 : 6 : return NULL;
1437 : : }
1438 : :
1439 : : return mp;
1440 : : }
1441 : :
1442 : : RTE_EXPORT_SYMBOL(rte_mempool_walk)
1443 : 8 : void rte_mempool_walk(void (*func)(struct rte_mempool *, void *),
1444 : : void *arg)
1445 : : {
1446 : : struct rte_tailq_entry *te = NULL;
1447 : : struct rte_mempool_list *mempool_list;
1448 : : void *tmp_te;
1449 : :
1450 : 8 : mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
1451 : :
1452 : 8 : rte_mcfg_mempool_read_lock();
1453 : :
1454 [ + + ]: 15 : RTE_TAILQ_FOREACH_SAFE(te, mempool_list, next, tmp_te) {
1455 : 7 : (*func)((struct rte_mempool *) te->data, arg);
1456 : : }
1457 : :
1458 : 8 : rte_mcfg_mempool_read_unlock();
1459 : 8 : }
1460 : :
1461 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_mempool_get_mem_range, 24.07)
1462 : 3 : int rte_mempool_get_mem_range(const struct rte_mempool *mp,
1463 : : struct rte_mempool_mem_range_info *mem_range)
1464 : : {
1465 : : void *address_low = (void *)UINTPTR_MAX;
1466 : : void *address_high = 0;
1467 : : size_t address_diff = 0;
1468 : : size_t total_size = 0;
1469 : : struct rte_mempool_memhdr *hdr;
1470 : :
1471 [ + + ]: 3 : if (mp == NULL || mem_range == NULL)
1472 : : return -EINVAL;
1473 : :
1474 : : /* go through memory chunks and find the lowest and highest addresses */
1475 [ + + ]: 4 : STAILQ_FOREACH(hdr, &mp->mem_list, next) {
1476 : 2 : if (address_low > hdr->addr)
1477 : : address_low = hdr->addr;
1478 : 2 : if (address_high < RTE_PTR_ADD(hdr->addr, hdr->len))
1479 : : address_high = RTE_PTR_ADD(hdr->addr, hdr->len);
1480 : 2 : total_size += hdr->len;
1481 : : }
1482 : :
1483 : : /* check if mempool was not populated yet (no memory chunks) */
1484 [ + - ]: 2 : if (address_low == (void *)UINTPTR_MAX)
1485 : : return -EINVAL;
1486 : :
1487 : 2 : address_diff = (size_t)RTE_PTR_DIFF(address_high, address_low);
1488 : :
1489 : 2 : mem_range->start = address_low;
1490 : 2 : mem_range->length = address_diff;
1491 : 2 : mem_range->is_contiguous = (total_size == address_diff) ? true : false;
1492 : :
1493 : 2 : return 0;
1494 : : }
1495 : :
1496 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_mempool_get_obj_alignment, 24.07)
1497 : 3 : size_t rte_mempool_get_obj_alignment(const struct rte_mempool *mp)
1498 : : {
1499 [ + + ]: 3 : if (mp == NULL)
1500 : : return 0;
1501 : :
1502 [ + + ]: 2 : if (mp->flags & RTE_MEMPOOL_F_NO_CACHE_ALIGN)
1503 : : return sizeof(uint64_t);
1504 : : else
1505 : 1 : return RTE_MEMPOOL_ALIGN;
1506 : : }
1507 : :
1508 : : struct mempool_callback_data {
1509 : : TAILQ_ENTRY(mempool_callback_data) callbacks;
1510 : : rte_mempool_event_callback *func;
1511 : : void *user_data;
1512 : : };
1513 : :
1514 : : static void
1515 : 249 : mempool_event_callback_invoke(enum rte_mempool_event event,
1516 : : struct rte_mempool *mp)
1517 : : {
1518 : : struct mempool_callback_data *cb;
1519 : : void *tmp_te;
1520 : :
1521 : 249 : rte_mcfg_tailq_read_lock();
1522 [ + + ]: 271 : RTE_TAILQ_FOREACH_SAFE(cb, &callback_tailq, callbacks, tmp_te) {
1523 : 22 : rte_mcfg_tailq_read_unlock();
1524 : 22 : cb->func(event, mp, cb->user_data);
1525 : 22 : rte_mcfg_tailq_read_lock();
1526 : : }
1527 : 249 : rte_mcfg_tailq_read_unlock();
1528 : 249 : }
1529 : :
1530 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_mempool_event_callback_register)
1531 : : int
1532 : 10 : rte_mempool_event_callback_register(rte_mempool_event_callback *func,
1533 : : void *user_data)
1534 : : {
1535 : : struct mempool_callback_data *cb;
1536 : : int ret;
1537 : :
1538 [ - + ]: 10 : if (func == NULL) {
1539 : 0 : rte_errno = EINVAL;
1540 : 0 : return -rte_errno;
1541 : : }
1542 : :
1543 : 10 : rte_mcfg_tailq_write_lock();
1544 [ + + ]: 19 : TAILQ_FOREACH(cb, &callback_tailq, callbacks) {
1545 [ + + + + ]: 10 : if (cb->func == func && cb->user_data == user_data) {
1546 : : ret = -EEXIST;
1547 : 1 : goto exit;
1548 : : }
1549 : : }
1550 : :
1551 : 9 : cb = calloc(1, sizeof(*cb));
1552 [ - + ]: 9 : if (cb == NULL) {
1553 : 0 : RTE_MEMPOOL_LOG(ERR, "Cannot allocate event callback!");
1554 : : ret = -ENOMEM;
1555 : 0 : goto exit;
1556 : : }
1557 : :
1558 : 9 : cb->func = func;
1559 : 9 : cb->user_data = user_data;
1560 : 9 : TAILQ_INSERT_TAIL(&callback_tailq, cb, callbacks);
1561 : : ret = 0;
1562 : :
1563 : 10 : exit:
1564 : 10 : rte_mcfg_tailq_write_unlock();
1565 : 10 : rte_errno = -ret;
1566 : 10 : return ret;
1567 : : }
1568 : :
1569 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_mempool_event_callback_unregister)
1570 : : int
1571 : 17 : rte_mempool_event_callback_unregister(rte_mempool_event_callback *func,
1572 : : void *user_data)
1573 : : {
1574 : : struct mempool_callback_data *cb;
1575 : : int ret = -ENOENT;
1576 : :
1577 : 17 : rte_mcfg_tailq_write_lock();
1578 [ + + ]: 31 : TAILQ_FOREACH(cb, &callback_tailq, callbacks) {
1579 [ + + + + ]: 23 : if (cb->func == func && cb->user_data == user_data) {
1580 [ + + ]: 9 : TAILQ_REMOVE(&callback_tailq, cb, callbacks);
1581 : : ret = 0;
1582 : : break;
1583 : : }
1584 : : }
1585 : 17 : rte_mcfg_tailq_write_unlock();
1586 : :
1587 : : if (ret == 0)
1588 : 9 : free(cb);
1589 : 17 : rte_errno = -ret;
1590 : 17 : return ret;
1591 : : }
1592 : :
1593 : : static void
1594 : 0 : mempool_list_cb(struct rte_mempool *mp, void *arg)
1595 : : {
1596 : : struct rte_tel_data *d = (struct rte_tel_data *)arg;
1597 : :
1598 : 0 : rte_tel_data_add_array_string(d, mp->name);
1599 : 0 : }
1600 : :
1601 : : static int
1602 : 3 : mempool_handle_list(const char *cmd __rte_unused,
1603 : : const char *params __rte_unused, struct rte_tel_data *d)
1604 : : {
1605 : 3 : rte_tel_data_start_array(d, RTE_TEL_STRING_VAL);
1606 : 3 : rte_mempool_walk(mempool_list_cb, d);
1607 : 3 : return 0;
1608 : : }
1609 : :
1610 : : struct mempool_info_cb_arg {
1611 : : char *pool_name;
1612 : : struct rte_tel_data *d;
1613 : : };
1614 : :
1615 : : static void
1616 : 0 : mempool_info_cb(struct rte_mempool *mp, void *arg)
1617 : : {
1618 : : struct mempool_info_cb_arg *info = (struct mempool_info_cb_arg *)arg;
1619 : : const struct rte_memzone *mz;
1620 : : uint64_t cache_count, common_count;
1621 : :
1622 [ # # ]: 0 : if (strncmp(mp->name, info->pool_name, RTE_MEMZONE_NAMESIZE))
1623 : : return;
1624 : :
1625 : 0 : rte_tel_data_add_dict_string(info->d, "name", mp->name);
1626 : 0 : rte_tel_data_add_dict_uint(info->d, "pool_id", mp->pool_id);
1627 : 0 : rte_tel_data_add_dict_uint(info->d, "flags", mp->flags);
1628 : 0 : rte_tel_data_add_dict_int(info->d, "socket_id", mp->socket_id);
1629 : 0 : rte_tel_data_add_dict_uint(info->d, "size", mp->size);
1630 : 0 : rte_tel_data_add_dict_uint(info->d, "cache_size", mp->cache_size);
1631 : 0 : rte_tel_data_add_dict_uint(info->d, "elt_size", mp->elt_size);
1632 : 0 : rte_tel_data_add_dict_uint(info->d, "header_size", mp->header_size);
1633 : 0 : rte_tel_data_add_dict_uint(info->d, "trailer_size", mp->trailer_size);
1634 : 0 : rte_tel_data_add_dict_uint(info->d, "private_data_size",
1635 : 0 : mp->private_data_size);
1636 : 0 : rte_tel_data_add_dict_int(info->d, "ops_index", mp->ops_index);
1637 : 0 : rte_tel_data_add_dict_uint(info->d, "populated_size",
1638 : 0 : mp->populated_size);
1639 : :
1640 : : cache_count = 0;
1641 [ # # ]: 0 : if (mp->local_cache != NULL) {
1642 : : const struct rte_mempool_cache *cache = mp->local_cache;
1643 [ # # ]: 0 : for (unsigned int lcore_id = 0; lcore_id < RTE_MAX_LCORE;
1644 : 0 : lcore_id++, cache = RTE_PTR_ADD(cache, mp->sizeof_cache_per_lcore))
1645 : 0 : cache_count += cache->len;
1646 : : }
1647 : 0 : rte_tel_data_add_dict_uint(info->d, "total_cache_count", cache_count);
1648 : 0 : common_count = rte_mempool_ops_get_count(mp);
1649 [ # # ]: 0 : if ((cache_count + common_count) > mp->size)
1650 : 0 : common_count = mp->size - cache_count;
1651 : 0 : rte_tel_data_add_dict_uint(info->d, "common_pool_count", common_count);
1652 : :
1653 : 0 : mz = mp->mz;
1654 : 0 : rte_tel_data_add_dict_string(info->d, "mz_name", mz->name);
1655 : 0 : rte_tel_data_add_dict_uint(info->d, "mz_len", mz->len);
1656 : 0 : rte_tel_data_add_dict_uint(info->d, "mz_hugepage_sz",
1657 : 0 : mz->hugepage_sz);
1658 : 0 : rte_tel_data_add_dict_int(info->d, "mz_socket_id", mz->socket_id);
1659 : 0 : rte_tel_data_add_dict_uint(info->d, "mz_flags", mz->flags);
1660 : : }
1661 : :
1662 : : static int
1663 : 3 : mempool_handle_info(const char *cmd __rte_unused, const char *params,
1664 : : struct rte_tel_data *d)
1665 : : {
1666 : : struct mempool_info_cb_arg mp_arg;
1667 : : char name[RTE_MEMZONE_NAMESIZE];
1668 : :
1669 [ + + + - ]: 3 : if (!params || strlen(params) == 0)
1670 : : return -EINVAL;
1671 : :
1672 : : rte_strlcpy(name, params, RTE_MEMZONE_NAMESIZE);
1673 : :
1674 : 2 : rte_tel_data_start_dict(d);
1675 : 2 : mp_arg.pool_name = name;
1676 : 2 : mp_arg.d = d;
1677 : 2 : rte_mempool_walk(mempool_info_cb, &mp_arg);
1678 : :
1679 : 2 : return 0;
1680 : : }
1681 : :
1682 : 305 : RTE_INIT(mempool_init_telemetry)
1683 : : {
1684 : 305 : rte_telemetry_register_cmd("/mempool/list", mempool_handle_list,
1685 : : "Returns list of available mempool. Takes no parameters");
1686 : 305 : rte_telemetry_register_cmd("/mempool/info", mempool_handle_info,
1687 : : "Returns mempool info. Parameters: pool_name");
1688 : 305 : }
|