Hugepage
PHP7¸Õ¸Õ*¢²¼ÁËRC4, °uº¬Ò»Ð(C)bugÐÞ¸´ºÍÒ»¸oÎÒÃÇ×iеÄÐÔÄÜÌaÉý³É¹u£¬ ÄǾÍÊÇ¡±HugePageFy PHP TEXT segment¡±£¬ ͨ¹ýÆoÓÃÕa¸oÌØÐÔ£¬PHP7»a°Ñ×ÔÉiµÄTEXT¶Î£¨Ö´ÐÐÌa£(C)¡±Å²¡°µ½HuagepageÉÏ£¬Ö®Ç°µÄ²aÊÔ£¬ÎÒÃÇÄÜÎȶ¨µÄÔÚWordpressÉÏ¿´µ½2%~3%µÄQPSÌaÉý¡£
¹ØÓÚHugepageÊÇɶ£¬¼oµ¥µÄ˵ϾÍÊÇĬÈϵÄÄÚ´aeÊÇÒÔ4KBÖÒ³µÄ£¬¶øÐeÄaµØÖºÍÄÚ´aeµØÖ*ÊÇÐeҪת»»µÄ£¬ ¶øÕa¸oת»»ÊÇÒª²e±iµÄ£¬CPUΪÁ˼ÓËÙÕa¸o²e±i¹ý³Ì¶¼»aÄÚ½¨TLB£¨Translation Lookaside Buffer£(C)£¬ ÏÔ¶øÒ×¼uÈç¹uÐeÄaҳԽС£¬±iÀiµÄÌoÄ¿ÊýÒ²¾ÍÔ½¶a£¬¶øTLB´oСÊÇÓÐÏ޵ģ¬ÌoÄ¿ÊýÔ½¶aTLBµÄCache MissÒ²¾Í»aÔ½¸ß£¬ ËuÒÔÈç¹uÎÒÃÇÄÜÆoÓôoÄÚ´aeÒ³¾ÍÄܼa½Ó½µµÍÕa¸oTLB Cache Miss£¬ÖÁÓÚÏeϸµÄ½eÉÜ£¬GoogleÒ»ËÑÒ»´o¶ÑÎҾͲ»×¸ÊoÁË£¬ÕaÀiÖ÷Ҫ˵Ã÷ÏÂÈçºÎÆoÓÃÕa¸oÐÂÌØÐÔ£¬ ´Ó¶ø´øÀ´Ã÷ÏÔµÄÐÔÄÜÌaÉý¡£
еÄKernelÆoÓÃHugepageÒѾ±aµÃdz£ÈÝÒ×ÁË£¬ÒÔÎҵĿª¢ÐeÄa»uΪÀý(Ubuntu Server 14.04£¬Kernel 3.13.0-45), Èç¹uÎÒÃDze¿´ÄÚ´aeÐÅÏ¢£º
$ cat /proc/meminfo | grep Huge
AnonHugePages: 444416 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
¿É¼uÒ»¸oHugepageµÄsizeÊÇ2MB, ¶øµ±Ç°²¢Ã»ÓÐÆoÓÃHugePages. ÏÖÔÚÈÃÎÒÃÇÏȱaÒePHP RC4, ¼ÇµÃÒ»¶¨²»Òª¼Ó£º ¨Cdisable-huge-code-pages £¨Õa¸oÐÂÌØÐÔÊÇĬÈÏÆoÓõģ¬ Äa¼ÓÁËÕa¸o¾Í¹ØÁË£(C)
È»ºoÅaÖÃopcache, ´ÓPHP5.5¿ªÊ¼OpcacheÒѾÊÇĬÈÏÆoÓñaÒeµÄ£¬µ«ÊÇÊDZaÒe¶¯Ì¬¿aµÄ£¬ ËuÒÔÎÒÃÇ»¹ÊÇÒªÔÚphp.iniÖÐÅaÖüÓÔØÏ¡£
zend_extension=opcache.so
Õa¸oÐÂÌØÐÔÊÇ×oÔÚOpcacheÀiµÄ£¬ËuÒÔҲҪͨ¹ýOpcacheÆoÓÃÕa¸oÌØÐÔ£¨Í¨¹ýÉeÖÃopcache.huge_code_pages=1£(C)£¬ ¾ßÌaµÄÅaÖãº
opcache.huge_code_pages=1
ÏÖÔÚÈÃÎÒÃÇÅaÖÃOS£¬ *ÖÅaÒ»Ð(C)Hugepages£º
$ sudo sysctl vm.nr_hugepages=128
vm.nr_hugepages = 128
ÏÖÔÚÈÃÎÒÃÇÔٴμi²eÄÚ´aeÐÅÏ¢:
$ cat /proc/meminfo | grep Huge
AnonHugePages: 444416 kB
HugePages_Total: 128
HugePages_Free: 128
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
¿ÉÒÔ¿´µ½ÎÒÃÇ*ÖÅaµÄ128¸oHugepagesÒѾ¾ÍÐ÷ÁË£¬ È»ºoÎÒÃÇÀ´Æo¶¯php-fpm:
$ /home/huixinchen/local/php7/sbin/php-fpm
[01-Oct-2015 09:33:27] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[01-Oct-2015 09:33:27] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
ÏÖÔÚ£¬ Ôٴμi²eÄÚ´aeÐÅÏ¢:
$ cat /proc/meminfo | grep Huge
AnonHugePages: 411648 kB
HugePages_Total: 128
HugePages_Free: 113
HugePages_Rsvd: 27
HugePages_Surp: 0
Hugepagesize: 2048 kB
˵µ½ÕaÀi£¬Èç¹uHugepages¿ÉÓ㬠ÆaʵOpcacheÒ²»aÓÃHugepagesÀ´´ae´¢opcodes»º´ae£¬ ËuÒÔΪÁËÑeÖ¤opcache.huge_code_pagesÈʵÉuЧ£¬ ÎÒÃDz»Á¹Ø±Õopcache.huge_code_pages, È»ºoÔÙÆo¶¯Ò»´Îºo¿´ÄÚ´aeÐÅÏ¢£º
$ cat /proc/meminfo | grep Huge
AnonHugePages: 436224 kB
HugePages_Total: 128
HugePages_Free: 117
HugePages_Rsvd: 27
HugePages_Surp: 0
Hugepagesize: 2048 kB
¿É¼u¿ªÆoÁËhuge_code_pagesÒÔºo£¬ fpmÆo¶¯ºo¶aÓÃÁË4¸opages, ÏÖÔÚÎÒÃǼi²eÏÂphp-fpmµÄtext´oС£º
$ size /home/huixinchen/local/php7/sbin/php-fpm
text data bss dec hex filename
10114565 695200 131528 10941293 a6f36d /home/huixinchen/local/php7/sbin/php-fpm
¿É¼utext¶ÎÓÐ10114565¸o×Ö½Ú´oС£¬ ×ܹ²ÐeÒªÕ¼ÓÃ4.8¸o×oÓÒµÄ2MµÄpages£¬ ¿¼Âǵ½¶ÔÆeÒÔºo£¨Î²²¿²»×a2M Page²¿Ö²»Å²¶¯£(C)£¬ ÉeÇe4¸opages£¬ ÕýºÃºÍÎÒÃÇ¿´µ½µÄÏau¡£
˵Ã÷ÅaÖóɹ¦£¡ Enjoy :)
µ«ÊÇÓÐÑÔÔÚÏÈ£¬ ÆoÓôËÌØÐÔÒÔºo£¬ »aÔi³ÉÒ»¸oÎÊÌa¾ÍÊÇÄaÈç¹u³¢ÊÔͨ¹ýPerf report/anno È¥profilingµÄʱºo£¬ »a¢ÏÖuºÅ¶ªÊ§(valgrind, gdb²»ÊÜÓ°Ïi), Õa¸oÖ÷ÒªÔÒoÊÇPerfµÄÉe¼Æ²ÉÓüaÌýÁËmmap£¬È»ºo¼Ç¼µØÖ*¶Î§£¬ ×oIPµ½uºÅµÄת»»£¬ µ«ÊÇÄ¿Ç°HugeTLBÖ»Ö§³ÖMAP_ANON£¬ ËuÒÔµ¼ÖÂPerfÈÏΪÕa²¿ÖµØÖûÓÐ*uºÅÐÅÏ¢£¬Ï£ÍuÒÔºo°ae±¾µÄKernel¿ÉÒÔÐÞ¸´Õa¸oÏÞÖÆ°É..
GCC PGO
PGOÕýÈçÃu×ÖËu˵(Profile Guided Optimization ÓÐÐËȤµÄ¿ÉÒÔGoogle), ËuÐeÒªÓÃÒ»Ð(C)ÓÃÀýÀ´»ñµÃ*´À¡, Ò²¾ÍÊÇ˵Õa¸oÓÅ»¯ÊÇÐeÒªºÍÒ»¸oÌض¨µÄ³¡¾°°o¶¨µÄ.
Äa¶ÔÒ»¸o³¡¾°µÄÓÅ»¯, Ò²ÐiÔÚÁiÍaÒ»¸o³¡¾°¾ÍÊÂÓeԸΥÁË. Ëu²»ÊÇÒ»¸oͨÓõÄÓÅ»¯. ËuÒÔÎÒÃDz»Äܼoµ¥µÄ¾Í°uº¬ÕaÐ(C)ÓÅ»¯, Ò²ÎÞ¨Ö±½Ó¢²¼PGO±aÒeºoµÄPHP7.
µ±È», ÎÒÃÇÕýÔÚ³¢ÊÔ´ÓPGOÕÒ³oÒ»Ð(C)¹²ÐÔµÄÓÅ»¯, È»ºoÊÖ¹¤Applyµ½PHP7ÉÏÈ¥, µ«Õa¸oºÜÃ÷ÏÔ²»ÄÜ×oµ½Õe¶ÔÒ»¸o³¡¾°µÄÌرðÓÅ»¯ËuÄÜ´iµ½µÄЧ¹u, ËuÒÔÎÒ¾o¶¨Ð´ÕaƪÎÄÕ¼oµ¥½eÉÜÏÂÔoôʹÓÃPGOÀ´±aÒePHP7, ÈÃÄa±aÒeµÄPHP7ÄÜÌرðµÄÈÃÄa×Ô¼ºµÄ¶ÀÁ¢µÄÓ¦ÓñaµÃ¸u¿i.
Ê×ÏÈ, Òª¾o¶¨µÄ¾ÍÊÇÄÃʲô³¡¾°È¥Feedback GCC, ÎÒÃÇÒ»°a¶¼»aÑ¡Ôñ: ÔÚÄaÒªÓÅ»¯µÄ³¡¾°ÖÐ: *ÃÎÊÁ¿×i´oµÄ, ºÄʱ×i¶aµÄ, ×ÊÔ´ÏuºÄ×iÖصÄÒ»¸oÒ³Ãae.
ÄÃWordpressΪÀý, ÎÒÃÇÑ¡ÔñWordpressµÄÊ×Ò³(ÒoΪÊ×Ò³ÍuÍuÊÇ*ÃÎÊÁ¿×i´oµÄ).
ÎÒÃÇÒÔÎҵĻuÆ÷ΪÀý:
Intel(R) Xeon(R) CPU X5687 @ 3.60GHz X 16(³¬Ïß³Ì),
48G Memory
php-fpm ²ÉÓù̶¨32¸oworker, opcache²ÉÓÃĬÈϵÄÅaÖÃ(Ò»¶¨Òª¼ÇµÃ¼ÓÔØopcache)
ÒÔwordpress 4.1ΪÓÅ»¯³¡¾°..
Ê×ÏÈÎÒÃÇÀ´²aÊÔÏÂÄ¿Ç°WPÔÚPHP7µÄÐÔÄÜ(ab -n 10000 -c 100):
$ ab -n 10000 -c 100 http://inf-dev-maybach.weibo.com:8000/wordpress/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking inf-dev-maybach.weibo.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: nginx/1.7.12
Server Hostname: inf-dev-maybach.weibo.com
Server Port: 8000
Document Path: /wordpress/
Document Length: 9048 bytes
Concurrency Level: 100
Time taken for tests: 8.957 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 92860000 bytes
HTML transferred: 90480000 bytes
Requests per second: 1116.48 [#/sec] (mean)
Time per request: 89.567 [ms] (mean)
Time per request: 0.896 [ms] (mean, across all concurrent requests)
Transfer rate: 10124.65 [Kbytes/sec] received
¿É¼uWordpress 4.1 Ä¿Ç°ÔÚÕa¸o»uÆ÷ÉÏ, Ê×Ò³µÄQPS¿ÉÒÔµ½1116.48. Ò²¾ÍÊÇÿÃeÖÓ¿ÉÒÔ´¦ÀiÕaô¶a¸o¶ÔÊ×Ò³µÄÇeÇo,
ÏÖÔÚ, ÈÃÎÒÃÇ¿ªÊ¼½ÌGCC, ÈÃËu±aÒe³oÅÜWordpress4.1¸u¿iµÄPHP7À´, Ê×ÏÈÒªÇoGCC 4.0ÒÔÉϵİae±¾, ²»¹ýÎÒ½¨Òe´o¼ÒʹÓÃGCC-4.8ÒÔÉϵİae±¾(ÏÖÔÚ¶¼GCC-5.1ÁË).
µÚÒ»²½, ×ÔÈ»ÊÇÏÂÔØPHP7µÄÔ´´uÂeÁË, È»ºo×o./configure. ÕaÐ(C)¶¼Ã»Ê²Ã´Çø±ð
½ÓÏÂÀ´¾ÍÊÇÓÐÇø±ðµÄµØ*½ÁË, ÎÒÃÇÒªÊ×ÏȵÚÒ»±e±aÒePHP7, ÈÃËuÉu³É»a²uÉuprofileÊý¾ÝµÄ¿ÉÖ´ÐÐÎļþ:
$ make prof-gen
×¢Òa, ÎÒÃÇÓõ½ÁËprof-gen²ÎÊý(Õa¸oÊÇPHP7µÄMakefileÌØÓеÄ, ²»Òª³¢ÊÔÔÚÆaËuÏiÄ¿ÉÏÒ²Õaô¸a¹þ :) )
È»ºo, ÈÃÎÒÃÇ¿ªÊ¼ÑµÁ*GCC:
$ sapi/cgi/php-cgi -T 100 /home/huixinchen/local/www/htdocs/wordpress/index.php >/dev/null
Ò²¾ÍÊÇÈÃphp-cgiÅÜ100±ewordpressµÄÊ×Ò³, ´Ó¶øÉu³ÉÒ»Ð(C)ÔÚÕa¸o¹ý³ÌÖеÄprofileÐÅÏ¢.
È»ºo, ÎÒÃÇ¿ªÊ¼µÚ¶þ´Î±aÒePHP7.
$ make prof-clean
$ make prof-use && make install
ºÃµÄ, ¾ÍÕaô¼oµ¥, PGO±aÒeÍe³ÉÁË, ÏÖÔÚÎÒÃÇ¿´¿´PGO±aÒeÒÔºoµÄPHP7µÄÐÔÄÜ:
$ ab -n10000 -c 100 http://inf-dev-maybach.weibo.com:8000/wordpress/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking inf-dev-maybach.weibo.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: nginx/1.7.12
Server Hostname: inf-dev-maybach.weibo.com
Server Port: 8000
Document Path: /wordpress/
Document Length: 9048 bytes
Concurrency Level: 100
Time taken for tests: 8.391 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 92860000 bytes
HTML transferred: 90480000 bytes
Requests per second: 1191.78 [#/sec] (mean)
Time per request: 83.908 [ms] (mean)
Time per request: 0.839 [ms] (mean, across all concurrent requests)
Transfer rate: 10807.45 [Kbytes/sec] received
ÏÖÔÚÿÃeÖÓ¿ÉÒÔ´¦Ài1191.78¸oQPSÁË, ÌaÉýÊÇ~7%. »¹²»Àµ¹þ(ß×, Äa²»ÊÇ˵10%ô? Ôoô³É7%ÁË? ºÇºÇ, ÕýÈçÎÒ֮ǰ˵¹ý, ÎÒÃdz¢ÊÔ*ÖÎoPGO¶¼×oÁËÐ(C)ʲôÓÅ»¯, È»ºo°ÑÒ»Ð(C)ͨÓõÄÓÅ»¯ÊÖ¹¤Applyµ½PHP7ÖÐ. ËuÒÔÒ²¾ÍÊÇ˵, ÄÇ~3%µÄ±È½ÏͨÓõÄÓÅ»¯ÒѾ°uº¬µ½ÁËPHP7ÀiÃaeÁË, µ±È»Õa¸o¹¤×÷»¹ÔÚ¼ÌÐø).
ÓÚÊǾÍÕaô¼oµ¥, ´o¼Ò¿ÉÒÔÓÃ×Ô¼ºµÄ²uƵľµa³¡¾°À´ÑµÁGCC, ¼oµ¥¼¸²½, »ñµÃÌaÉý, ºÎÀÖ¶ø²»ÎªÄØ