memory: fix for multi process support
[dpdk.git] / lib / librte_mempool / rte_mempool.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <stdarg.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_atomic.h>
49 #include <rte_launch.h>
50 #include <rte_tailq.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_mempool);
64
65 #define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
66
67 /*
68  * return the greatest common divisor between a and b (fast algorithm)
69  *
70  */
71 static unsigned get_gcd(unsigned a, unsigned b)
72 {
73         unsigned c;
74
75         if (0 == a)
76                 return b;
77         if (0 == b)
78                 return a;
79
80         if (a < b) {
81                 c = a;
82                 a = b;
83                 b = c;
84         }
85
86         while (b != 0) {
87                 c = a % b;
88                 a = b;
89                 b = c;
90         }
91
92         return a;
93 }
94
95 /*
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
99  * object.
100  */
101 static unsigned optimize_object_size(unsigned obj_size)
102 {
103         unsigned nrank, nchan;
104         unsigned new_obj_size;
105
106         /* get number of channels */
107         nchan = rte_memory_get_nchannel();
108         if (nchan == 0)
109                 nchan = 1;
110
111         nrank = rte_memory_get_nrank();
112         if (nrank == 0)
113                 nrank = 1;
114
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)
119                 new_obj_size++;
120         return new_obj_size * CACHE_LINE_SIZE;
121 }
122
123 /* create the mempool */
124 struct rte_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)
130 {
131         char mz_name[RTE_MEMZONE_NAMESIZE];
132         char rg_name[RTE_RING_NAMESIZE];
133         struct rte_mempool *mp = NULL;
134         struct rte_ring *r;
135         const struct rte_memzone *mz;
136         size_t mempool_size;
137         int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
138         int rg_flags = 0;
139         uint32_t header_size, trailer_size;
140         uint32_t total_elt_size;
141         unsigned i;
142         void *obj;
143
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);
152 #endif
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);
158 #endif
159
160         /* check that we have an initialised tail queue */
161         if (RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL, rte_mempool_list) == NULL) {
162                 rte_errno = E_RTE_NO_TAILQ;
163                 return NULL;    
164         }
165         
166         /* asked cache too big */
167         if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE){
168                 rte_errno = EINVAL;
169                 return NULL;
170         }
171
172         /* "no cache align" imply "no spread" */
173         if (flags & MEMPOOL_F_NO_CACHE_ALIGN)
174                 flags |= MEMPOOL_F_NO_SPREAD;
175
176         /* ring flags */
177         if (flags & MEMPOOL_F_SP_PUT)
178                 rg_flags |= RING_F_SP_ENQ;
179         if (flags & MEMPOOL_F_SC_GET)
180                 rg_flags |= RING_F_SC_DEQ;
181
182         rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
183
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);
190         if (r == NULL)
191                 goto exit;
192
193         /*
194          * In header, we have at least the pointer to the pool, and
195          * optionaly a 64 bits cookie.
196          */
197         header_size = 0;
198         header_size += sizeof(struct rte_mempool *); /* ptr to pool */
199 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
200         header_size += sizeof(uint64_t); /* cookie */
201 #endif
202         if ((flags & MEMPOOL_F_NO_CACHE_ALIGN) == 0)
203                 header_size = (header_size + CACHE_LINE_MASK) & (~CACHE_LINE_MASK);
204
205         /* trailer contains the cookie in debug mode */
206         trailer_size = 0;
207 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
208         trailer_size += sizeof(uint64_t); /* cookie */
209 #endif
210         /* element size is 8 bytes-aligned at least */
211         elt_size = (elt_size + 7) & (~7);
212
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)) &
218                                  CACHE_LINE_MASK);
219         }
220
221         /*
222          * increase trailer to add padding between objects in order to
223          * spread them accross memory channels/ranks
224          */
225         if ((flags & MEMPOOL_F_NO_SPREAD) == 0) {
226                 unsigned new_size;
227                 new_size = optimize_object_size(header_size + elt_size +
228                                                 trailer_size);
229                 trailer_size = new_size - header_size - elt_size;
230         }
231
232         /* this is the size of an object, including header and trailer */
233         total_elt_size = header_size + elt_size + trailer_size;
234
235         /* reserve a memory zone for this mempool: private data is
236          * cache-aligned */
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
243         mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
244
245         /*
246          * no more memory: in this case we loose previously reserved
247          * space for the as we cannot free it
248          */
249         if (mz == NULL)
250                 goto exit;
251
252         /* init the mempool structure */
253         mp = mz->addr;
254         memset(mp, 0, sizeof(*mp));
255         rte_snprintf(mp->name, sizeof(mp->name), "%s", name);
256         mp->phys_addr = mz->phys_addr;
257         mp->ring = r;
258         mp->size = n;
259         mp->flags = flags;
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->cache_flushthresh = (uint32_t)(cache_size * CACHE_FLUSHTHRESH_MULTIPLIER);
265         mp->private_data_size = private_data_size;
266
267         /* call the initializer */
268         if (mp_init)
269                 mp_init(mp, mp_init_arg);
270
271         /* fill the headers and trailers, and add objects in ring */
272         obj = (char *)mp + sizeof(struct rte_mempool) + private_data_size;
273         for (i = 0; i < n; i++) {
274                 struct rte_mempool **mpp;
275                 obj = (char *)obj + header_size;
276
277                 /* set mempool ptr in header */
278                 mpp = __mempool_from_obj(obj);
279                 *mpp = mp;
280
281 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
282                 __mempool_write_header_cookie(obj, 1);
283                 __mempool_write_trailer_cookie(obj);
284 #endif
285                 /* call the initializer */
286                 if (obj_init)
287                         obj_init(mp, obj_init_arg, obj, i);
288
289                 /* enqueue in ring */
290                 rte_ring_sp_enqueue(mp->ring, obj);
291                 obj = (char *)obj + elt_size + trailer_size;
292         }
293
294         RTE_EAL_TAILQ_INSERT_TAIL(RTE_TAILQ_MEMPOOL, rte_mempool_list, mp);
295
296 exit:
297         rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
298
299         return mp;
300 }
301
302 /* Return the number of entries in the mempool */
303 unsigned
304 rte_mempool_count(const struct rte_mempool *mp)
305 {
306         unsigned count;
307
308         count = rte_ring_count(mp->ring);
309
310 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
311         {
312                 unsigned lcore_id;
313                 if (mp->cache_size == 0)
314                         return count;
315
316                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
317                         count += mp->local_cache[lcore_id].len;
318         }
319 #endif
320
321         /*
322          * due to race condition (access to len is not locked), the
323          * total can be greater than size... so fix the result
324          */
325         if (count > mp->size)
326                 return mp->size;
327         return count;
328 }
329
330 /* dump the cache status */
331 static unsigned
332 rte_mempool_dump_cache(const struct rte_mempool *mp)
333 {
334 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
335         unsigned lcore_id;
336         unsigned count = 0;
337         unsigned cache_count;
338
339         printf("  cache infos:\n");
340         printf("    cache_size=%"PRIu32"\n", mp->cache_size);
341         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
342                 cache_count = mp->local_cache[lcore_id].len;
343                 printf("    cache_count[%u]=%u\n", lcore_id, cache_count);
344                 count += cache_count;
345         }
346         printf("    total_cache_count=%u\n", count);
347         return count;
348 #else
349         RTE_SET_USED(mp);
350         printf("  cache disabled\n");
351         return 0;
352 #endif
353 }
354
355 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
356 /* check cookies before and after objects */
357 #ifndef __INTEL_COMPILER
358 #pragma GCC diagnostic ignored "-Wcast-qual"
359 #endif
360 static void
361 mempool_audit_cookies(const struct rte_mempool *mp)
362 {
363         unsigned i;
364         void *obj;
365         void * const *obj_table;
366
367         obj = (char *)mp + sizeof(struct rte_mempool) + mp->private_data_size;
368         for (i = 0; i < mp->size; i++) {
369                 obj = (char *)obj + mp->header_size;
370                 obj_table = &obj;
371                 __mempool_check_cookies(mp, obj_table, 1, 2);
372                 obj = (char *)obj + mp->elt_size + mp->trailer_size;
373         }
374 }
375 #ifndef __INTEL_COMPILER
376 #pragma GCC diagnostic error "-Wcast-qual"
377 #endif
378 #else
379 #define mempool_audit_cookies(mp) do {} while(0)
380 #endif
381
382 #if RTE_MEMPOOL_CACHE_MAX_SIZE > 0
383 /* check cookies before and after objects */
384 static void
385 mempool_audit_cache(const struct rte_mempool *mp)
386 {
387         /* check cache size consistency */
388         unsigned lcore_id;
389         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
390                 if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
391                         RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
392                                 lcore_id);
393                         rte_panic("MEMPOOL: invalid cache len\n");
394                 }
395         }
396 }
397 #else
398 #define mempool_audit_cache(mp) do {} while(0)
399 #endif
400
401
402 /* check the consistency of mempool (size, cookies, ...) */
403 void
404 rte_mempool_audit(const struct rte_mempool *mp)
405 {
406         mempool_audit_cache(mp);
407         mempool_audit_cookies(mp);
408
409         /* For case where mempool DEBUG is not set, and cache size is 0 */
410         RTE_SET_USED(mp);
411 }
412
413 /* dump the status of the mempool on the console */
414 void
415 rte_mempool_dump(const struct rte_mempool *mp)
416 {
417 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
418         struct rte_mempool_debug_stats sum;
419         unsigned lcore_id;
420 #endif
421         unsigned common_count;
422         unsigned cache_count;
423
424         printf("mempool <%s>@%p\n", mp->name, mp);
425         printf("  flags=%x\n", mp->flags);
426         printf("  ring=<%s>@%p\n", mp->ring->name, mp->ring);
427         printf("  size=%"PRIu32"\n", mp->size);
428         printf("  header_size=%"PRIu32"\n", mp->header_size);
429         printf("  elt_size=%"PRIu32"\n", mp->elt_size);
430         printf("  trailer_size=%"PRIu32"\n", mp->trailer_size);
431         printf("  total_obj_size=%"PRIu32"\n",
432                mp->header_size + mp->elt_size + mp->trailer_size);
433
434         cache_count = rte_mempool_dump_cache(mp);
435         common_count = rte_ring_count(mp->ring);
436         if ((cache_count + common_count) > mp->size)
437                 common_count = mp->size - cache_count;
438         printf("  common_pool_count=%u\n", common_count);
439
440         /* sum and dump statistics */
441 #ifdef RTE_LIBRTE_MEMPOOL_DEBUG
442         memset(&sum, 0, sizeof(sum));
443         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
444                 sum.put_bulk += mp->stats[lcore_id].put_bulk;
445                 sum.put_objs += mp->stats[lcore_id].put_objs;
446                 sum.get_success_bulk += mp->stats[lcore_id].get_success_bulk;
447                 sum.get_success_objs += mp->stats[lcore_id].get_success_objs;
448                 sum.get_fail_bulk += mp->stats[lcore_id].get_fail_bulk;
449                 sum.get_fail_objs += mp->stats[lcore_id].get_fail_objs;
450         }
451         printf("  stats:\n");
452         printf("    put_bulk=%"PRIu64"\n", sum.put_bulk);
453         printf("    put_objs=%"PRIu64"\n", sum.put_objs);
454         printf("    get_success_bulk=%"PRIu64"\n", sum.get_success_bulk);
455         printf("    get_success_objs=%"PRIu64"\n", sum.get_success_objs);
456         printf("    get_fail_bulk=%"PRIu64"\n", sum.get_fail_bulk);
457         printf("    get_fail_objs=%"PRIu64"\n", sum.get_fail_objs);
458 #else
459         printf("  no statistics available\n");
460 #endif
461
462         rte_mempool_audit(mp);
463 }
464
465 /* dump the status of all mempools on the console */
466 void
467 rte_mempool_list_dump(void)
468 {
469         const struct rte_mempool *mp = NULL;
470         struct rte_mempool_list *mempool_list;
471
472         if ((mempool_list = 
473              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL, rte_mempool_list)) == NULL) {
474                 rte_errno = E_RTE_NO_TAILQ;
475                 return; 
476         }
477
478         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
479
480         TAILQ_FOREACH(mp, mempool_list, next) {
481                 rte_mempool_dump(mp);
482         }
483
484         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
485 }
486
487 /* search a mempool from its name */
488 struct rte_mempool *
489 rte_mempool_lookup(const char *name)
490 {
491         struct rte_mempool *mp = NULL;
492         struct rte_mempool_list *mempool_list;
493
494         if ((mempool_list = 
495              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL, rte_mempool_list)) == NULL) {
496                 rte_errno = E_RTE_NO_TAILQ;
497                 return NULL;
498         }
499
500         rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
501
502         TAILQ_FOREACH(mp, mempool_list, next) {
503                 if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
504                         break;
505         }
506         
507         rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
508         
509         if (mp == NULL)
510                 rte_errno = ENOENT;
511
512         return mp;
513 }