cvs: pecl /apc apc_cache.c apc_cache.h apc_globals.h apc_main.c apc_sma.c php_apc.c

From: Date: Tue, 29 Jan 2008 21:57:45 +0000
Subject: cvs: pecl /apc apc_cache.c apc_cache.h apc_globals.h apc_main.c apc_sma.c php_apc.c
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-9631@lists.php.net to get a copy of this message
shire Tue Jan 29 21:57:45 2008 UTC Modified files: /pecl/apc apc_cache.c apc_cache.h apc_globals.h apc_main.c apc_sma.c php_apc.c Log: add an expunge callback. Fixes bug in apc_add during a cache full, re-arranges headers for future work, allows for experimenting with expunge olgorithms.

http://cvs.php.net/viewvc.cgi/pecl/apc/apc_cache.c?r1=3.149&r2=3.150&diff_format=u Index: pecl/apc/apc_cache.c diff -u pecl/apc/apc_cache.c:3.149 pecl/apc/apc_cache.c:3.150 --- pecl/apc/apc_cache.c:3.149 Fri Jan 25 07:10:48 2008 +++ pecl/apc/apc_cache.c Tue Jan 29 21:57:44 2008 @@ -28,7 +28,7 @@ */ -/* $Id: apc_cache.c,v 3.149 2008/01/25 07:10:48 rasmus Exp $ */ +/* $Id: apc_cache.c,v 3.150 2008/01/29 21:57:44 shire Exp $ */ #include "apc_cache.h" #include "apc_lock.h" @@ -50,52 +50,12 @@ #define UNLOCK(c) { apc_lck_unlock(c->header->lock); HANDLE_UNBLOCK_INTERRUPTIONS(); } /* }}} */ -/* {{{ struct definition: slot_t */ -typedef struct slot_t slot_t; -struct slot_t { - apc_cache_key_t key; /* slot key */ - apc_cache_entry_t* value; /* slot value */ - slot_t* next; /* next slot in linked list */ - int num_hits; /* number of hits to this bucket */ - time_t creation_time; /* time slot was initialized */ - time_t deletion_time; /* time slot was removed from cache */ - time_t access_time; /* time slot was last accessed */ -}; -/* }}} */ - -/* {{{ struct definition: header_t - Any values that must be shared among processes should go in here. */ -typedef struct header_t header_t; -struct header_t { - apc_lck_t lock; /* read/write lock (exclusive blocking cache lock) */ - apc_lck_t wrlock; /* write lock (non-blocking used to prevent cache slams) */ - int num_hits; /* total successful hits in cache */ - int num_misses; /* total unsuccessful hits in cache */ - int num_inserts; /* total successful inserts in cache */ - slot_t* deleted_list; /* linked list of to-be-deleted slots */ - time_t start_time; /* time the above counters were reset */ - int expunges; /* total number of expunges */ - zend_bool busy; /* Flag to tell clients when we are busy cleaning the cache */ - int num_entries; /* Statistic on the number of entries */ - size_t mem_size; /* Statistic on the memory size used by this cache */ -}; -/* }}} */ - -/* {{{ struct definition: apc_cache_t */ -struct apc_cache_t { - void* shmaddr; /* process (local) address of shared cache */ - header_t* header; /* cache header (stored in SHM) */ - slot_t** slots; /* array of cache slots (stored in SHM) */ - int num_slots; /* number of slots in cache */ - int gc_ttl; /* maximum time on GC list for a slot */ - int ttl; /* if slot is needed and entry's access time is older than this ttl, remove it */ -}; -/* }}} */ - /* {{{ key_equals */ #define key_equals(a, b) (a.inode==b.inode && a.device==b.device) /* }}} */ +static size_t apc_cache_expunge(apc_cache_t* cache, size_t size); + /* {{{ hash */ static unsigned int hash(apc_cache_key_t key) { @@ -274,7 +234,7 @@ num_slots = size_hint > 0 ? size_hint*2 : 2000; cache = (apc_cache_t*) apc_emalloc(sizeof(apc_cache_t)); - cache_size = sizeof(header_t) + num_slots*sizeof(slot_t*); + cache_size = sizeof(cache_header_t) + num_slots*sizeof(slot_t*); cache->shmaddr = apc_sma_malloc(cache_size); if(!cache->shmaddr) { @@ -282,7 +242,7 @@ } memset(cache->shmaddr, 0, cache_size); - cache->header = (header_t*) cache->shmaddr; + cache->header = (cache_header_t*) cache->shmaddr; cache->header->num_hits = 0; cache->header->num_misses = 0; cache->header->deleted_list = NULL; @@ -290,7 +250,7 @@ cache->header->expunges = 0; cache->header->busy = 0; - cache->slots = (slot_t**) (((char*) cache->shmaddr) + sizeof(header_t)); + cache->slots = (slot_t**) (((char*) cache->shmaddr) + sizeof(cache_header_t)); cache->num_slots = num_slots; cache->gc_ttl = gc_ttl; cache->ttl = ttl; @@ -301,6 +261,7 @@ for (i = 0; i < num_slots; i++) { cache->slots[i] = NULL; } + cache->expunge_cb = apc_cache_expunge; return cache; } @@ -342,9 +303,21 @@ /* }}} */ /* {{{ apc_cache_expunge */ -void apc_cache_expunge(apc_cache_t* cache, time_t t) +static size_t apc_cache_expunge(apc_cache_t* cache, size_t size) { int i; + time_t t; + TSRMLS_FETCH(); + +#if PHP_API_VERSION < 20041225 +#if HAVE_APACHE && defined(APC_PHP4_STAT) + t = ((request_rec *)SG(server_context))->request_time; +#else + t = time(0); +#endif +#else + t = sapi_get_request_time(TSRMLS_C); +#endif if(!cache) return; http://cvs.php.net/viewvc.cgi/pecl/apc/apc_cache.h?r1=3.47&r2=3.48&diff_format=u Index: pecl/apc/apc_cache.h diff -u pecl/apc/apc_cache.h:3.47 pecl/apc/apc_cache.h:3.48 --- pecl/apc/apc_cache.h:3.47 Tue Jan 8 21:42:32 2008 +++ pecl/apc/apc_cache.h Tue Jan 29 21:57:44 2008 @@ -26,7 +26,7 @@ */ -/* $Id: apc_cache.h,v 3.47 2008/01/08 21:42:32 gopalv Exp $ */ +/* $Id: apc_cache.h,v 3.48 2008/01/29 21:57:44 shire Exp $ */ #ifndef APC_CACHE_H #define APC_CACHE_H @@ -38,6 +38,7 @@ #include "apc.h" #include "apc_compile.h" +#include "apc_lock.h" #define APC_CACHE_ENTRY_FILE 1 #define APC_CACHE_ENTRY_USER 2 @@ -277,9 +278,53 @@ }; /* }}} */ +/* {{{ struct definition: slot_t */ +typedef struct slot_t slot_t; +struct slot_t { + apc_cache_key_t key; /* slot key */ + apc_cache_entry_t* value; /* slot value */ + slot_t* next; /* next slot in linked list */ + int num_hits; /* number of hits to this bucket */ + time_t creation_time; /* time slot was initialized */ + time_t deletion_time; /* time slot was removed from cache */ + time_t access_time; /* time slot was last accessed */ +}; +/* }}} */ + +/* {{{ struct definition: cache_header_t + Any values that must be shared among processes should go in here. */ +typedef struct cache_header_t cache_header_t; +struct cache_header_t { + apc_lck_t lock; /* read/write lock (exclusive blocking cache lock) */ + apc_lck_t wrlock; /* write lock (non-blocking used to prevent cache slams) */ + int num_hits; /* total successful hits in cache */ + int num_misses; /* total unsuccessful hits in cache */ + int num_inserts; /* total successful inserts in cache */ + slot_t* deleted_list; /* linked list of to-be-deleted slots */ + time_t start_time; /* time the above counters were reset */ + int expunges; /* total number of expunges */ + zend_bool busy; /* Flag to tell clients when we are busy cleaning the cache */ + int num_entries; /* Statistic on the number of entries */ + size_t mem_size; /* Statistic on the memory size used by this cache */ +}; +/* }}} */ + +typedef size_t (*apc_expunge_cb_t)(T cache, size_t n); + +/* {{{ struct definition: apc_cache_t */ +struct apc_cache_t { + void* shmaddr; /* process (local) address of shared cache */ + cache_header_t* header; /* cache header (stored in SHM) */ + slot_t** slots; /* array of cache slots (stored in SHM) */ + int num_slots; /* number of slots in cache */ + int gc_ttl; /* maximum time on GC list for a slot */ + int ttl; /* if slot is needed and entry's access time is older than this ttl, remove it */ + apc_expunge_cb_t expunge_cb; /* cache specific expunge callback to free up sma memory */ +}; +/* }}} */ + extern apc_cache_info_t* apc_cache_info(T cache, zend_bool limited); extern void apc_cache_free_info(apc_cache_info_t* info); -extern void apc_cache_expunge(apc_cache_t* cache, time_t t); extern void apc_cache_unlock(apc_cache_t* cache); extern zend_bool apc_cache_busy(apc_cache_t* cache); extern zend_bool apc_cache_write_lock(apc_cache_t* cache); http://cvs.php.net/viewvc.cgi/pecl/apc/apc_globals.h?r1=3.73&r2=3.74&diff_format=u Index: pecl/apc/apc_globals.h diff -u pecl/apc/apc_globals.h:3.73 pecl/apc/apc_globals.h:3.74 --- pecl/apc/apc_globals.h:3.73 Tue Jan 8 21:42:32 2008 +++ pecl/apc/apc_globals.h Tue Jan 29 21:57:44 2008 @@ -29,7 +29,7 @@ */ -/* $Id: apc_globals.h,v 3.73 2008/01/08 21:42:32 gopalv Exp $ */ +/* $Id: apc_globals.h,v 3.74 2008/01/29 21:57:44 shire Exp $ */ #ifndef APC_GLOBALS_H #define APC_GLOBALS_H @@ -86,6 +86,7 @@ zval *filehits; /* Files that came from the cache for this request */ #endif zend_bool coredump_unmap; /* Trap signals that coredump and unmap shared memory */ + apc_cache_t *current_cache; /* current cache being modified/read */ ZEND_END_MODULE_GLOBALS(apc) /* (the following declaration is defined in php_apc.c) */ http://cvs.php.net/viewvc.cgi/pecl/apc/apc_main.c?r1=3.106&r2=3.107&diff_format=u Index: pecl/apc/apc_main.c diff -u pecl/apc/apc_main.c:3.106 pecl/apc/apc_main.c:3.107 --- pecl/apc/apc_main.c:3.106 Wed Jan 9 10:33:05 2008 +++ pecl/apc/apc_main.c Tue Jan 29 21:57:44 2008 @@ -28,7 +28,7 @@ */ -/* $Id: apc_main.c,v 3.106 2008/01/09 10:33:05 gopalv Exp $ */ +/* $Id: apc_main.c,v 3.107 2008/01/29 21:57:44 shire Exp $ */ #include "apc_php.h" #include "apc_main.h" @@ -390,10 +390,10 @@ mem_size = 0; APCG(mem_size_ptr) = &mem_size; + APCG(current_cache) = apc_cache; if(!(alloc_op_array = apc_copy_op_array(NULL, op_array, apc_sma_malloc, apc_sma_free TSRMLS_CC))) { - apc_cache_expunge(apc_cache,t); - apc_cache_expunge(apc_user_cache,t); APCG(mem_size_ptr) = NULL; + APCG(current_cache) = NULL; #if NONBLOCKING_LOCK_AVAILABLE if(APCG(write_lock)) { apc_cache_write_unlock(apc_cache); @@ -405,9 +405,8 @@ if(!(alloc_functions = apc_copy_new_functions(num_functions, apc_sma_malloc, apc_sma_free TSRMLS_CC))) { apc_free_op_array(alloc_op_array, apc_sma_free); - apc_cache_expunge(apc_cache,t); - apc_cache_expunge(apc_user_cache,t); APCG(mem_size_ptr) = NULL; + APCG(current_cache) = NULL; #if NONBLOCKING_LOCK_AVAILABLE if(APCG(write_lock)) { apc_cache_write_unlock(apc_cache); @@ -419,9 +418,8 @@ if(!(alloc_classes = apc_copy_new_classes(op_array, num_classes, apc_sma_malloc, apc_sma_free TSRMLS_CC))) { apc_free_op_array(alloc_op_array, apc_sma_free); apc_free_functions(alloc_functions, apc_sma_free); - apc_cache_expunge(apc_cache,t); - apc_cache_expunge(apc_user_cache,t); APCG(mem_size_ptr) = NULL; + APCG(current_cache) = NULL; #if NONBLOCKING_LOCK_AVAILABLE if(APCG(write_lock)) { apc_cache_write_unlock(apc_cache); @@ -442,9 +440,8 @@ apc_free_op_array(alloc_op_array, apc_sma_free); apc_free_functions(alloc_functions, apc_sma_free); apc_free_classes(alloc_classes, apc_sma_free); - apc_cache_expunge(apc_cache,t); - apc_cache_expunge(apc_user_cache,t); APCG(mem_size_ptr) = NULL; + APCG(current_cache) = NULL; #if NONBLOCKING_LOCK_AVAILABLE if(APCG(write_lock)) { apc_cache_write_unlock(apc_cache); @@ -458,12 +455,10 @@ if ((ret = apc_cache_insert(apc_cache, key, cache_entry, t)) != 1) { apc_cache_free_entry(cache_entry); - if(ret==-1) { - apc_cache_expunge(apc_cache,t); - apc_cache_expunge(apc_user_cache,t); - } } + APCG(current_cache) = NULL; + #if NONBLOCKING_LOCK_AVAILABLE if(APCG(write_lock)) { apc_cache_write_unlock(apc_cache); http://cvs.php.net/viewvc.cgi/pecl/apc/apc_sma.c?r1=1.72&r2=1.73&diff_format=u Index: pecl/apc/apc_sma.c diff -u pecl/apc/apc_sma.c:1.72 pecl/apc/apc_sma.c:1.73 --- pecl/apc/apc_sma.c:1.72 Tue Jan 15 02:38:09 2008 +++ pecl/apc/apc_sma.c Tue Jan 29 21:57:44 2008 @@ -26,13 +26,14 @@ */ -/* $Id: apc_sma.c,v 1.72 2008/01/15 02:38:09 shire Exp $ */ +/* $Id: apc_sma.c,v 1.73 2008/01/29 21:57:44 shire Exp $ */ #include "apc_sma.h" #include "apc.h" #include "apc_globals.h" #include "apc_lock.h" #include "apc_shm.h" +#include "apc_cache.h" #include <limits.h> #if APC_MMAP void *apc_mmap(char *file_mask, size_t size); @@ -413,12 +414,23 @@ { size_t off; int i; + size_t *orig_mem_size_ptr; TSRMLS_FETCH(); assert(sma_initialized); LOCK(((header_t*)sma_shmaddrs[sma_lastseg])->sma_lock); off = sma_allocate(sma_shmaddrs[sma_lastseg], n); + if(off == -1) { + /* retry failed allocation after we expunge */ + UNLOCK(((header_t*)sma_shmaddrs[sma_lastseg])->sma_lock); + orig_mem_size_ptr = APCG(mem_size_ptr); + APCG(mem_size_ptr) = NULL; + APCG(current_cache)->expunge_cb(APCG(current_cache), n); + APCG(mem_size_ptr) = orig_mem_size_ptr; + LOCK(((header_t*)sma_shmaddrs[sma_lastseg])->sma_lock); + off = sma_allocate(sma_shmaddrs[sma_lastseg], n); + } if (off != -1) { void* p = (void *)(((char *)(sma_shmaddrs[sma_lastseg])) + off); if (APCG(mem_size_ptr) != NULL) { *(APCG(mem_size_ptr)) += n; } @@ -436,6 +448,16 @@ } LOCK(((header_t*)sma_shmaddrs[i])->sma_lock); off = sma_allocate(sma_shmaddrs[i], n); + if(off == -1) { + /* retry failed allocation after we expunge */ + UNLOCK(((header_t*)sma_shmaddrs[i])->sma_lock); + orig_mem_size_ptr = APCG(mem_size_ptr); + APCG(mem_size_ptr) = NULL; + APCG(current_cache)->expunge_cb(APCG(current_cache), n); + APCG(mem_size_ptr) = orig_mem_size_ptr; + LOCK(((header_t*)sma_shmaddrs[i])->sma_lock); + off = sma_allocate(sma_shmaddrs[sma_lastseg], n); + } if (off != -1) { void* p = (void *)(((char *)(sma_shmaddrs[i])) + off); if (APCG(mem_size_ptr) != NULL) { *(APCG(mem_size_ptr)) += n; } http://cvs.php.net/viewvc.cgi/pecl/apc/php_apc.c?r1=3.159&r2=3.160&diff_format=u Index: pecl/apc/php_apc.c diff -u pecl/apc/php_apc.c:3.159 pecl/apc/php_apc.c:3.160 --- pecl/apc/php_apc.c:3.159 Fri Jan 25 07:10:48 2008 +++ pecl/apc/php_apc.c Tue Jan 29 21:57:44 2008 @@ -26,7 +26,7 @@ */ -/* $Id: php_apc.c,v 3.159 2008/01/25 07:10:48 rasmus Exp $ */ +/* $Id: php_apc.c,v 3.160 2008/01/29 21:57:44 shire Exp $ */ #include "apc_zend.h" #include "apc_cache.h" @@ -222,7 +222,7 @@ #else php_info_print_table_row(2, "Locking type", "File Locks"); #endif - php_info_print_table_row(2, "Revision", "$Revision: 3.159 $"); + php_info_print_table_row(2, "Revision", "$Revision: 3.160 $"); php_info_print_table_row(2, "Build Date", __DATE__ " " __TIME__); php_info_print_table_end(); DISPLAY_INI_ENTRIES(); @@ -556,19 +556,18 @@ HANDLE_BLOCK_INTERRUPTIONS(); APCG(mem_size_ptr) = &mem_size; + APCG(current_cache) = apc_user_cache; if (!(entry = apc_cache_make_user_entry(strkey, strkey_len + 1, val, ttl))) { APCG(mem_size_ptr) = NULL; - apc_cache_expunge(apc_cache,t); - apc_cache_expunge(apc_user_cache,t); + APCG(current_cache) = NULL; HANDLE_UNBLOCK_INTERRUPTIONS(); return 0; } if (!apc_cache_make_user_key(&key, strkey, strkey_len + 1, t)) { APCG(mem_size_ptr) = NULL; + APCG(current_cache) = NULL; apc_cache_free_entry(entry); - apc_cache_expunge(apc_cache,t); - apc_cache_expunge(apc_user_cache,t); HANDLE_UNBLOCK_INTERRUPTIONS(); return 0; } @@ -577,14 +576,14 @@ apc_cache_free_entry(entry); if(ret==-1) { APCG(mem_size_ptr) = NULL; - apc_cache_expunge(apc_cache,t); - apc_cache_expunge(apc_user_cache,t); + APCG(current_cache) = NULL; HANDLE_UNBLOCK_INTERRUPTIONS(); return 0; } } APCG(mem_size_ptr) = NULL; + APCG(current_cache) = NULL; HANDLE_UNBLOCK_INTERRUPTIONS();
« previous php.pecl.cvs (#9631) next »