Showing posts with label caching. Show all posts
Showing posts with label caching. Show all posts

Monday, February 27, 2012

PHP APC locking mechanism

The locking mechanisms that APC can currently use are:
  1. pthread locks
  2. pthread mutexes
  3. pthread locks
  4. pthread mutexes
  5. Slim reader/writer locks (Windows only)
  6. spinlocks
Stick to the default locking mechanism unless APC won't compile. Brian Shire at Facebook tested locking mechanism performance and presented the results.

As of APC 3.1.9, the PECL installer ask about enabling three additional options: memory protection, pthread read/write locks and pthread mutexes, which correspond to --enable-apc-memprotect, --enable-apc-pthreadrwlocks and --enable-apc-pthreadmutex. The first two are labeled experimental and disabled by default; the latter is enabled.
There are another two options.

1. enable-apc-filehits
2. enable-apc-spinlocks
The first option (--enable-apc-filehits) enables gathering information for the "filehits" option of apc_cache_info. Basically, you can use it to figure out which files are pulled from the cache for each request if you're debugging cache-related problems. If cache_type is "filehits", information about which files have been served from the bytecode cache for the current request will be returned. This feature must be enabled at compile time using --enable-filehits.
The second option (--enable-apc-spinlocks), spinlocks are a processor-cycle inefficient way of ensuring only one process accesses a resource at any given time. APC uses locks when dealing with shared memory. APC places the cache in shared memory so that all the PHP processes can, well, share the cache, and locks ensure that the processes don't trip over each other while doing it.

Another important setting is apc.shm_segments APC allows you to create multiple shared memory segments using the apc.shm_segments setting to get around shared memory segment size limits.The default is 1 segment, 32MB in size.
You should check the kernel max size for shared memory.

sysctl -a | grep shmmax

This could explain apache crash when you set a too big number (but you should have something about it in error.log). If you've configured APC to use memory mapped files (with the --enable-mmap option), the shm_segments setting is ignored unless you specify a file mask via the mmap_file_mask setting. Which could be fixed by giving a value to apc.mmap_file_mask configuration setting.
some versions of APC (3.0.14) seems to ignore the value of apc.shm_segments and go with the apc.shm_size, and also it accepts values larger than the maximum allowed size for each segment.
When you allocate more the 32M apache should hang. Below is some standard APC settings which will be useful.
  • apc.enabled=1
  • apc.shm_segments=3
  • apc.shm_size=32
  • apc.ttl=7200
  • apc.user_ttl=7200
  • apc.num_files_hint=2048
  • apc.mmap_file_mask=/tmp/apc.XXXXXX
  • apc.enable_cli=1
  • apc.max_file_size=10M