28
php SAPI ZendEngine2 2014/01/22 と 1 と PHP ととと @ ととと do_aki

php and sapi and zendengine2 and

  • Upload
    doaki

  • View
    2.059

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: php and sapi and zendengine2 and

php と

SAPI と

ZendEngine2 と

2014/01/22 第 1 回 PHP 勉強会 @ 相模

原do_aki

Page 2: php and sapi and zendengine2 and

@do_aki

http://do-aki.net/

Page 3: php and sapi and zendengine2 and
Page 4: php and sapi and zendengine2 and

20 代男子の php に対するイメージ

( 2014 年 俺脳内調べ)

Page 5: php and sapi and zendengine2 and

1位 洗練された ZendEngine2 は、たゆまぬ改良により、マイナバージョンアップですら ABI が変更されうる

2位 トリッキーな Zend Memory Managaer 。その複雑さのためか、ほとんど追加・改良されることがない (Zend/zend_alloc.c)

3位  SAPI により、多くの環境に最適化された入出力が実現されている感じがする

4位 選べる仮想マシン実装。ただし、標準で提供されている方式以外での実績は未知数

5位 その他

Page 6: php and sapi and zendengine2 and

#include "php.h"

<?php

Not php script

but php language system

(I talk about)

Page 7: php and sapi and zendengine2 and

php のコアってThose who know don't talk. ( 知ってる人は教えようとしない )

Those who talk don't know. ( 教えてる人はあまり知らない )

PHP: Zend API: PHP のコアをハックする より http://www.php.net/manual/ja/internals2.ze1.zendapi.php

Page 8: php and sapi and zendengine2 and

http://wiki.php.net/internals

情報増えてない

Page 9: php and sapi and zendengine2 and

Advanced PHP Programming (2004)http://books.google.co.jp/books?id=mCpnlNYzqOQC

Extending and Embedding PHP (2006)http://books.google.co.jp/books?id=zMbGvK17_tYC

書籍、洋書なら php コアの話あるけどやっぱり古め

Page 10: php and sapi and zendengine2 and

PHP Architecture

Web ServerorOS

SAPImodul

e

Zend Engine2

Extensions

PHP Script

Page 11: php and sapi and zendengine2 and

SAPI module

• Web サーバ /OS と PHP の橋渡し

• エントリーポイントはここにある

• 21 種類の bundle module

SAPImodul

e

Zend Engine

2

Extensions

PHP Script

Page 12: php and sapi and zendengine2 and

PHP SAPIs

aolserver / apache / apache2filter /

apache2handler / apache_hooks /

caudium / cgi / cli / continuity /

embed / fpm / isapi / litespeed /

milter / nsapi / phttpd / pi3web /

roxen / thttpd / tux / webjames(under sapi directory in php-5.5.8 source code)

Page 13: php and sapi and zendengine2 and

SAPI module

SAPI module

sapi_module_struct name startup function shutdown function activate function deactivate function ub_write function header_handler function executable_location php_ini_ignore…

Zend Engine2

register and call

callback

Page 14: php and sapi and zendengine2 and

ZendEngine2

• Parser / Compiler

• VirtualMachine• Memory

Manager• Garbage

Collection• (TSRM)

etc…

SAPImodule

Zend Engine

2

Extensions

PHP Script

Page 15: php and sapi and zendengine2 and

PHP(Zend) Extension

• Core• Bundle

Extensions• PECL

Extensions• PHP Ext と

Zend Ext の違いはよくわからない

Zend Engine

2

Extensions

PHP Script

SAPImodule

PHP extension と Zend extension の違い - hnw の日記 http://d.hatena.ne.jp/hnw/20130715

Page 16: php and sapi and zendengine2 and

Extension

PHP Extension zend_module_entry name function entry MINIT function MSHUTDOWN function RINIT function RSHUTDOWN function MINFO function extension version…

Zend Engine

2

load and call

callback

(register)

Page 17: php and sapi and zendengine2 and

PHP Script

• <?php• OpCode に変

換され、 VM で実行

• Extension や SAPI module を介して外部との入出力が行われる

SAPImodule

Extensions

PHP Script

Zend Engine

2

Page 18: php and sapi and zendengine2 and

SAPI

Page 19: php and sapi and zendengine2 and

life of php process

TSRM startup (only ZTS)SAPI startup

PHP module startup (Zend startup)PHP request startupexecute php scriptPHP request shutdownPHP module shutdown

SAPI shutdownTSRM shutdown

Page 20: php and sapi and zendengine2 and

startup process in SAPI

• prepare sapi_module (sapi_module_struct)

• TSRM startup – TSRM:=Thread Safe Resource Manager

• SAPI startup – SAPI:= Server API– SG (sapi_globals_struct@main/SAPI.h)

• call php_module_startup–main/main.c

Page 21: php and sapi and zendengine2 and

php_module_startup

• call zend_startup• init some global vars (EG,PG,SG,GC_G)• init some constant– PHP_VERSION,PHP_OS,PHP_SAPI etc...

• register auto globals ($_GET, $_POST …)

• read and scan ini files• init php (zend) extensions

Page 22: php and sapi and zendengine2 and

zend_startup

• init GLOBAL TABLE– FUNCTION/CLASS/AUTO_GLOBALS/CONSTANTS

• init INI_SCNG / LANG_SCNG• init interned strings• register builtin_functions– strlen, func_get_arg, class_exists, create_function

etc... (Zend/zend_builtin_functions.c)

• register standard constants – E_ERROR, TRUE, FALSE, NULL etc...

• register $GLOBALS

Page 23: php and sapi and zendengine2 and

request process (apache)

• prepare SG(request_info)• call php_request_startup– reset gc / compile/ executer– init output buffering stack– sapi_activate– init PG(http_globals)

• execute php script

Page 24: php and sapi and zendengine2 and

process chart

UA apacheSAPI

(mod_php)

zend engine

php script

[load module]

register_hook

[post_config]

sapi_startup / php_module_st

artup (zend_startup)

php_apache_

server_startup

compiler/scanner/ini/function_tab

le/class_table

等のメモリ確保

php_ap2_register_ho

ok

リクエスト待ち

Page 25: php and sapi and zendengine2 and

UA php script

http request [handler]

php_request_startup

zend_execute_scripts

apacheSAPI

(mod_php)

php_handle

r

zend engine

php_hash_environment に

て、 $_GET,$_POST,

$_COOKIE 等を構築

zend_compile_file

zend_execute

SG(request_info).query_stri

ng from apache

sapi_module.read_post

ap_get_brigade(input_filter…

sapi_module.read_cookies

apr_table_get(headers,”cookie”)

Page 26: php and sapi and zendengine2 and

UA

ap_write

apr_table_set or other

http response

echo (ZEND_ECHO)

sapi_module.ub_write

headersapi_module.header_handler

php script

zend engine

SAPI(mod_ph

p)apache

Page 27: php and sapi and zendengine2 and

最後に• php 処理系の全体像を解説しました• プロセスの持ち方や、 startup/

shutdown の制御は SAPI によって異なります

• SAPI と ZendEngine2 の責任の境界がイマイチ分からない (SG や sapi_module の扱い )

• 間違ってたら指摘ください

Page 28: php and sapi and zendengine2 and

End Of Slide

Let’s enjoy PHP hack life ;-)