Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP OPCache, Realpath Cache and Preloading

PHP OPCache, Realpath Cache and Preloading

Everybody wants quick applications. A lot of that speed can be gained by the way you write your software, but a big chunk has to do with the way PHP is configured. As PHP matured, it got quicker, it used less memory and it accumulated a lot of settings which can be tuned for performance.

The biggest, and often most misunderstood, features for performance are OPCache (introduced in 5.5) and preloading (introduced in 7.4). This talk covers how both features work, how you can take advantage of them on your servers and during deployments, and tries to show all the ini settings relevant for performance.

Jachim Coudenys

May 14, 2019
Tweet

More Decks by Jachim Coudenys

Other Decks in Technology

Transcript

  1. PHP OPCache,
    Realpath Cache
    and Preloading

    View Slide

  2. Jachim Coudenys

    View Slide

  3. PHP-WVL

    View Slide

  4. Combell

    View Slide

  5. OPCache

    View Slide

  6. PHP
    Scripting Language
    Fire and forget
    No "manual" compilation

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  10. OPCodes (usually) don't change

    View Slide

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

    View Slide

  12. Next run
    Cache: Opcodes
    Executing

    View Slide

  13. And the next run
    Cache: Opcodes
    Executing

    View Slide

  14. Even the next run
    Cache: Opcodes
    Executing

    View Slide

  15. OPCache
    Since PHP 5.5
    Zend Optimizer "donated" by Zend

    View Slide

  16. 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.

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  20. This can get big quite quickly
    Libraries
    Frameworks

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  24. 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

    View Slide

  25. Keys
    Full path to file
    Relative paths to files

    View Slide

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

    View Slide

  27. Opcache restarts
    OOM Restarts
    Hash Restarts
    Manual Restarts

    View Slide

  28. NEVER have a full cache!

    View Slide

  29. Opcache Priming
    opcache_compile_file()
    FPM Pools

    View Slide

  30. opcache.php demo

    View Slide

  31. APCu
    User Shared memory
    apc_* compatible

    View Slide

  32. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  36. Realpath Cache

    View Slide

  37. 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")

    View Slide

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

    View Slide

  39. realpath.php demo

    View Slide

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

    View Slide

  41. Preloading

    View Slide

  42. 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.

    View Slide

  43. 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

    View Slide

  44. 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

    View Slide

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

    View Slide

  46. PHP <= 7.3
    PHP process starts up

    View Slide

  47. 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

    View Slide

  48. 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

    View Slide

  49. PHP >= 7.4 + preloading
    PHP process starts up

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  53. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide