Memory exchange method and device
By compressing and organizing the page to be swapped into write units, the number of read and write times of non-volatile storage devices is reduced, the problem of shortening the device life in the prior art is solved, and more efficient memory switching is achieved.
Patent Information
- Application Number
- CN202510052819.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2020-01-20
- Publication Date
- 2025-06-06
AI Technical Summary
In the prior art, the number of read and write times of nonvolatile storage devices is too high, resulting in a shortening of the device life.
By selecting n pages to be swapped out, compress them into n compressed blocks, and cache these compressed blocks to the compressed data cache area. The compressed blocks are then organized into write units and written to the switching area of the nonvolatile storage device in batches, ensuring that at least one writing unit is written to a continuous space of the switching area at a time.
Reduces the number of writes during memory switching and reduces the impact on the life of non-volatile storage devices.
Smart Images

Figure CN120104515A_ABST
Abstract
Description
[0001] This application is a divisional application. The application number of the original application is 202010066109.0, and the original application date is January 20, 2020. The entire contents of the original application are incorporated into this application by reference. Technical Field
[0002] Embodiments of the present application relate to the field of memory management of electronic devices, and more particularly to a method for memory swapping. Background Art
[0003] Memory is one of the important components in a computer. It is a bridge for communication with the central processing unit (CPU). All programs in a computer run in memory, so the performance of memory has a great impact on the computer. Memory is also called internal storage. Its function is to temporarily store the calculation data in the CPU and the data exchanged with external storage such as the hard disk. When the computer is running, the CPU will transfer the data to be calculated to the memory for calculation. When the calculation is completed, the CPU will transmit the result. The operation of the memory also determines the stable operation of the computer. Due to the limitation of memory capacity, a memory recycling mechanism is designed on general computing devices. When the system memory is tight, part of the occupied memory space is recycled to meet new memory allocation requests. The objects of memory recycling are mainly divided into file pages and anonymous pages. Anonymous pages are pages without file backgrounds, such as heaps, stacks, data segments, etc. For modified file pages, these file pages need to be written back to the disk file when the memory is recycled, while the file pages that have not been modified will be released directly.
[0004] For anonymous pages, in the prior art, memory recovery can be performed through a swap mechanism. During the memory recovery process of anonymous pages, the system swaps the anonymous pages in the memory to the swap area or swap file of a non-volatile storage device, thereby recovering the memory space occupied by the anonymous pages, and achieving the effect of expanding the currently available memory space. The principle of the swap mechanism is that when memory resources are tight, the coldest memory page is selected according to the least recently used algorithm (LRU), and the coldest memory page is compressed into a compressed block and cached in the compressed data cache area. When the amount of cached data in the compressed data cache area reaches a threshold, the coldest compressed block is selected according to the LRU algorithm, and the coldest compressed block is decompressed and written to the swap area of the non-volatile storage device. It can be seen that when memory resources are tight, the system will frequently write pages in the memory to the swap area of the non-volatile storage device, and when the process accesses the page written to the swap area of the non-volatile storage device, the page is read into the memory. For embedded electronic devices, such as mobile phones or tablet computers, the non-volatile storage devices they use are usually flash memory devices. The number of read and write times of flash memory devices is limited, and the use of a swap mechanism will affect the life of flash memory devices. Summary of the invention
[0005] The present application provides a method and device for memory exchange, which are applied to the field of memory management of electronic devices to solve the following problems:
[0006] Reduce the number of read and write times of non-volatile storage devices and extend the service life of non-volatile storage devices.
[0007] This application is a memory swap solution for anonymous pages.
[0008] The following first introduces the terms that appear in the embodiments of the present application.
[0009] Running memory: refers to the memory required when the program is running. It can only temporarily store data and is used to exchange cache data with the CPU. It generally refers to random access memory (RAM).
[0010] Compressed data buffer: A memory space used to store pages / compressed blocks swapped out from the running memory.
[0011] Compression page: A memory page used to temporarily store compressed blocks while they are swapped out of the compressed data cache to the swap area of a nonvolatile storage device.
[0012] Swap sub-area: Memory space used to temporarily store compressed blocks when they are swapped out from the compressed data cache to the swap area of the non-volatile storage device. The size of the swap sub-area is an integer number of pages, such as 32K.
[0013] Swap area of non-volatile storage device: space on non-volatile storage devices such as disks used to store swapped data.
[0014] In a first aspect, an embodiment of the present application provides a method for memory swapping, the method comprising: selecting n pages to be swapped out, where n is an integer greater than 0; compressing the n pages to be swapped out into n compressed blocks, and caching the n compressed blocks into a compressed data cache area; organizing at least one of the n compressed blocks into m write units, where m is an integer less than n and greater than 0, and the size of the write unit is an integer number of pages; writing the m write units to a swap area of a non-volatile storage device in a maximum of m times, wherein at least one write unit is stored in a continuous space of the swap area.
[0015] Through this method, when swapping out a page, the page to be swapped out is compressed into a compressed block, reducing the amount of written data in the memory swap process, thereby indirectly reducing the number of writes; further, the compressed blocks are organized into write units and at least one write unit is written to a continuous space in the swap area each time. Compared with the prior art, the write amount of each I / O operation is greatly increased, thereby reducing the number of writes in the memory swap process, thereby reducing the impact of the memory swap process on the life of the non-volatile storage device.
[0016] It is understandable that m may be an integer equal to n. However, this only occurs when the write unit is the size of a page and the corresponding compressed block of each to-be-swapped page occupies a page. The present application combines the compression of the to-be-swapped page and the setting of the write unit size, and can achieve the above effect in most cases.
[0017] It should be noted that the aforementioned write unit refers to a set of compressed blocks of the write unit size. Taking a write unit of 4 pages as an example, the size of a write unit is 32K. During the page swapping process, the present application will store at least one compressed block in units of 32K until all compressed blocks to be swapped out are stored. It is understandable that all compressed blocks to be swapped out may occupy multiple 32K spaces. In addition, the present application will write compressed blocks into the swap area in units of 32K.
[0018] For example, suppose the system needs to swap out 10 compressed blocks, namely compressed block 1, compressed block 2, ... compressed block 10. The total data volume of compressed blocks 1-6 is 32K, and the total data volume of compressed blocks 7-10 is 32K. In the swap-out process, these 10 compressed blocks will be organized into 2 write units. If there is a 64K continuous space on the swap area, these two write units can be written to the swap area at one time, otherwise they will be written to the swap area in 2 times.
[0019] The size of the swap sub-area mentioned below is the same as the size of the write unit.
[0020] The caller mentioned below can be an operating system or an application at the application layer.
[0021] In a possible implementation, when selecting n pages to be swapped out, the caller may indicate a grouping identifier, and then select n pages to be swapped out having the grouping identifier. The grouping identifier includes a process group identifier, a process identifier, an application identifier, or an access time with similar resource usage behaviors. The grouping identifier may also be other identifiers that a person skilled in the art can think of to group the pages to be swapped out, and this application does not impose any special restrictions. n may also be indicated by the caller. The caller may be an operating system or an application at the application layer, and this application does not impose any restrictions. In this application, the caller may decide the timing of calling the method described in this application.
[0022] After determining the group identifier, for example, after determining to select n pages to be swapped out of process A, the n least used pages can be selected according to the least recently used algorithm (LRU). It should be noted that the embodiment of the present application can also select the pages to be swapped out according to other algorithms such as the least frequently used algorithm (LFU), and the present application does not limit this.
[0023] In this way, since the caller knows the hot and cold conditions of the process / application, the caller can select the page of the coldest process / application to swap out according to its own strategy, such as LRU, to avoid the page that has just been swapped out being swapped in again in a short time. At the same time, in this way, when selecting the page to be swapped out, pages with the same grouping identifier can be selected, which paves the way for the subsequent grouping and caching of pages with the same grouping identifier as a whole, reducing the number of reads and writes during the swap process, and improving the swap efficiency.
[0024] It can be understood that the coldest of the aforementioned coldest processes, coldest memory pages or coldest compressed blocks refers to the least used processes, memory pages or compressed blocks selected according to the LRU algorithm.
[0025] In a possible implementation, after selecting a page according to LRU, it is determined whether the page is an anonymous page. If it is an anonymous page, the page is marked as a dirty page and added to a temporary swap cache. Otherwise, other pages are selected until n pages are selected.
[0026] In a possible implementation, the selection of n pages to be swapped out may be performed when memory is insufficient and memory is reclaimed.
[0027] In a possible implementation, the selection of n pages to be swapped out may also be performed periodically by the system, where the period is set by the system.
[0028] In another possible implementation, the caller may also decide the timing of selecting n pages to be swapped out.
[0029] In a possible implementation, a to-be-swapped page corresponds to a compressed block after compression. When the compression rate of the selected to-be-swapped page is lower than a threshold, the original page can be directly saved without compression, and the pages with compression rates lower than the threshold are managed together with the compressed block. The compression rate threshold is set by the system.
[0030] In one possible implementation, caching the n compressed blocks into a compressed data cache area includes dividing the n compressed blocks into at least one group according to a group identifier and caching the at least one group into the compressed data cache area, wherein the compressed blocks contained in each group have the same group identifier, and the group identifier includes a process group identifier, a process identifier, an application identifier or an access time with similar resource usage behavior.
[0031] In a possible implementation, organizing at least one compressed block among the n compressed blocks into m write units includes organizing compressed blocks in the same group into at least one write unit.
[0032] In a possible implementation, before organizing at least one of the n compressed blocks into m write units, the step further includes selecting a portion of the at least one group according to a second indication, wherein the second indication includes the group identifier, and the group identifier includes a process group identifier, a process identifier, an application identifier, or an access time with similar resource usage behavior, and the second indication is an operating system identifier.
[0033] The second indication further includes the number of compressed blocks or the amount of swapped-out data, where the number of compressed blocks or the amount of data to be swapped out in the group indicated by the group identifier indicates the number of compressed blocks or the amount of data to be swapped out.
[0034] In a possible implementation, organizing at least one of the n compressed blocks into m write units includes organizing at least one of the n compressed blocks into m write units in descending order of compressed blocks.
[0035] It can be understood that, due to the reference relationship of the claims, this step includes two possible scenarios, one of which is that at least one compressed block is a compressed block corresponding to a selected group after selecting a group indicated by a group identifier; the other is that at least one compressed block is a compressed block without a group. In both scenarios, the compressed blocks can be organized into write units in descending order.
[0036] Specifically, in the process of caching the n compressed blocks into the compressed data cache area, if the data volume of all compressed blocks in the compressed data cache area is greater than or equal to a threshold, the caller is notified, and the caller determines whether to select the compressed block to be swapped out to the swap area of the non-volatile storage device. If so, the caller indicates the group where the compressed block to be swapped out is located and the number of compressed blocks x through the group identifier. According to the LRU algorithm, the x compressed blocks that have been least used recently in the group indicated by the caller are selected, where x is an integer greater than 0 and less than or equal to n, and the x compressed blocks are first sorted in order from large to small and then organized into at least one write unit.
[0037] It should be noted that the said sorting of x compressed blocks in order from large to small and then organizing them into write units means that the compressed blocks will be sorted in order from large to small before being organized into write units, and then stored according to the space conditions corresponding to the current write units; it does not mean that the storage order of the compressed blocks in the final write unit is sorted from large to small. To give a specific example, assuming that the memory management module specifies to swap out 10 compressed blocks, the present application will sort these 10 compressed blocks from large to small according to the amount of data into compressed block 1, compressed block 2, compressed block 3 until compressed block 10, and then store these 10 compressed blocks. The storage conditions of these 10 compressed blocks may be compressed blocks 1, 2, 3, 6, 4, 5, 7, 10, 8, 9, see the description of Example 1 for details.
[0038] In a possible implementation, during the process of caching the n compressed blocks into the compressed data cache area, if the data volume of all compressed blocks in the compressed data cache area is greater than or equal to a threshold, the caller is notified and the caller determines whether to select the compressed block to be swapped out to the swap area of the non-volatile storage device. If so, the caller indicates the group where the compressed block to be swapped out is located and the size of the swapped out data through a group identifier. According to the LRU algorithm, the least recently used compressed blocks in the group indicated by the caller are selected in sequence from cold to hot, until the total data volume of the selected compressed blocks is less than or equal to and closest to the size of the swapped out data. Assuming that the corresponding number of compressed blocks at this time is x, the x compressed blocks are first sorted in order from large to small and then organized into at least one write unit.
[0039] It should be noted that the said sorting of x compressed blocks in order from large to small and then organizing them into write units means that the compressed blocks will be sorted in order from large to small before being organized into write units, and then stored according to the space conditions corresponding to the current write units; it does not mean that the storage order of the compressed blocks in the final write unit is sorted from large to small. To give a specific example, assuming that the memory management module specifies to swap out 10 compressed blocks, the present application will sort these 10 compressed blocks from large to small according to the amount of data into compressed block 1, compressed block 2, compressed block 3 until compressed block 10, and then store these 10 compressed blocks. The storage conditions of these 10 compressed blocks may be compressed blocks 1, 2, 3, 6, 4, 5, 7, 10, 8, 9, see the description of Example 1 for details.
[0040] In a possible implementation, during the process of caching the n compressed blocks into the compressed data cache area, if the data volume of all compressed blocks in the compressed data cache area is greater than or equal to a threshold, the caller is notified, and the caller determines whether to select the compressed blocks to be swapped out to the swap area of the non-volatile storage device. If so, the caller indicates the group where the compressed blocks to be swapped out are located and the number of compressed blocks through a group identifier. According to the LRU algorithm, the x compressed blocks that have been least recently used in the group indicated by the caller are selected, where x is an integer greater than 0 and less than or equal to n, and x is the number of compressed blocks determined by the caller, and the x compressed blocks are first sorted from cold to hot in the order of least recent use and then organized into at least one write unit.
[0041] In a possible implementation, during the process of caching the n compressed blocks into the compressed data cache area, if the data volume of all compressed blocks in the compressed data cache area is greater than or equal to a threshold, the caller is notified and the caller determines whether to select the compressed block to be swapped out to the non-volatile storage device swap area. If so, the caller indicates the group where the compressed block to be swapped out is located and the swapped out data volume size through a group identifier. According to the LRU algorithm, the least recently used compressed blocks in the group indicated by the caller are selected in sequence from cold to hot, until the total data volume of the selected compressed blocks is less than or equal to and closest to the swapped out data volume size. Assuming that the corresponding number of compressed blocks at this time is x, the x compressed blocks are first sorted from cold to hot in the order of least recent use and then organized into at least one write unit.
[0042] It should be noted that the embodiment of the present application may also select x compression blocks according to other algorithms such as LFU, and the present application does not impose any limitation thereto.
[0043] It should be noted that the aforementioned sorting of the compressed blocks in order from large to small or in order from cold to hot according to the least recently used order is an optional step to improve storage space utilization. In the actual implementation process, the selected compressed blocks can be directly organized into at least one write unit without sorting.
[0044] It should be noted that, for the interface for swapping out compressed blocks to the swap area of a non-volatile storage device provided in the embodiment of the present application, the caller can decide to call it at any time, and the present application does not impose any restrictions. Selecting to swap out compressed blocks to the swap area of a non-volatile storage device upon receiving information that the amount of data of all compressed blocks in the compressed data cache is greater than or equal to a threshold is only one of the scenarios.
[0045] Specifically, the writing unit may be in the form of a temporary page. After the compressed blocks are sorted according to the aforementioned method, the system allocates a temporary page, and then stores the x compressed blocks to be swapped out indicated by the caller one by one in the temporary page. If the temporary page space is insufficient, the system allocates a new temporary page until the x compressed blocks to be swapped out indicated by the caller are all stored in the temporary page. When storing each compressed block, the system will query whether all allocated temporary pages have space to store the current compressed block until a temporary page is found for the current compressed block. Through this step, the space of each temporary page can be fully utilized to improve storage efficiency. When the x compressed blocks to be swapped out indicated by the caller are all stored in the temporary page, the number of temporary pages y is counted, and the y temporary pages are swapped out to the swap area of the non-volatile storage device. For example, if the compressed blocks to be swapped out occupy 8 temporary pages, if the swap area has a 32K continuous space, these 8 pages will be swapped out to the 32K space at one time. If there is no continuous space in the swap area, in the worst case, these 8 temporary pages will be swapped out to the swap area in 8 times. By using this method, the compressed blocks to be swapped out can be stored continuously, and the reading and writing are performed with the writing unit as the minimum unit during the swapping out process, thereby reducing the number of reading and writing times, thereby reducing the impact on the life of the non-volatile storage device.
[0046] In a possible implementation, the write unit may also be in the form of a temporary swap sub-area, the size of which is an integer number of pages, and the size of which is set by the system. After the compressed blocks are sorted according to the aforementioned method, the system allocates a temporary swap sub-area, and then stores the x compressed blocks to be swapped out indicated by the caller one by one into the temporary swap sub-area. If the temporary swap sub-area is insufficient, the system allocates a new temporary swap sub-area until all x compressed blocks are stored. Assuming that the total number of temporary swap sub-areas is m at this time, then these m temporary swap sub-areas correspond to m write units, which will be written to the swap area of the non-volatile storage device at most m times. Taking m=5, the size of the temporary swap sub-area, i.e., the size of the write unit, is 32K as an example, assuming that there is a 96K continuous space in the swap area at this time, and the rest is a 32K continuous space, then these 5 write units will be written into the swap area three times, 3 write units are written once, and 1 write unit is written twice each. It should be noted that, in this implementation, if the amount of data in the compressed block to be swapped out is smaller than the size of a temporary swap sub-area, it can be swapped out first. After the space in a temporary swap sub-area is used up, the compressed block in the temporary swap sub-area can be swapped out to the swap area.
[0047] By using this method, the compressed blocks to be swapped out can be stored continuously, and the reading and writing are performed with the writing unit as the minimum unit during the swapping out process, thereby reducing the number of write times and the impact on the life of the non-volatile storage device.
[0048] In a possible implementation, before writing the m write units to the swap area of the non-volatile storage device at most m times, a mapping table is established, wherein the mapping table contains a correspondence between a page identifier of the at least one compressed block and a swap area address of the at least one compressed block, wherein the page identifier is an identifier of at least one page to be swapped out corresponding to the at least one compressed block, and the swap area address is an address of a section of space belonging to the swap area allocated to the at least one compressed block, and the mapping table is used to obtain the swap area address through the page identifier when a page is swapped in.
[0049] In one possible implementation, after the m write units are written to the swap area of the non-volatile storage device at most m times, the space of the pre-swap cache is released, including the space of all the temporary pages or all the temporary swap sub-areas mentioned above, and the space of the compressed block to be swapped out in the compressed data cache area.
[0050] In a possible implementation, the usage of the swap area of the non-volatile storage device can be maintained through a swap area management structure such as an array, and the address of the compressed block in the swap area of the non-volatile storage device can be described by a triple of <swap sub-area starting address, compressed block offset, compressed block length>, so that addressing of compressed blocks of any length can be achieved.
[0051] Specifically, the swap area of the entire non-volatile storage device is a partition, and the swap area is divided into a series of swap sub-areas. Combined with the above description, it can be seen that the compressed block is organized into at least one write unit when swapped out, and the size of each write unit is the same as the size of the swap sub-area. In this way, one write unit can be written into one swap sub-area. If the compressed block is organized into m write units when swapped out, and there is a continuous space of m write units in the swap area of the non-volatile storage device, under the premise that the size of the m write units is less than the maximum read and write amount of one I / O, then these m write units can be written to the swap area at one time, thereby reducing the number of read and write times and reducing the impact on the life of the non-volatile storage device during the memory swap process. Among them, the maximum read and write amount of one I / O is related to the limitation of the non-volatile storage device.
[0052] In a possible implementation, the entire non-volatile storage device swap area is a file, and the address of a section of space belonging to the swap area allocated to the at least one compressed block includes allocating a continuous space from the file for storing the swapped compressed block.
[0053] In one possible implementation, an embodiment of the present application also includes requesting a page swap-in, including obtaining a swap area address corresponding to the page identifier of the page requested to be swapped-in from the mapping table according to the page identifier of the page requested to be swapped-in; reading a compressed block from the obtained swap area address, and storing the compressed block in the compressed data cache area.
[0054] In one possible implementation, the timing of executing the request page swap is determined by the caller, including when the page to be accessed by the process is not in the memory, triggering a page fault exception.
[0055] Specifically, requesting a page to be swapped in includes obtaining a page identifier of the page requested to be swapped in; determining whether the page requested to be swapped in is in the compressed data cache area; if the page requested to be swapped in is in the compressed data cache area, finding the compressed block corresponding to the page identifier according to the page identifier of the page requested to be swapped in, and decompressing the compressed block corresponding to the page requested to be swapped in to a specified space, which may be allocated by the caller when requesting a page to be swapped in. Releasing the space in the compressed data cache area where the compressed block corresponding to the page requested to be swapped in is located. If the page requested to be swapped in is not in the compressed data cache area, reading the requested compressed block from the swap area of the non-volatile storage device. Among them, for determining whether the page requested to be swapped in is in the compressed data cache area, a possible implementation method is to determine whether the page requested to be swapped in is in the memory through a flag bit in the page identifier; if not in the memory, it may be in the compressed data cache area or in the swap area of the non-volatile storage device; and then determining whether it is in the compressed data cache or in the swap area of the non-volatile storage device through a mapping table.
[0056] In one possible implementation, if the page requested to be swapped in is not in the compressed data cache area, reading the compressed block corresponding to the page requested to be swapped in from the swap area of the non-volatile storage device includes obtaining the swap area address corresponding to the page identifier of the page requested to be swapped in from the mapping table according to the page identifier of the page requested to be swapped in; reading the compressed block from the obtained swap area address, and storing the compressed block in the compressed data cache area.
[0057] In one possible implementation, if the page requested to be swapped in is not in the compressed data cache area, reading the compressed block corresponding to the page requested to be swapped in from the swap area of the non-volatile storage device includes obtaining the swap area address corresponding to the page identifier of the page requested to be swapped in from the mapping table according to the page identifier of the page requested to be swapped in; reading the write unit where the compressed block is located from the obtained swap area address, and storing the write unit in the compressed data cache area.
[0058] In a possible implementation, when reading the write unit where the compressed block is located, the state of the write unit is marked as being swapped in. In this implementation, it is possible to avoid repeated reading of the same write unit / swap sub-area multiple times at the same time, indirectly reducing the number of reads and writes, improving the swap efficiency, and reducing the impact on the life of the non-volatile storage device. It is understandable that when reading the write unit where the compressed block is located, the state of the write unit may not be marked as being swapped in.
[0059] In a possible implementation, the embodiment of the present application also includes deleting the mapping table entry corresponding to the page identifier of the page requested to be swapped in; releasing the space of the compression block or write unit / swap sub-area in the swap area of the non-volatile storage device.
[0060] In this method, the entire exchange sub-area data of the requested compressed block is read from the exchange area of the non-volatile storage device to the compressed data cache area, which can read data in batches, reduce the number of reads, and reduce the impact on the life of the non-volatile storage device.
[0061] In a possible implementation, releasing the space of the compressed block or the swap sub-area in the swap area of the non-volatile storage device may be achieved by setting the corresponding space of the compressed block or the swap sub-area as free in a swap area management structure such as an array.
[0062] In another possible implementation, the timing of requesting a page swap may also be when it is predicted that a page of a specific group is about to be accessed. The prediction may be based on a model trained on user behavior habits or application history usage patterns, such as matching and judging the model based on the time period of application use, geographic location, frequency of application use, and status of the electronic device such as network connection status. The process of requesting a page swap is consistent with the above content.
[0063] It should be noted that whether to perform page swapping and when to perform page swapping can be decided by the caller. Requesting page swapping when the page to be accessed by the process is not in the memory, triggering a page fault exception, or requesting page swapping when it is predicted that pages in a specified group are about to be accessed are only two of the scenarios, and this application does not impose any restrictions.
[0064] In a second aspect, in view of the method provided by the present application, an embodiment of the present application further provides a memory exchange device, the device comprising a module for implementing one or more steps of the method. The module can be implemented by software or a combination of software and hardware.
[0065] In a third aspect, an embodiment of the present application provides a terminal, characterized in that the terminal includes a processor and a memory, the memory is used to store software programs, and the processor is used to read the software programs stored in the memory and execute the method provided by any one of the implementation modes of the first aspect.
[0066] In a fourth aspect, an embodiment of the present application provides a storage medium, characterized in that it includes a computer program, and when the computer program is run on one or more processors, it is used to implement the method provided by any one of the implementation modes of the first aspect.
[0067] In a fifth aspect, an embodiment of the present application provides a computer program product, characterized in that it includes a computer program, and when the computer program is run on one or more processors, it is used to implement the method provided by any one of the implementation modes of the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS
[0068] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0069] Figure 1 A schematic diagram of the structure of a terminal device used in an embodiment of the present application;
[0070] Figure 2 A flowchart of a memory swap method provided in an embodiment of the present application;
[0071] Figure 3 A schematic diagram of a process for selecting a page to be swapped out provided in an embodiment of the present application;
[0072] Figure 4 A schematic diagram of a process for compressing selected pages to be swapped out into compressed blocks and grouping them according to grouping identifiers of the pages provided in an embodiment of the present application;
[0073] Figure 5 A schematic diagram of a process for organizing selected compressed blocks into compressed pages and then swapping them out to a continuous space in a swap area of a non-volatile storage device provided in an embodiment of the present application;
[0074] Figure 6 A schematic diagram of a process for organizing selected compressed blocks into a swap sub-area and then swapping them out to a continuous space in a swap area of a non-volatile storage device provided in an embodiment of the present application;
[0075] Figure 7 A schematic diagram of the page switching process provided in an embodiment of the present application;
[0076] Figure 8 A schematic diagram of the structure of a computer system provided in an embodiment of the present application;
[0077] Fig. 9 A schematic diagram of the structure of a memory exchange device provided in an embodiment of the present application;
[0078] Fig.10a This is a schematic diagram of an example of using the swap mechanism for memory exchange;
[0079] Fig.10b An example schematic diagram of a memory swap method provided in an embodiment of the present application. DETAILED DESCRIPTION
[0080] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0081] In the description of the embodiments of the present application, unless otherwise specified, "multiple" means two or more. The term "and / or" or the character " / " in the present application is only a description of the association relationship of the associated objects, indicating that there can be three relationships, for example, A and / or, or A / B, which can represent: A exists alone, A and B exist at the same time, and B exists alone.
[0082] The memory exchange method provided in the embodiment of the present application is mainly applied to terminal devices using the operating system of the Linux kernel. The terminal device may also be referred to as user equipment (UE), mobile station (MS), mobile terminal (Mobile Terminal), etc. Optionally, the terminal may have the ability to communicate with one or more core networks via a radio access network (RAN). For example, the terminal may be a mobile phone (or a "cellular" phone), or a computer with a mobile nature, etc. For example, the terminal may also be a portable, pocket-sized, handheld, computer-built-in or vehicle-mounted mobile device. It should be understood that in addition to terminal devices, the memory management method provided in the embodiment of the present application may also be applied to other types of computer systems, such as household appliances, servers, etc.
[0083] Please refer to Figure 1 , is a schematic diagram of the structure of the terminal device used in the embodiment of the present application. Figure 1As shown, the terminal device 100 includes a memory 180, a processor 150, and a display device 140. The memory 180 stores computer programs, including an operating system program 182 and an application program 181. The processor 150 is used to read the computer program in the memory 180 and then execute the method defined by the computer program, for example, the processor 150 reads the operating system program 182 to run the operating system on the terminal device 100 and implement various functions of the operating system, or reads one or more application programs 181 to run applications on the terminal device.
[0084] The processor 150 may include one or more processors. For example, the processor 150 may include one or more central processing units, or include a central processing unit and a graphics processing unit. When the processor 150 includes multiple processors, the multiple processors may be integrated on the same chip or may be independent chips. A processor may include one or more processing cores.
[0085] In addition, the memory 180 also stores other data 183 besides computer programs. The other data 183 may include data generated after the operating system 182 or the application 181 is run. The data includes system data (such as configuration parameters of the operating system) and user data. For example, the data cached by the application opened by the user is typical user data.
[0086] The memory 180 generally includes internal memory and external memory. The internal memory may be a random access memory (RAM), a read-only memory (ROM), and a cache (CACHE), etc. The external memory may be a hard disk, an optical disk, a USB disk, a floppy disk, a flash memory, or a tape drive, etc. Computer programs are usually stored in the external memory, and the processor loads the computer program from the external memory to the internal memory before executing the process. In the embodiment of the present application, the non-volatile storage device swap area is the space on the external memory. When a page fault is abnormal, the corresponding page will be swapped from the external memory to the internal memory.
[0087] The operating system program 182 includes a computer program that can implement the memory swap method provided in the embodiment of the present application, so that after the processor 150 reads the operating system program 182 and runs the operating system, the operating system can have the swap-in and / or swap-out functions provided in the embodiment of the present application. Furthermore, the operating system can open a calling interface for the swap-in and / or swap-out function to an upper-level caller, and after the processor 150 reads the application 181 from the memory 180 and runs it, the memory swap can be implemented by calling the interface.
[0088] The terminal device 100 may further include an input device 130 for receiving input digital information, character information or contact touch operation / contactless gesture, and generating signal input related to user settings and function control of the terminal device 100.
[0089] The terminal device 100 may further include a display device 140, which includes a display panel 141 for displaying information input by a user or information provided to a user and various menu interfaces of the terminal device 100. In some other embodiments, the touch panel 131 may cover the display panel 141 to form a touch display screen.
[0090] In addition to the above, the terminal device 100 may also include a power supply 190 for powering other modules and a camera 160 for taking photos or videos. The terminal device 100 may also include one or more sensors 120, such as an acceleration sensor, a light sensor, etc. The terminal device 100 may also include a wireless radio frequency (RF) circuit 110 for network communication with a wireless network device, and may also include a WiFi module 170 for WiFi communication with other devices.
[0091] The following embodiments will introduce the memory swap method provided by the embodiment of the present application, including page swap out and page swap in. The memory swap method provided by the embodiment of the present application can be implemented in Figure 1 In the operating system program 182 shown in the figure. The non-volatile storage device exchange area mentioned in the embodiment of the present application can be a flash memory.
[0092] For page swap out:
[0093] Please refer to Figure 2 , is a flow chart of a memory swap method provided in an embodiment of the present application, the method comprising:
[0094] Step S201, select n pages to be swapped out;
[0095] Step S202: compress the n pages to be swapped out into n compressed blocks, and cache the n compressed blocks in a compressed data cache area;
[0096] Step S203, organizing at least one compressed block among the n compressed blocks into m write units;
[0097] Step S204: writing the m write units into a swap area of a non-volatile storage device at most m times, wherein at least one write unit is stored in a continuous space of the swap area.
[0098] For details, please refer to Fig.10bAn example schematic diagram of a memory swap method provided in an embodiment of the present application:
[0099] After selecting n pages to be swapped out from the memory 1010, each page to be swapped out is compressed into a compressed block, and the compressed blocks are cached in groups 1, 2, ..., g in the compressed data buffer 1011 according to the group identifier, such as the process identifier. Among them, memory pages with the same group identifier will be cached in the same group after compression, such as Fig.10b As shown, compressed blocks with the same group identifier are identified by the same image filling format. The squares in the upper diagonal filling format represent compressed blocks after the memory page with the group identifier corresponding to group 1 is compressed; the squares in the lower diagonal filling format represent compressed blocks after the memory page with the group identifier corresponding to group 2 is compressed; the squares in the vertical line filling format represent compressed blocks after the memory page with the group identifier corresponding to group g is compressed. When the caller calls this method, it indicates to swap out the n compressed blocks corresponding to group 1, then the n compressed blocks are organized into m write units, which are written to the swap area 1021 of the non-volatile storage device 1020 at most m times. The reason for the maximum m times is that there may be multiple continuous spaces of a size of, for example, 5 write units on the swap area 1021, which can write these 5 write units to the swap area 1021 at one time. It should be noted that the upper limit of the amount of data written at one time is the upper limit of the amount of I / O read and write at one time, which is affected by the limitation of the non-volatile storage device.
[0100] For comparison, see Fig.10a , which is an example schematic diagram of memory swapping of the swap mechanism. Take insufficient memory as an example. When the memory is insufficient, the to-be-swapped-out page is swapped out from the memory 1010 according to the LRU algorithm, and the to-be-swapped-out page is compressed into a compressed block and written into the compressed data buffer area 1011, such as Fig.10a , using different pattern filling format blocks to represent the compressed blocks corresponding to different to-be-swapped pages. When the amount of data in the compressed data buffer reaches a threshold, a compressed block is selected according to the LRU algorithm and decompressed and swapped out to the swap area 1021 of the non-volatile storage device 1020 .
[0101] The selection of pages to be swapped out may be performed when memory is insufficient and memory is reclaimed, or it may be performed periodically by the system. It is triggered by the caller.
[0102] Specifically, in this process, the embodiment of the present application will provide two interfaces to the caller, that is, the caller decides whether to execute the page swap to the compressed data cache area or the compressed block swap to the swap area of the non-volatile storage device. The caller can be the memory management module, process management module or application framework of the operating system. In the Linux operating system kernel, the modules involved in this embodiment include mem_cgroup, kswapd, zswap / zram, f2fs / non-volatile storage device driver, etc., as follows:
[0103] In the mem_cgroup module, this embodiment modifies the grouping method of mem_cgroup and implements mem_cgroup grouping by process. It can be determined by configuration whether to group by process or by application, etc.
[0104] In the kswapd module, this embodiment adds a function of swapping part or all pages of the same group, such as the same process;
[0105] In the zswap / zram module, this embodiment adds the function of grouping compressed data by mem_cgroup and swapping out pages in the same group to the swap area of the non-volatile storage device;
[0106] If the swapped data is read and written through the swap file, the swap file read and write and address management module is implemented based on the f2fs file system; if the swapped data is read and written through the non-volatile storage device swap area, the swap area read and write and address management module is implemented based on the existing non-volatile storage device driver module.
[0107] The following will take a non-volatile storage device that is a flash memory as an example to describe in detail the steps of a memory swap method provided in an embodiment of the present application.
[0108] Please refer to Figure 3 , which is a specific flow chart of selecting a specified number of pages to be swapped out with association in step S201 of the memory swap method provided in an embodiment of the present application, including the following process:
[0109] Step S301, receiving an instruction sent by a caller, the instruction including a group identifier and a page number n;
[0110] Step S302: Select pages with the same group identifier according to the least recently used algorithm;
[0111] Step S303: for each selected page, determine whether it is an anonymous page:
[0112] If yes, execute step S304;
[0113] If not, execute step S306;
[0114] Step S304, setting the page as a dirty page;
[0115] Step S305, adding the page to the exchange cache;
[0116] Step S306: determine whether the number of selected pages is less than n:
[0117] If yes, proceed to step S302;
[0118] If not, end this process.
[0119] Specifically, for example, mem_cgroup is configured to group by application. When the activity manager (Activity Manager) in the application framework finds that application A is frozen, it means that the probability of application A being used again in a short period of time is low. Then, the interface provided by the present application is called to notify the embodiment of the present application of the application identifier of application A and the number of pages n that need to be swapped out. The embodiment of the present application selects n anonymous pages of application A according to the LRU algorithm according to the order of pages from cold to hot, marks the n anonymous pages as dirty pages, and adds them to the swap cache.
[0120] For another example, mem_cgroup is configured to group by process. When the memory management module finds that the system memory is insufficient, the interface provided by the present application is called, and the process A and the number of pages n that need to be swapped out are notified to the embodiment of the present application according to the memory recovery strategy determined by the memory management module. The embodiment of the present application selects n anonymous pages of process A according to the order of pages from cold to hot according to the LRU algorithm, marks the n anonymous pages as dirty pages, and adds them to the swap cache.
[0121] Please refer to Figure 4 , a specific flow chart of step S202 of the memory swap method provided in the embodiment of the present application compressing the n pages to be swapped out into n compressed blocks and caching them into the compressed data cache area according to the grouping identifiers of the pages, including the following process:
[0122] Step S401, compressing the selected pages into compressed blocks, and marking the grouping identifiers of the compressed blocks;
[0123] Step S402: Cache the compressed block groups into the compressed data buffer area according to the group identifier;
[0124] Step S403: Determine whether the amount of cached data in the compressed data cache area reaches a threshold:
[0125] If so, notify the caller that the amount of cached data exceeds the threshold;
[0126] If not, end the process.
[0127] In this process, the aforementioned n anonymous pages are compressed one by one and stored in the compressed data cache area according to the group identifier. During the storage process, it is determined whether the amount of cached data in the compressed data cache area has reached a threshold. If it has reached the threshold, the memory management module is notified that the amount of cached data has exceeded the threshold, and the memory management module decides whether to select the compressed block in the compressed data cache area to be swapped out to the non-volatile storage device swap area. If the memory management module determines that the compressed block needs to be swapped out to the non-volatile storage device swap area, the following process corresponding to S203-S204 is executed.
[0128] In steps S203-S204 of the memory exchange method provided in the embodiment of the present application, at least one of the n compressed blocks is organized into m write units, and the m write units are written to the exchange area of the non-volatile storage device at most m times, wherein at least one write unit is stored in a continuous space of the exchange area. The embodiment of the present application will be described from two specific application scenarios.
[0129] Application scenario 1: The size of the write unit is a page.
[0130] Swap out the selected compressed block to the continuous space of the swap area of the non-volatile storage device in the form of compressed pages. The compressed page here refers to the page used to temporarily store the selected compressed block. The size of a compressed page is the size of a memory page, that is, 4K. The selected compressed block may occupy multiple compressed pages, and multiple compressed pages will be swapped out to the continuous space of the swap area of the non-volatile storage device together.
[0131] The following describes in detail, through a specific embodiment, swapping out the selected compressed block in the form of compressed pages to the continuous space of the swap area of the non-volatile storage device.
[0132] Example 1: Please refer to Figure 5 , is a flow chart of swapping out the selected compressed block in the form of compressed pages to the continuous space of the swap area of the non-volatile storage device. In this embodiment, the writing unit is a compressed page. The following process is included:
[0133] Step S501: Select the coldest x compressed blocks of a specified process in the compressed data cache, where x is the number of compressed blocks determined by the caller or the total data volume of the x compressed blocks is less than or equal to and closest to the swap-out data volume determined by the caller;
[0134] Step S502, sort the compressed blocks from large to small;
[0135] Step S503: Allocate a temporary page in the memory, the temporary page is used to store the compressed blocks, so that they can be swapped out in batches to the continuous space of the swap area of the non-volatile storage device later;
[0136] Step S504, taking out a compressed block in order;
[0137] Step S505: Check whether there is space for all allocated temporary pages:
[0138] If not, execute step S506: allocate a new temporary page, and then continue to step S507;
[0139] If yes, execute step S507;
[0140] Step S507, store the compressed block in a temporary page with space, and continue to step S508;
[0141] Step S508: Determine whether there are any unprocessed compressed blocks:
[0142] If yes, return to step S504;
[0143] If not, continue to step S509;
[0144] Step S509, calculate the number of temporary pages, and continue to execute step S510;
[0145] Step S510: Determine whether the existing non-volatile storage device swap sub-area space is sufficient:
[0146] If not, then execute step S511: add a new exchange sub-region for the process, and then continue to step S512;
[0147] If yes, execute step S512;
[0148] Step S512: Create a mapping table entry for the compressed block to be swapped out, and proceed to step S513;
[0149] Step S513, write all temporary pages into the swap area of the non-volatile storage device, and continue to step S514;
[0150] Step S514: Release the temporarily allocated pages and the space of the compressed blocks in the compressed data buffer area.
[0151] This process ends.
[0152] In this process, if the memory management module determines that the compressed block needs to be swapped out to the non-volatile storage device swap area, then after receiving the memory management module swapping out the specified group, such as the x compressed blocks of application A mentioned above, the embodiment of the present application can select the coldest x compressed blocks in the compressed data cache according to the LRU algorithm, sort the compressed blocks in order from large to small according to the amount of data, and then save the compressed blocks in order to the compressed page. The compressed page is a page temporarily applied for storing compressed blocks. The size of a compressed page is the size of a memory page, that is, 4K. The selected compressed block may occupy multiple compressed pages, and multiple compressed pages will be swapped out together to the continuous space of the non-volatile storage device swap area.
[0153] It should be noted that, during the storage process, the embodiment of the present application will poll the allocated compressed pages to see if there is space to store the current compressed block until a compressed page is found for the current compressed block. This step can fully utilize the space of each compressed page and improve storage efficiency. For a specific example, assuming that the memory management module specifies to swap out 10 compressed blocks, these 10 compressed blocks will occupy 3 compressed pages, and these 10 compressed blocks will be sorted from large to small according to the amount of data into compressed block 1, compressed block 2, compressed block 3 until compressed block 10, and then these 10 compressed blocks will be stored in compressed pages in sequence. The temporary storage of these 10 compressed blocks may be as follows:
[0154] Compressed page 1 stores compressed blocks 1, 2, 3, and 6;
[0155] Compressed page 2 stores compressed blocks 4, 5, 7, and 10;
[0156] Compressed page 3 stores compressed blocks 8,9.
[0157] Reason: After compressed page 1 stores compressed blocks 1, 2, and 3, the remaining space is insufficient to store compressed block 4, so the memory management module allocates a new temporary page, namely compressed page 2, for compressed block 4.
[0158] When storing compressed block 5, the memory management module will determine whether there is enough space in the existing compressed page 1. If it is found that the space of compressed page 1 is insufficient, it will continue to determine whether there is enough space in compressed page 2. At this time, the space of compressed page 2 is sufficient, so compressed block 5 is stored in compressed page 2.
[0159] When storing compressed block 6, similarly, it is determined in turn whether there is enough space in the existing compressed block 1. If so, the compressed block 6 is stored in compressed page 1, and the storage of compressed block 7 continues. And so on.
[0160] The embodiment of the present application allocates a swap sub-area to the three compressed pages in the swap area management structure that maintains the usage of the non-volatile storage device, and then writes the three compressed pages into the swap sub-area of the swap area of the non-volatile storage device.
[0161] Application scenario 2: The size of the write unit is the size of a swap sub-area, such as 32K.
[0162] The selected compressed block is swapped out to the continuous space of the swap area of the non-volatile storage device in the form of a swap sub-area. The swap sub-area referred to here refers to a 32K space allocated by the memory management module for temporarily storing the selected compressed block. The selected compressed block may occupy multiple temporary swap sub-areas, and multiple temporary swap sub-areas will be swapped out together to the continuous space of the swap area of the non-volatile storage device.
[0163] The following describes in detail, through a specific embodiment, swapping out the selected compressed block to the continuous space of the swap area of the non-volatile storage device in the form of a temporary swap sub-area of 32K in size.
[0164] Example 2: Please refer to Figure 6 , which is a flow chart of a method of batch swapping selected compressed blocks in the form of temporary swap sub-areas to continuous space in a swap area of a non-volatile storage device provided by an embodiment of the present application, including the following process:
[0165] Step S601: Select the coldest x compressed blocks of a specified process in the compressed data cache, where x is the number of compressed blocks determined by the caller or the total data volume of the x compressed blocks is less than or equal to and closest to the swap-out data volume determined by the caller;
[0166] Step S602: Allocate a temporary switching sub-area for the current group;
[0167] Step S603, taking out a compressed block according to the page LRU order;
[0168] Step S604, determining whether the temporary swap sub-area space in the current memory is sufficient;
[0169] If not, execute step S605: allocate a new temporary swap sub-area for the process, and then proceed to step S606;
[0170] If yes, execute step S606;
[0171] Step S606: store the compressed block in the temporary exchange sub-area, and continue to execute step S607;
[0172] Step S607, determining whether there are any unprocessed compressed blocks;
[0173] If yes, return to step S603;
[0174] If not, execute step S608;
[0175] Step S608: Create a mapping table entry for the compressed block to be swapped out, and proceed to step S609;
[0176] Step S609, write all temporary exchange sub-areas into the exchange area of the non-volatile storage device, and continue to execute step S610;
[0177] Step S610: Release the temporarily allocated swap sub-area and the space of the compressed block in the compressed data cache.
[0178] This process ends.
[0179] In this process, if the memory management module determines that the compressed block needs to be swapped out to the non-volatile storage device exchange area, then after receiving the memory management module swapping out the specified group, such as the x compressed blocks of application A mentioned above, the embodiment of the present application can select the coldest x compressed blocks in the compressed data cache according to the LRU algorithm, apply for a 32K temporary exchange sub-area for the compressed block, select the compressed blocks from cold to hot according to the LRU algorithm and store them in the temporary exchange sub-area, and if the current exchange sub-area space is insufficient, apply for a new temporary exchange sub-area. The selected compressed block may occupy multiple temporary exchange sub-areas, and multiple temporary exchange sub-areas will be swapped out together to the continuous space of the non-volatile storage device exchange area. To take a specific example, suppose that 5 compressed blocks to be swapped out are selected in application A, among which compressed blocks 1, 2, and 3 are stored in temporary swap sub-area 1, and compressed blocks 4 and 5 are stored in temporary swap sub-area 2. When all compressed blocks are stored in the temporary swap sub-area, the memory management module allocates 2 swap sub-areas in the swap area management structure that maintains the usage of the non-volatile storage device, and then writes the contents of temporary swap sub-area 1 and temporary swap sub-area 2 into these two swap sub-areas.
[0180] The above is a detailed description of the steps of a memory swap method for page swapping provided in an embodiment of the present application.
[0181] For page swapping:
[0182] The page swap-in process is executed when a page fault exception is triggered when a process accesses a page that has been swapped out. When a page fault exception occurs, it means that the memory page corresponding to the currently accessed memory address has been swapped out from the memory, such as swapped out from the running memory to the non-volatile storage device swap area, or swapped out from the running memory to the compressed data cache. The operating system kernel will obtain the page table entry from the page table, and determine whether the page requested to be swapped in is in the compressed data cache according to the page identifier. Specifically, it can be determined whether the page identifier is in the mapping table. If the page identifier is not in the mapping table, it means that the page data is not in the non-volatile storage device swap area but in the compressed data cache area, and the existing swap-in process is used; otherwise, the swap area mapping table is searched according to the page identifier to determine the non-volatile storage device swap area address, and then the corresponding page data is read from the non-volatile storage device swap area and decompressed, and the decompressed data is saved in the newly allocated physical page when entering the page fault exception process. This process also includes deleting the mapping table entry corresponding to the page identifier of the page requested to be swapped in; releasing the space of the corresponding page data in the non-volatile storage device swap area.
[0183] It should be noted that when the compressed page or the entire swap sub-area data where the requested compressed block is located is read from the swap area of the non-volatile storage device to the compressed data cache, the status of the compressed page or the swap sub-area will be marked as being swapped in to avoid reading the same swap sub-area multiple times at the same time. When the status of the requested compressed page or swap sub-area is being swapped in, the requested data is waited to be swapped in.
[0184] The following describes in detail the process of requesting a page swap when a page to be accessed by a process is not in the memory and a page fault exception is triggered, through a specific embodiment.
[0185] Embodiment 3: Page swapping process in case of abnormal page fault.
[0186] Please refer to Figure 7 , is a schematic diagram of the page swap process described in this embodiment, including the following processes:
[0187] Step S701, obtaining the page identifier of the page requested to be swapped in;
[0188] Step S702: determine whether the page is in the compressed data cache;
[0189] If not, execute steps S703-S706, and then continue to step S707;
[0190] If yes, execute step S707;
[0191] Step S703, obtaining the swap area address of the non-volatile storage device from the mapping table according to the page identifier;
[0192] Step S704: read the requested compressed page or the entire swap sub-region data into the compressed data cache, and mark the page or sub-region as being swapped in;
[0193] Step S705: Delete the mapping table entry;
[0194] Step S706: Release the space of the compressed page or sub-area in the swap area;
[0195] Step S707, decompress the requested compressed block into the specified space, and continue to step S708;
[0196] Step S708: Release the space of the compressed block in the compressed data cache.
[0197] This process ends.
[0198] In this process, it is taken as an example that pages 1 and 2 of application A need to be swapped in. Combined with the above description, pages 1 and 2 have been swapped out to swap sub-area 1 of the swap area of the non-volatile storage device.
[0199] Specifically, the mapping table is searched according to the page identifiers of page 1 and page 2, and the corresponding table entry in the mapping table obtains the address of the exchange sub-area 1 where the compressed block 1 and compressed block 2 corresponding to page 1 and page 2 are located, and the content of the entire exchange sub-area 1 is read from the space corresponding to the address into the compressed data cache, and the state of the exchange sub-area is marked as being swapped in in the exchange area management structure that maintains the usage of the exchange area of the non-volatile storage device. After the compressed block 1 and the compressed block 2 are swapped into the compressed data cache, the corresponding mapping table entry is deleted, and the space of the exchange sub-area 1 is released, that is, the exchange sub-area 1 is marked as free in the exchange area management structure. Because the address of the compressed block in the exchange area of the non-volatile storage device is recorded by the triple of <exchange sub-area starting address, compressed block offset, compressed block length> when swapping out, when the compressed block is swapped into the compressed data cache, the compressed block 1 and the compressed block 2 can be decompressed into the compressed data cache according to the starting position and the compressed block length, and then swapped from the compressed data cache to the running memory. At this point, the page swap process ends.
[0200] The above is a detailed description of a memory swap method for page swapping provided in an embodiment of the present application.
[0201] In the embodiments of the present application, on the one hand, since page compression is used in the memory exchange process, the read and write volume of the non-volatile storage device is reduced and the read and write efficiency of the exchanged data is improved; on the other hand, since the association of pages is taken into account in the memory exchange process, pages are organized into write units when swapped out through page / compression block grouping, and at least one write unit is written to the continuous space of the swap area of the non-volatile storage device each time, thereby reducing the number of read and write times of the non-volatile storage device and improving the performance of the exchange process.
[0202] Please refer to Table 1, which is a comparison chart of the effects of using the swap mechanism for memory swapping and using the memory swapping provided by the embodiment of the present application for the same terminal and the same group of applications. The scheme comparison is as follows:
[0203] Before optimization: When the page to be swapped out is swapped out from the compressed data cache area to the non-volatile storage device swap area, the memory page is swapped out directly without page compression or consideration of the association between pages. As a result, the amount of exchanged data is large, the number of read and write operations is high, and the data throughput is low. In terminal scenarios, due to the use of flash memory, such frequent and large-scale read and write operations put a lot of pressure on the life of the flash memory.
[0204] After optimization: the pages to be swapped out are compressed, which reduces the amount of data to be swapped and improves data throughput; at the same time, the correlation between pages is taken into consideration, and related pages are stored continuously in the swap area of the non-volatile storage device, reducing the number of read and write times.
[0205] Table 1 compares the effects of using the swap mechanism for page swapping and using the memory swap method provided by the embodiment of the present application in terms of the amount of data swapped out, the amount of data swapped in, and the program startup time. It can be seen that the memory swap technology of the embodiment of the present application can reduce the read and write volume of the non-volatile storage device, reduce the impact of memory swapping on the life of the non-volatile storage device, and can also improve the swap performance.
[0206] Table 1
[0207]
[0208] The above mainly introduces the specific process of the method provided in the embodiment of the present application. Figure 8 Taking the Android operating system as an example, the implementation location and runtime status of the method provided in the embodiments of the present application are introduced. For more specific method processes, please refer to the description in the aforementioned embodiments.
[0209] Please refer to Figure 8 , is a schematic diagram of the structure of a computer system provided in an embodiment of the present application. The computer system 800 may be a terminal device or other types of computer devices.
[0210] The computer system includes an application layer 810 and an operating system layer 860, and the operating system may be an Android operating system. The operating system layer 860 is further divided into an application framework layer 820, a system runtime layer 830, and a Linux operating system kernel layer 840. Figure 8 The operating system layer 860 in the Figure 1 A specific implementation of the operating system 182 in Figure 8The application layer 810 in Figure 1 The Linux operating system kernel layer 840 provides the core and management of the operating system, including memory management 841, non-volatile storage device driver 842, file system 843, etc. The system runtime layer 830 includes interface management 831, multimedia library 832, etc. The application framework layer 820 may include an activity manager (Activity Manager), a package manager (Package Manager), etc. The application layer 810 may include application 1, application 2, etc.
[0211] In addition, the computer system 800 also includes a hardware layer 850. The hardware layer of the computer system 800 may include a memory 851 and a non-volatile storage device 852 (equivalent to Figure 1 Memory 180 in), etc.
[0212] The method provided in any of the above embodiments of the present application can be implemented in Figure 8 In the operating system layer 860 shown.
[0213] It can be seen from the above-introduced devices and methods that a computer system using the memory swap method provided in the embodiments of the present application can perform group swapping in and / or out after compressing memory pages, thereby reducing the read and write volume of non-volatile storage devices and improving the performance of the swap process. At the same time, it can also reduce the number of read and write times, thereby reducing the impact on the life of non-volatile storage devices during the memory swap process.
[0214] See also Fig. 9 As shown, a memory exchange device 900 provided in an embodiment of the present application is located in the operating system layer 860, and the device 900 includes:
[0215] A selection module 910 is used to select n pages to be swapped out;
[0216] A compression module 920, used for compressing the n pages to be swapped out into n compressed blocks, and caching the n compressed blocks into a compressed data cache area;
[0217] an organization module 930, configured to organize at least one of the n compressed blocks into m write units, where m is an integer less than or equal to n and greater than 0, and the size of the write unit is an integer number of pages;
[0218] A swap module 940, configured to write the m write units into a swap area of a non-volatile storage device at most m times, wherein at least one write unit is stored in a continuous space of the swap area;
[0219] The page swap module 950 is used to obtain the swap area address corresponding to the page identifier of the page requested to be swapped in from the mapping table according to the page identifier of the page requested to be swapped in; read the compressed block from the obtained swap area address, and store the compressed block in the compressed data cache area.
[0220] The specific implementation of the solutions involved in the device embodiment can be referred to the aforementioned method embodiment, which will not be repeated here.
[0221] The device provided in the embodiment of the present application can execute the swap-out and swap-in processing of memory pages simultaneously, or can execute the swap-out processing of memory pages and the swap-in processing of memory pages separately.
[0222] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated. Some or all of the modules may be selected according to actual needs to achieve the purpose of the present embodiment. In addition, in the device embodiment drawings provided by the present application, the modules may be connected to each other by one or more communication buses or signal lines. Those of ordinary skill in the art may understand and implement the invention without creative work.
[0223] The above descriptions are only some specific implementations of the present application, but the protection scope of the present application is not limited thereto.
Claims
1. A method of memory swapping, It is characterized in that include: Select n pages to be swapped out, where n is an integer greater than 0; Compressing the n pages to be swapped out into n compressed blocks, and caching the n compressed blocks into a compressed data buffer area; Organizing at least one of the n compressed blocks into m write units, where m is an integer less than n and greater than 0, and the size of the write unit is an integer number of pages; Writing the m write units into a swap area of a non-volatile storage device at most m times, wherein at least one write unit is stored in a continuous space of the swap area; The method further includes requesting a page swap in when a page fault exception occurs, wherein the page swap in includes: If the compressed block corresponding to the page requested to be swapped in is not in the compressed data cache area, part or all of the data in the swap sub-area where the compressed block corresponding to the page requested to be swapped in is located is read into the compressed data cache area, and the compressed block corresponding to the page requested to be swapped in is decompressed into the specified space.
2. The method according to claim 1, It is characterized in that The selecting n pages to be swapped out includes: receiving a first indication, wherein the first indication includes a group identifier, the group identifier includes a process group identifier, a process identifier, an application identifier, or an access time of a process with similar resource usage behavior, and the first indication is sent by an operating system or an application at an application layer; Select n pages to be swapped out that have the grouping identifier.
3. The method according to claim 1 or 2, It is characterized in that The caching the n compressed blocks into the compressed data buffer area comprises: Dividing the n compressed blocks into at least one group according to a group identifier and caching the at least one group into the compressed data cache area, wherein the compressed blocks contained in each group have the same group identifier, and the group identifier includes a process group identifier, a process identifier, an application identifier or an access time with similar resource usage behaviors; The organizing at least one of the n compressed blocks into m write units comprises: Organize compressed blocks in the same group into at least one write unit.
4. The method according to claim 3, It is characterized in that Before organizing at least one of the n compressed blocks into m write units, the method further includes: Select some groups from the at least one group according to a second indication, wherein the second indication includes the group identifier, and the group identifier includes a process group identifier, a process identifier, an application identifier or an access time with similar resource usage behaviors, and the second indication is sent by an operating system or an application at the application layer.
5. The method according to claim 4, It is characterized in that The second indication further includes the number of compressed blocks or the amount of swapped-out data, where the number of compressed blocks or the amount of data to be swapped out in the group indicated by the group identifier indicates the number of compressed blocks or the amount of data to be swapped out.
6. The method according to any one of claims 1 to 5, It is characterized in that The organizing at least one of the n compressed blocks into m write units comprises: At least one compressed block among the n compressed blocks is organized into m write units in descending order of compressed blocks.
7. The method according to any one of claims 1 to 5, It is characterized in that The organizing at least one of the n compressed blocks into m write units comprises: At least one of the n compressed blocks is organized into m write units in order from cold to hot after the compressed blocks are sorted by least recent use.
8. The method according to any one of claims 1 to 7, It is characterized in that Before writing the m write units into the exchange area of the non-volatile storage device at most m times, the method includes: A mapping table is established, wherein the mapping table includes a correspondence between a page identifier of the at least one compressed block and a swap area address of the at least one compressed block, wherein the page identifier is an identifier of at least one to-be-swapped-out page corresponding to the at least one compressed block, and the swap area address is an address of a section of space belonging to the swap area allocated to the at least one compressed block, and the mapping table is used to obtain the swap area address through the page identifier when a page is swapped in.
9. The method according to claim 8, It is characterized in that Also includes: According to the page identifier of the page requested to be swapped in, acquiring from the mapping table the swap area address corresponding to the page identifier of the page requested to be swapped in; A compressed block is read from the obtained exchange area address, and the compressed block is stored in the compressed data buffer area.
10. The method according to claim 8, It is characterized in that Also includes: According to the page identifier of the page requested to be swapped in, acquiring from the mapping table the swap area address corresponding to the page identifier of the page requested to be swapped in; The write unit where the compressed block is located is read from the acquired exchange area address, and the write unit is stored in the compressed data buffer area.
11. The method according to claim 10, It is characterized in that Also includes: The state of the write unit is marked as being swapped in.
12. A memory exchange device, It is characterized in that Includes selection module, compression module, organization module, exchange module and page swap module: The selection module is used to select n pages to be swapped out, where n is an integer greater than 0; The compression module is used to compress the n pages to be swapped out into n compressed blocks, and cache the n compressed blocks in the compressed data cache area; The organization module is used to organize at least one compressed block among the n compressed blocks into m write units, where m is an integer less than or equal to n and greater than 0, and the size of the write unit is an integer number of pages; The exchange module is used to write the m write units into the exchange area of the non-volatile storage device at most m times, wherein at least one write unit is stored in a continuous space of the exchange area; The page swap module is used to request page swap in the case of a page missing exception. The page swap includes: If the compressed block corresponding to the page requested to be swapped in is not in the compressed data cache area, part or all of the data in the swap sub-area where the compressed block corresponding to the page requested to be swapped in is located is read into the compressed data cache area, and the compressed block corresponding to the page requested to be swapped in is decompressed into the specified space.
13. The device according to claim 12, It is characterized in that The selection module is specifically used for: receiving a first indication, wherein the first indication includes a group identifier, the group identifier includes a process group identifier, a process identifier, an application identifier, or an access time of a process with similar resource usage behavior, and the first indication is sent by an operating system or an application at an application layer; Select n pages to be swapped out that have the grouping identifier.
14. The device according to claim 12 or 13, Features: The compression module is specifically used for: Dividing the n compressed blocks into at least one group according to a group identifier and caching the at least one group into the compressed data cache area, wherein the compressed blocks contained in each group have the same group identifier, and the group identifier includes a process group identifier, a process identifier, an application identifier or an access time with similar resource usage behaviors; The organization module is specifically used for: Organize compressed blocks in the same group into at least one write unit.
15. The device according to claim 14, It is characterized in that The organization module is specifically used for: Select some groups from the at least one group according to a second indication, wherein the second indication includes the group identifier, and the group identifier includes a process group identifier, a process identifier, an application identifier or an access time with similar resource usage behaviors, and the second indication is sent by an operating system or an application at the application layer.
16. The device according to claim 15, It is characterized in that The second indication further includes the number of compressed blocks or the amount of swapped-out data, where the number of compressed blocks or the amount of data to be swapped out in the group indicated by the group identifier indicates the number of compressed blocks or the amount of data to be swapped out.
17. The device according to any one of claims 12 to 16, It is characterized in that The organization module is specifically used for: At least one compressed block among the n compressed blocks is organized into m write units in descending order of compressed blocks.
18. The device according to any one of claims 12 to 16, It is characterized in that The organization module is specifically used for: At least one of the n compressed blocks is organized into m write units in order from cold to hot after the compressed blocks are sorted by least recent use.
19. The device according to any one of claims 12 to 18, It is characterized in that The switching module is specifically used for: A mapping table is established, wherein the mapping table includes a correspondence between a page identifier of the at least one compressed block and a swap area address of the at least one compressed block, wherein the page identifier is an identifier of at least one to-be-swapped-out page corresponding to the at least one compressed block, and the swap area address is an address of a section of space belonging to the swap area allocated to the at least one compressed block, and the mapping table is used to obtain the swap area address through the page identifier when a page is swapped in.
20. The device according to claim 19, It is characterized in that Also includes page swap module: The page swap module is used for: According to the page identifier of the page requested to be swapped in, acquiring from the mapping table the swap area address corresponding to the page identifier of the page requested to be swapped in; A compressed block is read from the obtained exchange area address, and the compressed block is stored in the compressed data buffer area.
21. The device according to claim 19, It is characterized in that Also includes page swap module: The page swap module is used for: According to the page identifier of the page requested to be swapped in, acquiring from the mapping table the swap area address corresponding to the page identifier of the page requested to be swapped in; The write unit where the compressed block is located is read from the acquired exchange area address, and the write unit is stored in the compressed data buffer area.
22. The device according to claim 21, It is characterized in that The page swap module is specifically used for: The state of the write unit is marked as being swapped in.
23. A terminal, It is characterized in that The terminal comprises a processor and a memory, wherein the memory is used to store a software program, and the processor is used to read the software program stored in the memory and execute the method of any one of claims 1 to 11.
24. A storage medium, It is characterized in that The invention comprises a computer program for implementing the method of any one of claims 1 to 11 when the computer program is run on one or more processors.
25. A computer program product, It is characterized in that The invention comprises a computer program for implementing the method of any one of claims 1 to 11 when the computer program is run on one or more processors.