SlideShare a Scribd company logo
What's up with OPCache ?
PHP 7 OPCode Cache tour
Hello everybody
 Julien PAULI
 Programming in PHP since ~20y (~1999-2000)
 Programming in C for PHP Internals
 PHP 5.5 and 5.6 Release Manager
 Working at SensioLabs in Paris
 http://www.phpinternalsbook.com co-author
 @julienpauli - http://jpauli.tech - jpauli@php.net
What we'll cover together
 Reminder on how PHP works
 Introduction to OPCodes
 The need of an OPCode cache
 OPCache in deep
 OPCache configuration settings
What is PHP ?
 PHP
 Programming language
 "Scripting language"
 No *manual* compilation needed
 Fire and forget
 Automatic memory management
 Facultative strong typing. Type juggler
 Provides OOP features
 Highly dynamic, highly extensible
How does PHP work ?
Parsing
(Lexer + parser)
Compiling
Executing
opcodes
PHP code
AST nodes
Result
Zend Virtual Machine
Parsing
(Lexer + parser)
Compiling
Executing
 "VirtualMachine"
 Its purpose is to provide a platform-
independent programming
environment that abstracts away
details of the underlying hardware or
operating system, and allows a
program to execute in the same way
on any platform (Wikipedia)
The PHP way
 Compile, execute, forget - Compile, execute, forget -
Compile, execute, forget - Compile, execute, forget -
Compile, execute, forget - Compile, execute, forget -
Compile, execute, forget - Compile, execute, forget …
 By default (by design), PHP discards all the code it just
executed
 Request n+1 knows nothing about request n
The PHP Way
 If your pages get hit several times without changing (most
likely)
 → compile the same scripts several times
 → it still produces the same OPCodes
Parsing
(Lexer + parser)
Compiling
Compiling VS Executing
 Compile, then execute
 Which one is the longest ?
 Depends on your scripts
Compile
Execute
Execute
Compile
Compiling VS Executing
 Compilation can really take time
 Usually less than execution, but...
Understanding compil. / exec.
 Classes usually require much more compile time than exec
time
 include / require / eval() = compile + execute
 autoload = compile + execute
 .. deffered at execution
Compile Execute
include/require ?
eval() ?
autoload ?
B
N
Y
E
OPCode cache roles
Parsing
(Lexer + parser)
Compiling
Executing
opcodes
PHP code
AST nodes
Result
Caching
opcodes
Save at first run
Compiling
Executing
opcodes
AST nodes
Caching
opcodesShared
Memory
opcodes
save
time
File
System
OR
Load (same script) after
OPCode cache
Executing
opcodes
PHP code
opcodes
load
Shared
Memory
File
System
OR
Optimize
 Why not optimize the OPCode array ?
opcodes
Optimizing
opcodes
opcodes
Caching
Compiling
save
Executing
opcodes
Shared
Memory
File
System
OR
PHP OPCache
OPCodes explained
OPCode
 In computer science, an opcode (operation code) is the
portion of a machine language instruction that specifies the
operation to be performed. (Wikipedia)
 Opcodes can also be found in so called byte codes and
other representations intended for a software interpreter
rather than a hardware device. (Wikipedia)
Zend OPCodes
Example of OPCode
<?php
const DEF = "default";
if (isset($argv[1])) {
$b = (array)$argv[1];
} else {
$b = DEF;
}
var_dump($b);
Example of OPCode
<?php
const DEF = "default";
if (isset($argv[1])) {
$b = (array)$argv[1];
} else {
$b = DEF;
}
var_dump($b);
L0 (3): DECLARE_CONST string("DEF") string("default")
L1 (5): T2 = ISSET_ISEMPTY_DIM_OBJ (isset) CV0($argv) int(1)
L2 (5): JMPZ T2 L7
L3 (6): T3 = FETCH_DIM_R CV0($argv) int(1)
L4 (6): T4 = CAST (array) T3
L5 (6): ASSIGN CV1($b) T4
L6 (6): JMP L9
L7 (8): T6 = FETCH_CONSTANT (unqualified) string("DEF")
L8 (8): ASSIGN CV1($b) T6
L9 (11): INIT_FCALL 1 96 string("var_dump")
L10 (11): SEND_VAR CV1($b) 1
L11 (11): DO_ICALL
L12 (12): RETURN int(1)
3
5
6
7
8
11
Example of OPCode
<?php
const DEF = "default";
if (isset($argv[1])) {
$b = (array)$argv[1];
} else {
$b = DEF;
}
var_dump($b);
Execution
Compilation
 The more code to parse, the longer the compilation phase
 The more OPCodes generated, the longer the execution
 Depends on OPCodes, some are long to run, some are very fast
DECLARE_CONST
ISSET_ISEMPTY_DIM_OBJ
JMPZ
FETCH_DIM_R
CAST
ASSIGN
JMP
FETCH_CONSTANT
ASSIGN
INIT_FCALL
SEND_VAR
DO_ICALL
RETURN
Optimize execution
 Some OPCodes are heavier than
others
 OPCodes may be run several
times (in fact, yes they do)
 The optimizer can play a role
Optimizing
Caching
Compiling
Executing
OPCache
 "OPCache" is PHP OPCode cache
 It is both a cache, and an optimizer
 It is bundled by defaut with PHP
 But it may not be activated by default
Optimizer
 Tries to simplify/optimize the OPCodes so that there are
less of them and they are more efficient
 Works on code branches (if, switch, try …)
 Optimize constant expressions - Look for dead code / code reuse
Try the optimizer
php 
-dzend_extension=opcache 
-dopcache.enable_cli=1 
-dopcache.opt_debug_level=0x30000 
-dopcache.optimization_level=0x7FFFFFFF 
/tmp/php.php
Optimizer impact
 The optimizer is very heavy, and burns tons of CPU cycles
 At compilation
 But compilation only happens once, as opcodes will be
cached
 So they'd better be cached optimized, than raw
 The very first run of a big application should pay a big perf
penalty
 Prime your caches
 Use OPCache file-based cache
Interned strings optimized
 In computer science, string interning is a method of storing
only one copy of each distinct string value, which must be
immutable. Interning strings makes some string processing
tasks more time- or space-efficient at the cost of requiring
more time when the string is created or interned. The
distinct values are stored in a string intern pool
Wikipedia
Interned strings concept
 Anytime a static string occurs in compiler, memorize it,
and reuse its pointer when needed.
 OPCache optimizes them even more
class foo
{
public function bar($a = "foo")
{
}
}
$foo = new foo;
$foo->bar("foo");
No Interned strings
class foo
{
public function bar($a = "foo")
{
}
}
$foo = new foo;
$foo->bar("foo");
foo bar a foo foo foo foo bar foo
memory
Interned strings
class foo
{
public function bar($a = "foo")
{
}
}
$foo = new foo;
$foo->bar("foo");
foo bar a
memory
OPCache & interned strings
 OPCache interns strings from all PHP processes of the same
PHP pool
 Nice memory savings on big pools
interned string mem interned string mem interned string mem
interned string mem
Tune your OPCache string buffer
php -dzend_extension=opcache 
-dopcache.memory_consumption=1024 
-dopcache.interned_strings_buffer=64 
/tmp/foo.php
Mb
Comments are interned strings
 How many interned string bytes for this script ?
<?php
/**
* @param $a string
*/
function foo(string $a)
{
}
PHPDoc comments are interned strings
 How many interned string bytes for this script ?
 opcache.save_comments = 0 disables saving of PHPDoc
comments to shared memory
<?php
/**
* @param $a string
*/
function foo(string $a)
{
}
28 + 3 + 1 + {garbage}
0 ending strings in C + some alignment bytes
PHP OPCache
OPCache API, settings and tuning
OPCache settings
 memory_consumption
 Size of Shared Memory
 Don't forget to increase according to your needs
 Buy more RAM, NEVER have a cache full, have margin
 max_accelerated_files (=2000)
 Max number of KEYS to be stored
 KEYS are paths to files : ./foo.php, ../foo.php, etc...
 One file usually is ~= 2 to 3 KEYS
 Aligned at next prime number
 Slots are preallocated : don't give too high number
 Monitor
OPCache explained
 memory_consumption=128M
 max_accelerated_files = 3000
 Next prime : 3907
 Cached files : 1878
 Cached keys : 2722 / 3907
OPCache settings
 validate_timestamps (=true)
 Check for file update to invalidate it
 Checks are triggered every revalidate_freq seconds
 revalidate_freq (=2)
 How often check for file updates to invalidate them
 0 = every time
 Revalidate = syscall cached by PHP's realpath cache
 revalidate_paths (=0)
 1 : only the resolved realpath is used as cache key
 0 : the unresolved path is also added as a cache key
 use_cwd (=1)
 prepend cwd to every cache key for relative paths
OPCache memory details
 When a script changes, it is
recompiled (if validate_timestamps
= 1)
 Its old memory space is then
considered as "wasted" (it is then
NOT freed)
 When cache is full, if max_wasted_percentage is reached :
a cache restart is triggered
 Cache restart = empty cache and recompile all
 Cache is never restarted if not full
OPcache other settings
 optimization_level (=0x7fffffff)
 Enable/disable some optimizations.
 enable_file_override (=0)
 Redefines file_exists(), is_readable() and is_file() to look into cache
before : recommanded
 blacklist_filename (=NULL)
 Prevents caching some files you decide
 consistency_checks (=0)
 Computes a checksum at each file fetch. Not recommanded, ~5-
8% slowdown
 opcache.save_comments (=1)
 Saves PHPDoc comments to shared memory
OPcache other settings
 enable_cli (=0)
 Enables opcache in CLI mode
 Long living apps like PHPUnit and COmposer mat benefit from the
optimizer and the cache
 Other short scripts will pay an impact against the optimizer : don't
enable
 huge_code_pages (=0)
 Moves the PHP process code memory segment to huge pages
 On some specific OS, with a mainly-statically-linked PHP, that can have
a beneficial impact
 Check /proc/meminfo to activate HugePage before usage
 protect_memory (=0)
 mprotect() the SHM against writes. Useless until you find a bug, crash,
and want to debug it
Know what happens
 error_log (=stderr)
 File to write log to
 log_verbosity_level (=1)
 0=fatals … 4=debug
OPCache do's and don't's
 Prime your cache smoothly
 https://github.com/engineyard/ey-php-performance-tools
 Prevent cache stampede
 Have enough shared memory
 Size your hashtables correctly
 Try not to generate php files on runtime
 Prevent highly modified files from beeing cached
 Use blacklists
 Every cache action will lock the shared memory
 And PHP Engine is not threaded
Thank you for listening

More Related Content

What's hot

PHP Internals and Virtual Machine
PHP Internals and Virtual MachinePHP Internals and Virtual Machine
PHP Internals and Virtual Machine
julien pauli
 
PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
julien pauli
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life CycleXinchen Hui
 
How PHP Works ?
How PHP Works ?How PHP Works ?
How PHP Works ?
Ravi Raj
 
Understanding PHP memory
Understanding PHP memoryUnderstanding PHP memory
Understanding PHP memoryjulien pauli
 
PHP 7 new engine
PHP 7 new enginePHP 7 new engine
PHP 7 new engine
julien pauli
 
PHP 7 performances from PHP 5
PHP 7 performances from PHP 5PHP 7 performances from PHP 5
PHP 7 performances from PHP 5
julien pauli
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extension
julien pauli
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13julien pauli
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
Patrick Allaert
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objectsjulien pauli
 
PHP5.5 is Here
PHP5.5 is HerePHP5.5 is Here
PHP5.5 is Here
julien pauli
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Moriyoshi Koizumi
 
Php’s guts
Php’s gutsPhp’s guts
Php’s guts
Elizabeth Smith
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
Elizabeth Smith
 
How PHP works
How PHP works How PHP works
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
Wim Godden
 
The why and how of moving to php 5.4
The why and how of moving to php 5.4The why and how of moving to php 5.4
The why and how of moving to php 5.4
Wim Godden
 
Streams, sockets and filters oh my!
Streams, sockets and filters oh my!Streams, sockets and filters oh my!
Streams, sockets and filters oh my!
Elizabeth Smith
 

What's hot (20)

PHP Internals and Virtual Machine
PHP Internals and Virtual MachinePHP Internals and Virtual Machine
PHP Internals and Virtual Machine
 
PHP7 is coming
PHP7 is comingPHP7 is coming
PHP7 is coming
 
The Php Life Cycle
The Php Life CycleThe Php Life Cycle
The Php Life Cycle
 
Building Custom PHP Extensions
Building Custom PHP ExtensionsBuilding Custom PHP Extensions
Building Custom PHP Extensions
 
How PHP Works ?
How PHP Works ?How PHP Works ?
How PHP Works ?
 
Understanding PHP memory
Understanding PHP memoryUnderstanding PHP memory
Understanding PHP memory
 
PHP 7 new engine
PHP 7 new enginePHP 7 new engine
PHP 7 new engine
 
PHP 7 performances from PHP 5
PHP 7 performances from PHP 5PHP 7 performances from PHP 5
PHP 7 performances from PHP 5
 
Mysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extensionMysqlnd, an unknown powerful PHP extension
Mysqlnd, an unknown powerful PHP extension
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
 
Create your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 VeronaCreate your own PHP extension, step by step - phpDay 2012 Verona
Create your own PHP extension, step by step - phpDay 2012 Verona
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
 
PHP5.5 is Here
PHP5.5 is HerePHP5.5 is Here
PHP5.5 is Here
 
Phpをいじり倒す10の方法
Phpをいじり倒す10の方法Phpをいじり倒す10の方法
Phpをいじり倒す10の方法
 
Php’s guts
Php’s gutsPhp’s guts
Php’s guts
 
Hacking with hhvm
Hacking with hhvmHacking with hhvm
Hacking with hhvm
 
How PHP works
How PHP works How PHP works
How PHP works
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
 
The why and how of moving to php 5.4
The why and how of moving to php 5.4The why and how of moving to php 5.4
The why and how of moving to php 5.4
 
Streams, sockets and filters oh my!
Streams, sockets and filters oh my!Streams, sockets and filters oh my!
Streams, sockets and filters oh my!
 

Similar to PHP 7 OPCache extension review

PHP & Performance
PHP & PerformancePHP & Performance
PHP & Performance
毅 吕
 
php & performance
 php & performance php & performance
php & performance
simon8410
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPtutorialsruby
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPtutorialsruby
 
PHP ITCS 323
PHP ITCS 323PHP ITCS 323
PHP ITCS 323
Sleepy Head
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
Vibrant Technologies & Computers
 
Basics PHP
Basics PHPBasics PHP
Caching with Memcached and APC
Caching with Memcached and APCCaching with Memcached and APC
Caching with Memcached and APC
Ben Ramsey
 
Tips
TipsTips
Tipsmclee
 
Php mysql
Php mysqlPhp mysql
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
SHARANBAJWA
 
build your own php extension
build your own php extensionbuild your own php extension
build your own php extensionhazzaz
 
PHP
PHPPHP
Php through the eyes of a hoster confoo
Php through the eyes of a hoster confooPhp through the eyes of a hoster confoo
Php through the eyes of a hoster confoo
Combell NV
 
Apc presentation
Apc presentationApc presentation
Apc presentation
guestef8544
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
anshkhurana01
 

Similar to PHP 7 OPCache extension review (20)

PHP & Performance
PHP & PerformancePHP & Performance
PHP & Performance
 
php & performance
 php & performance php & performance
php & performance
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHP
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHP
 
PHP ITCS 323
PHP ITCS 323PHP ITCS 323
PHP ITCS 323
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Caching with Memcached and APC
Caching with Memcached and APCCaching with Memcached and APC
Caching with Memcached and APC
 
Tips
TipsTips
Tips
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Php basics
Php basicsPhp basics
Php basics
 
Php ppt
Php pptPhp ppt
Php ppt
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
build your own php extension
build your own php extensionbuild your own php extension
build your own php extension
 
Php Ppt
Php PptPhp Ppt
Php Ppt
 
PHP
PHPPHP
PHP
 
Php through the eyes of a hoster confoo
Php through the eyes of a hoster confooPhp through the eyes of a hoster confoo
Php through the eyes of a hoster confoo
 
Php
PhpPhp
Php
 
Apc presentation
Apc presentationApc presentation
Apc presentation
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
 

More from julien pauli

Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019
julien pauli
 
Basics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNGBasics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNG
julien pauli
 
Mastering your home network - Do It Yourself
Mastering your home network - Do It YourselfMastering your home network - Do It Yourself
Mastering your home network - Do It Yourself
julien pauli
 
Tcpip
TcpipTcpip
Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)
julien pauli
 
Communications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHPCommunications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHPjulien pauli
 
PHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_ExtensionsPHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_Extensionsjulien pauli
 
PHPTour 2011 - PHP5.4
PHPTour 2011 - PHP5.4PHPTour 2011 - PHP5.4
PHPTour 2011 - PHP5.4julien pauli
 
Patterns and OOP in PHP
Patterns and OOP in PHPPatterns and OOP in PHP
Patterns and OOP in PHPjulien pauli
 
ZendFramework2 - Présentation
ZendFramework2 - PrésentationZendFramework2 - Présentation
ZendFramework2 - Présentation
julien pauli
 
AlterWay SolutionsLinux Outils Industrialisation PHP
AlterWay SolutionsLinux Outils Industrialisation PHPAlterWay SolutionsLinux Outils Industrialisation PHP
AlterWay SolutionsLinux Outils Industrialisation PHP
julien pauli
 
Apache for développeurs PHP
Apache for développeurs PHPApache for développeurs PHP
Apache for développeurs PHP
julien pauli
 

More from julien pauli (13)

Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019Doctrine with Symfony - SymfonyCon 2019
Doctrine with Symfony - SymfonyCon 2019
 
Dns
DnsDns
Dns
 
Basics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNGBasics of Cryptography - Stream ciphers and PRNG
Basics of Cryptography - Stream ciphers and PRNG
 
Mastering your home network - Do It Yourself
Mastering your home network - Do It YourselfMastering your home network - Do It Yourself
Mastering your home network - Do It Yourself
 
Tcpip
TcpipTcpip
Tcpip
 
Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)Php in 2013 (Web-5 2013 conference)
Php in 2013 (Web-5 2013 conference)
 
Communications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHPCommunications Réseaux et HTTP avec PHP
Communications Réseaux et HTTP avec PHP
 
PHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_ExtensionsPHPTour-2011-PHP_Extensions
PHPTour-2011-PHP_Extensions
 
PHPTour 2011 - PHP5.4
PHPTour 2011 - PHP5.4PHPTour 2011 - PHP5.4
PHPTour 2011 - PHP5.4
 
Patterns and OOP in PHP
Patterns and OOP in PHPPatterns and OOP in PHP
Patterns and OOP in PHP
 
ZendFramework2 - Présentation
ZendFramework2 - PrésentationZendFramework2 - Présentation
ZendFramework2 - Présentation
 
AlterWay SolutionsLinux Outils Industrialisation PHP
AlterWay SolutionsLinux Outils Industrialisation PHPAlterWay SolutionsLinux Outils Industrialisation PHP
AlterWay SolutionsLinux Outils Industrialisation PHP
 
Apache for développeurs PHP
Apache for développeurs PHPApache for développeurs PHP
Apache for développeurs PHP
 

Recently uploaded

Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 

Recently uploaded (20)

Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 

PHP 7 OPCache extension review

  • 1. What's up with OPCache ? PHP 7 OPCode Cache tour
  • 2. Hello everybody  Julien PAULI  Programming in PHP since ~20y (~1999-2000)  Programming in C for PHP Internals  PHP 5.5 and 5.6 Release Manager  Working at SensioLabs in Paris  http://www.phpinternalsbook.com co-author  @julienpauli - http://jpauli.tech - jpauli@php.net
  • 3. What we'll cover together  Reminder on how PHP works  Introduction to OPCodes  The need of an OPCode cache  OPCache in deep  OPCache configuration settings
  • 4. What is PHP ?  PHP  Programming language  "Scripting language"  No *manual* compilation needed  Fire and forget  Automatic memory management  Facultative strong typing. Type juggler  Provides OOP features  Highly dynamic, highly extensible
  • 5. How does PHP work ? Parsing (Lexer + parser) Compiling Executing opcodes PHP code AST nodes Result
  • 6. Zend Virtual Machine Parsing (Lexer + parser) Compiling Executing  "VirtualMachine"  Its purpose is to provide a platform- independent programming environment that abstracts away details of the underlying hardware or operating system, and allows a program to execute in the same way on any platform (Wikipedia)
  • 7. The PHP way  Compile, execute, forget - Compile, execute, forget - Compile, execute, forget - Compile, execute, forget - Compile, execute, forget - Compile, execute, forget - Compile, execute, forget - Compile, execute, forget …  By default (by design), PHP discards all the code it just executed  Request n+1 knows nothing about request n
  • 8. The PHP Way  If your pages get hit several times without changing (most likely)  → compile the same scripts several times  → it still produces the same OPCodes Parsing (Lexer + parser) Compiling
  • 9. Compiling VS Executing  Compile, then execute  Which one is the longest ?  Depends on your scripts Compile Execute Execute Compile
  • 10. Compiling VS Executing  Compilation can really take time  Usually less than execution, but...
  • 11. Understanding compil. / exec.  Classes usually require much more compile time than exec time  include / require / eval() = compile + execute  autoload = compile + execute  .. deffered at execution Compile Execute include/require ? eval() ? autoload ? B N Y E
  • 12. OPCode cache roles Parsing (Lexer + parser) Compiling Executing opcodes PHP code AST nodes Result Caching opcodes
  • 13. Save at first run Compiling Executing opcodes AST nodes Caching opcodesShared Memory opcodes save time File System OR
  • 14. Load (same script) after OPCode cache Executing opcodes PHP code opcodes load Shared Memory File System OR
  • 15. Optimize  Why not optimize the OPCode array ? opcodes Optimizing opcodes opcodes Caching Compiling save Executing opcodes Shared Memory File System OR
  • 17. OPCode  In computer science, an opcode (operation code) is the portion of a machine language instruction that specifies the operation to be performed. (Wikipedia)  Opcodes can also be found in so called byte codes and other representations intended for a software interpreter rather than a hardware device. (Wikipedia)
  • 19. Example of OPCode <?php const DEF = "default"; if (isset($argv[1])) { $b = (array)$argv[1]; } else { $b = DEF; } var_dump($b);
  • 20. Example of OPCode <?php const DEF = "default"; if (isset($argv[1])) { $b = (array)$argv[1]; } else { $b = DEF; } var_dump($b); L0 (3): DECLARE_CONST string("DEF") string("default") L1 (5): T2 = ISSET_ISEMPTY_DIM_OBJ (isset) CV0($argv) int(1) L2 (5): JMPZ T2 L7 L3 (6): T3 = FETCH_DIM_R CV0($argv) int(1) L4 (6): T4 = CAST (array) T3 L5 (6): ASSIGN CV1($b) T4 L6 (6): JMP L9 L7 (8): T6 = FETCH_CONSTANT (unqualified) string("DEF") L8 (8): ASSIGN CV1($b) T6 L9 (11): INIT_FCALL 1 96 string("var_dump") L10 (11): SEND_VAR CV1($b) 1 L11 (11): DO_ICALL L12 (12): RETURN int(1) 3 5 6 7 8 11
  • 21. Example of OPCode <?php const DEF = "default"; if (isset($argv[1])) { $b = (array)$argv[1]; } else { $b = DEF; } var_dump($b); Execution Compilation  The more code to parse, the longer the compilation phase  The more OPCodes generated, the longer the execution  Depends on OPCodes, some are long to run, some are very fast DECLARE_CONST ISSET_ISEMPTY_DIM_OBJ JMPZ FETCH_DIM_R CAST ASSIGN JMP FETCH_CONSTANT ASSIGN INIT_FCALL SEND_VAR DO_ICALL RETURN
  • 22. Optimize execution  Some OPCodes are heavier than others  OPCodes may be run several times (in fact, yes they do)  The optimizer can play a role Optimizing Caching Compiling Executing
  • 23. OPCache  "OPCache" is PHP OPCode cache  It is both a cache, and an optimizer  It is bundled by defaut with PHP  But it may not be activated by default
  • 24. Optimizer  Tries to simplify/optimize the OPCodes so that there are less of them and they are more efficient  Works on code branches (if, switch, try …)  Optimize constant expressions - Look for dead code / code reuse
  • 25. Try the optimizer php -dzend_extension=opcache -dopcache.enable_cli=1 -dopcache.opt_debug_level=0x30000 -dopcache.optimization_level=0x7FFFFFFF /tmp/php.php
  • 26. Optimizer impact  The optimizer is very heavy, and burns tons of CPU cycles  At compilation  But compilation only happens once, as opcodes will be cached  So they'd better be cached optimized, than raw  The very first run of a big application should pay a big perf penalty  Prime your caches  Use OPCache file-based cache
  • 27. Interned strings optimized  In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool Wikipedia
  • 28. Interned strings concept  Anytime a static string occurs in compiler, memorize it, and reuse its pointer when needed.  OPCache optimizes them even more class foo { public function bar($a = "foo") { } } $foo = new foo; $foo->bar("foo");
  • 29. No Interned strings class foo { public function bar($a = "foo") { } } $foo = new foo; $foo->bar("foo"); foo bar a foo foo foo foo bar foo memory
  • 30. Interned strings class foo { public function bar($a = "foo") { } } $foo = new foo; $foo->bar("foo"); foo bar a memory
  • 31. OPCache & interned strings  OPCache interns strings from all PHP processes of the same PHP pool  Nice memory savings on big pools interned string mem interned string mem interned string mem interned string mem
  • 32. Tune your OPCache string buffer php -dzend_extension=opcache -dopcache.memory_consumption=1024 -dopcache.interned_strings_buffer=64 /tmp/foo.php Mb
  • 33. Comments are interned strings  How many interned string bytes for this script ? <?php /** * @param $a string */ function foo(string $a) { }
  • 34. PHPDoc comments are interned strings  How many interned string bytes for this script ?  opcache.save_comments = 0 disables saving of PHPDoc comments to shared memory <?php /** * @param $a string */ function foo(string $a) { } 28 + 3 + 1 + {garbage} 0 ending strings in C + some alignment bytes
  • 35. PHP OPCache OPCache API, settings and tuning
  • 36. OPCache settings  memory_consumption  Size of Shared Memory  Don't forget to increase according to your needs  Buy more RAM, NEVER have a cache full, have margin  max_accelerated_files (=2000)  Max number of KEYS to be stored  KEYS are paths to files : ./foo.php, ../foo.php, etc...  One file usually is ~= 2 to 3 KEYS  Aligned at next prime number  Slots are preallocated : don't give too high number  Monitor
  • 37. OPCache explained  memory_consumption=128M  max_accelerated_files = 3000  Next prime : 3907  Cached files : 1878  Cached keys : 2722 / 3907
  • 38. OPCache settings  validate_timestamps (=true)  Check for file update to invalidate it  Checks are triggered every revalidate_freq seconds  revalidate_freq (=2)  How often check for file updates to invalidate them  0 = every time  Revalidate = syscall cached by PHP's realpath cache  revalidate_paths (=0)  1 : only the resolved realpath is used as cache key  0 : the unresolved path is also added as a cache key  use_cwd (=1)  prepend cwd to every cache key for relative paths
  • 39. OPCache memory details  When a script changes, it is recompiled (if validate_timestamps = 1)  Its old memory space is then considered as "wasted" (it is then NOT freed)  When cache is full, if max_wasted_percentage is reached : a cache restart is triggered  Cache restart = empty cache and recompile all  Cache is never restarted if not full
  • 40. OPcache other settings  optimization_level (=0x7fffffff)  Enable/disable some optimizations.  enable_file_override (=0)  Redefines file_exists(), is_readable() and is_file() to look into cache before : recommanded  blacklist_filename (=NULL)  Prevents caching some files you decide  consistency_checks (=0)  Computes a checksum at each file fetch. Not recommanded, ~5- 8% slowdown  opcache.save_comments (=1)  Saves PHPDoc comments to shared memory
  • 41. OPcache other settings  enable_cli (=0)  Enables opcache in CLI mode  Long living apps like PHPUnit and COmposer mat benefit from the optimizer and the cache  Other short scripts will pay an impact against the optimizer : don't enable  huge_code_pages (=0)  Moves the PHP process code memory segment to huge pages  On some specific OS, with a mainly-statically-linked PHP, that can have a beneficial impact  Check /proc/meminfo to activate HugePage before usage  protect_memory (=0)  mprotect() the SHM against writes. Useless until you find a bug, crash, and want to debug it
  • 42. Know what happens  error_log (=stderr)  File to write log to  log_verbosity_level (=1)  0=fatals … 4=debug
  • 43. OPCache do's and don't's  Prime your cache smoothly  https://github.com/engineyard/ey-php-performance-tools  Prevent cache stampede  Have enough shared memory  Size your hashtables correctly  Try not to generate php files on runtime  Prevent highly modified files from beeing cached  Use blacklists  Every cache action will lock the shared memory  And PHP Engine is not threaded
  • 44. Thank you for listening