cvs: pecl /apc apc.php apc_sma.c

From: Date: Sun, 26 Feb 2006 19:31:09 +0000
Subject: cvs: pecl /apc apc.php apc_sma.c
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-5274@lists.php.net to get a copy of this message
rasmus Sun Feb 26 19:31:09 2006 UTC Modified files: /pecl/apc apc.php apc_sma.c Log: Completely rework the allocator. I have switched it from being a best fit allocator to doing a next fit. I found that always using the smallest possible chunk ensured that we would get very small fragments that were less likely to be reused. Spending more time trying to find smaller chunks only to increase fragmentation makes no sense. So, instead I use the first chunk big enough to fit my alloc and save the position of my search so that the next search will start searching from the same spot wrapping around to the top when it gets near the end. I have also removed all the chunk sizing tricks. Hiding fragmentation simply by pretending to use more memory might make the fragmentation graph look nicer, and has a slightly higher chance of chunk reuse, but at the cost of using a lot more memory which in the end hurts us more. http://cvs.php.net/viewcvs.cgi/pecl/apc/apc.php?r1=3.48&r2=3.49&diff_format=u Index: pecl/apc/apc.php diff -u pecl/apc/apc.php:3.48 pecl/apc/apc.php:3.49 --- pecl/apc/apc.php:3.48 Sat Feb 25 19:50:33 2006 +++ pecl/apc/apc.php Sun Feb 26 19:31:09 2006 @@ -22,7 +22,7 @@ */ -$VERSION='$Id: apc.php,v 3.48 2006/02/25 19:50:33 rasmus Exp $'; +$VERSION='$Id: apc.php,v 3.49 2006/02/26 19:31:09 rasmus Exp $'; ////////// READ OPTIONAL CONFIGURATION FILE //////////// if (file_exists("apc.conf.php")) include("apc.conf.php"); @@ -167,14 +167,14 @@ } } -// clear cache -if ($AUTHENTICATED && isset($MYREQUEST['CC']) && $MYREQUEST['CC']) { - apc_clear_cache(); -} // select cache mode if ($AUTHENTICATED && $MYREQUEST['OB'] == OB_USER_CACHE) { $cache_mode='user'; } +// clear cache +if ($AUTHENTICATED && isset($MYREQUEST['CC']) && $MYREQUEST['CC']) { + apc_clear_cache($cache_mode); +} if(!$cache=@apc_cache_info($cache_mode)) { @@ -708,7 +708,7 @@ if ($AUTHENTICATED) { echo <<<EOB - <li><a class="aright" href="$MY_SELF&CC=1&OB={$MYREQUEST['OB']}" onClick="javascipt:return confirm('Are you sure?');">Clear Cache</a></li> + <li><a class="aright" href="$MY_SELF&CC=1&OB={$MYREQUEST['OB']}" onClick="javascipt:return confirm('Are you sure?');">Clear $cache_mode Cache</a></li> EOB; } echo <<<EOB http://cvs.php.net/viewcvs.cgi/pecl/apc/apc_sma.c?r1=1.39&r2=1.40&diff_format=u Index: pecl/apc/apc_sma.c diff -u pecl/apc/apc_sma.c:1.39 pecl/apc/apc_sma.c:1.40 --- pecl/apc/apc_sma.c:1.39 Sun Feb 26 00:07:43 2006 +++ pecl/apc/apc_sma.c Sun Feb 26 19:31:09 2006 @@ -26,7 +26,7 @@ */ -/* $Id: apc_sma.c,v 1.39 2006/02/26 00:07:43 rasmus Exp $ */ +/* $Id: apc_sma.c,v 1.40 2006/02/26 19:31:09 rasmus Exp $ */ #include "apc_sma.h" #include "apc.h" @@ -45,8 +45,6 @@ #define UNLOCK(c) { apc_lck_unlock(c); HANDLE_UNBLOCK_INTERRUPTIONS(); } /* }}} */ -enum { POWER_OF_TWO_BLOCKSIZE=0 }; /* force allocated blocks to 2^n? */ - enum { DEFAULT_NUMSEG=1, DEFAULT_SEGSIZE=30*1024*1024 }; static int sma_initialized = 0; /* true if the sma has been initialized */ @@ -61,6 +59,7 @@ struct header_t { size_t segsize; /* size of entire segment */ size_t avail; /* bytes available (not necessarily contiguous) */ + size_t nfoffset; /* start next fit search from this offset */ #if ALLOC_DISTRIBUTION size_t adist[30]; #endif @@ -98,43 +97,11 @@ header_t* header; /* header of shared memory segment */ block_t* prv; /* block prior to working block */ block_t* cur; /* working block in list */ - block_t* prvbestfit; /* block before best fit */ + block_t* prvnextfit; /* block before next fit */ size_t realsize; /* actual size of block needed, including header */ - size_t minsize; /* for finding best fit */ + size_t last_offset; /* save the last search offset */ -#if 1 - if(size < 8) realsize = 8; - else realsize = size; -#else - if(size < 0x40) { - realsize = 0x40; - } else { - /* - * Set realsize to the smallest power of 2 greater than or equal to - * realsize. This increases the likelihood that neighboring blocks can be - * coalesced, reducing memory fragmentation. - */ - if (POWER_OF_TWO_BLOCKSIZE) { - size_t p = size - 1; -#if SIZEOF_INT == 8 - p|=(p>>1); p|=(p>>2); p|=(p>>4); p|=(p>>8); p|=(p>>16); p|=(p>>32); -#else - p|=(p>>1); p|=(p>>2); p|=(p>>4); p|=(p>>8); p|=(p>>16); -#endif - realsize = p + 1; - } else { - /* Probably makes more sense to just round up to the next 64 byte boundary */ - size_t p = size - 1; -#if SIZEOF_INT == 8 - p&=0xffffffffffffffc0; p+=0x40; -#else - p&=0xffffffc0; p+=0x40; -#endif - realsize = p; - } - } -#endif - realsize = alignword(max(realsize + alignword(sizeof(int)), sizeof(block_t))); + realsize = alignword(max(size + alignword(sizeof(int)), sizeof(block_t))); /* * First, insure that the segment contains at least realsize free bytes, @@ -145,32 +112,36 @@ return -1; } - prvbestfit = 0; /* initially null (no fit) */ - minsize = INT_MAX; /* used to find best fit */ + prvnextfit = 0; /* initially null (no fit) */ + last_offset = 0; - prv = BLOCKAT(sizeof(header_t)); - while (prv->next != 0) { + /* If we have a next fit offset, start searching from there */ + if(header->nfoffset) { + prv = BLOCKAT(header->nfoffset); + } else { + prv = BLOCKAT(sizeof(header_t)); + } + while (prv->next != header->nfoffset) { cur = BLOCKAT(prv->next); - if (cur->size == realsize) { - /* found a perfect fit, stop searching */ - prvbestfit = prv; + /* If it fits perfectly or it fits after a split, stop searching */ + if (cur->size == realsize || (cur->size > (sizeof(block_t) + realsize))) { + prvnextfit = prv; break; } - else if (cur->size > (sizeof(block_t) + realsize) && - cur->size < minsize) - { - /* cur is acceptable and is the smallest so far */ - prvbestfit = prv; - minsize = cur->size; - } + last_offset = prv->next; prv = cur; + /* Check to see if we need to wrap around and search from the top */ + if(header->nfoffset && prv->next == 0) { + prv = BLOCKAT(sizeof(header_t)); + } } - if (prvbestfit == 0) { + if (prvnextfit == 0) { return -1; } - prv = prvbestfit; + header->nfoffset = last_offset; /* Next time we search, start at this offset */ + prv = prvnextfit; cur = BLOCKAT(prv->next); /* update the block header */ @@ -179,16 +150,16 @@ header->adist[(int)(log(size)/log(2))]++; #endif - if (cur->size == realsize || ((cur->size - realsize) < 0x20)) { + if (cur->size == realsize) { /* cur is a perfect fit for realsize; just unlink it */ prv->next = cur->next; } else { - block_t* nxt; /* the new block (chopped part of cur) */ + block_t* nxt; /* the new block (chopped part of cur) */ size_t nxtoffset; /* offset of the block currently after cur */ size_t oldsize; /* size of cur before split */ - /* bestfit is too big; split it into two smaller blocks */ + /* nextfit is too big; split it into two smaller blocks */ nxtoffset = cur->next; oldsize = cur->size; prv->next += realsize; @@ -300,6 +271,7 @@ header->segsize = sma_segsize; header->avail = sma_segsize - sizeof(header_t) - sizeof(block_t) - alignword(sizeof(int)); + header->nfoffset = 0; #if ALLOC_DISTRIBUTION { int j;

« previous php.pecl.cvs (#5274) next »