Slide 1

Slide 1 text

PHP OPCache, Realpath Cache and Preloading

Slide 2

Slide 2 text

Jachim Coudenys

Slide 3

Slide 3 text

PHP-WVL

Slide 4

Slide 4 text

Combell

Slide 5

Slide 5 text

OPCache

Slide 6

Slide 6 text

PHP Scripting Language Fire and forget No "manual" compilation

Slide 7

Slide 7 text

Compilation PHP Code Lexing + Parsing: Tokens Compiling: Opcodes Executing

Slide 8

Slide 8 text

https://engineering.facile.it/blog/eng/realpath-cache-is-it-all-php-opcache-s-fault/

Slide 9

Slide 9 text

Throwaway language Always performs same steps All information is discarded Request per request

Slide 10

Slide 10 text

OPCodes (usually) don't change

Slide 11

Slide 11 text

Add in some cache! PHP Code Lexing + Parsing: Tokens Compiling: Opcodes Cache: Opcodes Executing

Slide 12

Slide 12 text

Next run Cache: Opcodes Executing

Slide 13

Slide 13 text

And the next run Cache: Opcodes Executing

Slide 14

Slide 14 text

Even the next run Cache: Opcodes Executing

Slide 15

Slide 15 text

OPCache Since PHP 5.5 Zend Optimizer "donated" by Zend

Slide 16

Slide 16 text

Shared memory In computer hardware, shared memory refers to a (typically large) block of random access memory (RAM) that can be accessed by several different central processing units (CPUs) in a multiprocessor computer system.

Slide 17

Slide 17 text

PHP-FPM Only "relevant" SAPI nowadays :) Master process Dynamic worker child processes Shared memory in the master

Slide 18

Slide 18 text

Opcodes? In computing, an opcode (abbreviated from operation code) is the portion of a machine language instruction that specifies the operation to be performed.

Slide 19

Slide 19 text

Vulcan Logic Dumper https://3v4l.org/A1B4H/vld#output

Slide 20

Slide 20 text

This can get big quite quickly Libraries Frameworks

Slide 21

Slide 21 text

OPCache Optimizer Gets better every release Optimizes: Branches Dead code ++$a vs $a++

Slide 22

Slide 22 text

Interned strings Used in a lot of languages Since PHP 5.4 "Compression" for source code In shared memory (FPM master process)

Slide 23

Slide 23 text

opcache_compile_file() opcache_get_configuration() opcache_get_status() opcache_invalidate() opcache_is_script_cached() opcache_reset() php.net/manual/en/ ref.opcache.php

Slide 24

Slide 24 text

opcache.memory_consumption opcache.validate_timestamps opcache.revalidate_freq opcache.interned_strings_buffer opcache.max_accelerated_files (keys) opcache.max_wasted_percentage php.net/manual/en/ opcache.configuration.php

Slide 25

Slide 25 text

Keys Full path to file Relative paths to files

Slide 26

Slide 26 text

Wasted memory Opcache doesn't to "defragmentation" File changes cause recompilation opcache.max_wasted_percentage

Slide 27

Slide 27 text

Opcache restarts OOM Restarts Hash Restarts Manual Restarts

Slide 28

Slide 28 text

NEVER have a full cache!

Slide 29

Slide 29 text

Opcache Priming opcache_compile_file() FPM Pools

Slide 30

Slide 30 text

opcache.php demo

Slide 31

Slide 31 text

APCu User Shared memory apc_* compatible

Slide 32

Slide 32 text

Opcache File Cache Read opcodes from disk Can help busy sites Supports CLI (used to be "fire and forget") Cache to "user directory" http://talks.php.net/froscon15#/php7pcache1

Slide 33

Slide 33 text

opcache.file_cache=/var/tmp/php/opcache opcache.file_cache_only=1 # Useful for CLI opcache.file_cache_consistency_checks=1 # Adler checksum

Slide 34

Slide 34 text

Scenario's Memory Full? Interned Strings Full? Key store full?

Slide 35

Slide 35 text

Deployment Strategies It Depends ¯\_( )_/¯

Slide 36

Slide 36 text

Realpath Cache

Slide 37

Slide 37 text

Realpath Cache Used to reduce IO Caches all possible paths to destination path explode('/') No shared memory (per worker process)! Mem*Workers 4M (Prior to PHP 7.0.16 and 7.1.2, the default was "16K")

Slide 38

Slide 38 text

realpath_cache_get() realpath_cache_size () realpath() php.net/manual/en/ ref.filesystem.php

Slide 39

Slide 39 text

realpath.php demo

Slide 40

Slide 40 text

realpath_cache_size realpath_cache_ttl php.net/manual/en/ ini.core.php

Slide 41

Slide 41 text

Preloading

Slide 42

Slide 42 text

PHP has been using opcode caches for ages (APC, Turck MMCache, Zend OpCache). They achieve significant performance boost by ALMOST completely eliminating the overhead of PHP code recompilation. With an opcode cache, files are compiled once (on the first request that uses them), and are then stored in shared memory. All the following HTTP requests use the representation cached in shared memory.

Slide 43

Slide 43 text

Preloading PHP 7.4 Part of opcache Starts in master process before anything else Loads code in memory "permanently" No need to copy from shared memory to process memory

Slide 44

Slide 44 text

Preloading Opcache only works per file Preloading helps with class libraries Code will perform as internal entities (e.g. strlen, etc...) Simple file with autoloading magic

Slide 45

Slide 45 text

opcache.preload https://wiki.php.net/rfc/preload#proposal

Slide 46

Slide 46 text

PHP <= 7.3 PHP process starts up

Slide 47

Slide 47 text

PHP <= 7.3 First request: app does new A() autoloader include 'A.php'; PHP "compiles" A.php into opcodes and stores it in opcache opcodes are executed

Slide 48

Slide 48 text

PHP <= 7.3 Next requests: app does new A() autoloader include 'A.php'; A.php is already in opcache, no need to read the file and re-interpret the PHP opcodes are executed

Slide 49

Slide 49 text

PHP >= 7.4 + preloading PHP process starts up

Slide 50

Slide 50 text

PHP >= 7.4 + preloading preloading starts: preloader new A() preloader + PHP "compiles" A.php into opcodes and stores it in memory

Slide 51

Slide 51 text

PHP >= 7.4 + preloading First request: app does new A() opcodes are executed

Slide 52

Slide 52 text

PHP >= 7.4 + preloading Next requests: app does new A() opcodes are executed

Slide 53

Slide 53 text

Normal vs Preloading first request we save calls to the autoloader + compilation to opcode + storing to opcache next requests we save calls to the autoloader + fetching from opcache

Slide 54

Slide 54 text

https://externals.io/message/103333 https://wiki.php.net/rfc/preload https://github.com/php/php-src/pull/3538

Slide 55

Slide 55 text

PHPArch Opcache article https://www.phparch.com/article/diving-in-the- opcache/

Slide 56

Slide 56 text

Elastic Stack @ Combell Gather realpath/opcache/etc... data from all accounts Act upon that data (which is changing constantly) Autotuning

Slide 57

Slide 57 text

Thank you! https://joind.in/event/php-wvl-may-meetup-at-studio- emma-2019/php-opcache-and-preloading