[dpdk-dev] mbuf: optimize first reference increment in rte_pktmbuf_attach

Message ID 1433151145-8176-1-git-send-email-olivier.matz@6wind.com (mailing list archive)
State Superseded, archived
Headers

Commit Message

Olivier Matz June 1, 2015, 9:32 a.m. UTC
  As it's done in __rte_pktmbuf_prefree_seg(), we can avoid using an
atomic increment in rte_pktmbuf_attach() by checking if we are the
only owner of the mbuf first.

Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
---
 lib/librte_mbuf/rte_mbuf.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
  

Comments

Bruce Richardson June 3, 2015, 10:59 a.m. UTC | #1
On Mon, Jun 01, 2015 at 11:32:25AM +0200, Olivier Matz wrote:
> As it's done in __rte_pktmbuf_prefree_seg(), we can avoid using an
> atomic increment in rte_pktmbuf_attach() by checking if we are the
> only owner of the mbuf first.
> 
> Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
> ---
>  lib/librte_mbuf/rte_mbuf.h | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h
> index ab6de67..cea35b7 100644
> --- a/lib/librte_mbuf/rte_mbuf.h
> +++ b/lib/librte_mbuf/rte_mbuf.h
> @@ -838,7 +838,11 @@ static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
>  	else
>  		md = rte_mbuf_from_indirect(m);
>  
> -	rte_mbuf_refcnt_update(md, 1);
> +	/* optimize the case where we are the only owner */
> +	if (likely(rte_mbuf_refcnt_read(md) == 1))
> +		rte_mbuf_refcnt_set(md, 2);
> +	else
> +		rte_mbuf_refcnt_update(md, 1);
>  	mi->priv_size = m->priv_size;
>  	mi->buf_physaddr = m->buf_physaddr;
>  	mi->buf_addr = m->buf_addr;
> -- 
> 2.1.4
> 
Why not make the change inside rte_mbuf_refcnt_update itself? If it is ever
called with a current refcnt of 1, it should always be safe to do the update
without a cmpset.

/Bruce
  
Olivier Matz June 5, 2015, 9:07 a.m. UTC | #2
Hi Bruce,

On 06/03/2015 12:59 PM, Bruce Richardson wrote:
> On Mon, Jun 01, 2015 at 11:32:25AM +0200, Olivier Matz wrote:
>> As it's done in __rte_pktmbuf_prefree_seg(), we can avoid using an
>> atomic increment in rte_pktmbuf_attach() by checking if we are the
>> only owner of the mbuf first.
>>
>> Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
>> ---
>>  lib/librte_mbuf/rte_mbuf.h | 6 +++++-
>>  1 file changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h
>> index ab6de67..cea35b7 100644
>> --- a/lib/librte_mbuf/rte_mbuf.h
>> +++ b/lib/librte_mbuf/rte_mbuf.h
>> @@ -838,7 +838,11 @@ static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
>>  	else
>>  		md = rte_mbuf_from_indirect(m);
>>  
>> -	rte_mbuf_refcnt_update(md, 1);
>> +	/* optimize the case where we are the only owner */
>> +	if (likely(rte_mbuf_refcnt_read(md) == 1))
>> +		rte_mbuf_refcnt_set(md, 2);
>> +	else
>> +		rte_mbuf_refcnt_update(md, 1);
>>  	mi->priv_size = m->priv_size;
>>  	mi->buf_physaddr = m->buf_physaddr;
>>  	mi->buf_addr = m->buf_addr;
>> -- 
>> 2.1.4
>>
> Why not make the change inside rte_mbuf_refcnt_update itself? If it is ever
> called with a current refcnt of 1, it should always be safe to do the update
> without a cmpset.

Good idea, I'll see if I can do that in a new version.

Thanks,
Olivier


> 
> /Bruce
>
  

Patch

diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h
index ab6de67..cea35b7 100644
--- a/lib/librte_mbuf/rte_mbuf.h
+++ b/lib/librte_mbuf/rte_mbuf.h
@@ -838,7 +838,11 @@  static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
 	else
 		md = rte_mbuf_from_indirect(m);
 
-	rte_mbuf_refcnt_update(md, 1);
+	/* optimize the case where we are the only owner */
+	if (likely(rte_mbuf_refcnt_read(md) == 1))
+		rte_mbuf_refcnt_set(md, 2);
+	else
+		rte_mbuf_refcnt_update(md, 1);
 	mi->priv_size = m->priv_size;
 	mi->buf_physaddr = m->buf_physaddr;
 	mi->buf_addr = m->buf_addr;