让你的PHP7更快之Hugepage用法分析,php7hugepage
让你的PHP7更快之Hugepage用法分析,php7hugepage
本文实例讲述了让你的PHP7更快之Hugepage用法。分享给大家供大家参考,具体如下:
PHP7刚刚发布了RC4, 包含一些bug修复和一个我们最新的性能提升成果(NEWS), 那就是”HugePageFy PHP TEXT segment”, 通过启用这个特性,PHP7会把自身的TEXT段(执行体)”挪“到Huagepage上,之前的测试,我们能稳定的在Wordpress上看到2%~3%的QPS提升。
关于Hugepage是啥,简单的说下就是默认的内存是以4KB分页的,而虚拟地址和内存地址是需要转换的, 而这个转换是要查表的,CPU为了加速这个查表过程都会内建TLB(Translation Lookaside Buffer), 显而易见如果虚拟页越小,表里的条目数也就越多,而TLB大小是有限的,条目数越多TLB的Cache Miss也就会越高, 所以如果我们能启用大内存页就能间接降低这个TLB Cache Miss,至于详细的介绍,Google一搜一大堆我就不赘述了,这里主要说明下如何启用这个新特性, 从而带来明显的性能提升。
新的Kernel启用Hugepage已经变得非常容易了,以我的开发虚拟机为例(Ubuntu Server 14.04,Kernel 3.13.0-45), 如果我们查看内存信息:
$ cat /proc/meminfo | grep Huge AnonHugePages: 444416 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB
可见一个Hugepage的size是2MB, 而当前并没有启用HugePages. 现在让我们先编译PHP RC4, 记得一定不要加: –disable-huge-code-pages (这个新特性是默认启用的, 你加了这个就关了)
然后配置opcache, 从PHP5.5开始Opcache已经是默认启用编译的,但是是编译动态库的, 所以我们还是要在php.ini中配置加载下。
zend_extension=opcache.so
这个新特性是做在Opcache里的,所以也要通过Opcache启用这个特性(通过设置opcache.huge_code_pages=1), 具体的配置:
opcache.huge_code_pages=1
现在让我们配置OS, 分配一些Hugepages:
$ sudo sysctl vm.nr_hugepages=128 vm.nr_hugepages = 128
现在让我们再次检查内存信息:
$ cat /proc/meminfo | grep Huge AnonHugePages: 444416 kB HugePages_Total: 128 HugePages_Free: 128 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB
可以看到我们分配的128个Hugepages已经就绪了, 然后我们来启动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
现在, 再次检查内存信息:
$ cat /proc/meminfo | grep Huge AnonHugePages: 411648 kB HugePages_Total: 128 HugePages_Free: 113 HugePages_Rsvd: 27 HugePages_Surp: 0 Hugepagesize: 2048 kB
说到这里,如果Hugepages可用, 其实Opcache也会用Hugepages来存储opcodes缓存, 所以为了验证opcache.huge_code_pages确实生效, 我们不妨关闭opcache.huge_code_pages, 然后再启动一次后看内存信息:
$ cat /proc/meminfo | grep Huge AnonHugePages: 436224 kB HugePages_Total: 128 HugePages_Free: 117 HugePages_Rsvd: 27 HugePages_Surp: 0 Hugepagesize: 2048 kB
可见开启了huge_code_pages以后, fpm启动后多用了4个pages, 现在我们检查下php-fpm的text大小:
$ 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可以修复这个限制吧..
最后: 性能对比测试的任务就留给大家来试试了, 欢迎评论你们看到的变化!
希望本文所述对大家php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Compared with PHP7, PHP8 has some advantages and improvements in terms of performance, new features and syntax improvements, type system, error handling and extensions. However, choosing which version to use depends on your specific needs and project circumstances. Detailed introduction: 1. Performance improvement, PHP8 introduces the Just-in-Time (JIT) compiler, which can improve the execution speed of the code; 2. New features and syntax improvements, PHP8 supports the declaration of named parameters and optional parameters, making functions Calling is more flexible; anonymous classes, type declarations of properties, etc. are introduced.

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...
