CN116881256A - Data acquisition and update method, shared memory initialization method and related devices - Google Patents

Data acquisition and update method, shared memory initialization method and related devices Download PDF

Info

Publication number
CN116881256A
CN116881256A CN202310878696.7A CN202310878696A CN116881256A CN 116881256 A CN116881256 A CN 116881256A CN 202310878696 A CN202310878696 A CN 202310878696A CN 116881256 A CN116881256 A CN 116881256A
Authority
CN
China
Prior art keywords
hash table
target
shared memory
virtual address
bucket
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202310878696.7A
Other languages
Chinese (zh)
Inventor
李拓
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Qianxin Technology Group Co Ltd
Original Assignee
Qianxin Technology Group Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Qianxin Technology Group Co Ltd filed Critical Qianxin Technology Group Co Ltd
Priority to CN202310878696.7A priority Critical patent/CN116881256A/en
Publication of CN116881256A publication Critical patent/CN116881256A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/22Indexing; Data structures therefor; Storage structures
    • G06F16/2228Indexing structures
    • G06F16/2255Hash tables
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/23Updating
    • G06F16/2365Ensuring data consistency and integrity
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/245Query processing
    • G06F16/2455Query execution
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/54Interprogram communication
    • G06F9/544Buffers; Shared memory; Pipes
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Abstract

The application provides a data acquisition and update method, a shared memory initialization method and a related device, and relates to the technical field of computers. The data acquisition method comprises the following steps: acquiring a target key value, acquiring a virtual address corresponding to the target key value based on the target key value and a preset hash table, and acquiring target data stored in a hash table item corresponding to the virtual address; wherein the hash table is stored in the shared memory and is commonly used by a plurality of processes; each virtual address in the hash table uniquely corresponds to a hash table entry. Because the hash table is stored in the shared memory, the process sharing the shared memory can use the hash table, so that the sharing of the hash table is realized, and the hash tables are not required to be respectively established by different processes, thereby reducing the occupation of the memory. The hash tables stored in the shared memory are used by the processes, data synchronization among the hash tables is not needed, and system errors possibly caused by untimely synchronization of the hash tables are avoided.

Description

Data acquisition and update method, shared memory initialization method and related devices
Technical Field
The present application relates to the field of computer technologies, and in particular, to a data acquisition and update method, a shared memory initialization method, and a related device.
Background
The hash table is also called a hash table, and is a data structure for directly inquiring and accessing according to the value of the key (i.e. the target key value), and the key is directly corresponding to a certain position in the hash table through a mapping function (hash function), so that the searching speed is increased.
Modern operating systems manage in a virtual memory manner, so that the memory addresses accessed in the process are not actual physical memory addresses, but virtual addresses. The same pointer for a virtual address does not necessarily point to the same physical address in different processes. If the hash table of a process is applied to a designated process, the hash table established by the designated process may conflict with an external hash table, so that a situation that one virtual address corresponds to a plurality of physical addresses may occur in the designated process. Thus, it is difficult to share the hash table of one process to other processes.
To solve the above-mentioned problems, the prior art often adopts a method of creating copies of a hash table in different processes, that is, only one designated process creates and maintains the hash table, and the hash table created by the designated process is copied to other processes, so that the data stored in the hash table of the designated process is shared by a plurality of processes. When the hash table is updated, the designated process copies the updated hash table to other processes and overwrites the hash table before other processes. This approach wastes storage resources and may cause system errors if the hash tables are not synchronized in time.
Disclosure of Invention
The application provides a data acquisition and update method, a shared memory initialization method and a related device, which are used for solving the problems that in the prior art, in order to realize the sharing of a hash table, storage resources are wasted, and the synchronization of the hash table is not timely possible to cause system errors.
In a first aspect, the present application provides a data acquisition method, including: acquiring a target key value, acquiring a virtual address corresponding to the target key value based on the target key value and a preset hash table, and acquiring target data stored in a hash table item corresponding to the virtual address; wherein the hash table is stored in a shared memory and is commonly used by a plurality of processes; the hash table comprises a corresponding relation between a key value and a virtual address, and at least one hash table item, wherein each virtual address in the hash table uniquely corresponds to one hash table item.
In the embodiment of the application, because the hash table is stored in the shared memory, the process sharing the shared memory can use the hash table, so that the hash table can be shared, different processes are not required to respectively maintain the hash table, the occupation of the memory is reduced, and the storage resource waste is reduced. In addition, since the hash tables stored in the shared memory are used by the processes, data synchronization among the hash tables is not needed, and system errors caused by untimely synchronization of the hash tables can be avoided. Meanwhile, each virtual address in the hash table uniquely corresponds to one hash table item, and each hash table item is a storage space, namely, each hash table item uniquely corresponds to one physical address, so that the condition that one virtual address corresponds to a plurality of physical addresses can be avoided. That is, the above scheme of the embodiment of the present application can alleviate the problem of storage resource waste existing in the prior art on the basis of avoiding the occurrence of the situation that one virtual address corresponds to a plurality of physical addresses, and meanwhile, since a plurality of processes multiplex one hash table, there is no process of synchronization of the hash tables between a plurality of processes, so that the problem of system errors caused by untimely synchronization of the hash tables is avoided.
With reference to the foregoing technical solution provided in the first aspect, in some possible implementation manners, the hash table includes at least one hash table bucket and a plurality of hash table entries; the obtaining the virtual address corresponding to the target key value based on the target key value and a preset hash table, and obtaining the target data stored in the hash table item corresponding to the virtual address, includes: determining a corresponding target hash table bucket based on the target key value; wherein the target hash table bucket comprises a corresponding relation between a key value and a virtual address; determining a virtual address corresponding to the target key value based on the target hash table bucket; and acquiring the target data from the hash table item corresponding to the virtual address based on the virtual address.
In the embodiment of the application, the hash table bucket comprises the corresponding relation between the key value and the virtual address, so that the virtual address corresponding to the target key value can be found through the hash table bucket, and further the target data can be acquired from the corresponding hash table item by accessing the virtual address, thereby realizing accurate acquisition of the target data corresponding to the target key value.
With reference to the foregoing technical solution of the first aspect, in some possible implementation manners, the hash table bucket is provided with a spin lock, where the spin lock is stored in the shared memory, and after determining a corresponding target hash table bucket based on the target key value, the method further includes: determining that the target hash table bucket is not in a locked state.
In the embodiment of the application, the spin lock is arranged on the hash table barrel and stored in the shared memory, so that the spin lock can be shared by a plurality of processes (a plurality of processes sharing the hash table), and only one process of the hash table barrel can be used at the same time, thereby avoiding the condition that the hash table barrel is updated not timely and a system error occurs due to the simultaneous use of one hash table barrel by a plurality of processes.
With reference to the foregoing technical solution provided in the first aspect, in some possible implementation manners, after determining that the target hash table bucket is not in a locked state, the method further includes: setting the spin lock of the target hash table barrel to be in a locking state, and marking the target hash table barrel by using the identity of the current process; correspondingly, after the target data stored in the hash table entry corresponding to the virtual address is obtained, the method further includes: and releasing the locking state of the target hash table barrel, and clearing the mark of the target hash table barrel.
In the embodiment of the application, when the target hash table barrel is used, the target hash table barrel is set to be in a locking state and marked by the identity of the current process, so that the target hash table barrel can only be used by the current process. After the use is completed, the locking state of the target hash table bucket is released, and the mark of the target hash table bucket is cleared, so that other processes can normally use the target hash table bucket. Therefore, only one process can be used by one hash table barrel at the same time, and the situation that system errors occur due to the fact that a plurality of processes use one hash table barrel at the same time is avoided.
With reference to the foregoing technical solution provided in the first aspect, in some possible implementation manners, in a case where there is a process crash, the method further includes: acquiring an identity of a crash process; the crash process is at least one process of the plurality of processes; traversing all the hash table barrels, if the spin lock of the hash table barrel is in a locking state and the hash table barrel is marked by the identity of the collapse process, unlocking the locking state of the hash table barrel and clearing the mark of the hash table barrel.
In the embodiment of the application, because the hash table barrel is marked by the identity mark of the process when being used, the hash table barrel marked by the identity mark of the crashed process can be found by traversing the hash table barrel. By releasing the locking state of the hash table barrel and clearing the mark of the hash table barrel, the hash table barrel can continue to work normally without locking the hash table barrel due to process breakdown.
With reference to the foregoing technical solution of the first aspect, in some possible implementation manners, the hash table includes metadata, where the metadata is stored in a metadata area of the shared memory, the metadata includes a starting address and a total size of the shared memory, and before the obtaining the target key value, the method further includes: mapping the metadata area of the shared memory to a virtual address space of the current process; acquiring the starting address and the total size of the shared memory in the metadata; releasing the mapping relation of the metadata area of the shared memory to the virtual address space of the current process; and mapping the shared memory into an address space corresponding to the starting address based on the starting address and the total size of the shared memory, so that virtual addresses of any one region of the shared memory in different processes are the same.
In the embodiment of the application, because the metadata comprises the starting address and the total size of the shared memory, the process can acquire the metadata by mapping the metadata area to the virtual address space of the current process, and further can map the shared memory based on the starting address and the total size of the shared memory in the metadata. Because the starting address and the total size are the same, the virtual address of the shared memory after mapping can be ensured to be the same as the virtual address of the shared memory after mapping by other processes, and the condition that the same virtual address corresponds to a plurality of physical addresses is avoided.
With reference to the foregoing technical solution provided in the first aspect, in some possible implementation manners, if mapping the shared memory into an address space corresponding to the starting address fails based on the starting address and the total size of the shared memory, the method further includes: obtaining a target virtual address space which is not utilized and has a space larger than the shared memory; mapping the shared memory to the target virtual address space, and updating the starting address of the shared memory recorded in the metadata area, and updating the starting virtual address of the hash table entry storage area, the starting virtual address of the available table entry storage area and the starting virtual address of the hash table bucket storage area in the metadata; the hash table entry storage area is divided into a plurality of hash table entries of the hash table, the available table entry storage area is used for storing an available table entry stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table; and broadcasting notification information representing the re-mounting of the shared memory in the processes.
In the embodiment of the application, when mapping of the shared memory to the address space corresponding to the starting address fails, the current process re-maps the shared memory and updates metadata: the starting address of the shared memory, the starting virtual address of the hash table entry storage, the starting virtual address of the available table entry storage, and the starting virtual address of the hash table bucket storage. Therefore, a plurality of processes sharing the hash table can map the shared memory again based on the updated metadata, and virtual addresses corresponding to the shared memory in different processes are kept consistent.
With reference to the foregoing technical solution of the first aspect, in some possible implementation manners, the hash table occupies the shared memory.
With reference to the foregoing technical solution of the first aspect, in some possible implementation manners, different hash table entries are different storage areas that are pre-divided in the shared memory.
In a second aspect, the present application provides a data updating method, including: acquiring a target key value and a hash table updating instruction; updating target content in a preset hash table according to the hash table updating instruction; wherein, the target content is a hash table item and/or a hash table bucket corresponding to the target key value in the hash table; the hash table is stored in a shared memory, and is commonly used by a plurality of processes; the hash table comprises at least one hash table item and at least one hash table bucket, the hash table bucket comprises a corresponding relation between a key value and a virtual address, and each virtual address uniquely corresponds to one hash table item.
In the embodiment of the application, because the hash table is stored in the shared memory, the process sharing the shared memory can use the hash table, so that the hash table can be shared, different processes are not required to respectively maintain the hash table, the occupation of the memory is reduced, and the storage resource waste is reduced. In addition, since the hash tables stored in the shared memory are used by the plurality of processes, when the hash tables are updated, only the hash tables stored in the shared memory need to be updated, and data synchronization among the plurality of hash tables is not needed, so that the condition that system errors are caused by untimely synchronization of the hash tables can be avoided. Meanwhile, each virtual address in the hash table uniquely corresponds to one hash table item, and each hash table item is a storage space, namely, each hash table item uniquely corresponds to one physical address, so that the condition that one virtual address corresponds to a plurality of physical addresses can be avoided. That is, the above scheme of the embodiment of the present application can alleviate the problem of storage resource waste existing in the prior art on the basis of avoiding the occurrence of the situation that one virtual address corresponds to a plurality of physical addresses, and meanwhile, since a plurality of processes multiplex one hash table, there is no process of synchronization of the hash tables between a plurality of processes, so that the problem of system errors caused by untimely synchronization of the hash tables is avoided.
With reference to the foregoing technical solution provided in the second aspect, in some possible implementation manners, the hash table includes at least one hash table bucket, an available table entry stack, and a plurality of hash table entries, and the hash table update instruction includes a newly added shared data instruction; before updating the target content in the preset hash table according to the hash table updating instruction, the method further comprises the following steps: acquiring data to be shared; correspondingly, updating the target content in the preset hash table according to the hash table updating instruction comprises the following steps: responding to the acquired newly added shared data instruction, and determining a corresponding target hash table barrel based on the target key value; determining a virtual address of an allocatable hash table item from the available table item stack; wherein the available table item stack records virtual addresses of all allocatable hash table items, and the allocatable hash table items do not store data; and writing the corresponding relation between the virtual address of the allocatable hash table item and the target key value into the target hash table barrel, and storing the data to be shared in the hash table item.
In the embodiment of the application, the hash table bucket comprises the corresponding relation between the key value and the virtual address, so that the virtual address corresponding to the target key value can be searched through the hash table bucket, and the obtained data to be shared can be written into the hash table item corresponding to the virtual address. And writing the corresponding relation between the virtual address of the allocatable hash table item and the target key value into the target hash table barrel to finish the data updating of the hash table barrel, thereby realizing the accurate updating of the hash table barrel and the hash table item corresponding to the target key value.
With reference to the foregoing technical solution provided in the second aspect, in some possible implementation manners, the hash table includes at least one hash table bucket and a plurality of hash table entries, and the hash table update instruction includes a delete shared data instruction; updating target content in a preset hash table according to the hash table updating instruction, wherein the updating of the target content comprises the following steps: determining a corresponding target hash table bucket based on the target key value in response to the acquired hash table updating instruction; determining a virtual address corresponding to the target key value based on the target hash table bucket; deleting the data stored in the hash table item corresponding to the virtual address, and deleting the corresponding relation between the target key value and the virtual address from the target hash table bucket.
In the embodiment of the application, the hash table bucket comprises the corresponding relation between the key value and the virtual address, so that the virtual address corresponding to the target key value can be searched through the hash table bucket, and the data stored in the hash table item corresponding to the virtual address can be deleted. And deleting the correspondence between the virtual address of the allocatable hash table item and the target key value from the target hash table bucket, so that the hash table item corresponding to the target key value can be accurately deleted.
With reference to the foregoing technical solution provided in the second aspect, in some possible implementations, the hash table includes at least one hash table bucket and a plurality of hash table entries, and the hash table update instruction includes a data update instruction; before updating the target content in the preset hash table according to the hash table updating instruction, the method further comprises the following steps: acquiring data to be updated; correspondingly, updating the target content in the preset hash table according to the hash table updating instruction comprises the following steps: responding to the acquired data updating instruction, and determining a corresponding target hash table barrel based on the target key value; determining a virtual address corresponding to the target key value based on the target hash table bucket; and updating the data stored in the hash table item corresponding to the virtual address into the data to be updated.
In the embodiment of the application, the hash table bucket comprises the corresponding relation between the key value and the virtual address, so that the virtual address corresponding to the target key value can be found through the hash table bucket, and the obtained data to be updated can be written into the hash table item corresponding to the virtual address, thereby realizing accurate updating of the hash table item corresponding to the target key value.
With reference to the foregoing technical solution of the second aspect, in some possible implementation manners, determining a corresponding target hash table bucket based on the target key value includes: judging whether a hash table barrel corresponding to the target key value exists or not; if the target key value exists, taking a hash table bucket corresponding to the target key value as the target hash table bucket; if not, establishing a hash table bucket corresponding to the target key value, wherein the newly-established hash table bucket is the target hash table bucket.
With reference to the foregoing technical solution of the second aspect, in some possible implementation manners, the hash table bucket is provided with a spin lock, where the spin lock is stored in the shared memory, and after determining a corresponding target hash table bucket based on the target key value, the method further includes: determining that the target hash table bucket is not in a locked state.
With reference to the foregoing technical solution provided by the second aspect, in some possible implementation manners, after determining that the target hash table bucket is not in the locked state, the method further includes: setting the spin lock of the target hash table barrel to be in a locking state, and marking the target hash table barrel by using the identity of the current process; correspondingly, after performing the updating operation corresponding to the hash table updating instruction on the content corresponding to the target key value in the preset hash table to obtain the updated hash table, the method further includes: and releasing the locking state of the target hash table barrel, and clearing the mark of the target hash table barrel.
With reference to the foregoing technical solution provided by the second aspect, in some possible implementation manners, in a case where there is a process crash, the method further includes: acquiring an identity of a crash process; the crash process is at least one process of the plurality of processes; traversing all the hash table barrels, if the spin lock of the hash table barrel is in a locking state and the hash table barrel is marked by the identity of the collapse process, unlocking the locking state of the hash table barrel and clearing the mark of the hash table barrel.
With reference to the foregoing technical solution of the second aspect, in some possible implementation manners, the hash table includes metadata, where the metadata is stored in a metadata area of the shared memory, the metadata includes a starting address and a total size of the shared memory, and before the obtaining the target key value, the method further includes: mapping the metadata area of the shared memory to a virtual address space of the current process; acquiring the starting address and the total size of the shared memory in the metadata; releasing the mapping relation of the metadata area of the shared memory to the virtual address space of the current process; and mapping the shared memory into an address space corresponding to the starting address based on the starting address and the total size of the shared memory, so that virtual addresses of any one region of the shared memory in different processes are the same.
With reference to the foregoing technical solution provided in the second aspect, in some possible implementation manners, if mapping the shared memory into an address space corresponding to the starting address fails based on the starting address and the total size of the shared memory, the method further includes: obtaining a target virtual address space which is not utilized and has a space larger than the shared memory; mapping the shared memory to the target virtual address space, and updating the starting address of the shared memory recorded in the metadata area, and updating the starting virtual address of the hash table entry storage area, the starting virtual address of the available table entry storage area and the starting virtual address of the hash table bucket storage area in the metadata; the hash table entry storage area is divided into a plurality of hash table entries of the hash table, the available table entry storage area is used for storing an available table entry stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table; and broadcasting notification information representing the re-mounting of the shared memory in the processes.
In combination with the technical solution provided in the second aspect, in some possible implementations, the hash table occupies the shared memory.
With reference to the foregoing technical solution of the second aspect, in some possible implementation manners, different hash table entries are different storage areas that are pre-divided in the shared memory.
In a third aspect, the present application provides a method for initializing a shared memory, including: acquiring the size of a shared memory, wherein the shared memory comprises a metadata area, a hash table entry storage area, an available table entry storage area and a hash table bucket storage area; the metadata area is used for storing metadata of a hash table, the hash table item storage area is divided into a plurality of hash table items of the hash table, the available table item storage area is used for storing an available table item stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table; generating a starting address of the shared memory; mapping the shared memory to a virtual address space corresponding to the starting address; wherein each hash table entry corresponds to one virtual address one by one; and recording the starting address into the metadata, and updating the starting virtual address of the hash table item storage area, the starting virtual address of the available table item storage area and the starting virtual address of the hash table storage area in the metadata based on the respective sizes and sequences of the preset hash table item storage area, the available table item storage area and the hash table storage area.
In the embodiment of the application, the sizes and the sequences of the hash table item storage area, the available table item storage area and the hash table storage area are preset, so that after the shared memory is mapped into the virtual address space corresponding to the initial address, the initial virtual address of the hash table item storage area, the initial virtual address of the available table item storage area and the initial virtual address of the hash table storage area can be generated, and each hash table item corresponds to one virtual address one by one, thereby avoiding the condition that one virtual address corresponds to a plurality of physical addresses. In addition, because the hash table is stored in the shared memory, the process sharing the shared memory can use the hash table, so that the hash table can be shared, different processes are not required to respectively maintain the hash table, the occupation of the memory is reduced, and the storage resource waste is reduced. That is, the above scheme of the embodiment of the present application can alleviate the problem of storage resource waste existing in the prior art on the basis of avoiding the occurrence of the situation that one virtual address corresponds to a plurality of physical addresses, and meanwhile, since a plurality of processes multiplex one hash table, there is no process of synchronization of the hash tables between a plurality of processes, so that the problem of system errors caused by untimely synchronization of the hash tables is avoided.
With reference to the foregoing technical solution of the third aspect, in some possible implementation manners, the generating the starting address of the shared memory includes: generating a starting virtual address of the shared memory; and obtaining the starting address based on the starting virtual address of the shared memory, the size of the shared memory and a preset value.
In the embodiment of the application, the starting address is obtained through the starting virtual address of the shared memory, the size of the shared memory and the preset value, so that the acquisition principle of the starting address is different from that of the conventional virtual address allocation mode, and therefore, the probability that the starting address is used by other processes can be reduced, and the situation that the same virtual address corresponds to a plurality of physical addresses is avoided.
In a fourth aspect, the present application provides a data acquisition apparatus comprising: the acquisition module is used for acquiring the target key value; the processing module is used for obtaining a virtual address corresponding to the target key value based on the target key value and a preset hash table, and obtaining target data stored in a hash table item corresponding to the virtual address; wherein the hash table is stored in a shared memory and is commonly used by a plurality of processes; the hash table comprises a corresponding relation between a key value and a virtual address, and at least one hash table item, wherein each virtual address in the hash table uniquely corresponds to one hash table item.
In a fifth aspect, the present application provides a data updating apparatus comprising: the acquisition module is used for acquiring the target key value and the hash table updating instruction; the processing module is used for updating target contents in a preset hash table according to the hash table updating instruction; wherein, the target content is a hash table item and/or a hash table bucket corresponding to the target key value in the hash table; the hash table is stored in a shared memory, and is commonly used by a plurality of processes; the hash table comprises at least one hash table item and at least one hash table bucket, the hash table bucket comprises a corresponding relation between a key value and a virtual address, and each virtual address uniquely corresponds to one hash table item.
In a sixth aspect, the present application provides a shared memory initializing device, including: the system comprises an acquisition module, a generation module, a mapping module and a processing module, wherein the acquisition module is used for acquiring the size of a shared memory, and the shared memory comprises a metadata area, a hash table entry storage area, an available table entry storage area and a hash table bucket storage area; the metadata area is used for storing metadata of a hash table, the hash table item storage area is used for storing a plurality of hash table items of the hash table, the available table item storage area is used for storing an available table item stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table; the generation module is used for generating the starting address of the shared memory; the mapping module is used for mapping the shared memory to a virtual address space corresponding to the initial address; wherein each hash table entry corresponds to one virtual address one by one; the processing module is used for recording the starting address into the metadata, and updating the starting virtual address of the hash table item storage area, the starting virtual address of the available table item storage area and the starting virtual address of the hash table storage area in the metadata based on the size and the sequence of the preset hash table item storage area, the available table item storage area and the hash table storage area.
In a seventh aspect, the present application provides an electronic device, comprising: the device comprises a memory and a processor, wherein the memory is connected with the processor; the memory is used for storing programs; the processor is configured to invoke a program stored in the memory to perform the first aspect, the second aspect, the third aspect, and/or the method described in any possible implementation manner of the first aspect, the second aspect, and the third aspect.
In an eighth aspect, the present application provides a computer readable storage medium having stored thereon a computer program which, when executed by a computer, performs the method of the first aspect, the second aspect, the third aspect, and/or any one of the possible implementation manners of the first aspect, the second aspect, and the third aspect.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings that are needed in the embodiments will be briefly described below, it being understood that the following drawings only illustrate some embodiments of the present application and therefore should not be considered as limiting the scope, and other related drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
Fig. 1 is a flow chart of a data acquisition method according to an embodiment of the present application;
FIG. 2 is a flow chart of a data update method according to an embodiment of the present application;
FIG. 3 is a flow chart illustrating a method for initializing a shared memory according to an embodiment of the present application;
FIG. 4 is a block diagram of a data acquisition device according to an embodiment of the present application;
FIG. 5 is a block diagram showing a data updating apparatus according to an embodiment of the present application;
FIG. 6 is a block diagram illustrating a shared memory initializing device according to an embodiment of the present application;
fig. 7 is a block diagram of an electronic device according to an embodiment of the present application.
Detailed Description
The technical solutions in the embodiments of the present application will be described below with reference to the accompanying drawings in the embodiments of the present application.
It should be noted that: like reference numerals and letters denote like items in the following figures, and thus once an item is defined in one figure, no further definition or explanation thereof is necessary in the following figures. Meanwhile, relational terms such as "first," "second," and the like may be used solely to distinguish one entity or action from another entity or action in the description of the application without necessarily requiring or implying any actual such relationship or order between such entities or actions. Moreover, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
Furthermore, the term "and/or" in the present application is merely an association relationship describing the association object, and indicates that three relationships may exist, for example, a and/or B may indicate: a exists alone, A and B exist together, and B exists alone.
The technical scheme of the present application will be described in detail with reference to the accompanying drawings.
Referring to fig. 1, fig. 1 is a flowchart of a data acquisition method according to the present application, and the steps included in the method are described below with reference to fig. 1.
S100: and obtaining a target key value.
The target key value is a key value, and may be input by a user, or may be carried in a control instruction sent by upper software received by the current process. The specific manner of obtaining the target key value is not limited here.
S200: and obtaining a virtual address corresponding to the target key value based on the target key value and a preset hash table, and obtaining target data stored in a hash table item corresponding to the virtual address.
Wherein the hash table is stored in the shared memory and is commonly used by a plurality of processes; each virtual address in the hash table uniquely corresponds to a hash table entry.
Because the hash table is stored in the shared memory, the process sharing the shared memory can use the hash table, so that the sharing of the hash table is realized, different processes are not required to respectively maintain the hash table, the occupation of the memory is reduced, and the storage resource waste is reduced. In addition, since the hash tables stored in the shared memory are used by the processes, data synchronization among the hash tables is not needed, and system errors caused by untimely synchronization of the hash tables can be avoided. Meanwhile, each virtual address in the hash table uniquely corresponds to one hash table item, and each hash table item is a storage space, namely, each hash table item uniquely corresponds to one physical address, so that the condition that one virtual address corresponds to a plurality of physical addresses can be avoided. That is, the above scheme of the embodiment of the present application can alleviate the problem of storage resource waste existing in the prior art on the basis of avoiding the occurrence of the situation that one virtual address corresponds to a plurality of physical addresses, and meanwhile, since a plurality of processes multiplex one hash table, there is no process of synchronization of the hash tables between a plurality of processes, so that the problem of system errors caused by untimely synchronization of the hash tables is avoided.
In one embodiment, a hash table may include at least one hash table bucket and a plurality of hash table entries. In this case, based on the target key value and the preset hash table, the specific process of obtaining the virtual address corresponding to the target key value and obtaining the target data stored in the hash table item corresponding to the virtual address may be: the method comprises the steps of firstly determining a corresponding target hash table bucket based on a target key value, then determining a virtual address corresponding to the target key value based on the target hash table bucket, and finally obtaining target data from a hash table item corresponding to the virtual address based on the virtual address. The target hash table bucket comprises a corresponding relation between a key value and a virtual address.
For example, when the target key value is a key value 1, the hash table bucket corresponding to the key value 1 includes a correspondence between the key value 1 and the virtual address 1, and a correspondence between the key value 2 and the virtual address 2. Then after determining the hash table bucket corresponding to key 1, the virtual address corresponding to key 1 may be determined to be virtual address 1. Then accessing the virtual address 1 to find out the hash table item pointed by the virtual address, and finally obtaining the data stored in the hash table item to obtain the target data. The examples herein are for ease of understanding only and should not be construed as limiting the application.
Alternatively, the specific manner of determining the corresponding target hash table bucket based on the target key value may be that the hash value corresponding to the target key value is calculated first, and then the position of the target hash table bucket is obtained based on the hash value.
For example, a remainder is first calculated for a preset hash table bucket length and the hash value, where the hash value is a dividend and the hash table bucket length is a divisor. The remainder obtained by calculation is the position mark of the hash table barrel. Then the address of the hash table barrel is obtained by utilizing the first address of the hash table barrel storage area plus the position label of the hash table barrel and the size of a single hash table barrel.
The specific manner of calculating the hash value is well known to those skilled in the art, and is not described herein for brevity.
Or, a corresponding relation between the target key value and the hash table bucket is pre-established, and after the target key value is acquired, the target hash table bucket corresponding to the target key value can be found based on the pre-set corresponding relation.
Optionally, before determining the corresponding target hash table bucket based on the target key value, the data acquisition method may further include determining whether there is a hash table bucket corresponding to the target key value.
If the hash table bucket corresponding to the target key value exists, the hash table bucket corresponding to the target key value is taken as the target hash table bucket.
If the hash table barrel corresponding to the target key value does not exist, the hash table barrel corresponding to the target key value is established, and the newly-built hash table barrel is the target hash table barrel.
The specific implementation and principles of hash table bucket establishment are well known to those skilled in the art, and will not be described here in detail for brevity.
In order to avoid the situation that a hash table bucket is used by a plurality of processes at the same time, and a system error occurs due to untimely updating of the hash table bucket, in one embodiment, the hash table bucket is further provided with a spin lock, and the spin lock is stored in the shared memory.
Accordingly, after determining the corresponding target hash table bucket based on the target key value, the data acquisition method further includes: it is determined that the target hash table bucket is not in the locked state.
By setting the spin lock on the hash table barrel and storing the spin lock in the shared memory, the spin lock can be shared by a plurality of processes (namely a plurality of processes sharing the hash table), so that only one process of the hash table barrel can be used at the same time, and the situation of system errors caused by untimely updating of the hash table barrel due to simultaneous use of one hash table barrel by a plurality of processes is avoided.
The specific implementation and principles of spin locks are well known to those skilled in the art, and are not described herein for brevity.
In the case of setting a spin lock for a hash table bucket, after determining that the target hash table bucket is not in the locked state, the data acquisition method further includes: and setting the spin lock of the target hash table barrel to be in a locking state, and marking the target hash table barrel by using the identity of the current process.
Correspondingly, after the target data stored in the hash table item corresponding to the virtual address is acquired, the data acquisition method further comprises the following steps: the locked state of the target hash table bucket is released and the tag to the target hash table bucket is cleared.
When the hash table barrel is in a locking state, only the process corresponding to the identity information marked on the hash table barrel can use the hash table barrel.
The current process refers to a process of setting the spin lock of the target hash table bucket to a locked state.
The identity information may be information such as a process number of a process, which can uniquely characterize a process, and the specific type of the identity information is not limited herein.
When the target hash table bucket is used, the target hash table bucket is set to be in a locking state and marked by the identity of the current process, so that the target hash table bucket can only be used by the current process. After the use is completed, the locking state of the target hash table bucket is released, and the mark of the target hash table bucket is cleared, so that other processes can normally use the target hash table bucket. Therefore, only one process can be used by one hash table barrel at the same time, and the situation that system errors occur due to untimely updating of the hash table barrel caused by simultaneous use of one hash table barrel by a plurality of processes is avoided.
For example, if the process a determines that the target hash bucket is the hash bucket 1 according to the target key value, the process a first determines whether the hash bucket 1 is in the locked state, and if the hash bucket 1 is in the locked state, waits for the hash bucket 1 to be changed to the unlocked state. Then the target hash table bucket 1 is set to a locked state and marked with the identity of the current process. The target data may then be retrieved based on the hash table bucket 1. The examples herein are for ease of understanding only and should not be construed as limiting the application.
When the target data is acquired, the locking state of the target hash table bucket 1 is released, and the mark of the target hash table bucket is cleared. So that hash table bucket 1 can continue to be used by other processes.
Alternatively, the specific way to mark the target hash table bucket with the identity of the current process may be to record the identity of the current process to a preset location of the hash table bucket.
Accordingly, a specific way to clear the tag to the target hash table bucket may be to empty the data recorded in the preset location of the current process for recording the identity tag.
In the case that the hash table bucket is provided with the spin lock, if a process crash occurs, the hash table bucket marked by the process identity information cannot be used by other processes, so in one embodiment, when the process crash exists, the data acquisition method further includes: firstly, acquiring an identity of a crash process; and traversing all the hash table barrels, and if the spin lock of the hash table barrel is in a locked state and the hash table barrel is marked by the identity mark of the crashed process, releasing the locked state of the hash table barrel and clearing the mark of the hash table barrel. Wherein the crashed process is at least one process of a plurality of processes sharing the hash table.
Since the hash table bucket is marked by the identity of the process when used, the hash table bucket marked by the identity of the crashed process can be found by traversing the hash table bucket. By releasing the locking state of the hash table barrel and clearing the mark of the hash table barrel, the hash table barrel can continue to work normally without locking the hash table barrel due to process breakdown.
Alternatively, the above method for releasing the hash table bucket marked by the crash process may be implemented by a device executing the data acquisition method through a preset monitoring process.
In the case where the monitoring process is also one of the processes sharing the hash table, the monitoring process may directly traverse all the hash table buckets to find the hash table bucket marked by the identity of the crashed process.
When the monitoring process is not one of the processes sharing the hash table, the monitoring process needs to find the initial virtual address of the hash table bucket storage area for storing the hash table bucket in the shared memory, and then can find the hash table bucket based on the initial virtual address of the hash table bucket storage area, so as to realize traversing all the hash table buckets to find the hash table bucket marked by the identity of the crashed process.
The manner in which the monitoring process finds the starting virtual address of the hash table bucket storage area in the shared memory, where the hash table bucket is stored, may be: and mapping the metadata area of the shared memory to the virtual address space of the current process, and then acquiring the initial virtual address of the hash table barrel storage area in the metadata.
Alternatively, the manner in which the monitor process finds the starting virtual address of the hash table bucket storage area in the shared memory for storing the hash table bucket may be: firstly mapping a shared memory into a virtual address space to obtain a mapping head address of the shared memory, and then adding the mapping head address with a relative offset of an initial virtual address of a preset hash table barrel storage area and the initial virtual address of the preset shared memory to obtain the initial virtual address of the hash table barrel storage area.
Optionally, the specific manner of acquiring the identity of the crash process may be: firstly, the number of currently running processes is obtained, and if the number of currently running processes accords with the preset number, the process state is considered to be normal, namely, no crash process exists. If the number of the currently running processes is smaller than the preset number, the processes are considered to be crashed. And then, based on all the processes running currently and all the processes sharing the hash table, finding out the processes which are not running but still sharing the hash table, determining the processes to be crash processes, and acquiring the identity of the crash processes.
The preset number is set according to practical situations, and the specific number is not limited herein.
For example, when the preset number is 3 and the currently running processes are process 1 and process 3, and all the processes sharing the hash table include process 1, process 2 and process 3, if the number of the currently running processes is 2, it is determined that the processes crash. Then, based on all processes currently running and all processes sharing the hash table, it can be determined that process 2 is sharing the hash table, but not running, and thus, process 2 is determined to be a crash process. The examples herein are for ease of understanding only and should not be construed as limiting the application.
The process number of the current running process can be obtained by: the number of processes of item 1 (1 representing the name of the process) is obtained by regularly calling shell commands (ps-e|grep "1" |grep-v "grep" |wc-l) by the scheduled task system. The examples herein are for ease of understanding only and should not be construed as limiting the application.
In one embodiment, the metadata is stored in a metadata area of the shared memory, where the metadata includes a start address and a total size of the shared memory, and the data acquisition method further includes, before acquiring the target key value: firstly, mapping a metadata area of a shared memory to a virtual address space of a current process, and then acquiring a starting address and a total size of the shared memory in metadata. And then, the mapping relation of the metadata area of the shared memory to the virtual address space of the current process is released. And finally, mapping the shared memory into an address space corresponding to the initial address based on the initial address and the total size of the shared memory, so that the virtual addresses of any area of the shared memory in different processes are the same.
Because the metadata includes the starting address and the total size of the shared memory, the process can acquire the metadata by mapping the metadata area to the virtual address space of the current process, and can further map the shared memory based on the starting address and the total size of the shared memory in the metadata. Because the starting address and the total size are the same, the virtual address of the shared memory after mapping can be ensured to be the same as the virtual address of the shared memory after mapping by other processes, and the condition that the same virtual address corresponds to a plurality of physical addresses is avoided.
The specific implementation and principle of mapping the metadata area of the shared memory to the virtual address space of the current process are well known to those skilled in the art, and may be implemented by mmap (a method of mapping a file in a memory) system call, for example. Specific implementations of mapping the metadata area of the shared memory to the virtual address space of the current process include, but are not limited to, those illustrated above.
When mapping the shared memory into the address space corresponding to the starting address fails based on the starting address and the total size of the shared memory, the data acquisition method further comprises: first, a target virtual address space which is not utilized and has a space larger than the shared memory is obtained. And then mapping the shared memory to a target virtual address space, and updating the starting address of the shared memory recorded in the metadata area, and updating the starting virtual address of the hash table entry storage area, the starting virtual address of the available table entry storage area and the starting virtual address of the hash table bucket storage area in the metadata. And finally broadcasting notification information representing the re-mounting of the shared memory in a plurality of processes.
Wherein the current process is to perform the above, the hash table entry memory area is divided into a plurality of hash table entries of the hash table, the available table entry memory area is used for storing an available table entry stack of the hash table, and the hash table bucket memory area is used for storing at least one hash table bucket of the hash table.
Optionally, in the implementation manner of broadcasting the notification information characterizing the re-mounting of the shared memory in the multiple processes, the process that updates the metadata currently sends the notification information characterizing the re-mounting of the shared memory to other processes (processes except for the process itself) that share the hash table.
Alternatively, a specific implementation manner of acquiring the target virtual address space which is not utilized and has a space larger than the shared memory may be: first, the respective virtual address space gaps (i.e., unused virtual address space) of all processes sharing the hash table are acquired, resulting in a plurality of gap sets. A target virtual address space larger than the shared memory is then found from the intersection of the sets.
For example, when the processes sharing the hash table are process 1 and process 2, the gap before the process 1 maps the shared memory is acquired and is denoted as set a; the gap before the process 2 maps the shared memory is acquired and is denoted as set b. And calculating the intersection of the set a and the set b to obtain a set of unused virtual address spaces of the process 1 and the process 2. Then selecting a maximum gap from the virtual address space as a target virtual address space. The examples herein are for ease of understanding only and should not be construed as limiting the application.
Optionally, after determining the target virtual address space, the intermediate address of the target virtual address space may be used as the starting address of the shared memory, and then the shared memory is mapped into the virtual memory space corresponding to the starting address.
The virtual memory space corresponding to the starting address is a virtual address space with the starting address as a starting point and the size of the virtual memory space being the size of the shared memory.
In one embodiment, the hash table may include metadata, hash table entries, hash table buckets, and an available table stack. Accordingly, since the hash table is stored in the shared memory, the shared memory includes a metadata area for storing metadata, a hash table entry storage area for storing hash table entries, a hash table bucket storage area for storing hash table buckets, and an available table entry storage area for storing available table entry stacks.
The metadata is used for storing a starting virtual address of the shared memory, a total size (total size of the shared memory) and a starting virtual address of the hash table entry storage area, a starting virtual address of the available table entry storage area, and a starting virtual address of the hash table bucket storage area.
The hash table entry storage areas are memory spaces for storing hash table entries, and each hash table entry is a small memory space divided in the hash table entry storage areas.
The size of the hash table entry storage area may be preset, or may be determined according to a preset maximum number of hash table entries.
For example, if the number of hash table entries is n and the size of each hash table entry is item_size, then the size M of the hash table entry storage area Hashing =item_size*n。
Wherein the number of hash table entries may be preset.
The available entry store is a lock-free stack based on a linear table for storing virtual addresses (or pointers) of all allocatable hash entries, which do not store data.
Size M of available entry memory Stack =pointer_size, where n is the number of hash table entries and pointer_size is the size of memory space that can store a virtual address.
The hash table bucket storage area is used for storing a plurality of hash table buckets, and each hash table bucket is used for storing the corresponding relation between the target key value and the virtual address. The target key values corresponding to different hash table buckets are different.
Alternatively, the set of hash table buckets may be stored as a linear table, with the size M of the linear table = bucket_size (n (1+α)). Wherein, the bucket_size is the size of a single hash table bucket; a maximum of the number of n hash table entries; alpha is a redundancy factor, and alpha is a preset positive real number. The larger alpha is, the less likely a collision (collision refers to the same position of a hash table bucket obtained by calculating two keywords through a hash function) occurs; the smaller α, the greater the likelihood of collision. However, too large a may result in a waste of a large amount of memory.
Optionally, the hash table occupies the shared memory, that is, the size of the shared memory is the size of the memory occupied by the hash table. Alternatively, the hash table may occupy only a portion of the shared memory.
The sequence of the metadata area, the hash table entry storage area, the available table entry storage area and the hash table bucket storage area of the shared memory is preset, and the specific sequence of the metadata area, the hash table entry storage area, the available table entry storage area and the hash table bucket storage area can be set according to actual requirements without limiting the specific sequence.
Alternatively, in order to facilitate subsequent invocation of metadata stored in the metadata area, the metadata area may be set at the initial position of the shared memory, that is, the starting address of the shared memory is the starting virtual address of the metadata area.
Based on the same technical concept, the present application further provides a data updating method, referring to fig. 2, and the steps included in the method will be described with reference to fig. 2.
S300: and acquiring a target key value and a hash table updating instruction.
The specific implementation manner and the acquisition manner of the target key value are the same as those of the target key value in the data acquisition method, and are not described herein for brevity.
The hash table update instruction may be sent by upper layer software or may be sent by another process, and the specific manner of obtaining the hash table update instruction is not limited herein.
The hash table update instruction may be any one of a new shared data instruction, a delete shared data instruction, and a data update instruction.
S400: updating target content in a preset hash table according to a hash table updating instruction; the target content is a hash table item and/or a hash table bucket corresponding to the target key value in the hash table.
Wherein the hash table is stored in the shared memory and is commonly used by a plurality of processes; and the hash table bucket comprises a corresponding relation between the key value and the virtual address, and each virtual address uniquely corresponds to one hash table item.
The specific implementation manner and principle of the hash table and the shared memory are the same as those of the foregoing data acquisition method, and are not described here for brevity.
In one embodiment, the hash table includes at least one hash table bucket, an available table entry stack, and a plurality of hash table entries, and the hash table update instruction includes a newly added shared data instruction. In this case, before updating the target content in the preset hash table according to the hash table updating instruction, the data updating method further includes: and acquiring data to be shared.
Correspondingly, according to the hash table updating instruction, the specific way of updating the target content in the preset hash table can be as follows: in response to the acquired newly added shared data instruction, a corresponding target hash table bucket is first determined based on the target key value, and then a virtual address of an allocatable hash table item is determined from the available table item stack. And then writing the corresponding relation between the virtual address of the allocatable hash table item and the target key value into a target hash table barrel, and storing the data to be shared in the hash table item.
Wherein the available table entry stack records virtual addresses of all allocatable hash table entries, which do not store data.
The data to be shared can be acquired from the outside in real time, or can be stored in other areas outside the shared memory, or can be synchronously transmitted to the current process with the newly added shared data instruction, and the specific acquisition mode of the data to be shared is not limited.
Because the hash table bucket comprises the corresponding relation between the key value and the virtual address, the virtual address corresponding to the target key value can be found through the hash table bucket, and then the obtained data to be shared can be written into the hash table item corresponding to the virtual address. And writing the corresponding relation between the virtual address of the allocatable hash table item and the target key value into the target hash table barrel to finish the data updating of the hash table barrel, thereby realizing the accurate updating of the hash table barrel and the hash table item corresponding to the target key value.
For example, when the target key value is a key value 1, first, in response to the acquired newly added shared data instruction, the hash table bucket 1 corresponding to the key value 1 is determined. Virtual address 1 of the allocatable hash table entry is then determined from the stack of available entries. And then writing the corresponding relation between the virtual address 1 and the key value 1 of the allocatable hash table item into the hash table barrel 1, and storing the acquired data to be shared in the hash table item. Then, when the target data corresponding to the key value 1 needs to be searched, the virtual address 1 corresponding to the key value 1 can be found from the hash table barrel 1 based on the key value 1, and then the virtual address 1 is accessed, so that the target data corresponding to the key value 1 can be obtained from the hash table item corresponding to the virtual address 1. The examples herein are for ease of understanding only and should not be construed as limiting the application.
In one embodiment, the hash table includes at least one hash table bucket and a plurality of hash table entries, and the hash table update instruction includes a delete shared data instruction. In this case, according to the hash table update instruction, a specific manner of updating the target content in the preset hash table may be: first, in response to the obtained hash table update instruction, a corresponding target hash table bucket is determined based on the target key value. And then determining the virtual address corresponding to the target key value based on the target hash table bucket. And finally deleting the data stored in the hash table item corresponding to the virtual address, and deleting the corresponding relation between the target key value and the virtual address from the target hash table barrel. Updating of hash table buckets and hash table entries is achieved.
For example, when the target key value is key value 1 and the target hash table bucket corresponding to key value 1 is hash table bucket 1, hash table bucket 1 corresponding to key value 1 is determined in response to the obtained hash table update instruction. Then, virtual address 1 corresponding to key 1 is determined based on hash table bucket 1. And finally deleting the data stored in the hash table item corresponding to the virtual address 1, and deleting the corresponding relation between the key value 1 and the virtual address 1 from the target hash table bucket. Thereafter, the target data corresponding to the key value 1 does not exist in the hash table.
In one embodiment, the hash table includes at least one hash table bucket and a plurality of hash table entries, and the hash table update instruction includes a data update instruction. Before updating the target content in the preset hash table according to the hash table updating instruction, the data updating method further comprises the following steps: and acquiring data to be updated.
Correspondingly, according to the hash table updating instruction, the specific way of updating the target content in the preset hash table can be as follows: responding to the acquired data updating instruction, and determining a corresponding target hash table barrel based on the target key value; determining a virtual address corresponding to the target key value based on the target hash table bucket; and updating the data stored in the hash table item corresponding to the virtual address into the data to be updated.
The data to be updated can be acquired from the outside in real time, or can be stored in other areas outside the shared memory, or can be synchronously transmitted to the current process with the hash table updating instruction, and the specific acquisition mode of the data to be shared is not limited.
In one embodiment, the hash table updating instruction includes a hash table bucket deleting instruction, and according to the hash table updating instruction, a specific way of updating the target content in the preset hash table may be: responding to the acquired data updating instruction, and determining a corresponding target hash table barrel based on the target key value; the hash table bucket is then deleted.
In one embodiment, the specific manner of determining the corresponding target hash table bucket based on the target key value may be: judging whether a hash table barrel corresponding to the target key value exists or not; if the target key value exists, taking the hash table bucket corresponding to the target key value as a target hash table bucket; if not, establishing a hash table bucket corresponding to the target key value, wherein the newly-established hash table bucket is the target hash table bucket.
In one embodiment, the hash table bucket is provided with a spin lock, the spin lock is stored in the shared memory, and after determining a corresponding target hash table bucket based on the target key value, the data updating method further includes: it is determined that the target hash table bucket is not in the locked state.
Optionally, after determining that the target hash table bucket is not in the locked state, the data updating method further includes: and setting the spin lock of the target hash table barrel to be in a locking state, and marking the target hash table barrel by using the identity of the current process.
Correspondingly, after performing the corresponding updating operation of the hash table updating instruction on the content corresponding to the target key value in the preset hash table to obtain the updated hash table, the data updating method further comprises the following steps: the locked state of the target hash table bucket is released and the tag to the target hash table bucket is cleared.
Optionally, in the case that there is a process crash, the data updating method further includes: acquiring an identity of a crash process; the crash process is at least one process of the plurality of processes. Traversing all the hash table barrels, if the spin lock of the hash table barrel is in a locking state and the hash table barrel is marked by the identity mark of the crashing process, unlocking the locking state of the hash table barrel and clearing the mark of the hash table barrel.
In one embodiment, the hash table includes metadata, the metadata is stored in a metadata area of the shared memory, the metadata includes a start address and a total size of the shared memory, and the data updating method further includes, before the target key value is acquired: mapping the metadata area of the shared memory to a virtual address space of the current process; acquiring the starting address and the total size of a shared memory in metadata; removing the mapping relation of the metadata area of the shared memory to the virtual address space of the current process; and mapping the shared memory into an address space corresponding to the starting address based on the starting address and the total size of the shared memory, so that the virtual addresses of any area of the shared memory in different processes are the same.
Optionally, if mapping the shared memory to the address space corresponding to the starting address fails based on the starting address and the total size of the shared memory, the data updating method further includes: obtaining a target virtual address space which is not utilized and has a space larger than that of the shared memory; mapping the shared memory to a target virtual address space, and updating the starting address of the shared memory recorded in the metadata area, and updating the starting virtual address of the hash table entry storage area, the starting virtual address of the available table entry storage area and the starting virtual address of the hash table bucket storage area in the metadata; the hash table entry memory area is divided into a plurality of hash table entries of the hash table, the available table entry memory area is used for storing an available table entry stack of the hash table, and the hash table bucket memory area is used for storing at least one hash table bucket of the hash table; and broadcasting notification information representing the re-mounting of the shared memory in the processes.
In the foregoing description, the principles of some embodiments of the data updating method are the same as those of the corresponding embodiments of the foregoing data acquiring method, and for brevity, reference may be made to corresponding contents of the foregoing data acquiring method.
Based on the same technical concept, the present application provides a method for initializing a shared memory, as shown in fig. 3, and the steps included in the method will be described with reference to fig. 3.
S500: the size of the shared memory is obtained.
The size of the shared memory may be preset, or may be determined by the respective required sizes of the metadata area, the hash table entry storage area, the available table entry storage area and the hash table bucket storage area, that is, the size of the shared memory is the sum of the respective required sizes of the metadata area, the hash table entry storage area, the available table entry storage area and the hash table bucket storage area.
The shared memory comprises a metadata area, a hash table item storage area, an available table item storage area and a hash table barrel storage area; the metadata area is used for storing metadata of the hash table, the hash table item storage area is divided into a plurality of hash table items of the hash table, the available item storage area is used for storing an available item stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table.
The specific implementation manner and principle of the hash table and the shared memory are the same as those of the foregoing data acquisition method, and are not described here for brevity.
S600: a starting address of the shared memory is generated.
The starting address of the shared memory may be generated by any existing method for generating a virtual address, for example, by using mmap system call.
In one embodiment, the process of generating the starting address of the shared memory may be that the starting virtual address of the shared memory is first generated, and then the starting address is obtained based on the starting virtual address of the shared memory, the size of the shared memory, and a preset value.
The sum of the starting virtual address, the size of the shared memory and the preset value may be used as the starting address, that is, the starting virtual address+the size of the shared memory+the preset value=the starting address.
The preset value may be a larger value, for example, -100G to-1T, or 100G to 1T. The positive and negative of the preset value are determined according to the growth direction of the system heap address space. If the system heap address space is increased from large to small, the preset value is negative. If the system heap address space is increased from small to large, the preset value is positive.
Alternatively, the specific step of generating the starting address of the shared memory may be to first create a file with the same size as the shared memory. And then mapping the file into a virtual address space of the current process through mmap system call, wherein a return value addr (address) is the initial virtual address of the shared memory.
S700: mapping the shared memory to a virtual address space corresponding to the starting address.
Wherein each hash table entry corresponds to a virtual address one-to-one.
The specific implementation and principles of mapping a shared memory to a virtual address space corresponding to a starting address are well known to those skilled in the art, and will not be described in detail herein for brevity.
S800: and recording the starting address into the metadata, and updating the starting virtual address of the hash table item storage area, the starting virtual address of the available table item storage area and the starting virtual address of the hash table storage area in the metadata based on the respective sizes and the sequence of the preset hash table item storage area, the available table item storage area and the hash table storage area.
Because the sequence of the four storage areas of the metadata area, the hash table entry storage area, the available table entry storage area and the hash table bucket storage area of the shared memory is preset, and the sizes of the metadata area, the hash table entry storage area, the available table entry storage area and the hash table bucket storage area are preset.
Therefore, after determining the start address, the start address may be used as the start virtual address of the first storage area that is sequentially arranged at the front. The first storage area is any one of a metadata area, a hash table entry storage area, an available table entry storage area, and a hash table bucket storage area.
And then obtaining the initial virtual address of the second storage area based on the size and the initial address of the first storage area. The second storage area is any one of a metadata area, a hash table entry storage area, an available table entry storage area, and a hash table bucket storage area, and the first storage area and the second storage area are different.
And then obtaining the starting virtual address of the third storage area based on the starting virtual address of the second storage area and the size of the second storage area. The third storage area is any one of a metadata area, a hash table entry storage area, an available table entry storage area and a hash table bucket storage area, and the first storage area, the second storage area and the third storage area are different.
And finally, obtaining the starting virtual address of the fourth storage area based on the starting virtual address of the third storage area and the size of the third storage area. The fourth storage area is any one of a metadata area, a hash table entry storage area, an available table entry storage area and a hash table bucket storage area, and the first storage area, the second storage area, the third storage area and the fourth storage area are different.
For example, if the start address is A1, the size of the first storage area is B1, the size of the second storage area is B2, the size of the third storage area is B3, and the size of the fourth storage area is B4. The starting virtual address of the second storage area is a1+b1; the starting virtual address of the third storage area is a1+b1+b2; the starting virtual address of the fourth storage area is a1+b1+b2+b3. The examples herein are for ease of understanding only and should not be construed as limiting the application.
Alternatively, in order to facilitate subsequent invocation of metadata stored in the metadata area, the metadata area may be set at the initial position of the shared memory, that is, the starting address of the shared memory is the starting virtual address of the metadata area.
Because the size and the sequence of each of the hash table item storage area, the available table item storage area and the hash table storage area are preset, after the shared memory is mapped into the virtual address space corresponding to the initial address, the initial virtual addresses corresponding to each of the hash table item storage area, the available table item storage area and the hash table storage area can be generated, and each hash table item corresponds to one virtual address one by one, so that the condition that one virtual address corresponds to a plurality of physical addresses can be avoided. In addition, because the hash table is stored in the shared memory, the process sharing the shared memory can use the hash table, so that the sharing of the hash table is realized, and the hash tables are not required to be respectively established by different processes, thereby reducing the occupation of the memory.
Based on the same technical idea, the present application provides a data acquisition device, as shown in fig. 4, the data acquisition device 100 ensures an acquisition module 110 and a processing module 120.
The obtaining module 110 is configured to obtain a target key value.
The processing module 120 is configured to obtain a virtual address corresponding to the target key value based on the target key value and a preset hash table, and obtain target data stored in a hash table entry corresponding to the virtual address.
Wherein the hash table is stored in a shared memory and is commonly used by a plurality of processes; each virtual address in the hash table uniquely corresponds to a hash table entry.
The hash table includes at least one hash table bucket and a plurality of hash table entries; the processing module 120 is specifically configured to determine a corresponding target hash table bucket based on the target key value; wherein the target hash table bucket comprises a corresponding relation between a key value and a virtual address; determining a virtual address corresponding to the target key value based on the target hash table bucket; and acquiring the target data from the hash table item corresponding to the virtual address based on the virtual address.
The hash table bucket is provided with a spin lock, the spin lock is stored in the shared memory, and the processing module 120 is further configured to determine that the target hash table bucket is not in the locked state after determining the corresponding target hash table bucket based on the target key value.
The processing module 120 is further configured to set a spin lock of the target hash table bucket to a locked state, and mark the target hash table bucket with an identity of a current process; correspondingly, after the target data stored in the hash table item corresponding to the virtual address is obtained, the processing module is further configured to release the locking state of the target hash table bucket, and clear the mark of the target hash table bucket.
In the event of a process crash, the processing module 120 is further configured to obtain an identity of the crashed process; the crash process is at least one process of the plurality of processes; traversing all the hash table barrels, if the spin lock of the hash table barrel is in a locking state and the hash table barrel is marked by the identity of the collapse process, unlocking the locking state of the hash table barrel and clearing the mark of the hash table barrel.
The hash table includes metadata, where the metadata is stored in a metadata area of the shared memory, where the metadata includes a start address and a total size of the shared memory, and before the target key value is obtained, the processing module 120 is further configured to map the metadata area of the shared memory to a virtual address space of a current process; acquiring the starting address and the total size of the shared memory in the metadata; releasing the mapping relation of the metadata area of the shared memory to the virtual address space of the current process; and mapping the shared memory into an address space corresponding to the starting address based on the starting address and the total size of the shared memory, so that virtual addresses of any one region of the shared memory in different processes are the same.
If mapping the shared memory to the address space corresponding to the starting address fails based on the starting address and the total size of the shared memory, the processing module 120 is further configured to obtain a target virtual address space which is not utilized and has a space larger than the shared memory; mapping the shared memory to the target virtual address space, and updating the starting address of the shared memory recorded in the metadata area, and updating the starting virtual address of the hash table entry storage area, the starting virtual address of the available table entry storage area and the starting virtual address of the hash table bucket storage area in the metadata; the hash table entry storage area is divided into a plurality of hash table entries of the hash table, the available table entry storage area is used for storing an available table entry stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table; and broadcasting notification information representing the re-mounting of the shared memory in the processes.
In one embodiment, the hash table occupies the shared memory.
In one embodiment, the different hash table entries are different storage areas pre-divided in the shared memory.
The data acquisition device 100 according to the embodiment of the present application has the same implementation principle and technical effects as those of the foregoing data acquisition method embodiment, and for brevity, reference may be made to the corresponding content in the foregoing data acquisition method embodiment where the device embodiment is not mentioned.
Based on the same technical concept, the present application provides a data updating apparatus, as shown in fig. 5, the data updating apparatus 200 includes an acquisition module 210 and a processing module 220.
The obtaining module 210 is configured to obtain the target key value and the hash table update instruction.
A processing module 220, configured to update the target content in the preset hash table according to the hash table update instruction; the target content is a hash table item and/or a hash table bucket corresponding to the target key value in the hash table.
The hash table is stored in a shared memory, and is commonly used by a plurality of processes; and the hash table bucket comprises a corresponding relation between a key value and virtual addresses, and each virtual address uniquely corresponds to one hash table item.
The hash table comprises at least one hash table bucket, an available table item stack and a plurality of hash table items, and the hash table update instruction comprises a newly added shared data instruction; before updating the target content in the preset hash table according to the hash table updating instruction, the processing module 220 is further configured to obtain the data to be shared. Correspondingly, the processing module is specifically configured to determine a corresponding target hash table bucket based on the target key value in response to the acquired newly added shared data instruction; determining a virtual address of an allocatable hash table item from the available table item stack; wherein the available table item stack records virtual addresses of all allocatable hash table items, and the allocatable hash table items do not store data; and writing the corresponding relation between the virtual address of the allocatable hash table item and the target key value into the target hash table barrel, and storing the data to be shared in the hash table item.
The hash table comprises at least one hash table bucket and a plurality of hash table entries, and the hash table update instruction comprises a delete shared data instruction; the processing module 220 is specifically configured to determine, in response to the obtained hash table update instruction, a corresponding target hash table bucket based on the target key value; determining a virtual address corresponding to the target key value based on the target hash table bucket; deleting the data stored in the hash table item corresponding to the virtual address, and deleting the corresponding relation between the target key value and the virtual address from the target hash table bucket.
The hash table comprises at least one hash table bucket and a plurality of hash table entries, the hash table update instructions comprising data update instructions; before updating the target content in the preset hash table according to the hash table updating instruction, the processing module 220 is further configured to obtain data to be updated. Correspondingly, the processing module is specifically configured to determine a corresponding target hash table bucket based on the target key value in response to the acquired data update instruction; determining a virtual address corresponding to the target key value based on the target hash table bucket; and updating the data stored in the hash table item corresponding to the virtual address into the data to be updated.
The processing module 220 is specifically configured to determine whether a hash table bucket corresponding to the target key value exists; if the target key value exists, taking a hash table bucket corresponding to the target key value as the target hash table bucket; if not, establishing a hash table bucket corresponding to the target key value, wherein the newly-established hash table bucket is the target hash table bucket.
The hash table bucket is provided with a spin lock, the spin lock is stored in the shared memory, and after determining a corresponding target hash table bucket based on the target key value, the processing module 220 is further configured to determine that the target hash table bucket is not in a locked state.
The processing module 220 is further configured to set a spin lock of the target hash table bucket to a locked state, and mark the target hash table bucket with an identity of a current process; correspondingly, after the target data stored in the hash table item corresponding to the virtual address is obtained, the processing module is further configured to release the locking state of the target hash table bucket, and clear the mark of the target hash table bucket.
When there is a process crash, the processing module 220 is further configured to obtain an identity of the crash process; the crash process is at least one process of the plurality of processes; traversing all the hash table barrels, if the spin lock of the hash table barrel is in a locking state and the hash table barrel is marked by the identity of the collapse process, unlocking the locking state of the hash table barrel and clearing the mark of the hash table barrel.
The hash table includes metadata, where the metadata is stored in a metadata area of the shared memory, where the metadata includes a start address and a total size of the shared memory, and before the target key value is obtained, the processing module 220 is further configured to map the metadata area of the shared memory to a virtual address space of a current process; acquiring the starting address and the total size of the shared memory in the metadata; releasing the mapping relation of the metadata area of the shared memory to the virtual address space of the current process; and mapping the shared memory into an address space corresponding to the starting address based on the starting address and the total size of the shared memory, so that virtual addresses of any one region of the shared memory in different processes are the same.
If mapping the shared memory to the address space corresponding to the starting address fails based on the starting address and the total size of the shared memory, the processing module 220 is further configured to obtain a target virtual address space which is not utilized and has a space larger than the shared memory; mapping the shared memory to the target virtual address space, and updating the starting address of the shared memory recorded in the metadata area, and updating the starting virtual address of the hash table entry storage area, the starting virtual address of the available table entry storage area and the starting virtual address of the hash table bucket storage area in the metadata; the hash table entry storage area is divided into a plurality of hash table entries of the hash table, the available table entry storage area is used for storing an available table entry stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table; and broadcasting notification information representing the re-mounting of the shared memory in the processes.
In one embodiment, the hash table occupies the shared memory. Of course, in other embodiments, the hash table may occupy only a portion of the shared memory.
In one embodiment, the different hash table entries are different storage areas pre-divided in the shared memory. For example, the hash table entry storage area in the shared memory may be divided into n storage spaces, each of which is a hash table entry. Where n is a positive integer and the amount of storage space is determined based on the size of the hash table entry storage area and the size of each hash table entry.
The data updating apparatus 200 according to the embodiment of the present application has the same implementation principle and technical effects as those of the foregoing data updating method embodiment, and for brevity, reference may be made to corresponding contents in the foregoing data updating method embodiment for the description of the embodiment of the apparatus.
Based on the same technical concept, the present application provides a shared memory initializing device, as shown in fig. 6, the shared memory initializing device 300 includes an obtaining module 310, a generating module 320, a mapping module 330 and a processing module 340.
An obtaining module 310, configured to obtain a size of a shared memory, where the shared memory includes a metadata area, a hash table entry storage area, an available table entry storage area, and a hash table bucket storage area; the metadata area is used for storing metadata of a hash table, the hash table item storage area is used for storing a plurality of hash table items of the hash table, the available table item storage area is used for storing an available table item stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table.
The generating module 320 is configured to generate a starting address of the shared memory.
A mapping module 330, configured to map the shared memory to a virtual address space corresponding to the starting address; wherein each hash table entry corresponds to one virtual address one by one.
And a processing module 340, configured to record the start address into the metadata, and update the start virtual address of the hash table entry storage area, the start virtual address of the available table entry storage area, and the start virtual address of the hash table storage area in the metadata based on the size and the sequence of each of the preset hash table entry storage area, the available table entry storage area, and the hash table storage area.
The generating module 320 is specifically configured to generate a starting virtual address of the shared memory; and obtaining the starting address based on the starting virtual address of the shared memory, the size of the shared memory and a preset value.
The implementation principle and the technical effects of the shared memory initializing device 300 provided in the embodiment of the present application are the same as those of the embodiment of the shared memory initializing method, and for brevity, reference may be made to the corresponding contents of the embodiment of the shared memory initializing method for the parts of the embodiment of the device that are not mentioned.
Please refer to fig. 7, which illustrates an electronic device 400 according to an embodiment of the present application. The electronic device 400 includes: memory 410, processor 420.
The memory 410 and the processor 420 are directly or indirectly electrically connected to each other to realize data transmission or interaction. For example, the components may be electrically connected to each other via one or more communication buses or signal lines. The memory 410 is used for storing a computer program, for example, a software functional module shown in at least one of fig. 4, 5, and 6, that is, the data acquisition device 100, the data update device 200, and the shared memory initialization device 300. Wherein the processor 420 is configured to execute the executable modules stored in the memory 410.
For example, the data acquisition device 100 includes software functional modules or computer programs. At this time, the processor 420 is configured to obtain a target key value, obtain a virtual address corresponding to the target key value based on the target key value and a preset hash table, and obtain target data stored in a hash table item corresponding to the virtual address; wherein the hash table is stored in a shared memory and is commonly used by a plurality of processes; each virtual address in the hash table uniquely corresponds to a hash table entry.
For example, the data updating apparatus 200 includes software functional modules or computer programs. At this time, the processor 420 is configured to obtain the target key value and the hash table update instruction; updating target content in a preset hash table according to the hash table updating instruction; wherein, the target content is a hash table item and/or a hash table bucket corresponding to the target key value in the hash table; the hash table is stored in a shared memory, and is commonly used by a plurality of processes; and the hash table bucket comprises a corresponding relation between a key value and virtual addresses, and each virtual address uniquely corresponds to one hash table item.
For example, the shared memory initializing device 300 includes software functional modules or computer programs. At this time, the processor 420 is configured to obtain a size of a shared memory, where the shared memory includes a metadata area, a hash table entry storage area, an available table entry storage area, and a hash table bucket storage area; the metadata area is used for storing metadata of a hash table, the hash table item storage area is used for storing a plurality of hash table items of the hash table, the available table item storage area is used for storing an available table item stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table; generating a starting address of the shared memory; mapping the shared memory to a virtual address space corresponding to the starting address; wherein each hash table entry corresponds to one virtual address one by one; and recording the starting address into the metadata, and updating the starting virtual address of the hash table item storage area, the starting virtual address of the available table item storage area and the starting virtual address of the hash table storage area in the metadata based on the respective sizes and sequences of the preset hash table item storage area, the available table item storage area and the hash table storage area.
The Memory 410 may be, but is not limited to, a random access Memory (Random Access Memory, RAM), a Read Only Memory (ROM), a programmable Read Only Memory (Programmable Read-Only Memory, PROM), an erasable Read Only Memory (Erasable Programmable Read-Only Memory, EPROM), an electrically erasable Read Only Memory (Electric Erasable Programmable Read-Only Memory, EEPROM), etc.
The processor 420 may be an integrated circuit chip with signal processing capabilities. The processor may be a general-purpose processor, including a central processing unit (Central Processing Unit, CPU), a network processor (Network Processor, NP), etc.; but also digital signal processors (Digital Signal Processor, DSP), application specific integrated circuits (Application Specific Integrated Circuit, ASIC), field programmable gate arrays (Field Programmable Gate Array, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components. The disclosed methods, steps, and logic blocks in the embodiments of the present application may be implemented or performed. The general purpose processor may be a microprocessor or the processor 420 may be any conventional processor or the like.
The electronic device 400 includes, but is not limited to, a personal computer, a server, and the like.
The embodiment of the present application further provides a non-volatile computer readable storage medium (hereinafter referred to as a storage medium) storing a computer program, where the computer program, when executed by a computer such as the electronic device 400, performs the data acquisition method, the data update method, and the shared memory initialization method described above. The computer-readable storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a random access Memory (Random Access Memory, RAM), a magnetic disk, or an optical disk, or other various media capable of storing program codes.
The above description is only of the preferred embodiments of the present application and is not intended to limit the present application, but various modifications and variations can be made to the present application by those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the protection scope of the present application.

Claims (28)

1. A method of data acquisition, comprising:
acquiring a target key value, acquiring a virtual address corresponding to the target key value based on the target key value and a preset hash table, and acquiring target data stored in a hash table item corresponding to the virtual address;
Wherein the hash table is stored in a shared memory and is commonly used by a plurality of processes; the hash table comprises a corresponding relation between a key value and a virtual address, and at least one hash table item, wherein each virtual address in the hash table uniquely corresponds to one hash table item.
2. The method of claim 1, wherein the hash table comprises at least one hash table bucket and a plurality of hash table entries;
the obtaining the virtual address corresponding to the target key value based on the target key value and a preset hash table, and obtaining the target data stored in the hash table item corresponding to the virtual address, includes:
determining a corresponding target hash table bucket based on the target key value; wherein the target hash table bucket comprises a corresponding relation between a key value and a virtual address;
determining a virtual address corresponding to the target key value based on the target hash table bucket;
and acquiring the target data from the hash table item corresponding to the virtual address based on the virtual address.
3. The method of claim 2, wherein the hash table buckets are provided with spin locks stored in the shared memory, the method further comprising, after determining the corresponding target hash table bucket based on the target key value:
And determining that the target hash table bucket is not in a locking state, wherein the target hash table bucket cannot be accessed when in the locking state.
4. The method of claim 3, wherein upon determining that the target hash table bucket is not in a locked state, the method further comprises:
setting the spin lock of the target hash table barrel to be in a locking state, and marking the target hash table barrel by using the identity of the current process;
correspondingly, after the target data stored in the hash table entry corresponding to the virtual address is obtained, the method further includes:
and releasing the locking state of the target hash table barrel, and clearing the mark of the target hash table barrel.
5. The method of claim 4, wherein in the event that there is a process crash, the method further comprises:
acquiring an identity of a crash process; the crash process is at least one process of the plurality of processes;
traversing all the hash table barrels, if the spin lock of the hash table barrel is in a locking state and the hash table barrel is marked by the identity of the collapse process, unlocking the locking state of the hash table barrel and clearing the mark of the hash table barrel.
6. The method of claim 1, wherein the hash table includes metadata stored in a metadata area of the shared memory, the metadata including a starting address and a total size of the shared memory, the method further comprising, prior to the obtaining the target key:
mapping the metadata area of the shared memory to a virtual address space of the current process;
acquiring the starting address and the total size of the shared memory in the metadata;
releasing the mapping relation of the metadata area of the shared memory to the virtual address space of the current process;
and mapping the shared memory into an address space corresponding to the starting address based on the starting address and the total size of the shared memory, so that virtual addresses of any one region of the shared memory in different processes are the same.
7. The method of claim 6, wherein if mapping the shared memory into an address space corresponding to the starting address fails based on the starting address and a total size of the shared memory, the method further comprises:
obtaining a target virtual address space which is not utilized and has a space larger than the shared memory;
Mapping the shared memory to the target virtual address space, and updating the starting address of the shared memory recorded in the metadata area, and updating the starting virtual address of the hash table entry storage area, the starting virtual address of the available table entry storage area and the starting virtual address of the hash table bucket storage area in the metadata; the hash table entry storage area is divided into a plurality of hash table entries of the hash table, the available table entry storage area is used for storing an available table entry stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table;
and broadcasting notification information representing the re-mounting of the shared memory in the processes.
8. The method of claim 1, wherein the hash table occupies the shared memory.
9. The method of claim 1, wherein different ones of the hash table entries are pre-partitioned different storage areas in the shared memory.
10. A method of updating data, comprising:
acquiring a target key value and a hash table updating instruction;
updating target content in a preset hash table according to the hash table updating instruction; wherein, the target content is a hash table item and/or a hash table bucket corresponding to the target key value in the hash table;
The hash table is stored in a shared memory, and is commonly used by a plurality of processes; the hash table comprises at least one hash table item and at least one hash table bucket, wherein the hash table bucket comprises a corresponding relation between a key value and a virtual address, and each virtual address uniquely corresponds to one hash table item.
11. The method of claim 10, wherein the hash table comprises at least one hash table bucket, an available table item stack, and a plurality of hash table items, the hash table update instruction comprising a newly added shared data instruction;
before updating the target content in the preset hash table according to the hash table updating instruction, the method further comprises the following steps: acquiring data to be shared;
correspondingly, updating the target content in the preset hash table according to the hash table updating instruction comprises the following steps:
responding to the acquired newly added shared data instruction, and determining a corresponding target hash table barrel based on the target key value;
determining a virtual address of an allocatable hash table item from the available table item stack; wherein the available table item stack records virtual addresses of all allocatable hash table items, and the allocatable hash table items do not store data;
And writing the corresponding relation between the virtual address of the allocatable hash table item and the target key value into the target hash table barrel, and storing the data to be shared in the hash table item.
12. The method of claim 10, wherein the hash table comprises at least one hash table bucket and a plurality of hash table entries, the hash table update instruction comprising a delete shared data instruction;
updating target content in a preset hash table according to the hash table updating instruction, wherein the updating of the target content comprises the following steps:
determining a corresponding target hash table bucket based on the target key value in response to the acquired hash table updating instruction;
determining a virtual address corresponding to the target key value based on the target hash table bucket;
deleting the data stored in the hash table item corresponding to the virtual address, and deleting the corresponding relation between the target key value and the virtual address from the target hash table bucket.
13. The method of claim 10, wherein the hash table comprises at least one hash table bucket and a plurality of hash table entries, the hash table update instruction comprising a data update instruction;
before updating the target content in the preset hash table according to the hash table updating instruction, the method further comprises the following steps: acquiring data to be updated;
Correspondingly, updating the target content in the preset hash table according to the hash table updating instruction comprises the following steps:
responding to the acquired data updating instruction, and determining a corresponding target hash table barrel based on the target key value;
determining a virtual address corresponding to the target key value based on the target hash table bucket;
and updating the data stored in the hash table item corresponding to the virtual address into the data to be updated.
14. The method of any of claims 11-13, wherein determining a corresponding target hash table bucket based on the target key value comprises:
judging whether a hash table barrel corresponding to the target key value exists or not;
if the target key value exists, taking a hash table bucket corresponding to the target key value as the target hash table bucket;
if not, establishing a hash table bucket corresponding to the target key value, wherein the newly-established hash table bucket is the target hash table bucket.
15. The method of any of claims 11-13, wherein the hash table buckets are provided with spin locks stored in the shared memory, the method further comprising, after determining the corresponding target hash table bucket based on the target key value:
Determining that the target hash table bucket is not in a locked state.
16. The method of claim 15, wherein upon determining that the target hash table bucket is not in a locked state, the method further comprises:
setting the spin lock of the target hash table barrel to be in a locking state, and marking the target hash table barrel by using the identity of the current process;
correspondingly, after performing the updating operation corresponding to the hash table updating instruction on the content corresponding to the target key value in the preset hash table to obtain the updated hash table, the method further includes:
and releasing the locking state of the target hash table barrel, and clearing the mark of the target hash table barrel.
17. The method of claim 16, wherein in the event that there is a process crash, the method further comprises:
acquiring an identity of a crash process; the crash process is at least one process of the plurality of processes;
traversing all the hash table barrels, if the spin lock of the hash table barrel is in a locking state and the hash table barrel is marked by the identity of the collapse process, unlocking the locking state of the hash table barrel and clearing the mark of the hash table barrel.
18. The method of claim 10, wherein the hash table includes metadata stored in a metadata area of the shared memory, the metadata including a starting address and a total size of the shared memory, the method further comprising, prior to the obtaining the target key:
mapping the metadata area of the shared memory to a virtual address space of the current process;
acquiring the starting address and the total size of the shared memory in the metadata;
releasing the mapping relation of the metadata area of the shared memory to the virtual address space of the current process;
and mapping the shared memory into an address space corresponding to the starting address based on the starting address and the total size of the shared memory, so that virtual addresses of any one region of the shared memory in different processes are the same.
19. The method of claim 18, wherein if mapping the shared memory into an address space corresponding to the starting address fails based on the starting address and a total size of the shared memory, the method further comprises:
obtaining a target virtual address space which is not utilized and has a space larger than the shared memory;
Mapping the shared memory to the target virtual address space, and updating the starting address of the shared memory recorded in the metadata area, and updating the starting virtual address of the hash table entry storage area, the starting virtual address of the available table entry storage area and the starting virtual address of the hash table bucket storage area in the metadata; the hash table entry storage area is divided into a plurality of hash table entries of the hash table, the available table entry storage area is used for storing an available table entry stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table;
and broadcasting notification information representing the re-mounting of the shared memory in the processes.
20. The method of claim 10, wherein the hash table occupies the shared memory.
21. The method of claim 10, wherein different ones of the hash table entries are pre-partitioned different storage areas in the shared memory.
22. A method for initializing a shared memory, comprising:
acquiring the size of a shared memory, wherein the shared memory comprises a metadata area, a hash table entry storage area, an available table entry storage area and a hash table bucket storage area; the metadata area is used for storing metadata of a hash table, the hash table item storage area is divided into a plurality of hash table items of the hash table, the available table item storage area is used for storing an available table item stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table;
Generating a starting address of the shared memory;
mapping the shared memory to a virtual address space corresponding to the starting address; wherein each hash table entry corresponds to one virtual address one by one;
and recording the starting address into the metadata, and updating the starting virtual address of the hash table item storage area, the starting virtual address of the available table item storage area and the starting virtual address of the hash table storage area in the metadata based on a preset hash table item storage area, an available table item storage area and a hash table storage area.
23. The method of claim 22, wherein generating the starting address of the shared memory comprises:
generating a starting virtual address of the shared memory;
and obtaining the starting address based on the starting virtual address of the shared memory, the size of the shared memory and a preset value.
24. A data acquisition device, comprising:
the acquisition module is used for acquiring the target key value;
the processing module is used for obtaining a virtual address corresponding to the target key value based on the target key value and a preset hash table, and obtaining target data stored in a hash table item corresponding to the virtual address;
Wherein the hash table is stored in a shared memory and is commonly used by a plurality of processes; the hash table comprises a corresponding relation between a key value and a virtual address, and at least one hash table item, wherein each virtual address in the hash table uniquely corresponds to one hash table item.
25. A data updating apparatus, comprising:
the acquisition module is used for acquiring the target key value and the hash table updating instruction;
the processing module is used for updating target contents in a preset hash table according to the hash table updating instruction; wherein, the target content is a hash table item and/or a hash table bucket corresponding to the target key value in the hash table;
the hash table is stored in a shared memory, and is commonly used by a plurality of processes; the hash table comprises at least one hash table item and at least one hash table bucket, the hash table bucket comprises a corresponding relation between a key value and a virtual address, and each virtual address uniquely corresponds to one hash table item.
26. A shared memory initializing device, comprising:
the acquisition module is used for acquiring the size of a shared memory, wherein the shared memory comprises a metadata area, a hash table entry storage area, an available table entry storage area and a hash table bucket storage area; the metadata area is used for storing metadata of a hash table, the hash table item storage area is used for storing a plurality of hash table items of the hash table, the available table item storage area is used for storing an available table item stack of the hash table, and the hash table bucket storage area is used for storing at least one hash table bucket of the hash table;
The generation module is used for generating the starting address of the shared memory;
the mapping module is used for mapping the shared memory to a virtual address space corresponding to the initial address; wherein each hash table entry corresponds to one virtual address one by one;
and the processing module is used for recording the starting address into the metadata, and updating the starting virtual address of the hash table item storage area, the starting virtual address of the available table item storage area and the starting virtual address of the hash table storage area in the metadata based on the size and the sequence of the preset hash table item storage area, the available table item storage area and the hash table storage area.
27. An electronic device, comprising: the device comprises a memory and a processor, wherein the memory is connected with the processor;
the memory is used for storing programs;
the processor being configured to invoke a program stored in the memory to perform the method of any of claims 1-23.
28. A computer-readable storage medium, on which a computer program is stored, which, when being run by a computer, performs the method according to any one of claims 1-23.
CN202310878696.7A 2023-07-17 2023-07-17 Data acquisition and update method, shared memory initialization method and related devices Pending CN116881256A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202310878696.7A CN116881256A (en) 2023-07-17 2023-07-17 Data acquisition and update method, shared memory initialization method and related devices

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202310878696.7A CN116881256A (en) 2023-07-17 2023-07-17 Data acquisition and update method, shared memory initialization method and related devices

Publications (1)

Publication Number Publication Date
CN116881256A true CN116881256A (en) 2023-10-13

Family

ID=88256465

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202310878696.7A Pending CN116881256A (en) 2023-07-17 2023-07-17 Data acquisition and update method, shared memory initialization method and related devices

Country Status (1)

Country Link
CN (1) CN116881256A (en)

Similar Documents

Publication Publication Date Title
US11086560B2 (en) Data storage access method, device and apparatus for persistent memory
US9355028B2 (en) Data-storage device and flash memory control method
US5261088A (en) Managing locality in space reuse in a shadow written B-tree via interior node free space list
US8516211B2 (en) Secure storage management system and method
US7949839B2 (en) Managing memory pages
US7299320B2 (en) Message based inter-process for high volume data
US9501421B1 (en) Memory sharing and page deduplication using indirect lines
US7827374B2 (en) Relocating page tables
US5794256A (en) Pointer swizzling facility using three-state references to manage access to referenced objects
US20100077013A1 (en) Computer storage deduplication
US20080005521A1 (en) Kernel memory free algorithm
JPH05210637A (en) Method of simultaneously controlling access
US20230418754A1 (en) In-Memory Distributed Cache
US20100030994A1 (en) Methods, systems, and computer readable media for memory allocation and deallocation
CN110399333B (en) Method, apparatus and computer program product for deleting snapshots
CN114327917A (en) Memory management method, computing device and readable storage medium
US20100287351A1 (en) File access management system
US7991976B2 (en) Permanent pool memory management method and system
US7500074B2 (en) Identifying relocatable kernel mappings
US8341368B2 (en) Automatic reallocation of structured external storage structures
CN116881256A (en) Data acquisition and update method, shared memory initialization method and related devices
US20220269675A1 (en) Hash-based data structure
CN114201269A (en) Memory page changing method, system and storage medium
CN114281765A (en) Metadata processing method and equipment in distributed file system
US7185029B1 (en) Method and apparatus for maintaining, and updating in-memory copies of the first and second pointers to reference the new versions of the first and second control structures that indicate available and allocated portions of usable space in the data file

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination