Memory allocation method and device, electronic equipment and readable storage medium
By using the same memory pool in multiple computing units to process and store data, the problem of long data copying time between computing units is solved, and data processing efficiency is improved.
Patent Information
- Application Number
- CN202411073131.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-06
- Publication Date
- 2024-11-08
AI Technical Summary
When multiple computing units process data, since the memory space applied for by each computing unit belongs to a different address space, data copying takes a long time, resulting in poor efficiency of computing units for service data processing.
By obtaining the memory parameters corresponding to the service, determining the memory pool, and allocating the memory blocks to the computing unit that processes the data, so that it processes and stores data in the same memory pool, avoiding the data being copied to the memory space of other computing units.
The waiting time of the computing unit and the data processing time are reduced, and the efficiency of the multiple computing units in processing service data is improved.
Smart Images

Figure CN118916169A_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of computer technology, and specifically relates to a memory allocation method, device, electronic device and readable storage medium. Background Art
[0002] Typically, in a scenario where multiple computing units process data for a business, each computing unit can apply for a memory space from the operating system, so that one computing unit can first process the data and store the processed data in a memory space applied for by the one computing unit. Then, another computing unit can copy the data stored in the one memory space to another memory space applied for by the other computing unit and process the copied data, and so on, until the multiple computing units complete the data processing for the business data.
[0003] However, since the memory space applied for by each of the above-mentioned multiple computing units belongs to a different address space from the memory space applied for by other computing units, each computing unit needs to copy the data obtained after data processing by other computing units to the applied memory space before processing the data. Therefore, each computing unit may need to wait for a long time before it can process the data, which may cause the above-mentioned multiple computing units to need a long time to complete the data processing of the business data, thus resulting in poor efficiency of the computing units in processing the business data. Summary of the invention
[0004] The purpose of the embodiments of the present application is to provide a memory allocation method, device, electronic device and readable storage medium, which can reduce the time required for multiple computing units to complete data processing of business data and improve the efficiency of multiple computing units in processing business data.
[0005] In a first aspect, an embodiment of the present application provides a memory allocation method, the method comprising: obtaining a first memory parameter corresponding to a first business, determining a first memory pool based on the first memory parameter, the first memory pool comprising at least one first memory block, allocating at least one first memory block to at least one first computing unit, the first computing unit being used to process data of the first business.
[0006] In a second aspect, an embodiment of the present application provides a memory allocation device, the device comprising: an acquisition module, a determination module and an allocation module. The acquisition module is used to acquire a first memory parameter corresponding to a first service.
[0007] The determining module is used to determine a first memory pool according to the first memory parameter obtained by the obtaining module, wherein the first memory pool includes at least one first memory block. The allocating module is used to allocate at least one first memory block to at least one first computing unit, wherein the first computing unit is used to process data of the first business.
[0008] In a third aspect, an embodiment of the present application provides an electronic device, which includes a processor and a memory, wherein the memory stores programs or instructions that can be run on the processor, and when the program or instructions are executed by the processor, the steps of the method described in the first aspect are implemented.
[0009] In a fourth aspect, an embodiment of the present application provides a readable storage medium, on which a program or instruction is stored, and when the program or instruction is executed by a processor, the steps of the method described in the first aspect are implemented.
[0010] In a fifth aspect, an embodiment of the present application provides a chip, comprising a processor and a communication interface, wherein the communication interface is coupled to the processor, and the processor is used to run a program or instruction to implement the method described in the first aspect.
[0011] In a sixth aspect, an embodiment of the present application provides a computer program product, which is stored in a storage medium and is executed by at least one processor to implement the method described in the first aspect.
[0012] In an embodiment of the present application, the electronic device can first obtain a first memory parameter corresponding to the first business to determine the size of the memory required for the first business, and then determine a first memory pool according to the determined memory size, and then allocate at least one first memory block in the first memory pool to a computing unit for processing data of the first business, that is, the first memory blocks allocated to at least one computing unit for processing data of the first business are all in the same memory pool. On the one hand, in the case where at least one first computing unit includes one computing unit, the electronic device can also allocate the first memory block in the first memory pool to other computing units for processing data of the first business, so that after the one computing unit stores the data obtained after data processing of the data of the first business in the first memory block, its Other computing units can directly read the data stored in the first memory block without copying the data to the memory space applied for by other computing units; on the other hand, in the case where at least one first computing unit includes at least two computing units, after each computing unit stores the data obtained after data processing on the data of the first business in the first memory block, other computing units can directly read the data stored in the first memory block without copying the data to the memory space applied for by other computing units; therefore, the waiting time required for other computing units to process the data can be reduced, thereby reducing the time required for at least one computing unit to complete data processing on the data of the first business, and thus, the efficiency of the computing units in processing the business data can be improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0013] Figure 1 A flowchart of a memory allocation method provided in an embodiment of the present application;
[0014] Figure 2 A schematic diagram of the overall process of implementing memory reuse of different computing units by a memory pool across computing units provided in an embodiment of the present application;
[0015] Figure 3 A schematic diagram of the memory pool application and update process provided in an embodiment of the present application;
[0016] Figure 4 A schematic diagram of a memory application process for a memory pool provided in an embodiment of the present application;
[0017] Figure 5 A schematic diagram of a memory release process of a memory pool provided in an embodiment of the present application;
[0018] Figure 6 A schematic diagram of a list of free memory blocks when a memory pool is initialized according to an embodiment of the present application;
[0019] Figure 7A schematic diagram of a list of free memory blocks requested from a memory pool for the first time provided in an embodiment of the present application;
[0020] Figure 8 A schematic diagram of a list of free memory blocks after multiple release requests provided in an embodiment of the present application;
[0021] Fig. 9 A schematic diagram of a Cache Clean operation provided in an embodiment of the present application;
[0022] Fig.10 A schematic diagram of a Cache invalidate operation provided in an embodiment of the present application;
[0023] Fig.11 A schematic diagram of a local cache flush triple provided in an embodiment of the present application: fd, offset, size;
[0024] Fig.12 A schematic diagram of a flow chart of a memory allocation device provided in an embodiment of the present application;
[0025] Fig.13 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;
[0026] Fig.14 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0027] The following will be combined with the drawings in the embodiments of the present application to clearly describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, rather than all the embodiments. All other embodiments obtained by ordinary technicians in this field based on the embodiments in the present application belong to the scope of protection of this application.
[0028] The terms "first", "second", etc. in the specification and claims of the present application are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable under appropriate circumstances, so that the embodiments of the present application can be implemented in an order other than those illustrated or described here, and the objects distinguished by "first", "second", etc. are generally of one type, and the number of objects is not limited. For example, the first object can be one or more. In addition, "and / or" in the specification and claims represents at least one of the connected objects, and the character " / " generally indicates that the objects associated with each other are in an "or" relationship.
[0029] The terms "at least one (item)", "at least one of" and the like in the specification and claims of the present application refer to any one, any two or a combination of more than two of the objects included therein. For example, at least one (item) of a, b, and c can be represented by: "a", "b", "c", "a and b", "a and c", "b and c" and "a, b and c", where a, b, and c can be single or multiple. Similarly, "at least two (items)" refers to two or more, and its meaning is similar to that of "at least one (item)".
[0030] The memory management method provided in the embodiment of the present application is described in detail below through specific embodiments and their application scenarios in conjunction with the accompanying drawings.
[0031] The memory allocation method provided in the embodiment of the present application can be applied to the scenario of business scenario memory application during the memory management process.
[0032] With the development of imaging technology and artificial intelligence (AI) technology, the implementation of multiple cameras, increasingly complex imaging algorithms, and large AI models in terminal products has led to the need for more memory support in these business scenarios. Due to its complexity, imaging algorithms involve different computing units, such as the central processing unit (CPU), graphics processing unit (GPU), digital signal processor (DSP), neural network processor (NPU), etc.
[0033] In a scenario where at least one computing unit processes data of a business, each of the at least one computing unit can apply for a memory space from the operating system respectively, so that one of the at least one computing unit can first store the data obtained after data processing on the data in a memory space applied for by the one computing unit, and then another computing unit can copy the data stored in the one memory space to another memory space applied for by the other computing unit, and process the copied data until at least one computing unit completes data processing on the business data.
[0034] However, since the other computing unit needs to copy the data obtained after data processing by one of the at least one computing units to the other memory space before processing the data, it may cause the other computing unit to wait for a long time before processing the data, thereby causing the at least one computing unit to take a long time to complete the data processing of the business data. Moreover, when at least one computing unit needs to process the business data, since they belong to different memory units and use different memory mechanisms, the data needs to be copied in each memory unit. As a result, the efficiency of data processing of the business data by at least one computing unit is poor.
[0035] In an embodiment of the present application, an electronic device obtains a first memory parameter corresponding to a first business, determines a first memory pool according to the first memory parameter, the first memory pool includes at least one first memory block, and allocates at least one first memory block to at least one first computing unit, which is used to process data of the first business. In the scheme of the present application, the electronic device can first obtain the first memory parameter corresponding to the first business to determine the size of the memory required for the first business, and then determine the first memory pool according to the determined memory size, and then allocate at least one first memory block in the first memory pool to a computing unit used to process data of the first business, that is, the first memory blocks allocated to at least one computing unit used to process data of the first business are all in the same memory pool. On the one hand, in the case where at least one first computing unit includes one computing unit, the electronic device can also allocate the first memory block in the first memory pool to other computing units used to process data of the first business, so that after the one computing unit stores the data obtained after data processing of the data of the first business in the first memory block, the other computing units can allocate the first memory blocks in the first memory pool to the computing units used to process data of the first business. On the one hand, the first computing unit can directly read the data stored in the first memory block without copying the data to the memory space applied for by other computing units; on the other hand, in the case where at least one first computing unit includes at least two computing units, after each computing unit stores the data obtained after data processing on the data of the first business in the first memory block, the other computing units can directly read the data stored in the first memory block without copying the data to another memory space applied for by other computing units; therefore, the waiting time required for other computing units to process the data can be reduced, thereby reducing the time required for at least one computing unit to complete data processing on the data of the first business, and thus, the efficiency of the computing units in processing the business data can be improved.
[0036] The execution subject of the memory allocation method may be a memory allocation device, or an electronic device, or a functional module in an electronic device. The following takes an electronic device as an example to illustrate the technical solution provided in the embodiment of the present application.
[0037] Figure 1 A flowchart of a memory allocation method provided in an embodiment of the present application is shown. Figure 1 As shown, the memory allocation method provided in the embodiment of the present application may include the following steps 201 to 203.
[0038] Step 201: The electronic device obtains a first memory parameter corresponding to a first service.
[0039] Optionally, in the embodiment of the present application, the first service may be some complex services, for example, may include at least one of the following: photo taking service, video recording service, game service, robot service, augmented reality (AR), virtual reality (VR), AI, etc. It should be noted that the first service may also be a service that requires memory scenarios in other terminal services, which is not limited in the embodiment of the present application.
[0040] Optionally, in the embodiment of the present application, the first memory parameter may be a required memory configuration read by the electronic device according to the current first business scenario.
[0041] Optionally, in an embodiment of the present application, the above-mentioned memory configuration may include at least one of the following: memory size, memory performance, memory category statistics, etc.
[0042] In an embodiment of the present application, when a business scenario in an electronic device starts, for example, when the electronic device needs to perform a photo-taking service, or other business scenarios that require memory, a first memory parameter corresponding to the business scenario is obtained.
[0043] Step 202: The electronic device determines a first memory pool according to a first memory parameter.
[0044] In the embodiment of the present application, the first memory pool includes at least one first memory block.
[0045] It should be noted that the first memory pool is the entire memory pool block, and then the entire memory pool block is divided into at least one first memory block.
[0046] In an embodiment of the present application, when there are free memory blocks in the existing memory pool, the existing memory pool is updated to the first memory pool according to the first memory parameter; when there are no free memory blocks in the existing memory pool, the first memory pool is applied for from the operating system outside the memory pool according to the first memory parameter.
[0047] Exemplarily, assuming that the electronic device is in a business scenario of photographing night scenes, and the electronic device obtains that the first memory parameter in the business scenario is 100 MB, then it is determined that the memory size of the required first memory pool is 100 MB.
[0048] For example, Figure 2 As shown, when different business scenarios start to work, memory needs to be applied for. At this time, the first step is to read the memory pool size configuration of the current scenario according to the scenario configuration, and create or update the memory pool (ie, the first memory pool). The second step is that different computing units apply for, release memory or cache operations through the memory pool interface. The third step is that when the business scenario work is completed, the different memory usage categories in the current business scenario are classified and counted, and the performance of the current memory operation is counted. The fourth step is to release the memory pool when switching to a business scenario that does not require a memory pool or exiting the current major category of business scenarios. At this time, the process of building a memory pool across computing units and realizing memory reuse of different computing units is completed.
[0049] As another example, when the business scenario requires memory, a memory pool is created, and the memory size of the current memory pool is set according to the configuration. The business memory requirements of different scenarios are pre-configured according to prior information, such as configuration files such as xml and json, or can be configured in code, including but not limited to the above-described methods. For example: Scenario a: 320MB; Scenario b: 350MB; Scenario c: 480MB, etc. Of course, small scenarios under large scenarios can also be classified, such as sub-scenarios such as scenario a-1 and scenario a-2 under scenario a. If it is the first time to create a memory pool, then create the memory pool directly. When creating the memory pool, the memory size of each memory pool and the margin configuration need to be considered, which is defined as PoolMargin, which means that when the memory value of the two memory pools is within the PoolMargin range when the memory pool is updated, it will not be updated. In the memory pool configuration, the size of each memory pool is defined as PerPoolSize, such as 100MB, and the PoolMargin of the memory pool, such as 20MB. These values can be configured according to the actual scenario usage needs. The PoolMargin value is used when the memory pool is updated and does not need to be used when it is created for the first time.
[0050] When creating a multi-level memory pool, assuming that the number of memory pools is n (n>=1), the size of the first n-1 memory pools is the size of the above PerPoolSize, and the size of the last memory pool is irregular. If there is no memory pool currently, it is the first creation. For example, if the current memory pool is configured as 350MB, if PerPoolSize is configured as 150MB, and PoolMargin is 20MB, then three memory pools need to be created, the first memory pool is 150MB, the second memory pool is also 150MB, and the third memory pool is 50MB, that is, LastPoolSize is 50MB. If the previous memory size is 350MB, and the current scenario needs to apply for a 480MB memory pool, then keep the previous two 150MB memory pools, and then apply for another 150MB memory. The last memory is ideally 30MB, but since the size of the last memory pool is 50MB, the difference between the two memory pools is within 20MB, so keep the previous 50MB memory pool. Then a total of 500MB of memory is used. After all memory pools have been updated, all computing units are memory mapped to the updated memory pools. This memory mapping means that each computing unit has a physical address when in use, and the physical address needs to be converted into a virtual address for memory mapping so that each computing unit can process data in the updated memory pool.
[0051] As another example, Figure 3 As shown, the business scenario starts to read the memory pool configuration of the current scenario according to the scenario configuration. Traverse all memory pools to determine whether there is a memory pool. If it exists, determine whether the difference between the memory pool to be configured and the existing original memory pool meets PoolMargin. If not, determine the size relationship between the memory pool to be configured and the existing original memory pool. If the latter is larger than the former, the memory pool is reduced and updated, that is, the overlapping PerPoolSize memory pool is left and the redundant memory pool is released; if the latter is smaller than the former, the memory pool is expanded and updated, that is, the overlapping PerPoolSize memory pool is left and more memory pools are re-applied. If you only need to determine whether the tail memory pool of the memory pool to be configured and the existing original memory pool meets PoolMargin, if PoolMargin is not satisfied, you only need to release the tail memory pool and re-apply for the tail memory pool, and then perform memory mapping of different units for each new updated memory pool; if PoolMargin is satisfied, the size of the tail memory pool does not need to be updated. Finally, the memory pool application process ends.
[0052] As another example, here we assume that PoolMargin is set to 20MB. If the original memory pool size is 350MB, and the current new scenario sets the memory pool size to 340MB or 360MB, then the difference delta between the new and old memory pools is 10MB. If delta<=PoolMargin setting, then there is no need to update the old memory. On the contrary, if the difference delta between the new and old memory pools is 30MB, then it is necessary to determine the size relationship between the memory pool to be configured and the existing original memory pool. Assuming that the new memory pool is 480MB and the old memory pool is 350MB, with PerPoolSize being 150MB and PoolMargin being 20MB, then the tail memory pool of the new memory pool is 30MB, and the tail memory pool of the old memory pool is 50MB. The difference delta of the tail memory pool is 20MB, and if delta<=PoolMargin is satisfied, then the tail memory pool does not need to be updated. Otherwise, the tail memory pool needs to be updated.
[0053] In the embodiment of the present application, the flowchart of applying for memory through the memory pool is as follows: Figure 4 As shown, first traverse all the free memory block lists of the memory pool to see if there are any that meet the requirements of the current memory block application. If none of them meet the requirements, of course, this situation also includes the situation where the memory pool has not been applied for, then directly apply for it outside the memory pool (apply from the operating system). If it is determined that the currently traversed free memory list meets the memory requirements of the application, then find the free memory block in the free memory block list in the memory pool that is most suitable for the current memory application (such as the industry's First-fit algorithm, Best-fit algorithm, TLSF (Two-Level Segregated Fit) algorithm) and delete the selected free memory block from the free list. If the selected free memory block is larger than the memory applied for, then memory cutting is performed, otherwise it can be used directly. The free memory blocks cut from the memory block are put into the free memory block list. Finally, the current memory application situation is counted through maintenance and testing, including business scenarios, classification statistics of memory users, etc.
[0054] In the embodiment of the present application, the memory release flow chart of releasing memory through the memory pool is as follows: Figure 5 As shown, first determine whether the memory block to be released is the memory requested in the memory pool. If it is requested outside the pool, then directly follow the memory pool release process outside the pool. If it is the memory requested by the memory pool, first find the memory pool where the memory block to be released is located, and determine whether there are free memory blocks before and after the current memory block. If there are, merge the new free memory blocks. The merged memory blocks are put into the free list of the current memory pool. Finally, after the release is completed, maintenance and measurement statistics are performed.
[0055] In the embodiment of the present application, for a single memory pool, such as Figure 6As shown in the figure, the memory pool is divided into two blocks during initialization. The first is the free memory block and the second is the tail sentinel memory block. Then there is only one free memory block list. The first time you apply for memory from the memory pool, Figure 7 As shown in the figure, the original free memory block 1 is directly split, and the remaining memory blocks after the split are put into the free memory block list. After multiple requests and releases of the memory pool, there will be multiple free memory blocks, such as Figure 8 As shown, these free memory blocks are represented by a free memory block list.
[0056] For example, Fig. 9 As shown, when the DDR on the CPU side is shared with GPU or non-CPU modules such as DSP and NPU, the CPU consistency needs to be considered and the Cache clean operation needs to be performed. ① indicates that the CPU applies for memory, and then the application processes data for the memory. ② indicates that after ① processing, the part of the CPU cache corresponding to the previously used memory will be automatically set to dirty by the CPU, indicating that the current data is still in the CPU cache and the data in the DDR has not been updated. ③ When different computing units use the same memory, since the CPU cache corresponding to the current memory is dirty, the Cache clean operation must be performed first to flush the data on the CPU cache back to the DDR so that the memory data in the DDR is correct. Then the GPU can read the data corresponding to the current memory.
[0057] When the DDR of non-CPU modules is shared with the CPU, the cache consistency of the CPU needs to be considered, and a cache invalidate operation needs to be performed. For example, when the DDR of GPU, DSP, and NPU is shared with the CPU, a cache invalidate operation needs to be performed. Fig.10 As shown in the figure, taking the GPU sharing with the CPU as an example, ① indicates that the GPU processes data for a piece of memory. Since the CPU cache previously mapped this piece of memory by cache, the data in DDR is correct, but the data in Cache is incorrect. ② The CPU processes the data after the GPU. Since the data in DDR is correct and the data in Cache is incorrect, it is necessary to set cache invalidate so that the CPU can read data directly from DDR instead of using the data in CPU cache. ③ After setting cache invalidate, the data read by the CPU in this memory is correct.
[0058] In addition, if other non-CPU computing units have cache, the processing method is similar to the operation process of the CPU cache and is implemented in their respective drivers or chips.
[0059] In this way, the electronic device can determine the memory size of the first memory pool according to the first memory parameter, so as to apply for the required memory pool for the first service more accurately, thereby making memory management more efficient.
[0060] Optionally, in the embodiment of the present application, the above step 202 can be specifically implemented by the following step 301 or step 302.
[0061] Step 301: Without applying for any memory pool, the electronic device applies for a first memory pool from the operating system according to a first memory parameter.
[0062] In the embodiment of the present application, the electronic device has not applied for any memory pool, which means that there is no available free memory in the memory pool and the memory pool cannot be applied for. Therefore, it is necessary to apply for memory management to the operating system outside the memory pool according to the first memory parameter to apply for the first memory pool.
[0063] Exemplarily, the first memory parameter includes the memory size of the first memory pool, so that the electronic device can send a memory application request to the operating system, and the memory application request includes the memory size of the first memory pool, so as to apply to the operating system for the first memory pool of the memory size of the first memory pool. Assuming that the memory size of the first memory pool in the first memory parameter is 450MB, the electronic device can send a memory application request of 450MB to the operating system, and thus apply to the operating system for the first memory pool of 450MB of memory size.
[0064] Step 302: When the second memory pool has been applied for, the electronic device determines the first memory pool according to the second memory pool and the first memory parameter.
[0065] In the embodiment of the present application, the second memory pool may be a memory pool previously applied for by the electronic device.
[0066] Optionally, the second memory pool may be a memory previously applied by the electronic device for a second service, the second service being of the same service type as the first service. The second memory pool may also be a memory pool recently applied by the electronic device and corresponding to the first service.
[0067] Exemplarily, if the memory size of the second memory pool is 300 MB and the memory size of the first memory pool is 250 MB, it is necessary to determine a first memory pool of 250 MB in the second memory pool according to the first memory parameter.
[0068] In an embodiment of the present application, if the difference in memory size between the second memory pool and the first memory pool is within a preset range, the electronic device can directly determine the second memory pool as the first memory pool; or, if the difference in memory size between the second memory pool and the first memory pool is large, some redundant memory blocks in the second memory pool can be released to obtain the first memory pool; or, when the free memory blocks in the second memory pool are not enough for the business type of the first business, some memory blocks can be applied to the operating system, and the applied memory blocks plus the available free memory blocks in the second memory pool are determined as the first memory pool.
[0069] In this way, the electronic device can apply for the first memory pool regardless of whether it has applied for a memory pool or not, thereby achieving better memory management.
[0070] Optionally, in the embodiment of the present application, the first memory parameter includes: the memory size of the first memory pool and the first difference threshold. The "electronic device determines the first memory pool according to the second memory pool and the first memory parameter" in the above step 302 can be specifically implemented by the following step 401 or step 402.
[0071] Step 401: When a first difference between a memory size of a second memory pool and a memory size of a first memory pool is less than or equal to a first difference threshold, the electronic device determines the second memory pool as the first memory pool.
[0072] Optionally, in the embodiment of the present application, the first difference threshold may be equal to the memory pool size in the first memory pool except the memory size of the first memory pool, and the first difference threshold may be a fixed size, for example, the first difference threshold may be 10MB, 20MB, etc.
[0073] In an embodiment of the present application, the first memory pool may be composed of N memory pools, wherein N-1 memory pools may be of the same memory size, and one memory pool with a smaller memory size may be called a tail memory pool. For example, the memory size of the first memory pool may be 480MB, and the first memory pool may be composed of four memory pools, whose memory sizes are 150MB, 150MB, 150MB, and 30MB, respectively, wherein the memory size of 30MB is the tail memory pool, and N is an integer greater than or equal to 1.
[0074] Optionally, in the embodiment of the present application, the first difference may be a difference between a memory size of the second memory and a memory size of the first memory.
[0075] In an embodiment of the present application, the first business memory requirement of different scenarios is pre-configured according to prior information, for example, there will be multiple configuration files, and the memory requirement can also be configured by code, including but not limited to the method described above. For example, in the following scenarios, scene a: 320MB; scene b: 350MB; scene c: 480MB, etc. Of course, it is also possible to classify small scenes under large scenes, such as scene a has scene a-1, scene a-2 and other sub-scenes. If it is the first time to create a memory pool, then create the memory pool directly. When creating a memory pool, you need to consider the memory size of each memory pool and the margin configuration. The margin configuration is defined as PoolMargin, that is, the first difference threshold, which means that when the memory pool is updated, if the first difference between the memory values of the two memory pools is within the PoolMargin range, then no update will be performed.
[0076] For example, if the size of the second memory pool is 350MB, and the size of the first memory pool currently set for the first business is 340MB or 360MB, then the first difference between the second memory pool and the first memory pool is 10MB. If the first difference is <= the setting of the first difference threshold, then there is no need to update the second memory pool, and the second memory pool is directly determined as the first memory pool.
[0077] As another example, if the first memory pool is 480MB and the second memory pool is 350MB, with PerPoolSize being 150MB and the first difference threshold (PoolMargin) being 20MB, then the tail memory pool of the first memory pool is 30MB and the tail memory pool of the second memory pool is 50MB, and the first difference (tail memory pool difference) is 20MB, and if the first difference <= first difference threshold, the tail memory pool does not need to be updated, otherwise the tail memory pool needs to be updated.
[0078] Step 402: When a first difference between the memory size of the second memory pool and the memory size of the first memory pool is greater than a first difference threshold, the electronic device updates the memory size of the second memory pool to the memory size of the first memory pool, and determines the updated second memory pool as the first memory pool.
[0079] For example, if the first memory pool is 400MB and the second memory pool is 350MB, wherein PerPoolSize is 150MB, and the first difference threshold (PoolMargin) is 20MB, then the tail memory pool of the first memory pool is 100MB, and the tail memory pool of the second memory pool is 50MB, and the first difference (tail memory pool difference) is 50MB, which does not satisfy the first difference <= first difference threshold. In this case, the tail memory pool needs to be updated, and the second memory pool is updated to the first memory pool.
[0080] In this way, the electronic device can directly determine whether the second memory pool needs to be updated and whether the second memory pool can be determined as the first memory pool based on the size relationship between the first difference between the memory size of the second memory pool and the memory size of the first memory pool and the first difference threshold, thereby increasing the efficiency of memory pool application.
[0081] Optionally, in an embodiment of the present application, the first difference is greater than a first difference threshold; the second memory pool includes N first sub-memory pools, where N is a positive integer. The "electronic device updates the memory size of the second memory pool to the memory size of the first memory pool" in the above step 402 can be specifically implemented by the following step 402a or step 402b.
[0082] Step 402a: When the memory size of the first memory pool is smaller than the memory size of the second memory pool, the electronic device releases at least one first sub-memory pool from the N first sub-memory pools.
[0083] Optionally, in an embodiment of the present application, the memory size of the first sub-memory pool is equal to the first difference.
[0084] For example, the first memory pool is 200MB, the second memory pool is 350MB, the PerPoolSize is 150MB, the first difference threshold PoolMargin is 20MB, and the first difference is 150MB. At this time, the 350MB of the second memory pool is three PerPoolSizes, namely, A: 150MB, B: 150MB, and C: 150MB, and the first memory pool only needs 150MB+50MB of memory, so C (150MB) in the second memory pool is released, that is, 150MB is the memory size of the first sub-memory pool and is equal to the first difference.
[0085] Optionally, the electronic device may first determine a first number of first sub-memory pools that need to be released according to the first difference, and then release the first number of first sub-memory pools from the N first sub-memory pools.
[0086] The first number is an integer greater than or equal to 1, such as 1, 2, 3, 4, 5, 6, etc.
[0087] In this way, the electronic device can quickly update the size of the second memory pool by releasing the first sub-memory pool from the second memory pool.
[0088] Step 402b: When the memory size of the first memory pool is larger than the memory size of the second memory pool, the electronic device applies for at least one second sub-memory pool.
[0089] Optionally, in an embodiment of the present application, a memory size of the at least one second sub-memory pool is equal to the first difference.
[0090] Optionally, in the embodiment of the present application, the memory size of each second sub-memory pool may be the same as or different from the memory size of each first sub-memory pool.
[0091] Optionally, the electronic device may first determine a first number of second sub-memory pools to be applied for according to the first difference, and then apply to the operating system for the first number of second sub-memory pools.
[0092] In this way, the electronic device can quickly update the second memory pool by applying for at least one second sub-memory pool.
[0093] Optionally, in an embodiment of the present application, the first difference is greater than a first difference threshold; the first memory pool includes a first tail memory pool, and the second memory pool includes a second tail memory pool. Exemplarily, the step 402 can be implemented by the following step 402c or step 402d.
[0094] Step 402c: When a second difference between the memory size of the first tail memory pool and the memory size of the second tail memory pool is greater than a first difference threshold, the electronic device releases the second tail memory pool and applies for the first tail memory pool.
[0095] For example, if the memory size of the first memory pool is 490MB, the memory size of the second memory pool is 460MB, and the first difference threshold (PoolMargin) is 20MB, the memory size of the first tail memory pool is 40MB, and the size of the second tail memory pool is 10MB, so the second difference between the memory size of the first tail memory pool and the memory size of the second tail memory pool is 30MB. Because the second difference is greater than the first difference threshold, the tail memory pool needs to be updated. Then the second tail memory pool will be directly released, and after the release is completed, a new first tail memory pool with a memory pool size of 40MB will be applied.
[0096] In this way, the electronic device can quickly release the second tail memory pool and reapply for the first tail memory pool when the difference between the memory sizes of the first tail memory pool and the second tail memory pool is large, thereby avoiding occupying more memory size.
[0097] Step 402d: When a second difference between the memory size of the first tail memory pool and the memory size of the second tail memory pool is less than a first difference threshold, the electronic device determines the second tail memory pool as the first tail memory pool.
[0098] In this way, when the difference between the memory size of the first tail memory pool and the memory size of the second tail memory pool is small, the electronic device can directly determine the second tail memory pool as the first tail memory pool without re-releasing and / or applying for the memory pool. Therefore, the performance and power consumption overhead of frequent memory application or release of the electronic device can be reduced.
[0099] Step 203: The electronic device allocates at least one first memory block to at least one first computing unit.
[0100] In the embodiment of the present application, the first computing unit is used to process data of the first business.
[0101] In the embodiment of the present application, the above-mentioned first computing unit can be a computing unit such as a CPU, GPU, DSP, NPU, etc., and the present application does not limit this.
[0102] In an embodiment of the present application, the electronic device may first receive a memory application request from at least one first computing unit, the request including the required memory size, and then allocate the memory required by at least one first computing unit to the at least one first computing unit from at least one first memory block.
[0103] Optionally, in an embodiment of the present application, the memory block allocated to at least one first computing unit may be the same, and the memory block corresponds to at least one first computing unit in the first business.
[0104] In an embodiment of the present application, the electronic device may first obtain a first memory parameter corresponding to the first business to determine the size of the memory required for the first business, and then determine a first memory pool according to the determined memory size, and then allocate at least one first memory block in the first memory pool to a computing unit for processing data of the first business, that is, the first memory blocks allocated to at least one computing unit for processing data of the first business are all in the same memory pool. On the one hand, in the case where at least one first computing unit includes one computing unit, the electronic device may also allocate the first memory block in the first memory pool to other computing units for processing data of the first business, so that after the one computing unit stores the data obtained after processing the data of the first business in the first memory block, the other The computing unit can directly read the data stored in the first memory block without copying the data to the memory space requested by other computing units; on the other hand, in the case where at least one first computing unit includes at least two computing units, after each computing unit stores the data obtained after data processing on the data of the first business in the first memory block, the other computing units can directly read the data stored in the first memory block without copying the data to another memory space requested by other computing units; therefore, the waiting time required for other computing units to process the data can be reduced, thereby reducing the time required for at least one computing unit to complete data processing on the data of the first business, and thus, the efficiency of the computing units in processing the business data can be improved.
[0105] Optionally, in an embodiment of the present application, before the above step 203, the memory allocation method provided in the embodiment of the present application may further include the following step 501, and the above step 203 may be specifically implemented by the following steps 203a and 203b.
[0106] Step 501: The electronic device determines whether there is a target free memory block in the free memory blocks of at least one first memory block, the memory size of which is greater than or equal to the memory size required by the first computing unit.
[0107] In the embodiment of the present application, the above-mentioned free memory block is a memory block that is not used by any computing unit to process data and store data.
[0108] It should be noted that the electronic device determines the target free memory block whose memory size is greater than or equal to the memory size required by the first computing unit from the free memory blocks in at least one first memory block, which means selecting the target free memory block from the free memory blocks in at least one first memory block and the at least one first memory block with the smallest memory size difference, or it may be the smallest tail memory difference between the two, and the present application does not make any specific restrictions here.
[0109] Step 203a: When there is a target free memory block whose memory size is greater than or equal to the memory size required by the first computing unit in the free memory blocks in at least one of the first memory blocks, the electronic device allocates the target free memory block to a first computing unit.
[0110] Optionally, in the embodiment of the present application, the above-mentioned first computing unit may be any computing unit of at least one first computing unit.
[0111] For example, if there are free memory blocks in at least one of the first memory blocks, which are 10MB, 20MB, and 30MB respectively. The memory size required by a first computing unit is 9MB, and among the three free memory blocks, 10MB has the smallest difference with the memory size required by the first computing unit, that is, it is the free memory block that is greater than or equal to 9MB of the memory size required by the first computing unit and is the most matched. At this time, the free memory block with a memory size of 10MB is set as the target free memory block and allocated to the first computing unit, and the remaining 1MB free memory block is added to the free memory list.
[0112] Optionally, in an embodiment of the present application, the electronic device allocates a target free memory block to a first computing unit, including: cutting the target free memory block to obtain a memory block requested by the first computing unit and a second free memory block; adding the second free memory block to a free memory list. For example, when there are 80MB of free memory blocks, and the memory size required by the first computing unit is 20MB, 20MB is cut out of the 80MB free memory block as the memory block requested by the first computing unit, and the remaining 60MB of memory blocks are added to the free memory list.
[0113] Step 203b: When there is no target free memory block whose memory size is greater than or equal to the memory size required by the first computing unit in the free memory blocks in at least one first memory block, the electronic device applies for a second memory block from the operating system according to the memory size required by the first computing unit.
[0114] It should be noted that if there is no target free memory block in the free memory blocks of at least one of the first memory blocks that is greater than or equal to the memory size required by the first computing unit, it means that all memory blocks in the memory pool are in use, or the free memory blocks do not meet the requirements for applying for memory, that is, there is no free memory available for application.
[0115] Optionally, in the embodiment of the present application, the above-mentioned application for the second memory block from the operating system may be a memory application to the operating system outside the memory pool, and the size of the applied memory is the size of the second memory block.
[0116] Optionally, in the embodiment of the present application, the first memory pool includes at least one first sub-memory pool, and the target free memory block is a free memory block in the target sub-memory pool in the at least one first sub-memory pool. Exemplarily, after the above step 203a, the memory allocation method provided in the embodiment of the present application may further include the following step 601.
[0117] Step 601: When the target free memory block is completely used, the electronic device releases the target free memory block to the target sub-memory pool.
[0118] It can be understood that the completion of use of the target free memory block means that the at least one computing unit has completed processing and storage of its data, that is, the target free memory block is no longer needed to process data.
[0119] Optionally, in the embodiment of the present application, the target sub-memory pool may be a second memory pool.
[0120] Optionally, in an embodiment of the present application, after the above step 601, the memory allocation method provided in the embodiment of the present application may further include the following step 601a.
[0121] Step 601a: When the target sub-memory pool includes a first free memory block adjacent to the target free memory block, merge the target free memory block and the first free memory block.
[0122] Exemplarily, there are a target free memory block (for example, memory block A) and a first free memory block (for example, memory block B) in the second memory pool. If memory block A and memory block B are adjacent to each other, memory block A and memory block B are merged into one memory block. Memory block B can be the memory block in front of memory block A or the memory block behind memory block A.
[0123] In this way, free memory blocks can be merged to facilitate the next memory application, thereby improving the efficiency of memory management.
[0124] Optionally, in the embodiment of the present application, after the above step 203b, the memory allocation method provided in the embodiment of the present application may further include the following step 101.
[0125] Step 101: The electronic device releases the second memory block to the memory of the operating system.
[0126] It should be noted that the second memory block is the memory applied for from the operating system's memory, so when releasing the memory, it is also necessary to release the memory to the operating system. It can be understood that the operating system's memory is the system memory except the first memory pool.
[0127] In this way, the electronic device releases the second memory block to the memory of the operating system except the first memory pool, which can realize reasonable recycling of the memory and make the utilization rate of the memory higher.
[0128] Optionally, in the embodiment of the present application, the at least one first computing unit includes a second computing unit and a third computing unit, and the third computing unit is a CPU. Optionally, after the above step 203, the memory allocation method provided in the embodiment of the present application may also include the following steps 102 and 103.
[0129] Step 102: The electronic device determines a first cache area associated with a first memory block.
[0130] In the embodiment of the present application, the first cache area is a cache area of the third computing unit, and the first cache area is used to store data after the third computing unit processes the data in the first memory block.
[0131] Optionally, in the embodiment of the present application, the second computing unit may be a GPU.
[0132] In the embodiment of the present application, the first memory block maps dirty data of the first cache area.
[0133] In the embodiment of the present application, the first memory block in the above step 102 may be a third memory block, and the third memory block is a memory block that cannot be shared by at least one first computing unit in at least one first memory block.
[0134] Step 103: When the second computing unit of the electronic device needs to obtain data stored in the first memory block, the third computing unit of the electronic device controls the dirty data in the first cache area to be written into the first memory block, and reads the written data in the first memory block through the second computing unit.
[0135] In this way, when the second computing unit needs to obtain the data stored in the first memory block, the electronic device can control the third computing unit to write the dirty data in the first cache area into the first memory block, thereby ensuring that the data in the first memory block is consistent with the dirty data in the first cache area of the third computing unit. Therefore, it can be ensured that the data in the first memory block read by the second computing unit is consistent with the dirty data in the first cache area, thereby ensuring the consistency of the data being processed.
[0136] Optionally, in the embodiment of the present application, before the above step 102, the memory allocation method provided in the embodiment of the present application may further include the following step 104.
[0137] Step 104: When the third computing unit stores the processed data in the first cache area, the electronic device establishes an association relationship between the area address of the first cache area and the memory address of the first memory block.
[0138] In the embodiment of the present application, the area address of the first cache area is a physical address, and the physical address needs to be converted into a virtual address to establish a connection with the memory address of the first memory block.
[0139] For example, Fig.11 As shown, since the memory pool is a whole block of memory requested, such as dma_buf, it is necessary to support the local cache flushing interface and local cache flushing. The local cache flushing interface is the incoming fd, offset, and size, and then the cache clean and cache invalidate operations are performed based on this triple. Among them, fd represents the handle of the whole large memory, offset refers to the offset from the starting address of the large memory to the starting address of the split memory block, and size refers to the size of the current split memory block. The memory address of the first memory block mentioned above is the fd offset, size triple.
[0140] It should be noted that dma_buf is an independent subsystem in the Linux kernel, which provides a unified framework for sharing buffers between different devices and subsystems, and implements memory sharing and transmission through DMAbuffer and DMA buffer file descriptors. The current memory pool uses the dma_buf memory type, which is included but not limited to this memory type in actual scenarios. When using dma_buf memory, the drivers of computing units such as GPU, NPU, and DSP need to support importing dma_buf and support conversion of memory block fd, offset, and size.
[0141] Optionally, in the embodiment of the present application, after the above step 103, the memory allocation method provided in the embodiment of the present application may further include the following step 105.
[0142] Step 105: When the second computing unit writes the processed data into the first memory block, the electronic device prohibits the third computing unit from reading data from the first cache area, and controls the third computing unit to read data from the first memory block.
[0143] Exemplarily, when the memory of a computing unit (such as the CPU end) is used by other computing units (such as GPU or non-CPU modules such as DSP, NPU, etc.), since the first memory pool has been used by the CPU and used to process data, in order to maintain the consistency of the cache (each computing unit has a high-speed cache area that distinguishes the memory) interface, the part of the CPU cache corresponding to the previously used memory needs to be automatically set to dirty by the CPU, and the local cache interface of the memory pool is updated. Then when different computing units use the same piece of memory to process data, since the CPU cache corresponding to the current memory is dirty, the cache clean operation must be performed first to flush the data on the CPU cache back to the memory data in the memory is correct. Then other computing units can read the data corresponding to the current memory.
[0144] It should be noted that the interaction between caches of non-CPU computing units, such as DSP, GPU and other caches, is implemented by their respective drivers or hardware.
[0145] The memory allocation method provided in the embodiment of the present application is that the electronic device obtains the first memory parameter corresponding to the first business, determines the first memory pool according to the first memory parameter, the first memory pool includes at least one first memory block, and allocates the at least one first memory block to at least one first computing unit, and the first computing unit is used to process the data of the first business. In the scheme of the present application, the electronic device can first obtain the first memory parameter corresponding to the first business to determine the size of the memory required for the first business, and then determine the first memory pool according to the determined memory size, and then allocate at least one first memory block in the first memory pool to the computing unit used to process the data of the first business, that is, the first memory blocks allocated to at least one computing unit used to process the data of the first business are all in the same memory pool. On the one hand, when at least one first computing unit includes one computing unit, the electronic device can also allocate the first memory block in the first memory pool to other computing units used to process the data of the first business, so that after the one computing unit stores the data obtained after processing the data of the first business in the first memory block, the other computing units On the one hand, when at least one first computing unit includes at least two computing units, after each computing unit stores the data obtained after processing the data of the first business in the first memory block, the other computing units can directly read the data stored in the first memory block without copying the data to another memory space applied for by other computing units; therefore, the waiting time required for other computing units to process the data can be reduced, thereby reducing the time required for at least one computing unit to complete the data processing of the first business data, and thus, the efficiency of the at least two computing units in processing the business data can be improved.
[0146] Each of the above-mentioned method embodiments, or various possible implementation methods in each method embodiment can be executed separately, or any two or more of them can be executed in combination with each other. The specific implementation method can be determined according to actual usage requirements, and the embodiments of the present application do not limit this.
[0147] The memory allocation method provided in the embodiment of the present application can be executed by a memory allocation device. In the embodiment of the present application, the memory allocation device provided in the embodiment of the present application is described by taking the memory allocation device executing the memory allocation method as an example.
[0148] Fig.12 A possible structural diagram of a memory allocation device involved in some embodiments of the present application is shown. Fig.12 As shown, the memory allocation device 70 may include: an acquisition module 71 , a determination module 72 and an allocation module 73 .
[0149] The acquisition module 71 is used to acquire a first memory parameter corresponding to the first service. The determination module 72 is used to determine a first memory pool according to the first memory parameter acquired by the acquisition module, wherein the first memory pool includes at least one first memory block. The allocation module 73 is used to allocate at least one first memory block to at least one first computing unit, wherein the first computing unit is used to process data of the first service.
[0150] In a possible implementation, the determination module 72 is specifically configured to apply for a first memory pool from the operating system according to the first memory parameter obtained by the acquisition module 71 when no memory pool has been applied for. Alternatively, when a second memory pool has been applied for, determine the first memory pool according to the second memory pool and the first memory parameter obtained by the acquisition module 71.
[0151] In a possible implementation, the first memory parameter includes: a memory size of the first memory pool and a first difference threshold. The determination module 72 is specifically configured to determine the second memory pool as the first memory pool when the first difference between the memory size of the second memory pool and the memory size of the first memory pool is less than or equal to the first difference threshold. Alternatively, when the first difference between the memory size of the second memory pool and the memory size of the first memory pool is greater than the first difference threshold, update the second memory pool to the memory size of the first memory pool, and determine the updated second memory pool as the first memory pool.
[0152] In a possible implementation, the first difference is greater than a first difference threshold; the second memory pool includes N first sub-memory pools, where N is a positive integer. The determination module 72 is specifically configured to release at least one first sub-memory pool from the N first sub-memory pools when the memory size of the first memory pool is smaller than the memory size of the second memory pool; and to apply for at least one second sub-memory pool when the memory size of the first memory pool is larger than the memory size of the second memory pool.
[0153] In a possible implementation, the first difference is greater than a first difference threshold; the first memory pool includes a first tail memory pool, and the second memory pool includes a second tail memory pool;
[0154] The determination module 72 is specifically used to release the second tail memory pool and apply for the first tail memory pool when the second difference between the memory size of the first tail memory pool and the memory size of the second tail memory pool is greater than the first difference threshold; and to determine the second tail memory pool as the first tail memory pool when the second difference between the memory size of the first tail memory pool and the memory size of the second tail memory pool is less than the first difference threshold.
[0155] In a possible implementation, the above-mentioned determination module 72 is also used to determine whether there is a target free memory block whose memory size matches the memory size required by a first computing unit in the free memory blocks of at least one first memory block before allocating at least one first memory block to at least one first computing unit. The allocation module 73 is specifically used to allocate the target free memory block to a first computing unit when there is a target free memory block whose memory size is greater than or equal to the memory size required by the first computing unit in the free memory blocks of at least one first memory block. The memory allocation device 70 provided in the embodiment of the present application also includes: an application module. Among them, the application module is used to apply for a second memory block from the operating system according to the memory size required by a first computing unit when there is no target free memory block whose memory size is greater than or equal to the memory size required by the first computing unit in the free memory blocks of at least one first memory block.
[0156] In a possible implementation, the first memory pool includes at least one first sub-memory pool, and the target free memory block is a free memory block in a target sub-memory pool in the at least one first sub-memory pool. The memory allocation device 70 provided in the embodiment of the present application also includes: a release module. The release module is used to release the target free memory block to the target sub-memory pool after allocating the target free memory block to a first computing unit and when the target free memory block is used up.
[0157] In a possible implementation, the memory allocation device 70 provided in the embodiment of the present application further includes: a merging module. The merging module is used to merge the target free memory block with the first free memory block after releasing the target free memory block to the target sub-memory pool, if the target sub-memory pool includes a first free memory block adjacent to the target free memory block.
[0158] In a possible implementation, the release module is further configured to release the second memory block to the memory of the operating system after applying for the second memory block from the operating system according to the memory size required by the first computing unit.
[0159] In a possible implementation, at least one first computing unit includes a second computing unit and a third computing unit, and the third computing unit is a CPU. The above-mentioned determination module 72 is also used to determine a first cache area associated with the first memory block after the allocation module 73 allocates at least one first memory block to at least one first computing unit. The first cache area is a cache area of the third computing unit, and the first cache area is used to store data after the third computing unit processes the data in the first memory block. The memory allocation device 70 provided in the embodiment of the present application also includes: a control module, which is used to control the third computing unit to write dirty data in the first cache area into the first memory block when the second computing unit needs to obtain data stored in the first memory block, and read the data in the first memory block after writing through the second computing unit; wherein the first memory block maps the dirty data of the first cache area.
[0160] In one possible implementation, the memory allocation device 70 provided in the embodiment of the present application also includes: a mapping module, which is used to set the part of the first memory block in the third computing unit that maps the first cache area to invalid, and control the third computing unit to read data from the first memory block, after the control module reads the data in the first memory block after writing through the second computing unit, when the third computing unit needs to obtain the data in the first memory block after processing by the second computing unit.
[0161] An embodiment of the present application provides a memory allocation device, which can first obtain a first memory parameter corresponding to a first business to determine the size of the memory required for the first business, then determine a first memory pool according to the determined memory size, and then allocate at least one first memory block in the first memory pool to a computing unit for processing data of the first business, that is, the first memory blocks allocated to at least one computing unit for processing data of the first business are all in the same memory pool. On the one hand, when at least one first computing unit includes one computing unit, the electronic device can also allocate the first memory block in the first memory pool to other computing units for processing data of the first business, so that the data obtained after the one computing unit processes the data of the first business is stored in the first memory block After that, other computing units can directly read the data stored in the first memory block without copying the data to the memory space applied for by other computing units; on the other hand, in the case where at least one first computing unit includes at least two computing units, after each computing unit stores the data obtained after data processing on the data of the first business in the first memory block, other computing units can directly read the data stored in the first memory block without copying the data to another memory space applied for by other computing units; therefore, the waiting time required for other computing units to process the data can be reduced, thereby reducing the time required for at least one computing unit to complete data processing on the data of the first business, and thus, the efficiency of the computing units in processing the business data can be improved.
[0162] The memory allocation device in the embodiment of the present application can be an electronic device, or a component in the electronic device, such as an integrated circuit or a chip. The electronic device can be a terminal, or other devices other than a terminal. Exemplarily, the electronic device can be a mobile phone, a tablet computer, a laptop computer, a palmtop computer, a vehicle-mounted electronic device, a mobile Internet device (Mobile Internet Device, MID), an augmented reality (augmentedreality, AR) / virtual reality (virtualreality, VR) device, a robot, a wearable device, an ultra-mobile personal computer (ultra-mobile personal computer, UMPC), a netbook or a personal digital assistant (personal digital assistant, PDA), etc., and can also be a server, a network attached storage (Network Attached Storage, NAS), a personal computer (personalcomputer, PC), a television (television, TV), a teller machine or a self-service machine, etc., and the embodiment of the present application is not specifically limited.
[0163] The memory allocation device in the embodiment of the present application may be a device having an operating system. The operating system may be an Android operating system, an iOS operating system, or other possible operating systems, which are not specifically limited in the embodiment of the present application.
[0164] The memory allocation device provided in the embodiment of the present application can implement each process of the above-mentioned method embodiment. To avoid repetition, they will not be described again here.
[0165] Alternatively, if Fig.13 As shown, an embodiment of the present application also provides an electronic device 1000, including a processor 1001 and a memory 1002, wherein the memory 1002 stores programs or instructions that can be executed on the processor 1001, and when the program or instructions are executed by the processor 1001, the various steps of the above-mentioned memory allocation method embodiment are implemented, and the same technical effect can be achieved. To avoid repetition, they are not repeated here.
[0166] It should be noted that the electronic devices in the embodiments of the present application include the mobile electronic devices and non-mobile electronic devices mentioned above.
[0167] Fig.14 A schematic diagram of the hardware structure of an electronic device to implement an embodiment of the present application.
[0168] The electronic device 100 includes but is not limited to components such as a radio frequency unit 101, a network module 102, an audio output unit 103, an input unit 104, a sensor 105, a display unit 106, a user input unit 107, an interface unit 108, a memory 109, and a processor 110.
[0169] Those skilled in the art will appreciate that the electronic device 100 may also include a power source (such as a battery) for supplying power to various components, and the power source may be logically connected to the processor 110 through a power management system, thereby implementing functions such as managing charging, discharging, and power consumption management through the power management system. Fig.14 The electronic device structure shown in the figure does not constitute a limitation on the electronic device. The electronic device may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently, which will not be described in detail here.
[0170] Among them, the above-mentioned processor 110 is used to obtain a first memory parameter corresponding to the first business, determine a first memory pool according to the first memory parameter, the first memory pool includes at least one first memory block, and allocate at least one first memory block to at least one first computing unit, and the first computing unit is used to process data of the first business.
[0171] An embodiment of the present application provides an electronic device, which can first obtain a first memory parameter corresponding to a first business to determine the size of the memory required for the first business, and then determine a first memory pool according to the determined memory size, and then allocate at least one first memory block in the first memory pool to a computing unit for processing data of the first business, that is, the first memory blocks allocated to at least one computing unit for processing data of the first business are all in the same memory pool. On the one hand, in the case where at least one first computing unit includes one computing unit, the electronic device can also allocate the first memory block in the first memory pool to other computing units for processing data of the first business, so that after the one computing unit stores the data obtained after data processing of the data of the first business in the first memory block , other computing units can directly read the data stored in the first memory block without copying the data to the memory space applied for by other computing units; on the other hand, in the case where at least one first computing unit includes at least two computing units, after each computing unit stores the data obtained after data processing on the data of the first business in the first memory block, other computing units can directly read the data stored in the first memory block without copying the data to another memory space applied for by other computing units; therefore, the waiting time required for other computing units to process the data can be reduced, thereby reducing the time required for at least one computing unit to complete data processing on the data of the first business, and thus, the efficiency of the computing units in processing the business data can be improved.
[0172] Optionally, the first memory pool is determined according to the first memory parameter, and the processor 110 is specifically configured to apply for the first memory pool from the operating system according to the first memory parameter when no memory pool has been applied for. Alternatively, when the second memory pool has been applied for, the first memory pool is determined according to the second memory pool and the first memory parameter.
[0173] Optionally, the first memory parameter includes: a memory size of the first memory pool and a first difference threshold. The processor 110 is specifically configured to determine the second memory pool as the first memory pool when the first difference between the memory size of the second memory pool and the memory size of the first memory pool is less than or equal to the first difference threshold. Alternatively, when the first difference between the memory size of the second memory pool and the memory size of the first memory pool is greater than the first difference threshold, update the memory size of the second memory pool to the memory size of the first memory pool, and determine the updated second memory pool as the first memory pool.
[0174] Optionally, the first difference is greater than a first difference threshold; the second memory pool includes N first sub-memory pools, where N is a positive integer.
[0175] The processor 110 is specifically configured to release at least one first sub-memory pool from N first sub-memory pools when the memory size of the first memory pool is smaller than the memory size of the second memory pool; and to apply for at least one second sub-memory pool when the memory size of the first memory pool is larger than the memory size of the second memory pool.
[0176] Optionally, the first difference is greater than a first difference threshold; the first memory pool includes a first tail memory pool, and the second memory pool includes a second tail memory pool.
[0177] The processor 110 is specifically used to release the second tail memory pool and apply for the first tail memory pool when the second difference between the memory size of the first tail memory pool and the memory size of the second tail memory pool is greater than the first difference threshold; and to determine the second tail memory pool as the first tail memory pool when the second difference between the memory size of the first tail memory pool and the memory size of the second tail memory pool is less than the first difference threshold.
[0178] Optionally, the first memory pool includes at least one first sub-memory pool, and the target free memory block is a free memory block in a target sub-memory pool in the at least one first sub-memory pool. The processor 110 is further configured to release the target free memory block to the target sub-memory pool after allocating the target free memory block to a first computing unit and when the target free memory block is used up.
[0179] Optionally, the processor 110 is further configured to, after releasing the target free memory block to the target sub-memory pool, merge the target free memory block with the first free memory block when the target sub-memory pool includes a first free memory block adjacent to the target free memory block.
[0180] Optionally, the processor 110 is further configured to release the second memory block to a memory pool other than the first memory pool after applying for the second memory block from the operating system according to the memory size required by a first computing unit.
[0181] Optionally, the at least one first computing unit includes a second computing unit and a third computing unit, and the third computing unit is a CPU.
[0182] The processor 110 is also used to determine a first cache area associated with the first memory block after allocating at least one first memory block to at least one first computing unit, the first cache area being a cache area of a third computing unit, and the first cache area being used to store data after the third computing unit processes the data in the first memory block; and when the second computing unit needs to obtain data stored in the first memory block, control the third computing unit to write dirty data in the first cache area into the first memory block, and read the data in the written first memory block through the second computing unit.
[0183] The first memory block maps dirty data in the first cache area.
[0184] Optionally, the processor 110 is also used to, after reading the data in the first memory block after writing through the second computing unit, set the part of the first memory block in the third computing unit that maps the first cache area to invalid, and control the third computing unit to read data from the first memory block when the third computing unit needs to obtain the data in the first memory block after processing by the second computing unit.
[0185] The electronic device provided in the embodiment of the present application can implement each process implemented in the above method embodiment, and can achieve the same technical effect. To avoid repetition, it is not repeated here. The beneficial effects of various implementations in this embodiment can be specifically referred to the beneficial effects of the corresponding implementations in the above method embodiment. To avoid repetition, it is not repeated here.
[0186] It should be understood that in the embodiment of the present application, the input unit 104 may include a GPU 1041 and a microphone 1042, and the graphics processor 1041 processes the image data of a static picture or video obtained by an image capture device (such as a camera) in a video capture mode or an image capture mode. The display unit 106 may include a display panel 1061, and the display panel 1061 may be configured in the form of a liquid crystal display, an organic light emitting diode, etc. The user input unit 107 includes a touch panel 1071 and at least one of other input devices 1072. The touch panel 1071 is also called a touch screen. The touch panel 1071 may include two parts: a touch detection device and a touch controller. Other input devices 1072 may include, but are not limited to, a physical keyboard, function keys (such as volume control keys, switch keys, etc.), a trackball, a mouse, and a joystick, which will not be repeated here.
[0187] The memory 109 can be used to store software programs and various data. The memory 109 may mainly include a first storage area for storing programs or instructions and a second storage area for storing data, wherein the first storage area may store an operating system, an application program or instructions required for at least one function (such as a sound playback function, an image playback function, etc.), etc. In addition, the memory 109 may include a volatile memory or a non-volatile memory, or the memory 109 may include both volatile and non-volatile memories. Among them, the non-volatile memory may be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory may be a random access memory (RAM), a static random access memory (SRAM), a dynamic random access memory (DRAM), a synchronous dynamic random access memory (SDRAM), a double data rate synchronous dynamic random access memory (DDRSDRAM), an enhanced synchronous dynamic random access memory (ESDRAM), a synchronous link dynamic random access memory (SLDRAM) and a direct memory bus random access memory (DRRAM). The memory 109 in the embodiment of the present application includes but is not limited to these and any other suitable types of memory.
[0188] The processor 110 may include one or more processing units; optionally, the processor 110 integrates an application processor and a modem processor, wherein the application processor mainly processes operations related to an operating system, a user interface, and application programs, and the modem processor mainly processes wireless communication signals, such as a baseband processor. It is understandable that the modem processor may not be integrated into the processor 110.
[0189] An embodiment of the present application also provides a readable storage medium, on which a program or instruction is stored. When the program or instruction is executed by a processor, the various processes of the above-mentioned memory management method embodiment are implemented and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.
[0190] The processor is the processor in the electronic device described in the above embodiment. The readable storage medium includes a computer readable storage medium, such as a computer read-only memory ROM, a random access memory RAM, a magnetic disk or an optical disk.
[0191] An embodiment of the present application further provides a chip, which includes a processor and a communication interface, wherein the communication interface is coupled to the processor, and the processor is used to run programs or instructions to implement the various processes of the above-mentioned memory management method embodiment, and can achieve the same technical effect. To avoid repetition, it will not be repeated here.
[0192] It should be understood that the chip mentioned in the embodiments of the present application can also be called a system-level chip, a system chip, a chip system or a system-on-chip chip, etc.
[0193] An embodiment of the present application provides a computer program product, which is stored in a storage medium. The program product is executed by at least one processor to implement the various processes of the above-mentioned memory management method embodiment and can achieve the same technical effect. To avoid repetition, it will not be repeated here.
[0194] It should be noted that, in this article, the terms "comprise", "include" or any other variant thereof are intended to cover non-exclusive inclusion, so that the process, method, article or device including a series of elements includes not only those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise one..." do not exclude the presence of other identical elements in the process, method, article or device including the element. In addition, it should be pointed out that the scope of the methods and devices in the embodiments of the present application is not limited to performing functions in the order shown or discussed, and may also include performing functions in a substantially simultaneous manner or in reverse order according to the functions involved, for example, the described method may be performed in an order different from that described, and various steps may also be added, omitted, or combined. In addition, the features described with reference to certain examples may be combined in other examples.
[0195] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a computer software product, which is stored in a storage medium (such as ROM / RAM, a disk, or an optical disk), and includes a number of instructions for a terminal (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0196] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of the present application, ordinary technicians in this field can also make many forms without departing from the purpose of the present application and the scope of protection of the claims, all of which are within the protection of the present application.
Claims
1. A memory management method, It is characterized in that include: Obtaining a first memory parameter corresponding to the first service; Determine a first memory pool according to the first memory parameter, wherein the first memory pool includes at least one first memory block; At least one of the first memory blocks is allocated to at least one first computing unit, where the first computing unit is used to process data of the first service.
2. The method according to claim 1, It is characterized in that The determining the first memory pool according to the first memory parameter includes: In the case where no memory pool is applied for, applying for the first memory pool from the operating system according to the first memory parameter; or In a case where the second memory pool has been applied for, the first memory pool is determined according to the second memory pool and the first memory parameter.
3. The method according to claim 2, It is characterized in that The first memory parameter includes: a memory size of the first memory pool and a first difference threshold; The determining the first memory pool according to the second memory pool and the first memory parameter includes: In a case where a first difference between a memory size of the second memory pool and a memory size of the first memory pool is less than or equal to the first difference threshold, determining the second memory pool as the first memory pool; or, When a first difference between the memory size of the second memory pool and the memory size of the first memory pool is greater than the first difference threshold, the memory size of the second memory pool is updated to the memory size of the first memory pool, and the updated second memory pool is determined as the first memory pool.
4. The method according to claim 3, It is characterized in that The first difference is greater than the first difference threshold; the second memory pool includes N first sub-memory pools, where N is a positive integer; The updating the memory size of the second memory pool to the memory size of the first memory pool includes: When the memory size of the first memory pool is smaller than the memory size of the second memory pool, releasing at least one of the first sub-memory pools from the N first sub-memory pools; When the memory size of the first memory pool is larger than the memory size of the second memory pool, at least one second sub-memory pool is applied for.
5. The method according to claim 3, It is characterized in that The first difference is greater than the first difference threshold; the first memory pool includes a first tail memory pool, and the second memory pool includes a second tail memory pool; The updating the memory size of the second memory pool to the memory size of the first memory pool includes: When a second difference between the memory size of the first tail memory pool and the memory size of the second tail memory pool is greater than the first difference threshold, releasing the second tail memory pool and applying for the first tail memory pool; When a second difference between the memory size of the first tail memory pool and the memory size of the second tail memory pool is smaller than the first difference threshold, the second tail memory pool is determined as the first tail memory pool.
6. The method according to claim 1, It is characterized in that Before allocating at least one of the first memory blocks to at least one first computing unit, the method further includes: Determine whether there is a target free memory block whose memory size is greater than or equal to the memory size required by the first computing unit in at least one free memory block in the first memory blocks; The allocating at least one of the first memory blocks to at least one first computing unit comprises: In the case that there is a target free memory block whose memory size is greater than or equal to the memory size required by the first computing unit in at least one free memory block in the first memory blocks, allocating the target free memory block to one of the first computing units; The method further comprises: When there is no target free memory block whose size is greater than or equal to the memory size required by the first computing unit in at least one of the free memory blocks in the first memory blocks, a second memory block is requested from the operating system according to the memory size required by the first computing unit.
7. The method according to claim 6, It is characterized in that The first memory pool includes at least one first sub-memory pool, and the target free memory block is a free memory block in a target sub-memory pool in at least one of the first sub-memory pools; After allocating the target free memory block to one of the first computing units, the method further includes: When the target free memory block is completely used, the target free memory block is released to the target sub-memory pool.
8. The method according to claim 7, It is characterized in that After releasing the target free memory block to the target sub-memory pool, the method further includes: In a case where the target sub-memory pool includes a first free memory block adjacent to the target free memory block, the target free memory block and the first free memory block are merged.
9. The method according to claim 6, It is characterized in that The allocating the target free memory block to one of the first computing units comprises: Cutting the target free memory block to obtain the memory block applied for by the first computing unit and a second free memory block; The second free memory block is added to the free memory list.
10. The method according to claim 6, It is characterized in that After applying for a second memory block from the operating system according to the memory size required by the first computing unit, the method further includes: Release the second memory block to the memory of the operating system.
11. The method according to claim 1, It is characterized in that At least one of the first computing units includes a second computing unit and a third computing unit, and the third computing unit is a central processing unit CPU; After allocating at least one of the first memory blocks to at least one first computing unit, the method further includes: Determine a first cache area associated with the first memory block, the first cache area being a cache area of the third computing unit, the first cache area being used to store data after the third computing unit processes the data in the first memory block; When the second computing unit needs to obtain data stored in the first memory block, control the third computing unit to write the dirty data in the first cache area into the first memory block, and read the written data in the first memory block through the second computing unit; The first memory block maps dirty data of the first cache area.
12. The method according to claim 11, It is characterized in that After reading the written data in the first memory block by the second computing unit, the method further includes: In the case where the third computing unit needs to obtain data in the first memory block after being processed by the second computing unit, the portion of the first memory block in the third computing unit that maps the first cache area is set to invalid, and the third computing unit is controlled to read data from the first memory block.
13. A memory allocation device, It is characterized in that The device comprises: an acquisition module, a determination module and an allocation module; The acquisition module is used to acquire a first memory parameter corresponding to the first service; The determining module is configured to determine a first memory pool according to the first memory parameter acquired by the acquiring module, wherein the first memory pool includes at least one first memory block; The allocation module is used to allocate at least one of the first memory blocks to at least one first computing unit, and the first computing unit is used to process data of the first business.
14. The device according to claim 13, It is characterized in that The determination module is further configured to determine, before the allocation module allocates at least one of the first memory blocks to at least one first computing unit, whether there is a target free memory block whose memory size is greater than or equal to the memory size required by the first computing unit among the free memory blocks in at least one of the first memory blocks; The allocation module is further configured to allocate the target free memory block to one of the first computing units if there is a target free memory block whose memory size is greater than or equal to the memory size required by the first computing unit in at least one of the free memory blocks in the first memory blocks; The device also includes: an application module; The application module is used to apply for a second memory block from the operating system according to the memory size required by the first computing unit when there is no target free memory block whose size is greater than or equal to the memory size required by the first computing unit in at least one of the free memory blocks in the first memory blocks.
15. The device according to claim 13, It is characterized in that At least one of the first computing units includes a second computing unit and a third computing unit, and the third computing unit is a CPU; The determining module is further configured to determine a first cache area associated with the first memory block after the allocating module allocates at least one of the first memory blocks to at least one first computing unit, the first cache area being a cache area of the third computing unit, the first cache area being configured to store data after the third computing unit processes the data in the first memory block; The device also includes: a control module; The control module is configured to control the third computing unit to write the dirty data in the first cache area into the first memory block, and read the written data in the first memory block through the second computing unit, when the second computing unit needs to obtain the data stored in the first memory block; The first memory block maps dirty data of the first cache area.
16. An electronic device, It is characterized in that It comprises a processor and a memory, wherein the memory stores a program or instruction that can be run on the processor, and when the program or instruction is executed by the processor, the steps of the memory allocation method as described in any one of claims 1 to 12 are implemented.
17. A readable storage medium, It is characterized in that The readable storage medium stores a program or instruction, and when the program or instruction is executed by a processor, the steps of the memory allocation method according to any one of claims 1 to 12 are implemented.
Citation Information
Cited By
Parallel query method and device of relational database and related equipment
CN120407614A
Memory management method for AI accelerator, medium and computer program product
CN121833289A
