A summary of the development of persistent storage modules in PHP _php tutorial

Source: Internet
Author: User
It is often found in projects that there is a need to load large, fixed formatting data, such as some skill data, items, etc. in PvP. These data are read-only data, and may be larger, at present there are about tens of thousands of complex data, if serialize, pure text has about 20M. Tried to put an array directly in the PHP file, the results found that require this file is time-consuming, it may take dozens of MS, and this time the IO is very heavy, because the need to load dozens of m of data into memory, and to investigate the SQLite, this thing is relatively reliable, but the problem is , such as write-action functions, are uncomfortable to use, and thus produce an idea of how to write an extension. So the toss-up journey began.

The first thought was to call the Zend_execute_script method directly in the Minit to load a PHP file and return a zval to store in the global variable. It turned out that it was a delusion at all. The reason is that PHP minit when the VM is not initialized, it is impossible for you to call the Zend_execute_script method, and this method will not return a zval, to get zval must from the eg to get, very troublesome.

So the change of thinking, try to use unserialize/serialize, the results found that php_var_unserialize in the Minit stage is sure to be able to invoke. So, call this method to get a zval, and then in the global variable, in the Get method to return the Zval. After writing, in the Test Cup with the discovery, as long as the call will be the core AH. Then look at the document, think for yourself, and finally found that all non-pealloc assigned variables are cleared in the Php_rshutdown_function function. As a result, normal data in the Minit phase has been lost to the request phase.

Then look at the document, found in PHP provides pealloc such functions to provide persistent data distribution. Then change the thinking, the global variables in the Hashtable with Pealloc to allocate, and hastable set to persistent (thank goodness PHP Hashtable also save code and VMS, so there is this feature). But the cup is php_unserialize will only return a zval, you simply can not control whether it is persistent. No way, only call zend_hash_copy to do it. After writing and then testing, found or core, this does not understand, why? Lunch at noon, suddenly thought, may be a shallow copy of the problem, Zend_hash_copy provides a copy function and I did not set it. Coupled with a deep copy of the function after the test, found that sure enough to use, very refreshing. Www.2cto.com

The next test found that memory usage can not tolerate, a 20m data file loaded into memory, need about 100m of memory, if there are 100 php-cgi process, then more 10G of memory, this can not tolerate. Then imagine that you can use shared memory to solve this problem, anyway, this part of the data as long as it can read the line. PHP-CGI's main process is responsible for minit operations, as long as the child process reads this part of the data. But the trouble is, PHP does not provide any user to maintain the memory interface, so only a function of a function of the steak.

A closer look at the PHP Hashtable implementation, found more complex, and the key to use the ReAlloc function, this is so people have no words, always can not I also write a memory management bar. Currently, only shared memory is used to implement a simple thread allocation memory function, which allocates space from the shared memory sequentially. But fortunately, this part of the function is resize function is not needed at all. Because my goal is to copy the Zval from Php_var_unserialize to shared memory, and size I obviously know. And there's no need for UpdateA functionality, because it's a brand new copy. Finally, after the discovery can be used, the memory usage rate has really dropped.

The next stress test, suddenly found that the core has begun, this can not tolerate it, why AH? According to the core file, it was found that the hashtable inside the RefCount dropped to 0. So various tests, found that the single-threaded case is OK, only multi-threaded large pressure will hang. So think of RefCount will be modified, and multi-threaded modification, it must be possible to be confused. What about that? There's no lock.

Later thought carefully, suddenly thought as long as I return to this zval in the top layer of the Zval RefCount modified to greater than the number of php-cgi process value, it will be changed even if there is no problem, because it will not change to 0. Then after revising the test, found that sure enough.

To this, the whole problem is basically solved. But there is another problem that will still be core when restarting php-cgi, because some of the variables that are being used are forced to write 0. In fact, the correct usage of shared memory is that a process to write, another process to read, but I use this application to share memory as an absolute address is used, so it is impossible to write in one place, read elsewhere, unless the second parameter in Shmat is modified to a fixed value, But this requires a good understanding of the process's address assignment, knowing which memory is impossible to use at all. But this should be OK, because the php-cgi process has a memory limit, so you should be able to find a piece within the php-cgi running process can not be used. But the specific situation has to be studied in detail next.

Author of the Cloud

http://www.bkjia.com/PHPjc/478501.html www.bkjia.com true http://www.bkjia.com/PHPjc/478501.html techarticle It is often found in projects that there is a need to load large, fixed formatting data, such as some skill data, items, etc. in PvP. These data are read-only data, and ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.