Virtual machine memory allocation method, device and equipment

By prioritizing the allocation of memory segments aligned with large memory granularity, the problem of virtual machine memory allocation resulting in low TLB hit rate is solved, and the performance of virtual machines is improved.

CN114116119BActive Publication Date: 2025-06-06ALIBABA (CHINA) CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111221862.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-10-20
Publication Date
2025-06-06
Estimated Expiration
2041-10-20

AI Technical Summary

Technical Problem

In the prior art, the virtual machine memory allocation method leads to a low TLB hit rate and poor virtual machine performance.

Method used

In the virtual machine memory allocation method, memory segments aligned with large memory granularity are preferred according to the target memory size, thereby increasing the probability of cached large-map granularity mapping relationships in TLB.

Benefits of technology

By increasing the probability of cached large-map granularity mapping relationships in TLB, the performance of the virtual machine can be improved and the probability of hitting TLB can be improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114116119B_ABST
    Figure CN114116119B_ABST
Patent Text Reader

Abstract

The embodiment of the present application provides a virtual machine memory allocation method, device and equipment, the method comprising: if it is determined that the target memory size is greater than or equal to the first memory granularity, then searching for the first memory segment in the reserved memory until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed, and the found first memory segment is allocated to the virtual machine; if the target memory size is less than the first memory granularity, then searching for at least one second memory segment in the reserved memory, and the found at least one second memory segment is allocated to the virtual machine, the first memory segment is a section of free memory aligned according to the first memory granularity, the second memory segment is a section of free memory aligned according to the second memory granularity, and the first memory granularity is greater than the second memory granularity. When the end condition for searching for the first memory segment is traversing the reserved memory, it can be degraded to searching for the second memory segment. The method can increase the probability of hitting the TLB and improve the performance of the virtual machine.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a method, device and equipment for allocating memory to a virtual machine. Background Art

[0002] A virtual machine (VM) is a complete computer system that uses virtualization technology to simulate complete hardware system functions and runs in a completely isolated environment. Virtual machines are the basis of cloud computing platforms and computer hardware reuse. Multiple virtual machines can run concurrently on the same physical machine, increasing the utilization rate of the physical machine.

[0003] In the reserved memory scenario, if a virtual machine needs to apply for a piece of memory, the physical machine allocates memory to the virtual machine from the reserved memory. The usual memory allocation method mainly includes: for a section of free memory starting from the current allocation position in the reserved memory, first try to allocate 1G aligned memory to the virtual machine from the free memory, if 1G aligned memory cannot be allocated, then try to allocate 2M aligned memory to the virtual machine from the free memory, if 2M aligned memory cannot be allocated, then update the current allocation position to the starting address of the next section of free memory. Among them, the mapping granularity of 1G aligned memory can be 1G, and the mapping granularity of 2M aligned memory can be 2M.

[0004] However, with the above memory allocation method, there is a problem that the probability of hitting the mapping relationship between the virtual address cached in the TLB and the physical address is low, and the performance of the virtual machine is poor. Summary of the invention

[0005] The embodiments of the present application provide a virtual machine memory allocation method, device and equipment to solve the problem in the prior art that the probability of hitting TLB is low and the performance of the virtual machine is poor.

[0006] In a first aspect, an embodiment of the present application provides a virtual machine memory allocation method, comprising:

[0007] Determine the target memory size to be allocated to the virtual machine;

[0008] If the target memory size is greater than or equal to the first memory granularity, searching for a first memory segment in the reserved memory until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed and the found first memory segment is allocated to the virtual machine; the first memory segment is a section of free memory in the reserved memory aligned according to the first memory granularity, and the first size is equal to the integer part of the quotient of the target memory size and the first memory granularity multiplied by the first memory granularity;

[0009] If the target memory size is smaller than the first memory granularity, at least one second memory segment is searched in the reserved memory, and the at least one second memory segment found is allocated to the virtual machine, the second memory segment is a section of free memory in the reserved memory aligned according to the second memory granularity, the second memory granularity is smaller than the first memory granularity, and the sum of the sizes of the at least one second memory segment is equal to the target memory size.

[0010] In a second aspect, an embodiment of the present application provides a virtual machine memory allocation device, comprising:

[0011] A determination module, used to determine a target memory size that needs to be allocated to the virtual machine;

[0012] A first allocation module is configured to search for a first memory segment in the reserved memory if the target memory size is greater than or equal to a first memory granularity, until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed, and the found first memory segment is allocated to the virtual machine; the first memory segment is a section of free memory in the reserved memory aligned according to the first memory granularity, and the first size is equal to the integer part of the quotient of the target memory size and the first memory granularity multiplied by the first memory granularity;

[0013] A second allocation module is used to search for at least one second memory segment in the reserved memory and allocate the at least one second memory segment found to the virtual machine if the target memory size is smaller than the first memory granularity, wherein the second memory segment is a section of free memory in the reserved memory aligned according to the second memory granularity, the second memory granularity is smaller than the first memory granularity, and the sum of the sizes of the at least one second memory segment is equal to the target memory size.

[0014] In a third aspect, an embodiment of the present application provides a computer device, comprising: a memory and a processor; wherein the memory is used to store one or more computer instructions, wherein the one or more computer instructions, when executed by the processor, implement a method as described in any one of the above-mentioned first aspects.

[0015] In a fourth aspect, an embodiment of the present application provides a computer program, comprising computer program instructions, which, when executed by a processor, implement a method as described in any one of the above-mentioned first aspects.

[0016] In a fifth aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed, the method described in any one of the above-mentioned first aspects is implemented.

[0017] In an embodiment of the present application, when the target memory size that needs to be allocated to the virtual machine is greater than or equal to the large memory granularity, memory aligned according to the large memory granularity is allocated to the virtual machine from the reserved memory as much as possible, so that the number of mapping relationships of large mapping granularity established can be as large as possible, and the number of mapping relationships of small mapping granularity established can be as small as possible, thereby increasing the probability of caching the mapping relationship of large mapping granularity in the TLB, and because there are more virtual addresses that can hit a single mapping relationship of large mapping granularity, the probability of hitting the TLB can be increased by increasing the probability of caching the mapping relationship of small mapping granularity in the TLB, thereby improving the performance of the virtual machine. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0019] Figure 1 A flowchart of a virtual machine memory allocation method provided in one embodiment of the present application;

[0020] FIG. 2A to FIG. 2F A schematic diagram of allocating memory from a reserved memory to a virtual machine provided in an embodiment of the present application;

[0021] Figure 3 A schematic diagram of a process of searching for a first memory segment in a reserved memory provided in an embodiment of the present application;

[0022] Figure 4 This is a schematic diagram of selecting a second starting allocation position provided by an embodiment of the present application;

[0023] Figure 5 A schematic diagram of searching for a first memory segment and a second memory segment in a reserved memory provided in an embodiment of the present application;

[0024] Figure 6 A flowchart of a virtual machine memory allocation method provided by another embodiment of the present application;

[0025] Figure 7 A schematic diagram of the structure of a virtual machine memory allocation device provided in one embodiment of the present application;

[0026] Figure 8 A schematic diagram of the structure of a computer device provided in one embodiment of the present application. DETAILED DESCRIPTION

[0027] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0028] The terms used in the embodiments of the present application are only for the purpose of describing specific embodiments, and are not intended to limit the present application. The singular forms of "a", "said", and "the" used in the embodiments of the present application and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings, and "multiple" generally includes at least two, but does not exclude the inclusion of at least one.

[0029] It should be understood that the term "and / or" used in this article is only a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in this article generally indicates that the associated objects before and after are in an "or" relationship.

[0030] As used herein, the words "if" and "if" may be interpreted as "at the time of" or "when" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrases "if it is determined" or "if (stated condition or event) is detected" may be interpreted as "when it is determined" or "in response to determining" or "when detecting (stated condition or event)" or "in response to detecting (stated condition or event)", depending on the context.

[0031] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a product or system including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such a product or system. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the product or system including the elements.

[0032] In addition, the step sequence in the following method embodiments is only an example and not a strict limitation.

[0033] In order to facilitate those skilled in the art to understand the technical solution provided by the embodiments of the present application, the technical environment in which the technical solution is implemented is described below.

[0034] The technical solution of the embodiment of the present application can be applied to the memory allocation scenario of a virtual machine. A virtual machine is a computer system with complete hardware system functions that is simulated by a software environment. It can actually be a process running on a physical machine. The hardware and memory of the virtual machine are part of the hardware and memory on the physical machine. Among them, the computer on which the virtual machine is created can be called a physical machine or a host machine. In the reserved memory scenario, the physical machine can reserve a certain amount of physical memory for the virtual machine. If the virtual machine needs to apply for a piece of memory, the physical machine can allocate memory to the virtual machine from the reserved memory.

[0035] In order to facilitate the mapping of virtual addresses to physical addresses, the physical machine can allocate memory aligned according to a specific memory granularity to the virtual machine from the reserved memory. The memory aligned with a specific memory granularity can adopt the mapping of virtual addresses to physical addresses of a specific memory granularity, that is, the mapping granularity of virtual addresses to physical addresses can be a specific memory granularity. The specific memory granularity can be set according to actual usage requirements. The specific memory granularity can be 1G or 2M, for example. Taking the specific memory granularity of 1G as an example, the memory aligned with a specific memory granularity refers to a memory whose starting address is aligned with 1G (that is, the lower 30 bits of the starting address are all 0) and whose size is an integer multiple of 1G.

[0036] In addition, in order to achieve fast lookup from virtual address to physical address, the mapping relationship between virtual address and physical address can be cached in a translation lookaside buffer (TLB). When it is necessary to find the physical address corresponding to the virtual address, the virtual address can be first used to search in the TLB. If the TLB is hit, the physical address can be found directly. If it is not hit, the page table can be searched level by level to obtain the physical address.

[0037] Generally, when allocating memory to a virtual machine from a reserved memory, for a section of free memory starting from the current allocation position in the reserved memory, first try to allocate 1G aligned memory to the virtual machine from the section of free memory, if the 1G aligned memory cannot be allocated, then try to allocate 2M aligned memory to the virtual machine from the section of free memory, if the 2M aligned memory cannot be allocated, then update the current allocation position to the starting address of the next section of free memory, such allocation method will allocate 2M aligned memory to the virtual machine in most cases. Since the mapping granularity of 2M aligned memory is 2M, such allocation method will establish a large number of mapping relationships from virtual addresses with a mapping granularity of 2M to physical addresses, so that the probability of caching mapping relationships with a mapping granularity of 2M in the TLB is relatively high, and since there are fewer virtual addresses that can hit a single mapping relationship with a mapping granularity of 2M, when the probability of caching mapping relationships with a mapping granularity of 2M in the TLB is relatively high, there is a problem of low probability of hitting the TLB and poor performance of the virtual machine, which is particularly obvious when there are fewer mapping relationships in the TLB.

[0038] In order to solve the technical problem of low probability of hitting the TLB and poor performance of the virtual machine, in an embodiment of the present application, when the target memory size to be allocated to the virtual machine is greater than or equal to the large memory granularity, memory aligned according to the large memory granularity is allocated to the virtual machine from the reserved memory as much as possible, so that the number of mapping relationships of large mapping granularity established can be as large as possible, and the number of mapping relationships of small mapping granularity established can be as small as possible, thereby increasing the probability of caching the mapping relationships of large mapping granularity in the TLB, and because there are more virtual addresses that can hit a single mapping relationship of large mapping granularity, the probability of hitting the TLB can be increased by increasing the probability of caching the mapping relationships of small mapping granularity in the TLB, thereby improving the performance of the virtual machine.

[0039] In conjunction with the accompanying drawings, some embodiments of the present application are described in detail below. In the absence of conflict, the following embodiments and features in the embodiments can be combined with each other.

[0040] Figure 1 A flowchart of a virtual machine memory allocation method provided in an embodiment of the present application is shown in FIG. Figure 1 As shown, the method of this embodiment may include:

[0041] Step 11, determining the target memory size to be allocated to the virtual machine;

[0042] Step 12: if the target memory size is greater than or equal to the first memory granularity, search for a first memory segment in the reserved memory until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed and the found first memory segment is allocated to the virtual machine; the first memory segment is a section of free memory in the reserved memory aligned according to the first memory granularity, and the first size is equal to the integer part of the quotient of the target memory size and the first memory granularity multiplied by the first memory granularity;

[0043] Step 13: If the target memory size is smaller than the first memory granularity, search for at least one second memory segment in the reserved memory, and allocate the at least one second memory segment found to the virtual machine, where the second memory segment is a section of free memory in the reserved memory aligned according to the second memory granularity, the second memory granularity is smaller than the first memory granularity, and the sum of the sizes of the at least one second memory segment is equal to the target memory size.

[0044] Among them, the first memory granularity and the second memory granularity can be flexibly implemented according to needs. The following mainly takes the first memory granularity of 1G and the second memory granularity of 2M as an example for illustration. It should be noted that since the first memory granularity is larger than the second memory granularity, the first memory granularity can be understood as a large memory granularity, the second memory granularity can be understood as a small memory granularity, the mapping granularity of the first memory granularity can be understood as a large mapping granularity, and the mapping granularity of the second memory granularity can be understood as a small mapping granularity.

[0045] Exemplarily, the memory size requested by the virtual machine can be used as the memory size that needs to be allocated to the virtual machine (i.e., the target memory size), and the target memory size can be greater than or equal to the first memory granularity, or the target memory size can be smaller than the first memory granularity. If the target memory size is greater than or equal to the first memory granularity, memory aligned according to the large memory granularity can be allocated to the virtual machine as much as possible, that is, memory aligned according to the first memory granularity can be allocated to the virtual machine as much as possible; if the target memory size is smaller than the second memory granularity, memory aligned according to the small memory granularity can be allocated to the virtual machine, that is, memory aligned according to the small memory granularity can be allocated to the virtual machine as much as possible.

[0046] In an embodiment of the present application, if the target memory size can be greater than or equal to the first memory granularity, the first memory segment can be searched in the reserved memory until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed and the found first memory segment is allocated to the virtual machine, so as to allocate memory aligned according to the first memory granularity to the virtual machine as much as possible.

[0047] Among them, taking the first memory granularity as 1G as an example, the first memory segment can specifically be a segment of free memory with a starting address aligned to 1G and a size that is an integer multiple of 1G. It should be understood that in the embodiment of the present application, a segment of free memory refers to a segment of memory in which all memories are free. If part of a segment of memory is not free, then this segment of memory is not a segment of free memory.

[0048] It should be noted that the number of first memory segments found may be 0 or one or more. When the end condition for finding the first memory segment is to find at least one first memory segment whose sum of sizes is the first size, the number of first memory segments found is at least one, and the sum of the sizes of the at least one first memory segment is equal to the first size; when the end condition for finding the first memory segment is to traverse the reserved memory, the number of first memory segments found may be 0, 1 or more, and when the number of first memory segments found is 1 or more, the sum of the sizes of the 1 or more first memory segments is less than the first size. Among them, the first size is equal to the integer part of the quotient of the target memory size and the first memory granularity multiplied by the first memory granularity. Taking the target memory size of 3.5G as an example, the first size is equal to 3.5G divided by the integer part of 3G (i.e. 3) multiplied by 1G, that is, the first size is equal to 3G.

[0049] Example 1: Assume that the first memory granularity is 1G, the target memory size is 3.5G, and Figure 2A The size of free memory segment A is 3.5G, then by executing step 12, Figure 2B As shown, a memory segment A1 with a size of 3G and aligned according to 1G in the free memory segment A can be allocated to the virtual machine. The memory segment A1 is a first memory segment found, and the size of the found memory segment A1 is equal to the first size 3G.

[0050] Example 2: Assume that the first memory granularity is 1G, the target memory size is 3G, and Figure 2C The size of free memory segment B is 1.5G, and the size of free memory segment C is 2.5G. Then, by executing step 12, Figure 2D As shown, a memory segment B1 of 1G in size and aligned to 1G in free memory segment B and a memory segment C1 of 2G in size and aligned to 1G in free memory segment C can be allocated to the virtual machine. Memory segment B1 and memory segment C1 are the two first memory segments found, and the sum of the sizes of the found memory segments B1 and C1 is equal to the first size 3G.

[0051] Example 3: Assume that the first memory granularity is 1G, the target memory size is 3G, and Figure 2E The sizes of free memory segments D and E are both 1.5G, and the sizes of other free memory segments F, G, etc. are less than 1G. Then, execute step 12, such as Figure 2FAs shown, a memory segment D1 with a size of 1G and aligned with 1G in the free memory segment D and a memory segment E1 with a size of 1G and aligned with 1G in the free memory segment E can be allocated to the virtual machine. The memory segment D1 and the memory segment E1 are the two first memory segments found, and the sum of the sizes of the found memory segments D1 and E1 is less than the first size 3G.

[0052] It should be noted that FIG. 2A to FIG. 2F A medium gray filled rectangle may represent a non-free memory segment, and a white filled rectangle may represent a free memory segment.

[0053] It should be understood that any method that can realize searching for the first memory segment in the reserved memory so as to allocate the first memory segment to the virtual machine as much as possible from the reserved memory belongs to the protection scope of this application. Figure 3 As shown, searching for the first memory segment in the reserved memory until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed, may specifically include the following steps 31 to 35.

[0054] Step 31 : starting from the current allocation position corresponding to the first memory granularity in the reserved memory, a first starting allocation position aligned according to the first memory granularity is selected.

[0055] Step 32: In the reserved memory, a section of free memory starting from the first starting allocation position is searched, and alignment processing of the found section of free memory at a first memory granularity is performed to obtain a first aligned memory.

[0056] Step 33: If the size of the memory after the first alignment is 0, determine whether the reserved memory has been traversed.

[0057] If the reserved memory has been traversed, the search may be terminated; if not, step 35 may be continued.

[0058] Step 34: if the size of the first aligned memory is greater than 0, the first aligned memory is a first memory segment found, and it is determined whether the sum of the sizes of the first memory segments found reaches the first size.

[0059] If the sum of the sizes of the first memory segments found is the first size, the search may be terminated; if not, step 35 may be executed;

[0060] Step 35, update the current allocation position corresponding to the first memory granularity, starting from the updated current allocation position corresponding to the first memory granularity in the reserved memory, select the first starting allocation position aligned according to the first memory granularity, and return to step 32 to execute.

[0061] Among them, the memory at the current allocation position corresponding to the first memory granularity may be free memory, the first starting allocation position suitable for the first memory granularity refers to the allocation position aligned with the first memory granularity, and the memory at the first starting allocation position is free memory. Exemplarily, starting from the current allocation position corresponding to the first memory granularity in the reserved memory, along the first direction, the first allocation position aligned with the first memory granularity is selected as the first starting allocation position. Exemplarily, the first direction may include an address growth direction.

[0062] For example, suppose the current allocation location in the reserved memory corresponding to the first memory granularity is Figure 2A If position a in is not a 1G aligned position, then Figure 2B As shown, we can start from position a and follow Figure 2B Select position b as the first starting allocation position in the direction indicated by the arrow, and follow Figure 2B The direction indicated by the arrow in the middle searches for a section of free memory starting from position b in the reserved memory, for example Figure 2B The memory from position b to position c in the figure is aligned to 1G to obtain the first aligned memory. For example, Figure 2B The memory segment from position b to position d in the example 1 (i.e., memory segment A1 of size 3G) is found. Since the size of the found memory segment A1 is equal to the first size 3G, the search can be terminated, thereby allocating memory segment A1 to the virtual machine in the above example 1.

[0063] For another example, suppose the current allocation location corresponding to the first memory granularity in the reserved memory is Figure 2C If position e in is 1G aligned, then Figure 2D As shown, you can follow Figure 2D The direction indicated by the arrow in the middle searches for a section of free memory starting from position e in the reserved memory, for example Figure 2D The memory from position e to position f in the figure is aligned to 1G to obtain the first aligned memory. For example, Figure 2D The memory segment from position e to position g in the memory (ie, the memory segment B1 of size 1G). Since the size of the found memory segment B1 is smaller than the first size 3G, the current allocation position corresponding to the first memory granularity can be updated.

[0064] Further assume that the current allocation position is updated to Figure 2D The next position of position g in , then Figure 2D As shown, we can start from the next position of position g and follow Figure 2D Select position h as the first starting allocation position in the direction indicated by the middle arrow, and follow Figure 2DThe direction indicated by the arrow in the middle searches for a section of free memory starting from position h in the reserved memory, for example Figure 2D The memory from position h to position i in the image is found, and the free memory is aligned to 1G to obtain the first aligned memory, for example Figure 2D The memory segment from position h to position j in the memory (i.e., memory segment C1 of size 2G). Since the sum of the sizes of the found memory segments B1 and C1 is equal to the first size 3G, the search can be terminated, so that the memory segments B1 and C1 can be allocated to the virtual machine in the above example 2.

[0065] For another example, suppose the current allocation location corresponding to the first memory granularity in the reserved memory is Figure 2E If position k in is 1G aligned, then Figure 2F As shown, you can follow Figure 2F In the direction indicated by the arrow, search for a section of free memory starting from position k in the reserved memory, for example Figure 2F The memory from position k to position l in the image is found, and the free memory is aligned to 1G to obtain the first aligned memory, for example Figure 2F The memory segment from position k to position m (ie, the memory segment D1 of size 1G) is found. Since the size of the found memory segment D1 is smaller than the first size 3G, the current allocation position corresponding to the first memory granularity can be updated.

[0066] Furthermore, suppose the current allocation position is updated to Figure 2F The next position of position m in , then Figure 2F As shown, we can start from the next position of position m and follow Figure 2F Select position n as the first starting allocation position in the direction indicated by the arrow, and follow Figure 2F The direction indicated by the arrow in the middle searches for a section of free memory starting from position n in the reserved memory, for example Figure 2F The memory from position n to position o in the image is found, and the free memory is aligned to 1G to obtain the first aligned memory, for example Figure 2F The memory segment from position n to position p in the memory (ie, the memory segment E1 of size 2G). Since the sum of the sizes of the found memory segments D1 and E1 is less than 3G, the current allocation position corresponding to the first memory granularity can be updated.

[0067] Furthermore, suppose the current allocation position is updated to Figure 2F The next position of position p in , then Figure 2F As shown, we can start from the next position of position p and follow Figure 2F Select position q as the first starting allocation position in the direction indicated by the middle arrow, and search for a section of free memory starting from position q in the reserved memory, for example Figure 2FThe memory from position q to position r in the traversal is found, and the found memory is aligned to 1G. Since the size of memory segment F is less than 1G, the size of the first aligned memory is 0. Assuming that the reserved memory has not been traversed at this time, the current allocation position corresponding to the first memory granularity can be updated. Further assuming that the current allocation position is updated to Figure 2F If the position s in is 1g aligned, then Figure 2F As shown, you can follow Figure 2F The direction indicated by the arrow in the middle searches for a section of free memory starting from position s in the reserved memory, for example Figure 2F The memory segment from position s to position t in the example above is found, and the memory segment is aligned to 1G. Since the size of memory segment G is also less than 1G, the size of the first aligned memory is 0. Assuming that the reserved memory has been traversed at this time, the search can be terminated, so that the memory segments D1 and E1 can be allocated to the virtual machine in the above example 1.

[0068] Optional, such as Figure 3 As shown, before step 32, the following steps 36 to 38 may also be included.

[0069] Step 36, determining whether the target memory size is an integer multiple of the first memory size.

[0070] If the target memory size is an integer multiple of the first memory granularity, step 37 may be executed, otherwise step 32 may be executed.

[0071] Step 37, determining whether all the memory of the continuous target memory size starting from the first starting allocation position in the reserved memory is free.

[0072] Among them, if in the reserved memory, the memory of the continuous target memory size starting from the first starting allocation position is all free, then the memory of the continuous target memory size starting from the first starting allocation position is the first memory segment with the size of the target memory size found, so that the memory of the continuous target memory size starting from the first starting allocation position can be allocated to the virtual machine, otherwise step 32 can be executed.

[0073] Through the above steps 36 and 37, it is possible to preferentially try to allocate a whole segment of continuous memory to the virtual machine when the target memory size is larger than the first memory granularity and is an integer multiple of the first memory granularity.

[0074] Optionally, when the end condition of searching for the first memory segment is to traverse the reserved memory, it can also degenerate to searching for the second memory segment in the reserved memory. Based on this, the method provided in the embodiment of the present application can also include: searching for at least one second memory segment whose sum of sizes is the second size in the reserved memory, and allocating the at least one second memory segment found to the virtual machine, wherein the second size is the difference between the first size and the sum of the sizes of the first memory segments found. Thus, for the integer multiple of the target memory size relative to the first memory granularity, when no free memory segment (i.e., the first memory segment) aligned according to the large memory granularity can be found by traversing the reserved memory, the free memory segment (i.e., the second memory segment) aligned according to the small memory granularity in the reserved memory can be allocated to the virtual machine, so that on the basis of achieving the goal of allocating memory aligned according to the large memory granularity to the virtual machine as much as possible, at least one memory segment whose sum of sizes is the first size can be allocated to the virtual machine from the reserved memory.

[0075] For example, for the above example 3, the first size is 3G, and the second size is 3-2=1G. Based on example 3, assuming that the second memory granularity is 2M, then Figure 2F As shown, a memory segment F1 with a size of 0.5G and aligned with 2M in the free memory segment F and a memory segment G1 with a size of 0.5G and aligned with 2M in the free memory segment G can also be allocated to the virtual machine.

[0076] It should be understood that the decimal part of the quotient of the target memory size and the first memory granularity may be 0 or non-0. Optionally, when the decimal part of the target memory size and the first memory granularity is non-0, the method provided in the embodiment of the present application may also include: searching for at least one second memory segment whose sum of sizes is a third size in the reserved memory, and allocating the at least one second memory segment found to the virtual machine, the third size being equal to the decimal part of the quotient of the target memory size and the first memory granularity multiplied by the first memory granularity. Thereby, for the decimal multiple of the target memory size relative to the first memory, the free memory segment (i.e., the second memory segment) aligned according to the small memory granularity in the reserved memory can be allocated to the virtual machine, so that the size of the memory actually allocated to the virtual machine can be equal to the target memory size that needs to be allocated to the virtual machine.

[0077] In the embodiment of the present application, if the target memory size is smaller than the first memory granularity, at least one second memory segment can be found in the reserved memory, and the at least one second memory segment found can be allocated to the virtual machine. It should be understood that the sum of the sizes of the at least one second memory segment can be the target memory size, thereby allocating memory aligned according to the second memory granularity to the virtual machine. Optionally, finding at least one second memory segment in the reserved memory can specifically include the following steps A' to E'.

[0078] Step A', starting from the current allocation position corresponding to the second memory granularity in the reserved memory, select a second starting allocation position aligned according to the second memory granularity.

[0079] Step B': searching for a section of free memory starting from the second starting allocation position in the reserved memory, and performing alignment processing of the found section of free memory at a second memory granularity to obtain a second aligned memory.

[0080] Step C', if the size of the second aligned memory is greater than 0, the second aligned memory is a found second memory segment, and it is determined whether the sum of the sizes of the found second memory segments reaches the target memory size.

[0081] If the sum of the sizes of the second memory segments that have been found reaches the target memory size, the search may be terminated; if the sum of the sizes of the second memory segments that have been found does not reach the target memory size, step E' may be executed.

[0082] Step D': if the size of the second aligned memory is 0, execute step E'.

[0083] Step E', update the current allocation position corresponding to the second memory granularity, update the current allocation position corresponding to the second memory granularity, start from the updated current allocation position corresponding to the second memory granularity in the reserved memory, select the second starting allocation position aligned according to the second memory granularity, and return to step B' for execution.

[0084] Among them, the memory of the current allocation position corresponding to the second memory granularity can be free memory, the second starting allocation position suitable for the second memory granularity refers to the allocation position aligned with the second memory granularity, and the memory of the second starting allocation position is free memory. Exemplarily, starting from the current allocation position corresponding to the second memory granularity in the reserved memory, along the second direction opposite to the first direction, the first allocation position aligned with the second memory granularity can be selected as the second starting allocation position. Exemplarily, the second direction can include the address descending direction. By selecting the direction along which the second starting allocation position is opposite to the direction along which the first starting allocation position is selected, the direction of searching for the first memory segment in the reserved memory can be opposite to the direction of searching for the second memory segment in the reserved memory, which is conducive to improving the efficiency of searching for the first memory segment.

[0085] The address increase direction can be recorded as forward, and the allocation to the first memory segment can be understood as forward allocation. The address decrease direction can be recorded as reverse, and the allocation to the second memory segment can be understood as reverse allocation. Figure 5As shown, when 1.5G memory needs to be allocated to virtual machine 1, memory segment 1 with a size of 1G can be allocated to virtual machine 1 through forward allocation, and memory segment 2 with a size of 0.5G can be allocated to virtual machine 1 through reverse allocation; then, when 1.5G memory needs to be allocated to virtual machine 2, memory segment 3 with a size of 1G can be allocated to virtual machine 2 through forward allocation, and memory segment 4 with a size of 0.5G can be allocated to virtual machine 2 through reverse allocation; then, when 1.5G memory needs to be allocated to virtual machine 3, memory segment 5 with a size of 1G can be allocated to virtual machine 3 through forward allocation, and memory segment 6 with a size of 0.5G can be allocated to virtual machine 3 through reverse allocation; then, when 0.5G memory needs to be allocated to virtual machine 4, memory segment 7 with a size of 0.5G can be allocated to virtual machine 4 through reverse allocation.

[0086] Optionally, step A' may specifically include: starting from the current allocation position corresponding to the second memory granularity in the reserved memory, searching for a second starting allocation position suitable for the second memory granularity where all memories of the first memory granularity are not idle, until the reserved memory is found or traversed.

[0087] For example, assuming that the first memory granularity is 1G, and the second memory granularity is 2M, and Figure 4 The position u in the reserved memory space is the current allocation position corresponding to the second memory granularity, then Figure 4 As shown, we can start from position u and follow Figure 4 The second starting allocation position suitable for the second memory granularity and where the memory of the first memory granularity is not all free is searched in the direction indicated by the middle arrow. Further assuming that position v and position w are both starting allocation positions suitable for the second memory granularity, and the 1G memory where position v is located is all free, and there is non-free memory in the 1G memory where position w is located (that is, the 1G memory where position w is located is not all free), then position w can be the second starting allocation position found. It should be noted that Figure 4 A medium gray filled rectangle may represent a non-free memory segment, and a white filled rectangle may represent a free memory segment.

[0088] By searching for a second starting allocation position that is suitable for the second memory granularity and where the memory of the first memory granularity is not all free, until the reserved memory is found or traversed, it is achieved that the fragmented position of the memory of the first memory granularity is selected as the starting allocation position for allocating the second memory segment as much as possible, so as to minimize the fragmentation of the free memory segments aligned according to the first memory granularity due to the selection of the second starting allocation position. By searching for the first memory segment in the reserved memory in a direction opposite to the direction of searching for the second memory segment in the reserved memory, when selecting the second starting allocation position aligned according to the second memory granularity by searching for the second starting allocation position that is suitable for the second memory granularity and where the memory of the first memory granularity is not all free, it is beneficial to improve the efficiency of selecting the second starting allocation position, thereby facilitating the efficiency of finding the second memory segment.

[0089] Further optionally, step A' may also include: if a second starting allocation position suitable for the second memory granularity is not found and not all memories of the first memory granularity are free, then the first found starting allocation position suitable for the second memory granularity is used as the second starting allocation position suitable for the second memory granularity.

[0090] Optionally, the following steps F' to G' may be included before step B'.

[0091] Step F', determining whether the target memory size is an integer multiple of the second memory size.

[0092] If the target memory size is an integer multiple of the second memory granularity, step G' may be executed, otherwise step B' may be executed.

[0093] Step G', determining whether all the memory of the continuous target memory size starting from the second starting allocation position in the reserved memory is free.

[0094] Among them, if in the reserved memory, the memory of the continuous target memory size starting from the second starting allocation position is all free, then the memory of the continuous target memory size starting from the second starting allocation position is the second memory segment with the size of the target memory size found, so that the memory of the continuous target memory size starting from the second starting allocation position can be allocated to the virtual machine, otherwise step B' can be executed.

[0095] Through the above steps F' to G', it is possible to preferentially try to allocate a whole segment of continuous memory to the virtual machine when the target memory size is smaller than the first memory granularity and is an integer multiple of the second memory granularity.

[0096] It should be noted that, when searching for at least one second memory segment whose sum of sizes is the second size in the reserved memory, a similar method as shown in steps A' to E' may be used for searching, which will not be described in detail herein.

[0097] It should be noted that, when searching for at least one second memory segment whose sum of sizes is the third size in the reserved memory, a similar method as shown in steps A' to E' may be used for searching, which will not be described in detail herein.

[0098] For example, the first memory size is 1G and the second memory size is 2M. Figure 6 As shown, the virtual machine memory allocation method may include steps 61 to 618 as follows.

[0099] Step 61, determining whether the target memory size to be allocated to the virtual machine is greater than the total free memory size of the reserved memory.

[0100] If the target memory size is larger than the total free memory size of the reserved memory, the process may exit directly; otherwise, the process may execute step 62 .

[0101] Step 62: Determine a first allocation direction and a first allocation size according to the target memory size.

[0102] Specifically, when the target memory size is greater than or equal to 1G, the first allocation direction may be forward, and the first allocation size in the forward direction may be equal to the target memory size; when the target memory size is less than 1G, the first allocation direction may be reverse, and the first allocation size in the reverse direction may be equal to the target memory size. Taking the target memory size of 3.5G as an example, the first allocation direction is forward, and the first allocation size is 3.5G; taking the target memory size of 3G as an example, the first allocation direction is forward, and the first allocation size is 3G; taking the target memory size of 0.5G as an example, the first allocation direction is reverse, and the first allocation size is 0.5G.

[0103] Step 63, determining whether the first allocation size meets the alignment requirement.

[0104] Specifically, when the first allocation direction is forward, it can be determined whether the first allocation size is an integer multiple of 1G. If so, it means that the alignment requirement is met. If not, it means that the alignment requirement is not met. When the first allocation direction is reverse, it can be determined whether the first allocation size is an integer multiple of 2M. If so, it means that the alignment requirement is met. If not, it means that the alignment requirement is not met. Taking the first allocation direction as forward and the first allocation size as 3.5G as an example, the first allocation size does not meet the alignment requirement; taking the first allocation direction as forward and the first allocation size as 3G as an example, the first allocation size meets the alignment requirement; taking the first allocation direction as reverse and the first allocation size as 0.5G as an example, the first allocation size meets the alignment requirement.

[0105] If the first allocation size meets the alignment requirement, the following step 64 may be executed to try to allocate a whole segment of continuous memory to the virtual machine; if the first allocation size does not meet the alignment requirement, step 66 may be executed to perform segmented allocation.

[0106] Step 64: Select a suitable starting allocation position according to the first allocation direction.

[0107] Specifically, if the first allocation direction is forward, you can start from the current allocation position corresponding to 1G and select a free memory position aligned with 1G as the starting allocation position. If the first allocation direction is reverse, you can start from the current allocation position corresponding to 2M and search for a free memory position aligned with 2M in the reverse direction as the starting allocation position.

[0108] If the first allocation direction is reverse, you can start from the current allocation position corresponding to 2M, search in reverse for the first 2M-aligned free memory position, and determine whether the entire 1G memory area where the first 2M-aligned free memory position is located is a fragmented memory segment. If so, you can use the first 2M-aligned free memory position as the starting allocation position. If not, you can continue to search for the second 2M-aligned free memory position, and determine whether the entire 1G memory area where the second 2M-aligned free memory position is located is a fragmented memory segment. If so, you can use the second 2M-aligned free memory position as the starting allocation position. If not, you can continue to search for the third 2M-aligned free memory position, ...; if you traverse the reserved memory and do not find a 2M-aligned free memory position where the entire 1G memory area is a fragmented memory segment, you can select the 2M-aligned free memory position found in the first reverse search as the starting allocation position. It should be noted that when a whole segment of 1G memory area is not all free, the whole segment of 1G memory area can be considered as a fragmented memory segment; when a whole segment of 1G memory area is all free, the whole segment of 1G memory area can be considered as not a fragmented memory segment.

[0109] Step 65, determining whether all the consecutive memories of the first allocation size in the first allocation direction starting from the initial allocation position in the reserved memory are free.

[0110] Specifically, when the first allocation direction is forward, it can be determined whether all the first allocation size of the memory in the reserved memory is free from the start allocation position in the forward direction. When the first allocation direction is reverse, it can be determined whether all the first allocation size of the memory in the reserved memory is free from the start allocation position in the reverse direction.

[0111] If all the continuous first allocation size memory in the first allocation direction starting from the start allocation position in the reserved memory is free, it means that a whole continuous memory segment is found, so the whole continuous memory segment can be allocated to the virtual machine, otherwise step 66 can be executed to perform segmented allocation.

[0112] Step 66: Determine a second allocation direction and a second allocation size according to the target memory size.

[0113] Specifically, when the target memory size is greater than or equal to 1G, and the decimal part of the quotient of the target memory size and 1G is 0, the second allocation direction may include a forward direction, wherein the second allocation size in the forward direction may be equal to the target memory size; when the target memory size is greater than or equal to 1G, and the decimal part of the quotient of the target memory size and 1G is non-0, the second allocation direction may include a forward direction and a reverse direction, wherein the second allocation size in the forward direction may be equal to the integer part of the quotient of the target memory size and 1G multiplied by 1G, and the second allocation size in the reverse direction may be equal to the decimal part of the quotient of the target memory size and 1G multiplied by 1G; when the target memory size is less than 1G, the second allocation direction may include a reverse direction, and the second allocation size in the reverse direction may be equal to the target memory size.

[0114] Step 67, selecting a suitable starting allocation position according to the second allocation direction.

[0115] It should be noted that the specific method of determining the starting allocation position according to the second allocation direction may be similar to determining the appropriate starting allocation position according to the first allocation direction, and will not be described in detail herein.

[0116] When the second allocation direction includes a forward direction, the appropriate starting allocation position includes a 1G alignment position, and step 68 may be performed; when the second allocation direction includes a reverse direction, the appropriate starting allocation position includes a 2M alignment position, and step 612 may be performed.

[0117] Step 68, in the reserved memory, forward search for a section of free memory starting from the selected 1G alignment position.

[0118] Step 69, align the found free memory to obtain 1G aligned memory.

[0119] If the size of the 1G aligned memory is 0, it may indicate that a 1G aligned free memory starting from the currently selected 1G aligned position is not found, and step 610 may be further performed; if the size of the 1G aligned memory is greater than 0, it may indicate that a 1G aligned free memory starting from the currently selected 1G aligned position is found, and step 611 may be further performed. It should be understood that if a 1G aligned free memory is found, the found 1G aligned free memory may be allocated to the virtual machine.

[0120] Step 610, determine whether the reserved memory has been traversed.

[0121] If yes, execute step 615 to perform a reverse search of the 2M aligned memory in a degenerate manner; otherwise, select the next suitable 1G aligned position and return to step 68 for execution.

[0122] Step 611, determine whether the first remaining size is 0.

[0123] The first remaining size may be equal to the difference between the second forward allocation size and the sum of the sizes of at least one 1G aligned free memory segment that has been found. If the first remaining size is 0, it indicates that the allocation is complete, otherwise the next suitable 1G aligned position may be selected, and the process returns to step 68 for execution.

[0124] Step 612, in the reserved memory, reversely search for a section of free memory starting from the selected 2M alignment position.

[0125] Step 613, align the found free memory to obtain 2M aligned memory.

[0126] If the size of the 2M aligned memory is 0, it may indicate that a 2M aligned free memory segment starting from the currently selected 2M aligned position has not been found, and the next suitable 2M aligned position may be further selected, and the process returns to step 612 for execution. If the size of the 2M aligned memory is greater than 0, it may indicate that a 2M aligned free memory segment starting from the currently selected 2M aligned position has been found, and step 614 may be further executed. It should be understood that if a 2M aligned free memory segment is found, the found 2M aligned free memory segment may be allocated to the virtual machine.

[0127] Step 614, determining whether the second remaining size is zero.

[0128] The second remaining size may be equal to the difference between the second forward allocation size and the sum of the sizes of at least one 2M aligned free memory segment found through step 612 and step 613. If the second remaining size is 0, it indicates that the allocation is complete, otherwise the next suitable 2M aligned position may be selected, and the process returns to step 612 for execution.

[0129] Step 615, select the 2M alignment position.

[0130] Step 616, in the reserved memory, reversely search for a section of free memory starting from the selected 2M alignment position.

[0131] Step 617, align the found free memory to obtain 2M aligned memory.

[0132] If the size of the 2M aligned memory is 0, it may indicate that a 2M aligned free memory segment starting from the currently selected 2M aligned position has not been found, and the next suitable 2M aligned position may be further selected, and the process returns to step 616 for execution. If the size of the 2M aligned memory is greater than 0, it may indicate that a 2M aligned free memory segment starting from the currently selected 2M aligned position has been found, and step 618 may be further executed. It should be understood that if a 2M aligned free memory segment is found, the found 2M aligned free memory segment may be allocated to the virtual machine.

[0133] Step 618, determining whether the third remaining size is zero.

[0134] The third remaining size may be equal to the difference between the positive second allocation size and the sum result, which is the sum of the sizes of at least one 1G-aligned free memory found through step 68 and step 69, and the sum of the sizes of at least one 2M-aligned free memory found through step 616 and step 617. If the third remaining size is 0, it means that the allocation is complete, otherwise the next suitable 2M-aligned position may be selected, and the process returns to step 616 for execution.

[0135] The virtual machine memory allocation method provided in the embodiment of the present application is as follows: if it is determined that the target memory size to be allocated to the virtual machine is greater than or equal to the first memory granularity, the first memory segment is searched in the reserved memory until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed and the found first memory segment is allocated to the virtual machine; if the target memory size is less than the first memory granularity, at least one second memory segment is searched in the reserved memory, and the at least one second memory segment found is allocated to the virtual machine, wherein the first memory segment is a section of free memory in the reserved memory aligned according to the first memory granularity, and the second memory segment is a section of free memory in the reserved memory aligned according to the second memory granularity Free memory, the first memory granularity is greater than the second memory granularity, so that when the target memory size to be allocated to the virtual machine is greater than or equal to the large memory granularity, memory aligned according to the larger memory granularity is allocated to the virtual machine from the reserved memory as much as possible, so that the number of mapping relationships of the large mapping granularity established can be as large as possible, and the number of mapping relationships of the small mapping granularity established can be as small as possible, thereby increasing the probability of caching the mapping relationship of the large mapping granularity in the TLB, and because there are more virtual addresses that can hit a single mapping relationship of the large mapping granularity, the probability of hitting the TLB can be increased by increasing the probability of caching the mapping relationship of the small mapping granularity in the TLB, thereby improving the performance of the virtual machine.

[0136] Figure 7 A schematic diagram of the structure of a virtual machine memory allocation device provided in an embodiment of the present application; Figure 7 As shown, this embodiment provides a virtual machine memory allocation device, which can execute the above method. Specifically, the device may include:

[0137] A determination module 71 is used to determine a target memory size to be allocated to the virtual machine;

[0138] A first allocation module 72 is configured to search for a first memory segment in the reserved memory if the target memory size is greater than or equal to a first memory granularity, until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed, and the found first memory segment is allocated to the virtual machine; the first memory segment is a section of free memory in the reserved memory aligned according to the first memory granularity, and the first size is equal to the integer part of the quotient of the target memory size and the first memory granularity multiplied by the first memory granularity;

[0139] A second allocation module 73 is used to search for at least one second memory segment in the reserved memory and allocate the at least one second memory segment found to the virtual machine if the target memory size is smaller than the first memory granularity, wherein the second memory segment is a section of free memory in the reserved memory aligned according to the second memory granularity, the second memory granularity is smaller than the first memory granularity, and the sum of the sizes of the at least one second memory segment is equal to the target memory size.

[0140] Optionally, the first allocation module 72 is configured to search for the first memory segment in the reserved memory until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed, specifically including:

[0141] Starting from a current allocation position in the reserved memory corresponding to the first memory granularity, selecting a first starting allocation position aligned according to the first memory granularity;

[0142] In the reserved memory, searching for a section of free memory starting from the first starting allocation position, and performing alignment processing of a first memory granularity on the found section of free memory to obtain a first aligned memory;

[0143] If the size of the first aligned memory is 0, determining whether the reserved memory has been traversed, and if so, ending the search;

[0144] If the size of the first aligned memory is greater than 0, the first aligned memory is a found first memory segment, and it is determined whether the sum of the sizes of the found first memory segments reaches the first size, and if so, the search is terminated;

[0145] Otherwise, update the current allocation position corresponding to the first memory granularity, start from the updated current allocation position corresponding to the first memory granularity in the reserved memory, select the first starting allocation position aligned according to the first memory granularity, and return to the step of searching for a section of free memory starting from the first starting allocation position in the reserved memory.

[0146] Optionally, the first allocation module 72 is further used for:

[0147] Determining whether the target memory size is an integer multiple of the first memory size;

[0148] If the target memory size is an integer multiple of the first memory granularity, determining whether all consecutive memories of the target memory size starting from the first starting allocation position in the reserved memory are free;

[0149] If all are free, the memory of the target memory size continuous from the first starting allocation position is the first memory segment of the target memory size found;

[0150] If the target memory size is not an integer multiple of the first memory granularity, or if the target memory size is an integer multiple of the first memory granularity but the continuous memory of the target memory size starting from the first starting allocation position in the reserved memory is not all free, then execute the step of searching for a section of free memory starting from the first starting allocation position in the reserved memory.

[0151] Optionally, the first allocation module 72 is further used for:

[0152] When the end condition for finding the first memory segment is traversing the reserved memory, searching the reserved memory for at least one second memory segment whose sum of sizes is a second size, and allocating the at least one second memory segment found to the virtual machine, the second size being a difference between the first size and the sum of sizes of the first memory segments found.

[0153] Optionally, the first allocation module 72 is further used for:

[0154] When the decimal part of the quotient of the target memory size and the first memory granularity is non-zero, search the reserved memory for at least one second memory segment whose sum of sizes is a third size, and allocate the at least one second memory segment found to the virtual machine, where the third size is equal to the decimal part of the quotient of the target memory size and the first memory granularity multiplied by the first memory granularity.

[0155] Optionally, the second allocation module 73 is used to search for at least one second memory segment in the reserved memory, specifically including:

[0156] Starting from a current allocation position in the reserved memory corresponding to the second memory granularity, selecting a second starting allocation position aligned according to the second memory granularity;

[0157] In the reserved memory, searching for a section of free memory starting from the second starting allocation position, and performing alignment processing of the found section of free memory at a second memory granularity to obtain a second aligned memory;

[0158] If the size of the second aligned memory is greater than 0, the second aligned memory is a found second memory segment, and it is determined whether the sum of the sizes of the found second memory segments reaches the target memory size. If yes, the search is terminated;

[0159] If the size of the second aligned memory is 0, or the sum of the sizes of the second memory segments that have been found does not reach the target memory size, the current allocation position corresponding to the second memory granularity is updated, and starting from the updated current allocation position corresponding to the second memory granularity in the reserved memory, the second starting allocation position aligned according to the second memory granularity is selected, and the step of searching for a section of free memory starting from the second starting allocation position in the reserved memory is returned to be executed.

[0160] Optionally, the second allocation module 73 is further used for:

[0161] Determining whether the target memory size is an integer multiple of the second memory size;

[0162] If the target memory size is an integer multiple of the second memory granularity, determining whether all the memories of the target memory size in the reserved memory starting from the second starting allocation position are free;

[0163] If all are free, the memory of the target memory size continuous from the second starting allocation position is the found second memory segment of the target memory size;

[0164] If the target memory size is not an integer multiple of the second memory granularity, or if the target memory size is an integer multiple of the second memory granularity but the continuous memory of the target memory size starting from the second starting allocation position in the reserved memory is not all free, then execute the step of searching for a section of free memory starting from the second starting allocation position in the reserved memory.

[0165] Optionally, the second allocation module 73 is configured to select, starting from the current allocation position corresponding to the second memory granularity in the reserved memory, a second starting allocation position aligned according to the second memory granularity, specifically including:

[0166] Starting from the current allocation position corresponding to the second memory granularity in the reserved memory, look for a second starting allocation position suitable for the second memory granularity and where all memories of the first memory granularity are not idle, until the reserved memory is found or traversed.

[0167] Optionally, the direction of searching for the first memory segment in the reserved memory is opposite to the direction of searching for the second memory segment in the reserved memory.

[0168] Figure 7The device shown can execute the method provided by the embodiment shown in the above method. For the part not described in detail in this embodiment, reference can be made to the relevant description of the above method embodiment. The execution process and technical effect of the technical solution refer to the description in the above method embodiment, which will not be repeated here.

[0169] In one possible implementation, Figure 7 The structure of the device shown can be implemented as a computer device. Figure 8 As shown, the computer device may include: a processor 81 and a memory 82. The memory 82 is used to store a program that supports the computer device to execute the method provided in the above method embodiment, and the processor 81 is configured to execute the program stored in the memory 82.

[0170] The program includes one or more computer instructions, wherein when the one or more computer instructions are executed by the processor 81, the following steps can be implemented:

[0171] Determine the target memory size to be allocated to the virtual machine;

[0172] If the target memory size is greater than or equal to the first memory granularity, searching for a first memory segment in the reserved memory until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed and the found first memory segment is allocated to the virtual machine; the first memory segment is a section of free memory in the reserved memory aligned according to the first memory granularity, and the first size is equal to the integer part of the quotient of the target memory size and the first memory granularity multiplied by the first memory granularity;

[0173] If the target memory size is smaller than the first memory granularity, at least one second memory segment is searched in the reserved memory, and the at least one second memory segment found is allocated to the virtual machine, the second memory segment is a section of free memory in the reserved memory aligned according to the second memory granularity, the second memory granularity is smaller than the first memory granularity, and the sum of the sizes of the at least one second memory segment is equal to the target memory size.

[0174] Optionally, the processor 81 is also used to execute all or part of the steps in the aforementioned method embodiment.

[0175] The structure of the computer device may also include a communication interface 83 for the computer device to communicate with other devices or a communication network.

[0176] In addition, an embodiment of the present application provides a computer program, including computer program instructions, which, when executed by a processor, implement the method described in the above method embodiment.

[0177] An embodiment of the present application further provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed, the method described in the above method embodiment is implemented.

[0178] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Those of ordinary skill in the art may understand and implement it without creative labor.

[0179] Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by adding a necessary general hardware platform, and of course can also be implemented by combining hardware and software. Based on such an understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a computer product, and the present application can be in the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.

[0180] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable device to generate a machine, so that the instructions executed by the processor of the computer or other programmable device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0181] These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable device to work in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0182] These computer program instructions may also be loaded onto a computer or other programmable device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0183] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0184] The memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.

[0185] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, linked lists, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0186] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.

Claims

1. A virtual machine memory allocation method, It is characterized in that include: Determine the target memory size to be allocated to the virtual machine; If the target memory size is greater than or equal to the first memory granularity, searching for the first memory segment in the reserved memory until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed and the found first memory segment is allocated to the virtual machine; The first memory segment is a segment of free memory in the reserved memory aligned according to a first memory granularity, and the first size is equal to an integer part of a quotient of the target memory size and the first memory granularity multiplied by the first memory granularity; If the target memory size is smaller than the first memory granularity, at least one second memory segment is searched in the reserved memory, and the at least one second memory segment found is allocated to the virtual machine, the second memory segment is a section of free memory in the reserved memory aligned according to the second memory granularity, the second memory granularity is smaller than the first memory granularity, and the sum of the sizes of the at least one second memory segment is equal to the target memory size.

2. The method according to claim 1, It is characterized in that The step of searching for the first memory segment in the reserved memory until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed, comprises: Starting from a current allocation position in the reserved memory corresponding to the first memory granularity, selecting a first starting allocation position aligned according to the first memory granularity; In the reserved memory, searching for a section of free memory starting from the first starting allocation position, and performing alignment processing of a first memory granularity on the found section of free memory to obtain a first aligned memory; If the size of the first aligned memory is 0, determining whether the reserved memory has been traversed, and if so, ending the search; If the size of the first aligned memory is greater than 0, the first aligned memory is a found first memory segment, and it is determined whether the sum of the sizes of the found first memory segments reaches the first size, and if so, the search is terminated; Otherwise, update the current allocation position corresponding to the first memory granularity, start from the updated current allocation position corresponding to the first memory granularity in the reserved memory, select the first starting allocation position aligned according to the first memory granularity, and return to the step of searching for a section of free memory starting from the first starting allocation position in the reserved memory.

3. The method according to claim 2, It is characterized in that Before searching for a section of free memory starting from the first starting allocation position in the reserved memory, the method further includes: Determining whether the target memory size is an integer multiple of the first memory granularity; If the target memory size is an integer multiple of the first memory granularity, determining whether all consecutive memories of the target memory size starting from the first starting allocation position in the reserved memory are free; If all are free, the memory of the target memory size continuous from the first starting allocation position is the first memory segment of the target memory size found; If the target memory size is not an integer multiple of the first memory granularity, or if the target memory size is an integer multiple of the first memory granularity but the continuous memory of the target memory size starting from the first starting allocation position in the reserved memory is not all free, then execute the step of searching for a section of free memory starting from the first starting allocation position in the reserved memory.

4. The method according to any one of claims 1 to 3, It is characterized in that The method further comprises: When the end condition for finding the first memory segment is traversing the reserved memory, searching the reserved memory for at least one second memory segment whose sum of sizes is a second size, and allocating the at least one second memory segment found to the virtual machine, the second size being a difference between the first size and the sum of sizes of the first memory segments found.

5. The method according to any one of claims 1 to 3, It is characterized in that The method further comprises: When the decimal part of the quotient of the target memory size and the first memory granularity is non-zero, search the reserved memory for at least one second memory segment whose sum of sizes is a third size, and allocate the at least one second memory segment found to the virtual machine, where the third size is equal to the decimal part of the quotient of the target memory size and the first memory granularity multiplied by the first memory granularity.

6. The method according to any one of claims 1 to 3, It is characterized in that The step of searching for at least one second memory segment in the reserved memory includes: Starting from a current allocation position in the reserved memory corresponding to the second memory granularity, selecting a second starting allocation position aligned according to the second memory granularity; In the reserved memory, searching for a section of free memory starting from the second starting allocation position, and performing alignment processing of the found section of free memory at a second memory granularity to obtain a second aligned memory; If the size of the second aligned memory is greater than 0, the second aligned memory is a found second memory segment, and it is determined whether the sum of the sizes of the found second memory segments reaches the target memory size. If yes, the search is terminated; If the size of the second aligned memory is 0, or the sum of the sizes of the second memory segments that have been found does not reach the target memory size, the current allocation position corresponding to the second memory granularity is updated, and starting from the updated current allocation position corresponding to the second memory granularity in the reserved memory, the second starting allocation position aligned according to the second memory granularity is selected, and the step of searching for a section of free memory starting from the second starting allocation position in the reserved memory is returned to be executed.

7. The method according to claim 6, It is characterized in that Before searching for a section of free memory starting from the second starting allocation position in the reserved memory, the method further includes: Determining whether the target memory size is an integer multiple of the second memory granularity; If the target memory size is an integer multiple of the second memory granularity, determining whether all the memories of the target memory size in the reserved memory starting from the second starting allocation position are free; If all are free, the memory of the target memory size continuous from the second starting allocation position is the found second memory segment of the target memory size; If the target memory size is not an integer multiple of the second memory granularity, or if the target memory size is an integer multiple of the second memory granularity but the continuous memory of the target memory size starting from the second starting allocation position in the reserved memory is not all free, then execute the step of searching for a section of free memory starting from the second starting allocation position in the reserved memory.

8. The method according to claim 6, It is characterized in that The selecting, starting from the current allocation position corresponding to the second memory granularity in the reserved memory, a second starting allocation position aligned according to the second memory granularity includes: Starting from the current allocation position corresponding to the second memory granularity in the reserved memory, look for a second starting allocation position suitable for the second memory granularity and where all memories of the first memory granularity are not idle, until the reserved memory is found or traversed.

9. The method according to claim 1, It is characterized in that The direction of searching for the first memory segment in the reserved memory is opposite to the direction of searching for the second memory segment in the reserved memory.

10. A virtual machine memory allocation device, It is characterized in that include: A determination module, used to determine a target memory size that needs to be allocated to the virtual machine; a first allocation module, configured to search for a first memory segment in the reserved memory if the target memory size is greater than or equal to the first memory granularity, until at least one first memory segment whose sum of sizes is the first size is found, or until the reserved memory is traversed, and the found first memory segment is allocated to the virtual machine; The first memory segment is a segment of free memory in the reserved memory aligned according to a first memory granularity, and the first size is equal to an integer part of a quotient of the target memory size and the first memory granularity multiplied by the first memory granularity; A second allocation module is used to search for at least one second memory segment in the reserved memory and allocate the at least one second memory segment found to the virtual machine if the target memory size is smaller than the first memory granularity, wherein the second memory segment is a section of free memory in the reserved memory aligned according to the second memory granularity, the second memory granularity is smaller than the first memory granularity, and the sum of the sizes of the at least one second memory segment is equal to the target memory size.

11. A computer device, It is characterized in that include: A memory, a processor; wherein the memory is used to store one or more computer instructions, wherein the one or more computer instructions, when executed by the processor, implement the method as described in any one of claims 1 to 9.

12. A computer program product, It is characterized in that The method comprises computer program instructions, which, when executed by a processor, implement the method according to any one of claims 1 to 9.

13. A computer-readable storage medium, It is characterized in that A computer program is stored thereon, and when the computer program is executed, the method according to any one of claims 1 to 9 is implemented.

Citation Information

Patent Citations

  • Method and device for access to memory of virtual machine and finders

    CN102662869A

  • Method and device for managing memories of virtual machine

    CN107783812A