Difference between #5 and #9 of
Getting the Most out of APC for Yii

Revision #9 has been created by Da:Sourcerer on Dec 10, 2013, 5:04:43 AM with the memo:

Added a note regarding apc.shm_strings_buffer
« previous (#5) next (#10) »

Changes

Title unchanged

Getting the Most out of APC for Yii

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

caching, apc

Content changed

Preface ------- The [Advanced PHP Cache](http://pecl.php.net/package/APC) is a PHP extension which primarily serves as an opcode cache for PHP. The basic idea is to save PHP from re-evaluating the PHP code to intermediate bytecode on each request. Installing and enabling APC already yields a significant performance benefit. However, APC is not a black box that will magically change all for the better. More over it is important to understand that APC needs memory to operate. Hence, _b
 
> Note: B
y enabling APC you are trading memory for speed_.

In addition to the opcode cache, APC can also serve as a user cache for Yii via [CApcCache]. It should be noted that this will make APC's memory needs less predictable.
[...]
If you are running a self-compiled version of PHP, you can install APC via the PEAR/PECL command-line tool:
~~~
$
 sudo pecl install APC ~~~ > Info: Be advised that this will **_compile**_ the current version of APC on your system, so you'll need to have the GCC toolchain installed as well as the PHP headers along with the `phpize` helper.
 
 
You will almost certainly need to add APC to your PHP configuration. Otherwise, PHP won't load it. Simply drop a new file called `apc.ini` into `/usr/local/etc/php.d` (RedHat) or `/usr/local/php.d/apache2/conf.d` (Debian) with the following content:
~~~
extension=apc.so
[...]
Preparing Yii
=============
**The main gain provided by APC is the opcode cache that will speed up each request**. The Yii framework comes with a `yiilite.php` which can be used instead of `yii.php`. Despite its name, it includes the most used classes. The benefit to APC is that the opcode cache can be quickly filled via one single file instead of including several smaller files and dynamically loading several classes. This change must be done in the file `index.php`, in the webroot directory of your application.

If you wish to use APC as a user cache as well, you'll need to register the CApcCache as cache component in your config. Locate the `components`-stanza in your config and add the following lines:
[...]
Self explaining. This option enables or disables APC alltogether. Be advised that this is a system-wide setting, soyou cannot selectively switch APC on or off for e.g. certain directories via a `.htaccess` or `.user.ini` file.
You'll want this to be set to **on**, which is the default value.
- [`apc.shm_size`](http://php.net/manual/apc.configuration.php#ini.apc.shm-size) The size of the cache. Take note that is the size of the opcode **and** the user cache. Currently, APC does not allow you to specify seperate memory segments for these. As a general rule: Do not set this lower than 16MB and monitor APC's memory usage closely. If you set this too low, APC's memory might suffer heavy fragmentation, which will result in a high performance penalty
 or even constant segfaults. Monitor your APC activity (see the following section) and set a value where APC's memory isn't too fragmented (generally at least 32 MB). - [`apc.stat`](http://php.net/manual/apc.configuration.php#ini.apc.stat) Controls if APC should check for modified files on every request. This setting will bring you the greatest speed benefit. Set it to **0** on production servers but to **1** in development environments (otherwise PHP won't pick up changes you've applied to the code). If you're rolling out a new version of your software, simply restart your webserver or FastCGI process in order to clear the opcode cache.  
- [`apc.shm_strings_buffer`](http://php.net/manual/apc.configuration.php#ini.apc.shm-strings-buffer) This setting got introduced in v3.1.14 of APC. It controls the internal strings buffer that is being shared between php-fpm worker instances, avoiding to store stings for each instance separately. The adequate size of this setting is extremely difficult to evaluate as no fill count is reported through APC's API. Try to set this as low as possible as the memory penalty for a value too high is quite hefty, increasing the memory need by several magnitudes in its extremes.


Other interesting parameters are:
[...]
- [`apc.user_entries_hint`](http://php.net/manual/apc.configuration.php#ini.apc.user-entries-hint) Same as `apc.num_files_hint` but for user cache entries, so it is only interesting if you're using APC as a user cache, too. A too high value might result in over-provisioning memory for the cache. This is highly individual, so you'll need to evaluate this yourself.
- [`apc.mmap_file_mask`](http://php.net/manual/apc.configuration.php#ini.apc.mmap-file-mask)
Filemask for the shared memory mechanism. The
 best setting I could find has been **/dev/shm/apc.XXXXXX**re are three kinds of values this setting can take. Something like `/tmp/apc.XXXXXX` (place exactly 6 `X`'s) will  create an mmap'ed file in your `/tmp` directory. `/dev/zero` does the same but without creating a file. The best setting I could find has been **/dev/shm/apc.XXXXXX**. For this to work properly, `/dev/shm` must be mounted on a tempfs. The benefit of using shared memory for APC lies in the lockless nature. The downside, however, is that this method for storing APC's content is everything but thread-safe. This is mostly important if you are running PHP in an Apache environment via mod_php with an MPM other than [prefork](http://httpd.apache.org/docs/2.2/mod/prefork.html), as you will experience constant segfaults. - [`apc.lazy_classes`](http://php.net/manual/apc.configuration.php#ini.apc.lazy-classes) and [`apc.lazy_functions`](http://php.net/manual/apc.configuration.php#ini.apc.lazy-functions) These settings are marked as _experimental_. However, I've found no regressions after using these. If you're feeling lucky and you're using anonymous functions for [`evaluateExpression`()`](http://www.yiiframework.com/doc/api/1.1/CComponent/#evaluateExpression-detail), you may want to set these to **1**. - [`apc.include_once_override`](http://php.net/manual/apc.configuration.php#ini.apc.include-once-override) Setting this to on will speed up `include*` and `require*` calls. However, this setting will introduce changes in the behaviour of PHP. While Yii is running fine with it but others (such as phpMyAdmin) aren't so happy. So **evaluate carefully if this doesn't break other applications on your server**. You've been warned!
 
 
Monitoring APC
 
--------------
 
Some of the aforementioned settings can be very specific to your setup and your application. So you need to take a look at what APC is doing. There are currently two ways of achieving this:
 
 
 - APC itself comes witha bundled `apc.php` which will tell you a bit about memory consumption, fragmentation and cache content. You'll most likely find it in `/usr/local/doc/apc`. Copy it to a location where your webserver can reach it and take appropriate measures to protect it from unauthorized access (open the php file and set the password in its first lines).
 
 - Available as an extension to your very own app comes the [apcinfo extension](http://yiiframework.com/extension/apcinfo) which does roughly the same job as `apc.php` but integrates nicely into your app
content with this being switched on, other applications (such as phpMyAdmin) are not. So watch out for breaking applications.
 
- [`apc.serializer`](http://php.net/manual/apc.configuration.php#ini.apc.serializer) Starting with version 3.1.7, APC can facilitate a substitute for PHP's native [`serialize()`](http://php.net/serialize) function. This is most interesting in conjunction with the [igbinary serializer](http://pecl.php.net/package/igbinary), which aims to provide a more compact serialization. This _might_ result into smaller cache sizes for you (among with a small speed benefit). But again: Your milage may vary, so evaluate carfully. It should be noted that the two serialization formats are incompatible. So when changing the serializer, clearing caches is advisable.
 
- [`apc.optimizer`](http://php.net/manual/apc.configuration.php#ini.apc.optimizer) This setting receives mentioning every now and then. But truth be told: It doesn't do anything since [APC v3.0.13](http://pecl.php.net/package-changelog.php?package=APC&release=3.0.13). The APC optimizer got forked into a [separate project](http://pecl.php.net/package/optimizer) which never really took off.
 
 
Monitoring APC
 
--------------
 
Some of the aforementioned settings can be very specific to your setup and your application. So you need to take a look at what APC is doing. There are currently two ways of achieving this:
 
 
 - APC itself comes with a bundled `apc.php` which will tell you a bit about memory consumption, fragmentation and cache content. You'll most likely find it in `/usr/local/doc/apc`. Copy it to a location where your webserver can reach it and take appropriate measures to protect it from unauthorized access (open the php file and set the password in its first lines).
 
 - Available as an extension to your very own app comes the [apcinfo extension](http://yiiframework.com/extension/apcinfo) which does roughly the same job as `apc.php` but integrates nicely into your app.
 
 
Situation as of PHP v5.5
 
------------------------
 
The last officially released version of APC as of this writing is not compatible with PHP 5.5's ABI. A solution to this dilemma is fetching the tag [3.1.14](http://git.php.net/?p=pecl/caching/apc.git;a=commit;h=2b75460901c68a1e390b492f38f192ae2517b5d2) from git and building the APC extension from source. The alternative is to use the built-in [opcache](http://php.net/manual/book.opcache.php) in conjunction with the [APCu](http://pecl.php.net/package/APCu) extension
.
15 0
19 followers
Viewed: 48 979 times
Version: 1.1
Category: How-tos
Tags: apc, caching
Written by: Da:Sourcerer
Last updated by: Da:Sourcerer
Created on: Mar 5, 2012
Last updated: 10 years ago
Update Article

Revisions

View all history