• Télécharger
Yoopee cache (op cache internals)
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Yoopee cache (op cache internals)

on

  • 3,936 vues

 

Statistiques

Vues

Total des vues
3,936
Vues sur SlideShare
3,363
Vues externes
573

Actions

J'aime
9
Téléchargements
0
Commentaires
1

8 Ajouts 573

http://remibarbe.fr 369
http://deamonspace.fr 87
https://twitter.com 57
http://www.scoop.it 28
http://confluence.digitick.local 17
http://librosweb.es 10
http://localhost 4
https://www.linkedin.com 1
Plus...

Accessibilité

Catégories

Détails de l'import

Uploaded via as OpenOffice

Droits d'utilisation

© Tous droits réservés

Report content

Signalé comme inapproprié Signaler comme inapproprié
Signaler comme inapproprié

Indiquez la raison pour laquelle vous avez signalé cette présentation comme n'étant pas appropriée.

Annuler
  • Full Name Full Name Comment goes here.
    Êtes-vous sûr de vouloir
    Votre message apparaîtra ici
    Processing...
Poster un commentaire
Modifier votre commentaire

Yoopee cache (op cache internals) Presentation Transcript

  • 1. YooopeeCache PHP OPCache explained
  • 2. Hello everybody  Julien PAULI  Programming in PHP since ~10y  PHP Internals reviewer since ~3y  PHP 5.5 and 5.6 Release Manager  Working at SensioLabs in Paris  http://www.phpinternalsbook.com  @julienpauli - github.com/jpauli - 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  Benchmarks
  • 4. What is PHP ?  PHP  Programming language  "Scripting language"  No *manual* compilation needed  Fire and forget     Automatic memory management No strong typing Provides OOP features Highly dynamic, highly extensible
  • 5. How does PHP work ? Result Executing opcodes Compiling nodes Parsing (Lexer + parser) PHP code
  • 6. Zend Virtual Machine  "VirtualMachine"  Its purpose is to provide a platformindependent 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) Executing Compiling Parsing (Lexer + parser)
  • 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 Compiling Parsing (Lexer + parser)
  • 9. Compiling VS Executing  Compile, then execute  Which one is the longest ?  Depends on your scripts Execute Execute Compile 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 B Compile Execute E Y include/require ? eval() ? autoload ? N
  • 12. OPCode cache roles Result Executing opcodes Caching opcodes Compiling nodes Parsing (Lexer + parser) PHP code
  • 13. opcodes Save at first run Shared Memory Executing opcodes Caching save opcodes Compiling nodes
  • 14. opcodes Load (same script) after Executing opcodes Shared Memory load OPCode cache PHP code
  • 15. Optimize  Why not optimize the OPCode array ? Executing opcodes Shared Memory save Caching opcodes opcodes Optimizing opcodes Compiling
  • 16. PHP OPCache OPCodes explained
  • 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)
  • 18. Zend OPCodes
  • 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);
  • 21. Example of OPCode if (isset($argv[1])) { $b = (array)$argv[1]; } else { $b = DEF; } Compilation Execution <?php const DEF = "default"; var_dump($b);  The more code to parse, the longer the compilation phase  The more OPCodes generated, the longer the execution (usually)
  • 22. Optimize compilation  "The more code to parse, the longer the compilation phase" opcodes  No problem : OPCode caches compile any script just once Executing opcodes Shared Memory load OPCode cache PHP code
  • 23. Optimize execution  "The more OPCodes generated, the longer the execution (usually)" Executing  Some OPCodes are heavier than others  OPCodes may be run several times (in fact, yes they do)  The optimizer can play a role Caching Optimizing Compiling
  • 24. OPCache  "OPCache" is the default PHP OPCode cache  It is available for 5.3, 5.4 and 5.5  Bundled in the 5.5 distribution  PECL for others  It is both a cache, and an optimizer
  • 25. 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 Look for ways to reuse things  Examples :
  • 26. Optimizer in action if (false) { echo "foo"; } else { echo "bar"; } Classic compilation : Optimized compilation :
  • 27. Optimizer in action $a = 4 + "33"; echo $a; Classic compilation : Optimized compilation :
  • 28. Optimizer in action  Can you get the trick ? $a = 8; $c = $a + "42"; echo $c; Classic compilation : Optimized compilation :
  • 29. Optimizer in action $i = "foo"; $i = $i + 42; echo $i; Classic compilation : Optimized compilation :
  • 30. Optimizer in action $j = 4; $j++; echo $j; Classic compilation : Optimized compilation :
  • 31. Optimizer in action Classic compilation : Optimized compilation : $a = $_GET['a'] + 7; $b = $_GET['a'] - 18;
  • 32. Optimizer in action const FOO = "bar"; echo FOO; Classic compilation : Optimized compilation :
  • 33. More optimizations  This was the visible part  OPCache takes care of lots more invisible optimizations  http://en.wikipedia.org/wiki/Category:Compiler_optimizations
  • 34. 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
  • 35. Interned strings concept  Anytime a static string occurs in compiler, memorize it, and reuse its pointer when needed. class foo { public function bar($a = "foo") { } } $foo = new foo; $foo->bar("foo");  Interned strings have been added to PHP5.4 compiler  OPCache optimizes them even more
  • 36. Interned strings before 5.4  Before 5.4, the compiler allocated memory for each string it met, whatever it was class foo { public function bar($a = "foo") { } } $foo = new foo; $foo->bar("foo"); foo bar a foo foo foo memory foo bar foo
  • 37. Interned strings with >=5.4  Starting from 5.4, the compiler allocates memory for strings and reuse the pointer if it meets the same string later class foo { public function bar($a = "foo") { } } $foo = new foo; $foo->bar("foo"); foo bar a memory
  • 38. Interned strings optimizations  When strings are interned, they no longer need to be duplicated static void zend_duplicate_property_info(zend_property_info *property_info) /* {{{ */ { if (!IS_INTERNED(property_info->name)) { property_info->name = estrndup(property_info->name, property_info->name_length); } if (property_info->doc_comment) { property_info->doc_comment = estrndup(property_info->doc_comment, property_info->doc_comment_len); } }  Every hash of hashtable is precomputed if (IS_INTERNED(Z_STRVAL_P(varname))) { hash_value = INTERNED_HASH(Z_STRVAL_P(varname)); } else { hash_value = zend_hash_func(Z_STRVAL_P(varname), Z_STRLEN_P(varname)+1); }
  • 39. 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
  • 40. PHP OPCache OPCache API, settings and tuning
  • 41. OPCache API  opcache_reset()  Resets the whole cache, invalidates all files  opcache_invalidate($file)  Invalidates a single file  opcache_get_configuration()  returns current INI settings  opcache_get_status()  returns an array of internal stats  opcache_compile_file()  Compile a single file into the cache at runtime
  • 42. OPCache frontend  https://github.com/rlerdorf/opcachestatus/blob/master/opcache.php
  • 43. OPCache settings  memory_consumption (=64M)  Size of Shared Memory  Don't forget to increase if needed  For example, a "classical" Symfony2 app needs ~40Mb  Buy more RAM, NEVER have a cache full, have margin  max_accelerated_files (=2000)     Max number of KEYS to be stored Aligned at next prime number Slots are preallocated : don't give too high number A "classical" Symfony2 app needs ~3000
  • 44. OPCache explained  memory_consumption=128M  max_accelerated_files = 3000  Next prime : 3907  Cached files : 1877  Cached keys : 2722 / 3907
  • 45. How OPCache computes keys ?  A file "Foo" may be accessed in your script  With a full path (/path/to/foo.php)  With many relative paths (../../foo.php)  Which then all have to be resolved  OPCache makes the slots' keys with all the met paths to a file  This prevents it from calling realpath() too often  This occupies "slots"  Typically, an average is ~3 slots per file  If you play with symlinks, mind php realpath_cache_ttl setting
  • 46. How OPCache computes keys ? require "/full/path/to/foo.php" key 1 require "../relativepath/to/foo.php" key 2 require "./relativepath/../to/foo.php" key 3 max_accelerated_files Foo script
  • 47. 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  Try not to put 0, meaning every time  Revalidate often use syscall fstat()  But SAPI stat cache is used, thus preventing a stat call  max_wasted_percentage (=5)  Percentage threshold to trigger an opcache reset
  • 48. 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
  • 49. OPCache restarts  This cache is full , but current_wasted_percentage < max_wasted_percentage  So the cache will not restart yet  restart_pending = restart_in_progress = false
  • 50. OPCache restart reasons  oom_restarts  Cannot allocate memory for keys of for script space  Your memory segment is too small, increase memory_consumption  hash_restarts  One hashtable is full and needs more slots  You reached the max num of keys, increase max_accelerated_files  manual_restarts  You used opcache_reset()
  • 51. Know what happens  error_log (=stderr)  File to write log to  log_verbosity_level (=1)  0=fatals … 4=debug
  • 52. OPcache other settings  optimization_level (=0xffffffff)  Enable/disable some optimizations. Recommanded : all  fast_shutdown (=0)  Changes the RShutdown sequence with an optimized one : recommanded  enable_file_override (=1)  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
  • 53. 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
  • 54. Thank you for listening