update Intel copyright years to 2014
[dpdk.git] / lib / librte_mempool / rte_mempool.c
index 07efd85..4d24b82 100644 (file)
@@ -1,35 +1,34 @@
 /*-
  *   BSD LICENSE
  * 
- *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
  *   All rights reserved.
  * 
- *   Redistribution and use in source and binary forms, with or without 
- *   modification, are permitted provided that the following conditions 
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
  *   are met:
  * 
- *     * Redistributions of source code must retain the above copyright 
+ *     * Redistributions of source code must retain the above copyright
  *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright 
- *       notice, this list of conditions and the following disclaimer in 
- *       the documentation and/or other materials provided with the 
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
  *       distribution.
- *     * Neither the name of Intel Corporation nor the names of its 
- *       contributors may be used to endorse or promote products derived 
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
  *       from this software without specific prior written permission.
  * 
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- * 
  */
 
 #include <stdio.h>
 #include <rte_launch.h>
 #include <rte_tailq.h>
 #include <rte_eal.h>
+#include <rte_eal_memconfig.h>
 #include <rte_per_lcore.h>
 #include <rte_lcore.h>
 #include <rte_branch_prediction.h>
 #include <rte_ring.h>
 #include <rte_errno.h>
 #include <rte_string_fns.h>
+#include <rte_spinlock.h>
 
 #include "rte_mempool.h"
 
 TAILQ_HEAD(rte_mempool_list, rte_mempool);
 
-/* global list of mempool (used for debug/dump) */
-static struct rte_mempool_list *mempool_list;
+#define CACHE_FLUSHTHRESH_MULTIPLIER 1.5
 
 /*
  * return the greatest common divisor between a and b (fast algorithm)
@@ -129,14 +129,13 @@ rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
 {
        char mz_name[RTE_MEMZONE_NAMESIZE];
        char rg_name[RTE_RING_NAMESIZE];
-       struct rte_mempool *mp;
+       struct rte_mempool *mp = NULL;
        struct rte_ring *r;
        const struct rte_memzone *mz;
-       size_t mempool_size;
+       size_t mempool_size, total_elt_size;
        int mz_flags = RTE_MEMZONE_1GB|RTE_MEMZONE_SIZE_HINT_ONLY;
        int rg_flags = 0;
        uint32_t header_size, trailer_size;
-       uint32_t total_elt_size;
        unsigned i;
        void *obj;
 
@@ -157,13 +156,11 @@ rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
 #endif
 
        /* check that we have an initialised tail queue */
-       if (mempool_list == NULL)
-               if ((mempool_list = RTE_TAILQ_RESERVE("RTE_MEMPOOL", \
-                               rte_mempool_list)) == NULL){
-                       rte_errno = E_RTE_NO_TAILQ;
-                       return NULL;
-               }
-
+       if (RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL, rte_mempool_list) == NULL) {
+               rte_errno = E_RTE_NO_TAILQ;
+               return NULL;    
+       }
+       
        /* asked cache too big */
        if (cache_size > RTE_MEMPOOL_CACHE_MAX_SIZE){
                rte_errno = EINVAL;
@@ -180,6 +177,8 @@ rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
        if (flags & MEMPOOL_F_SC_GET)
                rg_flags |= RING_F_SC_DEQ;
 
+       rte_rwlock_write_lock(RTE_EAL_MEMPOOL_RWLOCK);
+
        /* allocate the ring that will be used to store objects */
        /* Ring functions will return appropriate errors if we are
         * running as a secondary process etc., so no checks made
@@ -187,7 +186,7 @@ rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
        rte_snprintf(rg_name, sizeof(rg_name), "MP_%s", name);
        r = rte_ring_create(rg_name, rte_align32pow2(n+1), socket_id, rg_flags);
        if (r == NULL)
-               return NULL;
+               goto exit;
 
        /*
         * In header, we have at least the pointer to the pool, and
@@ -238,6 +237,7 @@ rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
        mempool_size = total_elt_size * n +
                sizeof(struct rte_mempool) + private_data_size;
        rte_snprintf(mz_name, sizeof(mz_name), "MP_%s", name);
+
        mz = rte_memzone_reserve(mz_name, mempool_size, socket_id, mz_flags);
 
        /*
@@ -245,7 +245,7 @@ rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
         * space for the as we cannot free it
         */
        if (mz == NULL)
-               return NULL;
+               goto exit;
 
        /* init the mempool structure */
        mp = mz->addr;
@@ -255,11 +255,11 @@ rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
        mp->ring = r;
        mp->size = n;
        mp->flags = flags;
-       mp->bulk_default = 1;
        mp->elt_size = elt_size;
        mp->header_size = header_size;
        mp->trailer_size = trailer_size;
        mp->cache_size = cache_size;
+       mp->cache_flushthresh = (uint32_t)(cache_size * CACHE_FLUSHTHRESH_MULTIPLIER);
        mp->private_data_size = private_data_size;
 
        /* call the initializer */
@@ -289,7 +289,11 @@ rte_mempool_create(const char *name, unsigned n, unsigned elt_size,
                obj = (char *)obj + elt_size + trailer_size;
        }
 
-       TAILQ_INSERT_TAIL(mempool_list, mp, next);
+       RTE_EAL_TAILQ_INSERT_TAIL(RTE_TAILQ_MEMPOOL, rte_mempool_list, mp);
+
+exit:
+       rte_rwlock_write_unlock(RTE_EAL_MEMPOOL_RWLOCK);
+
        return mp;
 }
 
@@ -381,7 +385,7 @@ mempool_audit_cache(const struct rte_mempool *mp)
        /* check cache size consistency */
        unsigned lcore_id;
        for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
-               if (mp->local_cache[lcore_id].len > mp->cache_size) {
+               if (mp->local_cache[lcore_id].len > mp->cache_flushthresh) {
                        RTE_LOG(CRIT, MEMPOOL, "badness on cache[%u]\n",
                                lcore_id);
                        rte_panic("MEMPOOL: invalid cache len\n");
@@ -399,6 +403,9 @@ rte_mempool_audit(const struct rte_mempool *mp)
 {
        mempool_audit_cache(mp);
        mempool_audit_cookies(mp);
+
+       /* For case where mempool DEBUG is not set, and cache size is 0 */
+       RTE_SET_USED(mp);
 }
 
 /* dump the status of the mempool on the console */
@@ -416,7 +423,6 @@ rte_mempool_dump(const struct rte_mempool *mp)
        printf("  flags=%x\n", mp->flags);
        printf("  ring=<%s>@%p\n", mp->ring->name, mp->ring);
        printf("  size=%"PRIu32"\n", mp->size);
-       printf("  bulk_default=%"PRIu32"\n", mp->bulk_default);
        printf("  header_size=%"PRIu32"\n", mp->header_size);
        printf("  elt_size=%"PRIu32"\n", mp->elt_size);
        printf("  trailer_size=%"PRIu32"\n", mp->trailer_size);
@@ -459,10 +465,21 @@ void
 rte_mempool_list_dump(void)
 {
        const struct rte_mempool *mp = NULL;
+       struct rte_mempool_list *mempool_list;
+
+       if ((mempool_list = 
+            RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL, rte_mempool_list)) == NULL) {
+               rte_errno = E_RTE_NO_TAILQ;
+               return; 
+       }
+
+       rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
 
        TAILQ_FOREACH(mp, mempool_list, next) {
                rte_mempool_dump(mp);
        }
+
+       rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
 }
 
 /* search a mempool from its name */
@@ -470,19 +487,23 @@ struct rte_mempool *
 rte_mempool_lookup(const char *name)
 {
        struct rte_mempool *mp = NULL;
+       struct rte_mempool_list *mempool_list;
 
-       /* check that we have an initialised tail queue */
-       if (mempool_list == NULL)
-               if ((mempool_list = RTE_TAILQ_RESERVE("RTE_MEMPOOL", \
-                               rte_mempool_list)) == NULL){
-                       rte_errno = E_RTE_NO_TAILQ;
-                       return NULL;
-               }
+       if ((mempool_list = 
+            RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_MEMPOOL, rte_mempool_list)) == NULL) {
+               rte_errno = E_RTE_NO_TAILQ;
+               return NULL;
+       }
+
+       rte_rwlock_read_lock(RTE_EAL_MEMPOOL_RWLOCK);
 
        TAILQ_FOREACH(mp, mempool_list, next) {
                if (strncmp(name, mp->name, RTE_MEMPOOL_NAMESIZE) == 0)
                        break;
        }
+       
+       rte_rwlock_read_unlock(RTE_EAL_MEMPOOL_RWLOCK);
+       
        if (mp == NULL)
                rte_errno = ENOENT;