xref: /PHP-7.1/ext/opcache/ZendAccelerator.h (revision 50949c93)
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 
93 # if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || (defined(__APPLE__) && defined(__MACH__)/* Darwin */) || defined(__OpenBSD__) || defined(__NetBSD__)
94 #  define FLOCK_STRUCTURE(name, type, whence, start, len) \
95 		struct flock name = {start, len, -1, type, whence}
96 # elif defined(__svr4__)
97 #  define FLOCK_STRUCTURE(name, type, whence, start, len) \
98 		struct flock name = {type, whence, start, len}
99 # elif defined(__linux__) || defined(__hpux) || defined(__GNU__)
100 #  define FLOCK_STRUCTURE(name, type, whence, start, len) \
101 		struct flock name = {type, whence, start, len, 0}
102 # elif defined(_AIX)
103 #  if defined(_LARGE_FILES) || defined(__64BIT__)
104 #   define FLOCK_STRUCTURE(name, type, whence, start, len) \
105 		struct flock name = {type, whence, 0, 0, 0, start, len }
106 #  else
107 #   define FLOCK_STRUCTURE(name, type, whence, start, len) \
108 		struct flock name = {type, whence, start, len}
109 #  endif
110 # elif defined(HAVE_FLOCK_BSD)
111 #  define FLOCK_STRUCTURE(name, type, whence, start, len) \
112 		struct flock name = {start, len, -1, type, whence}
113 # elif defined(HAVE_FLOCK_LINUX)
114 #  define FLOCK_STRUCTURE(name, type, whence, start, len) \
115 		struct flock name = {type, whence, start, len}
116 # else
117 #  error "Don't know how to define struct flock"
118 # endif
119 #endif
120 
121 #if defined(HAVE_OPCACHE_FILE_CACHE) && defined(ZEND_WIN32)
122 # define ENABLE_FILE_CACHE_FALLBACK 1
123 #else
124 # define ENABLE_FILE_CACHE_FALLBACK 0
125 #endif
126 
127 #if ZEND_WIN32
128 typedef unsigned __int64 accel_time_t;
129 #else
130 typedef time_t accel_time_t;
131 #endif
132 
133 typedef enum _zend_accel_restart_reason {
134 	ACCEL_RESTART_OOM,    /* restart because of out of memory */
135 	ACCEL_RESTART_HASH,   /* restart because of hash overflow */
136 	ACCEL_RESTART_USER    /* restart scheduled by opcache_reset() */
137 } zend_accel_restart_reason;
138 
139 typedef struct _zend_persistent_script {
140 	zend_script    script;
141 	zend_long      compiler_halt_offset;   /* position of __HALT_COMPILER or -1 */
142 	int            ping_auto_globals_mask; /* which autoglobals are used by the script */
143 	accel_time_t   timestamp;              /* the script modification time */
144 	zend_bool      corrupted;
145 	zend_bool      is_phar;
146 
147 	void          *mem;                    /* shared memory area used by script structures */
148 	size_t         size;                   /* size of used shared memory */
149 	void          *arena_mem;              /* part that should be copied into process */
150 	size_t         arena_size;
151 
152 	/* All entries that shouldn't be counted in the ADLER32
153 	 * checksum must be declared in this struct
154 	 */
155 	struct zend_persistent_script_dynamic_members {
156 		time_t       last_used;
157 #ifdef ZEND_WIN32
158 		LONGLONG   hits;
159 #else
160 		zend_ulong        hits;
161 #endif
162 		unsigned int memory_consumption;
163 		unsigned int checksum;
164 		time_t       revalidate;
165 	} dynamic_members;
166 } zend_persistent_script;
167 
168 typedef struct _zend_accel_directives {
169 	zend_long           memory_consumption;
170 	zend_long           max_accelerated_files;
171 	double         max_wasted_percentage;
172 	char          *user_blacklist_filename;
173 	zend_long           consistency_checks;
174 	zend_long           force_restart_timeout;
175 	zend_bool      use_cwd;
176 	zend_bool      ignore_dups;
177 	zend_bool      validate_timestamps;
178 	zend_bool      revalidate_path;
179 	zend_bool      save_comments;
180 	zend_bool      fast_shutdown;
181 	zend_bool      protect_memory;
182 	zend_bool      file_override_enabled;
183 	zend_bool      inherited_hack;
184 	zend_bool      enable_cli;
185 	zend_bool      validate_permission;
186 #ifndef ZEND_WIN32
187 	zend_bool      validate_root;
188 #endif
189 	zend_ulong     revalidate_freq;
190 	zend_ulong     file_update_protection;
191 	char          *error_log;
192 #ifdef ZEND_WIN32
193 	char          *mmap_base;
194 #endif
195 	char          *memory_model;
196 	zend_long           log_verbosity_level;
197 
198 	zend_long           optimization_level;
199 	zend_long           opt_debug_level;
200 	zend_long           max_file_size;
201 	zend_long           interned_strings_buffer;
202 	char          *restrict_api;
203 #ifndef ZEND_WIN32
204 	char          *lockfile_path;
205 #endif
206 #ifdef HAVE_OPCACHE_FILE_CACHE
207 	char          *file_cache;
208 	zend_bool      file_cache_only;
209 	zend_bool      file_cache_consistency_checks;
210 #endif
211 #if ENABLE_FILE_CACHE_FALLBACK
212 	zend_bool      file_cache_fallback;
213 #endif
214 #ifdef HAVE_HUGE_CODE_PAGES
215 	zend_bool      huge_code_pages;
216 #endif
217 } zend_accel_directives;
218 
219 typedef struct _zend_accel_globals {
220     /* copy of CG(function_table) used for compilation scripts into cache */
221     /* initially it contains only internal functions */
222 	HashTable               function_table;
223 	int                     internal_functions_count;
224 	int                     counted;   /* the process uses shared memory */
225 	zend_bool               enabled;
226 	zend_bool               locked;    /* thread obtained exclusive lock */
227 	HashTable               bind_hash; /* prototype and zval lookup table */
228 	zend_accel_directives   accel_directives;
229 	zend_string            *cwd;                  /* current working directory or NULL */
230 	zend_string            *include_path;         /* current value of "include_path" directive */
231 	char                    include_path_key[32]; /* key of current "include_path" */
232 	char                    cwd_key[32];          /* key of current working directory */
233 	int                     include_path_key_len;
234 	int                     include_path_check;
235 	int                     cwd_key_len;
236 	int                     cwd_check;
237 	int                     auto_globals_mask;
238 	time_t                  request_time;
239 	time_t                  last_restart_time; /* used to synchronize SHM and in-process caches */
240 	char                    system_id[32];
241 	HashTable               xlat_table;
242 #ifndef ZEND_WIN32
243 	zend_ulong              root_hash;
244 #endif
245 	/* preallocated shared-memory block to save current script */
246 	void                   *mem;
247 	void                   *arena_mem;
248 	zend_persistent_script *current_persistent_script;
249 	/* cache to save hash lookup on the same INCLUDE opcode */
250 	const zend_op          *cache_opline;
251 	zend_persistent_script *cache_persistent_script;
252 	/* preallocated buffer for keys */
253 	int                     key_len;
254 	char                    key[MAXPATHLEN * 8];
255 } zend_accel_globals;
256 
257 typedef struct _zend_accel_shared_globals {
258 	/* Cache Data Structures */
259 	zend_ulong   hits;
260 	zend_ulong   misses;
261 	zend_ulong   blacklist_misses;
262 	zend_ulong   oom_restarts;     /* number of restarts because of out of memory */
263 	zend_ulong   hash_restarts;    /* number of restarts because of hash overflow */
264 	zend_ulong   manual_restarts;  /* number of restarts scheduled by opcache_reset() */
265 	zend_accel_hash hash;             /* hash table for cached scripts */
266 
267 	/* Directives & Maintenance */
268 	time_t          start_time;
269 	time_t          last_restart_time;
270 	time_t          force_restart_time;
271 	zend_bool       accelerator_enabled;
272 	zend_bool       restart_pending;
273 	zend_accel_restart_reason restart_reason;
274 	zend_bool       cache_status_before_restart;
275 #ifdef ZEND_WIN32
276 	LONGLONG   mem_usage;
277 	LONGLONG   restart_in;
278 #endif
279 	zend_bool       restart_in_progress;
280 	/* Interned Strings Support */
281 	char           *interned_strings_start;
282 	char           *interned_strings_top;
283 	char           *interned_strings_end;
284 	char           *interned_strings_saved_top;
285 	HashTable       interned_strings;
286 	/* uninitialized HashTable Support */
287 	uint32_t uninitialized_bucket[-HT_MIN_MASK];
288 } zend_accel_shared_globals;
289 
290 extern zend_bool accel_startup_ok;
291 #ifdef HAVE_OPCACHE_FILE_CACHE
292 extern zend_bool file_cache_only;
293 #endif
294 #if ENABLE_FILE_CACHE_FALLBACK
295 extern zend_bool fallback_process;
296 #endif
297 
298 extern zend_accel_shared_globals *accel_shared_globals;
299 #define ZCSG(element)   (accel_shared_globals->element)
300 
301 #ifdef ZTS
302 # define ZCG(v)	ZEND_TSRMG(accel_globals_id, zend_accel_globals *, v)
303 extern int accel_globals_id;
304 # ifdef COMPILE_DL_OPCACHE
305 ZEND_TSRMLS_CACHE_EXTERN()
306 # endif
307 #else
308 # define ZCG(v) (accel_globals.v)
309 extern zend_accel_globals accel_globals;
310 #endif
311 
312 extern char *zps_api_failure_reason;
313 
314 void accel_shutdown(void);
315 int  accel_post_deactivate(void);
316 void zend_accel_schedule_restart(zend_accel_restart_reason reason);
317 void zend_accel_schedule_restart_if_necessary(zend_accel_restart_reason reason);
318 accel_time_t zend_get_file_handle_timestamp(zend_file_handle *file_handle, size_t *size);
319 int  validate_timestamp_and_record(zend_persistent_script *persistent_script, zend_file_handle *file_handle);
320 int  validate_timestamp_and_record_ex(zend_persistent_script *persistent_script, zend_file_handle *file_handle);
321 int  zend_accel_invalidate(const char *filename, int filename_len, zend_bool force);
322 int  accelerator_shm_read_lock(void);
323 void accelerator_shm_read_unlock(void);
324 
325 char *accel_make_persistent_key(const char *path, int path_length, int *key_len);
326 zend_op_array *persistent_compile_file(zend_file_handle *file_handle, int type);
327 
328 #define IS_ACCEL_INTERNED(str) \
329 	((char*)(str) >= ZCSG(interned_strings_start) && (char*)(str) < ZCSG(interned_strings_end))
330 
331 zend_string *accel_new_interned_string(zend_string *str);
332 
333 #endif /* ZEND_ACCELERATOR_H */
334