4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
33 * version: DPDK.L.1.2.3-3
42 #include <sys/queue.h>
44 #include <rte_common.h>
46 #include <rte_debug.h>
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
49 #include <rte_atomic.h>
50 #include <rte_launch.h>
51 #include <rte_tailq.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
55 #include <rte_branch_prediction.h>
57 #include <rte_errno.h>
58 #include <rte_string_fns.h>
60 #include "rte_mempool.h"
62 TAILQ_HEAD(rte_mempool_list, rte_mempool);
64 /* global list of mempool (used for debug/dump) */
65 static struct rte_mempool_list *mempool_list;
68 * return the greatest common divisor between a and b (fast algorithm)
71 static unsigned get_gcd(unsigned a, unsigned b)
96 * Depending on memory configuration, objects addresses are spreaded
97 * between channels and ranks in RAM: the pool allocator will add
98 * padding between objects. This function return the new size of the
101 static unsigned optimize_object_size(unsigned obj_size)
103 unsigned nrank, nchan;
104 unsigned new_obj_size;
106 /* get number of channels */
107 nchan = rte_memory_get_nchannel();
111 nrank = rte_memory_get_nrank();
115 /* process new object size */
116 new_obj_size = (obj_size + CACHE_LINE_MASK) / CACHE_LINE_SIZE;
117 while (get_gcd(new_obj_size, nrank * nchan) != 1 ||
118 get_gcd(nchan, new_obj_size) != 1)
120 return new_obj_size * CACHE_LINE_SIZE;
123 /* create the mempool */
125 rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
126 unsigned cache_size, unsigned private_data_size,
127 rte_mempool_ctor_t *mp_init, void *mp_init_arg,
128 rte_mempool_obj_ctor_t *obj_init, void *obj_init_arg,
129 int socket_id, unsigned flags)
131 char mz_name[RTE_MEMZONE_NAMESIZE];
132 char rg_name[RTE_RING_NAMESIZE];
133 struct rte_mempool *mp;
135 const struct rte_memzone *mz;
137 int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
139 uint32_t header_size, trailer_size;
140 uint32_t total_elt_size;
144 /* compilation-time checks */
145 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool) &
146 CACHE_LINE_MASK) != 0);
147 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
148 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_cache) &
149 CACHE_LINE_MASK) != 0);
150 RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, local_cache) &
151 CACHE_LINE_MASK) != 0);
153 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
154 RTE_BUILD_BUG_ON((sizeof(struct rte_mempool_debug_stats) &
155 CACHE_LINE_MASK) != 0);
156 RTE_BUILD_BUG_ON((offsetof(struct rte_mempool, stats) &
157 CACHE_LINE_MASK) != 0);
160 /* check that we have an initialised tail queue */
161 if (mempool_list == NULL)
162 if ((mempool_list = RTE_TAILQ_RESERVE("RTE_MEMPOOL", \
163 rte_mempool_list)) == NULL){
164 rte_errno = E_RTE_NO_TAILQ;
168 /* asked cache too big */
169 if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE){
174 /* "no cache align" imply "no spread" */
175 if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
176 flags |= MEMPOOL_F_NO_SPREAD;
179 if (flags & MEMPOOL_F_SP_PUT)
180 rg_flags |= RING_F_SP_ENQ;
181 if (flags & MEMPOOL_F_SC_GET)
182 rg_flags |= RING_F_SC_DEQ;
184 /* allocate the ring that will be used to store objects */
185 /* Ring functions will return appropriate errors if we are
186 * running as a secondary process etc., so no checks made
187 * in this function for that condition */
188 rte_snprintf(rg_name, sizeof(rg_name), "MP_%s", name);
189 r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
194 * In header, we have at least the pointer to the pool, and
195 * optionaly a 64 bits cookie.
198 header_size += sizeof(struct rte_mempool *); /* ptr to pool */
199 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
200 header_size += sizeof(uint64_t); /* cookie */
202 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
203 header_size = (header_size + CACHE_LINE_MASK) & (~CACHE_LINE_MASK);
205 /* trailer contains the cookie in debug mode */
207 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
208 trailer_size += sizeof(uint64_t); /* cookie */
210 /* element size is 8 bytes-aligned at least */
211 elt_size = (elt_size + 7) & (~7);
213 /* expand trailer to next cache line */
214 if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0) {
215 total_elt_size = header_size + elt_size + trailer_size;
216 trailer_size += ((CACHE_LINE_SIZE -
217 (total_elt_size & CACHE_LINE_MASK)) &
222 * increase trailer to add padding between objects in order to
223 * spread them accross memory channels/ranks
225 if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
227 new_size = optimize_object_size(header_size + elt_size +
229 trailer_size = new_size - header_size - elt_size;
232 /* this is the size of an object, including header and trailer */
233 total_elt_size = header_size + elt_size + trailer_size;
235 /* reserve a memory zone for this mempool: private data is
237 private_data_size = (private_data_size +
238 CACHE_LINE_MASK) & (~CACHE_LINE_MASK);
239 mempool_size = total_elt_size * n +
240 sizeof(struct rte_mempool) + private_data_size;
241 rte_snprintf(mz_name, sizeof(mz_name), "MP_%s", name);
242 mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
245 * no more memory: in this case we loose previously reserved
246 * space for the as we cannot free it
251 /* init the mempool structure */
253 memset(mp, 0, sizeof(*mp));
254 rte_snprintf(mp->name, sizeof(mp->name), "%s", name);
255 mp->phys_addr = mz->phys_addr;
259 mp->bulk_default = 1;
260 mp->elt_size = elt_size;
261 mp->header_size = header_size;
262 mp->trailer_size = trailer_size;
263 mp->cache_size = cache_size;
264 mp->private_data_size = private_data_size;
266 /* call the initializer */
268 mp_init(mp, mp_init_arg);
270 /* fill the headers and trailers, and add objects in ring */
271 obj = (char *)mp + sizeof(struct rte_mempool) + private_data_size;
272 for (i = 0; i < n; i++) {
273 struct rte_mempool **mpp;
274 obj = (char *)obj + header_size;
276 /* set mempool ptr in header */
277 mpp = __mempool_from_obj(obj);
280 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
281 __mempool_write_header_cookie(obj, 1);
282 __mempool_write_trailer_cookie(obj);
284 /* call the initializer */
286 obj_init(mp, obj_init_arg, obj, i);
288 /* enqueue in ring */
289 rte_ring_sp_enqueue(mp->ring, obj);
290 obj = (char *)obj + elt_size + trailer_size;
293 TAILQ_INSERT_TAIL(mempool_list, mp, next);
297 /* Return the number of entries in the mempool */
299 rte_mempool_count(const struct rte_mempool *mp)
303 count = rte_ring_count(mp->ring);
305 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
308 if (mp->cache_size == 0)
311 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
312 count += mp->local_cache[lcore_id].len;
317 * due to race condition (access to len is not locked), the
318 * total can be greater than size... so fix the result
320 if (count > mp->size)
325 /* dump the cache status */
327 rte_mempool_dump_cache(const struct rte_mempool *mp)
329 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
332 unsigned cache_count;
334 printf(" cache infos:\n");
335 printf(" cache_size=%"PRIu32"\n", mp->cache_size);
336 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
337 cache_count = mp->local_cache[lcore_id].len;
338 printf(" cache_count[%u]=%u\n", lcore_id, cache_count);
339 count += cache_count;
341 printf(" total_cache_count=%u\n", count);
345 printf(" cache disabled\n");
350 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
351 /* check cookies before and after objects */
352 #ifndef __INTEL_COMPILER
353 #pragma GCC diagnostic ignored "-Wcast-qual"
356 mempool_audit_cookies(const struct rte_mempool *mp)
360 void * const *obj_table;
362 obj = (char *)mp + sizeof(struct rte_mempool) + mp->private_data_size;
363 for (i = 0; i < mp->size; i++) {
364 obj = (char *)obj + mp->header_size;
366 __mempool_check_cookies(mp, obj_table, 1, 2);
367 obj = (char *)obj + mp->elt_size + mp->trailer_size;
370 #ifndef __INTEL_COMPILER
371 #pragma GCC diagnostic error "-Wcast-qual"
374 #define mempool_audit_cookies(mp) do {} while(0)
377 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
378 /* check cookies before and after objects */
380 mempool_audit_cache(const struct rte_mempool *mp)
382 /* check cache size consistency */
384 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
385 if (mp->local_cache[lcore_id].len > mp->cache_size) {
386 RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
388 rte_panic("MEMPOOL: invalid cache len\n");
393 #define mempool_audit_cache(mp) do {} while(0)
397 /* check the consistency of mempool (size, cookies, ...) */
399 rte_mempool_audit(const struct rte_mempool *mp)
401 mempool_audit_cache(mp);
402 mempool_audit_cookies(mp);
405 /* dump the status of the mempool on the console */
407 rte_mempool_dump(const struct rte_mempool *mp)
409 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
410 struct rte_mempool_debug_stats sum;
413 unsigned common_count;
414 unsigned cache_count;
416 printf("mempool <%s>@%p\n", mp->name, mp);
417 printf(" flags=%x\n", mp->flags);
418 printf(" ring=<%s>@%p\n", mp->ring->name, mp->ring);
419 printf(" size=%"PRIu32"\n", mp->size);
420 printf(" bulk_default=%"PRIu32"\n", mp->bulk_default);
421 printf(" header_size=%"PRIu32"\n", mp->header_size);
422 printf(" elt_size=%"PRIu32"\n", mp->elt_size);
423 printf(" trailer_size=%"PRIu32"\n", mp->trailer_size);
424 printf(" total_obj_size=%"PRIu32"\n",
425 mp->header_size + mp->elt_size + mp->trailer_size);
427 cache_count = rte_mempool_dump_cache(mp);
428 common_count = rte_ring_count(mp->ring);
429 if ((cache_count + common_count) > mp->size)
430 common_count = mp->size - cache_count;
431 printf(" common_pool_count=%u\n", common_count);
433 /* sum and dump statistics */
434 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
435 memset(&sum, 0, sizeof(sum));
436 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
437 sum.put_bulk += mp->stats[lcore_id].put_bulk;
438 sum.put_objs += mp->stats[lcore_id].put_objs;
439 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
440 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
441 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
442 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
445 printf(" put_bulk=%"PRIu64"\n", sum.put_bulk);
446 printf(" put_objs=%"PRIu64"\n", sum.put_objs);
447 printf(" get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
448 printf(" get_success_objs=%"PRIu64"\n", sum.get_success_objs);
449 printf(" get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
450 printf(" get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
452 printf(" no statistics available\n");
455 rte_mempool_audit(mp);
458 /* dump the status of all mempools on the console */
460 rte_mempool_list_dump(void)
462 const struct rte_mempool *mp = NULL;
464 TAILQ_FOREACH(mp, mempool_list, next) {
465 rte_mempool_dump(mp);
469 /* search a mempool from its name */
471 rte_mempool_lookup(const char *name)
473 struct rte_mempool *mp = NULL;
475 /* check that we have an initialised tail queue */
476 if (mempool_list == NULL)
477 if ((mempool_list = RTE_TAILQ_RESERVE("RTE_MEMPOOL", \
478 rte_mempool_list)) == NULL){
479 rte_errno = E_RTE_NO_TAILQ;
483 TAILQ_FOREACH(mp, mempool_list, next) {
484 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)