Kind of way dynamic library table using the resources of the program

During this time a little research to optimize program resources table, regarded with some success, in this record.

First some background to explain it: our server on the resource table in the shared memory. The main reason for this is that when the process does not need to re-build core and then pulled out again and resource table (main building resource table is to build a data structure of the index query, such as building a hash table is used to configure it according to HeroID inquiry hero species). Then, consider the possibility of deploying multiple processes on the same machine, so naturally think, whether there is a mechanism that allows multiple processes on a shared memory machine with a resource table?

A more intuitive idea is that multiple processes hung on the same piece of shared memory resource table. Doing so can really achieve the purpose of shared memory, but need to consider the circumstances of the resource table reload. This memory would do when writing reload operation, and we know that there will be a write once read many concurrency issues. This embodiment therefore still need to introduce a mechanism to deal with concurrency issues. Common example is a double buffer, after a good build-time switch to the new resource table memory. I learned some team is really doing, specifically not elaborate.

The way I think of is this: the use of code generation technology, the resource table complete guide to c ++ code. All data used in the query structure is through automatic code generation, and compile it statically constructed better. This memory can be loaded directly into the process runs, the process does not need to run as before in the resource table constructed from the service. Therefore, this memory does not need to manually put on the shared memory. After the core process and then pulled up to the table memory is a resource to be unloaded, and the next will be to trigger a page fault is loaded into memory again through use. In order to do shared memory on the same machine, it can be packaged into a resource table dynamic library. So this piece of work shared memory naturally pushed to the operating system to do it.

This approach has several advantages. I think the most important of which is that this is a one-time problem-solving approach (which I also mentioned in a previous blog: P). Most of the code table is substantially resources are generated automatically be a. And when a process to use some resources to the table, this dynamic library can be loaded directly. Even the dynamic loading of code can be written in the frame layer, the default is every process to load the resource table. Before we in other processes (in addition to other processes than the gamesvr, such as scenes process) if you want to use resource table is very troublesome: to be in a specified file definition to use to which resource table, and if the resource table of there are inter-dependent relationship, but also need to rely on the resource table is also configured to come. Now, we can all loaded resource table for all processes, and do not need to worry about the risks of memory growth.

Second, you do not need to experience a memory limit of the estimated resource table values. For example, the definition of an array of length 1000 to keep equipment configuration, when planning a more than 1000 students with equipment, it is necessary to manually modify the code to change this experience (since before the resource table is to put shared memory, requiring memory pre-allocated). Because the resource table is statically built, the size of the array can be fully automatic code generation do. Also, we do not need their own handwriting indexing code, all automatically generated by the tool. For example, there may be a configuration table skills according to the needs and Level ID inquiry now only need to specify the primary key table in the guide, you can automatically generate code these indexes.

Static build data structures Another advantage is theoretically possible to use a more optimal memory structure to store. In general, the resource table memory will not be reloaded after the modification, for this scenario we can design a Bi Haxi table faster, cheaper memory data structures to do the index. By way of example, minima perfect hash for each primary key generated hash function do not conflict. Since no dynamic expansion and therefore no need to use a hash table to store sparse (a compact array can). The basic idea is to push calculated by the runtime to compile, or even when pushed to the generated code.

There is the reload process is very simple, uninstall the old dynamic libraries to load new dynamic library can be. Since the resource table and hold a shared memory, so as not to reserve some memory space for the new content before it is needed, no need to consider the structure of the shared memory compatibility issues.

The last point is very simple to achieve. All add up to less than 1000 lines of code, including code written in python with a static code generation tools, and c ++ resource table management code and so on.

 

In the implementation process also encountered several interesting questions, here, too, along with the record about.

(1) For one thing, linux dynamic library is the only .text shared memory segment. And we are here to share the main data, how to do it? First thing to understand is that the reason why the dynamic library is not shared memory segment .data, because this is part of the data may modify (including structural writable data, as well as the relocation table, etc.) in the process of being loaded, it must be every a process maintains a private memory. But we do not have the revision of the data, and therefore can be shared memory. The solution is simple: use const array can be used to modify the resource table. Doing so this memory will be put in .rodata. And in the link stage, linker will merge all .rodata to .text section.

PS Check elf file through readelf -l command program header will be able to see which section is incorporated into the .text segment, which is incorporated into the .data section

 

(2) In addition, there is a question about how hot a dynamic library update. Before I always thought about the file directly to replace the library directory is not the problem (due to file reference count). But the boss pointed out, covered in linux using cp command actually change the content of the original document, not equivalent to delete the original directory entry in the directory, then create a new directory entry point to the new file content. When the principle is covered cp will modify the original file inode, not create a new inode (details can be seen here ). But here I feel more like a bug cp command: When the copy process should consider whether there are references to the original file, if you should consider creating a new inode, rather than rewriting the original inode (similar to when writing copy). But the problem may feel further in-depth look, is there any other concerns?

 

Guess you like

Origin www.cnblogs.com/adinosaur/p/11545948.html