Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PHP Accelerator configuration artifact how to use opcache

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use the PHP Accelerator configuration artifact opcache. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

What is opcode?

When the interpreter finishes analyzing the script code, it generates intermediate code, also known as Operate Code,opcode, that can be run directly. The goal of Opcode cache is to avoid repeated compilation and reduce CPU and memory overhead. If the performance bottleneck of dynamic content does not lie in CPU and memory, but in the Imax O operations, such as the disk Imax O overhead caused by database queries, then the performance improvement of opcode cache is very limited. But since opcode cache can lead to lower CPU and memory overhead, this is always a good thing.

Why use opcode caching?

This starts with the life cycle of the PHP code. When you request a PHP script, you go through five steps, as shown in the following figure:

The Zend engine must read files from the file system, scan its dictionaries and expressions, parse files, create computer code to execute (called Opcode), and finally execute Opcode. Each request PHP script will perform the above steps once. If the PHP source code does not change, then the Opcode will not change. Obviously, there is no need to generate Opcode every time. Combined with the ubiquitous caching mechanism in Web, we can cache the Opcode. Wouldn't it be faster to access the cached Opcode directly in the future? the flow chart after enabling Opcode caching is as follows:

PHP opcode principle

Opcode is an intermediate language compiled by PHP scripts, such as ByteCode for Java or MSL for .NET. For example, you write the following PHP code:

< ?php echo "Hello World"; $a = 1 + 1; echo $a;?>

When PHP executes this code, it goes through the following four steps (specifically, it should be PHP's language engine Zend)

1) Scanning (Lexing) to convert the PHP code into a language fragment (Tokens). 2) Parsing, which converts Tokens into a simple and meaningful expression. 3) Compilation, compile the expression to Opocdes. 4) Execution, execute Opcodes sequentially, one at a time, thus realizing the function of PHP script.

PHP opcache introduction

Optimizer+ (Optimizer+ was renamed Opcache in mid-March 2013). OPcache improves the performance of PHP by storing precompiled bytecode in shared memory. The advantage of storing precompiled bytecode is that it saves the overhead of loading and parsing PHP scripts each time.

The OPcache extension has been bundled with PHP 5.5.0 and later. The OPcache library in the »PECL extension can be used for versions 5.3 and 5.4 of PHP 5.2.

PHP 5.5.0 and later

OPcache can only be compiled as a shared extension. If you disable the construction of the default extension with the-disable-all parameter, you must use the-enable-opcache option to turn on OPcache. After compilation, you can use the zend_extension directive to load the OPcache extension into PHP.

Recommended php.ini settings

Use the following recommended settings for better performance:

Opcache.memory_consumption=128opcache.interned_strings_buffer=8opcache.max_accelerated_files=4000opcache.revalidate_freq=60opcache.fast_shutdown=1opcache.enable_cli=1opcache.save_comments=0

You can also disable opcache.save_comments and enable opcache.enable_file_override. It is important to note that before using the above configuration in a production environment, it must be rigorously tested. Because there is a known problem with the above configuration, it can throw some exceptions to frameworks and applications, especially if comments are used in existing documents.

The following is the configuration description of opcache, which is the default configuration:

; opcache switch, turn off the time code is no longer optimized. Opcache. enable = 1; Determines if Zend OPCache is enabled for the CLI version of PHPopcache.enable_cli=1; OPcache shared memory size in megabytes. Total amount of precompiled PHP code that can be stored (in MB); recommended 128opcache.memory_consumption=64; memory size used to store temporary strings in megabytes.; recommended 8opcache.interned_strings_buffer=4; maximum number of cached files between 200 and 100000.; recommended 4000opcache.max_accelerated_files=2000; memory "waste" reaches the corresponding percentage of this value, a restart schedule will be initiated. Opcache.max _ wasted_percentage=5 When this instruction is turned on, Zend Optimizer + automatically appends the name of the current working directory to the script key to eliminate key naming conflicts between files with the same name. Turning off this directive will improve performance, but damage existing applications. Opcache. use _ cwd=0; open the file timestamp to verify opcache.validate_timestamps=1; and check that the script timestamp has an update period in seconds. Setting to 0 causes OPcache to check for script updates for each request.; recommend 60opcache.revalidate_freq=2; optimizations that allow or disable file search in include_path. Opcache.revalidate _ path=0 If disabled, the comments in the script file will not be included in the opcode cache file, which can effectively reduce the optimized file size. Disabling this configuration directive may cause some applications or frameworks that rely on comments or annotations not to work properly, such as Doctrine,Zend Framework2; 0opcache.save_comments=1 is recommended; if disabled, these comments will not be loaded even if the file contains comments. This option can be used with opcache.save_comments to load comment content on demand. Opcache.load_comments=1; opens and closes quickly, opening this will increase the speed of collecting memory when PHP Request Shutdown is opened.; recommend 1opcache.fast_shutdown=1; allow override file existence (file_exists, etc.) optimization feature .opcache.enable _ file_override=0; define how many optimization processes are started. Opcache.optimization _ level=0xffffffff Enable this Hack to temporarily resolve the "can't redeclare class" error. Opcache.inherited_hack=1; enable this Hack to temporarily resolve the "can't redeclare class" error; opcache.dups_fix=0; remove the cache of large files by the file size screen, all files will be cached by default.; opcache.max_file_size=0; check the cache check every N requests. The default value of 0 means that checking is disabled. Since calculating the check value is detrimental to performance, this instruction should be turned on tightly during development and debugging.; opcache.consistency_checks=0; after the cache is not accessed, how long (in seconds) to wait for scheduling restart.; opcache.force_restart_timeout=180; logging level, default is only fatal error and error.;opcache.error_log=; write error information to the server (Apache, etc.) log Opcache.log_verbosity_level=1; preferred background for memory sharing. Leave blank to let the system choose.; opcache.preferred_memory_model=; protect shared memory from accidental writes when running php scripts, which is only useful for debug.; opcache.protect_memory=0

Finally, let's talk about the pitfalls that you should pay attention to when using opcache to accelerate php:

Opcache relies on the modify time of the PHP file as the detection condition for the file to be modified, which leads to two problems.

The first problem is that when doing a version rollback, because the file modification time after the version rollback is a little earlier than the file time of the existing opcache cache, it may cause opcache not to clear the cache and require manual reload.

The second problem is that when a version is released, it is usually in the sync mode. It may be cached by opcache half of the time when the file is released, and a program error will be reported when the user accesses it. This is mainly because the file content is half cached, but the timestamp of the file will not change, so even if the opcache is detected, the new file will not be read, and manual reload is needed.

Not only can reload solve these two problems, but the opcache cache can also be cleared by calling the interface of opcache.

You can use the opcache_reset () or opcache_invalidate () function to manually reset the OPcache.

Opcache_reset (): this function resets the entire bytecode cache, and after calling opcache_reset (), all scripts will be reloaded and reparsed the next time they are clicked.

Opcache_invalidate (): this function invalidates the bytecode cache of the specified script. If force is not set or FALSE is passed in, the script's cache will be invalidated only if the modification time of the script is newer than the time of the corresponding bytecode.

However, it is not recommended. Individuals call opcache_reset () to clear the cache after code release in the production environment (the test does clear the cache). There have been strange problems (applications with high traffic), and then they decisively give up and use the reload method.

On the PHP accelerator configuration artifact opcache how to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report