Skip to content

Commit be02063

Browse files
committed
More accurate restart scheduling
1 parent 0ab356c commit be02063

File tree

4 files changed

+29
-30
lines changed

4 files changed

+29
-30
lines changed

ZendAccelerator.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ typedef int gid_t;
7171
#include <sys/stat.h>
7272
#include <errno.h>
7373

74-
#define MIN_FREE_MEMORY 64*1024
75-
7674
#define SHM_PROTECT() \
7775
do { \
7876
if (ZCG(accel_directives).protect_memory) { \
@@ -853,6 +851,7 @@ static inline int do_validate_timestamps(zend_persistent_script *persistent_scri
853851
static void zend_accel_schedule_restart_if_necessary(TSRMLS_D)
854852
{
855853
if ((((double) ZSMMG(wasted_shared_memory)) / ZCG(accel_directives).memory_consumption) >= ZCG(accel_directives).max_wasted_percentage) {
854+
ZSMMG(memory_exhausted) = 1;
856855
zend_accel_schedule_restart(TSRMLS_C);
857856
}
858857
}
@@ -1031,14 +1030,12 @@ static void zend_accel_add_key(char *key, unsigned int key_length, zend_accel_ha
10311030
if (zend_accel_hash_is_full(&ZCSG(hash))) {
10321031
zend_accel_error(ACCEL_LOG_DEBUG, "No more entries in hash table!");
10331032
ZSMMG(memory_exhausted) = 1;
1033+
zend_accel_schedule_restart(TSRMLS_C);
10341034
} else {
10351035
char *new_key = zend_shared_alloc(key_length + 1);
10361036
if (new_key) {
10371037
memcpy(new_key, key, key_length + 1);
10381038
zend_accel_hash_update(&ZCSG(hash), new_key, key_length + 1, 1, bucket);
1039-
} else {
1040-
zend_accel_error(ACCEL_LOG_DEBUG, "No more memory!");
1041-
ZSMMG(memory_exhausted) = 1;
10421039
}
10431040
}
10441041
}
@@ -1060,7 +1057,7 @@ static zend_persistent_script *cache_script_in_shared_memory(zend_persistent_scr
10601057
if (zend_accel_hash_is_full(&ZCSG(hash))) {
10611058
zend_accel_error(ACCEL_LOG_DEBUG, "No more entries in hash table!");
10621059
ZSMMG(memory_exhausted) = 1;
1063-
zend_accel_schedule_restart_if_necessary(TSRMLS_C);
1060+
zend_accel_schedule_restart(TSRMLS_C);
10641061
zend_shared_alloc_unlock(TSRMLS_C);
10651062
return new_persistent_script;
10661063
}
@@ -1088,11 +1085,6 @@ static zend_persistent_script *cache_script_in_shared_memory(zend_persistent_scr
10881085
/* Allocate shared memory */
10891086
ZCG(mem) = zend_shared_alloc(memory_used);
10901087
if (!ZCG(mem)) {
1091-
zend_accel_error(ACCEL_LOG_DEBUG, "No more memory!");
1092-
zend_accel_schedule_restart_if_necessary(TSRMLS_C);
1093-
if (zend_shared_alloc_get_largest_free_block() < MIN_FREE_MEMORY) {
1094-
ZSMMG(memory_exhausted) = 1;
1095-
}
10961088
zend_shared_alloc_unlock(TSRMLS_C);
10971089
return new_persistent_script;
10981090
}
@@ -1126,6 +1118,7 @@ static zend_persistent_script *cache_script_in_shared_memory(zend_persistent_scr
11261118
if (!zend_accel_hash_update(&ZCSG(hash), key, key_length + 1, 1, bucket)) {
11271119
zend_accel_error(ACCEL_LOG_DEBUG, "No more entries in hash table!");
11281120
ZSMMG(memory_exhausted) = 1;
1121+
zend_accel_schedule_restart(TSRMLS_C);
11291122
}
11301123
}
11311124

@@ -1469,6 +1462,7 @@ static zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int
14691462
persistent_script->corrupted = 1;
14701463
persistent_script->timestamp = 0;
14711464
ZSMMG(wasted_shared_memory) += persistent_script->dynamic_members.memory_consumption;
1465+
zend_accel_schedule_restart_if_necessary(TSRMLS_C);
14721466
}
14731467
zend_shared_alloc_unlock(TSRMLS_C);
14741468
persistent_script = NULL;
@@ -1489,6 +1483,7 @@ static zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int
14891483
persistent_script->corrupted = 1;
14901484
persistent_script->timestamp = 0;
14911485
ZSMMG(wasted_shared_memory) += persistent_script->dynamic_members.memory_consumption;
1486+
zend_accel_schedule_restart_if_necessary(TSRMLS_C);
14921487
}
14931488
zend_shared_alloc_unlock(TSRMLS_C);
14941489
persistent_script = NULL;

zend_accelerator_module.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,8 @@ static ZEND_FUNCTION(accelerator_get_status)
496496
MAKE_STD_ZVAL(statistics);
497497
array_init(statistics);
498498
add_assoc_long(statistics, "num_cached_scripts", ZCSG(hash).num_direct_entries);
499-
add_assoc_long(statistics, "max_cached_scripts", ZCSG(hash).max_num_entries);
499+
add_assoc_long(statistics, "num_cached_keys", ZCSG(hash).num_entries);
500+
add_assoc_long(statistics, "max_cached_keys", ZCSG(hash).max_num_entries);
500501
add_assoc_long(statistics, "hits", ZCSG(hits));
501502
add_assoc_long(statistics, "last_restart_time", ZCSG(last_restart_time));
502503
add_assoc_long(statistics, "misses", ZSMMG(memory_exhausted)?ZCSG(misses):ZCSG(misses)-ZCSG(blacklist_misses));

zend_shared_alloc.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,29 @@ void zend_shared_alloc_shutdown(void)
256256
#endif
257257
}
258258

259+
static size_t zend_shared_alloc_get_largest_free_block(void)
260+
{
261+
int i;
262+
size_t largest_block_size = 0;
263+
264+
for (i = 0; i < ZSMMG(shared_segments_count); i++) {
265+
size_t block_size = ZSMMG(shared_segments)[i]->size - ZSMMG(shared_segments)[i]->pos;
266+
267+
if (block_size>largest_block_size) {
268+
largest_block_size = block_size;
269+
}
270+
}
271+
return largest_block_size;
272+
}
273+
274+
#define MIN_FREE_MEMORY 64*1024
275+
259276
#define SHARED_ALLOC_FAILED() do { \
260277
zend_accel_error(ACCEL_LOG_WARNING, "Not enough free shared space to allocate %ld bytes (%ld bytes free)", (long)size, (long)ZSMMG(shared_free)); \
261-
ZSMMG(memory_exhausted) = 1; \
262-
zend_accel_schedule_restart(TSRMLS_C); \
278+
if (zend_shared_alloc_get_largest_free_block() < MIN_FREE_MEMORY) { \
279+
ZSMMG(memory_exhausted) = 1; \
280+
zend_accel_schedule_restart(TSRMLS_C); \
281+
} \
263282
} while (0)
264283

265284
void *zend_shared_alloc(size_t size)
@@ -425,21 +444,6 @@ size_t zend_shared_alloc_get_free_memory(void)
425444
return ZSMMG(shared_free);
426445
}
427446

428-
size_t zend_shared_alloc_get_largest_free_block(void)
429-
{
430-
int i;
431-
size_t largest_block_size = 0;
432-
433-
for (i = 0; i < ZSMMG(shared_segments_count); i++) {
434-
size_t block_size = ZSMMG(shared_segments)[i]->size - ZSMMG(shared_segments)[i]->pos;
435-
436-
if (block_size>largest_block_size) {
437-
largest_block_size = block_size;
438-
}
439-
}
440-
return largest_block_size;
441-
}
442-
443447
void zend_shared_alloc_save_state(void)
444448
{
445449
int i;

zend_shared_alloc.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ void *zend_shared_alloc_get_xlat_entry(const void *old);
159159
size_t zend_shared_alloc_get_free_memory(void);
160160
void zend_shared_alloc_save_state(void);
161161
void zend_shared_alloc_restore_state(void);
162-
size_t zend_shared_alloc_get_largest_free_block(void);
163162
const char *zend_accel_get_shared_model(void);
164163

165164
/* memory write protection */

0 commit comments

Comments
 (0)