Detailed explanation of Hugepage to make PHP7 faster
PHP7 has just released RC4, which contains some bug fixes and one of our latest performance improvements (NEWS), which is "HugePageFy PHP TEXT segment". By enabling this feature, PHP7 will "Move" its own TEXT segment (execution body) to Huagepage. In previous tests, we can steadily see a 2% to 3% QPS increase 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 lookup, the CPU Table procedures will all have built-in TLB (Translation Lookaside Buffer). Obviously, 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 can enable large memory pages, we 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 we mainly explain how to enable this new feature, thereby bringing obvious performance promote.
It has become very easy to enable Hugepage in 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 this off)
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 built 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 us configure OS, allocate some Hugepages:
$ sudo sysctl vm.nr_hugepages=128 vm.nr_hugepages = 128
Now let us 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 does take effect, we might as well Close opcache.huge_code_pages, then start it again and check 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 turning on huge_code_pages, fpm uses 4 more pages after starting. 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
It can be seen that the text segment has a size of 10114565 bytes, which requires a total of about 4.8 pages of 2M. After taking into account the alignment (the tail part less than 2M Page will not be moved), apply for 4 pages, which is exactly what we see. consistent.
Indicates that the configuration is successful! Enjoy
But I have to say it first. After enabling this feature, a problem will occur. If you try to profile through Perf report/anno, you will find that symbols are lost (valgrind, gdb are not affected). The main reason is that Perf is designed to monitor mmap, then record the address range, and convert IP to symbol. However, currently HugeTLB only supports MAP_ANON, so Perf thinks that this part of the address has no symbol information. I hope that future versions of Kernel can fix this limitation...
Recommended tutorial: "PHP7"
The above is the detailed content of Detailed explanation of Hugepage to make PHP7 faster. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
