PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/opcache - zend_accelerator_module.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 88 330 26.7 %
Date: 2014-10-22 Functions: 11 23 47.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend OPcache                                                         |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    |          Stanislav Malyshev <stas@zend.com>                          |
      18             :    |          Dmitry Stogov <dmitry@zend.com>                             |
      19             :    +----------------------------------------------------------------------+
      20             : */
      21             : 
      22             : #include <time.h>
      23             : 
      24             : #include "php.h"
      25             : #include "ZendAccelerator.h"
      26             : #include "zend_API.h"
      27             : #include "zend_shared_alloc.h"
      28             : #include "zend_accelerator_blacklist.h"
      29             : #include "php_ini.h"
      30             : #include "SAPI.h"
      31             : #include "zend_virtual_cwd.h"
      32             : #include "ext/standard/info.h"
      33             : #include "ext/standard/php_filestat.h"
      34             : 
      35             : #define STRING_NOT_NULL(s) (NULL == (s)?"":s)
      36             : #define MIN_ACCEL_FILES 200
      37             : #define MAX_ACCEL_FILES 1000000
      38             : #define TOKENTOSTR(X) #X
      39             : 
      40             : static void (*orig_file_exists)(INTERNAL_FUNCTION_PARAMETERS) = NULL;
      41             : static void (*orig_is_file)(INTERNAL_FUNCTION_PARAMETERS) = NULL;
      42             : static void (*orig_is_readable)(INTERNAL_FUNCTION_PARAMETERS) = NULL;
      43             : 
      44             : ZEND_BEGIN_ARG_INFO(arginfo_opcache_none, 0)
      45             : ZEND_END_ARG_INFO()
      46             : 
      47             : ZEND_BEGIN_ARG_INFO_EX(arginfo_opcache_get_status, 0, 0, 0)
      48             :         ZEND_ARG_INFO(0, fetch_scripts)
      49             : ZEND_END_ARG_INFO()
      50             : 
      51             : ZEND_BEGIN_ARG_INFO_EX(arginfo_opcache_compile_file, 0, 0, 1)
      52             :         ZEND_ARG_INFO(0, file)
      53             : ZEND_END_ARG_INFO()
      54             : 
      55             : ZEND_BEGIN_ARG_INFO_EX(arginfo_opcache_invalidate, 0, 0, 1)
      56             :         ZEND_ARG_INFO(0, script)
      57             :         ZEND_ARG_INFO(0, force)
      58             : ZEND_END_ARG_INFO()
      59             : 
      60             : ZEND_BEGIN_ARG_INFO_EX(arginfo_opcache_is_script_cached, 0, 0, 1)
      61             :         ZEND_ARG_INFO(0, script)
      62             : ZEND_END_ARG_INFO()
      63             : 
      64             : /* User functions */
      65             : static ZEND_FUNCTION(opcache_reset);
      66             : static ZEND_FUNCTION(opcache_invalidate);
      67             : static ZEND_FUNCTION(opcache_is_script_cached);
      68             : 
      69             : /* Private functions */
      70             : static ZEND_FUNCTION(opcache_get_status);
      71             : static ZEND_FUNCTION(opcache_compile_file);
      72             : static ZEND_FUNCTION(opcache_get_configuration);
      73             : 
      74             : static zend_function_entry accel_functions[] = {
      75             :         /* User functions */
      76             :         ZEND_FE(opcache_reset,                                  arginfo_opcache_none)
      77             :         ZEND_FE(opcache_invalidate,                             arginfo_opcache_invalidate)
      78             :         ZEND_FE(opcache_compile_file,                   arginfo_opcache_compile_file)
      79             :         ZEND_FE(opcache_is_script_cached,               arginfo_opcache_is_script_cached)
      80             :         /* Private functions */
      81             :         ZEND_FE(opcache_get_configuration,              arginfo_opcache_none)
      82             :         ZEND_FE(opcache_get_status,                             arginfo_opcache_get_status)
      83             :         { NULL, NULL, NULL, 0, 0 }
      84             : };
      85             : 
      86           9 : static int validate_api_restriction(TSRMLS_D)
      87             : {
      88           9 :         if (ZCG(accel_directives).restrict_api && *ZCG(accel_directives).restrict_api) {
      89           0 :                 int len = strlen(ZCG(accel_directives).restrict_api);
      90             : 
      91           0 :                 if (!SG(request_info).path_translated ||
      92           0 :                     strlen(SG(request_info).path_translated) < len ||
      93           0 :                     memcmp(SG(request_info).path_translated, ZCG(accel_directives).restrict_api, len) != 0) {
      94           0 :                         zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME " API is restricted by \"restrict_api\" configuration directive");
      95           0 :                         return 0;
      96             :                 }
      97             :         }       
      98           9 :         return 1;
      99             : }
     100             : 
     101       20303 : static ZEND_INI_MH(OnUpdateMemoryConsumption)
     102             : {
     103             :         zend_long *p;
     104             :         zend_long memsize;
     105             : #ifndef ZTS
     106       20303 :         char *base = (char *) mh_arg2;
     107             : #else
     108             :         char *base = (char *) ts_resource(*((int *) mh_arg2));
     109             : #endif
     110             : 
     111             :         /* keep the compiler happy */
     112             :         (void)entry; (void)mh_arg2; (void)mh_arg3; (void)stage;
     113             : 
     114       20303 :         p = (zend_long *) (base + (size_t)mh_arg1);
     115       20303 :         memsize = atoi(new_value->val);
     116             :         /* sanity check we must use at least 8 MB */
     117       20303 :         if (memsize < 8) {
     118           0 :                 const char *new_new_value = "8";
     119             :                 zend_ini_entry *ini_entry;
     120             : 
     121           0 :                 memsize = 8;
     122           0 :                 zend_accel_error(ACCEL_LOG_WARNING, "opcache.memory_consumption is set below the required 8MB.\n");
     123           0 :                 zend_accel_error(ACCEL_LOG_WARNING, ACCELERATOR_PRODUCT_NAME " will use the minimal 8MB configuration.\n");
     124             : 
     125           0 :                 if ((ini_entry = zend_hash_str_find_ptr(EG(ini_directives),
     126             :                                         "opcache.memory_consumption",
     127             :                                         sizeof("opcache.memory_consumption")-1)) == NULL) {
     128           0 :                         return FAILURE;
     129             :                 }
     130             : 
     131           0 :                 ini_entry->value = zend_string_init(new_new_value, 1, 1);
     132             :         }
     133       20303 :         *p = memsize * (1024 * 1024);
     134       20303 :         return SUCCESS;
     135             : }
     136             : 
     137       20303 : static ZEND_INI_MH(OnUpdateMaxAcceleratedFiles)
     138             : {
     139             :         zend_long *p;
     140             :         zend_long size;
     141             : #ifndef ZTS
     142       20303 :         char *base = (char *) mh_arg2;
     143             : #else
     144             :         char *base = (char *) ts_resource(*((int *) mh_arg2));
     145             : #endif
     146             : 
     147             :         /* keep the compiler happy */
     148             :         (void)entry; (void)mh_arg2; (void)mh_arg3; (void)stage;
     149             : 
     150       20303 :         p = (zend_long *) (base + (size_t)mh_arg1);
     151       20303 :         size = atoi(new_value->val);
     152             :         /* sanity check we must use a value between MIN_ACCEL_FILES and MAX_ACCEL_FILES */
     153             : 
     154       20303 :         if (size < MIN_ACCEL_FILES || size > MAX_ACCEL_FILES) {
     155             :                 const char *new_new_value;
     156             :                 zend_ini_entry *ini_entry;
     157             : 
     158           0 :                 if (size < MIN_ACCEL_FILES) {
     159           0 :                         size = MIN_ACCEL_FILES;
     160           0 :                         new_new_value = TOKENTOSTR(MIN_ACCEL_FILES);
     161           0 :                         zend_accel_error(ACCEL_LOG_WARNING, "opcache.max_accelerated_files is set below the required minimum (%d).\n", MIN_ACCEL_FILES);
     162           0 :                         zend_accel_error(ACCEL_LOG_WARNING, ACCELERATOR_PRODUCT_NAME " will use the minimal configuration.\n");
     163             :                 }
     164           0 :                 if (size > MAX_ACCEL_FILES) {
     165           0 :                         size = MAX_ACCEL_FILES;
     166           0 :                         new_new_value = TOKENTOSTR(MAX_ACCEL_FILES);
     167           0 :                         zend_accel_error(ACCEL_LOG_WARNING, "opcache.max_accelerated_files is set above the limit (%d).\n", MAX_ACCEL_FILES);
     168           0 :                         zend_accel_error(ACCEL_LOG_WARNING, ACCELERATOR_PRODUCT_NAME " will use the maximal configuration.\n");
     169             :                 }
     170           0 :                 if ((ini_entry = zend_hash_str_find_ptr(EG(ini_directives),
     171             :                                         "opcache.max_accelerated_files",
     172             :                                         sizeof("opcache.max_accelerated_files")-1)) == NULL) {
     173           0 :                         return FAILURE;
     174             :                 }
     175           0 :                 ini_entry->value = zend_string_init(new_new_value, strlen(new_new_value), 1);
     176             :         }
     177       20303 :         *p = size;
     178       20303 :         return SUCCESS;
     179             : }
     180             : 
     181       20303 : static ZEND_INI_MH(OnUpdateMaxWastedPercentage)
     182             : {
     183             :         double *p;
     184             :         zend_long percentage;
     185             : #ifndef ZTS
     186       20303 :         char *base = (char *) mh_arg2;
     187             : #else
     188             :         char *base = (char *) ts_resource(*((int *) mh_arg2));
     189             : #endif
     190             : 
     191             :         /* keep the compiler happy */
     192             :         (void)entry; (void)mh_arg2; (void)mh_arg3; (void)stage;
     193             : 
     194       20303 :         p = (double *) (base + (size_t)mh_arg1);
     195       20303 :         percentage = atoi(new_value->val);
     196             : 
     197       20303 :         if (percentage <= 0 || percentage > 50) {
     198           0 :                 const char *new_new_value = "5";
     199             :                 zend_ini_entry *ini_entry;
     200             : 
     201           0 :                 percentage = 5;
     202           0 :                 zend_accel_error(ACCEL_LOG_WARNING, "opcache.max_wasted_percentage must be set between 1 and 50.\n");
     203           0 :                 zend_accel_error(ACCEL_LOG_WARNING, ACCELERATOR_PRODUCT_NAME " will use 5%.\n");
     204           0 :                 if ((ini_entry = zend_hash_str_find_ptr(EG(ini_directives),
     205             :                                         "opcache.max_wasted_percentage",
     206             :                                         sizeof("opcache.max_wasted_percentage")-1)) == NULL) {
     207           0 :                         return FAILURE;
     208             :                 }
     209           0 :                 ini_entry->value = zend_string_init(new_new_value, strlen(new_new_value), 1);
     210             :         }
     211       20303 :         *p = (double)percentage / 100.0;
     212       20303 :         return SUCCESS;
     213             : }
     214             : 
     215       20303 : static ZEND_INI_MH(OnEnable)
     216             : {
     217       20303 :         if (stage == ZEND_INI_STAGE_STARTUP ||
     218             :             stage == ZEND_INI_STAGE_SHUTDOWN ||
     219             :             stage == ZEND_INI_STAGE_DEACTIVATE) {
     220       20303 :                 return OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
     221             :         } else {
     222             :                 /* It may be only temporary disabled */
     223             :                 zend_bool *p;
     224             : #ifndef ZTS
     225           0 :                 char *base = (char *) mh_arg2;
     226             : #else
     227             :                 char *base = (char *) ts_resource(*((int *) mh_arg2));
     228             : #endif
     229             : 
     230           0 :                 p = (zend_bool *) (base+(size_t) mh_arg1);
     231           0 :                 if ((new_value->len == 2 && strcasecmp("on", new_value->val) == 0) ||
     232           0 :                     (new_value->len == 3 && strcasecmp("yes", new_value->val) == 0) ||
     233           0 :                     (new_value->len == 4 && strcasecmp("true", new_value->val) == 0) ||
     234           0 :                         atoi(new_value->val) != 0) {
     235           0 :                         zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME " can't be temporary enabled (it may be only disabled till the end of request)");
     236           0 :                         return FAILURE;
     237             :                 } else {
     238           0 :                         *p = 0;
     239           0 :                         return SUCCESS;
     240             :                 }
     241             :         }
     242             : }
     243             : 
     244             : ZEND_INI_BEGIN()
     245             :     STD_PHP_INI_BOOLEAN("opcache.enable"             , "1", PHP_INI_ALL,    OnEnable,     enabled                             , zend_accel_globals, accel_globals)
     246             :         STD_PHP_INI_BOOLEAN("opcache.use_cwd"            , "1", PHP_INI_SYSTEM, OnUpdateBool, accel_directives.use_cwd            , zend_accel_globals, accel_globals)
     247             :         STD_PHP_INI_BOOLEAN("opcache.validate_timestamps", "1", PHP_INI_ALL   , OnUpdateBool, accel_directives.validate_timestamps, zend_accel_globals, accel_globals)
     248             :         STD_PHP_INI_BOOLEAN("opcache.inherited_hack"     , "1", PHP_INI_SYSTEM, OnUpdateBool, accel_directives.inherited_hack     , zend_accel_globals, accel_globals)
     249             :         STD_PHP_INI_BOOLEAN("opcache.dups_fix"           , "0", PHP_INI_ALL   , OnUpdateBool, accel_directives.ignore_dups        , zend_accel_globals, accel_globals)
     250             :         STD_PHP_INI_BOOLEAN("opcache.revalidate_path"    , "0", PHP_INI_ALL   , OnUpdateBool, accel_directives.revalidate_path    , zend_accel_globals, accel_globals)
     251             : 
     252             :         STD_PHP_INI_ENTRY("opcache.log_verbosity_level"   , "1"   , PHP_INI_SYSTEM, OnUpdateLong, accel_directives.log_verbosity_level,       zend_accel_globals, accel_globals)
     253             :         STD_PHP_INI_ENTRY("opcache.memory_consumption"    , "64"  , PHP_INI_SYSTEM, OnUpdateMemoryConsumption,    accel_directives.memory_consumption,        zend_accel_globals, accel_globals)
     254             :         STD_PHP_INI_ENTRY("opcache.interned_strings_buffer", "4"  , PHP_INI_SYSTEM, OnUpdateLong,                 accel_directives.interned_strings_buffer,   zend_accel_globals, accel_globals)
     255             :         STD_PHP_INI_ENTRY("opcache.max_accelerated_files" , "2000", PHP_INI_SYSTEM, OnUpdateMaxAcceleratedFiles,     accel_directives.max_accelerated_files,     zend_accel_globals, accel_globals)
     256             :         STD_PHP_INI_ENTRY("opcache.max_wasted_percentage" , "5"   , PHP_INI_SYSTEM, OnUpdateMaxWastedPercentage,     accel_directives.max_wasted_percentage,     zend_accel_globals, accel_globals)
     257             :         STD_PHP_INI_ENTRY("opcache.consistency_checks"    , "0"   , PHP_INI_ALL   , OnUpdateLong,                accel_directives.consistency_checks,        zend_accel_globals, accel_globals)
     258             :         STD_PHP_INI_ENTRY("opcache.force_restart_timeout" , "180" , PHP_INI_SYSTEM, OnUpdateLong,                accel_directives.force_restart_timeout,     zend_accel_globals, accel_globals)
     259             :         STD_PHP_INI_ENTRY("opcache.revalidate_freq"       , "2"   , PHP_INI_ALL   , OnUpdateLong,                accel_directives.revalidate_freq,           zend_accel_globals, accel_globals)
     260             :         STD_PHP_INI_ENTRY("opcache.file_update_protection", "2"   , PHP_INI_ALL   , OnUpdateLong,                accel_directives.file_update_protection,    zend_accel_globals, accel_globals)
     261             :         STD_PHP_INI_ENTRY("opcache.preferred_memory_model", ""    , PHP_INI_SYSTEM, OnUpdateStringUnempty,       accel_directives.memory_model,              zend_accel_globals, accel_globals)
     262             :         STD_PHP_INI_ENTRY("opcache.blacklist_filename"    , ""    , PHP_INI_SYSTEM, OnUpdateString,              accel_directives.user_blacklist_filename,   zend_accel_globals, accel_globals)
     263             :         STD_PHP_INI_ENTRY("opcache.max_file_size"         , "0"   , PHP_INI_SYSTEM, OnUpdateLong,                accel_directives.max_file_size,             zend_accel_globals, accel_globals)
     264             : 
     265             :         STD_PHP_INI_ENTRY("opcache.protect_memory"        , "0"  , PHP_INI_SYSTEM, OnUpdateBool,                  accel_directives.protect_memory,            zend_accel_globals, accel_globals)
     266             :         STD_PHP_INI_ENTRY("opcache.save_comments"         , "1"  , PHP_INI_SYSTEM, OnUpdateBool,                  accel_directives.save_comments,             zend_accel_globals, accel_globals)
     267             :         STD_PHP_INI_ENTRY("opcache.load_comments"         , "1"  , PHP_INI_ALL,    OnUpdateBool,                  accel_directives.load_comments,             zend_accel_globals, accel_globals)
     268             :         STD_PHP_INI_ENTRY("opcache.fast_shutdown"         , "0"  , PHP_INI_SYSTEM, OnUpdateBool,                  accel_directives.fast_shutdown,             zend_accel_globals, accel_globals)
     269             : 
     270             :         STD_PHP_INI_ENTRY("opcache.optimization_level"    , DEFAULT_OPTIMIZATION_LEVEL , PHP_INI_SYSTEM, OnUpdateLong, accel_directives.optimization_level,   zend_accel_globals, accel_globals)
     271             :         STD_PHP_INI_BOOLEAN("opcache.enable_file_override"    , "0"   , PHP_INI_SYSTEM, OnUpdateBool,              accel_directives.file_override_enabled,     zend_accel_globals, accel_globals)
     272             :         STD_PHP_INI_BOOLEAN("opcache.enable_cli"             , "0"   , PHP_INI_SYSTEM, OnUpdateBool,              accel_directives.enable_cli,                zend_accel_globals, accel_globals)
     273             :         STD_PHP_INI_ENTRY("opcache.error_log"                , ""    , PHP_INI_SYSTEM, OnUpdateString,               accel_directives.error_log,                 zend_accel_globals, accel_globals)
     274             :         STD_PHP_INI_ENTRY("opcache.restrict_api"             , ""    , PHP_INI_SYSTEM, OnUpdateString,               accel_directives.restrict_api,              zend_accel_globals, accel_globals)
     275             : 
     276             : #ifdef ZEND_WIN32
     277             :         STD_PHP_INI_ENTRY("opcache.mmap_base", NULL, PHP_INI_SYSTEM,  OnUpdateString,                              accel_directives.mmap_base,                 zend_accel_globals, accel_globals)
     278             : #endif
     279             : ZEND_INI_END()
     280             : 
     281           0 : static int filename_is_in_cache(char *filename, int filename_len TSRMLS_DC)
     282             : {
     283             :         char *key;
     284             :         int key_length;
     285           0 :         zend_file_handle handle = {0};
     286             :         zend_persistent_script *persistent_script;
     287             : 
     288           0 :         handle.filename = filename;
     289           0 :         handle.type = ZEND_HANDLE_FILENAME;
     290             : 
     291           0 :         if (IS_ABSOLUTE_PATH(filename, filename_len)) {
     292           0 :                 persistent_script = zend_accel_hash_find(&ZCSG(hash), filename, filename_len);
     293           0 :                 if (persistent_script) {
     294           0 :                         return !persistent_script->corrupted &&
     295           0 :                                 validate_timestamp_and_record(persistent_script, &handle TSRMLS_CC) == SUCCESS;
     296             :                 }
     297             :         }
     298             : 
     299           0 :         if ((key = accel_make_persistent_key_ex(&handle, filename_len, &key_length TSRMLS_CC)) != NULL) {
     300           0 :                 persistent_script = zend_accel_hash_find(&ZCSG(hash), key, key_length);
     301           0 :                 return persistent_script && !persistent_script->corrupted &&
     302           0 :                         validate_timestamp_and_record(persistent_script, &handle TSRMLS_CC) == SUCCESS;
     303             :         }
     304             : 
     305           0 :         return 0;
     306             : }
     307             : 
     308           0 : static int accel_file_in_cache(INTERNAL_FUNCTION_PARAMETERS)
     309             : {
     310             :         zval zfilename;
     311             : 
     312           0 :         if (ZEND_NUM_ARGS() != 1 ||
     313           0 :             zend_get_parameters_array_ex(1, &zfilename) == FAILURE ||
     314             :             Z_TYPE(zfilename) != IS_STRING ||
     315           0 :             Z_STRLEN(zfilename) == 0) {
     316           0 :                 return 0;
     317             :         }
     318           0 :         return filename_is_in_cache(Z_STRVAL(zfilename), Z_STRLEN(zfilename) TSRMLS_CC);
     319             : }
     320             : 
     321           0 : static void accel_file_exists(INTERNAL_FUNCTION_PARAMETERS)
     322             : {
     323           0 :         if (accel_file_in_cache(INTERNAL_FUNCTION_PARAM_PASSTHRU)) {
     324           0 :                 RETURN_TRUE;
     325             :         } else {
     326           0 :                 orig_file_exists(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     327             :         }
     328             : }
     329             : 
     330           0 : static void accel_is_file(INTERNAL_FUNCTION_PARAMETERS)
     331             : {
     332           0 :         if (accel_file_in_cache(INTERNAL_FUNCTION_PARAM_PASSTHRU)) {
     333           0 :                 RETURN_TRUE;
     334             :         } else {
     335           0 :                 orig_is_file(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     336             :         }
     337             : }
     338             : 
     339           0 : static void accel_is_readable(INTERNAL_FUNCTION_PARAMETERS)
     340             : {
     341           0 :         if (accel_file_in_cache(INTERNAL_FUNCTION_PARAM_PASSTHRU)) {
     342           0 :                 RETURN_TRUE;
     343             :         } else {
     344           0 :                 orig_is_readable(INTERNAL_FUNCTION_PARAM_PASSTHRU);
     345             :         }
     346             : }
     347             : 
     348       20303 : static ZEND_MINIT_FUNCTION(zend_accelerator)
     349             : {
     350             :         (void)type; /* keep the compiler happy */
     351             : 
     352       20303 :         REGISTER_INI_ENTRIES();
     353             : 
     354       20303 :         return SUCCESS;
     355             : }
     356             : 
     357         368 : void zend_accel_override_file_functions(TSRMLS_D)
     358             : {
     359             :         zend_function *old_function;
     360         368 :         if (ZCG(enabled) && accel_startup_ok && ZCG(accel_directives).file_override_enabled) {
     361             :                 /* override file_exists */
     362           0 :                 if ((old_function = zend_hash_str_find_ptr(CG(function_table), "file_exists", sizeof("file_exists")-1)) != NULL) {
     363           0 :                         orig_file_exists = old_function->internal_function.handler;
     364           0 :                         old_function->internal_function.handler = accel_file_exists;
     365             :                 }
     366           0 :                 if ((old_function = zend_hash_str_find_ptr(CG(function_table), "is_file", sizeof("is_file")-1)) != NULL) {
     367           0 :                         orig_is_file = old_function->internal_function.handler;
     368           0 :                         old_function->internal_function.handler = accel_is_file;
     369             :                 }
     370           0 :                 if ((old_function = zend_hash_str_find_ptr(CG(function_table), "is_readable", sizeof("is_readable")-1)) != NULL) {
     371           0 :                         orig_is_readable = old_function->internal_function.handler;
     372           0 :                         old_function->internal_function.handler = accel_is_readable;
     373             :                 }
     374             :         }
     375         368 : }
     376             : 
     377       20301 : static ZEND_MSHUTDOWN_FUNCTION(zend_accelerator)
     378             : {
     379             :         (void)type; /* keep the compiler happy */
     380             : 
     381       20301 :         UNREGISTER_INI_ENTRIES();
     382       20301 :         accel_shutdown(TSRMLS_C);
     383       20301 :         return SUCCESS;
     384             : }
     385             : 
     386         142 : void zend_accel_info(ZEND_MODULE_INFO_FUNC_ARGS)
     387             : {
     388         142 :         php_info_print_table_start();
     389             : 
     390         143 :         if (ZCG(enabled) && accel_startup_ok && (ZCG(counted) || ZCSG(accelerator_enabled))) {
     391           1 :                 php_info_print_table_row(2, "Opcode Caching", "Up and Running");
     392             :         } else {
     393         141 :                 php_info_print_table_row(2, "Opcode Caching", "Disabled");
     394             :         }
     395         143 :         if (ZCG(enabled) && accel_startup_ok && ZCSG(accelerator_enabled) && ZCG(accel_directives).optimization_level) {
     396           1 :                 php_info_print_table_row(2, "Optimization", "Enabled");
     397             :         } else {
     398         141 :                 php_info_print_table_row(2, "Optimization", "Disabled");
     399             :         }
     400         142 :         if (ZCG(enabled)) {
     401         283 :                 if (!accel_startup_ok || zps_api_failure_reason) {
     402         141 :                         php_info_print_table_row(2, "Startup Failed", zps_api_failure_reason);
     403             :                 } else {
     404             :                         char buf[32];
     405           1 :                         php_info_print_table_row(2, "Startup", "OK");
     406           1 :                         php_info_print_table_row(2, "Shared memory model", zend_accel_get_shared_model());
     407           1 :                         snprintf(buf, sizeof(buf), "%pd", (zend_ulong)ZCSG(hits));
     408           1 :                         php_info_print_table_row(2, "Cache hits", buf);
     409           1 :                         snprintf(buf, sizeof(buf), "%pd", ZSMMG(memory_exhausted)?ZCSG(misses):ZCSG(misses)-ZCSG(blacklist_misses));
     410           1 :                         php_info_print_table_row(2, "Cache misses", buf);
     411           1 :                         snprintf(buf, sizeof(buf), ZEND_LONG_FMT, ZCG(accel_directives).memory_consumption-zend_shared_alloc_get_free_memory()-ZSMMG(wasted_shared_memory));
     412           1 :                         php_info_print_table_row(2, "Used memory", buf);
     413           1 :                         snprintf(buf, sizeof(buf), "%pd", zend_shared_alloc_get_free_memory());
     414           1 :                         php_info_print_table_row(2, "Free memory", buf);
     415           1 :                         snprintf(buf, sizeof(buf), "%pd", ZSMMG(wasted_shared_memory));
     416           1 :                         php_info_print_table_row(2, "Wasted memory", buf);
     417           1 :                         if (ZCSG(interned_strings_start) && ZCSG(interned_strings_end) && ZCSG(interned_strings_top)) {
     418           1 :                                 snprintf(buf, sizeof(buf), "%pd", ZCSG(interned_strings_top) - ZCSG(interned_strings_start));
     419           1 :                                 php_info_print_table_row(2, "Interned Strings Used memory", buf);
     420           1 :                                 snprintf(buf, sizeof(buf), "%pd", ZCSG(interned_strings_end) - ZCSG(interned_strings_top));
     421           1 :                                 php_info_print_table_row(2, "Interned Strings Free memory", buf);
     422             :                         }
     423           1 :                         snprintf(buf, sizeof(buf), "%ld", ZCSG(hash).num_direct_entries);
     424           1 :                         php_info_print_table_row(2, "Cached scripts", buf);
     425           1 :                         snprintf(buf, sizeof(buf), "%ld", ZCSG(hash).num_entries);
     426           1 :                         php_info_print_table_row(2, "Cached keys", buf);
     427           1 :                         snprintf(buf, sizeof(buf), "%pd", ZCSG(hash).max_num_entries);
     428           1 :                         php_info_print_table_row(2, "Max keys", buf);
     429           1 :                         snprintf(buf, sizeof(buf), "%pd", ZCSG(oom_restarts));
     430           1 :                         php_info_print_table_row(2, "OOM restarts", buf);
     431           1 :                         snprintf(buf, sizeof(buf), "%pd", ZCSG(hash_restarts));
     432           1 :                         php_info_print_table_row(2, "Hash keys restarts", buf);
     433           1 :                         snprintf(buf, sizeof(buf), "%pd", ZCSG(manual_restarts));
     434           1 :                         php_info_print_table_row(2, "Manual restarts", buf);
     435             :                 }
     436             :         }
     437             : 
     438         142 :         php_info_print_table_end();
     439         142 :         DISPLAY_INI_ENTRIES();
     440         142 : }
     441             : 
     442             : static zend_module_entry accel_module_entry = {
     443             :         STANDARD_MODULE_HEADER,
     444             :         ACCELERATOR_PRODUCT_NAME,
     445             :         accel_functions,
     446             :         ZEND_MINIT(zend_accelerator),
     447             :         ZEND_MSHUTDOWN(zend_accelerator),
     448             :         NULL,
     449             :         NULL,
     450             :         zend_accel_info,
     451             :     ACCELERATOR_VERSION "FE",
     452             :         STANDARD_MODULE_PROPERTIES
     453             : };
     454             : 
     455       20303 : int start_accel_module(TSRMLS_D)
     456             : {
     457       20303 :         return zend_startup_module(&accel_module_entry TSRMLS_CC);
     458             : }
     459             : 
     460             : /* {{{ proto array accelerator_get_scripts()
     461             :    Get the scripts which are accelerated by ZendAccelerator */
     462           0 : static int accelerator_get_scripts(zval *return_value TSRMLS_DC)
     463             : {
     464             :         uint i;
     465             :         zval persistent_script_report;
     466             :         zend_accel_hash_entry *cache_entry;
     467             :         struct tm *ta;
     468             :         struct timeval exec_time;
     469             :         struct timeval fetch_time;
     470             : 
     471           0 :         if (!ZCG(enabled) || !accel_startup_ok || !ZCSG(accelerator_enabled) || accelerator_shm_read_lock(TSRMLS_C) != SUCCESS) {
     472           0 :                 return 0;
     473             :         }
     474             : 
     475           0 :         array_init(return_value);
     476           0 :         for (i = 0; i<ZCSG(hash).max_num_entries; i++) {
     477           0 :                 for (cache_entry = ZCSG(hash).hash_table[i]; cache_entry; cache_entry = cache_entry->next) {
     478             :                         zend_persistent_script *script;
     479             :                         char *str;
     480             :                         size_t len;
     481             : 
     482           0 :                         if (cache_entry->indirect) continue;
     483             : 
     484           0 :                         script = (zend_persistent_script *)cache_entry->data;
     485             : 
     486           0 :                         array_init(&persistent_script_report);
     487           0 :                         add_assoc_str(&persistent_script_report, "full_path", zend_string_dup(script->full_path, 0));
     488           0 :                         add_assoc_long(&persistent_script_report, "hits", (zend_long)script->dynamic_members.hits);
     489           0 :                         add_assoc_long(&persistent_script_report, "memory_consumption", script->dynamic_members.memory_consumption);
     490           0 :                         ta = localtime(&script->dynamic_members.last_used);
     491           0 :                         str = asctime(ta);
     492           0 :                         len = strlen(str);
     493           0 :                         if (len > 0 && str[len - 1] == '\n') len--;
     494           0 :                         add_assoc_stringl(&persistent_script_report, "last_used", str, len);
     495           0 :                         add_assoc_long(&persistent_script_report, "last_used_timestamp", script->dynamic_members.last_used);
     496           0 :                         if (ZCG(accel_directives).validate_timestamps) {
     497           0 :                                 add_assoc_long(&persistent_script_report, "timestamp", (zend_long)script->timestamp);
     498             :                         }
     499           0 :                         timerclear(&exec_time);
     500           0 :                         timerclear(&fetch_time);
     501             : 
     502           0 :                         zend_hash_str_update(Z_ARRVAL_P(return_value), cache_entry->key, cache_entry->key_length-1, &persistent_script_report);
     503             :                 }
     504             :         }
     505           0 :         accelerator_shm_read_unlock(TSRMLS_C);
     506             : 
     507           0 :         return 1;
     508             : }
     509             : 
     510             : /* {{{ proto array accelerator_get_status([bool fetch_scripts])
     511             :    Obtain statistics information regarding code acceleration */
     512           9 : static ZEND_FUNCTION(opcache_get_status)
     513             : {
     514             :         zend_long reqs;
     515             :         zval memory_usage, statistics, scripts;
     516           9 :         zend_bool fetch_scripts = 1;
     517             : 
     518           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &fetch_scripts) == FAILURE) {
     519           0 :                 return;
     520             :         }
     521             :         
     522           9 :         if (!validate_api_restriction(TSRMLS_C)) {
     523           0 :                 RETURN_FALSE;
     524             :         }
     525             : 
     526           9 :         if (!accel_startup_ok) {
     527           9 :                 RETURN_FALSE;
     528             :         }
     529             : 
     530           0 :         array_init(return_value);
     531             : 
     532             :         /* Trivia */
     533           0 :         add_assoc_bool(return_value, "opcache_enabled", ZCG(enabled) && (ZCG(counted) || ZCSG(accelerator_enabled)));
     534           0 :         add_assoc_bool(return_value, "cache_full", ZSMMG(memory_exhausted));
     535           0 :         add_assoc_bool(return_value, "restart_pending", ZCSG(restart_pending));
     536           0 :         add_assoc_bool(return_value, "restart_in_progress", ZCSG(restart_in_progress));
     537             : 
     538             :         /* Memory usage statistics */
     539           0 :         array_init(&memory_usage);
     540           0 :         add_assoc_long(&memory_usage, "used_memory", ZCG(accel_directives).memory_consumption-zend_shared_alloc_get_free_memory()-ZSMMG(wasted_shared_memory));
     541           0 :         add_assoc_long(&memory_usage, "free_memory", zend_shared_alloc_get_free_memory());
     542           0 :         add_assoc_long(&memory_usage, "wasted_memory", ZSMMG(wasted_shared_memory));
     543           0 :         add_assoc_double(&memory_usage, "current_wasted_percentage", (((double) ZSMMG(wasted_shared_memory))/ZCG(accel_directives).memory_consumption)*100.0);
     544           0 :         add_assoc_zval(return_value, "memory_usage", &memory_usage);
     545             : 
     546           0 :         if (ZCSG(interned_strings_start) && ZCSG(interned_strings_end) && ZCSG(interned_strings_top)) {
     547             :                 zval interned_strings_usage;
     548             : 
     549           0 :                 array_init(&interned_strings_usage);
     550           0 :                 add_assoc_long(&interned_strings_usage, "buffer_size", ZCSG(interned_strings_end) - ZCSG(interned_strings_start));
     551           0 :                 add_assoc_long(&interned_strings_usage, "used_memory", ZCSG(interned_strings_top) - ZCSG(interned_strings_start));
     552           0 :                 add_assoc_long(&interned_strings_usage, "free_memory", ZCSG(interned_strings_end) - ZCSG(interned_strings_top));
     553           0 :                 add_assoc_long(&interned_strings_usage, "number_of_strings", ZCSG(interned_strings).nNumOfElements);
     554           0 :                 add_assoc_zval(return_value, "interned_strings_usage", &interned_strings_usage);
     555             :         }
     556             :         
     557             :         /* Accelerator statistics */
     558           0 :         array_init(&statistics);
     559           0 :         add_assoc_long(&statistics, "num_cached_scripts", ZCSG(hash).num_direct_entries);
     560           0 :         add_assoc_long(&statistics, "num_cached_keys",    ZCSG(hash).num_entries);
     561           0 :         add_assoc_long(&statistics, "max_cached_keys",    ZCSG(hash).max_num_entries);
     562           0 :         add_assoc_long(&statistics, "hits", (zend_long)ZCSG(hits));
     563           0 :         add_assoc_long(&statistics, "start_time", ZCSG(start_time));
     564           0 :         add_assoc_long(&statistics, "last_restart_time", ZCSG(last_restart_time));
     565           0 :         add_assoc_long(&statistics, "oom_restarts", ZCSG(oom_restarts));
     566           0 :         add_assoc_long(&statistics, "hash_restarts", ZCSG(hash_restarts));
     567           0 :         add_assoc_long(&statistics, "manual_restarts", ZCSG(manual_restarts));
     568           0 :         add_assoc_long(&statistics, "misses", ZSMMG(memory_exhausted)?ZCSG(misses):ZCSG(misses)-ZCSG(blacklist_misses));
     569           0 :         add_assoc_long(&statistics, "blacklist_misses", ZCSG(blacklist_misses));
     570           0 :         reqs = ZCSG(hits)+ZCSG(misses);
     571           0 :         add_assoc_double(&statistics, "blacklist_miss_ratio", reqs?(((double) ZCSG(blacklist_misses))/reqs)*100.0:0);
     572           0 :         add_assoc_double(&statistics, "opcache_hit_rate", reqs?(((double) ZCSG(hits))/reqs)*100.0:0);
     573           0 :         add_assoc_zval(return_value, "opcache_statistics", &statistics);
     574             : 
     575           0 :         if (fetch_scripts) {
     576             :                 /* accelerated scripts */
     577           0 :                 if (accelerator_get_scripts(&scripts TSRMLS_CC)) {
     578           0 :                         add_assoc_zval(return_value, "scripts", &scripts);
     579             :                 }
     580             :         }
     581             : }
     582             : 
     583           0 : static int add_blacklist_path(zend_blacklist_entry *p, zval *return_value TSRMLS_DC)
     584             : {
     585           0 :         add_next_index_stringl(return_value, p->path, p->path_length);
     586           0 :         return 0;
     587             : }
     588             : 
     589             : /* {{{ proto array accelerator_get_configuration()
     590             :    Obtain configuration information */
     591           0 : static ZEND_FUNCTION(opcache_get_configuration)
     592             : {
     593             :         zval directives, version, blacklist;
     594             : 
     595           0 :         if (zend_parse_parameters_none() == FAILURE) {
     596           0 :                 RETURN_FALSE;
     597             :         }
     598             : 
     599           0 :         if (!validate_api_restriction(TSRMLS_C)) {
     600           0 :                 RETURN_FALSE;
     601             :         }
     602             : 
     603           0 :         array_init(return_value);
     604             : 
     605             :         /* directives */
     606           0 :         array_init(&directives);
     607           0 :         add_assoc_bool(&directives, "opcache.enable",              ZCG(enabled));
     608           0 :         add_assoc_bool(&directives, "opcache.enable_cli",          ZCG(accel_directives).enable_cli);
     609           0 :         add_assoc_bool(&directives, "opcache.use_cwd",             ZCG(accel_directives).use_cwd);
     610           0 :         add_assoc_bool(&directives, "opcache.validate_timestamps", ZCG(accel_directives).validate_timestamps);
     611           0 :         add_assoc_bool(&directives, "opcache.inherited_hack",      ZCG(accel_directives).inherited_hack);
     612           0 :         add_assoc_bool(&directives, "opcache.dups_fix",            ZCG(accel_directives).ignore_dups);
     613           0 :         add_assoc_bool(&directives, "opcache.revalidate_path",     ZCG(accel_directives).revalidate_path);
     614             : 
     615           0 :         add_assoc_long(&directives,   "opcache.log_verbosity_level",    ZCG(accel_directives).log_verbosity_level);
     616           0 :         add_assoc_long(&directives,  "opcache.memory_consumption",     ZCG(accel_directives).memory_consumption);
     617           0 :         add_assoc_long(&directives,  "opcache.interned_strings_buffer",ZCG(accel_directives).interned_strings_buffer);
     618           0 :         add_assoc_long(&directives,          "opcache.max_accelerated_files",  ZCG(accel_directives).max_accelerated_files);
     619           0 :         add_assoc_double(&directives, "opcache.max_wasted_percentage",  ZCG(accel_directives).max_wasted_percentage);
     620           0 :         add_assoc_long(&directives,          "opcache.consistency_checks",     ZCG(accel_directives).consistency_checks);
     621           0 :         add_assoc_long(&directives,          "opcache.force_restart_timeout",  ZCG(accel_directives).force_restart_timeout);
     622           0 :         add_assoc_long(&directives,          "opcache.revalidate_freq",        ZCG(accel_directives).revalidate_freq);
     623           0 :         add_assoc_string(&directives, "opcache.preferred_memory_model", STRING_NOT_NULL(ZCG(accel_directives).memory_model));
     624           0 :         add_assoc_string(&directives, "opcache.blacklist_filename",     STRING_NOT_NULL(ZCG(accel_directives).user_blacklist_filename));
     625           0 :         add_assoc_long(&directives,   "opcache.max_file_size",          ZCG(accel_directives).max_file_size);
     626           0 :         add_assoc_string(&directives, "opcache.error_log",              STRING_NOT_NULL(ZCG(accel_directives).error_log));
     627             : 
     628           0 :         add_assoc_bool(&directives,   "opcache.protect_memory",         ZCG(accel_directives).protect_memory);
     629           0 :         add_assoc_bool(&directives,   "opcache.save_comments",          ZCG(accel_directives).save_comments);
     630           0 :         add_assoc_bool(&directives,   "opcache.load_comments",          ZCG(accel_directives).load_comments);
     631           0 :         add_assoc_bool(&directives,   "opcache.fast_shutdown",          ZCG(accel_directives).fast_shutdown);
     632           0 :         add_assoc_bool(&directives,   "opcache.enable_file_override",   ZCG(accel_directives).file_override_enabled);
     633           0 :         add_assoc_long(&directives,          "opcache.optimization_level",     ZCG(accel_directives).optimization_level);
     634             : 
     635           0 :         add_assoc_zval(return_value, "directives", &directives);
     636             : 
     637             :         /*version */
     638           0 :         array_init(&version);
     639           0 :         add_assoc_string(&version, "version", ACCELERATOR_VERSION);
     640           0 :         add_assoc_string(&version, "opcache_product_name", ACCELERATOR_PRODUCT_NAME);
     641           0 :         add_assoc_zval(return_value, "version", &version);
     642             : 
     643             :         /* blacklist */
     644           0 :         array_init(&blacklist);
     645           0 :         zend_accel_blacklist_apply(&accel_blacklist, add_blacklist_path, &blacklist TSRMLS_CC);
     646           0 :         add_assoc_zval(return_value, "blacklist", &blacklist);
     647             : }
     648             : 
     649             : /* {{{ proto void accelerator_reset()
     650             :    Request that the contents of the opcode cache to be reset */
     651           0 : static ZEND_FUNCTION(opcache_reset)
     652             : {
     653           0 :         if (zend_parse_parameters_none() == FAILURE) {
     654           0 :                 RETURN_FALSE;
     655             :         }
     656             : 
     657           0 :         if (!validate_api_restriction(TSRMLS_C)) {
     658           0 :                 RETURN_FALSE;
     659             :         }
     660             : 
     661           0 :         if (!ZCG(enabled) || !accel_startup_ok || !ZCSG(accelerator_enabled)) {
     662           0 :                 RETURN_FALSE;
     663             :         }
     664             : 
     665           0 :         zend_accel_schedule_restart(ACCEL_RESTART_USER TSRMLS_CC);
     666           0 :         RETURN_TRUE;
     667             : }
     668             : 
     669             : /* {{{ proto void opcache_invalidate(string $script [, bool $force = false])
     670             :    Invalidates cached script (in necessary or forced) */
     671           0 : static ZEND_FUNCTION(opcache_invalidate)
     672             : {
     673             :         char *script_name;
     674             :         size_t script_name_len;
     675           0 :         zend_bool force = 0;
     676             : 
     677           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &script_name, &script_name_len, &force) == FAILURE) {
     678           0 :                 return;
     679             :         }
     680             : 
     681           0 :         if (!validate_api_restriction(TSRMLS_C)) {
     682           0 :                 RETURN_FALSE;
     683             :         }
     684             : 
     685           0 :         if (zend_accel_invalidate(script_name, script_name_len, force TSRMLS_CC) == SUCCESS) {
     686           0 :                 RETURN_TRUE;
     687             :         } else {
     688           0 :                 RETURN_FALSE;
     689             :         }
     690             : }
     691             : 
     692           0 : static ZEND_FUNCTION(opcache_compile_file)
     693             : {
     694             :         char *script_name;
     695             :         size_t script_name_len;
     696             :         zend_file_handle handle;
     697           0 :         zend_op_array *op_array = NULL;
     698           0 :         zend_execute_data *orig_execute_data = NULL;
     699             : 
     700           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &script_name, &script_name_len) == FAILURE) {
     701           0 :                 return;
     702             :         }
     703             : 
     704           0 :         if (!ZCG(enabled) || !accel_startup_ok || !ZCSG(accelerator_enabled)) {
     705           0 :                 zend_error(E_NOTICE, ACCELERATOR_PRODUCT_NAME " seems to be disabled, can't compile file");
     706           0 :                 RETURN_FALSE;
     707             :         }
     708             : 
     709           0 :         handle.filename = script_name;
     710           0 :         handle.free_filename = 0;
     711           0 :         handle.opened_path = NULL;
     712           0 :         handle.type = ZEND_HANDLE_FILENAME;
     713             : 
     714           0 :         orig_execute_data = EG(current_execute_data);
     715             : 
     716           0 :         zend_try {
     717           0 :                 op_array = persistent_compile_file(&handle, ZEND_INCLUDE TSRMLS_CC);
     718           0 :         } zend_catch {
     719           0 :                 EG(current_execute_data) = orig_execute_data;
     720           0 :                 zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME " could not compile file %s", handle.filename);
     721           0 :         } zend_end_try();
     722             : 
     723           0 :         if(op_array != NULL) {
     724           0 :                 destroy_op_array(op_array TSRMLS_CC);
     725           0 :                 efree(op_array);
     726           0 :                 RETVAL_TRUE;
     727             :         } else {
     728           0 :                 RETVAL_FALSE;
     729             :         }
     730           0 :         zend_destroy_file_handle(&handle TSRMLS_CC);
     731             : }
     732             : 
     733             : /* {{{ proto bool opcache_is_script_cached(string $script)
     734             :    Return true if the script is cached in OPCache, false if it is not cached or if OPCache is not running. */
     735           0 : static ZEND_FUNCTION(opcache_is_script_cached)
     736             : {
     737             :         char *script_name;
     738             :         size_t script_name_len;
     739             : 
     740           0 :         if (!validate_api_restriction(TSRMLS_C)) {
     741           0 :                 RETURN_FALSE;
     742             :         }
     743             : 
     744           0 :         if (!ZCG(enabled) || !accel_startup_ok || !ZCSG(accelerator_enabled)) {
     745           0 :                 RETURN_FALSE;
     746             :         }
     747             : 
     748           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &script_name, &script_name_len) == FAILURE) {
     749           0 :                 return;
     750             :         }
     751             : 
     752           0 :         RETURN_BOOL(filename_is_in_cache(script_name, script_name_len TSRMLS_CC));
     753             : }

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:53 +0000 (2 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.