| 1 | |
|---|
| 2 | #if 0 |
|---|
| 3 | #define XCACHE_DEBUG |
|---|
| 4 | #endif |
|---|
| 5 | |
|---|
| 6 | #if 0 |
|---|
| 7 | #define SHOW_DPRINT |
|---|
| 8 | #endif |
|---|
| 9 | |
|---|
| 10 | /* {{{ macros */ |
|---|
| 11 | #include <stdlib.h> |
|---|
| 12 | #include <stdio.h> |
|---|
| 13 | #include <string.h> |
|---|
| 14 | |
|---|
| 15 | #include <signal.h> |
|---|
| 16 | |
|---|
| 17 | #include "php.h" |
|---|
| 18 | #include "ext/standard/info.h" |
|---|
| 19 | #include "ext/standard/md5.h" |
|---|
| 20 | #include "ext/standard/php_math.h" |
|---|
| 21 | #include "ext/standard/php_string.h" |
|---|
| 22 | #include "zend_extensions.h" |
|---|
| 23 | #include "SAPI.h" |
|---|
| 24 | |
|---|
| 25 | #include "xcache.h" |
|---|
| 26 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 27 | #include "ext/date/php_date.h" |
|---|
| 28 | #endif |
|---|
| 29 | #include "optimizer.h" |
|---|
| 30 | #include "coverager.h" |
|---|
| 31 | #include "disassembler.h" |
|---|
| 32 | #include "align.h" |
|---|
| 33 | #include "stack.h" |
|---|
| 34 | #include "xcache_globals.h" |
|---|
| 35 | #include "processor.h" |
|---|
| 36 | #include "const_string.h" |
|---|
| 37 | #include "opcode_spec.h" |
|---|
| 38 | #include "utils.h" |
|---|
| 39 | |
|---|
| 40 | #define VAR_ENTRY_EXPIRED(pentry) ((pentry)->ttl && XG(request_time) > pentry->ctime + (pentry)->ttl) |
|---|
| 41 | #define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0) |
|---|
| 42 | #define LOCK(x) xc_lock(x->lck) |
|---|
| 43 | #define UNLOCK(x) xc_unlock(x->lck) |
|---|
| 44 | |
|---|
| 45 | #define ENTER_LOCK_EX(x) \ |
|---|
| 46 | xc_lock(x->lck); \ |
|---|
| 47 | zend_try { \ |
|---|
| 48 | do |
|---|
| 49 | #define LEAVE_LOCK_EX(x) \ |
|---|
| 50 | while (0); \ |
|---|
| 51 | } zend_catch { \ |
|---|
| 52 | catched = 1; \ |
|---|
| 53 | } zend_end_try(); \ |
|---|
| 54 | xc_unlock(x->lck) |
|---|
| 55 | |
|---|
| 56 | #define ENTER_LOCK(x) do { \ |
|---|
| 57 | int catched = 0; \ |
|---|
| 58 | ENTER_LOCK_EX(x) |
|---|
| 59 | #define LEAVE_LOCK(x) \ |
|---|
| 60 | LEAVE_LOCK_EX(x); \ |
|---|
| 61 | if (catched) { \ |
|---|
| 62 | zend_bailout(); \ |
|---|
| 63 | } \ |
|---|
| 64 | } while(0) |
|---|
| 65 | |
|---|
| 66 | /* }}} */ |
|---|
| 67 | |
|---|
| 68 | /* {{{ globals */ |
|---|
| 69 | static char *xc_shm_scheme = NULL; |
|---|
| 70 | static char *xc_mmap_path = NULL; |
|---|
| 71 | static char *xc_coredump_dir = NULL; |
|---|
| 72 | |
|---|
| 73 | static xc_hash_t xc_php_hcache = {0}; |
|---|
| 74 | static xc_hash_t xc_php_hentry = {0}; |
|---|
| 75 | static xc_hash_t xc_var_hcache = {0}; |
|---|
| 76 | static xc_hash_t xc_var_hentry = {0}; |
|---|
| 77 | |
|---|
| 78 | static zend_ulong xc_php_ttl = 0; |
|---|
| 79 | static zend_ulong xc_var_maxttl = 0; |
|---|
| 80 | |
|---|
| 81 | enum { xc_deletes_gc_interval = 120 }; |
|---|
| 82 | static zend_ulong xc_php_gc_interval = 0; |
|---|
| 83 | static zend_ulong xc_var_gc_interval = 0; |
|---|
| 84 | |
|---|
| 85 | /* total size */ |
|---|
| 86 | static zend_ulong xc_php_size = 0; |
|---|
| 87 | static zend_ulong xc_var_size = 0; |
|---|
| 88 | |
|---|
| 89 | static xc_cache_t **xc_php_caches = NULL; |
|---|
| 90 | static xc_cache_t **xc_var_caches = NULL; |
|---|
| 91 | |
|---|
| 92 | static zend_bool xc_initized = 0; |
|---|
| 93 | static time_t xc_init_time = 0; |
|---|
| 94 | static long unsigned xc_init_instance_id = 0; |
|---|
| 95 | #ifdef ZTS |
|---|
| 96 | static long unsigned xc_init_instance_subid = 0; |
|---|
| 97 | #endif |
|---|
| 98 | static zend_compile_file_t *origin_compile_file = NULL; |
|---|
| 99 | static zend_compile_file_t *old_compile_file = NULL; |
|---|
| 100 | static zend_llist_element *xc_llist_zend_extension = NULL; |
|---|
| 101 | |
|---|
| 102 | static zend_bool xc_test = 0; |
|---|
| 103 | static zend_bool xc_readonly_protection = 0; |
|---|
| 104 | |
|---|
| 105 | zend_bool xc_have_op_array_ctor = 0; |
|---|
| 106 | |
|---|
| 107 | static zend_bool xc_module_gotup = 0; |
|---|
| 108 | static zend_bool xc_zend_extension_gotup = 0; |
|---|
| 109 | static zend_bool xc_zend_extension_faked = 0; |
|---|
| 110 | #if !COMPILE_DL_XCACHE |
|---|
| 111 | # define zend_extension_entry xcache_zend_extension_entry |
|---|
| 112 | #endif |
|---|
| 113 | ZEND_DLEXPORT zend_extension zend_extension_entry; |
|---|
| 114 | ZEND_DECLARE_MODULE_GLOBALS(xcache); |
|---|
| 115 | /* }}} */ |
|---|
| 116 | |
|---|
| 117 | /* any function in *_dmz is only safe be called within locked(single thread) area */ |
|---|
| 118 | |
|---|
| 119 | static void xc_php_add_dmz(xc_entry_data_php_t *php) /* {{{ */ |
|---|
| 120 | { |
|---|
| 121 | xc_entry_data_php_t **head = &(php->cache->phps[php->hvalue]); |
|---|
| 122 | php->next = *head; |
|---|
| 123 | *head = php; |
|---|
| 124 | php->cache->phps_count ++; |
|---|
| 125 | } |
|---|
| 126 | /* }}} */ |
|---|
| 127 | static xc_entry_data_php_t *xc_php_store_dmz(xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
|---|
| 128 | { |
|---|
| 129 | xc_entry_data_php_t *stored_php; |
|---|
| 130 | |
|---|
| 131 | php->hits = 0; |
|---|
| 132 | php->refcount = 0; |
|---|
| 133 | stored_php = xc_processor_store_xc_entry_data_php_t(php TSRMLS_CC); |
|---|
| 134 | if (stored_php) { |
|---|
| 135 | xc_php_add_dmz(stored_php); |
|---|
| 136 | return stored_php; |
|---|
| 137 | } |
|---|
| 138 | else { |
|---|
| 139 | php->cache->ooms ++; |
|---|
| 140 | return NULL; |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | /* }}} */ |
|---|
| 144 | static xc_entry_data_php_t *xc_php_find_dmz(xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
|---|
| 145 | { |
|---|
| 146 | xc_entry_data_php_t *p; |
|---|
| 147 | for (p = php->cache->phps[php->hvalue]; p; p = p->next) { |
|---|
| 148 | if (memcmp(php->md5, p->md5, sizeof(php->md5)) == 0) { |
|---|
| 149 | p->hits ++; |
|---|
| 150 | return p; |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | return NULL; |
|---|
| 154 | } |
|---|
| 155 | /* }}} */ |
|---|
| 156 | static void xc_php_free_dmz(xc_entry_data_php_t *php) /* {{{ */ |
|---|
| 157 | { |
|---|
| 158 | php->cache->mem->handlers->free(php->cache->mem, (xc_entry_data_php_t *)php); |
|---|
| 159 | } |
|---|
| 160 | /* }}} */ |
|---|
| 161 | static void xc_php_addref_dmz(xc_entry_data_php_t *php) /* {{{ */ |
|---|
| 162 | { |
|---|
| 163 | php->refcount ++; |
|---|
| 164 | } |
|---|
| 165 | /* }}} */ |
|---|
| 166 | static void xc_php_release_dmz(xc_entry_data_php_t *php) /* {{{ */ |
|---|
| 167 | { |
|---|
| 168 | if (-- php->refcount == 0) { |
|---|
| 169 | xc_entry_data_php_t **pp = &(php->cache->phps[php->hvalue]); |
|---|
| 170 | xc_entry_data_php_t *p; |
|---|
| 171 | for (p = *pp; p; pp = &(p->next), p = p->next) { |
|---|
| 172 | if (memcmp(php->md5, p->md5, sizeof(php->md5)) == 0) { |
|---|
| 173 | /* unlink */ |
|---|
| 174 | *pp = p->next; |
|---|
| 175 | xc_php_free_dmz(php); |
|---|
| 176 | return; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | assert(0); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | /* }}} */ |
|---|
| 183 | |
|---|
| 184 | static inline int xc_entry_equal_dmz(xc_entry_t *a, xc_entry_t *b) /* {{{ */ |
|---|
| 185 | { |
|---|
| 186 | /* this function isn't required but can be in dmz */ |
|---|
| 187 | |
|---|
| 188 | if (a->type != b->type) { |
|---|
| 189 | return 0; |
|---|
| 190 | } |
|---|
| 191 | switch (a->type) { |
|---|
| 192 | case XC_TYPE_PHP: |
|---|
| 193 | #ifdef HAVE_INODE |
|---|
| 194 | do { |
|---|
| 195 | if (a->inode) { |
|---|
| 196 | return a->inode == b->inode |
|---|
| 197 | && a->device == b->device; |
|---|
| 198 | } |
|---|
| 199 | } while(0); |
|---|
| 200 | #endif |
|---|
| 201 | /* fall */ |
|---|
| 202 | |
|---|
| 203 | case XC_TYPE_VAR: |
|---|
| 204 | do { |
|---|
| 205 | #ifdef IS_UNICODE |
|---|
| 206 | if (a->name_type == IS_UNICODE) { |
|---|
| 207 | if (a->name.ustr.len != b->name.ustr.len) { |
|---|
| 208 | return 0; |
|---|
| 209 | } |
|---|
| 210 | return memcmp(a->name.ustr.val, b->name.ustr.val, (a->name.ustr.len + 1) * sizeof(UChar)) == 0; |
|---|
| 211 | } |
|---|
| 212 | else { |
|---|
| 213 | return memcmp(a->name.str.val, b->name.str.val, a->name.str.len + 1) == 0; |
|---|
| 214 | } |
|---|
| 215 | #else |
|---|
| 216 | return memcmp(a->name.str.val, b->name.str.val, a->name.str.len + 1) == 0; |
|---|
| 217 | #endif |
|---|
| 218 | |
|---|
| 219 | } while(0); |
|---|
| 220 | default: |
|---|
| 221 | assert(0); |
|---|
| 222 | } |
|---|
| 223 | return 0; |
|---|
| 224 | } |
|---|
| 225 | /* }}} */ |
|---|
| 226 | static void xc_entry_add_dmz(xc_entry_t *xce) /* {{{ */ |
|---|
| 227 | { |
|---|
| 228 | xc_entry_t **head = &(xce->cache->entries[xce->hvalue]); |
|---|
| 229 | xce->next = *head; |
|---|
| 230 | *head = xce; |
|---|
| 231 | xce->cache->entries_count ++; |
|---|
| 232 | } |
|---|
| 233 | /* }}} */ |
|---|
| 234 | static xc_entry_t *xc_entry_store_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 235 | { |
|---|
| 236 | xc_entry_t *stored_xce; |
|---|
| 237 | |
|---|
| 238 | xce->hits = 0; |
|---|
| 239 | xce->ctime = XG(request_time); |
|---|
| 240 | xce->atime = XG(request_time); |
|---|
| 241 | stored_xce = xc_processor_store_xc_entry_t(xce TSRMLS_CC); |
|---|
| 242 | if (stored_xce) { |
|---|
| 243 | xc_entry_add_dmz(stored_xce); |
|---|
| 244 | return stored_xce; |
|---|
| 245 | } |
|---|
| 246 | else { |
|---|
| 247 | xce->cache->ooms ++; |
|---|
| 248 | return NULL; |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | /* }}} */ |
|---|
| 252 | static void xc_entry_free_real_dmz(volatile xc_entry_t *xce) /* {{{ */ |
|---|
| 253 | { |
|---|
| 254 | if (xce->type == XC_TYPE_PHP) { |
|---|
| 255 | xc_php_release_dmz(xce->data.php); |
|---|
| 256 | } |
|---|
| 257 | xce->cache->mem->handlers->free(xce->cache->mem, (xc_entry_t *)xce); |
|---|
| 258 | } |
|---|
| 259 | /* }}} */ |
|---|
| 260 | static void xc_entry_free_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 261 | { |
|---|
| 262 | xce->cache->entries_count --; |
|---|
| 263 | if (xce->refcount == 0) { |
|---|
| 264 | xc_entry_free_real_dmz(xce); |
|---|
| 265 | } |
|---|
| 266 | else { |
|---|
| 267 | xce->next = xce->cache->deletes; |
|---|
| 268 | xce->cache->deletes = xce; |
|---|
| 269 | xce->dtime = XG(request_time); |
|---|
| 270 | xce->cache->deletes_count ++; |
|---|
| 271 | } |
|---|
| 272 | return; |
|---|
| 273 | } |
|---|
| 274 | /* }}} */ |
|---|
| 275 | static void xc_entry_remove_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 276 | { |
|---|
| 277 | xc_entry_t **pp = &(xce->cache->entries[xce->hvalue]); |
|---|
| 278 | xc_entry_t *p; |
|---|
| 279 | for (p = *pp; p; pp = &(p->next), p = p->next) { |
|---|
| 280 | if (xc_entry_equal_dmz(xce, p)) { |
|---|
| 281 | /* unlink */ |
|---|
| 282 | *pp = p->next; |
|---|
| 283 | xc_entry_free_dmz(xce TSRMLS_CC); |
|---|
| 284 | return; |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | assert(0); |
|---|
| 288 | } |
|---|
| 289 | /* }}} */ |
|---|
| 290 | static xc_entry_t *xc_entry_find_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 291 | { |
|---|
| 292 | xc_entry_t *p; |
|---|
| 293 | for (p = xce->cache->entries[xce->hvalue]; p; p = p->next) { |
|---|
| 294 | if (xc_entry_equal_dmz(xce, p)) { |
|---|
| 295 | if (p->type == XC_TYPE_VAR || /* PHP */ p->mtime == xce->mtime && p->data.php->sourcesize == xce->data.php->sourcesize) { |
|---|
| 296 | p->hits ++; |
|---|
| 297 | p->atime = XG(request_time); |
|---|
| 298 | return p; |
|---|
| 299 | } |
|---|
| 300 | else { |
|---|
| 301 | xc_entry_remove_dmz(p TSRMLS_CC); |
|---|
| 302 | return NULL; |
|---|
| 303 | } |
|---|
| 304 | } |
|---|
| 305 | } |
|---|
| 306 | return NULL; |
|---|
| 307 | } |
|---|
| 308 | /* }}} */ |
|---|
| 309 | static void xc_entry_hold_php_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 310 | { |
|---|
| 311 | TRACE("hold %s", xce->name.str.val); |
|---|
| 312 | xce->refcount ++; |
|---|
| 313 | xc_stack_push(&XG(php_holds)[xce->cache->cacheid], (void *)xce); |
|---|
| 314 | } |
|---|
| 315 | /* }}} */ |
|---|
| 316 | #if 0 |
|---|
| 317 | static void xc_entry_hold_var_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 318 | { |
|---|
| 319 | xce->refcount ++; |
|---|
| 320 | xc_stack_push(&XG(var_holds)[xce->cache->cacheid], (void *)xce); |
|---|
| 321 | } |
|---|
| 322 | /* }}} */ |
|---|
| 323 | #endif |
|---|
| 324 | static inline zend_uint advance_wrapped(zend_uint val, zend_uint count) /* {{{ */ |
|---|
| 325 | { |
|---|
| 326 | if (val + 1 >= count) { |
|---|
| 327 | return 0; |
|---|
| 328 | } |
|---|
| 329 | return val + 1; |
|---|
| 330 | } |
|---|
| 331 | /* }}} */ |
|---|
| 332 | static void xc_counters_inc(time_t *curtime, zend_uint *curslot, time_t period, zend_ulong *counters, zend_uint count TSRMLS_DC) /* {{{ */ |
|---|
| 333 | { |
|---|
| 334 | time_t n = XG(request_time) / period; |
|---|
| 335 | if (*curtime != n) { |
|---|
| 336 | zend_uint target_slot = n % count; |
|---|
| 337 | if (n - *curtime > period) { |
|---|
| 338 | memset(counters, 0, sizeof(counters[0]) * count); |
|---|
| 339 | } |
|---|
| 340 | else { |
|---|
| 341 | zend_uint slot; |
|---|
| 342 | for (slot = advance_wrapped(*curslot, count); |
|---|
| 343 | slot != target_slot; |
|---|
| 344 | slot = advance_wrapped(slot, count)) { |
|---|
| 345 | counters[slot] = 0; |
|---|
| 346 | } |
|---|
| 347 | counters[target_slot] = 0; |
|---|
| 348 | } |
|---|
| 349 | *curtime = n; |
|---|
| 350 | *curslot = target_slot; |
|---|
| 351 | } |
|---|
| 352 | counters[*curslot] ++; |
|---|
| 353 | } |
|---|
| 354 | /* }}} */ |
|---|
| 355 | static void xc_cache_hit_dmz(xc_cache_t *cache TSRMLS_DC) /* {{{ */ |
|---|
| 356 | { |
|---|
| 357 | cache->hits ++; |
|---|
| 358 | |
|---|
| 359 | xc_counters_inc(&cache->hits_by_hour_cur_time |
|---|
| 360 | , &cache->hits_by_hour_cur_slot, 60 * 60 |
|---|
| 361 | , cache->hits_by_hour |
|---|
| 362 | , sizeof(cache->hits_by_hour) / sizeof(cache->hits_by_hour[0]) |
|---|
| 363 | TSRMLS_CC); |
|---|
| 364 | |
|---|
| 365 | xc_counters_inc(&cache->hits_by_second_cur_time |
|---|
| 366 | , &cache->hits_by_second_cur_slot |
|---|
| 367 | , 1 |
|---|
| 368 | , cache->hits_by_second |
|---|
| 369 | , sizeof(cache->hits_by_second) / sizeof(cache->hits_by_second[0]) |
|---|
| 370 | TSRMLS_CC); |
|---|
| 371 | } |
|---|
| 372 | /* }}} */ |
|---|
| 373 | |
|---|
| 374 | /* helper function that loop through each entry */ |
|---|
| 375 | #define XC_ENTRY_APPLY_FUNC(name) int name(xc_entry_t *entry TSRMLS_DC) |
|---|
| 376 | typedef XC_ENTRY_APPLY_FUNC((*cache_apply_dmz_func_t)); |
|---|
| 377 | static void xc_entry_apply_dmz(xc_cache_t *cache, cache_apply_dmz_func_t apply_func TSRMLS_DC) /* {{{ */ |
|---|
| 378 | { |
|---|
| 379 | xc_entry_t *p, **pp; |
|---|
| 380 | int i, c; |
|---|
| 381 | |
|---|
| 382 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
|---|
| 383 | pp = &(cache->entries[i]); |
|---|
| 384 | for (p = *pp; p; p = *pp) { |
|---|
| 385 | if (apply_func(p TSRMLS_CC)) { |
|---|
| 386 | /* unlink */ |
|---|
| 387 | *pp = p->next; |
|---|
| 388 | xc_entry_free_dmz(p TSRMLS_CC); |
|---|
| 389 | } |
|---|
| 390 | else { |
|---|
| 391 | pp = &(p->next); |
|---|
| 392 | } |
|---|
| 393 | } |
|---|
| 394 | } |
|---|
| 395 | } |
|---|
| 396 | /* }}} */ |
|---|
| 397 | |
|---|
| 398 | #define XC_CACHE_APPLY_FUNC(name) void name(xc_cache_t *cache TSRMLS_DC) |
|---|
| 399 | /* call graph: |
|---|
| 400 | * xc_gc_expires_php -> xc_gc_expires_one -> xc_entry_apply_dmz -> xc_gc_expires_php_entry_dmz |
|---|
| 401 | * xc_gc_expires_var -> xc_gc_expires_one -> xc_entry_apply_dmz -> xc_gc_expires_var_entry_dmz |
|---|
| 402 | */ |
|---|
| 403 | static XC_ENTRY_APPLY_FUNC(xc_gc_expires_php_entry_dmz) /* {{{ */ |
|---|
| 404 | { |
|---|
| 405 | TRACE("ttl %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) entry->atime, xc_php_ttl); |
|---|
| 406 | if (XG(request_time) > entry->atime + xc_php_ttl) { |
|---|
| 407 | return 1; |
|---|
| 408 | } |
|---|
| 409 | return 0; |
|---|
| 410 | } |
|---|
| 411 | /* }}} */ |
|---|
| 412 | static XC_ENTRY_APPLY_FUNC(xc_gc_expires_var_entry_dmz) /* {{{ */ |
|---|
| 413 | { |
|---|
| 414 | if (VAR_ENTRY_EXPIRED(entry)) { |
|---|
| 415 | return 1; |
|---|
| 416 | } |
|---|
| 417 | return 0; |
|---|
| 418 | } |
|---|
| 419 | /* }}} */ |
|---|
| 420 | static void xc_gc_expires_one(xc_cache_t *cache, zend_ulong gc_interval, cache_apply_dmz_func_t apply_func TSRMLS_DC) /* {{{ */ |
|---|
| 421 | { |
|---|
| 422 | TRACE("interval %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) cache->last_gc_expires, gc_interval); |
|---|
| 423 | if (XG(request_time) - cache->last_gc_expires >= gc_interval) { |
|---|
| 424 | ENTER_LOCK(cache) { |
|---|
| 425 | if (XG(request_time) - cache->last_gc_expires >= gc_interval) { |
|---|
| 426 | cache->last_gc_expires = XG(request_time); |
|---|
| 427 | xc_entry_apply_dmz(cache, apply_func TSRMLS_CC); |
|---|
| 428 | } |
|---|
| 429 | } LEAVE_LOCK(cache); |
|---|
| 430 | } |
|---|
| 431 | } |
|---|
| 432 | /* }}} */ |
|---|
| 433 | static void xc_gc_expires_php(TSRMLS_D) /* {{{ */ |
|---|
| 434 | { |
|---|
| 435 | int i, c; |
|---|
| 436 | |
|---|
| 437 | if (!xc_php_ttl || !xc_php_gc_interval || !xc_php_caches) { |
|---|
| 438 | return; |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | for (i = 0, c = xc_php_hcache.size; i < c; i ++) { |
|---|
| 442 | xc_gc_expires_one(xc_php_caches[i], xc_php_gc_interval, xc_gc_expires_php_entry_dmz TSRMLS_CC); |
|---|
| 443 | } |
|---|
| 444 | } |
|---|
| 445 | /* }}} */ |
|---|
| 446 | static void xc_gc_expires_var(TSRMLS_D) /* {{{ */ |
|---|
| 447 | { |
|---|
| 448 | int i, c; |
|---|
| 449 | |
|---|
| 450 | if (!xc_var_gc_interval || !xc_var_caches) { |
|---|
| 451 | return; |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | for (i = 0, c = xc_var_hcache.size; i < c; i ++) { |
|---|
| 455 | xc_gc_expires_one(xc_var_caches[i], xc_var_gc_interval, xc_gc_expires_var_entry_dmz TSRMLS_CC); |
|---|
| 456 | } |
|---|
| 457 | } |
|---|
| 458 | /* }}} */ |
|---|
| 459 | |
|---|
| 460 | static XC_CACHE_APPLY_FUNC(xc_gc_delete_dmz) /* {{{ */ |
|---|
| 461 | { |
|---|
| 462 | xc_entry_t *p, **pp; |
|---|
| 463 | |
|---|
| 464 | pp = &cache->deletes; |
|---|
| 465 | for (p = *pp; p; p = *pp) { |
|---|
| 466 | if (XG(request_time) - p->dtime > 3600) { |
|---|
| 467 | p->refcount = 0; |
|---|
| 468 | /* issue warning here */ |
|---|
| 469 | } |
|---|
| 470 | if (p->refcount == 0) { |
|---|
| 471 | /* unlink */ |
|---|
| 472 | *pp = p->next; |
|---|
| 473 | cache->deletes_count --; |
|---|
| 474 | xc_entry_free_real_dmz(p); |
|---|
| 475 | } |
|---|
| 476 | else { |
|---|
| 477 | pp = &(p->next); |
|---|
| 478 | } |
|---|
| 479 | } |
|---|
| 480 | } |
|---|
| 481 | /* }}} */ |
|---|
| 482 | static XC_CACHE_APPLY_FUNC(xc_gc_deletes_one) /* {{{ */ |
|---|
| 483 | { |
|---|
| 484 | if (cache->deletes && XG(request_time) - cache->last_gc_deletes > xc_deletes_gc_interval) { |
|---|
| 485 | ENTER_LOCK(cache) { |
|---|
| 486 | if (cache->deletes && XG(request_time) - cache->last_gc_deletes > xc_deletes_gc_interval) { |
|---|
| 487 | cache->last_gc_deletes = XG(request_time); |
|---|
| 488 | xc_gc_delete_dmz(cache TSRMLS_CC); |
|---|
| 489 | } |
|---|
| 490 | } LEAVE_LOCK(cache); |
|---|
| 491 | } |
|---|
| 492 | } |
|---|
| 493 | /* }}} */ |
|---|
| 494 | static void xc_gc_deletes(TSRMLS_D) /* {{{ */ |
|---|
| 495 | { |
|---|
| 496 | int i, c; |
|---|
| 497 | |
|---|
| 498 | if (xc_php_caches) { |
|---|
| 499 | for (i = 0, c = xc_php_hcache.size; i < c; i ++) { |
|---|
| 500 | xc_gc_deletes_one(xc_php_caches[i] TSRMLS_CC); |
|---|
| 501 | } |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | if (xc_var_caches) { |
|---|
| 505 | for (i = 0, c = xc_var_hcache.size; i < c; i ++) { |
|---|
| 506 | xc_gc_deletes_one(xc_var_caches[i] TSRMLS_CC); |
|---|
| 507 | } |
|---|
| 508 | } |
|---|
| 509 | } |
|---|
| 510 | /* }}} */ |
|---|
| 511 | |
|---|
| 512 | /* helper functions for user functions */ |
|---|
| 513 | static void xc_fillinfo_dmz(int cachetype, xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */ |
|---|
| 514 | { |
|---|
| 515 | zval *blocks, *hits; |
|---|
| 516 | int i; |
|---|
| 517 | const xc_block_t *b; |
|---|
| 518 | #ifndef NDEBUG |
|---|
| 519 | xc_memsize_t avail = 0; |
|---|
| 520 | #endif |
|---|
| 521 | xc_mem_t *mem = cache->mem; |
|---|
| 522 | const xc_mem_handlers_t *handlers = mem->handlers; |
|---|
| 523 | zend_ulong interval; |
|---|
| 524 | if (cachetype == XC_TYPE_PHP) { |
|---|
| 525 | interval = xc_php_ttl ? xc_php_gc_interval : 0; |
|---|
| 526 | } |
|---|
| 527 | else { |
|---|
| 528 | interval = xc_var_gc_interval; |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | add_assoc_long_ex(return_value, ZEND_STRS("slots"), cache->hentry->size); |
|---|
| 532 | add_assoc_long_ex(return_value, ZEND_STRS("compiling"), cache->compiling); |
|---|
| 533 | add_assoc_long_ex(return_value, ZEND_STRS("misses"), cache->misses); |
|---|
| 534 | add_assoc_long_ex(return_value, ZEND_STRS("hits"), cache->hits); |
|---|
| 535 | add_assoc_long_ex(return_value, ZEND_STRS("clogs"), cache->clogs); |
|---|
| 536 | add_assoc_long_ex(return_value, ZEND_STRS("ooms"), cache->ooms); |
|---|
| 537 | add_assoc_long_ex(return_value, ZEND_STRS("errors"), cache->errors); |
|---|
| 538 | |
|---|
| 539 | add_assoc_long_ex(return_value, ZEND_STRS("cached"), cache->entries_count); |
|---|
| 540 | add_assoc_long_ex(return_value, ZEND_STRS("deleted"), cache->deletes_count); |
|---|
| 541 | if (interval) { |
|---|
| 542 | time_t gc = (cache->last_gc_expires + interval) - XG(request_time); |
|---|
| 543 | add_assoc_long_ex(return_value, ZEND_STRS("gc"), gc > 0 ? gc : 0); |
|---|
| 544 | } |
|---|
| 545 | else { |
|---|
| 546 | add_assoc_null_ex(return_value, ZEND_STRS("gc")); |
|---|
| 547 | } |
|---|
| 548 | MAKE_STD_ZVAL(hits); |
|---|
| 549 | array_init(hits); |
|---|
| 550 | for (i = 0; i < sizeof(cache->hits_by_hour) / sizeof(cache->hits_by_hour[0]); i ++) { |
|---|
| 551 | add_next_index_long(hits, (long) cache->hits_by_hour[i]); |
|---|
| 552 | } |
|---|
| 553 | add_assoc_zval_ex(return_value, ZEND_STRS("hits_by_hour"), hits); |
|---|
| 554 | |
|---|
| 555 | MAKE_STD_ZVAL(hits); |
|---|
| 556 | array_init(hits); |
|---|
| 557 | for (i = 0; i < sizeof(cache->hits_by_second) / sizeof(cache->hits_by_second[0]); i ++) { |
|---|
| 558 | add_next_index_long(hits, (long) cache->hits_by_second[i]); |
|---|
| 559 | } |
|---|
| 560 | add_assoc_zval_ex(return_value, ZEND_STRS("hits_by_second"), hits); |
|---|
| 561 | |
|---|
| 562 | MAKE_STD_ZVAL(blocks); |
|---|
| 563 | array_init(blocks); |
|---|
| 564 | |
|---|
| 565 | add_assoc_long_ex(return_value, ZEND_STRS("size"), handlers->size(mem)); |
|---|
| 566 | add_assoc_long_ex(return_value, ZEND_STRS("avail"), handlers->avail(mem)); |
|---|
| 567 | add_assoc_bool_ex(return_value, ZEND_STRS("can_readonly"), xc_readonly_protection); |
|---|
| 568 | |
|---|
| 569 | for (b = handlers->freeblock_first(mem); b; b = handlers->freeblock_next(b)) { |
|---|
| 570 | zval *bi; |
|---|
| 571 | |
|---|
| 572 | MAKE_STD_ZVAL(bi); |
|---|
| 573 | array_init(bi); |
|---|
| 574 | |
|---|
| 575 | add_assoc_long_ex(bi, ZEND_STRS("size"), handlers->block_size(b)); |
|---|
| 576 | add_assoc_long_ex(bi, ZEND_STRS("offset"), handlers->block_offset(mem, b)); |
|---|
| 577 | add_next_index_zval(blocks, bi); |
|---|
| 578 | #ifndef NDEBUG |
|---|
| 579 | avail += handlers->block_size(b); |
|---|
| 580 | #endif |
|---|
| 581 | } |
|---|
| 582 | add_assoc_zval_ex(return_value, ZEND_STRS("free_blocks"), blocks); |
|---|
| 583 | #ifndef NDEBUG |
|---|
| 584 | assert(avail == handlers->avail(mem)); |
|---|
| 585 | #endif |
|---|
| 586 | } |
|---|
| 587 | /* }}} */ |
|---|
| 588 | static void xc_fillentry_dmz(xc_entry_t *entry, int del, zval *list TSRMLS_DC) /* {{{ */ |
|---|
| 589 | { |
|---|
| 590 | zval* ei; |
|---|
| 591 | xc_entry_data_php_t *php; |
|---|
| 592 | xc_entry_data_var_t *var; |
|---|
| 593 | |
|---|
| 594 | ALLOC_INIT_ZVAL(ei); |
|---|
| 595 | array_init(ei); |
|---|
| 596 | |
|---|
| 597 | add_assoc_long_ex(ei, ZEND_STRS("refcount"), entry->refcount); |
|---|
| 598 | add_assoc_long_ex(ei, ZEND_STRS("hits"), entry->hits); |
|---|
| 599 | add_assoc_long_ex(ei, ZEND_STRS("ctime"), entry->ctime); |
|---|
| 600 | add_assoc_long_ex(ei, ZEND_STRS("atime"), entry->atime); |
|---|
| 601 | add_assoc_long_ex(ei, ZEND_STRS("hvalue"), entry->hvalue); |
|---|
| 602 | if (del) { |
|---|
| 603 | add_assoc_long_ex(ei, ZEND_STRS("dtime"), entry->dtime); |
|---|
| 604 | } |
|---|
| 605 | #ifdef IS_UNICODE |
|---|
| 606 | do { |
|---|
| 607 | zval *zv; |
|---|
| 608 | ALLOC_INIT_ZVAL(zv); |
|---|
| 609 | switch (entry->name_type) { |
|---|
| 610 | case IS_UNICODE: |
|---|
| 611 | ZVAL_UNICODEL(zv, entry->name.ustr.val, entry->name.ustr.len, 1); |
|---|
| 612 | break; |
|---|
| 613 | case IS_STRING: |
|---|
| 614 | ZVAL_STRINGL(zv, entry->name.str.val, entry->name.str.len, 1); |
|---|
| 615 | break; |
|---|
| 616 | default: |
|---|
| 617 | assert(0); |
|---|
| 618 | } |
|---|
| 619 | zv->type = entry->name_type; |
|---|
| 620 | add_assoc_zval_ex(ei, ZEND_STRS("name"), zv); |
|---|
| 621 | } while (0); |
|---|
| 622 | #else |
|---|
| 623 | add_assoc_stringl_ex(ei, ZEND_STRS("name"), entry->name.str.val, entry->name.str.len, 1); |
|---|
| 624 | #endif |
|---|
| 625 | switch (entry->type) { |
|---|
| 626 | case XC_TYPE_PHP: |
|---|
| 627 | php = entry->data.php; |
|---|
| 628 | add_assoc_long_ex(ei, ZEND_STRS("size"), entry->size + php->size); |
|---|
| 629 | add_assoc_long_ex(ei, ZEND_STRS("phprefcount"), php->refcount); |
|---|
| 630 | add_assoc_long_ex(ei, ZEND_STRS("sourcesize"), php->sourcesize); |
|---|
| 631 | #ifdef HAVE_INODE |
|---|
| 632 | add_assoc_long_ex(ei, ZEND_STRS("device"), entry->device); |
|---|
| 633 | add_assoc_long_ex(ei, ZEND_STRS("inode"), entry->inode); |
|---|
| 634 | #endif |
|---|
| 635 | add_assoc_long_ex(ei, ZEND_STRS("mtime"), entry->mtime); |
|---|
| 636 | |
|---|
| 637 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 638 | add_assoc_long_ex(ei, ZEND_STRS("constinfo_cnt"), php->constinfo_cnt); |
|---|
| 639 | #endif |
|---|
| 640 | add_assoc_long_ex(ei, ZEND_STRS("function_cnt"), php->funcinfo_cnt); |
|---|
| 641 | add_assoc_long_ex(ei, ZEND_STRS("class_cnt"), php->classinfo_cnt); |
|---|
| 642 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 643 | add_assoc_long_ex(ei, ZEND_STRS("autoglobal_cnt"),php->autoglobal_cnt); |
|---|
| 644 | #endif |
|---|
| 645 | break; |
|---|
| 646 | |
|---|
| 647 | case XC_TYPE_VAR: |
|---|
| 648 | var = entry->data.var; |
|---|
| 649 | add_assoc_long_ex(ei, ZEND_STRS("size"), entry->size); |
|---|
| 650 | break; |
|---|
| 651 | |
|---|
| 652 | default: |
|---|
| 653 | assert(0); |
|---|
| 654 | } |
|---|
| 655 | |
|---|
| 656 | add_next_index_zval(list, ei); |
|---|
| 657 | } |
|---|
| 658 | /* }}} */ |
|---|
| 659 | static void xc_filllist_dmz(xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */ |
|---|
| 660 | { |
|---|
| 661 | zval* list; |
|---|
| 662 | int i, c; |
|---|
| 663 | xc_entry_t *e; |
|---|
| 664 | |
|---|
| 665 | ALLOC_INIT_ZVAL(list); |
|---|
| 666 | array_init(list); |
|---|
| 667 | |
|---|
| 668 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
|---|
| 669 | for (e = cache->entries[i]; e; e = e->next) { |
|---|
| 670 | xc_fillentry_dmz(e, 0, list TSRMLS_CC); |
|---|
| 671 | } |
|---|
| 672 | } |
|---|
| 673 | add_assoc_zval(return_value, "cache_list", list); |
|---|
| 674 | |
|---|
| 675 | ALLOC_INIT_ZVAL(list); |
|---|
| 676 | array_init(list); |
|---|
| 677 | for (e = cache->deletes; e; e = e->next) { |
|---|
| 678 | xc_fillentry_dmz(e, 1, list TSRMLS_CC); |
|---|
| 679 | } |
|---|
| 680 | add_assoc_zval(return_value, "deleted_list", list); |
|---|
| 681 | } |
|---|
| 682 | /* }}} */ |
|---|
| 683 | |
|---|
| 684 | static zend_op_array *xc_entry_install(xc_entry_t *xce, zend_file_handle *h TSRMLS_DC) /* {{{ */ |
|---|
| 685 | { |
|---|
| 686 | zend_uint i; |
|---|
| 687 | xc_entry_data_php_t *p = xce->data.php; |
|---|
| 688 | zend_op_array *old_active_op_array = CG(active_op_array); |
|---|
| 689 | #ifndef ZEND_ENGINE_2 |
|---|
| 690 | ALLOCA_FLAG(use_heap) |
|---|
| 691 | /* new ptr which is stored inside CG(class_table) */ |
|---|
| 692 | xc_cest_t **new_cest_ptrs = (xc_cest_t **)my_do_alloca(sizeof(xc_cest_t*) * p->classinfo_cnt, use_heap); |
|---|
| 693 | #endif |
|---|
| 694 | |
|---|
| 695 | CG(active_op_array) = p->op_array; |
|---|
| 696 | |
|---|
| 697 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 698 | /* install constant */ |
|---|
| 699 | for (i = 0; i < p->constinfo_cnt; i ++) { |
|---|
| 700 | xc_constinfo_t *ci = &p->constinfos[i]; |
|---|
| 701 | xc_install_constant(xce->name.str.val, &ci->constant, |
|---|
| 702 | UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC); |
|---|
| 703 | } |
|---|
| 704 | #endif |
|---|
| 705 | |
|---|
| 706 | /* install function */ |
|---|
| 707 | for (i = 0; i < p->funcinfo_cnt; i ++) { |
|---|
| 708 | xc_funcinfo_t *fi = &p->funcinfos[i]; |
|---|
| 709 | xc_install_function(xce->name.str.val, &fi->func, |
|---|
| 710 | UNISW(0, fi->type), fi->key, fi->key_size, fi->h TSRMLS_CC); |
|---|
| 711 | } |
|---|
| 712 | |
|---|
| 713 | /* install class */ |
|---|
| 714 | for (i = 0; i < p->classinfo_cnt; i ++) { |
|---|
| 715 | xc_classinfo_t *ci = &p->classinfos[i]; |
|---|
| 716 | #ifndef ZEND_ENGINE_2 |
|---|
| 717 | zend_class_entry *ce = CestToCePtr(ci->cest); |
|---|
| 718 | /* fix pointer to the be which inside class_table */ |
|---|
| 719 | if (ce->parent) { |
|---|
| 720 | zend_uint class_idx = (/* class_num */ (int) (long) ce->parent) - 1; |
|---|
| 721 | assert(class_idx < i); |
|---|
| 722 | ci->cest.parent = new_cest_ptrs[class_idx]; |
|---|
| 723 | } |
|---|
| 724 | new_cest_ptrs[i] = |
|---|
| 725 | #endif |
|---|
| 726 | #ifdef ZEND_COMPILE_DELAYED_BINDING |
|---|
| 727 | xc_install_class(xce->name.str.val, &ci |
|---|