APC PHP – Tuning apc.mmap_file_mask

alternative-php-cachePHPphp5

When configuring APC as an opcode cache for PHP, there's a configuration setting called apc.mmap_file_mask. From what I've read there are three ways you can configure it but I don't really understand the implications of each.

  1. /tmp/apc.XXXXXX – (default) "file-backed mmap"
  2. /tmp/apc.shm.XXXXXX – use a "POSIX-style shm_open/mmap"
  3. /dev/zero – "use your kernel's /dev/zero interface to anonymous mmap'ed memory"

Source: http://php.net/manual/en/apc.configuration.php#ini.apc.mmap-file-mask

Can anyone comment on these and what they'd recommend? I'm guessing there will be memory usage and performance ramifications and perhaps security ones, but I don't know if that's the case? From the reading I'm done I'm assuming #2 and #3 are faster, but I thought APC is already using shared memory as it is (as set by apc.shm_size), so I don't understand.

Best Answer

/tmp/apc.XXXXXX -> This mmap file mask is a normal filesystem based mmap and uses mkstemp to create a unique temporary file which is mmap'd. The 6 'X's are replaced by the unique string to make the filename unique. This is just writing data to a file in filesystem.

/tmp/apc.shm.XXXXXX -> Note that it_must_ be only /apc.shm.XXXXXX on linux systems. The difference from straight file-backed mmap is this mechanism creates a temporary file via mktemp() call and makes a call to shm_open() which creates and opens a new, or opens an existing, POSIX shared memory object. A POSIX shared memory object is in effect a handle which can be used by unrelated processes to mmap the same region of shared memory. I've not tried this before but I think it can have as minimum as 3 'X's (so apc.shml.XXX also should work).

/dev/zero -> mmap'ing /dev/zero is an anonymous memory mapping which means its the memory object having no associated file and all the contents initialized to zero. If you don't specify mmap_file_mask, the APC would use the anonymous map (with flags MAP_SHARED and MAP_ANON). Thus, specifying /dev/zero and not specifying mmap_file_mask are equivalent in the sense that both of them are anonymous map. Historically, MAP_SHARED and MAP_ANON together in linux had no support before kernel version 2.4.

Performance-wise, 3 will do the best since file-backed mmap'ing has considerably more disk I/O. So, 3 must be the fastest as it has no backed file and is part of actual memory itself, then 2 and 1 finally. However, this is only theory and practical benchmarks across various configuration can prove the reality. However, the disadvantage of anonymous mapping (& shared memory mapping) is that mmap'd memory is not persistent between the application executions thus loosing cache on restart.

Related Topic