Memory allocation performance

Peter Jeremy peterjeremy at optushome.com.au
Sat Feb 2 11:37:01 PST 2008


On Sat, Feb 02, 2008 at 11:31:31AM +0200, Alexander Motin wrote:
>To check UMA dependency I have made a trivial one-element cache which in my 
>test case allows to avoid two for four allocations per packet.

You should be able to implement this lockless using atomic(9).  I haven't
verified it, but the following should work.

>.....alloc.....
>-       item = uma_zalloc(ng_qzone, wait | M_ZERO);

>+       mtx_lock_spin(&itemcachemtx);
>+       item = itemcache;
>+       itemcache = NULL;
>+       mtx_unlock_spin(&itemcachemtx);
 =	 item = atomic_readandclear_ptr(&itemcache);

>+       if (item == NULL)
>+               item = uma_zalloc(ng_qzone, wait | M_ZERO);
>+       else
>+               bzero(item, sizeof(*item));

>.....free.....
>-       uma_zfree(ng_qzone, item);

>+       mtx_lock_spin(&itemcachemtx);
>+       if (itemcache == NULL) {
>+               itemcache = item;
>+               item = NULL;
>+       }
>+       mtx_unlock_spin(&itemcachemtx);
>+       if (item)
>+               uma_zfree(ng_qzone, item);
 =	 if (atomic_cmpset_ptr(&itemcache, NULL, item) == 0)
 =		 uma_zfree(ng_qzone, item);

-- 
Peter Jeremy
Please excuse any delays as the result of my ISP's inability to implement
an MTA that is either RFC2821-compliant or matches their claimed behaviour.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20080202/1c9b71a4/attachment.pgp


More information about the freebsd-hackers mailing list