lord-inflight

22
Jun
2011

APC

Uncategorized

No, sorry, this post isn’t about the armoured vehicles. It’s about the PHP cache type of APC.

I use it for my collection of sites, like this one, to help speed things up. Cheap server, needs all the help it can get. ;)

Anyhow, a while back, I wanted it to do a few things it couldn’t, so I grabbed the source and made some changes. I sent them to the first e-mail on their developers list, but I guess they weren’t interested since I never got a reply, and it didn’t show up in their SVN changelogs. So, I’m posting them here just in case anyone else wants them. (p.s. if the APC guys happen to read this, you’re still welcome to add them!)

Basically, it started out with my wanting to use /dev/zram1 (it’s a LZO-compressed RAM block device, from the compcache project) as a backing for APC. It can’t do this normally, it expects a directory where it can make a temporary file or the special-cased /dev/zero. So my first patch was a very simple one: let it open any file in /dev directly. This could also be used for people with a special partition for their APC cache or some other sort of block device they want to use.
Download: opendev.patch
Configure flag: None. It doesn’t do any harm if you don’t use it, so it’s always enabled.

With that working, I also thought it would be really handy if it were to zero out any parts of memory it deallocated, so that zram could recognize the page as unused and free the memory.
Download: zerodealloc.patch
Configure flag: –enable-apc-zerodealloc

Also, I kinda get annoyed with the amount of fragmentation APC generates after a while. So my third patch was to have it allocate in blocks, so that when something is allocated then freed, at least it’s a nice regular size that something else can probably fit. This also goes really well with zram compression, since the slack space hardly takes any extra memory, being all zeroes, and having everything aligned helps too. The configure script sets the blocks to your RAM page size by default (4096 bytes on x86-type hardware) but you can set it manually with the configure option. (If your OS doesn’t have getpagesize(), you’ll have to do this whether you want the default or not.)
Download: pagesize.patch
Configure flag: –enable-apc-page[=size]

Hopefully the patches still apply to the latest versions, they were made a while ago. If not, please let me know in the comments and I’ll make new ones.

I’ve been using all three for a while without any crashes or mysterious corruption, but I still recommend testing them before any production use. Especially if you’re on something other than 32-bit x86 hardware… the more different it is, the more likely it is to break something. :)

This entry was posted on Wednesday, June 22nd, 2011 at 8:25 PM and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

To deter spammers, your browser is required to perform some mathematical work before your comment can be posted.
The "Submit" button will be available once this is complete.
The time this takes is random, but should only be a few seconds.