Forum PHP Paris 2007: APC @ Facebook

37
Brian M. Shire Forum PHP Paris 2007 November 22nd, 10:45-11:45 am Facebook © 2007

Transcript of Forum PHP Paris 2007: APC @ Facebook

Page 1: Forum PHP Paris 2007: APC @ Facebook

Brian M. ShireForum PHP Paris 2007

November 22nd, 10:45-11:45 am

Facebook © 2007

Page 2: Forum PHP Paris 2007: APC @ Facebook

Facebook

We are a technology company

A social utility to communicate & share information

300+ employees

Offices: Palo Alto, New York, Chicago, Detroit, London

Over 50 million active users

Page 3: Forum PHP Paris 2007: APC @ Facebook

Alexa Graphs

Page 4: Forum PHP Paris 2007: APC @ Facebook

Alexa Graphs

Page 5: Forum PHP Paris 2007: APC @ Facebook

Alexa Graphs

Page 6: Forum PHP Paris 2007: APC @ Facebook

Customized source for Facebook’s requirements.

Global collaboration is beneficial.

“Own the stack”.

http://developers.facebook.com/opensource.php

Original Facebook releases: Thrift, PHP Shell and the Firefox Toolbar.

Open Source

Page 7: Forum PHP Paris 2007: APC @ Facebook

http://pecl.php.net/packages/APC/

Provides a significant PHP interpreter performance increase.

PHP variable cache can also be used for application specific benefits.

Open source allows collaboration, bug fixes and optimizations.

Alternative PHP Cache (APC)

George SchlossnagleDaniel CowgillRasmus LerdorfGopal VijayaraghavanEdin Kadribasic

“APC is a free, open, and robust framework for caching and optimizing PHP intermediate

code.”

APC Developers:

Ilia AlshanetskyMarcus BörgerSara GolemonBrian Shire

Page 8: Forum PHP Paris 2007: APC @ Facebook

Requests per second provide an overall measurement of performance gains.

Measurements made using Apache Bench (ab) with 1000 total requests, concurrency of 40.Executed on a dual Dual Core AMD Opteron 2.2Ghz, 8GB RAM.

The APC Advantage

Facebook

APC User & File

APC User

PHP 2.47 rps

5.24 rps (x2.12)

22.01rps (x8.91)

29.56 rps (x11.96)

Page 9: Forum PHP Paris 2007: APC @ Facebook

Start Request

Compilation:

Convert PHP source

into opcodes

Execute opcodes

End Request

APC hook Opcode

Cache

StoreAPC hook

Miss

Hit

Fetch Opcodes

Start Request

Compilation:

Convert PHP source

into opcodes

Execute opcodes

End Request

PHP APCPHP vs. APC

Page 10: Forum PHP Paris 2007: APC @ Facebook

1 <?php2 $output = ’Hello World!’;3 echo $output;4 ?>

1 ASSIGN !0 ‘Hello+World%21’2 ECHO !03 RETURN 14 ZEND_HANDLE_EXCEPTION

Opcode output generated with the Vulcan Logic Dissassembler (VLD)by Derick Rethans (http://pecl.php.net/packages/VLD/)

From Code to Opcode

PHP Source Opcodes

Page 11: Forum PHP Paris 2007: APC @ Facebook

1 <?php2 $output = ’Hello World’;3 if($_GET[‘exclaim’]) {4 $output .= ‘!’;5 } else {6 $output .= ‘.’;7 }8 echo $output;9 ?>

PHP Source

1 ASSIGN !0, ‘HELLO+WORLD’ 2 FETCH_R GLOBAL $1, ‘_GET’ 3 FETCH_DIM_R $2, $1, ‘exclaim’ 4 JMPZ $2, ->6 5 ASSIGN_CONCAT !0, ‘%21’ 6 JMP ->7 7 ASSIGN_CONCAT !0, ‘.’ 8 ECHO $0 9 RETURN 110 ZEND_HANDLE_EXCEPTION

Opcodes

Opcode output generated with the Vulcan Logic Dissassembler (VLD)by Derick Rethans (http://pecl.php.net/packages/VLD/)

From Code to Opcode

Page 12: Forum PHP Paris 2007: APC @ Facebook

phpinfo() displays configuration information

Verify configuration settings have taken place

Debug configuration problems

<? phpinfo(); ?>

Page 13: Forum PHP Paris 2007: APC @ Facebook

Enable APC when running via the command line

apc.mmap_file_mask NULL

Enable APC

apc.enabled_cli 0

apc.enable 0

Name of the file mask as specified by mmap

Basic Configuration Options

Page 14: Forum PHP Paris 2007: APC @ Facebook

apc.shm_segments 1

apc.shm_size 30

apc.num_files_hint 1000

apc.user_entries_hint 4096

One shared memory segment required

Size needs to hold all file and user entries

Monitor usage using apc.php

Extra space is required for deleted entries

Behaves poorly when memory is at capacity

Hints optimize hash lookup tables

Maximum number of files or user entries

@ Facebook:

apc.shm_segments=1apc.shm_size=648

@ Facebook:

apc.num_files_hint=100apc.user_entries_hint=640000

Cache Size and Hints

Page 15: Forum PHP Paris 2007: APC @ Facebook

APCOpcode Cache

Refresh Data View Host Stats System Cache Entries User Cache Entries Version Check

Login

General Cache Information

APC Version 3.0.15-dev

PHP Version 5.2.2-dev

APC Host shirebook.local

Server Software Apache/1.3.37 (Darwin) PHP/5.2.2-dev

Shared Memory 1 Segment(s) with 800.0 MBytes (mmap memory, file locking)

Start Time 2007/04/30 23:32:27

Uptime 3 minutes

File Upload Support 1

File Cache Information

Cached Files 2 (397.6 KBytes)

Hits 15

Misses 2

Request Rate (hits, misses) 0.08 cache requests/second

Hit Rate 0.07 cache requests/second

Miss Rate 0.01 cache requests/second

Insert Rate 0.01 cache requests/second

Cache full count 0

User Cache Information

Cached Variables 0 ( 0.0 Bytes)

Hits 0

Misses 0

Request Rate (hits, misses) 0.00 cache requests/second

Hit Rate 0.00 cache requests/second

Miss Rate 0.00 cache requests/second

Insert Rate 0.00 cache requests/second

Cache full count 0

Runtime Settings

apc.cache_by_default 1

apc.enable_cli 1

apc.enabled 1

apc.file_update_protection 0

apc.filters

apc.gc_ttl 3600

apc.include_once_override 0

apc.localcache 0

apc.localcache.size 512

apc.max_file_size 1M

apc.mmap_file_mask /tmp/apc.IXpNut

apc.num_files_hint 200

apc.report_autofilter 0

apc.rfc1867 0

apc.shm_segments 1

apc.shm_size 800

apc.slam_defense 0

apc.stat 1

apc.stat_ctime 0

apc.ttl 7500

apc.user_entries_hint 162000

apc.user_ttl 7500

apc.write_lock 1

Host Status Diagrams

Memory Usage Hits & Misses

Free: 798.3 MBytes (99.8%) Hits: 15 (88.2%)

Used: 1.7 MBytes (0.2%) Misses: 2 (11.8%)

Detailed Memory Usage and Fragmentation

Fragmentation: 0%

apc.php is located in the APC source directory

User and file cache browser

Graphs of hit rates and memory usage

apc.php

Page 16: Forum PHP Paris 2007: APC @ Facebook

Uses file locking operations

Faster alternative to file locks

Stable, not effecient

Architecture specific, Linux Kernel 2.6.x or later

Significant performance gain

Better alternative to Linux Futex Locks

Same performance gain

More stable, more architecture support

Ported from the PostgreSQL project

Runs in user space

Locking type

File Locks

IPC Semaphore Locks

Linux Futex Locks

pthread mutex Locks

spin Locks

Currently Used @ Facebook

EXPERIMENTAL

Future Default (apc-3.1.0)

EXPERIMENTAL

Default

Locking Mechanisms

--enable-apc-sem

--enable-apc-futex

--enable-apc-pthreadmutex

--enable-apc-spinlocks

A process localized cacheapc.localcache 0

apc.localcache_size 512

Page 17: Forum PHP Paris 2007: APC @ Facebook

0

50

100

File

0

50

100

IPC Sem

0

50

100

Futex

0

50

100

Pthread

Spin

0 10 15 20 25 30 60 120 180

0

50

100

Seconds

Perc

ent

User CPU

System CPU

Locking Performance (Overlayed)

Page 18: Forum PHP Paris 2007: APC @ Facebook

APC stats files to determine if they’ve been updatedStart Request

Compilation:

Convert PHP source

into opcodes

Execute opcodes

End Request

APC hook Opcode

Cache

StoreAPC hook

Miss

Hit

apc.stat_ctime FALSE

apc.stat TRUE

stat()Disabling updates will increase performance

Requires restart or apc_cache_clear() to update

CVS, SVN and rsync backdate modified times

stat_ctime checks the creation time for updates

@ Facebook:apc.stat=FALSE

@ Facebook:apc.stat_ctime=FALSE

To Stat or Not to Stat?

Optimizes include_once() calls(not recommended)

apc.include_once_override 0

EXPERIMENTAL

Page 19: Forum PHP Paris 2007: APC @ Facebook

Inline garbage collector removes deleted entries from cache as soon as possible.

apc.file_update_protection 2

Spreads load on startup by only caching given percent of requests

A non-blocking write lock provides a better solution to setting slam_defense

Read updated files after given delay to prevent loading incomplete files

“Time to Live”

Entries still in use by a request will be removed after the gc_ttl has expired.

Maximum time a cache entry can remain in cache.

apc.ttl 0

apc.user_ttl 0

apc.gc_ttl 3600

apc.write_lock 1

apc.slam_defense 0

Slam Defenses & TTL

Page 20: Forum PHP Paris 2007: APC @ Facebook

Limits the maximum file size that will be cached.

A regular expression that excludes files from being cached.

apc.cache_by_default 1 Setting to zero causes files to only cache if they match apc.filters.

apc.report_autofilter 0

apc.filters NULL

apc.max_file_size 1M

Logs files excluded due to early/late binding issues

Filters

No longer necessary in latest releases, files will not be excluded.

Page 21: Forum PHP Paris 2007: APC @ Facebook

rfc1867

Upload progress supportapc.rfc1867 On

apc.rfc1867_freq 0

apc.rfc1867_name APC_UPLOAD_PROGRESS

apc.rfc1867_prefix upload_

Update frequency in percent of file size or per byte count

Hidden form input name for apc user variable key

Prefix for the apc user variable key

<form> <input type=”hidden” name=”APC_UPLOAD_PROGRESS” value=”A86DCF0C”> ... </form>

Produces an APC user cache entry “upload_A86DCF0C” containing upload statistics.

The HTML Form:

Page 22: Forum PHP Paris 2007: APC @ Facebook

boolean apc_add(string key, mixed value, int ttl)

http://us.php.net/manual/en/ref.apc.php

array apc_cache_info(string cache, boolean limited)

boolean apc_clear_cache(string cache)

array apc_sma_info(boolean limited)

boolean apc_store(string key, mixed value, int ttl)

mixed apc_fetch(string key)

boolean apc_delete(string key)

boolean apc_compile_file(string file)

boolean apc_define_constants(string key, array constants bool case_sensitive)boolean apc_load_constants(string key, bool case_sensitive)

Cache information

Shared memory segment information.

Store key/value pair in cache.

Add key/value pair if key isn’t in cache.

Fetch the value associated with key.

Delete the value associated with key.

Clear all entries from the cache.

Define an array of key/value constants.

Assign each key/value constants in cache using define()

Compile given file and store in cache, bypass all filters.

API

Page 23: Forum PHP Paris 2007: APC @ Facebook

User cache stores PHP variables across multiples requests on a per server basis.

boolean apc_store(string key, mixed value)

mixed apc_fetch(string key)

Primary commands for utilizing the user cache:

Optimize...

Application configuration

Statistics such as site usage, request types, error conditions, timing

Nth tier cache in addition to memcache or other caching service

HTML or other output

Database backed values that only change rarely like product listings

Site behavior like new features, A/B tests, or rate controls

The User Variable Cache

boolean apc_add(string key, mixed value)

Page 24: Forum PHP Paris 2007: APC @ Facebook

APC + SVN Client Cache

function get_static_suffix($file) { global $ROOT; if ($version = cache_get($file, ‘sv’, 1) === null) { $version = trim(shell_exec(“svn info $ROOT/$file | grep ‘Changed Rev’ | cut -c 19-”));

apc_store(“sv:$file”, $version); } return '?'.$version;}

User cache used to create version suffixes for static content (.js, .css, ...)

Page 25: Forum PHP Paris 2007: APC @ Facebook

APC + Useragent Strings

Useragent string parsing is inefficient in PHP

Cache parsed useragents in APC for the first 10 minutes

Hit rate of over 50%

Pear implementation available soon:

‣ PEAR::Net_Useragent_Detect_APC

Page 26: Forum PHP Paris 2007: APC @ Facebook

Facebook controls site configuration and features via the user cache.

Datacenter 'B

web Servers:

Datacenter 'A'

Web Servers:

apc_sitevar.php

Spawns requests to multiple web servers

The site updates in the time it takes to make HTTP requests.

Updates are simple and fast for the Engineer.

Engineers Users

Controlling Site Behavior with "Sitevars"

Site-wide Server Variables: “Sitevars”

Page 27: Forum PHP Paris 2007: APC @ Facebook

Facebook primes it’s cache before each server restart.

Ready to serve requests without delay due to compilation or updating cache values.

Handles immediate flood of requests with limited warm-up time.

Starts with same cache state limiting differences between servers.

boolean apc_store(string key, mixed value)

boolean apc_compile_file(string file)

Optimus Prime

Apache is restarted for major code pushes.

Page 28: Forum PHP Paris 2007: APC @ Facebook

Serialize Cache Values

Stop Apache

Disallow connections via

iptables or load balancer

Deserialize and apc_store()

values.

Start Apache

apc_compile_file()

PHP source

Allow connections via

iptables or load balancer

Serialized User

Cache Values

Serialized User

Cache Values

Distributed to Web Servers

Master Restart Script

Single Control Server Multiple Web Servers

System

Apache

PHP & APC

Local Disk

APC Priming and Restart

Page 29: Forum PHP Paris 2007: APC @ Facebook

APC Binary Dump (patch)

An experimental binary dump patch is now available!

Allows the abilty to copy the APC memory cache to disk, and reload at a later time.

Faster priming and restarts (it was taking too long at Facebook).

Ability to load pre-compiled files into your cache.

Ability to load and run code without touching disk or keeping source code on file.(Note: for other ways of possibly keeping your source code safe see ap_source_defense at http://tekrat.com/ap_source_defense.)

Doesn’t currently support apc.stat=1 mode.

Will be available after the talk at: http://tekrat.com/patches/ http://mirror.facebook.com/facebook/patches/

Page 30: Forum PHP Paris 2007: APC @ Facebook

Prime APC Cache and

apc_bindump_file("gzip://...")

Stop Apache

Disallow connections via

iptables or load balancer

apc_binload_file("gzip://...")

Start Apache

Allow connections via

iptables or load balancer

Binary File Binary FileDistributed to Web Servers

Master Restart Script

Single Control Server Multiple Web Servers

System

Apache

PHP & APC

Local Disk

APC Binary Priming and Restart

Page 31: Forum PHP Paris 2007: APC @ Facebook

You don’t always want to restart apache to push new code when apc.stat=0.

Causes longer code pushes.

Can interrupt user requests especially file uploads.

Instead use the apc_compile_file(string file) function to programatically update changed files.

Pushing code without Apache Restart

Only recommended for small code changes.

Requires 2X memory for each changed file until previous requests have been served.

Only available after APC version 3.0.15.

Page 32: Forum PHP Paris 2007: APC @ Facebook

File Hits (cvs 3.1.0-dev)

Currently available only in CVS (apc-3.1.0-dev)

Compile time configuration: ./configure --enable-apc-filehits

<? var_dump(apc_cache_info(‘filehits’)); ?>

Keeps an internal list of files that came from the APC cache for the current request.

Useful in development environments for debugging and ensuring cache is working as expected.

array(1) { [0]=> string(25) “/Users/shire/www/test.php”}

array(0) {}

First request:

Subsequent requests:

Page 33: Forum PHP Paris 2007: APC @ Facebook

Future Work...

Least Frequently Used (LFU) list for expunging full caches.

Multiple memory segments with independent configurations.

Testing and compatibility with Ilia’s optimizer (currently under development with Facebook).

Reliable spin locking.

Page 34: Forum PHP Paris 2007: APC @ Facebook

Installation via “pecl install apc”or source http://pecl.php.net/packages/APC/

Start with a basic apc.ini file:

apc.enabled=1apc.shm_size=100M

Install apc.php under the DocumentRoot, configure the USER and PASS variablesMonitor apc.php for usage and adjust configuration

Try tuning some of the discussed settings to meet application needs

Add some APC user variables

Try different locking types

Try the apc.stat=0 setting

Measure changes in CPU usage and maximum requests per second

Getting Started

Page 35: Forum PHP Paris 2007: APC @ Facebook

Recommendations...

Try APC before making low level optimizations.

I appreciate feedback about APC, patches, this talk, and anything else! <[email protected]>

Try out some patches...Available at http://tekrat.com with mirrors at http://mirror.facebook.com/facebook/patches

ap_source_defense

apc_bindump

php_ini_includes

ap_UnsetErrorDocument

php_mysql_t

Helps defend source code from being displayed in Apache.

Create binary dumps of your cache.

Allow PHP INI includes.

Unset the Apache ErrorDocument directive.

Automatically cast mysql return values.

More implementation details available in Lucas Nealan's “Facebook Performance Caching”http://sizzo.org/wp/talks/

Page 36: Forum PHP Paris 2007: APC @ Facebook

Any Questions?

Q & A

Merci!

Page 37: Forum PHP Paris 2007: APC @ Facebook