]> granicus.if.org Git - php/commitdiff
More accurate restart scheduling
authorDmitry Stogov <dmitry@zend.com>
Fri, 15 Mar 2013 07:49:33 +0000 (11:49 +0400)
committerDmitry Stogov <dmitry@zend.com>
Fri, 15 Mar 2013 07:49:33 +0000 (11:49 +0400)
ZendAccelerator.c
zend_accelerator_module.c
zend_shared_alloc.c
zend_shared_alloc.h

index bb571a48056eab2587c512748474b5a55f9370be..87b60f1e01aaa63ec2f4f8b6dfc9be4701835095 100644 (file)
@@ -71,8 +71,6 @@ typedef int gid_t;
 #include <sys/stat.h>
 #include <errno.h>
 
-#define MIN_FREE_MEMORY 64*1024
-
 #define SHM_PROTECT() \
        do { \
                if (ZCG(accel_directives).protect_memory) { \
@@ -853,6 +851,7 @@ static inline int do_validate_timestamps(zend_persistent_script *persistent_scri
 static void zend_accel_schedule_restart_if_necessary(TSRMLS_D)
 {
        if ((((double) ZSMMG(wasted_shared_memory)) / ZCG(accel_directives).memory_consumption) >= ZCG(accel_directives).max_wasted_percentage) {
+           ZSMMG(memory_exhausted) = 1;
                zend_accel_schedule_restart(TSRMLS_C);
        }
 }
@@ -1031,14 +1030,12 @@ static void zend_accel_add_key(char *key, unsigned int key_length, zend_accel_ha
                if (zend_accel_hash_is_full(&ZCSG(hash))) {
                        zend_accel_error(ACCEL_LOG_DEBUG, "No more entries in hash table!");
                        ZSMMG(memory_exhausted) = 1;
+                       zend_accel_schedule_restart(TSRMLS_C);
                } else {
                        char *new_key = zend_shared_alloc(key_length + 1);
                        if (new_key) {
                                memcpy(new_key, key, key_length + 1);
                                zend_accel_hash_update(&ZCSG(hash), new_key, key_length + 1, 1, bucket);
-                       } else {
-                               zend_accel_error(ACCEL_LOG_DEBUG, "No more memory!");
-                               ZSMMG(memory_exhausted) = 1;
                        }
                }
        }
@@ -1060,7 +1057,7 @@ static zend_persistent_script *cache_script_in_shared_memory(zend_persistent_scr
        if (zend_accel_hash_is_full(&ZCSG(hash))) {
                zend_accel_error(ACCEL_LOG_DEBUG, "No more entries in hash table!");
                ZSMMG(memory_exhausted) = 1;
-               zend_accel_schedule_restart_if_necessary(TSRMLS_C);
+               zend_accel_schedule_restart(TSRMLS_C);
                zend_shared_alloc_unlock(TSRMLS_C);
                return new_persistent_script;
        }
@@ -1088,11 +1085,6 @@ static zend_persistent_script *cache_script_in_shared_memory(zend_persistent_scr
        /* Allocate shared memory */
        ZCG(mem) = zend_shared_alloc(memory_used);
        if (!ZCG(mem)) {
-               zend_accel_error(ACCEL_LOG_DEBUG, "No more memory!");
-               zend_accel_schedule_restart_if_necessary(TSRMLS_C);
-               if (zend_shared_alloc_get_largest_free_block() < MIN_FREE_MEMORY) {
-                       ZSMMG(memory_exhausted) = 1;
-               }
                zend_shared_alloc_unlock(TSRMLS_C);
                return new_persistent_script;
        }
@@ -1126,6 +1118,7 @@ static zend_persistent_script *cache_script_in_shared_memory(zend_persistent_scr
                if (!zend_accel_hash_update(&ZCSG(hash), key, key_length + 1, 1, bucket)) {
                        zend_accel_error(ACCEL_LOG_DEBUG, "No more entries in hash table!");
                        ZSMMG(memory_exhausted) = 1;
+                       zend_accel_schedule_restart(TSRMLS_C);
                }
        }
 
@@ -1469,6 +1462,7 @@ static zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int
                                persistent_script->corrupted = 1;
                                persistent_script->timestamp = 0;
                                ZSMMG(wasted_shared_memory) += persistent_script->dynamic_members.memory_consumption;
+                               zend_accel_schedule_restart_if_necessary(TSRMLS_C);
                        }
                        zend_shared_alloc_unlock(TSRMLS_C);
                        persistent_script = NULL;
@@ -1489,6 +1483,7 @@ static zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int
                                persistent_script->corrupted = 1;
                                persistent_script->timestamp = 0;
                                ZSMMG(wasted_shared_memory) += persistent_script->dynamic_members.memory_consumption;
+                               zend_accel_schedule_restart_if_necessary(TSRMLS_C);
                        }
                        zend_shared_alloc_unlock(TSRMLS_C);
                        persistent_script = NULL;
index 701820f228e2fa50e854c73fd45d10d76de04abd..c7c3b6058f23621c00a06dfb5b7d8dc690ec33bf 100644 (file)
@@ -496,7 +496,8 @@ static ZEND_FUNCTION(accelerator_get_status)
        MAKE_STD_ZVAL(statistics);
        array_init(statistics);
        add_assoc_long(statistics, "num_cached_scripts", ZCSG(hash).num_direct_entries);
-       add_assoc_long(statistics, "max_cached_scripts", ZCSG(hash).max_num_entries);
+       add_assoc_long(statistics, "num_cached_keys",    ZCSG(hash).num_entries);
+       add_assoc_long(statistics, "max_cached_keys",    ZCSG(hash).max_num_entries);
        add_assoc_long(statistics, "hits", ZCSG(hits));
        add_assoc_long(statistics, "last_restart_time", ZCSG(last_restart_time));
        add_assoc_long(statistics, "misses", ZSMMG(memory_exhausted)?ZCSG(misses):ZCSG(misses)-ZCSG(blacklist_misses));
index b6040cf0ad858bebba87d32402fa5bd85c95d825..1a983aeea702fc90359d1844617f2fd28a4aa8c5 100644 (file)
@@ -256,10 +256,29 @@ void zend_shared_alloc_shutdown(void)
 #endif
 }
 
+static size_t zend_shared_alloc_get_largest_free_block(void)
+{
+       int i;
+       size_t largest_block_size = 0;
+
+       for (i = 0; i < ZSMMG(shared_segments_count); i++) {
+               size_t block_size = ZSMMG(shared_segments)[i]->size - ZSMMG(shared_segments)[i]->pos;
+
+               if (block_size>largest_block_size) {
+                       largest_block_size = block_size;
+               }
+       }
+       return largest_block_size;
+}
+
+#define MIN_FREE_MEMORY 64*1024
+
 #define SHARED_ALLOC_FAILED() do {             \
                zend_accel_error(ACCEL_LOG_WARNING, "Not enough free shared space to allocate %ld bytes (%ld bytes free)", (long)size, (long)ZSMMG(shared_free)); \
-               ZSMMG(memory_exhausted) = 1;                    \
-               zend_accel_schedule_restart(TSRMLS_C);  \
+               if (zend_shared_alloc_get_largest_free_block() < MIN_FREE_MEMORY) { \
+                       ZSMMG(memory_exhausted) = 1;                    \
+                       zend_accel_schedule_restart(TSRMLS_C);  \
+               } \
        } while (0)
 
 void *zend_shared_alloc(size_t size)
@@ -425,21 +444,6 @@ size_t zend_shared_alloc_get_free_memory(void)
        return ZSMMG(shared_free);
 }
 
-size_t zend_shared_alloc_get_largest_free_block(void)
-{
-       int i;
-       size_t largest_block_size = 0;
-
-       for (i = 0; i < ZSMMG(shared_segments_count); i++) {
-               size_t block_size = ZSMMG(shared_segments)[i]->size - ZSMMG(shared_segments)[i]->pos;
-
-               if (block_size>largest_block_size) {
-                       largest_block_size = block_size;
-               }
-       }
-       return largest_block_size;
-}
-
 void zend_shared_alloc_save_state(void)
 {
        int i;
index 77ef723fbba75bf9e1533e983adb0d85315993df..c8411b5624c6ef52e3b67f6ff5b8e1a85d929840 100644 (file)
@@ -159,7 +159,6 @@ void *zend_shared_alloc_get_xlat_entry(const void *old);
 size_t zend_shared_alloc_get_free_memory(void);
 void zend_shared_alloc_save_state(void);
 void zend_shared_alloc_restore_state(void);
-size_t zend_shared_alloc_get_largest_free_block(void);
 const char *zend_accel_get_shared_model(void);
 
 /* memory write protection */