Shared memory space management method and device, equipment, medium and product
By dividing the shared memory space of the virtual machine into two independent spaces and configuring write and read functions, and updating the pointer after data operation, the problem of data inconsistency between virtual machines is solved, and data consistency and efficient communication are achieved.
Patent Information
- Application Number
- CN202410621900.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-20
- Publication Date
- 2025-11-21
AI Technical Summary
The problem of inconsistent data read when sharing memory space between virtual machines and data written by another virtual machine.
The shared memory space of the first virtual machine and the second virtual machine is divided into a first memory space and a second memory space, and configured so that the first virtual machine writes data and the second virtual machine reads data, and vice versa. When the first target virtual machine writes data, its write pointer is updated; when the second target virtual machine reads data, its read pointer is updated to ensure data consistency.
By partitioning and configuring memory space and updating pointers, the data read by the virtual machine is ensured to be consistent with the data written by another virtual machine, thus improving the accuracy of data communication.
Smart Images

Figure CN120994351A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application belongs to the technical field of virtual machines, and particularly relates to a shared memory space management method, device, equipment, medium and product. BACKGROUND
[0002] In an existing virtualization implementation scheme, virtual machines (VMs) usually adopt a shared memory space (Share Memory) to realize data interaction between the VMs. Through the shared memory space, transmission of large blocks of data in the data interaction process can be avoided, and communication efficiency is improved.
[0003] However, since the shared memory space is shared by the VMs for data interaction, the VMs for data interaction can both write data into the shared memory space and read data from the shared memory space, which leads to the fact that when one VM reads data written by another VM, the data read may be the data written by the one VM itself, resulting in inconsistency between the data read and the data written by the other VM. SUMMARY
[0004] Embodiments of the application provide a shared memory space management method, device, equipment, medium and product, which can solve the problem of inconsistency between the data read and the data written by another virtual machine.
[0005] In a first aspect, embodiments of the application provide a shared memory space management method, comprising:
[0006] dividing a shared memory space of a first virtual machine and a second virtual machine into a first memory space and a second memory space;
[0007] configuring the first memory space to be used for the first virtual machine to write data and the second virtual machine to read data, and configuring the second memory space to be used for the second virtual machine to write data and the first virtual machine to read data;
[0008] updating a write pointer of a target memory space after a first target virtual machine writes data into the target memory space; the first target virtual machine is the first virtual machine or the second virtual machine; the target memory space is a memory space configured to be used for the first target virtual machine to write data in the first memory space and the second memory space; and the write pointer is used to record an address of the target memory space corresponding to next time of writing data;
[0009] updating a read pointer of a target memory space after a second target virtual machine reads data from the target memory space; the second target virtual machine is a virtual machine different from the first target virtual machine in the first virtual machine and the second virtual machine; and the read pointer is used to record an address of the target memory space corresponding to next time of reading data.
[0010] In a second aspect, an embodiment of the present application provides a shared memory space management apparatus, comprising:
[0011] a first dividing module, configured to divide the shared memory space of the first virtual machine and the second virtual machine into a first memory space and a second memory space;
[0012] a configuring module, configured to configure the first memory space to be used for the first virtual machine to write data and the second virtual machine to read data, and configure the second memory space to be used for the second virtual machine to write data and the first virtual machine to read data;
[0013] a first updating module, configured to update a write pointer of a target memory space after a first target virtual machine writes data into the target memory space; the first target virtual machine is the first virtual machine or the second virtual machine; the target memory space is a virtual machine configured to be used for the first target virtual machine to write data in the first memory space and the second memory space; and the write pointer is used to record an address of the target memory space corresponding to next time of writing data;
[0014] a second updating module, configured to update a read pointer of a target memory space after a second target virtual machine reads data from the target memory space; the second target virtual machine is a virtual machine different from the first target virtual machine in the first virtual machine and the second virtual machine; and the read pointer is used to record an address of the target memory space corresponding to next time of reading data.
[0015] In a third aspect, an embodiment of the present application provides an electronic device, comprising a processor and a memory storing computer program instructions; the processor reads and executes the computer program instructions to implement the steps of the shared memory space management method provided by the embodiment of the present application.
[0016] In a fourth aspect, an embodiment of the present application provides a computer readable storage medium, storing computer program instructions; the computer program instructions are executed by a processor to implement the steps of the shared memory space management method provided by the embodiment of the present application.
[0017] In a fifth aspect, an embodiment of the present application provides a computer program product, comprising computer program instructions; the computer program instructions are executed by a processor to implement the steps of the shared memory space management method provided by the embodiment of the present application.
[0018] In the embodiment of the present application, the shared memory space of the first virtual machine and the second virtual machine is divided into a first memory space and a second memory space; the first memory space is configured for the first virtual machine to write data and the second virtual machine to read data; the second memory space is configured for the second virtual machine to write data and the first virtual machine to read data; after the first target virtual machine writes data into the target memory space, the write pointer of the target memory space is updated; the first target virtual machine is the first virtual machine or the second virtual machine; the target memory space is the memory space configured for the first target virtual machine to write data in the first memory space and the second memory space; the write pointer is used to record the address of the target memory space corresponding to the next time of writing data; after the second target virtual machine reads data from the target memory space, the read pointer of the target memory space is updated; the second target virtual machine is the virtual machine different from the first target virtual machine in the first virtual machine and the second virtual machine; the read pointer is used to record the address of the target memory space corresponding to the next time of reading data. Since the target memory space is the memory space configured for the first target virtual machine to write data in the first memory space and the second memory space, it can only be written data by the first target virtual machine and read data by the second target virtual machine, but cannot be written data by the second target virtual machine and read data by the first target virtual machine, and the write pointer records the address of the target memory space corresponding to the next time of writing data, and the read pointer records the address of the target memory space corresponding to the next time of reading data, therefore, it can be ensured that the data read by the second target virtual machine is consistent with the data written by the first target virtual machine, that is, it can be ensured that the data read by one virtual machine is consistent with the data written by another virtual machine. BRIEF DESCRIPTION OF DRAWINGS
[0019] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings needed to be used in the embodiments of the present application will be briefly introduced as follows, and other drawings can also be obtained by those of ordinary skill in the art without creative labor on the basis of these drawings.
[0020] Figure 1 is a flow diagram of the shared memory space management method provided by the embodiments of the present application;
[0021] Figure 2 is a schematic diagram of the ring buffer provided by the embodiments of the present application;
[0022] Figure 3 is a schematic diagram of the application program accessing the shared memory space provided by the embodiments of the present application;
[0023] Figure 4 is a structural schematic diagram of the shared memory space management device provided by the embodiments of the present application;
[0024] Figure 5FIG. 1 is a structural schematic diagram of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0025] In order to more clearly understand the above-mentioned purposes, features and advantages of the present application, the solutions of the present application will be further described below. It should be noted that the embodiments of the present application and the features in the embodiments can be combined with each other without conflict.
[0026] In the following description, many specific details are set forth in order to provide a thorough understanding of the present application, but the present application can also be implemented in other ways different from those described herein; obviously, the embodiments in the description are only some of the embodiments of the present application, not all the embodiments.
[0027] The shared memory space management method, device, equipment, medium and product provided by the embodiments of the present application will be described in detail below in combination with the drawings, specific embodiments and application scenarios.
[0028] Figure 1 FIG. 1 is a flow schematic diagram of a shared memory space management method provided by an embodiment of the present application. As shown in FIG. 1, the shared memory space management method can include the following steps. Figure 1
[0029] Step 101: dividing the shared memory space of the first virtual machine and the second virtual machine into a first memory space and a second memory space;
[0030] In some possible implementations of the embodiments of the present application, the shared memory space of the first virtual machine and the second virtual machine can be divided into two memory spaces of the same size, and the shared memory space of the first virtual machine and the second virtual machine can also be divided into two memory spaces of different sizes.
[0031] In some possible implementations of the embodiments of the present application, before step 101, the shared memory space management method provided by the embodiments of the present application can further include: allocating a shared memory space when a fourth target virtual machine is created, wherein the fourth target virtual machine is any one of the first virtual machine and the second virtual machine, and the shared memory space is a memory space requested by the fourth target virtual machine; mapping the shared memory space to a physical address of the fourth target virtual machine; when a fifth target virtual machine is created, mapping the shared memory space to a physical address of the fifth target virtual machine, and the shared memory space is a memory space requested by the fifth target virtual machine, wherein the fifth target virtual machine is a virtual machine other than the fourth target virtual machine in the first virtual machine and the second virtual machine.
[0032] In some possible implementations of the embodiments of the present application, each virtual machine can specify the size and name of the memory used for communication of the virtual machine, and the Hypervisor allocates and maps the memory when creating the virtual machine. When two virtual machines specify the same name of the memory, the two virtual machines communicate through the shared memory.
[0033] For example, virtual machine A and virtual machine B both specify 8K bytes of shared memory with the name "Test", virtual machine A is created earlier than virtual machine B, the Hypervisor allocates 8K continuous physical pages when creating virtual machine A, names the 8K continuous physical pages as "Test", and maps the 8K continuous physical pages to the physical address of virtual machine A, and does not need to allocate physical pages again when creating virtual machine B, but only needs to map the 8K continuous physical pages to the physical address of virtual machine B. Virtual machine A and virtual machine B can both write data to and read data from the 8K continuous physical pages. It can be understood that, in the present example, the 8K continuous physical pages are the shared memory space of virtual machine A and virtual machine B.
[0034] In the embodiments of the present application, the shared memory space can be created by mapping the memory space created when creating a virtual machine to the physical address of another virtual machine, without creating a shared memory space for two virtual machines again, thereby saving memory resources.
[0035] Step 102: configure the first memory space to be used for the first virtual machine to write data and the second virtual machine to read data; and configure the second memory space to be used for the second virtual machine to write data and the first virtual machine to read data;
[0036] In the embodiments of the present application, the first memory space is used for the first virtual machine to write data and the second virtual machine to read data; and the second memory space is used for the second virtual machine to write data and the first virtual machine to read data; the second virtual machine cannot write data to the first memory space, and the first virtual machine cannot write data to the second memory space.
[0037] Step 103: updating the write pointer of the target memory space after the first target virtual machine writes data to the target memory space; the first target virtual machine is the first virtual machine or the second virtual machine; the target memory space is the memory space configured to be used for the first target virtual machine to write data in the first memory space and the second memory space; and the write pointer is used to record the address of the target memory space corresponding to the next write data;
[0038] Step 104: updating the read pointer of the target memory space after the second target virtual machine reads data from the target memory space; the second target virtual machine is a virtual machine different from the first target virtual machine in the first virtual machine and the second virtual machine; the read pointer is used to record the address of the target memory space corresponding to the next reading of data.
[0039] When the first target virtual machine finishes writing data into the target memory space, the write pointer is directed to the next address of the address written into after the data writing, i.e., the write pointer is updated to the address written into after the data writing, for example, the first target virtual machine starts writing data from the address A of the target memory space, when the data is written into the memory space corresponding to the address B, the data writing is completed, and the write pointer is directed to the next address of the address B. When the first target virtual machine finishes reading data from the target memory space, the read pointer is directed to the next address of the address corresponding to the data read after the data reading is completed, i.e., the read pointer is updated to the next address of the address corresponding to the data read after the data reading is completed, for example, the second target virtual machine starts reading data from the address A of the target memory space, when the data x is read, the data reading is completed, and the read pointer is directed to the next address of the address corresponding to the data x.
[0040] In some possible implementations of the embodiments of the present application, before the first target virtual machine writes data into the target memory space, the shared memory space management method provided by the embodiments of the present application can further include: dividing the target memory space into N memory slots, where N is a positive integer. Correspondingly, step 103 can include: updating the write pointer to the start address of the next memory slot of the i th memory slot of the target memory space after the first target virtual machine writes data into the i th memory slot of the target memory space, when i is a positive integer less than N, the next memory slot of the i th memory slot is the i+1 th memory slot, and when i is equal to N, the next memory slot of the i th memory slot is the 1 st memory slot; and step 104 can include: updating the read pointer to the start address of the next memory slot of the i th memory slot after the second target virtual machine reads data from the i th memory slot.
[0041] It can be understood that the target memory space is logically implemented as a ring buffer at this time.
[0042] In some possible implementations of the embodiments of the present application, the target memory space can be divided into a plurality of memory subspaces of different sizes, or can be divided into a plurality of memory subspaces of the same size. Optionally, in order to facilitate the updating of the write pointer and the read pointer, the target memory space can be divided into a plurality of memory subspaces of the same size. After each data writing, the write pointer is directed to the start address of the next memory subspace of the memory subspace where the data is written. After each data reading, the read pointer is directed to the start address of the next memory subspace of the memory subspace where the data is read. Since the plurality of memory subspaces are of the same size, the memory subspace where the data is written or the memory subspace where the data is read can be quickly determined, and then the write pointer or the read pointer is directed to the start address of the next memory subspace of the memory subspace.
[0043] As shown in Figure 2 , Figure 2 is a schematic diagram of a ring buffer provided by the embodiments of the present application. In Figure 2 , the ring buffer is divided into eight slots of the same size, the write pointer is directed to the start address of the next slot of the slot where the data is written after the data writing is completed, and the read pointer is directed to the start address of the next slot of the slot where the data is read after the data reading is completed.
[0044] Exemplarily, the target memory space is divided into eight slots of the same size. When data is written, after the data is written into the third slot, the data writing is completed, and the write pointer is directed to the start address of the fourth slot. When the data is written into the eighth slot, there is still data that is not written, and the data is continuously written into the first slot. After the data is written into the first slot, the data writing is completed, and the write pointer is directed to the start address of the second slot. When data is read, after the data is read from the third slot, the data reading is completed, and the read pointer is directed to the start address of the fourth slot. When the data is read from the eighth slot, there is still data that is not read, and the data in the first slot is continuously read. After the data is read from the first slot, the data reading is completed, and the read pointer is directed to the start address of the second slot.
[0045] It should be noted that, when data is written into the target memory space, the data is written from the address directed by the write pointer, and the write pointer is updated after the data writing is completed. When data is read from the target memory space, the data is read from the address directed by the read pointer until the write pointer is read, and the read pointer is updated.
[0046] Exemplarily, when writing data into the target memory space, the write pointer points to the start address of the 2nd slot, data is written from the start address of the 2nd slot, when data is written into the 4th slot, data writing is completed, and the write pointer is pointed to the start address of the 5th slot.
[0047] Exemplarily, when reading data from the target memory space, the read pointer points to the start address of the 1st slot, and the write pointer points to the start address of the 4th slot. When reading data, the write pointer does not change, i.e. no data is written into the target memory space or data writing is not completed. Data is read from the start address of the 1st slot until the start address of the 4th slot is read, and the read pointer is pointed to the start address of the 4th slot.
[0048] Exemplarily, when reading data from the target memory space, the read pointer points to the start address of the 1st slot, and the write pointer points to the start address of the 4th slot. When reading data, the write pointer changes, i.e. data is written into the target memory space in the process of reading data from the target memory space, and data writing is completed, the write pointer is updated, and it is assumed that the write pointer is updated to point to the start address of the 7th slot. Data is read from the start address of the 1st slot until the start address of the 7th slot is read, and the read pointer is pointed to the start address of the 7th slot.
[0049] In the embodiment of the present application, the shared memory space of the first virtual machine and the second virtual machine is divided into a first memory space and a second memory space; the first memory space is configured to be used for the first virtual machine to write data and the second virtual machine to read data; the second memory space is configured to be used for the second virtual machine to write data and the first virtual machine to read data; after the first target virtual machine writes data into the target memory space, the write pointer of the target memory space is updated; the first target virtual machine is the first virtual machine or the second virtual machine; the target memory space is the memory space configured to be used for the first target virtual machine to write data in the first memory space and the second memory space; the write pointer is used to record the address of the target memory space corresponding to the next data writing; after the second target virtual machine reads data from the target memory space, the read pointer of the target memory space is updated; the second target virtual machine is the virtual machine different from the first target virtual machine in the first virtual machine and the second virtual machine; the read pointer is used to record the address of the target memory space corresponding to the next data reading. Since the target memory space is the memory space configured to be used for the first target virtual machine to write data in the first memory space and the second memory space, it can only be written data by the first target virtual machine and read data by the second target virtual machine, but cannot be written data by the second target virtual machine and read data by the first target virtual machine, and the write pointer records the address of the target memory space corresponding to the next data writing, and the read pointer records the address of the target memory space corresponding to the next data reading, therefore, it can be ensured that the data read by the second target virtual machine is consistent with the data written by the first target virtual machine, that is, it can be ensured that the data read by one virtual machine is consistent with the data written by another virtual machine.
[0050] In some possible implementations of the embodiment of the present application, after step 103, the shared memory space management method provided by the embodiment of the present application can further include: notifying the second target virtual machine to read data in the target memory space.
[0051] For example, after the first target virtual machine completes writing data into the target memory space, the Hypervisor can send a data reading message to the second target virtual machine, the data reading message being used to instruct the second target virtual machine to read data from the target memory space, and the second target virtual machine reads data from the target memory space after receiving the data reading message.
[0052] In the embodiment of the present application, after one virtual machine completes writing data, another virtual machine is notified to read data, which can make the virtual machine read data in time compared with the related art in which the virtual machine reads data from the shared memory space every fixed time.
[0053] In some possible implementations of the embodiments of the present application, notifying the second target virtual machine to read the data in the target memory space can include: triggering a data write exception when the first target virtual machine writes data to the third memory space, where the third memory space is configured to be used for the first virtual machine and the second virtual machine to read data and is prohibited from being used for the first virtual machine and the second virtual machine to write data; and injecting an interrupt into the second target virtual machine based on the data write exception, where the interrupt is used to notify the second target virtual machine to read the data in the target memory space.
[0054] In some possible implementations of the embodiments of the present application, the third memory space used for the first virtual machine and the second virtual machine to read data and prohibited from being used for the first virtual machine and the second virtual machine to write data can be created in advance. When the first target virtual machine finishes writing data to the target memory space, the first target virtual machine writes data to the third memory space. Since the third memory space is used for the first virtual machine and the second virtual machine to read data and is prohibited from being used for the first virtual machine and the second virtual machine to write data, a data write exception occurs at this time. At this time, the Hypervisor injects an interrupt into the second target virtual machine. After the second target virtual machine is interrupted, the second target virtual machine suspends the currently running program and reads the data in the target memory space. When the data reading is completed, the second target virtual machine continues to execute the suspended program.
[0055] In the embodiments of the present application, the data write exception and the interrupt mechanism are triggered by writing data to the memory space that can only read data but cannot write data, so that the virtual machine can preferentially read the data in the target memory space without processing the currently running program.
[0056] In some possible implementations of the embodiments of the present application, before triggering the data write exception when the first target virtual machine writes data to the third memory space, the shared memory space management method provided by the embodiments of the present application can further include: creating a Userspace I / O (UIO) device in a kernel of the third target virtual machine, where the third target virtual machine is any one of the first virtual machine and the second virtual machine; and storing addresses of the first memory space, the second memory space and the third memory space into three storage areas of the UIO device respectively.
[0057] In the embodiments of the present application, an application program in the virtual machine only needs to create a memory mapping of the UIO device, and can access the first memory space, the second memory space and the third memory space through the addresses of the first memory space, the second memory space and the third memory space stored in the three storage areas.
[0058] Exemplarily, as shown in FIG. 3, Figure 3 Figure 3 is a schematic diagram of application program accessing shared memory space provided by the embodiment of the present application. In Figure 3 In the embodiment, the UIO device is created in the virtual machine A and the virtual machine B, the storage area mem[0] of the UIO device in the virtual machine A and the storage area mem[0] of the UIO device in the virtual machine B store the address of the third memory space; the storage area mem[1] of the UIO device in the virtual machine A and the storage area mem[2] of the UIO device in the virtual machine B store the address of the first memory space; the storage area mem[2] of the UIO device in the virtual machine A and the storage area mem[1] of the UIO device in the virtual machine B store the address of the second memory space.
[0059] The application program of the virtual machine A writes data to the first memory space through the address of the first memory space stored in the storage area mem[1] of the UIO device in the virtual machine A; the application program of the virtual machine B reads data from the first memory space through the address of the first memory space stored in the storage area mem[2] of the UIO device in the virtual machine B.
[0060] The application program of the virtual machine B writes data to the second memory space through the address of the second memory space stored in the storage area mem[2] of the UIO device in the virtual machine B; the application program of the virtual machine A reads data from the second memory space through the address of the second memory space stored in the storage area mem[1] of the UIO device in the virtual machine A.
[0061] The application program of the virtual machine A writes data to the third memory space through the address of the third memory space stored in the storage area mem[0] of the UIO device in the virtual machine A, triggers an interrupt, and makes the application program of the virtual machine B read data from the first memory space through the address of the first memory space stored in the storage area mem[2] of the UIO device in the virtual machine B.
[0062] The application program of the virtual machine B writes data to the third memory space through the address of the third memory space stored in the storage area mem[0] of the UIO device in the virtual machine B, triggers an interrupt, and makes the application program of the virtual machine A read data from the first memory space through the address of the first memory space stored in the storage area mem[1] of the UIO device in the virtual machine A.
[0063] The overall process of the communication between the virtual machine A and the virtual machine B is described as follows.
[0064] The virtual machine A and the virtual machine B both specify the same name and size of shared memory, the virtual machine A is created prior to the virtual machine B, the hypervisor allocates a continuous physical page when creating the virtual machine A, maps the continuous physical page to the physical address of the virtual machine A, maps the continuous physical page to the physical address of the virtual machine B when creating the virtual machine B, and the continuous physical page is the shared memory space of the virtual machine A and the virtual machine B.
[0065] The continuous physical page is divided into a first memory space and a second memory space, the first memory space is configured for the virtual machine A to write data and the virtual machine B to read data, and the second memory space is configured for the virtual machine B to write data and the virtual machine A to read data.
[0066] The first memory space and the second memory space are respectively divided into slots with the same size.
[0067] A third memory space is created, the third memory space is configured for the virtual machine A and the virtual machine B to read data, and the virtual machine A and the virtual machine B are prohibited from writing data.
[0068] The UIO device is created in the virtual machine A and the virtual machine B, the address of the first memory space is written into the storage area mem[1] of the UIO device of the virtual machine A and the storage area mem[2] of the UIO device of the virtual machine B, the address of the second memory space is written into the storage area mem[2] of the UIO device of the virtual machine A and the storage area mem[1] of the UIO device of the virtual machine B, and the address of the third memory space is written into the storage area mem[0] of the UIO device of the virtual machine A and the storage area mem[0] of the UIO device of the virtual machine B.
[0069] The application program of the virtual machine A writes data to the first memory space through the address of the first memory space stored in the storage area mem[1] of the UIO device of the virtual machine A, after the data writing is completed, the write pointer is updated to point to the start address of the next slot of the slot where the data is written, then the application program of the virtual machine A writes data to the third memory space through the address of the third memory space stored in the storage area mem[0] of the UIO device of the virtual machine A, triggers an interrupt, and makes the application program of the virtual machine B read data from the first memory space through the address of the first memory space stored in the storage area mem[2] of the UIO device of the virtual machine B, after the data reading is completed, the read pointer is updated to point to the start address of the next slot of the slot where the data is read.
[0070] The application program of the virtual machine B writes data to the second memory space through the address of the second memory space stored in the storage area mem[1] of the UIO device in the virtual machine B, and after the data writing is completed, the write pointer is updated to point to the start address of the next slot of the slot to which the data is written after the data writing is completed; then, the application program of the virtual machine B writes data to the third memory space through the address of the third memory space stored in the storage area mem[0] of the UIO device in the virtual machine B, triggers an interrupt, and makes the application program of the virtual machine A read data from the second memory space through the address of the second memory space stored in the storage area mem[2] of the UIO device in the virtual machine A, and after the data reading is completed, the read pointer is updated to point to the start address of the next slot of the slot in which the data is located after the data reading is completed.
[0071] Corresponding to the method embodiments described above, the embodiments of the present application also provide a shared memory space management device. As shown in Figure 4 Figure 4 is a structural schematic diagram of the shared memory space management device provided by the embodiments of the present application. The shared memory space management device 400 can include:
[0072] The first division module 401 is configured to divide the shared memory space of the first virtual machine and the second virtual machine into the first memory space and the second memory space.
[0073] The configuration module 402 is configured to configure the first memory space as being used for the first virtual machine to write data and the second virtual machine to read data, and configure the second memory space as being used for the second virtual machine to write data and the first virtual machine to read data.
[0074] The first update module is configured to update the write pointer of the target memory space after the first target virtual machine writes data to the target memory space; the first target virtual machine is the first virtual machine or the second virtual machine; the target memory space is the virtual machine configured as being used for the first target virtual machine to write data in the first memory space and the second memory space; and the write pointer is used to record the address of the target memory space corresponding to the next data writing.
[0075] The second update module is configured to update the read pointer of the target memory space after the second target virtual machine reads data from the target memory space; the second target virtual machine is the virtual machine different from the first target virtual machine in the first virtual machine and the second virtual machine; and the read pointer is used to record the address of the target memory space corresponding to the next data reading.
[0076] In the embodiment of the present application, the shared memory space of the first virtual machine and the second virtual machine is divided into a first memory space and a second memory space; the first memory space is configured to be used for the first virtual machine to write data and the second virtual machine to read data; the second memory space is configured to be used for the second virtual machine to write data and the first virtual machine to read data; after the first target virtual machine writes data into the target memory space, the write pointer of the target memory space is updated; the first target virtual machine is the first virtual machine or the second virtual machine; the target memory space is the memory space configured to be used for the first target virtual machine to write data in the first memory space and the second memory space; the write pointer is used to record the address of the target memory space corresponding to the next time of writing data; after the second target virtual machine reads data from the target memory space, the read pointer of the target memory space is updated; the second target virtual machine is the virtual machine different from the first target virtual machine in the first virtual machine and the second virtual machine; the read pointer is used to record the address of the target memory space corresponding to the next time of reading data. Since the target memory space is the memory space configured to be used for the first target virtual machine to write data in the first memory space and the second memory space, it can only be written data by the first target virtual machine and read data by the second target virtual machine, but cannot be written data by the second target virtual machine and read data by the first target virtual machine, and the write pointer records the address of the target memory space corresponding to the next time of writing data, and the read pointer records the address of the target memory space corresponding to the next time of reading data, therefore, it can be ensured that the data read by the second target virtual machine is consistent with the data written by the first target virtual machine, that is, it can be ensured that the data read by one virtual machine is consistent with the data written by another virtual machine.
[0077] In some possible implementations of the embodiment of the present application, the shared memory space management apparatus provided by the embodiment of the present application can further include:
[0078] The second dividing module is configured to divide the target memory space into N memory subspaces, where N is a positive integer.
[0079] The first updating module is specifically configured to:
[0080] After the first target virtual machine writes data into the i th memory subspace of the target memory space, the write pointer is updated to the start address of the next memory subspace of the i th memory subspace, when i is a positive integer less than N, the next memory subspace of the i th memory subspace is the (i+1) th memory subspace, and when i is equal to N, the next memory subspace of the i th memory subspace is the 1 st memory subspace.
[0081] The second updating module is specifically configured to:
[0082] After the second target virtual machine reads data from the i th memory subspace, the read pointer is updated to the start address of the next memory subspace of the i th memory subspace.
[0083] In some possible implementation of the embodiments of the present application, the shared memory space management apparatus provided by the embodiments of the present application can further include:
[0084] The notification module is configured to notify the second target virtual machine to read the data in the target memory space.
[0085] In the embodiments of the present application, after the one virtual machine finishes writing data, the other virtual machine is notified to read the data, which can make the virtual machine read the data in time, compared with the related art in which the virtual machine reads data from the shared memory space at fixed time intervals.
[0086] In some possible implementation of the embodiments of the present application, the notification module can include:
[0087] The triggering submodule is configured to trigger a data write exception when the first target virtual machine writes data into the third memory space, wherein the third memory space is configured to be used for the first virtual machine and the second virtual machine to read data, and the first virtual machine and the second virtual machine are prohibited from writing data into the third memory space;
[0088] The injection submodule is configured to inject an interruption into the second target virtual machine based on the data write exception, wherein the interruption is used to notify the second target virtual machine to read the data in the target memory space.
[0089] In the embodiments of the present application, the data write exception and the interruption mechanism are triggered by writing data into the memory space that can only read data but cannot write data, which can make the virtual machine preferentially read the data in the target memory space without processing the program currently running.
[0090] In some possible implementation of the embodiments of the present application, the shared memory space management apparatus provided by the embodiments of the present application can further include:
[0091] The first creation module is configured to create a user space input / output device in the kernel of the third target virtual machine, wherein the third target virtual machine is any one of the first virtual machine and the second virtual machine.
[0092] The storage module is configured to store the addresses of the first memory space, the second memory space and the third memory space into three storage areas of the user space input / output device.
[0093] In some possible implementation of the embodiments of the present application, the shared memory space management apparatus provided by the embodiments of the present application can further include:
[0094] The second creation module is configured to allocate a shared memory space when creating the fourth target virtual machine, wherein the fourth target virtual machine is any one of the first virtual machine and the second virtual machine, and the shared memory space is the memory space requested by the fourth target virtual machine.
[0095] The first mapping module is configured to map the shared memory space to a physical address of the fourth target virtual machine.
[0096] The second mapping module is configured to map the shared memory space to a physical address of a fifth target virtual machine when the fifth target virtual machine is created, the shared memory space being a memory space requested by the fifth target virtual machine, wherein the fifth target virtual machine is a virtual machine other than the fourth target virtual machine in the first virtual machine and the second virtual machine.
[0097] In the embodiments of the present application, the memory space created when a virtual machine is created is mapped to a physical address of another virtual machine, so that the creation of the shared memory space is realized, and a shared memory space does not need to be created for the two virtual machines, thereby saving memory resources.
[0098] Figure 5 FIG. 1 is a structural schematic diagram of an electronic device provided by an embodiment of the present application.
[0099] The electronic device can include a processor 501 and a memory 502 having stored computer program instructions.
[0100] Specifically, the processor 501 can include a central processing unit (CPU), or an application specific integrated circuit (ASIC), or one or more integrated circuits configured to implement embodiments of the present application.
[0101] The memory 502 can include a mass storage for data or instructions. By way of example and not limitation, the memory 502 can include a hard disk drive (HDD), a floppy disk drive, a flash memory, an optical disk, a magneto-optical disk, a magnetic tape, or a universal serial bus (USB) drive or a combination of two or more of these. Where appropriate, the memory 502 can include removable or non-removable (or fixed) media. Where appropriate, the memory 502 can be internal or external to the electronic device. In some particular embodiments, the memory 502 is a non-volatile solid-state memory.
[0102] In some particular embodiments, the memory can include Read-Only Memory (ROM), Random Access Memory (RAM), magnetic disk storage media devices, optical storage media devices, flash memory devices, electrical, optical, or other physical / tangible memory storage devices. Thus, generally, the memory includes one or more tangible (non-transitory) computer-readable storage media (e.g., memory devices) encoded with software comprising computer-executable instructions that, when executed (e.g., by one or more processors), are operable to perform the operations described with reference to the shared memory space management method according to the present application.
[0103] The processor 501 implements the steps of the shared memory space management method provided by the embodiments of the present application by reading and executing computer program instructions stored in the memory 502.
[0104] In some examples, the electronic device can also include a communication interface 503 and a bus 510. Wherein, as shown in the figure, the processor 501, the memory 502, the communication interface 503 are connected through the bus 510 and complete the communication between each other. Figure 5
[0105] The communication interface 503 is mainly used to realize the communication between each module, device, unit and / or equipment in the embodiments of the present application.
[0106] Bus 510 includes a hardware, software, or both that couples components of electronic device to each other. As an example and not by way of limitation, bus can include an Accelerated Graphics Port (AGP) or other graphics bus, an Extended Industry Standard Architecture (EISA) bus, a Front Side Bus (FSB), a Hyper Transport (HT) interconnect, an Industry Standard Architecture (ISA) bus, an InfiniBand (IB) interconnect, a Low Pin Count (LPC) bus, a memory bus, a Micro Channel Architecture (MCA) bus, a Peripheral Component Interconnect (PCI) bus, a PCI-Express (PCI-X) bus, a Serial Advanced Technology Attachment (SATA) bus, a Video Electronics Standards Association Local (VLB) bus, or another suitable bus or a combination of two or more of these. Where appropriate, bus 510 can include one or more buses. Although this application describes and shows a particular bus, this application contemplates any suitable bus or interconnect.
[0107] The electronic device can execute the shared memory space management method provided by the embodiments of the present application, thereby achieving the corresponding technical effects of the shared memory space management method provided by the embodiments of the present application.
[0108] In addition, in combination with the shared memory space management method in the above embodiments, the embodiments of the present application also provide a computer readable storage medium for implementation. The computer readable storage medium stores computer program instructions; the computer program instructions are executed by the processor to implement the steps of the shared memory space management method provided by the embodiments of the present application. Examples of computer readable storage medium include non-transitory computer readable medium, such as ROM, RAM, magnetic disk or optical disk, etc.
[0109] The embodiments of the present application also provide a computer program product, which includes computer program instructions; the computer program instructions are executed by the processor to implement the steps of the shared memory space management method provided by the embodiments of the present application, and can achieve the same technical effects. To avoid repetition, it will not be repeated here.
[0110] It has to be noted that, in the present document, the terms "first", "second", etc. merely serve the purpose of distinguishing between two entities or operations, without necessarily requiring or implying any actual relationship or order between these entities or operations. Moreover, the terms "comprising", "having", etc. shall be interpreted as specifying the presence of stated elements but not excluding the presence of other elements or the possession of additional elements.
Claims
1. A method of managing a shared memory space, characterized by, The method comprises: dividing a shared memory space of a first virtual machine and a second virtual machine into a first memory space and a second memory space; configuring the first memory space to be used for the first virtual machine to write data and the second virtual machine to read data, and configuring the second memory space to be used for the second virtual machine to write data and the first virtual machine to read data; updating a write pointer of a target memory space after a first target virtual machine writes data into the target memory space; the first target virtual machine is the first virtual machine or the second virtual machine; the target memory space is a memory space configured to be used for the first target virtual machine to write data in the first memory space and the second memory space; and the write pointer is used to record an address of the target memory space corresponding to next data writing; updating a read pointer of the target memory space after a second target virtual machine reads data from the target memory space; the second target virtual machine is a virtual machine different from the first target virtual machine in the first virtual machine and the second virtual machine; and the read pointer is used to record an address of the target memory space corresponding to next data reading.
2. The method of claim 1, wherein, Before the first target virtual machine writes data into the target memory space, the method further comprises: dividing the target memory space into N memory subspaces, where N is a positive integer; the updating of the write pointer of the target memory space after the first target virtual machine writes data into the target memory space comprises: updating the write pointer to a start address of a next memory subspace of the i th memory subspace after the first target virtual machine writes data into the i th memory subspace of the target memory space; when i is a positive integer less than N, the next memory subspace of the i th memory subspace is an (i+1) th memory subspace; and when i is equal to N, the next memory subspace of the i th memory subspace is a 1 st memory subspace; the updating of the read pointer of the target memory space after the second target virtual machine reads data from the target memory space comprises: updating the read pointer to a start address of a next memory subspace of the i th memory subspace after the second target virtual machine reads data from the i th memory subspace.
3. The method of claim 1, wherein, After the updating of the write pointer of the target memory space after the first target virtual machine writes data into the target memory space, the method further comprises: informing the second target virtual machine to read data in the target memory space.
4. The method of claim 3, wherein, The informing of the second target virtual machine to read data in the target memory space comprises: triggering a data writing exception when the first target virtual machine writes data into a third memory space; the third memory space is configured to be used for the first virtual machine and the second virtual machine to read data, and the first virtual machine and the second virtual machine are prohibited from writing data; injecting an interruption into the second target virtual machine based on the data writing exception; and the interruption is used to inform the second target virtual machine to read data in the target memory space.
5. The method of claim 4, wherein, Before triggering the data write exception when the first target virtual machine writes data into the third memory space, the method further comprises: creating a user space input / output device in a kernel of a third target virtual machine, wherein the third target virtual machine is any one of the first virtual machine and the second virtual machine; storing addresses of the first memory space, the second memory space and the third memory space into three storage areas of the user space input / output device respectively.
6. The method of claim 1, wherein, Before dividing the shared memory space of the first virtual machine and the second virtual machine into the first memory space and the second memory space, the method further comprises: allocating the shared memory space when creating a fourth target virtual machine, wherein the fourth target virtual machine is any one of the first virtual machine and the second virtual machine, and the shared memory space is a memory space requested by the fourth target virtual machine; mapping the shared memory space to a physical address of the fourth target virtual machine; mapping the shared memory space to a physical address of a fifth target virtual machine when creating the fifth target virtual machine, wherein the shared memory space is a memory space requested by the fifth target virtual machine, and the fifth target virtual machine is a virtual machine other than the fourth target virtual machine among the first virtual machine and the second virtual machine.
7. A shared memory space management apparatus characterized by comprising: The apparatus comprises: a first dividing module configured to divide a shared memory space of a first virtual machine and a second virtual machine into a first memory space and a second memory space; a configuration module configured to configure the first memory space to be used for the first virtual machine to write data and the second virtual machine to read data, and configure the second memory space to be used for the second virtual machine to write data and the first virtual machine to read data; a first updating module configured to update a write pointer of a target memory space after a first target virtual machine writes data into the target memory space, wherein the first target virtual machine is the first virtual machine or the second virtual machine, the target memory space is a memory space configured to be used for the first target virtual machine to write data among the first memory space and the second memory space, and the write pointer is used to record an address of the target memory space corresponding to next data writing; a second updating module configured to update a read pointer of the target memory space after a second target virtual machine reads data from the target memory space, wherein the second target virtual machine is a virtual machine other than the first target virtual machine among the first virtual machine and the second virtual machine, and the read pointer is used to record an address of the target memory space corresponding to next data reading.
8. An electronic device, comprising: The electronic device comprises a processor and a memory storing computer program instructions; The processor reads and executes the computer program instructions to implement the steps of the shared memory space management method according to any one of claims 1-6.
9. A computer-readable storage medium, characterized in that, The computer readable storage medium stores computer program instructions, and the computer program instructions are executed by the processor to implement the steps of the shared memory space management method according to any one of claims 1-6.
10. A computer program product, characterised in that, The computer program product comprises computer program instructions which, when executed by a processor, implement the steps of the shared memory space management method according to any one of claims 1 to 6.