Home > Article > Backend Development > Use Huagepage and PGO to improve the execution performance of PHP7_php tips
Hugepage
PHP7 has just released RC4, which includes some bug fixes and one of our latest performance improvements, which is "HugePageFy PHP TEXT segment". By enabling this feature, PHP7 will "move" its own TEXT segment (execution body) On Huagepage, in previous tests, we could steadily see a 2% to 3% QPS improvement on WordPress.
Regarding what Hugepage is, to put it simply, the default memory is paged in 4KB, and the virtual address and the memory address need to be converted, and this conversion requires a table lookup. In order to speed up the table lookup process, the CPU will Built-in TLB (Translation Lookaside Buffer), it is obvious that if the virtual page is smaller, the number of entries in the table will be more, and the TLB size is limited, the more entries, the higher the Cache Miss of the TLB will be, so if we Enabling large memory pages can indirectly reduce this TLB Cache Miss. As for the detailed introduction, I will not go into details after searching a lot on Google. Here I mainly explain how to enable this new feature, which will bring significant performance improvements.
It has become very easy to enable Hugepage with the new Kernel. Taking my development virtual machine as an example (Ubuntu Server 14.04, Kernel 3.13.0-45), if we view the memory information:
$ cat /proc/meminfo | grep Huge
AnonHugePages: 444416 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB
It can be seen that the size of a Hugepage is 2MB, and HugePages are not currently enabled. Now let us compile PHP RC4 first, remember not to add: –disable-huge-code-pages (this new feature is enabled by default, you add Turn it off after this)
Then configure opcache. Starting from PHP5.5, Opcache has been enabled for compilation by default, but it is used to compile dynamic libraries, so we still need to configure and load it in php.ini.
zend_extension=opcache.so
This new feature is implemented in Opcache, so this feature must also be enabled through Opcache (by setting opcache.huge_code_pages=1), specific configuration:
opcache.huge_code_pages=1
Now let’s configure the OS and allocate some Hugepages:
$ sudo sysctl vm.nr_hugepages=128 vm.nr_hugepages = 128
Now let’s check the memory information again:
$ cat /proc/meminfo | grep Huge
AnonHugePages: 444416 kB HugePages_Total: 128 HugePages_Free: 128 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB
You can see that the 128 Hugepages we allocated are ready, and then we start 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
Now, check the memory information again:
$ cat /proc/meminfo | grep Huge
AnonHugePages: 411648 kB HugePages_Total: 128 HugePages_Free: 113 HugePages_Rsvd: 27 HugePages_Surp: 0 Hugepagesize: 2048 kB
Speaking of which, if Hugepages is available, Opcache will actually use Hugepages to store the opcodes cache, so in order to verify that opcache.huge_code_pages is indeed effective, we might as well close opcache.huge_code_pages, and then start it again to see the memory information:
$ cat /proc/meminfo | grep Huge
AnonHugePages: 436224 kB HugePages_Total: 128 HugePages_Free: 117 HugePages_Rsvd: 27 HugePages_Surp: 0 Hugepagesize: 2048 kB
It can be seen that after huge_code_pages is turned on, 4 more pages are used after fpm is started. Now let’s check the text size of php-fpm:
$ 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
可见text段有10114565个字节大小, 总共需要占用4.8个左右的2M的pages, 考虑到对齐以后(尾部不足2M Page部分不挪动), 申请4个pages, 正好和我们看到的相符。
说明配置成功! Enjoy :)
但是有言在先, 启用此特性以后, 会造成一个问题就是你如果尝试通过Perf report/anno 去profiling的时候, 会发现符号丢失(valgrind, gdb不受影响), 这个主要原因是Perf的设计采用监听了mmap,然后记录地址范围, 做IP到符号的转换, 但是目前HugeTLB只支持MAP_ANON, 所以导致Perf认为这部分地址没有符号信息,希望以后版本的Kernel可以修复这个限制吧..
GCC PGO
PGO正如名字所说(Profile Guided Optimization 有兴趣的可以Google), 他需要用一些用例来获得反馈, 也就是说这个优化是需要和一个特定的场景绑定的.
你对一个场景的优化, 也许在另外一个场景就事与愿违了. 它不是一个通用的优化. 所以我们不能简单的就包含这些优化, 也无法直接发布PGO编译后的PHP7.
当然, 我们正在尝试从PGO找出一些共性的优化, 然后手工Apply到PHP7上去, 但这个很明显不能做到针对一个场景的特别优化所能达到的效果, 所以我决定写这篇文章简单介绍下怎么使用PGO来编译PHP7, 让你编译的PHP7能特别的让你自己的独立的应用变得更快.
首先, 要决定的就是拿什么场景去Feedback GCC, 我们一般都会选择: 在你要优化的场景中: 访问量最大的, 耗时最多的, 资源消耗最重的一个页面.
拿Wordpress为例, 我们选择Wordpress的首页(因为首页往往是访问量最大的).
我们以我的机器为例:
Intel(R) Xeon(R) CPU X5687 @ 3.60GHz X 16(超线程),
48G Memory
php-fpm 采用固定32个worker, opcache采用默认的配置(一定要记得加载opcache)
以wordpress 4.1为优化场景..
首先我们来测试下目前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
可见Wordpress 4.1 目前在这个机器上, 首页的QPS可以到1116.48. 也就是每秒钟可以处理这么多个对首页的请求,
现在, 让我们开始教GCC, 让他编译出跑Wordpress4.1更快的PHP7来, 首先要求GCC 4.0以上的版本, 不过我建议大家使用GCC-4.8以上的版本(现在都GCC-5.1了).
第一步, 自然是下载PHP7的源代码了, 然后做./configure. 这些都没什么区别
接下来就是有区别的地方了, 我们要首先第一遍编译PHP7, 让它生成会产生profile数据的可执行文件:
$ make prof-gen
注意, 我们用到了prof-gen参数(这个是PHP7的Makefile特有的, 不要尝试在其他项目上也这么搞哈 :) )
然后, 让我们开始训练GCC:
$ sapi/cgi/php-cgi -T 100 /home/huixinchen/local/www/htdocs/wordpress/index.php >/dev/null
也就是让php-cgi跑100遍wordpress的首页, 从而生成一些在这个过程中的profile信息.
然后, 我们开始第二次编译PHP7.
$ make prof-clean $ make prof-use && make install
好的, 就这么简单, PGO编译完成了, 现在我们看看PGO编译以后的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
现在每秒钟可以处理1191.78个QPS了, 提升是~7%. 还不赖哈(咦, 你不是说10%么? 怎么成7%了? 呵呵, 正如我之前说过, 我们尝试分析PGO都做了些什么优化, 然后把一些通用的优化手工Apply到PHP7中. 所以也就是说, 那~3%的比较通用的优化已经包含到了PHP7里面了, 当然这个工作还在继续).
于是就这么简单, 大家可以用自己的产品的经典场景来训练GCC, 简单几步, 获得提升, 何乐而不为呢