ZendAccelerator.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend OPcache |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 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@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Stanislav Malyshev <stas@zend.com> |
  18. | Dmitry Stogov <dmitry@php.net> |
  19. +----------------------------------------------------------------------+
  20. */
  21. #ifndef ZEND_ACCELERATOR_H
  22. #define ZEND_ACCELERATOR_H
  23. #ifdef HAVE_CONFIG_H
  24. # include <config.h>
  25. #endif
  26. #define ACCELERATOR_PRODUCT_NAME "Zend OPcache"
  27. /* 2 - added Profiler support, on 20010712 */
  28. /* 3 - added support for Optimizer's encoded-only-files mode */
  29. /* 4 - works with the new Optimizer, that supports the file format with licenses */
  30. /* 5 - API 4 didn't really work with the license-enabled file format. v5 does. */
  31. /* 6 - Monitor was removed from ZendPlatform.so, to a module of its own */
  32. /* 7 - Optimizer was embedded into Accelerator */
  33. /* 8 - Standalone Open Source Zend OPcache */
  34. #define ACCELERATOR_API_NO 8
  35. #if ZEND_WIN32
  36. # include "zend_config.w32.h"
  37. #else
  38. #include "zend_config.h"
  39. # include <sys/time.h>
  40. # include <sys/resource.h>
  41. #endif
  42. #if HAVE_UNISTD_H
  43. # include "unistd.h"
  44. #endif
  45. #include "zend_extensions.h"
  46. #include "zend_compile.h"
  47. #include "Optimizer/zend_optimizer.h"
  48. #include "zend_accelerator_hash.h"
  49. #include "zend_accelerator_debug.h"
  50. #ifndef PHPAPI
  51. # ifdef ZEND_WIN32
  52. # define PHPAPI __declspec(dllimport)
  53. # else
  54. # define PHPAPI
  55. # endif
  56. #endif
  57. #ifndef ZEND_EXT_API
  58. # ifdef ZEND_WIN32
  59. # define ZEND_EXT_API __declspec(dllexport)
  60. # elif defined(__GNUC__) && __GNUC__ >= 4
  61. # define ZEND_EXT_API __attribute__ ((visibility("default")))
  62. # else
  63. # define ZEND_EXT_API
  64. # endif
  65. #endif
  66. #ifdef ZEND_WIN32
  67. # ifndef MAXPATHLEN
  68. # include "win32/ioutil.h"
  69. # define MAXPATHLEN PHP_WIN32_IOUTIL_MAXPATHLEN
  70. # endif
  71. # include <direct.h>
  72. #else
  73. # ifndef MAXPATHLEN
  74. # define MAXPATHLEN 4096
  75. # endif
  76. # include <sys/param.h>
  77. #endif
  78. /*** file locking ***/
  79. #ifndef ZEND_WIN32
  80. extern int lock_file;
  81. #endif
  82. #if defined(HAVE_OPCACHE_FILE_CACHE) && defined(ZEND_WIN32)
  83. # define ENABLE_FILE_CACHE_FALLBACK 1
  84. #else
  85. # define ENABLE_FILE_CACHE_FALLBACK 0
  86. #endif
  87. #if ZEND_WIN32
  88. typedef unsigned __int64 accel_time_t;
  89. #else
  90. typedef time_t accel_time_t;
  91. #endif
  92. typedef enum _zend_accel_restart_reason {
  93. ACCEL_RESTART_OOM, /* restart because of out of memory */
  94. ACCEL_RESTART_HASH, /* restart because of hash overflow */
  95. ACCEL_RESTART_USER /* restart scheduled by opcache_reset() */
  96. } zend_accel_restart_reason;
  97. typedef struct _zend_persistent_script {
  98. zend_script script;
  99. zend_long compiler_halt_offset; /* position of __HALT_COMPILER or -1 */
  100. int ping_auto_globals_mask; /* which autoglobals are used by the script */
  101. accel_time_t timestamp; /* the script modification time */
  102. zend_bool corrupted;
  103. zend_bool is_phar;
  104. void *mem; /* shared memory area used by script structures */
  105. size_t size; /* size of used shared memory */
  106. void *arena_mem; /* part that should be copied into process */
  107. size_t arena_size;
  108. /* All entries that shouldn't be counted in the ADLER32
  109. * checksum must be declared in this struct
  110. */
  111. struct zend_persistent_script_dynamic_members {
  112. time_t last_used;
  113. #ifdef ZEND_WIN32
  114. LONGLONG hits;
  115. #else
  116. zend_ulong hits;
  117. #endif
  118. unsigned int memory_consumption;
  119. unsigned int checksum;
  120. time_t revalidate;
  121. } dynamic_members;
  122. } zend_persistent_script;
  123. typedef struct _zend_accel_directives {
  124. zend_long memory_consumption;
  125. zend_long max_accelerated_files;
  126. double max_wasted_percentage;
  127. char *user_blacklist_filename;
  128. zend_long consistency_checks;
  129. zend_long force_restart_timeout;
  130. zend_bool use_cwd;
  131. zend_bool ignore_dups;
  132. zend_bool validate_timestamps;
  133. zend_bool revalidate_path;
  134. zend_bool save_comments;
  135. zend_bool protect_memory;
  136. zend_bool file_override_enabled;
  137. zend_bool enable_cli;
  138. zend_bool validate_permission;
  139. #ifndef ZEND_WIN32
  140. zend_bool validate_root;
  141. #endif
  142. zend_ulong revalidate_freq;
  143. zend_ulong file_update_protection;
  144. char *error_log;
  145. #ifdef ZEND_WIN32
  146. char *mmap_base;
  147. #endif
  148. char *memory_model;
  149. zend_long log_verbosity_level;
  150. zend_long optimization_level;
  151. zend_long opt_debug_level;
  152. zend_long max_file_size;
  153. zend_long interned_strings_buffer;
  154. char *restrict_api;
  155. #ifndef ZEND_WIN32
  156. char *lockfile_path;
  157. #endif
  158. #ifdef HAVE_OPCACHE_FILE_CACHE
  159. char *file_cache;
  160. zend_bool file_cache_only;
  161. zend_bool file_cache_consistency_checks;
  162. #endif
  163. #if ENABLE_FILE_CACHE_FALLBACK
  164. zend_bool file_cache_fallback;
  165. #endif
  166. #ifdef HAVE_HUGE_CODE_PAGES
  167. zend_bool huge_code_pages;
  168. #endif
  169. } zend_accel_directives;
  170. typedef struct _zend_accel_globals {
  171. /* copy of CG(function_table) used for compilation scripts into cache */
  172. /* initially it contains only internal functions */
  173. HashTable function_table;
  174. int internal_functions_count;
  175. int counted; /* the process uses shared memory */
  176. zend_bool enabled;
  177. zend_bool locked; /* thread obtained exclusive lock */
  178. zend_bool accelerator_enabled; /* accelerator enabled for current request */
  179. zend_bool pcre_reseted;
  180. HashTable bind_hash; /* prototype and zval lookup table */
  181. zend_accel_directives accel_directives;
  182. zend_string *cwd; /* current working directory or NULL */
  183. zend_string *include_path; /* current value of "include_path" directive */
  184. char include_path_key[32]; /* key of current "include_path" */
  185. char cwd_key[32]; /* key of current working directory */
  186. int include_path_key_len;
  187. int include_path_check;
  188. int cwd_key_len;
  189. int cwd_check;
  190. int auto_globals_mask;
  191. time_t request_time;
  192. time_t last_restart_time; /* used to synchronize SHM and in-process caches */
  193. char system_id[32];
  194. HashTable xlat_table;
  195. #ifndef ZEND_WIN32
  196. zend_ulong root_hash;
  197. #endif
  198. /* preallocated shared-memory block to save current script */
  199. void *mem;
  200. void *arena_mem;
  201. zend_persistent_script *current_persistent_script;
  202. /* cache to save hash lookup on the same INCLUDE opcode */
  203. const zend_op *cache_opline;
  204. zend_persistent_script *cache_persistent_script;
  205. /* preallocated buffer for keys */
  206. int key_len;
  207. char key[MAXPATHLEN * 8];
  208. } zend_accel_globals;
  209. typedef struct _zend_string_table {
  210. uint32_t nTableMask;
  211. uint32_t nNumOfElements;
  212. zend_string *start;
  213. zend_string *top;
  214. zend_string *end;
  215. zend_string *saved_top;
  216. } zend_string_table;
  217. typedef struct _zend_accel_shared_globals {
  218. /* Cache Data Structures */
  219. zend_ulong hits;
  220. zend_ulong misses;
  221. zend_ulong blacklist_misses;
  222. zend_ulong oom_restarts; /* number of restarts because of out of memory */
  223. zend_ulong hash_restarts; /* number of restarts because of hash overflow */
  224. zend_ulong manual_restarts; /* number of restarts scheduled by opcache_reset() */
  225. zend_accel_hash hash; /* hash table for cached scripts */
  226. /* Directives & Maintenance */
  227. time_t start_time;
  228. time_t last_restart_time;
  229. time_t force_restart_time;
  230. zend_bool accelerator_enabled;
  231. zend_bool restart_pending;
  232. zend_accel_restart_reason restart_reason;
  233. zend_bool cache_status_before_restart;
  234. #ifdef ZEND_WIN32
  235. LONGLONG mem_usage;
  236. LONGLONG restart_in;
  237. #endif
  238. zend_bool restart_in_progress;
  239. /* uninitialized HashTable Support */
  240. uint32_t uninitialized_bucket[-HT_MIN_MASK];
  241. /* Interned Strings Support (must be the last element) */
  242. zend_string_table interned_strings;
  243. } zend_accel_shared_globals;
  244. extern zend_bool accel_startup_ok;
  245. #ifdef HAVE_OPCACHE_FILE_CACHE
  246. extern zend_bool file_cache_only;
  247. #endif
  248. #if ENABLE_FILE_CACHE_FALLBACK
  249. extern zend_bool fallback_process;
  250. #endif
  251. extern zend_accel_shared_globals *accel_shared_globals;
  252. #define ZCSG(element) (accel_shared_globals->element)
  253. #ifdef ZTS
  254. # define ZCG(v) ZEND_TSRMG(accel_globals_id, zend_accel_globals *, v)
  255. extern int accel_globals_id;
  256. # ifdef COMPILE_DL_OPCACHE
  257. ZEND_TSRMLS_CACHE_EXTERN()
  258. # endif
  259. #else
  260. # define ZCG(v) (accel_globals.v)
  261. extern zend_accel_globals accel_globals;
  262. #endif
  263. extern char *zps_api_failure_reason;
  264. void accel_shutdown(void);
  265. int accel_post_deactivate(void);
  266. void zend_accel_schedule_restart(zend_accel_restart_reason reason);
  267. void zend_accel_schedule_restart_if_necessary(zend_accel_restart_reason reason);
  268. accel_time_t zend_get_file_handle_timestamp(zend_file_handle *file_handle, size_t *size);
  269. int validate_timestamp_and_record(zend_persistent_script *persistent_script, zend_file_handle *file_handle);
  270. int validate_timestamp_and_record_ex(zend_persistent_script *persistent_script, zend_file_handle *file_handle);
  271. int zend_accel_invalidate(const char *filename, size_t filename_len, zend_bool force);
  272. int accelerator_shm_read_lock(void);
  273. void accelerator_shm_read_unlock(void);
  274. char *accel_make_persistent_key(const char *path, size_t path_length, int *key_len);
  275. zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type);
  276. #define IS_ACCEL_INTERNED(str) \
  277. ((char*)(str) >= (char*)ZCSG(interned_strings).start && (char*)(str) < (char*)ZCSG(interned_strings).top)
  278. zend_string* ZEND_FASTCALL accel_new_interned_string(zend_string *str);
  279. #endif /* ZEND_ACCELERATOR_H */