From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by dpdk.org (Postfix) with ESMTP id 4D3845938 for ; Wed, 18 Mar 2015 00:46:54 +0100 (CET) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga102.fm.intel.com with ESMTP; 17 Mar 2015 16:46:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.11,418,1422950400"; d="scan'208";a="666674822" Received: from irsmsx107.ger.corp.intel.com ([163.33.3.99]) by orsmga001.jf.intel.com with ESMTP; 17 Mar 2015 16:46:52 -0700 Received: from irsmsx105.ger.corp.intel.com ([169.254.7.117]) by IRSMSX107.ger.corp.intel.com ([169.254.10.35]) with mapi id 14.03.0195.001; Tue, 17 Mar 2015 23:46:51 +0000 From: "Ananyev, Konstantin" To: "vadim.suraev@gmail.com" , "dev@dpdk.org" Thread-Topic: [PATCH v2] rte_mbuf: mbuf bulk alloc/free functions added + unittest Thread-Index: AQHQYPpsu9YquxZduU6MoiIW4nRTTp0hU+8A Date: Tue, 17 Mar 2015 23:46:50 +0000 Message-ID: <2601191342CEEE43887BDE71AB977258213F6F10@irsmsx105.ger.corp.intel.com> References: <1426628169-1735-1-git-send-email-vadim.suraev@gmail.com> In-Reply-To: <1426628169-1735-1-git-send-email-vadim.suraev@gmail.com> Accept-Language: en-IE, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [163.33.239.181] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: [dpdk-dev] [PATCH v2] rte_mbuf: mbuf bulk alloc/free functions added + unittest X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 Mar 2015 23:46:55 -0000 Hi Vadim, > -----Original Message----- > From: vadim.suraev@gmail.com [mailto:vadim.suraev@gmail.com] > Sent: Tuesday, March 17, 2015 9:36 PM > To: dev@dpdk.org > Cc: olivier.matz@6wind.com; stephen@networkplumber.org; Ananyev, Konstant= in; vadim.suraev@gmail.com > Subject: [PATCH v2] rte_mbuf: mbuf bulk alloc/free functions added + unit= test >=20 > From: "vadim.suraev@gmail.com" >=20 > This patch adds mbuf bulk allocation/freeing functions and unittest >=20 > Signed-off-by: Vadim Suraev > > --- > New in v2: > - function rte_pktmbuf_alloc_bulk added > - function rte_pktmbuf_bulk_free added > - function rte_pktmbuf_free_chain added > - applied reviewers' comments >=20 > app/test/test_mbuf.c | 94 ++++++++++++++++++++++++++++++++++++++= +++++- > lib/librte_mbuf/rte_mbuf.h | 89 ++++++++++++++++++++++++++++++++++++++= +++ > 2 files changed, 182 insertions(+), 1 deletion(-) >=20 > diff --git a/app/test/test_mbuf.c b/app/test/test_mbuf.c > index 1ff66cb..b20c6a4 100644 > --- a/app/test/test_mbuf.c > +++ b/app/test/test_mbuf.c > @@ -77,6 +77,7 @@ > #define REFCNT_RING_SIZE (REFCNT_MBUF_NUM * REFCNT_MAX_REF) >=20 > #define MAKE_STRING(x) # x > +#define MBUF_POOL_LOCAL_CACHE_SIZE 32 >=20 > static struct rte_mempool *pktmbuf_pool =3D NULL; >=20 > @@ -405,6 +406,84 @@ test_pktmbuf_pool(void) > return ret; > } >=20 > +/* test pktmbuf bulk allocation and freeing > +*/ > +static int > +test_pktmbuf_pool_bulk(void) > +{ > + unsigned i; > + /* size of mempool - size of local cache, otherwise may fail */ > + unsigned mbufs_to_allocate =3D NB_MBUF - MBUF_POOL_LOCAL_CACHE_SIZE; > + struct rte_mbuf *m[mbufs_to_allocate]; > + int ret =3D 0; > + unsigned mbuf_count_before_allocation =3D rte_mempool_count(pktmbuf_poo= l); > + > + for (i =3D 0; i < mbufs_to_allocate; i++) > + m[i] =3D NULL; > + /* alloc NB_MBUF-MBUF_POOL_LOCAL_CACHE_SIZE mbufs */ > + ret =3D rte_pktmbuf_alloc_bulk(pktmbuf_pool, m, mbufs_to_allocate); > + if (ret) { > + printf("cannot allocate %d mbufs bulk mempool_cnt=3D%d ret=3D%d\n", > + mbufs_to_allocate, > + rte_mempool_count(pktmbuf_pool), > + ret); > + return -1; > + } > + if ((rte_mempool_count(pktmbuf_pool) + mbufs_to_allocate) !=3D > + mbuf_count_before_allocation) { > + printf("mempool count %d + allocated %d !=3D initial %d\n", > + rte_mempool_count(pktmbuf_pool), > + mbufs_to_allocate, > + mbuf_count_before_allocation); > + return -1; > + } > + /* free them */ > + rte_pktmbuf_bulk_free(m, mbufs_to_allocate); > + > + if (rte_mempool_count(pktmbuf_pool) !=3D mbuf_count_before_allocation)= { > + printf("mempool count %d !=3D initial %d\n", > + rte_mempool_count(pktmbuf_pool), > + mbuf_count_before_allocation); > + return -1; > + } > + for (i =3D 0; i < mbufs_to_allocate; i++) > + m[i] =3D NULL; > + > + /* alloc NB_MBUF-MBUF_POOL_LOCAL_CACHE_SIZE mbufs */ > + ret =3D rte_pktmbuf_alloc_bulk(pktmbuf_pool, m, mbufs_to_allocate); > + if (ret) { > + printf("cannot allocate %d mbufs bulk mempool_cnt=3D%d ret=3D%d\n", > + mbufs_to_allocate, > + rte_mempool_count(pktmbuf_pool), > + ret); > + return -1; > + } > + if ((rte_mempool_count(pktmbuf_pool) + mbufs_to_allocate) !=3D > + mbuf_count_before_allocation) { > + printf("mempool count %d + allocated %d !=3D initial %d\n", > + rte_mempool_count(pktmbuf_pool), > + mbufs_to_allocate, > + mbuf_count_before_allocation); > + return -1; > + } > + > + /* chain it */ > + for (i =3D 0; i < mbufs_to_allocate - 1; i++) { > + m[i]->next =3D m[i + 1]; > + m[0]->nb_segs++; > + } > + /* free them */ > + rte_pktmbuf_free_chain(m[0]); > + > + if (rte_mempool_count(pktmbuf_pool) !=3D mbuf_count_before_allocation)= { > + printf("mempool count %d !=3D initial %d\n", > + rte_mempool_count(pktmbuf_pool), > + mbuf_count_before_allocation); > + return -1; > + } > + return ret; > +} > + > /* > * test that the pointer to the data on a packet mbuf is set properly > */ > @@ -766,7 +845,8 @@ test_mbuf(void) > if (pktmbuf_pool =3D=3D NULL) { > pktmbuf_pool =3D > rte_mempool_create("test_pktmbuf_pool", NB_MBUF, > - MBUF_SIZE, 32, > + MBUF_SIZE, > + MBUF_POOL_LOCAL_CACHE_SIZE, > sizeof(struct rte_pktmbuf_pool_private), > rte_pktmbuf_pool_init, NULL, > rte_pktmbuf_init, NULL, > @@ -790,6 +870,18 @@ test_mbuf(void) > return -1; > } >=20 > + /* test bulk allocation and freeing */ > + if (test_pktmbuf_pool_bulk() < 0) { > + printf("test_pktmbuf_pool_bulk() failed\n"); > + return -1; > + } > + > + /* once again to ensure all mbufs were freed */ > + if (test_pktmbuf_pool_bulk() < 0) { > + printf("test_pktmbuf_pool_bulk() failed\n"); > + return -1; > + } > + > /* test that the pointer to the data on a packet mbuf is set properly *= / > if (test_pktmbuf_pool_ptr() < 0) { > printf("test_pktmbuf_pool_ptr() failed\n"); > diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h > index 17ba791..995237d 100644 > --- a/lib/librte_mbuf/rte_mbuf.h > +++ b/lib/librte_mbuf/rte_mbuf.h > @@ -825,6 +825,95 @@ static inline void rte_pktmbuf_free(struct rte_mbuf = *m) > } >=20 > /** > + * Allocate a bulk of mbufs, initiate refcnt and resets > + * > + * @param pool > + * memory pool to allocate from > + * @param mbufs > + * Array of pointers to mbuf > + * @param count > + * Array size > + */ > +static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool, > + struct rte_mbuf **mbufs, > + unsigned count) > +{ > + unsigned idx; > + int rc =3D 0; > + > + rc =3D rte_mempool_get_bulk(pool, (void **)mbufs, count); > + if (unlikely(rc)) > + return rc; > + > + for (idx =3D 0; idx < count; idx++) { > + RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(mbufs[idx]) =3D=3D 0); > + rte_mbuf_refcnt_set(mbufs[idx], 1); > + rte_pktmbuf_reset(mbufs[idx]); > + } > + return rc; > +} > + > +/** > + * Free a bulk of mbufs into its original mempool. > + * This function assumes refcnt equals 1 > + * as well as the freed mbufs are direct I think your forgot to mention in comments one more requirement for that fu= nction: all mbufs have to be from the same mempool. > + * > + * @param mbufs > + * Array of pointers to mbuf > + * @param count > + * Array size > + */ > +static inline void rte_pktmbuf_bulk_free(struct rte_mbuf **mbufs, > + unsigned count) > +{ > + unsigned idx; > + > + RTE_MBUF_ASSERT(count > 0); > + > + for (idx =3D 0; idx < count; idx++) { > + rte_mbuf_refcnt_update(mbufs[idx], -1); You can do just: rte_mbuf_refcnt_set(m, 0); here and move your assert above it. Something like: RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(mbufs[idx]) =3D=3D 1); rte_mbuf_refcnt_set(m, 0); Also probably would be a good thing to add one more assert here, something like: RTE_MBUF_ASSERT(mbufs[idx]->pool =3D=3D mufs[0]->pool); > + RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(mbufs[idx]) =3D=3D 0); > + } > + rte_mempool_put_bulk(mbufs[0]->pool, (void **)mbufs, count); > +} > + > +/** > + * Free chained (scattered) mbufs into its original mempool(s). > + * > + * @param head > + * The head of mbufs to be freed chain. Must not be NULL > + */ > +static inline void rte_pktmbuf_free_chain(struct rte_mbuf *head) > +{ > + struct rte_mbuf *mbufs[head->nb_segs]; > + unsigned mbufs_count =3D 0; > + struct rte_mbuf *next; > + > + while (head) { > + next =3D head->next; > + head->next =3D NULL; Shouldn't the line above be inside if (head !=3D NULL) {...} block? > + head =3D __rte_pktmbuf_prefree_seg(head); > + if (likely(head !=3D NULL)) { > + RTE_MBUF_ASSERT(rte_mbuf_refcnt_read(head) =3D=3D 0); I don't think there is any use of the assert above. If prefree_seg returns non-NULL value, it sets refcnt to 0 for that mbuf. > + if (likely((!mbufs_count) || > + (head->pool =3D=3D mbufs[0]->pool))) > + mbufs[mbufs_count++] =3D head; > + else { > + rte_mempool_put_bulk(mbufs[0]->pool, > + (void **)mbufs, > + mbufs_count); > + mbufs_count =3D 0; > + } > + } > + head =3D next; > + } > + if (mbufs_count > 0) > + rte_mempool_put_bulk(mbufs[0]->pool, > + (void **)mbufs, > + mbufs_count); > +} > + > +/** > * Creates a "clone" of the given packet mbuf. > * > * Walks through all segments of the given packet mbuf, and for each of = them: > -- > 1.7.9.5