id:koyhogeããã®PHPのコードキャッシュがなぜ速いのか - Blog::koyhoge::Techããã(å æ¥ã¯ãããã¨ããããã¾ãã m(__)m)
ãPHPã¯å é¨ã§stat()ãå¼ã³ã¾ãã£ã¦ããã®ã§é ããã¨ã®ãã¨ã§ãã®ã§ãinclude_pathã®å¾ãã®æ¹ã®ãã£ã¬ã¯ããªã«ããã¹ã¯ãªãããã¡ã¤ã«ã大éã«requireãã¦ãããããã¨ãããã ãã§ã³ã¼ããã£ãã·ã¥ã®ã¡ãªãããåºã¦ããã®ããããã¾ããã
PHPのコードキャッシュがなぜ速いのか - Blog::koyhoge::Tech
ã©ã®ã¬ãã«ãããã£ãã·ã¥ãå©ç¨ãããã®ãã¯ãã¡ãã£ã¨è¨æ¶ãç¡ãã®ã§ãã³ã¼ããèªã¿ç´ããªãã¨ãã¡ãªã®ã§ããããstat()ãå¼ã³ã¾ãã£ã¦é
ããã®stat()ãççºçã«å¼ã°ããåãã軽ã説æï¼ãããã¨ãããã¾ãã(æ©ãå¯ãªããã¨é©å½ã«æ¸ãã¦ãã®ã§ã説æã«ãªã£ã¦ãªãããã¼ã¼ï¼ããããããããããã®ãã¾ããããªãã®ã§ãã)
statã³ã¼ã«ãã©ã®ãããªæãã«å©ããã¦ãããã¯ãstraceãªãããå©ãã¦ã¿ãã°åããã¾ãã
ãã¹ãã³ã¼ã
ä¾ãã°ã以ä¸ã®ããã«ãPEARãã£ã¬ã¯ããªã«ãããSystem.phpãrequireããã ãã®ã³ã¼ããæ¸ããã¨ãã¾ãã(System.phpã¯ãPEAR.php 㨠Console/Getopt.php ãrequireãã¾ãã)
<?php require "System.php"; ?>
include_path 3ã¤ç®ã§è¦ã¤ãããã¿ã¼ã³
ããã§ãincludeãã¹ã ".:/tmp:/usr/local/lib/php" ã¨ããã¨ãã®åããã¿ã¦ã¿ã¾ãã3ã¤ç®ã®ãã¹ã§ãã¡ã¤ã«ãè¦ã¤ããã¾ãã(ä»æ¥ãUbuntuã«å ¥ãã¦ã¿ãPHP v5.2.3 (cli)ã§opcodeãã£ãã·ã¥ã®ããããå ¥ã£ã¦ãªãç°å¢ã§æ¤è¨¼)
$ strace -etrace=file php -d include_path=".:/tmp:/usr/local/lib/php" test.php ... åæåã®openã³ã¼ã«ããã ãã ã¨ç¶ã ... open("test.php", O_RDONLY) = 3 getcwd("/home/stattest/tmp", 4096) = 19 lstat64("/home", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest/tmp", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest/tmp/test.php", {st_mode=S_IFREG|0644, st_size=114, ...}) = 0 # ããããSystem.phpæ¢ã getcwd("/home/stattest/tmp", 4096) = 19 lstat64("/home", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest/tmp", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest/tmp/System.php", 0xbfe632ec) = -1 ENOENT (No such file or directory) open("/home/stattest/tmp/System.php", O_RDONLY) = -1 ENOENT (No such file or directory) lstat64("/tmp", {st_mode=S_IFDIR|S_ISVTX|0777, st_size=4096, ...}) = 0 lstat64("/tmp/System.php", 0xbfe632ec) = -1 ENOENT (No such file or directory) open("/tmp/System.php", O_RDONLY) = -1 ENOENT (No such file or directory) lstat64("/usr", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php/System.php", {st_mode=S_IFREG|0644, st_size=19727, ...}) = 0 open("/usr/local/lib/php/System.php", O_RDONLY) = 3 # PEAR.phpæ¢ã getcwd("/home/stattest/tmp", 4096) = 19 lstat64("/home", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest/tmp", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest/tmp/PEAR.php", 0xbfe6323c) = -1 ENOENT (No such file or directory) open("/home/stattest/tmp/PEAR.php", O_RDONLY) = -1 ENOENT (No such file or directory) lstat64("/tmp", {st_mode=S_IFDIR|S_ISVTX|0777, st_size=4096, ...}) = 0 lstat64("/tmp/PEAR.php", 0xbfe6323c) = -1 ENOENT (No such file or directory) open("/tmp/PEAR.php", O_RDONLY) = -1 ENOENT (No such file or directory) lstat64("/usr", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php/PEAR.php", {st_mode=S_IFREG|0644, st_size=34557, ...}) = 0 open("/usr/local/lib/php/PEAR.php", O_RDONLY) = 3 # Console/GetOpt.phpæ¢ã getcwd("/home/stattest/tmp", 4096) = 19 lstat64("/home", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest/tmp", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest/tmp/Console", 0xbfe6323c) = -1 ENOENT (No such file or directory) open("/home/stattest/tmp/Console/Getopt.php", O_RDONLY) = -1 ENOENT (No such file or directory) lstat64("/tmp", {st_mode=S_IFDIR|S_ISVTX|0777, st_size=4096, ...}) = 0 lstat64("/tmp/Console", 0xbfe6323c) = -1 ENOENT (No such file or directory) open("/tmp/Console/Getopt.php", O_RDONLY) = -1 ENOENT (No such file or directory) lstat64("/usr", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php/Console", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php/Console/Getopt.php", {st_mode=S_IFREG|0644, st_size=10447, ...}) = 0 open("/usr/local/lib/php/Console/Getopt.php", O_RDONLY) = 3 // PEAR.phpãåæ¢ç´¢ (Console/GetOpt.phpãrequireãã¦ãã®ã§) getcwd("/home/stattest/tmp", 4096) = 19 open("/home/stattest/tmp/PEAR.php", O_RDONLY) = -1 ENOENT (No such file or directory) open("/tmp/PEAR.php", O_RDONLY) = -1 ENOENT (No such file or directory) open("/usr/local/lib/php/PEAR.php", O_RDONLY) = 3 Process 30416 detached
åæ¢ç´¢ã§ãgetcwdãå©ããã¦ãã®ã¯ãinclude_pathã®1ã¤ç®ã§æå®ãã¦ãã "." ãå±éããããã§ãã
æå¾ã®åæ¢ç´¢ã ãã¯ã©ãããã£ãã·ã¥ãããã¦ããããã§ãstatã³ã¼ã«ãçç¥ããã¦ã¾ããPHP5ã§ã¯ãrealpathã®çµæããã£ãã·ã¥ããããã«ãªã£ãã¨ãããã¨ã§ã4ããè¯ããªã£ãã¨ãã話ãèãããã¨ãããã¾ãã(ãããµãã§ãã) ããã®ãã¨ï¼
include_pathã®1ã¤ç®ã§è¦ã¤ãããã¿ã¼ã³
ããã§ãincludeãã¹ã "/usr/local/lib/php:/tmp:." ã¨ããã¨ãã®åããã¿ã¦ã¿ã¾ãã1ã¤ç®ã®ãã¹ã§ãã¡ã¤ã«ãè¦ã¤ããããã«ãªãã¾ãã
$ strace -etrace=file -a 90 php -d include_path="/usr/local/lib/php:/tmp:." test.php ... open("test.php", O_RDONLY) = 3 getcwd("/home/stattest/tmp", 4096) = 19 lstat64("/home", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest/tmp", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/home/stattest/tmp/test.php", {st_mode=S_IFREG|0644, st_size=114, ...}) = 0 # ããããSystem.phpæ¢ã lstat64("/usr", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php/System.php", {st_mode=S_IFREG|0644, st_size=19727, ...}) = 0 open("/usr/local/lib/php/System.php", O_RDONLY) = 3 # PEAR.phpæ¢ã lstat64("/usr", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php/PEAR.php", {st_mode=S_IFREG|0644, st_size=34557, ...}) = 0 open("/usr/local/lib/php/PEAR.php", O_RDONLY) = 3 # Console/GetOpt.phpæ¢ã lstat64("/usr", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php/Console", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 lstat64("/usr/local/lib/php/Console/Getopt.php", {st_mode=S_IFREG|0644, st_size=10447, ...}) = 0 open("/usr/local/lib/php/Console/Getopt.php", O_RDONLY) = 3 // PEAR.phpãåæ¢ç´¢ (Console/GetOpt.phpãrequireãã¦ãã®ã§) open("/usr/local/lib/php/PEAR.php", O_RDONLY) = 3 Process 30597 detached
ã ãã¶ãçããªã£ã¦ããã®ãåããã¨æãã¾ããPEARã®ãã£ã¬ã¯ããªã®çµ¶å¯¾ãã¹ããã£ã¨çãã¨ããã«ããã°ãè¦ã¦åããããã«ããã£ã¨æ°ãæ¸ãã¾ãã(ã ããã¨ãã£ã¦ããã¹ãçããããããªãã¨ã¯å«ã§ãã)
大ã¾ããªåã
- include_pathã®å é ãããè¦ã¤ããã¾ã§ statã³ã¼ã«ã¨openãããç¶ããã
- ã·ã³ããªãã¯ãªã³ã¯æ»æã¨ãã£ã¬ã¯ããªãã©ãã¼ã¹æ»æãé²ãããã«ããã¹ãå±éãã expand_filepath() ã¨ããé¢æ°ã§realpath()ãã¦ããã®ã§ããã¹ã®æ°ã ãstat()ãèµ°ã
- ãã®å±éãããã¹ã§ãopen_basedirãæååæ¯è¼ãã¦ãã¾ãã
- åè) http://www.linux.or.jp/JM/html/LDP_man-pages/man3/realpath.3.html, IPA ISEC セキュア・プログラミング講座 - 7-1. シンボリックリンクの悪用
- ã§ããã®realpathããã®ãå¤ãã¦ãã¾ãã®ããhttp://papelipe.no/tags/php/optimizing_php_for_intel_based_macã®è©±
- ãã®æåã¯ãinclude/requireã ãã§ãªããfopenãªã©ãå«ããã¡ã¤ã«ãéãç³»å ¨è¬ã§çºçãã¾ãã