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