ZendAccelerator.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend OPcache |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 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. | https://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(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. bool corrupted;
  103. bool is_phar;
  104. bool empty;
  105. uint32_t num_warnings;
  106. zend_error_info **warnings;
  107. void *mem; /* shared memory area used by script structures */
  108. size_t size; /* size of used shared memory */
  109. /* All entries that shouldn't be counted in the ADLER32
  110. * checksum must be declared in this struct
  111. */
  112. struct zend_persistent_script_dynamic_members {
  113. time_t last_used;
  114. #ifdef ZEND_WIN32
  115. LONGLONG hits;
  116. #else
  117. zend_ulong hits;
  118. #endif
  119. unsigned int memory_consumption;
  120. unsigned int checksum;
  121. time_t revalidate;
  122. } dynamic_members;
  123. } zend_persistent_script;
  124. typedef struct _zend_accel_directives {
  125. zend_long memory_consumption;
  126. zend_long max_accelerated_files;
  127. double max_wasted_percentage;
  128. char *user_blacklist_filename;
  129. zend_long consistency_checks;
  130. zend_long force_restart_timeout;
  131. bool use_cwd;
  132. bool ignore_dups;
  133. bool validate_timestamps;
  134. bool revalidate_path;
  135. bool save_comments;
  136. bool record_warnings;
  137. bool protect_memory;
  138. bool file_override_enabled;
  139. bool enable_cli;
  140. bool validate_permission;
  141. #ifndef ZEND_WIN32
  142. bool validate_root;
  143. #endif
  144. zend_ulong revalidate_freq;
  145. zend_ulong file_update_protection;
  146. char *error_log;
  147. #ifdef ZEND_WIN32
  148. char *mmap_base;
  149. #endif
  150. char *memory_model;
  151. zend_long log_verbosity_level;
  152. zend_long optimization_level;
  153. zend_long opt_debug_level;
  154. zend_long max_file_size;
  155. zend_long interned_strings_buffer;
  156. char *restrict_api;
  157. #ifndef ZEND_WIN32
  158. char *lockfile_path;
  159. #endif
  160. char *file_cache;
  161. bool file_cache_only;
  162. bool file_cache_consistency_checks;
  163. #if ENABLE_FILE_CACHE_FALLBACK
  164. bool file_cache_fallback;
  165. #endif
  166. #ifdef HAVE_HUGE_CODE_PAGES
  167. bool huge_code_pages;
  168. #endif
  169. char *preload;
  170. #ifndef ZEND_WIN32
  171. char *preload_user;
  172. #endif
  173. #ifdef ZEND_WIN32
  174. char *cache_id;
  175. #endif
  176. } zend_accel_directives;
  177. typedef struct _zend_accel_globals {
  178. int counted; /* the process uses shared memory */
  179. bool enabled;
  180. bool locked; /* thread obtained exclusive lock */
  181. bool accelerator_enabled; /* accelerator enabled for current request */
  182. bool pcre_reseted;
  183. zend_accel_directives accel_directives;
  184. zend_string *cwd; /* current working directory or NULL */
  185. zend_string *include_path; /* current value of "include_path" directive */
  186. char include_path_key[32]; /* key of current "include_path" */
  187. char cwd_key[32]; /* key of current working directory */
  188. int include_path_key_len;
  189. int include_path_check;
  190. int cwd_key_len;
  191. int cwd_check;
  192. int auto_globals_mask;
  193. time_t request_time;
  194. time_t last_restart_time; /* used to synchronize SHM and in-process caches */
  195. HashTable xlat_table;
  196. #ifndef ZEND_WIN32
  197. zend_ulong root_hash;
  198. #endif
  199. /* preallocated shared-memory block to save current script */
  200. void *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. zend_string key;
  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. size_t map_ptr_last;
  227. /* Directives & Maintenance */
  228. time_t start_time;
  229. time_t last_restart_time;
  230. time_t force_restart_time;
  231. bool accelerator_enabled;
  232. bool restart_pending;
  233. zend_accel_restart_reason restart_reason;
  234. bool cache_status_before_restart;
  235. #ifdef ZEND_WIN32
  236. LONGLONG mem_usage;
  237. LONGLONG restart_in;
  238. #endif
  239. bool restart_in_progress;
  240. /* Preloading */
  241. zend_persistent_script *preload_script;
  242. zend_persistent_script **saved_scripts;
  243. /* uninitialized HashTable Support */
  244. uint32_t uninitialized_bucket[-HT_MIN_MASK];
  245. /* Tracing JIT */
  246. void *jit_traces;
  247. const void **jit_exit_groups;
  248. /* Interned Strings Support (must be the last element) */
  249. zend_string_table interned_strings;
  250. } zend_accel_shared_globals;
  251. #ifdef ZEND_WIN32
  252. extern char accel_uname_id[32];
  253. #endif
  254. extern bool accel_startup_ok;
  255. extern bool file_cache_only;
  256. #if ENABLE_FILE_CACHE_FALLBACK
  257. extern bool fallback_process;
  258. #endif
  259. extern zend_accel_shared_globals *accel_shared_globals;
  260. #define ZCSG(element) (accel_shared_globals->element)
  261. #ifdef ZTS
  262. # define ZCG(v) ZEND_TSRMG(accel_globals_id, zend_accel_globals *, v)
  263. extern int accel_globals_id;
  264. # ifdef COMPILE_DL_OPCACHE
  265. ZEND_TSRMLS_CACHE_EXTERN()
  266. # endif
  267. #else
  268. # define ZCG(v) (accel_globals.v)
  269. extern zend_accel_globals accel_globals;
  270. #endif
  271. extern char *zps_api_failure_reason;
  272. void accel_shutdown(void);
  273. zend_result accel_activate(INIT_FUNC_ARGS);
  274. zend_result accel_post_deactivate(void);
  275. void zend_accel_schedule_restart(zend_accel_restart_reason reason);
  276. void zend_accel_schedule_restart_if_necessary(zend_accel_restart_reason reason);
  277. accel_time_t zend_get_file_handle_timestamp(zend_file_handle *file_handle, size_t *size);
  278. int validate_timestamp_and_record(zend_persistent_script *persistent_script, zend_file_handle *file_handle);
  279. int validate_timestamp_and_record_ex(zend_persistent_script *persistent_script, zend_file_handle *file_handle);
  280. int zend_accel_invalidate(zend_string *filename, bool force);
  281. int accelerator_shm_read_lock(void);
  282. void accelerator_shm_read_unlock(void);
  283. zend_string *accel_make_persistent_key(zend_string *path);
  284. zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type);
  285. #define IS_ACCEL_INTERNED(str) \
  286. ((char*)(str) >= (char*)ZCSG(interned_strings).start && (char*)(str) < (char*)ZCSG(interned_strings).top)
  287. zend_string* ZEND_FASTCALL accel_new_interned_string(zend_string *str);
  288. uint32_t zend_accel_get_class_name_map_ptr(zend_string *type_name);
  289. /* memory write protection */
  290. #define SHM_PROTECT() \
  291. do { \
  292. if (ZCG(accel_directives).protect_memory) { \
  293. zend_accel_shared_protect(1); \
  294. } \
  295. } while (0)
  296. #define SHM_UNPROTECT() \
  297. do { \
  298. if (ZCG(accel_directives).protect_memory) { \
  299. zend_accel_shared_protect(0); \
  300. } \
  301. } while (0)
  302. #endif /* ZEND_ACCELERATOR_H */