Memory processing methods, devices, electronic devices and readable media
By canceling the allocation of a specified memory page when the current allocated memory of the virtual machine is not less than a preset threshold, the memory mapping is dynamically adjusted, which solves the problem of host machine memory shortage caused by virtual machine device passthrough and ensures host machine processing performance.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-30
- Publication Date
- 2026-03-13
AI Technical Summary
When virtual machine devices pass through, the host machine experiences memory shortages, leading to a decrease in processing performance.
By canceling the allocation of a specified memory page when the current allocated memory of the virtual machine is not less than a preset memory threshold, and dynamically adjusting the memory mapping during the virtual machine startup and operation, the reasonable utilization of the host machine's memory is ensured.
This avoids allocating all of the virtual machine's memory, alleviating the host machine's memory pressure and ensuring the host machine's processing performance.
Smart Images

Figure CN114661422B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of electronic device technology, and in particular to a memory processing method, apparatus, electronic device and readable medium. Background Technology
[0002] With the continuous development of network technology, virtual machines are being used more and more. When using a virtual machine, in order to facilitate external devices accessing the virtual machine's memory on the host machine, it is necessary to pass the external devices through to the virtual machine.
[0003] The current method allocates all of the virtual machine's memory to the external device during device pass-through, allowing the external device to access the virtual machine's memory. This approach can negatively impact the host machine's processing performance when the host machine experiences memory constraints. Summary of the Invention
[0004] This invention provides a memory processing method, apparatus, electronic device, and readable medium to avoid affecting the processing performance of the host machine when host memory is insufficient.
[0005] Firstly, a memory processing method is provided, applied to a host machine connected to external devices, the method comprising:
[0006] For any virtual machine in the host machine, the amount of currently allocated memory is determined based on the currently allocated memory of the virtual machine; the currently allocated memory is the memory in the virtual machine's memory that is currently available for direct access by the external device.
[0007] If the currently allocated memory amount is not less than a preset memory amount threshold, cancel the allocation of a specified memory page in the allocated memory; wherein the preset memory amount threshold is not greater than the virtual machine memory of the virtual machine.
[0008] Optionally, the method further includes:
[0009] During the startup phase of the virtual machine, X memory pages are selected as the first memory pages; X is less than the total number M of memory pages in the virtual machine's memory.
[0010] A first mapping entry corresponding to the first memory page is established to initialize the first memory page as a memory page that can be directly accessed by the external device.
[0011] Optionally, the method further includes:
[0012] During the operation of the virtual machine, if an access request for the second memory page is received, a second mapping entry corresponding to the second memory page is established to set the second memory page as a memory page that can be directly accessed by the external device; the second memory page is different from the first memory page;
[0013] Accordingly, canceling the allocation of a specified memory page in the allocated memory when the currently allocated memory amount is not less than a preset memory amount threshold includes:
[0014] If the total memory amount of the first memory page and the second memory page is not less than the preset memory amount threshold, select the specified memory page according to the usage record of the currently allocated memory pages; cancel the allocation of the specified memory page.
[0015] Optionally, the method further includes: if a memory release instruction is received, using the memory page indicated by the memory release instruction as a third memory page, and adding the third memory page to a preset reservation pool;
[0016] Before selecting the specified memory page based on the usage record of currently allocated memory pages, the method further includes:
[0017] If the preset reservation pool is not empty, the third memory page that was first added to the preset reservation pool will be determined as the specified memory page;
[0018] If the preset reservation pool is empty, the operation of selecting the specified memory page based on the usage record of the currently allocated memory pages is performed.
[0019] Optionally, before adding the third memory page to the preset retention pool, the method further includes:
[0020] If the sum of the memory amounts corresponding to the first memory page, the second memory page, and the third memory page is not greater than the preset memory amount threshold, then the operation of adding the third memory page to the preset retention pool is performed.
[0021] If the sum of the memory amounts corresponding to the first memory page, the second memory page, and the third memory page is greater than the preset memory amount threshold, the allocation of the third memory page is directly cancelled.
[0022] Optionally, the method further includes:
[0023] Detect the current load of the virtual machine;
[0024] The preset memory threshold is adjusted based on the current load; the preset memory threshold is positively correlated with the current load.
[0025] Secondly, a memory processing device is provided for use in a host machine connected to external devices, the device comprising:
[0026] The first determining module is used to determine the amount of currently allocated memory for any virtual machine in the host machine, based on the currently allocated memory of the virtual machine; the currently allocated memory is the memory in the virtual machine memory of the virtual machine that is currently available for direct access by the external device.
[0027] The first cancellation module is used to cancel the allocation of a specified memory page in the allocated memory when the currently allocated memory amount is not less than a preset memory amount threshold; wherein the preset memory amount threshold is not greater than the virtual machine memory of the virtual machine.
[0028] Optionally, the device further includes:
[0029] The selection module is used to select X memory pages as the first memory pages during the startup phase of the virtual machine; where X is less than the total number M of memory pages in the virtual machine memory.
[0030] The first establishment module is used to establish a first mapping entry corresponding to the first memory page, so as to initialize the first memory page as a memory page that can be directly accessed by the external device.
[0031] Optionally, the device further includes:
[0032] The second establishment module is used to, during the runtime of the virtual machine, if an access request for the second memory page is received, establish a second mapping entry corresponding to the second memory page, so as to set the second memory page as a memory page that can be directly accessed by the external device; the second memory page is different from the first memory page;
[0033] Accordingly, the first cancellation module is specifically used for:
[0034] If the total memory amount of the first memory page and the second memory page is not less than the preset memory amount threshold, select the specified memory page according to the usage record of the currently allocated memory pages; cancel the allocation of the specified memory page.
[0035] Optionally, the device further includes: an addition module, configured to, upon receiving a memory release instruction, use the memory page indicated by the memory release instruction as a third memory page and add the third memory page to a preset reservation pool;
[0036] The device further includes:
[0037] The second determining module is used to determine the third memory page that was first added to the preset reservation pool as the specified memory page before selecting the specified memory page based on the usage records of the currently allocated memory pages;
[0038] The first execution module is used to perform the operation of selecting the specified memory page based on the usage record of the currently allocated memory pages when the preset reservation pool is empty.
[0039] Optionally, the device further includes:
[0040] The second execution module is used to perform the operation of adding the third memory page to the preset retention pool before adding the third memory page to the preset retention pool, provided that the sum of the memory amounts corresponding to the first memory page, the second memory page, and the third memory page is not greater than the preset memory amount threshold.
[0041] The second cancellation module is used to directly cancel the allocation of the third memory page when the sum of the memory amounts corresponding to the first memory page, the second memory page, and the third memory page is greater than the preset memory amount threshold.
[0042] Optionally, the device further includes:
[0043] A detection module is used to detect the current load of the virtual machine;
[0044] An adjustment module is used to adjust the preset memory threshold based on the current load; the preset memory threshold is positively correlated with the current load.
[0045] Thirdly, an electronic device is provided, comprising:
[0046] One or more processors; and one or more machine-readable media thereon storing instructions that, when executed by the one or more processors, cause the electronic device to perform the memory processing method.
[0047] Fourthly, one or more machine-readable media are provided having instructions stored thereon that, when executed by one or more processors, cause the processors to perform the memory processing method.
[0048] In this embodiment of the invention, for any virtual machine, the host machine connected to an external device determines the amount of memory currently allocated based on the virtual machine's currently allocated memory. The currently allocated memory refers to the portion of the virtual machine's memory that is currently directly accessible to the external device. If the amount of currently allocated memory is not less than a preset memory threshold, the allocation of a specified memory page within the allocated memory is canceled; wherein the preset memory threshold is not greater than the virtual machine's total memory. This avoids allocating all of the virtual machine's memory, thereby mitigating the impact on the host machine's processing performance when memory is insufficient.
[0049] The above description is merely an overview of the technical solution of the present invention. In order to better understand the technical means of the present invention and to implement it in accordance with the contents of the specification, and in order to make the above and other objects, features and advantages of the present invention more apparent and understandable, specific embodiments of the present invention are described below. Attached Figure Description
[0050] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the invention. Furthermore, the same reference numerals denote the same parts throughout the drawings. In the drawings:
[0051] Figure 1 This is a flowchart of the steps of a memory processing method provided in an embodiment of the present invention;
[0052] Figure 2 This is a structural block diagram of a memory processing device provided in an embodiment of the present invention;
[0053] Figure 3 This is a structural block diagram of an electronic device according to an exemplary embodiment. Detailed Implementation
[0054] Exemplary embodiments of the invention will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of the invention are shown in the drawings, it should be understood that the invention may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this invention will be thorough and complete, and will fully convey the scope of the invention to those skilled in the art.
[0055] Figure 1 This is a flowchart illustrating the steps of a memory processing method provided in an embodiment of the present invention. This method can be applied to a host machine connected to external devices, such as... Figure 1 As shown, the method may include:
[0056] Step 101: For any virtual machine in the host machine, determine the amount of currently allocated memory based on the currently allocated memory of the virtual machine; the currently allocated memory is the memory in the virtual machine memory of the virtual machine that is currently available for direct access by the external device.
[0057] In this embodiment of the invention, virtual machine memory can be all the memory of the virtual machine. In an exemplary application scenario, when deploying a virtual machine on a host machine, a portion of the host machine's memory resources can be allocated to the virtual machine as its virtual machine memory. One or more virtual machines can be deployed on the host machine. Since a portion of the host machine's memory is allocated to the virtual machines, the available memory on the host machine will be reduced accordingly. Correspondingly, if the virtual machine initializes all its memory to be directly accessible to external devices—that is, if all the virtual machine's memory is allocated and fixed—and the host machine experiences memory shortages, the memory allocated to the virtual machines will be fixed and cannot be temporarily used by the host machine, thus affecting the host machine's processing performance. In this embodiment of the invention, the virtual machine can refer to a kernel-based virtual machine (Kernel-Based Virtual Machine), which can simulate the functions of a complete hardware system through software, providing users with all simulated hardware resources on a physical machine and creating a complete, runnable computer system on those hardware resources. KVM is part of Linux, using the regular Linux scheduler and memory management. It consists of a loadable kernel module and a processor-specific module (associated with the processor architecture) that provides the core virtualization infrastructure. Using KVM, multiple virtual machines running unmodified Linux or Windows images can be run. Each virtual machine has its own private virtualization hardware: network card, disk, graphics adapter, etc. KVM can swap the operating system (Guest OS) running on the virtual machine to the disk to free up RAM. This embodiment uses KVM as an example for detailed explanation. It is worth noting that this application can be applied to virtual machines capable of virtualizing CPU and memory; the specific type of virtual machine used can be determined according to actual settings. For example, the virtual machine can be VMware, VirtualBox, etc.
[0058] Furthermore, in this embodiment of the invention, memory can be understood as a memory region, which can generally be divided into memory pages, and is composed of memory pages. The currently allocated memory amount can be the number of memory pages that have been allocated and initialized for direct access by external devices. These currently allocated memory pages that can be directly accessed by external devices can be allocated to external devices in any previous stage, and this embodiment of the invention does not impose any restrictions on this. The virtual machine allocating memory pages to external devices can be understood as establishing a mapping entry representing the mapping relationship between the Input / Output Virtual Address (IOVA) corresponding to the external device and the Host Physical Address (HPA) corresponding to the memory page, thus fixing the memory page for use by the external device.
[0059] Step 102: If the currently allocated memory amount is not less than a preset memory amount threshold, cancel the allocation of a specified memory page in the allocated memory; wherein, the preset memory amount threshold is not greater than the virtual machine memory of the virtual machine.
[0060] The preset memory threshold can be pre-set according to actual needs. For example, the preset memory threshold can be less than the virtual machine's memory, such as 70% of the virtual machine's memory. This way, when a large amount of memory is currently locked, the allocation of a specific memory page is cancelled, thus ensuring that the virtual machine has sufficient memory. The specified memory page can be selected according to actual needs. For example, it can be randomly selected or a specific allocated memory page can be selected, such as a memory page with a usage frequency lower than a preset frequency threshold, or a memory page allocated to an external device with an activity level lower than a preset threshold, etc. This embodiment of the invention does not impose any limitations on this. Thus, by using memory pages with a usage frequency lower than the preset frequency threshold and memory pages allocated to external devices with an activity level lower than the preset threshold as specified memory pages, the cancellation of memory pages used by external devices can be avoided to some extent, thereby minimizing the impact of the cancellation operation on external devices.
[0061] In this embodiment of the invention, it can be detected whether the currently allocated memory amount is less than a preset memory threshold. If it is not less, it can be determined that the number of currently locked memory pages is relatively large. Therefore, in this case, the allocation of a specified memory page in the allocated memory can be canceled until the currently allocated memory amount is less than the preset memory threshold, thereby ensuring that the virtual machine has memory available to a certain extent. This avoids allocating all the virtual machine's memory at the beginning and also avoids all of the virtual machine's memory being allocated during operation. In this way, when the host machine experiences host memory shortage, the unallocated memory of the virtual machine can alleviate the host machine's host memory shortage problem, thereby ensuring the host machine's processing performance to a certain extent. It should be noted that a preset memory threshold less than the virtual machine's memory can also be directly set to reserve as much memory as possible; this embodiment of the invention does not limit this.
[0062] In summary, the memory processing method provided in this embodiment of the invention allows a host machine connected to external devices to determine the amount of currently allocated memory for any virtual machine based on the virtual machine's currently allocated memory. The currently allocated memory refers to the portion of the virtual machine's memory that is currently accessible directly to the external device. If the amount of currently allocated memory is not less than a preset memory threshold, the allocation of a specified memory page within the allocated memory is canceled; wherein the preset memory threshold is not greater than the virtual machine's total memory. This avoids all of the virtual machine's memory being allocated, thereby mitigating the impact on the host machine's processing performance when memory is scarce.
[0063] Optionally, the following steps may also be performed in embodiments of the present invention:
[0064] Step S21: During the startup phase of the virtual machine, select X memory pages as the first memory pages; X is less than the total number M of memory pages in the virtual machine memory.
[0065] Step S22: Establish a first mapping entry corresponding to the first memory page to initialize the first memory page as a memory page that can be directly accessed by the external device, thereby completing the allocation.
[0066] In this embodiment of the invention, a first mapping entry corresponding to a first memory page is established to initialize the first memory page as a memory page that can be directly accessed by an external device, thereby completing the allocation of the first memory page. The memory page that can be directly accessed by an external device is the allocated memory page. The specific number of X can be set according to actual needs, and the memory amount of the first memory page can be denoted as M1, where M1 is less than a preset memory amount threshold. Specifically, during allocation, a first mapping entry corresponding to the first memory page can be established, and the first memory page can be fixed. For example, it can be set as page-locked memory. The first mapping entry corresponding to the first memory page can be used to characterize the mapping relationship between the IOVA corresponding to the external device and the HPA corresponding to the first memory page. The first memory page can be a memory page with a single page size larger than the regular page size; that is, when the virtual machine starts, the memory portion M1 can be mapped using large pages. The first mapping entry can be saved to the I / O page table, specifically to the Input Output Translation Lookaside Buffer (IOTLB). By recording the mapping entry in the I / O page table, the host machine can easily know which memory pages are directly accessible to external devices.
[0067] Furthermore, there can be one or more external devices. When there are multiple external devices, a mapping relationship can be established between the IOVA corresponding to each external device and the HPA corresponding to the first memory page, meaning that multiple external devices share all the first memory pages. Alternatively, a mapping relationship can be established between the IOVA corresponding to each external device and the HPA corresponding to a portion of the first memory pages, meaning that one external device uses a portion of the first memory pages. This embodiment of the invention does not limit this approach.
[0068] In this embodiment of the invention, during the virtual machine startup phase, X memory pages are selected as the first memory pages. X is less than the total number M of memory pages in the virtual machine's memory. Then, a first mapping entry corresponding to the first memory page is established to initialize the first memory page as a memory page that can be directly accessed by the external device. Thus, by setting X to be less than the total number M of memory pages in the virtual machine's memory, only a portion of the memory is pre-fixed during the virtual machine startup phase. This avoids the situation where all available memory of the virtual machine is fixed to the external device during startup, thereby ensuring that the external device can access the allocated memory during operation while preventing the host machine's processing performance from being affected by memory shortages.
[0069] Optionally, the following steps may also be performed in embodiments of the present invention:
[0070] Step S31: During the operation of the virtual machine, if an access request for the second memory page is received, a second mapping entry corresponding to the second memory page is established to set the second memory page as a memory page that can be directly accessed by the external device; the second memory page is different from the first memory page.
[0071] In practical applications, external devices can preferentially use the first memory page when accessing memory. For example, when an external device accesses memory using Direct Memory Access (DMA), it can preferentially allocate DMA pages from the memory represented by M1. The memory pages in the memory represented by M1 can be used for DMA implementation. The virtual machine can pre-send information about memory pages that can be used for DMA implementation to the host machine, so that the host machine can determine which pages can be used for DMA implementation, i.e., which pages can be allocated as DMA pages. Of course, the external device may need to access other memory pages besides M1, i.e., access a second memory page. Therefore, upon receiving an access request, it can be checked whether a first mapping entry corresponding to the memory page indicated by the access request exists; that is, it can be checked whether the initiated DMA address (the address corresponding to the memory page indicated by the access request) has a mapping in the I / O page table. If it does not exist, it can be determined that an access request for a second memory page has been received. Accordingly, a second mapping entry corresponding to the second memory page can be established, and the second memory page can be fixed. The second mapping entry corresponding to the second memory page can be used to characterize the mapping relationship between the IOVA corresponding to the external device and the HPA corresponding to the second memory page. The second memory page can also be a memory page with a single page size larger than the regular page size; that is, when establishing a new mapping, a large page mapping can also be used. The second mapping entry can also be saved to the I / O page table, specifically to the IOTLB.
[0072] Accordingly, the operation of canceling the allocation of a specified memory page in the allocated memory when the currently allocated memory amount is not less than a preset memory amount threshold can specifically include:
[0073] Step S41: If the total memory amount of the first memory page and the second memory page is not less than the preset memory amount threshold, select the specified memory page according to the usage record of the currently allocated memory pages.
[0074] Step S42: Cancel the allocation of the specified memory page.
[0075] As the number of second memory pages increases, the actual amount of memory allocated in the virtual machine also increases. Therefore, in this embodiment of the invention, when a new second memory page is mapped, it can be checked whether the currently allocated memory is not less than a preset memory threshold. That is, it can be checked whether the sum of the memory of the first memory page and the second memory page is not less than the preset memory threshold. For any virtual machine in the host machine, the sum of the memory of the first memory page and the second memory page can be the sum of the memory of all first memory pages and all second memory pages of that virtual machine. If so, a specific memory page can be determined to cancel the allocation of that specific memory page, thereby ensuring that the currently allocated memory is less than the preset memory threshold. It should be noted that as more and more allocated second memory pages are used, the mapping entries for the established second memory pages can be maintained indefinitely. Correspondingly, in the case of needing to allocate a certain second memory page, it may be possible that the sum of the memory of the first memory page and the second memory page is not less than the preset memory threshold.
[0076] Furthermore, based on the usage records of allocated memory pages, the number of times each allocated memory page was used within a specified time period can be determined, and the allocated memory page with the fewest usages can be identified as the designated memory page. The specified time period can be a preset duration from the current time. Specifically, the designated memory page can be determined based on the Least Recently Used (LRU) algorithm. More specifically, the allocation of the memory page can be canceled by clearing the mapping entry corresponding to the memory page and releasing the memory page.
[0077] In this embodiment of the invention, during the virtual machine's operation, if an access request for a second memory page is received, a second mapping entry corresponding to the second memory page is established to set the second memory page as a memory page that can be directly accessed by external devices; the second memory page is different from the first memory page. Correspondingly, if the total memory amount of the first and second memory pages is not less than a preset memory amount threshold, a specified memory page is selected based on the usage records of currently allocated memory pages; the allocation of the specified memory page is then cancelled. In this way, while supporting newly allocated memory pages, it can ensure, to a certain extent, that the virtual machine has sufficient memory, thereby ensuring the processing performance of the host machine.
[0078] Optionally, the following steps may also be performed in embodiments of the present invention:
[0079] Step S51: If a memory release instruction is received, the memory page indicated by the memory release instruction is used as the third memory page, and the third memory page is added to the preset reservation pool.
[0080] The memory release command can be used to instruct the release of fixed memory pages. This command can be sent by an external device or triggered internally by the electronic device. For example, a memory release command can be triggered when a device passthrough is released or a memory region (e.g., a data buffer) is released. The memory page indicated by the memory release command can be a previously allocated memory page. For instance, the memory page indicated by the command can belong to either the first or second memory page, and the third mapping entry corresponding to the third memory page can belong to either the previously established first or second mapping entry.
[0081] Furthermore, in this embodiment of the invention, after receiving a memory release instruction, only the DMA page can be unsecured first, that is, the third memory page can be unsecured, but the third mapping entry of the third memory page can be retained, and the third memory page can be added to a preset retention pool. The preset retention pool can be a pre-defined storage area.
[0082] Accordingly, in this embodiment of the invention, before selecting the specified memory page based on the usage records of currently allocated memory pages, it can be checked whether the preset reservation pool is empty. Further, if the preset reservation pool is not empty, the third memory page that was first added to the preset reservation pool is determined as the specified memory page. Correspondingly, if the preset reservation pool is not empty, it can be determined that there are third memory pages that have been unfixed but not yet canceled, therefore, the specified memory page can be selected from the preset reservation pool. In this case, since the selected specified memory page is a page that has been previously unfixed, the operation to cancel the allocation of the memory page can specifically be: clearing the third mapping entry corresponding to the specified memory page; specifically, the most recently established second mapping entry can be used to overwrite the third mapping entry corresponding to the specified memory page. Further, the specified memory page can also be cleared from the preset reservation pool to avoid unnecessary occupation. By selecting the third memory page that was first added to the preset reservation pool as the specified memory page, it can be ensured to some extent that the third memory page can be canceled as soon as possible. If the preset reservation pool is empty, the operation of selecting the specified memory page based on the usage records of currently allocated memory pages is performed. If the preset reservation pool is empty, the least recently used allocated memory page can be selected directly, thus reserving memory pages that are more likely to be used. In this case, canceling the allocation of the memory page can specifically involve: clearing the mapping entry corresponding to the specified memory page, for example, replacing the mapping entry corresponding to the specified memory page with the second most recently created mapping entry, and releasing the specified memory page to cancel its attachment.
[0083] Optionally, the following steps may also be performed in embodiments of the present invention:
[0084] Step S61: Detect the current load of the virtual machine.
[0085] For example, the current workload of the virtual machine can be detected based on a preset load detection algorithm to obtain the current load. Alternatively, the current number of pending tasks and the number of processed but incomplete tasks of the virtual machine can be counted, and the current load can be determined based on the count. The larger the count, the greater the current load. The operation of detecting the current load can be performed automatically periodically or in response to user instructions; this embodiment of the invention does not impose any limitations on this.
[0086] Step S62: Adjust the preset memory threshold based on the current load; the preset memory threshold is positively correlated with the current load.
[0087] In this embodiment of the invention, the higher the current load, the higher the preset memory threshold can be adjusted. For example, it can be detected whether the current load is greater than the previously detected load. If it is, the current preset memory threshold is increased by a specified value to complete the adjustment. If it is not greater, the current preset memory threshold is subtracted by a specified value to complete the adjustment. The specified value can be pre-set, or it can be the absolute value of the difference between the current load and the previously detected load used as the input to a preset function, with the output of the preset function as the specified value. The specified value is a value greater than 0, and the output of the preset function is positively correlated with the input. The dynamic adjustment process can be implemented based on a bubble mechanism.
[0088] If the preset memory threshold is too high, it may cause memory pressure on the host machine; if the preset memory threshold is too low, it may lead to memory waste and affect the performance of the virtual machine. In this embodiment of the invention, during the operation of the virtual machine, the preset memory threshold is dynamically adjusted based on the workload of the virtual machine. To a certain extent, a more suitable preset memory threshold can be set, thereby avoiding the problem of setting it too low, which leads to memory waste and affects the performance of the virtual machine, and avoiding the problem of setting the preset memory threshold too high, which leads to memory pressure on the host machine. Of course, the preset memory threshold can also be statically configured, and this embodiment of the invention does not limit this.
[0089] It should be noted that, in this embodiment of the invention, when performing the operation of selecting X memory pages as the first memory pages, the estimated load of the virtual machine can be obtained first. Then, the value of X is determined based on the estimated load. The value of X is positively correlated with the estimated load. This allows for on-demand mapping for pass-through devices, thereby ensuring, to some extent, that an appropriate number of first memory pages can be selected. The estimated load can be determined in advance based on experience, or it can be estimated based on a preset estimation method using the virtual machine's historical operating parameters as input.
[0090] Optionally, before adding the third memory page to the preset retention pool, the following steps may also be performed in this embodiment of the invention:
[0091] Step S71: If the sum of the memory amounts corresponding to the first memory page, the second memory page, and the third memory page is not greater than the preset memory amount threshold, perform the operation of adding the third memory page to the preset retention pool.
[0092] Step S72: If the sum of the memory amounts corresponding to the first memory page, the second memory page, and the third memory page is greater than the preset memory amount threshold, the allocation of the third memory page is directly cancelled.
[0093] In this embodiment of the invention, although the third memory page is unfixed, the third mapping entry corresponding to the third memory page is not yet cleared. That is, the allocation of the third memory page has not yet been successfully cancelled. Therefore, the sum of the memory amounts corresponding to the first, second, and third memory pages can be detected. Only if the sum of the three memory amounts is not greater than a preset memory amount threshold will the third memory page be added to the preset reservation pool. Otherwise, the allocation of the third memory page is directly cancelled, and the third memory page is not added to the preset reservation pool. Specifically, after the fixation of the third memory page is released in response to the memory release instruction, the third mapping entry of the third memory page can be deleted to complete the operation of cancelling the allocation of the third memory page, thereby preventing the currently allocated memory amount from exceeding the preset memory amount threshold and ensuring that the virtual machine has sufficient memory. For example, let M1 represent the memory amount of the first memory page set, M2 represent the memory amount of the second memory page set, and M3 represent the memory amount of the memory page set retained in the preset reservation pool, where M1 + M2 + M3 ≤ T. Here, T is the preset memory amount threshold. The amount of memory can be the number of memory pages, or the amount of data occupied by a memory page.
[0094] The following describes an application scenario related to an embodiment of the present invention. Currently, based on Input Output Memory Management Unit (IOMMU) technology, it is possible to pass external devices directly to the virtual machine, allowing the external device to directly access the virtual machine's memory. The address used by the external device to initiate a request is IOVA. To access the data actually stored in the virtual machine, HPA is required. Therefore, the IOMMU can translate IOVA into HPA. In the DMA mapping strategy, for each valid IOVA used by the external device in DMA mode, the virtual machine needs to establish a mapping from that IOVA to the HPA. That is, in the IOMMU pass-through environment, as long as the device can legally use the IOVA, the memory page must be fixed. However, if the entire virtual machine's memory is fixed in advance, it will affect the host performance when host memory is scarce.
[0095] One related technique uses persistent mapping. Once mapped, the mapping is maintained so it can be reused later. However, this method still fixes all of the virtual machine's memory. Another solution uses single-map, mapping memory only once before direct access and immediately unmapping it after the DMA operation is complete. This approach leads to excessive runtime overhead.
[0096] In this embodiment of the invention, a portion of memory is reserved for external devices and directly mapped when the virtual machine starts. Subsequently, as the reserved memory increases, it is dynamically adjusted based on a preset memory threshold to avoid exceeding it. This provides a memory-friendly memory handling method, ensuring that unallocated memory is available. This alleviates host memory pressure while achieving certain I / O performance and avoiding excessive operational overhead.
[0097] Figure 2 This is a structural block diagram of a memory processing device provided in an embodiment of the present invention. The device is applied to a host machine connected to external devices. The device 20 includes:
[0098] The first determining module 201 is used to determine the amount of currently allocated memory for any virtual machine in the host machine based on the currently allocated memory of the virtual machine; the currently allocated memory is the memory in the virtual machine memory of the virtual machine that is currently available for direct access by the external device.
[0099] The first cancellation module 202 is used to cancel the allocation of a specified memory page in the allocated memory when the currently allocated memory amount is not less than a preset memory amount threshold; wherein the preset memory amount threshold is not greater than the virtual machine memory of the virtual machine.
[0100] Optionally, the device 20 further includes:
[0101] The selection module is used to select X memory pages as the first memory pages during the startup phase of the virtual machine; where X is less than the total number M of memory pages in the virtual machine memory.
[0102] The first establishment module is used to establish a first mapping entry corresponding to the first memory page, so as to initialize the first memory page as a memory page that can be directly accessed by the external device.
[0103] Optionally, the device 20 further includes:
[0104] The second establishment module is used to, during the runtime of the virtual machine, if an access request for the second memory page is received, establish a second mapping entry corresponding to the second memory page, so as to set the second memory page as a memory page that can be directly accessed by the external device; the second memory page is different from the first memory page;
[0105] Accordingly, the first cancellation module 201 is specifically used for:
[0106] If the total memory amount of the first memory page and the second memory page is not less than the preset memory amount threshold, select the specified memory page according to the usage record of the currently allocated memory pages; cancel the allocation of the specified memory page.
[0107] Optionally, the device 20 further includes: an addition module, configured to, upon receiving a memory release instruction, use the memory page indicated by the memory release instruction as a third memory page and add the third memory page to a preset reservation pool;
[0108] The device 20 further includes:
[0109] The second determining module is used to determine the third memory page that was first added to the preset reservation pool as the specified memory page before selecting the specified memory page based on the usage records of the currently allocated memory pages;
[0110] The first execution module is used to perform the operation of selecting a specified memory page based on the usage record of the currently allocated memory pages when the preset reservation pool is empty.
[0111] Optionally, the device 20 further includes:
[0112] The second execution module is used to perform the operation of adding the third memory page to the preset retention pool before adding the third memory page to the preset retention pool, provided that the sum of the memory amounts corresponding to the first memory page, the second memory page, and the third memory page is not greater than the preset memory amount threshold.
[0113] The second cancellation module is used to directly cancel the allocation of the third memory page when the sum of the memory amounts corresponding to the first memory page, the second memory page, and the third memory page is greater than the preset memory amount threshold.
[0114] Optionally, the device 20 further includes:
[0115] A detection module is used to detect the current load of the virtual machine;
[0116] An adjustment module is used to adjust the preset memory threshold based on the current load; the preset memory threshold is positively correlated with the current load.
[0117] In summary, the memory processing device provided in this embodiment of the invention allows a host machine connected to external devices to determine the amount of currently allocated memory for any virtual machine based on the virtual machine's currently allocated memory. The currently allocated memory refers to the portion of the virtual machine's memory that is currently accessible directly to the external device. If the amount of currently allocated memory is not less than a preset memory threshold, the allocation of a specified memory page within the allocated memory is canceled; wherein the preset memory threshold is not greater than the virtual machine's memory. This avoids allocating all of the virtual machine's memory, thereby mitigating the impact on the host machine's processing performance when memory is scarce.
[0118] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0119] Figure 3 This is a structural block diagram of an electronic device according to an exemplary embodiment. For example, the electronic device 400 may be a mobile phone, computer, digital broadcasting terminal, messaging device, game console, tablet device, medical device, fitness equipment, personal digital assistant, etc.
[0120] Reference Figure 3 The electronic device 400 may include one or more of the following components: processing component 402, memory 404, power supply component 406, multimedia component 408, audio component 410, input / output (I / O) interface 412, sensor component 414, and communication component 416.
[0121] Processing component 402 typically controls the overall operation of electronic device 400, such as operations associated with display, telephone calls, data communication, camera operation, and recording operations. Processing component 402 may include one or more processors 420 to execute instructions to perform all or part of the steps of the methods described above. Furthermore, processing component 402 may include one or more modules to facilitate interaction between processing component 402 and other components. For example, processing component 402 may include a multimedia module to facilitate interaction between multimedia component 408 and processing component 402.
[0122] Memory 404 is configured to store various types of data to support the operation of device 400. Examples of this data include instructions for any application or method operating on electronic device 400, contact data, phonebook data, messages, pictures, videos, etc. Memory 404 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk.
[0123] Power supply component 406 provides power to various components of electronic device 400. Power supply component 406 may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power to electronic device 400.
[0124] Multimedia component 408 includes a screen that provides an output interface between the electronic device 400 and the user. In some embodiments, the screen may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen may be implemented as a touchscreen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touches, swipes, and gestures on the touch panel. The touch sensors may sense not only the boundaries of the touch or swipe action but also the duration and pressure associated with the touch or swipe operation. In some embodiments, multimedia component 408 includes a front-facing camera and / or a rear-facing camera. When the electronic device 400 is in an operating mode, such as a shooting mode or a video mode, the front-facing camera and / or the rear-facing camera may receive external multimedia data. Each front-facing camera and rear-facing camera may be a fixed optical lens system or have focal length and optical zoom capabilities.
[0125] Audio component 410 is configured to output and / or input audio signals. For example, audio component 410 includes a microphone (MIC) configured to receive external audio signals when electronic device 400 is in an operating mode, such as call mode, recording mode, and voice recognition mode. The received audio signals may be further stored in memory 404 or transmitted via communication component 416. In some embodiments, audio component 410 also includes a speaker for outputting audio signals.
[0126] I / O interface 412 provides an interface between processing component 402 and peripheral interface modules, such as keyboards, click wheels, buttons, etc. These buttons may include, but are not limited to, home buttons, volume buttons, power buttons, and lock buttons.
[0127] Sensor assembly 414 includes one or more sensors for providing state assessments of various aspects of electronic device 400. For example, sensor assembly 414 may detect the on / off state of device 400, the relative positioning of components such as the display and keypad of electronic device 400, changes in position of electronic device 400 or a component of electronic device 400, the presence or absence of user contact with electronic device 400, orientation or acceleration / deceleration of electronic device 400, and temperature changes of electronic device 400. Sensor assembly 414 may include a proximity sensor configured to detect the presence of nearby objects without any physical contact. Sensor assembly 414 may also include a light sensor, such as a CMOS or CCD image sensor, for use in imaging applications. In some embodiments, sensor assembly 414 may also include an accelerometer, gyroscope, magnetometer, pressure sensor, or temperature sensor.
[0128] Communication component 416 is configured to facilitate wired or wireless communication between electronic device 400 and other devices. Electronic device 400 can access wireless networks based on communication standards, such as WiFi, 2G, or 3G, or combinations thereof. In one exemplary embodiment, communication component 414 receives broadcast signals or broadcast-related information from an external broadcast management system via a broadcast channel. In one exemplary embodiment, communication component 414 also includes a near-field communication (NFC) module to facilitate short-range communication. For example, the NFC module may be implemented based on radio frequency identification (RFID) technology, Infrared Data Association (IrDA) technology, ultra-wideband (UWB) technology, Bluetooth (BT) technology, and other technologies.
[0129] In an exemplary embodiment, the electronic device 400 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field-programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components to perform the methods described above.
[0130] This invention also provides an electronic device, including one or more processors; and one or more machine-readable media storing instructions thereon, which, when executed by the one or more processors, cause the electronic device to perform the various processes of the above method embodiments and achieve the same technical effect. To avoid repetition, these will not be described again here.
[0131] This invention also provides one or more machine-readable media storing instructions that, when executed by one or more processors, cause the processors to implement the various processes of the above-described method embodiments and achieve the same technical effects. To avoid repetition, these instructions will not be elaborated further here. The machine-readable media may include read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0132] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0133] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes several instructions to cause a terminal (which may be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in the various embodiments of the present invention.
[0134] The embodiments of the present invention have been described above with reference to the accompanying drawings. However, the present invention is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of the present invention without departing from the spirit and scope of the claims, and all of these forms are within the protection scope of the present invention.
Claims
1. A memory processing method, characterized in that, The method is applied to a host machine connected to an external device, and the method includes: For any virtual machine in the host machine, the amount of memory currently allocated is determined based on the currently allocated memory of the virtual machine; the currently allocated memory is the memory in the virtual machine's memory that is currently available for direct access by the external device; the amount of memory currently allocated is the number of memory pages that have been allocated and initialized to be available for direct access by the external device. If the currently allocated memory amount is not less than a preset memory amount threshold, cancel the allocation of a specified memory page in the allocated memory; wherein, the preset memory amount threshold is not greater than the virtual machine memory of the virtual machine; If a memory release instruction is received, the memory page indicated by the memory release instruction is used as the third memory page, and the third memory page is added to the preset reservation pool; If the preset reservation pool is not empty, the third memory page that was first added to the preset reservation pool will be determined as the specified memory page.
2. The method according to claim 1, characterized in that, The method further includes: During the startup phase of the virtual machine, X memory pages are selected as the first memory pages; X is less than the total number M of memory pages in the virtual machine's memory. A first mapping entry corresponding to the first memory page is established to initialize the first memory page as a memory page that can be directly accessed by the external device.
3. The method according to claim 2, characterized in that, The method further includes: During the operation of the virtual machine, if an access request for the second memory page is received, a second mapping entry corresponding to the second memory page is established to set the second memory page as a memory page that can be directly accessed by the external device; the second memory page is different from the first memory page; Accordingly, canceling the allocation of a specified memory page in the allocated memory when the currently allocated memory amount is not less than a preset memory amount threshold includes: If the total memory amount of the first memory page and the second memory page is not less than the preset memory amount threshold, select the specified memory page according to the usage record of the currently allocated memory pages; cancel the allocation of the specified memory page.
4. The method according to claim 3, characterized in that, Before selecting the specified memory page based on the usage record of currently allocated memory pages, the method further includes: If the preset reservation pool is empty, the operation of selecting the specified memory page based on the usage record of the currently allocated memory pages is performed.
5. The method according to claim 4, characterized in that, Before adding the third memory page to the preset retention pool, the method further includes: If the sum of the memory amounts corresponding to the first memory page, the second memory page, and the third memory page is not greater than the preset memory amount threshold, then the operation of adding the third memory page to the preset retention pool is performed. If the sum of the memory amounts corresponding to the first memory page, the second memory page, and the third memory page is greater than the preset memory amount threshold, the allocation of the third memory page is directly cancelled.
6. The method according to any one of claims 1-5, characterized in that, The method further includes: Detect the current load of the virtual machine; The preset memory threshold is adjusted based on the current load; the preset memory threshold is positively correlated with the current load.
7. A memory processing device, characterized in that, The device is applied to a host machine connected to an external device, and the device includes: The first determining module is used to determine the amount of currently allocated memory for any virtual machine in the host machine, based on the currently allocated memory of the virtual machine; the currently allocated memory is the memory in the virtual machine memory of the virtual machine that is currently directly accessible to the external device; the currently allocated memory amount is the number of memory pages that have been allocated and initialized to be directly accessible to the external device. The first cancellation module is used to cancel the allocation of a specified memory page in the allocated memory when the currently allocated memory amount is not less than a preset memory amount threshold; wherein the preset memory amount threshold is not greater than the virtual machine memory of the virtual machine; The device is also configured to, upon receiving a memory release instruction, use the memory page indicated by the memory release instruction as a third memory page and add the third memory page to a preset retention pool; If the preset reservation pool is not empty, the third memory page that was first added to the preset reservation pool will be determined as the specified memory page.
8. The apparatus according to claim 7, characterized in that, The device further includes: The selection module is used to select X memory pages as the first memory pages during the startup phase of the virtual machine; where X is less than the total number M of memory pages in the virtual machine memory. The first establishment module is used to establish a first mapping entry corresponding to the first memory page, so as to initialize the first memory page as a memory page that can be directly accessed by the external device.
9. An electronic device, characterized in that, include: One or more processors; And one or more machine-readable media thereon storing instructions, which, when executed by the one or more processors, cause the electronic device to perform the method as described in any one of claims 1-6.
10. One or more machine-readable media, characterized in that, It stores instructions that, when executed by one or more processors, cause the processors to perform the method as described in any one of claims 1-6.
Citation Information
Patent Citations
Virtual machine memory optimization processing method and related device
CN111651236A
Equipment processing method and device, electronic equipment and readable medium
CN113360243A