Deploying PHP 7

Scotland PHP

Edinburgh

Oct.29, 2016

http://talks.php.net/scotphp16

Rasmus Lerdorf
@rasmus

✔ engine improvements

  • 100%+ performance gain on most real-world applications
  • Lower memory usage, sometimes drastically lower

JIT?

Improve CPU cache usage

  • Step 1: Decrease overall data
  • Step 2: Better data locality and less indirections
  • Step 3: Save the world!
  • zval size reduced from 24 to 16 bytes
  • Hashtable size reduced from 72 to 56 bytes
  • Hashtable bucket size reduced from 72 to 32 bytes
  • Immutable array optimization
$a = [];
for($i=0; $i < 100000;$i++) {
    $a[] = ['abc','def','ghi','jkl','mno','pqr'];
}
echo memory_get_usage(true);

// PHP 5.x  109M
// PHP 7.0   42M no opcache
// PHP 7.0    6M with opcache enabled
  • New memory allocator similar to jemalloc
  • Faster hashtable iteration API
  • Array duplication optimization
  • PCRE JIT enabled by default
  • Precomputed string hashes
  • Fast ZPP (ZendParseParameters) implementation
  • Faster stack-allocated zvals (instead of heap)
  • Optimized VM calling
  • Global register variables with gcc 4.8+
  • plus hundreds of micro-optimizations

JIT?

GCC Feedback-Directed Optimization (FDO)

$ make clean
$ make -j8 prof-gen
...
$ sapi/cgi/php-cgi -T 1000 /var/www/wordpress/index.php > /dev/null
$ make prof-clean
$ make -j8 prof-use

Had to fix one line of code in the Avalon database library:

diff --git a/database/model.php b/database/model.php
index 6c5f7da..c93e726 100644
--- a/database/model.php
+++ b/database/model.php
@@ -397,7 +397,7 @@ public function __get($var) {
           $belongs_to['column'] = $var . '_id';
       }
       $model = $belongs_to['model'];
-      return $this->$var = $model::find($belongs_to['foreign_key'], $this->$belongs_to['column']);
+      return $this->$var = $model::find($belongs_to['foreign_key'], $this->{$belongs_to['column']});
   } else {
       $val = $this->$var;

✔ engine improvements

  • 100%+ performance gain on most real-world applications
  • Lower memory usage, sometimes drastically lower

✔ Exceptions on Fatals

function call_method($obj) {
    $obj->method();
}
call_method(null);
Fatal error: Uncaught Error: Call to a member function method() on null in file:2
Stack trace:
#0 file(4): call_method(NULL)
#1 {main}
  thrown in file on line 2
try {
    call_method(null);
} catch (Error $e) {
    echo "Caught Exception: {$e->getMessage()}\n";
}
Caught Exception: Call to a member function method() on null

PHP 7 Exception Hierarchy


  • Throwable
  • Exception implements Throwable
  • Error implements Throwable
  • TypeError extends Error
  • ParseError extends Error

✔ Return Types

function get_config(): array {
    return 42;
}
get_config();
Fatal error: Uncaught TypeError: Return value of get_config() must be
of the type array, integer returned in file:2
Stack trace:
#0 file(4): get_config()
#1 {main}
  thrown in file on line 2

✔ Coercive Scalar Types

function logmsg(string $msg, int $level, float $severity) {
    var_dump($msg);      // string(1) "1"
    var_dump($level);    // int(2)
    var_dump($severity); // float(3)
}
logmsg(1, "2.5 bananas", 3);
Notice: A non well formed numeric value encountered in file on line 2

✔ Strict Scalar Types

declare(strict_types=1);
...
logmsg(1, "2.5", 3);
Fatal error: Uncaught TypeError: Argument 1 passed to logmsg() must be of the
type string, integer given, called in file on line 7 and defined in file:3
Stack trace:
#0 file(7): logmsg(1, '2.5', 3)
#1 {main}
  thrown in file on line 2

✔ Anonymous Classes

return new class($controller) implements Page {
    public function __construct($controller) {
        /* ... */
    }
    /* ... */
};

class MyObject extends MyStuff {
    public function getInterface() {
        return new class implements MyInterface {
            /* ... */
        };
    }
}

✔ Coalesce Operator

$a = NULL;
$b = 0;
$c = 2;

echo $a ?? $b; // 0
echo $c ?? $b; // 2
echo $a ?? $b ?? $c; // 0
echo $a ?? $x ?? $c; // 2

✔ Spaceship Operator

function cmp_php5($a, $b) {
    return ($a < $b) ? -1 : (($a >$b) ? 1 : 0);
}

function cmp_php7($a, $b) {
    return $a <=> $b;
}

✔ Removal of many deprecated features
     (Your PHP4 code will break!)

- ext/ereg (use ext/pcre instead)
- preg_replace() eval modifier (use preg_replace_callback() instead)
- ext/mysql (use ext/mysqli or ext/pdo_mysql instead)
- Assignment of new by reference
- Scoped calls of non-static methods from incompatible $this context

- dl() in php-fpm
- set_magic_quotes_runtime() and magic_quotes_runtime()
- set_socket_blocking() (use stream_set_blocking() instead)
- mcrypt_generic_end() (use mcrypt_generic_deinit() instead)
- mcrypt_ecb, mcrypt_cbc, mcrypt_cfb and mcrypt_ofb 
  (use mcrypt_encrypt() and mcrypt_decrypt() instead)
- datefmt_set_timezone_id() and IntlDateFormatter::setTimeZoneID() 
  (use datefmt_set_timezone() or IntlDateFormatter::setTimeZone() instead)

- xsl.security_prefs (use XsltProcessor::setSecurityPrefs() instead)
- iconv.input_encoding, iconv.output_encoding, iconv.internal_encoding,
  mbstring.http_input, mbstring.http_output and mbstring.internal_encoding
  (use php.input_encoding, php.internal_encoding and php.output_encoding instead)

- $is_dst parameter of the mktime() and gmmktime() functions
- # style comments in ini files (use ; style comments instead)
- String category names in setlocale() (use LC_* constants instead)
- Unsafe curl file uploads (use CurlFile instead)
- PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT driver option 
  (use PDO::ATTR_EMULATE_PREPARES instead)
- CN_match and SNI_server_name stream context option (use peer_name instead)

✔ New reserved words:

  • bool
  • int
  • float
  • string
  • null
  • false
  • true
  • resource
  • object
  • mixed
  • numeric

✔ 64-bit integer support on Windows

✔ Cleanup edge-case integer overflow/underflow

✔ Support for strings with length >= 2^31 bytes in 64 bit builds.

✔ Parse error on invalid numeric literals

$mask = 0855;  // Parse error: Invalid numeric literal

✔ Uniform variable syntax

// left-to-right
$this->$belongs_to['column']
// vs.
$this->{$belongs_to['column']}

// support missing combinations of operations
$foo()['bar']()
[$obj1, $obj2][0]->prop
getStr(){0}
 
// support nested ::
$foo['bar']::$baz
$foo::$bar::$baz
$foo->bar()::baz()
 
// support nested ()
foo()()
$foo->bar()()
Foo::bar()()
$foo()()
 
// support operations on arbitrary (...) expressions
(...)['foo']
(...)->foo
(...)->foo()
(...)::$foo
(...)::foo()
(...)()
 
// two more practical examples for the last point
(function() { ... })()
($obj->closure)()
 
// support all operations on dereferencable scalars
// (not very useful)
"string"->toLower()
[$obj, 'method']()
'Foo'::$bar

✔ Unicode Codepoint Escape Syntax

echo "\u{202E}Right-to-left text";

echo " \u{26BD}";
‮Right-to-left text ⚽		

✔ ICU IntlChar class added to intl extension

✔ CSPRNG

$int   = random_int(-500, 500);
$bytes = random_bytes(10);

var_dump( $int );
var_dump( bin2hex($bytes) );
int(-433)
string(20) "dafd192c3a58142e2586"
		

Top-5 Things that might bite you




For the full list see

php.net/migration70

Left-to-right semantics for complicated expressions

$$foo['bar']['baz'] // interpreted as ($$foo)['bar']['baz']
$foo->$bar['baz']   // interpreted as ($foo->$bar)['baz']
$foo->$bar['baz']() // interpreted as ($foo->$bar)['baz']()
Foo::$bar['baz']()  // interpreted as (Foo::$bar)['baz']()

To restore the previous behaviour add explicit curly braces:

${$foo['bar']['baz']}
$foo->{$bar['baz']}
$foo->{$bar['baz']}()
Foo::{$bar['baz']}()

Detection: phan or unit test failures

Removed support for /e (PREG_REPLACE_EVAL) modifier

echo preg_replace('/:-:(.*?):-:/e', '$this->pres->\\1', $text);

Change to:

echo preg_replace_callback(
  '/:-:(.*?):-:/', 
  function($matches) {
    return $this->pres->{$matches[1]}; // Careful!
  },
  $text);

Detection: grep, warnings in logs or unit test failures

$HTTP_RAW_POST_DATA global removed

if (empty($GLOBALS['HTTP_RAW_POST_DATA']) &&
    strpos($_SERVER['CONTENT_TYPE'], 'www-form-urlencoded') === false) {
    $GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents("php://input");
}

Detection: grep, warnings in logs or unit test failures

session.lazy_write enabled by default

session.lazy_write = 0

Detection: Can cause out-of-band session read timing issues

Invalid octal literals now produce a parse error

echo 05678; // PHP 5.x outputs 375
Parse error: Invalid numeric literal in file.php on line 2		

Detecting parse errors is easy: php -l

Static Analysis




github.com/etsy/phan
% phan -h
Usage: ./phan [options] [files...]
 -f, --file-list <filename>
  A file containing a list of PHP files to be analyzed

 -r, --file-list-only
  A file containing a list of PHP files to be analyzed to the
  exclusion of any other directories or files passed in. This
  is useful when running Phan from a stored state file and
  passing in a small subset of files to be re-analyzed.

 -l, --directory <directory>
  A directory to recursively read PHP files from to analyze

 -3, --exclude-directory-list <dir_list>
  A comma-separated list of directories for which any files
  included from that directory will not be analysis. Note
  that adding a directory here will not cause its files to
  be parsed.

 -d, --project-root-directory
  Hunt for a directory named .phan in the current or parent
  directory and read configuration file config.php from that
  path.

 -m <mode>, --output-mode
  Output mode from 'text', 'json', 'codeclimate', or 'checkstyle'

 -o, --output <filename>
  Output filename

 -p, --progress-bar
  Show progress bar

 -a, --dump-ast
  Emit an AST for each file rather than analyze

 -e, --expand-file-list
  Expand the list of files passed in to include any files
  that depend on elements defined in those files. This is
  useful when running Phan from a state file and passing in
  just the set of changed files.

 -q, --quick
  Quick mode - doesn't recurse into all function calls

 -b, --backward-compatibility-checks
  Check for potential PHP 5 -> PHP 7 BC issues

 -i, --ignore-undeclared
  Ignore undeclared functions and classes

 -y, --minimum-severity <level in {0,5,10}>
  Minimum severity level (low=0, normal=5, critical=10) to report.
  Defaults to 0.

 -c, --parent-constructor-required
  Comma-separated list of classes that require
  parent::__construct() to be called

 -x, --dead-code-detection
  Emit issues for classes, methods, functions, constants and
  properties that are probably never referenced and can
  possibly be removed.

 -j, --processes <int>
  The number of parallel processes to run during the analysis
  phase. Defaults to 1.

 -z, --signature-compatibility
  Analyze signatures for methods that are overrides to ensure
  compatiiblity with what they're overriding.

 -h,--help
  This help information
% phan -i -b display.php

display.php:416 CompatError expression may not be PHP 7 compatible
echo preg_replace('/:-:(.*?):-:/e', '$this->pres->\\1', $text);
echo preg_replace_callback(
    '/:-:(.*?):-:/', 
    function($matches) {
      return $this->pres->$matches[1]; // Oops!
    },
    $text);
echo preg_replace_callback(
    '/:-:(.*?):-:/', 
    function($matches) {
      return $this->pres->{$matches[1]}; // Ok
    },
    $text);
% git clone https://github.com/Seldaek/monolog.git
% cd monolog
% find . -name '*.php' | grep -v test > filelist.txt
% phan -i -f filelist.txt

./src/Monolog/Handler/ChromePHPHandler.php:178 PhanTypeMismatchReturn Returning type int but headersAccepted() is declared to return bool
./src/Monolog/Handler/ElasticSearchHandler.php:124 PhanTypeMismatchArgumentInternal Argument 3 (previous) is \elastica\exception\exceptioninterface but \runtimeexception::__construct() takes \runtimeexception|\throwable
./src/Monolog/Handler/FirePHPHandler.php:81 PhanTypeMismatchReturn Returning type array but createRecordHeader() is declared to return string
./src/Monolog/Handler/FirePHPHandler.php:153 PhanTypeMismatchArgumentInternal Argument 1 (array_arg) is string but \current() takes array
./src/Monolog/Handler/FirePHPHandler.php:154 PhanTypeMismatchArgumentInternal Argument 1 (array_arg) is string but \current() takes array
./src/Monolog/Handler/FirePHPHandler.php:154 PhanTypeMismatchArgumentInternal Argument 1 (array_arg) is string but \key() takes array
./src/Monolog/Handler/FlowdockHandler.php:70 PhanTypeMissingReturn Method \monolog\handler\flowdockhandler::getdefaultformatter is declared to return \monolog\formatter\formatterinterface but has no return value
./src/Monolog/Handler/GelfHandler.php:55 PhanTypeMismatchProperty Assigning null to property but \monolog\handler\gelfhandler::publisher is \gelf\imessagepublisher|\gelf\publisher|\gelf\publisherinterface
./src/Monolog/Handler/MandrillHandler.php:49 PhanSignatureMismatch Declaration of function send($content, array $records) should be compatible with function send(string $content, array $records) defined in ./src/Monolog/Handler/MailHandler.php:46
./src/Monolog/Handler/NativeMailerHandler.php:117 PhanSignatureMismatch Declaration of function send($content, array $records) should be compatible with function send(string $content, array $records) defined in ./src/Monolog/Handler/MailHandler.php:46
./src/Monolog/Handler/RedisHandler.php:41 PhanTypeMismatchDefault Default value for int $capSize can't be bool
./src/Monolog/Handler/SocketHandler.php:115 PhanTypeMismatchProperty Assigning float to property but \monolog\handler\sockethandler::timeout is int
./src/Monolog/Handler/SocketHandler.php:126 PhanTypeMismatchProperty Assigning float to property but \monolog\handler\sockethandler::writingTimeout is int
./src/Monolog/Handler/SocketHandler.php:218 PhanTypeMismatchArgumentInternal Argument 2 (seconds) is float but \stream_set_timeout() takes int
./src/Monolog/Handler/SocketHandler.php:218 PhanTypeMismatchArgumentInternal Argument 3 (microseconds) is float but \stream_set_timeout() takes int
./src/Monolog/Handler/SocketHandler.php:274 PhanTypeMismatchProperty Assigning resource to property but \monolog\handler\sockethandler::resource is null
./src/Monolog/Handler/StreamHandler.php:65 PhanTypeMismatchProperty Assigning null to property but \monolog\handler\streamhandler::stream is resource|string
./src/Monolog/Handler/StreamHandler.php:86 PhanTypeMismatchProperty Assigning null to property but \monolog\handler\streamhandler::stream is resource|string
./src/Monolog/Handler/StreamHandler.php:105 PhanTypeMismatchProperty Assigning array|string to property but \monolog\handler\streamhandler::errorMessage is null
./src/Monolog/Handler/SwiftMailerHandler.php:43 PhanSignatureMismatch Declaration of function send($content, array $records) should be compatible with function send(string $content, array $records) defined in ./src/Monolog/Handler/MailHandler.php:46
./src/Monolog/Handler/SyslogUdp/UdpSocket.php:38 PhanTypeMismatchProperty Assigning null to property but \monolog\handler\syslogudp\udpsocket::socket is resource
ChromePHPHandler.php:178 PhanTypeMismatchReturn Returning type int but headersAccepted() is declared to return bool
/**
 * Verifies if the headers are accepted by the current user agent
 *
 * @return Boolean
 */
protected function headersAccepted() {
    if (empty($_SERVER['HTTP_USER_AGENT'])) {
        return false;
    }
    return preg_match('{\bChrome/\d+[\.\d+]*\b}', $_SERVER['HTTP_USER_AGENT']);
}
FirePHPHandler.php:154 PhanTypeMismatchArgumentInternal Argument 1 (array_arg) is string but \current() takes array
/**
 * Base header creation function used by init headers & record headers
 *
 * @param  array  $meta    Wildfire Plugin, Protocol & Structure Indexes
 * @param  string $message Log message
 * @return array  Complete header string ready for the client as key and message as value
 */
protected function createHeader(array $meta, $message) {
    $header = sprintf('%s-%s', self::HEADER_PREFIX, join('-', $meta));

    return array($header => $message);
}

/**
 * Creates message header from record
 *
 * @see createHeader()
 * @param  array  $record
 * @return string
 */
protected function createRecordHeader(array $record)
{
    // Wildfire is extensible to support multiple protocols & plugins in a single request,
    // but we're not taking advantage of that (yet), so we're using "1" for simplicity's sake.
    return $this->createHeader(
        array(1, 1, 1, self::$messageIndex++),
        $record['formatted']
    );
}

/**
 * Creates & sends header for a record, ensuring init headers have been sent prior
 *
 * @see sendHeader()
 * @see sendInitHeaders()
 * @param array $record
 */
protected function write(array $record)
{
    if (!self::$sendHeaders) {
        return;
    }

    // WildFire-specific headers must be sent prior to any messages
    if (!self::$initialized) {
        self::$initialized = true;

        self::$sendHeaders = $this->headersAccepted();
        if (!self::$sendHeaders) {
            return;
        }

        foreach ($this->getInitHeaders() as $header => $content) {
            $this->sendHeader($header, $content);
        }
    }

    $header = $this->createRecordHeader($record);
    if (trim(current($header)) !== '') {
        $this->sendHeader(key($header), current($header));
    }
}

PHP 7 Tuning


Check your phpinfo()

Opcache

opcache.memory_consumption=2048
opcache.max_accelerated_files=100000
opcache.validate_timestamps=1
opcache.revalidate_freq=2
opcache.save_comments=0
opcache.enable_file_override=0
opcache.enable_cli=0
opcache.max_wasted_percentage=10
opcache.interned_strings_buffer=128
opcache.fast_shutdown=1
opcache.huge_code_pages=1

Opcache

Huge Pages

$ sysctl -w vm.nr_hugepages=512
vm.nr_hugepages = 512
(Add it to your /etc/sysctl.conf)

$ grep Huge /proc/meminfo
AnonHugePages:      6144 kB
HugePages_Total:     512
HugePages_Free:      300
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
USE_ZEND_ALLOC_HUGE_PAGES=1 httpd ...

increase realpath_cache_size

realpath_cache_size=128k

If using MySQL, use mysqlnd

Check your command buffer usage

DocumentRoot on tmpfs

$ mount | grep tmpfs
tmpfs on /var/www type tmpfs (rw,relatime,size=12288000k,mode=755)

$ ls -la /var/www
total 5
drwxr-xr-x  5 root   root    160 Feb 23 02:47 .
drwxr-xr-x 26 root   root   4096 Feb  7 19:40 ..
lrwxrwxrwx  1 root   root     14 Feb 23 02:47 current -> /var/www/A
drwxrwxr-x 25 apache apache  640 Feb 11 22:04 A
drwxrwxr-x 25 apache apache  640 Feb 11 22:04 B

Application-level changes?

Remember this?

$a = [];
for($i=0; $i < 100000;$i++) {
    $a[] = ['abc','def','ghi','jkl','mno','pqr'];
}
echo memory_get_usage(true);

// PHP 5.x  109M
// PHP 7.0   42M no opcache
// PHP 7.0    6M with opcache enabled

Use it!

include 'config.php'; // $config = [ ... ]
include 'countries.php'; // $countries = [ 'CA'=>'Canada', ... ]

Hyperthreading and NUMA


  • HyperThreading handles extreme loads better
  • If you don't have multi-socket servers, turn on HT and move on
  • For multi-socket servers, things get interesting
$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                48
On-line CPU(s) list:   0-47
Thread(s) per core:    2
Core(s) per socket:    12
Socket(s):             2
NUMA node(s):          2
NUMA node0 CPU(s):     0-11,24-35
NUMA node1 CPU(s):     12-23,36-47

Solutions?

  • numactl --interleave=all httpd/php-fpm
  • split multi-socket with containers
  • BIOS Snoop Mode setting? HS/ES/COD?
  • ignore it

Let's deploy it!

Atomic

No performance hit

  • No restarts
  • No LB removal
  • No thundering herd
  • Cache reuse

Must be able to serve two versions of the site concurrently!

Requests that begin on DocumentRoot A must finish on A

Set the DocumentRoot to symlink target!

Easy with nginx

fastcgi_param DOCUMENT_ROOT $realpath_root

Apache

github.com/etsy/mod_realdoc

Avoid hardcoding full paths

Watch your include_path setting

incpath extension can resolve your include_path for you

https://github.com/etsy/incpath

Version all static assets

DB Schema changes need special care

PHP 7 in production


Thank You

https://github.com/rlerdorf/php7dev
https://github.com/rlerdorf/phan
https://bugs.php.net
http://talks.php.net/scotphp16



Report Bugs

Useful bug reports, please!