Php step-up PHPAPC analysis

Source: Internet
Author: User
Tags apc apc configuration
[Switch] php accelerates PHPAPC analysis PHPAPC provides two caching functions: cache Opcode (target file), which we call apc_compiler_cache. It also provides some interfaces for PHP developers to resident user data in the memory, which is called apc_user_cache. Here we mainly discuss the configuration of php-apc. Analysis on PHP acceleration by installing php [to] PHP APC
Php apc provides two caching functions: cache Opcode (target file), which is called apc_compiler_cache. It also provides some interfaces for PHP developers to resident user data in the memory, which is called apc_user_cache. Here we mainly discuss the configuration of php-apc.
Install PHP APC

As a test environment, we use CentOS5.3 (2.6.18-128. el5PAE) + Apache2.0 (prefork) + php5.2. We can go to pecl apc to download APC-3.0.19.tgz

# tar -xzvf APC-3.0.19.tgz#cd  APC-3.0.19# /usr/bin/phpize# ./configure --enable-apc --enable-mmap --enable-apc-spinlocks --disable-apc-pthreadmutex#make#make install


Note: mmap is supported and the spin locks are also used. Spinlocks is recommended by Facebook and also a lock mechanism recommended by APC developers.
Php apc configuration parameters

If your system environment is the same as my test environment, you can go to/etc/php. d. create the apc file under the Directory. and write the configuration to/etc/php. d/apc. ini file. Here, we have selected some common configurations and discussed them. Put related configurations together for explanation.

Apc. enabled = 1
The default value of apc. enabled is 1. you can set it to 0 to disable APC. If you set it to 0, you can also comment out extension = apc. so (this can save memory resources ). Once the APC function is enabled, the Opcodes will be cached to the shared memory.

Apc. shm_segments = 1
Apc. shm_size = 30
Since APC caches data in the memory, it is necessary to limit its memory resources. These two configurations can be used to limit the memory space available for APC. Apc. shm_segments specifies the number of shared memory blocks, while apc. shm_size specifies the size of a shared memory. the unit is M. Therefore, the memory size allowed for APC should be apc. shm_segments * apc. shm_size = 30 M. You can adjust the size of a shared memory. Of course, the maximum shared memory size is limited by the operating system, that is, the size cannot exceed/proc/sys/kernel/shmmax. Otherwise, APC fails to create shared memory. When apc. shm_size reaches the upper limit, you can set apc. shm_segments to allow APC to use more memory space. We recommend that if you call APC to use memory space, filter apc. shm_size first, and then filter apc. shm_segments. Specific values can be planned and adjusted based on apc. php monitoring. It is worth noting that the httpd daemon needs to be restarted for each adjustment so that the apc. so module can be reloaded. With the startup of the httpd daemon, the apc. so module is loaded. When apc. so is loaded and initialized, mmap requests are used to allocate memory of the specified size, that is, apc. shm_size * apc. shm_segments. In addition, the anonymous memory ing method is used here. by ing a special device/dev/zero, a large memory is provided, which fills up zero memory for APC management.
To verify the above statement, we commented out the apc. ini configuration and wrote the following php script to observe the allocated memory space initialized by the apc. so module.

 



# Strace-p 'cat/var/run/httpd. Pi'
Open ("/var/www/html/apc_load.php", O_RDONLY) = 13
...
Mmap2 (NULL, 31457280, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS,-1, 0) = 0xb5ce7000
...
Nanosleep ({3600, 0 },

The red part. The mmap system kernel call allocates 30 M (31457280/1024/1024/) memory space. PROT_READ | PROT_WRITE indicates that the memory space is available for reading and writing. MAP_SHARED indicates that the memory space is shared with other processes, that is, other processes can also read and write data. we can use apc. php to manage the memory space. MAP_ANONYMOUS indicates anonymous ING. In this example, fd =-1 indicates that the special device/dev/zero mapped here is ignored. The last 0 indicates no offset. We can also view the memory details of this block through the process image file.
# Cat, proc, 14735, and smaps

B5ce7000-b7ae7000 rw-s 00000000 633695/dev/zero (deleted)
Size: 30720 kB
Rss: 44 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 44 kB

It is easy to find that the starting address 0xb5ce7000 is the same as the address returned by the above mmap system kernel call. This block of memory can read and write rw and share s with other processes. While/dev/zero is a ing file. the file node is 633695. Here, size indicates the memory space that can be used by the process, while rss indicates the actually allocated memory, and Private_Dirty shows that the actually allocated 44 kB memory is allocated by the current process.

Apc. num_files_hint = 1000
Apc. user_entries_hint = 4096
These two configurations specify the number of cache entries that apc can have. Apc. num_files_hint indicates the estimated number of file opcodes to be slowed down, that is, the approximate number of apc_compiler_cache entries. In addition, apc. user_entries_hint indicates the estimated number of apc_userdata_cache entries to be cached. If your project does not use apc_store () to cache user data, you can set this value to a smaller value. That is to say, the sum of apc. num_files_hint and apc. user_entries_hint determines the maximum number of cached object entries allowed by APC. Setting these two values accurately gives you the best query performance. Of course, if you do not know how much cache (Cache object instance) you want, you do not have to modify these two configurations.
Here, apc. user_entries_hint is used to estimate the value of apc_store () based on the actual project development. In comparison, apc. num_files_hint can be more easily estimated by using the find command. For example, if our web root project is/var/vhosts, use the find command below to roughly count the current apc. num_files_hint number.
# Find/var/vhosts \ (-name "*. php"-or-name "*. inc" \)-type f-print | wc-l
1442

Apc. stat = 1
Apc. stat_ctime = 0
These two parameters are only related to the apc_compiler_cache cache and do not affect apc_user_cache. We mentioned earlier that apc_complier_cache caches the opcodes (target file) corresponding to the php source files one by one ). The PHP source file is stored on the disk device, and the corresponding Opcodes target file location memory space (shared memory), after the php source file is modified, how can I notify the opcodes of the updated memory? After receiving the request, APC checks the last modification time of the opened php source file, if the last modification time of the file is inconsistent with the last modification time of the cache object record of the corresponding memory space, APC considers the Opcode target File (Cache object) stored in the memory space) if it has expired, acp will clear the cache object and save the newly parsed Opcode. We are concerned that even if no php source file is updated, every time an http request is received, APC will request the system kernel to call stat () to obtain the final modification of the php source file. We can set apc. stat to 0 and require APC not to check whether the php source file corresponding to Opcodes is updated. In this way, we recommend the best performance. However, if the PHP source file is updated, restart the httpd daemon or call apc_cache_clear () the function clears the APC cache to ensure that the php source file is consistent with the Opcodes cached in the memory.

 # strace -e trace=file -p `cat /var/run/httpd.pid`getcwd("/var/www/html", 4096)           = 14stat64("/var/www/html/i1.php", {st_mode=S_IFREG|0644, st_size=39, ...}) = 0stat64("/var/www/html/i2.php", {st_mode=S_IFREG|0644, st_size=39, ...}) = 0lstat64("/var", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0lstat64("/var/www", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0lstat64("/var/www/html", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0lstat64("/var/www/html/i3.php", {st_mode=S_IFREG|0644, st_size=39, ...}) = 0open("/var/www/html/i3.php", O_RDONLY)  = 12stat64("/var/www/html/i3.php", {st_mode=S_IFREG|0644, st_size=39, ...}) = 0lstat64("/var", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0lstat64("/var/www", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0lstat64("/var/www/html", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0lstat64("/var/www/html/i4.php", {st_mode=S_IFREG|0644, st_size=39, ...}) = 0open("/var/www/html/i4.php", O_RDONLY)  = 12stat64("/var/www/html/i4.php", {st_mode=S_IFREG|0644, st_size=39, ...}) = 0stat64("/var/www/html/i5.php", {st_mode=S_IFREG|0644, st_size=39, ...}) = 0stat64("/var/www/html/i6.php", {st_mode=S_IFREG|0644, st_size=39, ...}) = 0chdir("/tmp")
= 0

# Strace-e trace = file-p 'cat/var/run/httpd. pid'
Getcwd ("/var/www/html", 4096) = 14
Open ("/var/www/html/i3.php", O_RDONLY) = 12
Open ("/var/www/html/i4.php", O_RDONLY) = 12
Chdir ("/tmp") = 0

The comparison shows that when apc. stat is set to 0, many system kernel calls are saved. We do not see that stat64 is called by the system kernel. Among them, i3.php and i4.php are respectively called by the include_once and require_once functions of php. it is handed over to the fstat () system kernel to check whether the file has been opened. From the performance perspective, require has better performance than require_once.

Setting apc. stat_ctime is significant. If apc. when the stat_ctime value is 1, only when the creation time (ctime) of the php source file is greater than the last modification time (mtime) of the php source file, the mtime of the cached object will be replaced by the ctime of the php source file. Otherwise, the mtime of the cached object will still be recorded as the mtime of the php source file. This prevents the use of tools such as cvs, svn, or rsync to refresh the mtime of the php source file. this will cause the APC to determine whether the cache object has expired by comparing the creation time ctime of the php source file. We recommend that you retain the default value apc. stat_ctime = 0.

Apc. ttl = 0
Apc. user_ttl = 0
The lifecycle of the cached object. Ttl indicates Time To Live, indicating that the cache object will be cleared after the specified Time. 0 indicates that it never expires. We mentioned earlier that the items that can be cached by APC are limited. if you set ttl to never expire, when the cache entry is full or the cache space is insufficient, the cache will fail.
Among them, apc. ttl acts on apc_compiler_cache. When apc. when ttl is greater than 0, each request compares the difference between the time of the request and the time of the previous request to be greater than apc. ttl. if the value is greater than apc. ttl.
What is interesting is apc. user_ttl, which mainly acts on apc_user_cache Cache. We know that this type of cache is a cache object created through apc_store ($ key, $ var, $ ttl = 0. What are the similarities and differences between the $ ttl specified in the apc_store () function and the apc. user_ttl set in php. ini. Because they also act on apc_userdata_cache cache. After analysis, we know that the basis for judging apc_user_cache cache expiration is when apc. user_ttl is greater than 0, and the difference between the http request time and the previous http request time is greater than apc. user_ttl indicates that the cache entry has expired; or, user. data. ttl (the $ ttl specified in the php function apc_store () is greater than 0, and the difference between the http request time and the cache object creation time ctime is greater than the user. data. ttl, the cache entries are also considered to have expired and will be cleared.
We recommend that you set apc. stat to 0 if your project is stable. If apc. shm_size and apc. num_files_hint are set properly, we recommend that you set apc. ttl to 0. That is, apc_compiler_cache will never be recycled until the httpd daemon is restarted or the apc_cache_clear () function is called to clear the cache. For apc. user_ttl, we recommend that you set it to 0. when the developer calls the apc_store () function, set $ ttl to specify the lifecycle of the cache object.

Apc. slam_defense = 0
Apc. write_lock = 1
Apc. file_update_protection = 2
The reason for putting these three configurations together is that they have similar meanings. Among them, apc. file_update_protection is better understood, and its unit is time unit second. If the difference between the current http request time and the php source file is better than apc. file_update_protection time, APC will not cache the Opcodes corresponding to the php source file until a next visit, and the access time and the last modification time of the php source file are later than apc. file_update_protection Time. only the corresponding Opcodes will be cached in the shared memory. The advantage of this is that it is not easy for users to access the source file you are modifying. We recommend that you set this value to a greater value in the development environment, but keep the default value in the operation environment.
When your website has a high concurrency, multiple sub-processes fork by the http daemon may cache the same Opcodes at the same time. Apc. slam_defense can reduce the probability of such a thing. For example, when the apc. slam_defense value is set to 60, cache-free Opcodes will not be cached for 60 times per 100 times. For websites with low concurrency, we recommend that you set this value to 0. for websites with high concurrency, we can adjust this value according to statistics. While apc. write_lock is a Boolean value. when this value is set to 1, when multiple processes cache the same Opcodes at the same time, only the first process cache is valid, and others are invalid. The apc. write_lock setting effectively avoids the cache write competition.

Apc. max_file_size = 1 M
Apc. filters = NULL
Apc. cache_by_default = 1
These three configurations are put together because they are used to restrict the cache. Among them, apc. max_file_size indicates that if the php source file exceeds 1 MB, the corresponding opcodes will not be cached. Apc. filters specifies a file filter list separated by commas. When apc. cache_by_default is equal to 1, files that match the specified file name in the apc. filters list will not be cached. Conversely, when apc. cache_by_default is 0, only files that match the files specified in the acp. filters list are cached.
Summary

1. use the Spinlocks lock mechanism to achieve optimal performance.
2. APC provides apc. php for monitoring and managing the APC cache. Do not forget to change the administrator name and password
3. by default, APC creates shared memory through mmap anonymous ing, and cache objects are stored in this "large" memory space. APC manages the shared memory
4. we need to adjust the values of apc. shm_size, apc. num_files_hints, and apc. user_entries_hint through statistics. Until the best
5. well, I admit that apc. stat = 0 can achieve better performance. Everything I want to do is acceptable.
6. PHP pre-defined constants. you can use the apc_define_constants () function. However, according to APC developers, the performance of pecl hidef is better, and define is inefficient.
7. the apc_store () function. for PHP variables such as system settings, the life cycle is the whole application (from the httpd daemon process to the httpd daemon). APC is better than Memcached. It must not pass through the network transmission protocol tcp.
8. APC is not suitable for caching user data that frequently changes through the apc_store () function, and some strange phenomena may occur.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.