|
|
|
#if 0
|
|
|
|
#define SHOW_DPRINT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* {{{ macros */
|
|
|
|
#include "xc_cacher.h"
|
|
|
|
#include "xc_cache.h"
|
|
|
|
#include "xcache.h"
|
|
|
|
#include "xc_processor.h"
|
|
|
|
#include "xcache_globals.h"
|
|
|
|
#include "xcache/xc_extension.h"
|
|
|
|
#include "xcache/xc_ini.h"
|
|
|
|
#include "xcache/xc_utils.h"
|
|
|
|
#include "xcache/xc_sandbox.h"
|
|
|
|
#include "util/xc_trace.h"
|
|
|
|
#include "util/xc_vector.h"
|
|
|
|
#include "util/xc_align.h"
|
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
#include "ext/standard/info.h"
|
|
|
|
#include "ext/standard/md5.h"
|
|
|
|
#ifdef ZEND_ENGINE_2_1
|
|
|
|
# include "ext/date/php_date.h"
|
|
|
|
#endif
|
|
|
|
#ifdef ZEND_WIN32
|
|
|
|
# include <process.h>
|
|
|
|
#endif
|
|
|
|
#include "ext/standard/php_math.h"
|
|
|
|
#include "SAPI.h"
|
|
|
|
|
|
|
|
#define ECALLOC_N(x, n) ((x) = ecalloc(n, sizeof((x)[0])))
|
|
|
|
#define ECALLOC_ONE(x) ECALLOC_N(x, 1)
|
|
|
|
#define VAR_ENTRY_EXPIRED(pentry) ((pentry)->ttl && XG(request_time) > (pentry)->ctime + (time_t) (pentry)->ttl)
|
|
|
|
#define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
|
|
|
|
#define LOCK(x) xc_mutex_lock((x)->mutex)
|
|
|
|
#define UNLOCK(x) xc_mutex_unlock((x)->mutex)
|
|
|
|
|
|
|
|
#define ENTER_LOCK_EX(x) \
|
|
|
|
LOCK((x)); \
|
|
|
|
zend_try { \
|
|
|
|
do
|
|
|
|
#define LEAVE_LOCK_EX(x) \
|
|
|
|
while (0); \
|
|
|
|
} zend_catch { \
|
|
|
|
catched = 1; \
|
|
|
|
} zend_end_try(); \
|
|
|
|
UNLOCK((x))
|
|
|
|
|
|
|
|
#define ENTER_LOCK(x) do { \
|
|
|
|
int catched = 0; \
|
|
|
|
ENTER_LOCK_EX(x)
|
|
|
|
#define LEAVE_LOCK(x) \
|
|
|
|
LEAVE_LOCK_EX(x); \
|
|
|
|
if (catched) { \
|
|
|
|
zend_bailout(); \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
struct _xc_hash_t { /* {{{ */
|
|
|
|
size_t bits;
|
|
|
|
size_t size;
|
|
|
|
xc_hash_value_t mask;
|
|
|
|
};
|
|
|
|
/* }}} */
|
|
|
|
struct _xc_cached_t { /* {{{ stored in shm */
|
|
|
|
int cacheid;
|
|
|
|
|
|
|
|
time_t compiling;
|
|
|
|
time_t disabled;
|
|
|
|
zend_ulong updates;
|
|
|
|
zend_ulong hits;
|
|
|
|
zend_ulong skips;
|
|
|
|
zend_ulong ooms;
|
|
|
|
zend_ulong errors;
|
|
|
|
|
|
|
|
xc_entry_t **entries;
|
|
|
|
int entries_count;
|
|
|
|
xc_entry_data_php_t **phps;
|
|
|
|
int phps_count;
|
|
|
|
xc_entry_t *deletes;
|
|
|
|
int deletes_count;
|
|
|
|
|
|
|
|
time_t last_gc_deletes;
|
|
|
|
time_t last_gc_expires;
|
|
|
|
|
|
|
|
time_t hits_by_hour_cur_time;
|
|
|
|
zend_uint hits_by_hour_cur_slot;
|
|
|
|
zend_ulong hits_by_hour[24];
|
|
|
|
time_t hits_by_second_cur_time;
|
|
|
|
zend_uint hits_by_second_cur_slot;
|
|
|
|
zend_ulong hits_by_second[5];
|
|
|
|
};
|
|
|
|
/* }}} */
|
|
|
|
typedef struct { /* {{{ xc_cache_t: only cache info, not in shm */
|
|
|
|
int cacheid;
|
|
|
|
xc_hash_t *hcache; /* hash to cacheid */
|
|
|
|
|
|
|
|
xc_mutex_t *mutex;
|
|
|
|
xc_shm_t *shm; /* which shm contains us */
|
|
|
|
xc_allocator_t *allocator;
|
|
|
|
|
|
|
|
xc_hash_t *hentry; /* hash settings to entry */
|
|
|
|
xc_hash_t *hphp; /* hash settings to php */
|
|
|
|
xc_cached_t *cached;
|
|
|
|
} xc_cache_t;
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ globals */
|
|
|
|
static char *xc_shm_scheme = NULL;
|
|
|
|
static char *xc_mmap_path = NULL;
|
|
|
|
|
|
|
|
static zend_bool xc_admin_enable_auth = 1;
|
|
|
|
static xc_hash_t xc_php_hcache = { 0, 0, 0 };
|
|
|
|
static xc_hash_t xc_php_hentry = { 0, 0, 0 };
|
|
|
|
static xc_hash_t xc_var_hcache = { 0, 0, 0 };
|
|
|
|
static xc_hash_t xc_var_hentry = { 0, 0, 0 };
|
|
|
|
|
|
|
|
static zend_ulong xc_php_ttl = 0;
|
|
|
|
static zend_ulong xc_var_maxttl = 0;
|
|
|
|
|
|
|
|
enum { xc_deletes_gc_interval = 120 };
|
|
|
|
static zend_ulong xc_php_gc_interval = 0;
|
|
|
|
static zend_ulong xc_var_gc_interval = 0;
|
|
|
|
|
|
|
|
static char *xc_php_allocator = NULL;
|
|
|
|
static char *xc_var_allocator = NULL;
|
|
|
|
|
|
|
|
/* total size */
|
|
|
|
static zend_ulong xc_php_size = 0;
|
|
|
|
static zend_ulong xc_var_size = 0;
|
|
|
|
|
|
|
|
static xc_cache_t *xc_php_caches = NULL;
|
|
|
|
static xc_cache_t *xc_var_caches = NULL;
|
|
|
|
|
|
|
|
static zend_bool xc_initized = 0;
|
|
|
|
static time_t xc_init_time = 0;
|
|
|
|
static long unsigned xc_init_instance_id = 0;
|
|
|
|
#ifdef ZTS
|
|
|
|
static long unsigned xc_init_instance_subid = 0;
|
|
|
|
#endif
|
|
|
|
static zend_compile_file_t *old_compile_file = NULL;
|
|
|
|
|
|
|
|
static zend_bool xc_readonly_protection = 0;
|
|
|
|
|
|
|
|
static zend_ulong xc_var_namespace_mode = 0;
|
|
|
|
static char *xc_var_namespace = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
zend_bool xc_have_op_array_ctor = 0;
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t;
|
|
|
|
|
|
|
|
static void xc_holds_init(TSRMLS_D);
|
|
|
|
static void xc_holds_destroy(TSRMLS_D);
|
|
|
|
|
|
|
|
static void *xc_cache_storage(void *data, size_t size) /* {{{ */
|
|
|
|
{
|
|
|
|
xc_allocator_t *allocator = (xc_allocator_t *) data;
|
|
|
|
return allocator->vtable->malloc(allocator, size);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* any function in *_unlocked is only safe be called within locked (single thread access) area */
|
|
|
|
|
|
|
|
static void xc_php_add_unlocked(xc_cached_t *cached, xc_entry_data_php_t *php) /* {{{ */
|
|
|
|
{
|
|
|
|
xc_entry_data_php_t **head = &(cached->phps[php->hvalue]);
|
|
|
|
php->next = *head;
|
|
|
|
*head = php;
|
|
|
|
cached->phps_count ++;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static xc_entry_data_php_t *xc_php_store_unlocked(xc_cache_t *cache, xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
xc_entry_data_php_t *stored_php;
|
|
|
|
xc_processor_storage_t storage;
|
|
|
|
storage.allocator = &xc_cache_storage;
|
|
|
|
storage.allocator_data = (void *) cache->allocator;
|
|
|
|
storage.relocatediff = cache->shm->readonlydiff;
|
|
|
|
|
|
|
|
php->hits = 0;
|
|
|
|
php->refcount = 0;
|
|
|
|
stored_php = xc_processor_store_xc_entry_data_php_t(&storage, php TSRMLS_CC);
|
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
xc_entry_data_php_t *p = malloc(stored_php->size);
|
|
|
|
xc_entry_data_php_t *backup = malloc(stored_php->size);
|
|
|
|
fprintf(stderr, "%lu\n", stored_php->size);
|
|
|
|
memcpy(backup, stored_php, stored_php->size);
|
|
|
|
|
|
|
|
{
|
|
|
|
memcpy(p, stored_php, stored_php->size);
|
|
|
|
xc_processor_relocate_xc_entry_data_php_t(p, p , stored_php, stored_php TSRMLS_CC);
|
|
|
|
assert(memcmp(stored_php, backup, stored_php->size) == 0);
|
|
|
|
|
|
|
|
memcpy(stored_php, p, p->size);
|
|
|
|
xc_processor_relocate_xc_entry_data_php_t(stored_php, stored_php, p, p TSRMLS_CC);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
memcpy(p, stored_php, stored_php->size);
|
|
|
|
xc_processor_relocate_xc_entry_data_php_t(p, 0, stored_php, stored_php TSRMLS_CC);
|
|
|
|
assert(memcmp(stored_php, backup, stored_php->size) == 0);
|
|
|
|
|
|
|
|
memcpy(stored_php, p, p->size);
|
|
|
|
xc_processor_relocate_xc_entry_data_php_t(stored_php, stored_php, p, 0 TSRMLS_CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (stored_php) {
|
|
|
|
xc_php_add_unlocked(cache->cached, stored_php);
|
|
|
|
return stored_php;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cache->cached->ooms ++;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static xc_entry_data_php_t *xc_php_find_unlocked(xc_cached_t *cached, xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
xc_entry_data_php_t *p;
|
|
|
|
for (p = cached->phps[php->hvalue]; p; p = (xc_entry_data_php_t *) p->next) {
|
|
|
|
if (memcmp(&php->md5.digest, &p->md5.digest, sizeof(php->md5.digest)) == 0) {
|
|
|
|
p->hits ++;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_php_free_unlocked(xc_cache_t *cache, xc_entry_data_php_t *php) /* {{{ */
|
|
|
|
{
|
|
|
|
cache->allocator->vtable->free(cache->allocator, (xc_entry_data_php_t *)php);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_php_addref_unlocked(xc_entry_data_php_t *php) /* {{{ */
|
|
|
|
{
|
|
|
|
php->refcount ++;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_php_release_unlocked(xc_cache_t *cache, xc_entry_data_php_t *php) /* {{{ */
|
|
|
|
{
|
|
|
|
if (-- php->refcount == 0) {
|
|
|
|
xc_entry_data_php_t **pp = &(cache->cached->phps[php->hvalue]);
|
|
|
|
xc_entry_data_php_t *p;
|
|
|
|
for (p = *pp; p; pp = &(p->next), p = p->next) {
|
|
|
|
if (memcmp(&php->md5.digest, &p->md5.digest, sizeof(php->md5.digest)) == 0) {
|
|
|
|
/* unlink */
|
|
|
|
*pp = p->next;
|
|
|
|
xc_php_free_unlocked(cache, php);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static inline zend_bool xc_entry_equal_unlocked(xc_entry_type_t type, const xc_entry_t *entry1, const xc_entry_t *entry2 TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
/* this function isn't required but can be in unlocked */
|
|
|
|
switch (type) {
|
|
|
|
case XC_TYPE_PHP:
|
|
|
|
{
|
|
|
|
const xc_entry_php_t *php_entry1 = (const xc_entry_php_t *) entry1;
|
|
|
|
const xc_entry_php_t *php_entry2 = (const xc_entry_php_t *) entry2;
|
|
|
|
if (php_entry1->file_inode && php_entry2->file_inode) {
|
|
|
|
zend_bool inodeIsSame = php_entry1->file_inode == php_entry2->file_inode
|
|
|
|
&& php_entry1->file_device == php_entry2->file_device;
|
|
|
|
if (!inodeIsSame) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(strstr(entry1->name.str.val, "://") != NULL || IS_ABSOLUTE_PATH(entry1->name.str.val, entry1->name.str.len));
|
|
|
|
assert(strstr(entry1->name.str.val, "://") != NULL || IS_ABSOLUTE_PATH(entry2->name.str.val, entry2->name.str.len));
|
|
|
|
|
|
|
|
return entry1->name.str.len == entry2->name.str.len
|
|
|
|
&& memcmp(entry1->name.str.val, entry2->name.str.val, entry1->name.str.len + 1) == 0;
|
|
|
|
|
|
|
|
case XC_TYPE_VAR:
|
|
|
|
#ifdef IS_UNICODE
|
|
|
|
if (entry1->name_type != entry2->name_type) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry1->name_type == IS_UNICODE) {
|
|
|
|
return entry1->name.ustr.len == entry2->name.ustr.len
|
|
|
|
&& memcmp(entry1->name.ustr.val, entry2->name.ustr.val, (entry1->name.ustr.len + 1) * sizeof(entry1->name.ustr.val[0])) == 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return entry1->name.str.len == entry2->name.str.len
|
|
|
|
&& memcmp(entry1->name.str.val, entry2->name.str.val, entry1->name.str.len + 1) == 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_entry_add_unlocked(xc_cached_t *cached, xc_hash_value_t entryslotid, xc_entry_t *entry) /* {{{ */
|
|
|
|
{
|
|
|
|
xc_entry_t **head = &(cached->entries[entryslotid]);
|
|
|
|
entry->next = *head;
|
|
|
|
*head = entry;
|
|
|
|
cached->entries_count ++;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static xc_entry_t *xc_entry_store_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *entry TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
xc_entry_t *stored_entry;
|
|
|
|
xc_processor_storage_t storage;
|
|
|
|
storage.allocator = &xc_cache_storage;
|
|
|
|
storage.allocator_data = (void *) cache->allocator;
|
|
|
|
storage.relocatediff = cache->shm->readonlydiff;
|
|
|
|
|
|
|
|
entry->hits = 0;
|
|
|
|
entry->ctime = XG(request_time);
|
|
|
|
entry->atime = XG(request_time);
|
|
|
|
stored_entry = type == XC_TYPE_PHP
|
|
|
|
? (xc_entry_t *) xc_processor_store_xc_entry_php_t(&storage, (xc_entry_php_t *) entry TSRMLS_CC)
|
|
|
|
: (xc_entry_t *) xc_processor_store_xc_entry_var_t(&storage, (xc_entry_var_t *) entry TSRMLS_CC);
|
|
|
|
if (stored_entry) {
|
|
|
|
xc_entry_add_unlocked(cache->cached, entryslotid, stored_entry);
|
|
|
|
++cache->cached->updates;
|
|
|
|
return stored_entry;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cache->cached->ooms ++;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static xc_entry_php_t *xc_entry_php_store_unlocked(xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_php_t *entry_php TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
return (xc_entry_php_t *) xc_entry_store_unlocked(XC_TYPE_PHP, cache, entryslotid, (xc_entry_t *) entry_php TSRMLS_CC);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static xc_entry_var_t *xc_entry_var_store_unlocked(xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_var_t *entry_var TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
return (xc_entry_var_t *) xc_entry_store_unlocked(XC_TYPE_VAR, cache, entryslotid, (xc_entry_t *) entry_var TSRMLS_CC);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_entry_free_real_unlocked(xc_entry_type_t type, xc_cache_t *cache, volatile xc_entry_t *entry) /* {{{ */
|
|
|
|
{
|
|
|
|
if (type == XC_TYPE_PHP) {
|
|
|
|
xc_php_release_unlocked(cache, ((xc_entry_php_t *) entry)->php);
|
|
|
|
}
|
|
|
|
cache->allocator->vtable->free(cache->allocator, (xc_entry_t *)entry);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_entry_free_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_entry_t *entry TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
cache->cached->entries_count --;
|
|
|
|
if ((type == XC_TYPE_PHP ? ((xc_entry_php_t *) entry)->refcount : 0) == 0) {
|
|
|
|
xc_entry_free_real_unlocked(type, cache, entry);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
entry->next = cache->cached->deletes;
|
|
|
|
cache->cached->deletes = entry;
|
|
|
|
entry->dtime = XG(request_time);
|
|
|
|
cache->cached->deletes_count ++;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_entry_remove_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *entry TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
xc_entry_t **pp = &(cache->cached->entries[entryslotid]);
|
|
|
|
xc_entry_t *p;
|
|
|
|
for (p = *pp; p; pp = &(p->next), p = p->next) {
|
|
|
|
if (xc_entry_equal_unlocked(type, entry, p TSRMLS_CC)) {
|
|
|
|
/* unlink */
|
|
|
|
*pp = p->next;
|
|
|
|
xc_entry_free_unlocked(type, cache, entry TSRMLS_CC);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static xc_entry_t *xc_entry_find_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *entry TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
xc_entry_t *p;
|
|
|
|
for (p = cache->cached->entries[entryslotid]; p; p = p->next) {
|
|
|
|
if (xc_entry_equal_unlocked(type, entry, p TSRMLS_CC)) {
|
|
|
|
zend_bool fresh;
|
|
|
|
switch (type) {
|
|
|
|
case XC_TYPE_PHP:
|
|
|
|
{
|
|
|
|
xc_entry_php_t *p_php = (xc_entry_php_t *) p;
|
|
|
|
xc_entry_php_t *entry_php = (xc_entry_php_t *) entry;
|
|
|
|
fresh = p_php->file_mtime == entry_php->file_mtime && p_php->file_size == entry_php->file_size;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XC_TYPE_VAR:
|
|
|
|
{
|
|
|
|
fresh = !VAR_ENTRY_EXPIRED(p);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fresh) {
|
|
|
|
p->hits ++;
|
|
|
|
p->atime = XG(request_time);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
xc_entry_remove_unlocked(type, cache, entryslotid, p TSRMLS_CC);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_entry_hold_php_unlocked(xc_cache_t *cache, xc_entry_php_t *entry TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
TRACE("hold %lu:%s", (unsigned long) entry->file_inode, entry->entry.name.str.val);
|
|
|
|
#ifndef ZEND_WIN32
|
|
|
|
if (XG(holds_pid) != getpid()) {
|
|
|
|
xc_holds_destroy(TSRMLS_C);
|
|
|
|
xc_holds_init(TSRMLS_C);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
entry->refcount ++;
|
|
|
|
xc_vector_push_back(&XG(php_holds)[cache->cacheid], &entry);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static inline zend_uint advance_wrapped(zend_uint val, zend_uint count) /* {{{ */
|
|
|
|
{
|
|
|
|
if (val + 1 >= count) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return val + 1;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static inline void xc_counters_inc(time_t *curtime, zend_uint *curslot, time_t interval, zend_ulong *counters, zend_uint ncounters TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
time_t n = XG(request_time) / interval;
|
|
|
|
if (*curtime < n) {
|
|
|
|
zend_uint target_slot = ((zend_uint) n) % ncounters;
|
|
|
|
zend_uint slot;
|
|
|
|
for (slot = advance_wrapped(*curslot, ncounters);
|
|
|
|
slot != target_slot;
|
|
|
|
slot = advance_wrapped(slot, ncounters)) {
|
|
|
|
counters[slot] = 0;
|
|
|
|
}
|
|
|
|
counters[target_slot] = 0;
|
|
|
|
*curtime = n;
|
|
|
|
*curslot = target_slot;
|
|
|
|
}
|
|
|
|
counters[*curslot] ++;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
#define xc_countof(array) (sizeof(array) / sizeof(array[0]))
|
|
|
|
static inline void xc_cached_hit_unlocked(xc_cached_t *cached TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
cached->hits ++;
|
|
|
|
|
|
|
|
xc_counters_inc(&cached->hits_by_hour_cur_time
|
|
|
|
, &cached->hits_by_hour_cur_slot, 60 * 60
|
|
|
|
, cached->hits_by_hour
|
|
|
|
, xc_countof(cached->hits_by_hour)
|
|
|
|
TSRMLS_CC);
|
|
|
|
|
|
|
|
xc_counters_inc(&cached->hits_by_second_cur_time
|
|
|
|
, &cached->hits_by_second_cur_slot, 1
|
|
|
|
, cached->hits_by_second
|
|
|
|
, xc_countof(cached->hits_by_second)
|
|
|
|
TSRMLS_CC);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* helper function that loop through each entry */
|
|
|
|
#define XC_ENTRY_APPLY_FUNC(name) zend_bool name(xc_entry_t *entry TSRMLS_DC)
|
|
|
|
typedef XC_ENTRY_APPLY_FUNC((*cache_apply_unlocked_func_t));
|
|
|
|
static void xc_entry_apply_unlocked(xc_entry_type_t type, xc_cache_t *cache, cache_apply_unlocked_func_t apply_func TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
xc_entry_t *p, **pp;
|
|
|
|
size_t i, c;
|
|
|
|
|
|
|
|
for (i = 0, c = cache->hentry->size; i < c; i ++) {
|
|
|
|
pp = &(cache->cached->entries[i]);
|
|
|
|
for (p = *pp; p; p = *pp) {
|
|
|
|
if (apply_func(p TSRMLS_CC)) {
|
|
|
|
/* unlink */
|
|
|
|
*pp = p->next;
|
|
|
|
xc_entry_free_unlocked(type, cache, p TSRMLS_CC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pp = &(p->next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
#define XC_CACHE_APPLY_FUNC(name) void name(xc_cache_t *cache TSRMLS_DC)
|
|
|
|
/* call graph:
|
|
|
|
* xc_gc_expires_php -> xc_gc_expires_one -> xc_entry_apply_unlocked -> xc_gc_expires_php_entry_unlocked
|
|
|
|
* xc_gc_expires_var -> xc_gc_expires_one -> xc_entry_apply_unlocked -> xc_gc_expires_var_entry_unlocked
|
|
|
|
*/
|
|
|
|
static XC_ENTRY_APPLY_FUNC(xc_gc_expires_php_entry_unlocked) /* {{{ */
|
|
|
|
{
|
|
|
|
TRACE("ttl %lu, %lu %lu", (unsigned long) XG(request_time), (unsigned long) entry->atime, xc_php_ttl);
|
|
|
|
if (XG(request_time) > entry->atime + (time_t) xc_php_ttl) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static XC_ENTRY_APPLY_FUNC(xc_gc_expires_var_entry_unlocked) /* {{{ */
|
|
|
|
{
|
|
|
|
if (VAR_ENTRY_EXPIRED(entry)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_gc_expires_one(xc_entry_type_t type, xc_cache_t *cache, zend_ulong gc_interval, cache_apply_unlocked_func_t apply_func TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
TRACE("interval %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) cache->cached->last_gc_expires, gc_interval);
|
|
|
|
if (!cache->cached->disabled && XG(request_time) >= cache->cached->last_gc_expires + (time_t) gc_interval) {
|
|
|
|
ENTER_LOCK(cache) {
|
|
|
|
if (XG(request_time) >= cache->cached->last_gc_expires + (time_t) gc_interval) {
|
|
|
|
cache->cached->last_gc_expires = XG(request_time);
|
|
|
|
xc_entry_apply_unlocked(type, cache, apply_func TSRMLS_CC);
|
|
|
|
}
|
|
|
|
} LEAVE_LOCK(cache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_gc_expires_php(TSRMLS_D) /* {{{ */
|
|
|
|
{
|
|
|
|
size_t i, c;
|
|
|
|
|
|
|
|
if (!xc_php_ttl || !xc_php_gc_interval || !xc_php_caches) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, c = xc_php_hcache.size; i < c; i ++) {
|
|
|
|
xc_gc_expires_one(XC_TYPE_PHP, &xc_php_caches[i], xc_php_gc_interval, xc_gc_expires_php_entry_unlocked TSRMLS_CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_gc_expires_var(TSRMLS_D) /* {{{ */
|
|
|
|
{
|
|
|
|
size_t i, c;
|
|
|
|
|
|
|
|
if (!xc_var_gc_interval || !xc_var_caches) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, c = xc_var_hcache.size; i < c; i ++) {
|
|
|
|
xc_gc_expires_one(XC_TYPE_VAR, &xc_var_caches[i], xc_var_gc_interval, xc_gc_expires_var_entry_unlocked TSRMLS_CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static XC_CACHE_APPLY_FUNC(xc_gc_delete_unlocked) /* {{{ */
|
|
|
|
{
|
|
|
|
xc_entry_t *p, **pp;
|
|
|
|
|
|
|
|
pp = &cache->cached->deletes;
|
|
|
|
for (p = *pp; p; p = *pp) {
|
|
|
|
xc_entry_php_t *entry = (xc_entry_php_t *) p;
|
|
|
|
if (XG(request_time) - p->dtime > 3600) {
|
|
|
|
entry->refcount = 0;
|
|
|
|
/* issue warning here */
|
|
|
|
}
|
|
|
|
if (entry->refcount == 0) {
|
|
|
|
/* unlink */
|
|
|
|
*pp = p->next;
|
|
|
|
cache->cached->deletes_count --;
|
|
|
|
xc_entry_free_real_unlocked(XC_TYPE_PHP, cache, p);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pp = &(p->next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static XC_CACHE_APPLY_FUNC(xc_gc_deletes_one) /* {{{ */
|
|
|
|
{
|
|
|
|
if (!cache->cached->disabled && cache->cached->deletes && XG(request_time) - cache->cached->last_gc_deletes > xc_deletes_gc_interval) {
|
|
|
|
ENTER_LOCK(cache) {
|
|
|
|
if (cache->cached->deletes && XG(request_time) - cache->cached->last_gc_deletes > xc_deletes_gc_interval) {
|
|
|
|
cache->cached->last_gc_deletes = XG(request_time);
|
|
|
|
xc_gc_delete_unlocked(cache TSRMLS_CC);
|
|
|
|
}
|
|
|
|
} LEAVE_LOCK(cache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_gc_deletes(TSRMLS_D) /* {{{ */
|
|
|
|
{
|
|
|
|
size_t i, c;
|
|
|
|
|
|
|
|
if (xc_php_caches) {
|
|
|
|
for (i = 0, c = xc_php_hcache.size; i < c; i ++) {
|
|
|
|
xc_gc_deletes_one(&xc_php_caches[i] TSRMLS_CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xc_var_caches) {
|
|
|
|
for (i = 0, c = xc_var_hcache.size; i < c; i ++) {
|
|
|
|
xc_gc_deletes_one(&xc_var_caches[i] TSRMLS_CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* helper functions for user functions */
|
|
|
|
static void xc_fillinfo_unlocked(int cachetype, xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
zval *blocks, *hits;
|
|
|
|
size_t i;
|
|
|
|
const xc_allocator_block_t *b;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
xc_memsize_t avail = 0;
|
|
|
|
#endif
|
|
|
|
const xc_allocator_t *allocator = cache->allocator;
|
|
|
|
const xc_allocator_vtable_t *vtable = allocator->vtable;
|
|
|
|
zend_ulong interval;
|
|
|
|
const xc_cached_t *cached = cache->cached;
|
|
|
|
|
|
|
|
if (cachetype == XC_TYPE_PHP) {
|
|
|
|
interval = xc_php_ttl ? xc_php_gc_interval : 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
interval = xc_var_gc_interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("slots"), cache->hentry->size);
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("compiling"), cached->compiling);
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("disabled"), cached->disabled);
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("updates"), cached->updates);
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("misses"), cached->updates); /* deprecated */
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("hits"), cached->hits);
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("skips"), cached->skips);
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("clogs"), cached->skips); /* deprecated */
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("ooms"), cached->ooms);
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("errors"), cached->errors);
|
|
|
|
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("cached"), cached->entries_count);
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("deleted"), cached->deletes_count);
|
|
|
|
if (interval) {
|
|
|
|
time_t gc = (cached->last_gc_expires + interval) - XG(request_time);
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("gc"), gc > 0 ? gc : 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
add_assoc_null_ex(return_value, XCACHE_STRS("gc"));
|
|
|
|
}
|
|
|
|
MAKE_STD_ZVAL(hits);
|
|
|
|
array_init(hits);
|
|
|
|
for (i = 0; i < xc_countof(cached->hits_by_hour); i ++) {
|
|
|
|
add_next_index_long(hits, (long) cached->hits_by_hour[i]);
|
|
|
|
}
|
|
|
|
add_assoc_zval_ex(return_value, XCACHE_STRS("hits_by_hour"), hits);
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(hits);
|
|
|
|
array_init(hits);
|
|
|
|
for (i = 0; i < xc_countof(cached->hits_by_second); i ++) {
|
|
|
|
add_next_index_long(hits, (long) cached->hits_by_second[i]);
|
|
|
|
}
|
|
|
|
add_assoc_zval_ex(return_value, XCACHE_STRS("hits_by_second"), hits);
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(blocks);
|
|
|
|
array_init(blocks);
|
|
|
|
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("size"), vtable->size(allocator));
|
|
|
|
add_assoc_long_ex(return_value, XCACHE_STRS("avail"), vtable->avail(allocator));
|
|
|
|
add_assoc_bool_ex(return_value, XCACHE_STRS("can_readonly"), xc_readonly_protection);
|
|
|
|
|
|
|
|
for (b = vtable->freeblock_first(allocator); b; b = vtable->freeblock_next(b)) {
|
|
|
|
zval *bi;
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(bi);
|
|
|
|
array_init(bi);
|
|
|
|
|
|
|
|
add_assoc_long_ex(bi, XCACHE_STRS("size"), vtable->block_size(b));
|
|
|
|
add_assoc_long_ex(bi, XCACHE_STRS("offset"), vtable->block_offset(allocator, b));
|
|
|
|
add_next_index_zval(blocks, bi);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
avail += vtable->block_size(b);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
add_assoc_zval_ex(return_value, XCACHE_STRS("free_blocks"), blocks);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(avail == vtable->avail(allocator));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_fillentry_unlocked(xc_entry_type_t type, const xc_entry_t *entry, xc_hash_value_t entryslotid, int del, zval *list TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
zval* ei;
|
|
|
|
const xc_entry_data_php_t *php;
|
|
|
|
|
|
|
|
ALLOC_INIT_ZVAL(ei);
|
|
|
|
array_init(ei);
|
|
|
|
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("hits"), entry->hits);
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("ctime"), entry->ctime);
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("atime"), entry->atime);
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("hvalue"), entryslotid);
|
|
|
|
if (del) {
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("dtime"), entry->dtime);
|
|
|
|
}
|
|
|
|
#ifdef IS_UNICODE
|
|
|
|
do {
|
|
|
|
zval *zv;
|
|
|
|
ALLOC_INIT_ZVAL(zv);
|
|
|
|
switch (entry->name_type) {
|
|
|
|
case IS_UNICODE:
|
|
|
|
ZVAL_UNICODEL(zv, entry->name.ustr.val, entry->name.ustr.len, 1);
|
|
|
|
break;
|
|
|
|
case IS_STRING:
|
|
|
|
ZVAL_STRINGL(zv, entry->name.str.val, entry->name.str.len, 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
zv->type = entry->name_type;
|
|
|
|
add_assoc_zval_ex(ei, XCACHE_STRS("name"), zv);
|
|
|
|
} while (0);
|
|
|
|
#else
|
|
|
|
add_assoc_stringl_ex(ei, XCACHE_STRS("name"), entry->name.str.val, entry->name.str.len, 1);
|
|
|
|
#endif
|
|
|
|
switch (type) {
|
|
|
|
case XC_TYPE_PHP: {
|
|
|
|
xc_entry_php_t *entry_php = (xc_entry_php_t *) entry;
|
|
|
|
php = entry_php->php;
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("size"), entry->size + php->size);
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("refcount"), entry_php->refcount);
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("phprefcount"), php->refcount);
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("file_mtime"), entry_php->file_mtime);
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("file_size"), entry_php->file_size);
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("file_device"), entry_php->file_device);
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("file_inode"), entry_php->file_inode);
|
|
|
|
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("constinfo_cnt"), php->constinfo_cnt);
|
|
|
|
#endif
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("function_cnt"), php->funcinfo_cnt);
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("class_cnt"), php->classinfo_cnt);
|
|
|
|
#ifdef ZEND_ENGINE_2_1
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("autoglobal_cnt"),php->autoglobal_cnt);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case XC_TYPE_VAR:
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("refcount"), 0); /* for BC only */
|
|
|
|
add_assoc_long_ex(ei, XCACHE_STRS("size"), entry->size);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
add_next_index_zval(list, ei);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
static void xc_filllist_unlocked(xc_entry_type_t type, xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
zval* list;
|
|
|
|
size_t i, c;
|
|
|
|
xc_entry_t *e;
|
|
|
|
|
|
|
|
ALLOC_INIT_ZVAL(list);
|
|
|
|
array_init(list);
|
|
|
|
|
|
|
|
for (i = 0, c = cache->hentry->size; i < c; i ++) {
|
|
|
|
for (e = cache->cached->entries[i]; e; e = e->next) {
|
|
|
|
xc_fillentry_unlocked(type, e, i, 0, list TSRMLS_CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_assoc_zval(return_value, "cache_list", list);
|
|
|
|
|
|
|
|
ALLOC_INIT_ZVAL(list);
|
|
|
|
array_init(list);
|
|
|
|
for (e = cache->cached->deletes; e; e = e->next) {
|
|
|
|
xc_fillentry_unlocked(XC_TYPE_PHP, e, 0, 1, list TSRMLS_CC);
|
|
|
|
}
|
|
|
|
add_assoc_zval(return_value, "deleted_list", list);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
static zend_op_array *xc_entry_install(xc_entry_php_t *entry_php TSRMLS_DC) /* {{{ */
|
|
|
|
{
|
|
|
|
zend_uint i;
|
|
|
|
xc_entry_data_php_t *p = entry_php->php;
|
|
|
|
zend_op_array *old_active_op_array = CG(active_op_array);
|
|
|
|
#ifndef ZEND_ENGINE_2
|
|
|
|
ALLOCA_FLAG(use_heap)
|
|
|
|
/* new ptr which is stored inside CG(class_table) */
|
|
|
|
xc_cest_t **new_cest_ptrs = (xc_cest_t **)xc_do_alloca(sizeof(xc_cest_t*) * p->classinfo_cnt, use_heap);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CG(active_op_array) = p->op_array;
|
|
|
|
|
|
|
|
#ifdef HAVE_XCACHE_CONSTANT
|
|
|
|
/* install constant */
|
|
|
|
for (i = 0; i < p->constinfo_cnt; i ++) {
|
|
|
|
xc_constinfo_t *ci = &p->constinfos[i];
|
|
|
|
xc_install_constant(entry_php->entry.name.str.val, &ci->constant,
|
|
|
|
UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* install function */
|
|
|
|
for (i = 0; i < p->funcinfo_cnt; i ++) {
|
|
|
|
xc_funcinfo_t *fi = &p->funcinfos[i];
|
|
|
|
xc_install_function(entry_php->entry.name.str.val, &fi->func,
|
|
|
|
UNISW(0, fi->type), fi->key, fi->key_size, fi->h TSRMLS_CC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* install class */
|
|
|
|
for (i = 0; i < p->classinfo_cnt; i ++) {
|
|
|
|
xc_classinfo_t *ci = &p->classinfos[i];
|
|
|
|
#ifndef ZEND_ENGINE_2
|
|
|
|
zend_class_entry *ce = CestToCePtr(ci->cest);
|
|
|
|
/* fix pointer to the be which inside class_table */
|
|
|
|
if (ce->parent) {
|
|
|
|
zend_uint class_idx = (/* class_num */ (int) (long) ce->parent) - 1;
|
|
|
|
assert(class_idx < i);
|
|
|
|
ci->cest.parent = new_cest_ptrs[class_idx];
|
|
|
|
}
|
|
|
|
new_cest_ptrs[i] =
|
|
|
|
#endif
|
|
|
|
#ifdef ZEND_COMPILE_DELAYED_BINDING
|
|
|
|
xc_install_class(entry_php->entry.name.str.val, &ci->cest, -1,
|
|