mempool: add structure for object headers
[dpdk.git] / lib / librte_mempool / rte_mempool.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <stdarg.h>
38 #include <unistd.h>
39 #include <inttypes.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_common.h>
44 #include <rte_log.h>
45 #include <rte_debug.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
49 #include <rte_atomic.h>
50 #include <rte_launch.h>
51 #include <rte_eal.h>
52 #include <rte_eal_memconfig.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_ring.h>
57 #include <rte_errno.h>
58 #include <rte_string_fns.h>
59 #include <rte_spinlock.h>
60
61 #include "rte_mempool.h"
62
63 TAILQ_HEAD(rte_mempool_list, rte_tailq_entry);
64
65 static struct rte_tailq_elem rte_mempool_tailq = {
66         .name = "RTE_MEMPOOL",
67 };
68 EAL_REGISTER_TAILQ(rte_mempool_tailq)
69
70 #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
71 #define CALC_CACHE_FLUSHTHRESH(c)       \
72         ((typeof(c))((c) * CACHE_FLUSHTHRESH_MULTIPLIER))
73
74 /*
75  * return the greatest common divisor between a and b (fast algorithm)
76  *
77  */
78 static unsigned get_gcd(unsigned a, unsigned b)
79 {
80         unsigned c;
81
82         if (0 == a)
83                 return b;
84         if (0 == b)
85                 return a;
86
87         if (a < b) {
88                 c = a;
89                 a = b;
90                 b = c;
91         }
92
93         while (b != 0) {
94                 c = a % b;
95                 a = b;
96                 b = c;
97         }
98
99         return a;
100 }
101
102 /*
103  * Depending on memory configuration, objects addresses are spread
104  * between channels and ranks in RAM: the pool allocator will add
105  * padding between objects. This function return the new size of the
106  * object.
107  */
108 static unsigned optimize_object_size(unsigned obj_size)
109 {
110         unsigned nrank, nchan;
111         unsigned new_obj_size;
112
113         /* get number of channels */
114         nchan = rte_memory_get_nchannel();
115         if (nchan == 0)
116                 nchan = 1;
117
118         nrank = rte_memory_get_nrank();
119         if (nrank == 0)
120                 nrank = 1;
121
122         /* process new object size */
123         new_obj_size = (obj_size + RTE_CACHE_LINE_MASK) / RTE_CACHE_LINE_SIZE;
124         while (get_gcd(new_obj_size, nrank * nchan) != 1)
125                 new_obj_size++;
126         return new_obj_size * RTE_CACHE_LINE_SIZE;
127 }
128
129 static void
130 mempool_add_elem(struct rte_mempool *mp, void *obj, uint32_t obj_idx,
131         rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg)
132 {
133         struct rte_mempool_objhdr *hdr;
134
135         obj = (char *)obj + mp->header_size;
136
137         /* set mempool ptr in header */
138         hdr = (struct rte_mempool_objhdr *)((char *)obj - sizeof(*hdr));
139         hdr->mp = mp;
140
141 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
142         hdr->cookie = RTE_MEMPOOL_HEADER_COOKIE2;
143         __mempool_write_trailer_cookie(obj);
144 #endif
145         /* call the initializer */
146         if (obj_init)
147                 obj_init(mp, obj_init_arg, obj, obj_idx);
148
149         /* enqueue in ring */
150         rte_ring_sp_enqueue(mp->ring, obj);
151 }
152
153 uint32_t
154 rte_mempool_obj_iter(void *vaddr, uint32_t elt_num, size_t elt_sz, size_t align,
155         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift,
156         rte_mempool_obj_iter_t obj_iter, void *obj_iter_arg)
157 {
158         uint32_t i, j, k;
159         uint32_t pgn, pgf;
160         uintptr_t end, start, va;
161         uintptr_t pg_sz;
162
163         pg_sz = (uintptr_t)1 << pg_shift;
164         va = (uintptr_t)vaddr;
165
166         i = 0;
167         j = 0;
168
169         while (i != elt_num && j != pg_num) {
170
171                 start = RTE_ALIGN_CEIL(va, align);
172                 end = start + elt_sz;
173
174                 /* index of the first page for the next element. */
175                 pgf = (end >> pg_shift) - (start >> pg_shift);
176
177                 /* index of the last page for the current element. */
178                 pgn = ((end - 1) >> pg_shift) - (start >> pg_shift);
179                 pgn += j;
180
181                 /* do we have enough space left for the element. */
182                 if (pgn >= pg_num)
183                         break;
184
185                 for (k = j;
186                                 k != pgn &&
187                                 paddr[k] + pg_sz == paddr[k + 1];
188                                 k++)
189                         ;
190
191                 /*
192                  * if next pgn chunks of memory physically continuous,
193                  * use it to create next element.
194                  * otherwise, just skip that chunk unused.
195                  */
196                 if (k == pgn) {
197                         if (obj_iter != NULL)
198                                 obj_iter(obj_iter_arg, (void *)start,
199                                         (void *)end, i);
200                         va = end;
201                         j += pgf;
202                         i++;
203                 } else {
204                         va = RTE_ALIGN_CEIL((va + 1), pg_sz);
205                         j++;
206                 }
207         }
208
209         return (i);
210 }
211
212 /*
213  * Populate  mempool with the objects.
214  */
215
216 struct mempool_populate_arg {
217         struct rte_mempool     *mp;
218         rte_mempool_obj_ctor_t *obj_init;
219         void                   *obj_init_arg;
220 };
221
222 static void
223 mempool_obj_populate(void *arg, void *start, void *end, uint32_t idx)
224 {
225         struct mempool_populate_arg *pa = arg;
226
227         mempool_add_elem(pa->mp, start, idx, pa->obj_init, pa->obj_init_arg);
228         pa->mp->elt_va_end = (uintptr_t)end;
229 }
230
231 static void
232 mempool_populate(struct rte_mempool *mp, size_t num, size_t align,
233         rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg)
234 {
235         uint32_t elt_sz;
236         struct mempool_populate_arg arg;
237
238         elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
239         arg.mp = mp;
240         arg.obj_init = obj_init;
241         arg.obj_init_arg = obj_init_arg;
242
243         mp->size = rte_mempool_obj_iter((void *)mp->elt_va_start,
244                 num, elt_sz, align,
245                 mp->elt_pa, mp->pg_num, mp->pg_shift,
246                 mempool_obj_populate, &arg);
247 }
248
249 uint32_t
250 rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags,
251         struct rte_mempool_objsz *sz)
252 {
253         struct rte_mempool_objsz lsz;
254
255         sz = (sz != NULL) ? sz : &lsz;
256
257         /*
258          * In header, we have at least the pointer to the pool, and
259          * optionaly a 64 bits cookie.
260          */
261         sz->header_size = 0;
262         sz->header_size += sizeof(struct rte_mempool *); /* ptr to pool */
263 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
264         sz->header_size += sizeof(uint64_t); /* cookie */
265 #endif
266         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
267                 sz->header_size = RTE_ALIGN_CEIL(sz->header_size,
268                         RTE_CACHE_LINE_SIZE);
269
270         /* trailer contains the cookie in debug mode */
271         sz->trailer_size = 0;
272 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
273         sz->trailer_size += sizeof(uint64_t); /* cookie */
274 #endif
275         /* element size is 8 bytes-aligned at least */
276         sz->elt_size = RTE_ALIGN_CEIL(elt_size, sizeof(uint64_t));
277
278         /* expand trailer to next cache line */
279         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
280                 sz->total_size = sz->header_size + sz->elt_size +
281                         sz->trailer_size;
282                 sz->trailer_size += ((RTE_CACHE_LINE_SIZE -
283                                   (sz->total_size & RTE_CACHE_LINE_MASK)) &
284                                  RTE_CACHE_LINE_MASK);
285         }
286
287         /*
288          * increase trailer to add padding between objects in order to
289          * spread them across memory channels/ranks
290          */
291         if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
292                 unsigned new_size;
293                 new_size = optimize_object_size(sz->header_size + sz->elt_size +
294                         sz->trailer_size);
295                 sz->trailer_size = new_size - sz->header_size - sz->elt_size;
296         }
297
298         if (! rte_eal_has_hugepages()) {
299                 /*
300                  * compute trailer size so that pool elements fit exactly in
301                  * a standard page
302                  */
303                 int page_size = getpagesize();
304                 int new_size = page_size - sz->header_size - sz->elt_size;
305                 if (new_size < 0 || (unsigned int)new_size < sz->trailer_size) {
306                         printf("When hugepages are disabled, pool objects "
307                                "can't exceed PAGE_SIZE: %d + %d + %d > %d\n",
308                                sz->header_size, sz->elt_size, sz->trailer_size,
309                                page_size);
310                         return 0;
311                 }
312                 sz->trailer_size = new_size;
313         }
314
315         /* this is the size of an object, including header and trailer */
316         sz->total_size = sz->header_size + sz->elt_size + sz->trailer_size;
317
318         return (sz->total_size);
319 }
320
321
322 /*
323  * Calculate maximum amount of memory required to store given number of objects.
324  */
325 size_t
326 rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz, uint32_t pg_shift)
327 {
328         size_t n, pg_num, pg_sz, sz;
329
330         pg_sz = (size_t)1 << pg_shift;
331
332         if ((n = pg_sz / elt_sz) > 0) {
333                 pg_num = (elt_num + n - 1) / n;
334                 sz = pg_num << pg_shift;
335         } else {
336                 sz = RTE_ALIGN_CEIL(elt_sz, pg_sz) * elt_num;
337         }
338
339         return (sz);
340 }
341
342 /*
343  * Calculate how much memory would be actually required with the
344  * given memory footprint to store required number of elements.
345  */
346 static void
347 mempool_lelem_iter(void *arg, __rte_unused void *start, void *end,
348         __rte_unused uint32_t idx)
349 {
350         *(uintptr_t *)arg = (uintptr_t)end;
351 }
352
353 ssize_t
354 rte_mempool_xmem_usage(void *vaddr, uint32_t elt_num, size_t elt_sz,
355         const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
356 {
357         uint32_t n;
358         uintptr_t va, uv;
359         size_t pg_sz, usz;
360
361         pg_sz = (size_t)1 << pg_shift;
362         va = (uintptr_t)vaddr;
363         uv = va;
364
365         if ((n = rte_mempool_obj_iter(vaddr, elt_num, elt_sz, 1,
366                         paddr, pg_num, pg_shift, mempool_lelem_iter,
367                         &uv)) != elt_num) {
368                 return (-(ssize_t)n);
369         }
370
371         uv = RTE_ALIGN_CEIL(uv, pg_sz);
372         usz = uv - va;
373         return (usz);
374 }
375
376 /* create the mempool */
377 struct rte_mempool *
378 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
379                    unsigned cache_size, unsigned private_data_size,
380                    rte_mempool_ctor_t *mp_init, void *mp_init_arg,
381                    rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
382                    int socket_id, unsigned flags)
383 {
384 #ifdef RTE_LIBRTE_XEN_DOM0
385         return (rte_dom0_mempool_create(name, n, elt_size,
386                 cache_size, private_data_size,
387                 mp_init, mp_init_arg,
388                 obj_init, obj_init_arg,
389                 socket_id, flags));
390 #else
391         return (rte_mempool_xmem_create(name, n, elt_size,
392                 cache_size, private_data_size,
393                 mp_init, mp_init_arg,
394                 obj_init, obj_init_arg,
395                 socket_id, flags,
396                 NULL, NULL, MEMPOOL_PG_NUM_DEFAULT, MEMPOOL_PG_SHIFT_MAX));
397 #endif
398 }
399
400 /*
401  * Create the mempool over already allocated chunk of memory.
402  * That external memory buffer can consists of physically disjoint pages.
403  * Setting vaddr to NULL, makes mempool to fallback to original behaviour
404  * and allocate space for mempool and it's elements as one big chunk of
405  * physically continuos memory.
406  * */
407 struct rte_mempool *
408 rte_mempool_xmem_create(const char *name, unsigned n, unsigned elt_size,
409                 unsigned cache_size, unsigned private_data_size,
410                 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
411                 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
412                 int socket_id, unsigned flags, void *vaddr,
413                 const phys_addr_t paddr[], uint32_t pg_num, uint32_t pg_shift)
414 {
415         char mz_name[RTE_MEMZONE_NAMESIZE];
416         char rg_name[RTE_RING_NAMESIZE];
417         struct rte_mempool_list *mempool_list;
418         struct rte_mempool *mp = NULL;
419         struct rte_tailq_entry *te;
420         struct rte_ring *r;
421         const struct rte_memzone *mz;
422         size_t mempool_size;
423         int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
424         int rg_flags = 0;
425         void *obj;
426         struct rte_mempool_objsz objsz;
427         void *startaddr;
428         int page_size = getpagesize();
429
430         /* compilation-time checks */
431         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
432                           RTE_CACHE_LINE_MASK) != 0);
433 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
434         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
435                           RTE_CACHE_LINE_MASK) != 0);
436         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, local_cache) &
437                           RTE_CACHE_LINE_MASK) != 0);
438 #endif
439 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
440         RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
441                           RTE_CACHE_LINE_MASK) != 0);
442         RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
443                           RTE_CACHE_LINE_MASK) != 0);
444 #endif
445
446         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
447
448         /* asked cache too big */
449         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE ||
450             CALC_CACHE_FLUSHTHRESH(cache_size) > n) {
451                 rte_errno = EINVAL;
452                 return NULL;
453         }
454
455         /* check that we have both VA and PA */
456         if (vaddr != NULL && paddr == NULL) {
457                 rte_errno = EINVAL;
458                 return NULL;
459         }
460
461         /* Check that pg_num and pg_shift parameters are valid. */
462         if (pg_num < RTE_DIM(mp->elt_pa) || pg_shift > MEMPOOL_PG_SHIFT_MAX) {
463                 rte_errno = EINVAL;
464                 return NULL;
465         }
466
467         /* "no cache align" imply "no spread" */
468         if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
469                 flags |= MEMPOOL_F_NO_SPREAD;
470
471         /* ring flags */
472         if (flags & MEMPOOL_F_SP_PUT)
473                 rg_flags |= RING_F_SP_ENQ;
474         if (flags & MEMPOOL_F_SC_GET)
475                 rg_flags |= RING_F_SC_DEQ;
476
477         /* calculate mempool object sizes. */
478         if (!rte_mempool_calc_obj_size(elt_size, flags, &objsz)) {
479                 rte_errno = EINVAL;
480                 return NULL;
481         }
482
483         rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
484
485         /* allocate the ring that will be used to store objects */
486         /* Ring functions will return appropriate errors if we are
487          * running as a secondary process etc., so no checks made
488          * in this function for that condition */
489         snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT, name);
490         r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
491         if (r == NULL)
492                 goto exit;
493
494         /*
495          * reserve a memory zone for this mempool: private data is
496          * cache-aligned
497          */
498         private_data_size = (private_data_size +
499                              RTE_CACHE_LINE_MASK) & (~RTE_CACHE_LINE_MASK);
500
501         if (! rte_eal_has_hugepages()) {
502                 /*
503                  * expand private data size to a whole page, so that the
504                  * first pool element will start on a new standard page
505                  */
506                 int head = sizeof(struct rte_mempool);
507                 int new_size = (private_data_size + head) % page_size;
508                 if (new_size) {
509                         private_data_size += page_size - new_size;
510                 }
511         }
512
513         /* try to allocate tailq entry */
514         te = rte_zmalloc("MEMPOOL_TAILQ_ENTRY", sizeof(*te), 0);
515         if (te == NULL) {
516                 RTE_LOG(ERR, MEMPOOL, "Cannot allocate tailq entry!\n");
517                 goto exit;
518         }
519
520         /*
521          * If user provided an external memory buffer, then use it to
522          * store mempool objects. Otherwise reserve memzone big enough to
523          * hold mempool header and metadata plus mempool objects.
524          */
525         mempool_size = MEMPOOL_HEADER_SIZE(mp, pg_num) + private_data_size;
526         if (vaddr == NULL)
527                 mempool_size += (size_t)objsz.total_size * n;
528
529         if (! rte_eal_has_hugepages()) {
530                 /*
531                  * we want the memory pool to start on a page boundary,
532                  * because pool elements crossing page boundaries would
533                  * result in discontiguous physical addresses
534                  */
535                 mempool_size += page_size;
536         }
537
538         snprintf(mz_name, sizeof(mz_name), RTE_MEMPOOL_MZ_FORMAT, name);
539
540         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
541
542         /*
543          * no more memory: in this case we loose previously reserved
544          * space for the as we cannot free it
545          */
546         if (mz == NULL) {
547                 rte_free(te);
548                 goto exit;
549         }
550
551         if (rte_eal_has_hugepages()) {
552                 startaddr = (void*)mz->addr;
553         } else {
554                 /* align memory pool start address on a page boundary */
555                 unsigned long addr = (unsigned long)mz->addr;
556                 if (addr & (page_size - 1)) {
557                         addr += page_size;
558                         addr &= ~(page_size - 1);
559                 }
560                 startaddr = (void*)addr;
561         }
562
563         /* init the mempool structure */
564         mp = startaddr;
565         memset(mp, 0, sizeof(*mp));
566         snprintf(mp->name, sizeof(mp->name), "%s", name);
567         mp->phys_addr = mz->phys_addr;
568         mp->ring = r;
569         mp->size = n;
570         mp->flags = flags;
571         mp->elt_size = objsz.elt_size;
572         mp->header_size = objsz.header_size;
573         mp->trailer_size = objsz.trailer_size;
574         mp->cache_size = cache_size;
575         mp->cache_flushthresh = CALC_CACHE_FLUSHTHRESH(cache_size);
576         mp->private_data_size = private_data_size;
577
578         /* calculate address of the first element for continuous mempool. */
579         obj = (char *)mp + MEMPOOL_HEADER_SIZE(mp, pg_num) +
580                 private_data_size;
581
582         /* populate address translation fields. */
583         mp->pg_num = pg_num;
584         mp->pg_shift = pg_shift;
585         mp->pg_mask = RTE_LEN2MASK(mp->pg_shift, typeof(mp->pg_mask));
586
587         /* mempool elements allocated together with mempool */
588         if (vaddr == NULL) {
589                 mp->elt_va_start = (uintptr_t)obj;
590                 mp->elt_pa[0] = mp->phys_addr +
591                         (mp->elt_va_start - (uintptr_t)mp);
592
593         /* mempool elements in a separate chunk of memory. */
594         } else {
595                 mp->elt_va_start = (uintptr_t)vaddr;
596                 memcpy(mp->elt_pa, paddr, sizeof (mp->elt_pa[0]) * pg_num);
597         }
598
599         mp->elt_va_end = mp->elt_va_start;
600
601         /* call the initializer */
602         if (mp_init)
603                 mp_init(mp, mp_init_arg);
604
605         mempool_populate(mp, n, 1, obj_init, obj_init_arg);
606
607         te->data = (void *) mp;
608
609         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
610         TAILQ_INSERT_TAIL(mempool_list, te, next);
611         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
612
613 exit:
614         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
615
616         return mp;
617 }
618
619 /* Return the number of entries in the mempool */
620 unsigned
621 rte_mempool_count(const struct rte_mempool *mp)
622 {
623         unsigned count;
624
625         count = rte_ring_count(mp->ring);
626
627 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
628         {
629                 unsigned lcore_id;
630                 if (mp->cache_size == 0)
631                         return count;
632
633                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
634                         count += mp->local_cache[lcore_id].len;
635         }
636 #endif
637
638         /*
639          * due to race condition (access to len is not locked), the
640          * total can be greater than size... so fix the result
641          */
642         if (count > mp->size)
643                 return mp->size;
644         return count;
645 }
646
647 /* dump the cache status */
648 static unsigned
649 rte_mempool_dump_cache(FILE *f, const struct rte_mempool *mp)
650 {
651 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
652         unsigned lcore_id;
653         unsigned count = 0;
654         unsigned cache_count;
655
656         fprintf(f, "  cache infos:\n");
657         fprintf(f, "    cache_size=%"PRIu32"\n", mp->cache_size);
658         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
659                 cache_count = mp->local_cache[lcore_id].len;
660                 fprintf(f, "    cache_count[%u]=%u\n", lcore_id, cache_count);
661                 count += cache_count;
662         }
663         fprintf(f, "    total_cache_count=%u\n", count);
664         return count;
665 #else
666         RTE_SET_USED(mp);
667         fprintf(f, "  cache disabled\n");
668         return 0;
669 #endif
670 }
671
672 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
673 /* check cookies before and after objects */
674 #ifndef __INTEL_COMPILER
675 #pragma GCC diagnostic ignored "-Wcast-qual"
676 #endif
677
678 struct mempool_audit_arg {
679         const struct rte_mempool *mp;
680         uintptr_t obj_end;
681         uint32_t obj_num;
682 };
683
684 static void
685 mempool_obj_audit(void *arg, void *start, void *end, uint32_t idx)
686 {
687         struct mempool_audit_arg *pa = arg;
688         void *obj;
689
690         obj = (char *)start + pa->mp->header_size;
691         pa->obj_end = (uintptr_t)end;
692         pa->obj_num = idx + 1;
693         __mempool_check_cookies(pa->mp, &obj, 1, 2);
694 }
695
696 static void
697 mempool_audit_cookies(const struct rte_mempool *mp)
698 {
699         uint32_t elt_sz, num;
700         struct mempool_audit_arg arg;
701
702         elt_sz = mp->elt_size + mp->header_size + mp->trailer_size;
703
704         arg.mp = mp;
705         arg.obj_end = mp->elt_va_start;
706         arg.obj_num = 0;
707
708         num = rte_mempool_obj_iter((void *)mp->elt_va_start,
709                 mp->size, elt_sz, 1,
710                 mp->elt_pa, mp->pg_num, mp->pg_shift,
711                 mempool_obj_audit, &arg);
712
713         if (num != mp->size) {
714                         rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
715                         "iterated only over %u elements\n",
716                         mp, mp->size, num);
717         } else if (arg.obj_end != mp->elt_va_end || arg.obj_num != mp->size) {
718                         rte_panic("rte_mempool_obj_iter(mempool=%p, size=%u) "
719                         "last callback va_end: %#tx (%#tx expeceted), "
720                         "num of objects: %u (%u expected)\n",
721                         mp, mp->size,
722                         arg.obj_end, mp->elt_va_end,
723                         arg.obj_num, mp->size);
724         }
725 }
726
727 #ifndef __INTEL_COMPILER
728 #pragma GCC diagnostic error "-Wcast-qual"
729 #endif
730 #else
731 #define mempool_audit_cookies(mp) do {} while(0)
732 #endif
733
734 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
735 /* check cookies before and after objects */
736 static void
737 mempool_audit_cache(const struct rte_mempool *mp)
738 {
739         /* check cache size consistency */
740         unsigned lcore_id;
741         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
742                 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
743                         RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
744                                 lcore_id);
745                         rte_panic("MEMPOOL: invalid cache len\n");
746                 }
747         }
748 }
749 #else
750 #define mempool_audit_cache(mp) do {} while(0)
751 #endif
752
753
754 /* check the consistency of mempool (size, cookies, ...) */
755 void
756 rte_mempool_audit(const struct rte_mempool *mp)
757 {
758         mempool_audit_cache(mp);
759         mempool_audit_cookies(mp);
760
761         /* For case where mempool DEBUG is not set, and cache size is 0 */
762         RTE_SET_USED(mp);
763 }
764
765 /* dump the status of the mempool on the console */
766 void
767 rte_mempool_dump(FILE *f, const struct rte_mempool *mp)
768 {
769 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
770         struct rte_mempool_debug_stats sum;
771         unsigned lcore_id;
772 #endif
773         unsigned common_count;
774         unsigned cache_count;
775
776         RTE_VERIFY(f != NULL);
777         RTE_VERIFY(mp != NULL);
778
779         fprintf(f, "mempool <%s>@%p\n", mp->name, mp);
780         fprintf(f, "  flags=%x\n", mp->flags);
781         fprintf(f, "  ring=<%s>@%p\n", mp->ring->name, mp->ring);
782         fprintf(f, "  phys_addr=0x%" PRIx64 "\n", mp->phys_addr);
783         fprintf(f, "  size=%"PRIu32"\n", mp->size);
784         fprintf(f, "  header_size=%"PRIu32"\n", mp->header_size);
785         fprintf(f, "  elt_size=%"PRIu32"\n", mp->elt_size);
786         fprintf(f, "  trailer_size=%"PRIu32"\n", mp->trailer_size);
787         fprintf(f, "  total_obj_size=%"PRIu32"\n",
788                mp->header_size + mp->elt_size + mp->trailer_size);
789
790         fprintf(f, "  private_data_size=%"PRIu32"\n", mp->private_data_size);
791         fprintf(f, "  pg_num=%"PRIu32"\n", mp->pg_num);
792         fprintf(f, "  pg_shift=%"PRIu32"\n", mp->pg_shift);
793         fprintf(f, "  pg_mask=%#tx\n", mp->pg_mask);
794         fprintf(f, "  elt_va_start=%#tx\n", mp->elt_va_start);
795         fprintf(f, "  elt_va_end=%#tx\n", mp->elt_va_end);
796         fprintf(f, "  elt_pa[0]=0x%" PRIx64 "\n", mp->elt_pa[0]);
797
798         if (mp->size != 0)
799                 fprintf(f, "  avg bytes/object=%#Lf\n",
800                         (long double)(mp->elt_va_end - mp->elt_va_start) /
801                         mp->size);
802
803         cache_count = rte_mempool_dump_cache(f, mp);
804         common_count = rte_ring_count(mp->ring);
805         if ((cache_count + common_count) > mp->size)
806                 common_count = mp->size - cache_count;
807         fprintf(f, "  common_pool_count=%u\n", common_count);
808
809         /* sum and dump statistics */
810 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
811         memset(&sum, 0, sizeof(sum));
812         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
813                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
814                 sum.put_objs += mp->stats[lcore_id].put_objs;
815                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
816                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
817                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
818                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
819         }
820         fprintf(f, "  stats:\n");
821         fprintf(f, "    put_bulk=%"PRIu64"\n", sum.put_bulk);
822         fprintf(f, "    put_objs=%"PRIu64"\n", sum.put_objs);
823         fprintf(f, "    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
824         fprintf(f, "    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
825         fprintf(f, "    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
826         fprintf(f, "    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
827 #else
828         fprintf(f, "  no statistics available\n");
829 #endif
830
831         rte_mempool_audit(mp);
832 }
833
834 /* dump the status of all mempools on the console */
835 void
836 rte_mempool_list_dump(FILE *f)
837 {
838         const struct rte_mempool *mp = NULL;
839         struct rte_tailq_entry *te;
840         struct rte_mempool_list *mempool_list;
841
842         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
843
844         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
845
846         TAILQ_FOREACH(te, mempool_list, next) {
847                 mp = (struct rte_mempool *) te->data;
848                 rte_mempool_dump(f, mp);
849         }
850
851         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
852 }
853
854 /* search a mempool from its name */
855 struct rte_mempool *
856 rte_mempool_lookup(const char *name)
857 {
858         struct rte_mempool *mp = NULL;
859         struct rte_tailq_entry *te;
860         struct rte_mempool_list *mempool_list;
861
862         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
863
864         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
865
866         TAILQ_FOREACH(te, mempool_list, next) {
867                 mp = (struct rte_mempool *) te->data;
868                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
869                         break;
870         }
871
872         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
873
874         if (te == NULL) {
875                 rte_errno = ENOENT;
876                 return NULL;
877         }
878
879         return mp;
880 }
881
882 void rte_mempool_walk(void (*func)(const struct rte_mempool *, void *),
883                       void *arg)
884 {
885         struct rte_tailq_entry *te = NULL;
886         struct rte_mempool_list *mempool_list;
887
888         mempool_list = RTE_TAILQ_CAST(rte_mempool_tailq.head, rte_mempool_list);
889
890         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
891
892         TAILQ_FOREACH(te, mempool_list, next) {
893                 (*func)((struct rte_mempool *) te->data, arg);
894         }
895
896         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
897 }