PHP opens opcache

Time:2019-5-10

Optimization plan

(1) Enabling Zend Opcache is very simple, adding in the PHP.ini configuration file:

zend_extension=opcache.so 
opcache.enable=1 
opcache.enable_cli=1

(2) Using compilers above GCC4.8 to compile installation packages, only PHP compiled above GCC4.8 will enable Global Register for opline and execute_data support.

(3) Open HugePage support, first open HugePages in the system, and then open Opcache’s hugecode pages.

On HugePage

The default memory of the operating system is paginated at 4KB, while the virtual address and memory address need to be converted, and this conversion needs to look up tables. CPU will build TLB (Translation Lookaside Buffer) in order to speed up the process of looking up tables. Obviously, the smaller the virtual page, the more entries in the table, and the limited TLB size, the more entries TLB Cache Miss will be higher, so if we can enable large memory pages, we can indirectly reduce the TLB Cache Miss.

Opening method

Take CentOS 6.5 for example, by command:

sudo sysctl vm.nr_hugepages=128

Allocate 128 reserved large pages of memory.

$ cat /proc/meminfo | grep Huge 
AnonHugePages:    444416 kB 
HugePages_Total:     128 
HugePages_Free:      128 
HugePages_Rsvd:        0 
HugePages_Surp:        0 
Hugepagesize:       2048 kB

Then add in PHP.ini

opcache.huge_code_pages=1