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 full-size slide

  2. Jachim Coudenys

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  7. OPCodes (usually) don't change

    View full-size slide

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

    View full-size slide

  9. Next run
    Cache: Opcodes
    Executing

    View full-size slide

  10. And the next run
    Cache: Opcodes
    Executing

    View full-size slide

  11. Even the next run
    Cache: Opcodes
    Executing

    View full-size slide

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

    View full-size slide

  13. 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 full-size slide

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

    View full-size slide

  15. 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 full-size slide

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

    View full-size slide

  17. This can get big quite quickly
    Libraries
    Frameworks

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  20. 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 full-size slide

  21. 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 full-size slide

  22. Keys
    Full path to file
    Relative paths to files

    View full-size slide

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

    View full-size slide

  24. Opcache restarts
    OOM Restarts
    Hash Restarts
    Manual Restarts

    View full-size slide

  25. NEVER have a full cache!

    View full-size slide

  26. Opcache Priming
    opcache_compile_file()
    FPM Pools

    View full-size slide

  27. opcache.php demo

    View full-size slide

  28. APCu
    User Shared memory
    apc_* compatible

    View full-size slide

  29. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  33. Realpath Cache

    View full-size slide

  34. 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 full-size slide

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

    View full-size slide

  36. realpath.php demo

    View full-size slide

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

    View full-size slide

  38. 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 full-size slide

  39. 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 full-size slide

  40. 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 full-size slide

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

    View full-size slide

  42. PHP <= 7.3
    PHP process starts up

    View full-size slide

  43. 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 full-size slide

  44. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  49. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide