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