WO2021147330A1 - 内存交换的方法、装置 - Google Patents
内存交换的方法、装置 Download PDFInfo
- Publication number
- WO2021147330A1 WO2021147330A1 PCT/CN2020/113676 CN2020113676W WO2021147330A1 WO 2021147330 A1 WO2021147330 A1 WO 2021147330A1 CN 2020113676 W CN2020113676 W CN 2020113676W WO 2021147330 A1 WO2021147330 A1 WO 2021147330A1
- Authority
- WO
- WIPO (PCT)
- Prior art keywords
- compressed
- page
- swap
- identifier
- swapped
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Ceased
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F12/00—Accessing, addressing or allocating within memory systems or architectures
- G06F12/02—Addressing or allocation; Relocation
- G06F12/0223—User address space allocation, e.g. contiguous or non contiguous base addressing
- G06F12/023—Free address space management
- G06F12/0238—Memory management in non-volatile memory, e.g. resistive RAM or ferroelectric memory
- G06F12/0246—Memory management in non-volatile memory, e.g. resistive RAM or ferroelectric memory in block erasable memory, e.g. flash memory
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F12/00—Accessing, addressing or allocating within memory systems or architectures
- G06F12/02—Addressing or allocation; Relocation
- G06F12/06—Addressing a physical block of locations, e.g. base addressing, module addressing, memory dedication
- G06F12/0646—Configuration or reconfiguration
- G06F12/0653—Configuration or reconfiguration with centralised address assignment
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F3/00—Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
- G06F3/06—Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
- G06F3/0601—Interfaces specially adapted for storage systems
- G06F3/0628—Interfaces specially adapted for storage systems making use of a particular technique
- G06F3/0655—Vertical data movement, i.e. input-output transfer; data movement between one or more hosts and one or more storage devices
- G06F3/0656—Data buffering arrangements
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F3/00—Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
- G06F3/06—Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
- G06F3/0601—Interfaces specially adapted for storage systems
- G06F3/0602—Interfaces specially adapted for storage systems specifically adapted to achieve a particular effect
- G06F3/0608—Saving storage space on storage systems
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F3/00—Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
- G06F3/06—Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
- G06F3/0601—Interfaces specially adapted for storage systems
- G06F3/0628—Interfaces specially adapted for storage systems making use of a particular technique
- G06F3/0638—Organizing or formatting or addressing of data
- G06F3/064—Management of blocks
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F3/00—Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
- G06F3/06—Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
- G06F3/0601—Interfaces specially adapted for storage systems
- G06F3/0628—Interfaces specially adapted for storage systems making use of a particular technique
- G06F3/0655—Vertical data movement, i.e. input-output transfer; data movement between one or more hosts and one or more storage devices
- G06F3/0659—Command handling arrangements, e.g. command buffers, queues, command scheduling
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F3/00—Input arrangements for transferring data to be processed into a form capable of being handled by the computer; Output arrangements for transferring data from processing unit to output unit, e.g. interface arrangements
- G06F3/06—Digital input from, or digital output to, record carriers, e.g. RAID, emulated record carriers or networked record carriers
- G06F3/0601—Interfaces specially adapted for storage systems
- G06F3/0668—Interfaces specially adapted for storage systems adopting a particular infrastructure
- G06F3/0671—In-line storage system
- G06F3/0673—Single storage device
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F2212/00—Indexing scheme relating to accessing, addressing or allocation within memory systems or architectures
- G06F2212/40—Specific encoding of data in memory or cache
- G06F2212/401—Compressed data
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F2212/00—Indexing scheme relating to accessing, addressing or allocation within memory systems or architectures
- G06F2212/72—Details relating to flash memory management
- G06F2212/7203—Temporary buffering, e.g. using volatile buffer or dedicated buffer blocks
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F2212/00—Indexing scheme relating to accessing, addressing or allocation within memory systems or architectures
- G06F2212/72—Details relating to flash memory management
- G06F2212/7208—Multiple device management, e.g. distributing data over multiple flash devices
Definitions
- the embodiments of the present application relate to the field of memory management of electronic devices, and particularly relate to methods of memory swapping.
- Memory is one of the important components in a computer, and it is a bridge to communicate with the central processing unit (CPU). All programs in the computer are run in the memory, so the performance of the memory has a great impact on the computer. Memory is also called internal memory, and its function is to temporarily store calculation data in the CPU and data exchanged with external storage such as hard disks. When the computer is running, the CPU will transfer the data that needs to be calculated into 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 recovery mechanism is designed on general computing devices. When the system memory is tight, part of the occupied memory space is recovered to meet new memory allocation requests.
- the objects of memory reclamation are mainly divided into file pages and anonymous pages.
- Anonymous pages are pages without file background, such as heap, stack, data segment, etc.
- file background such as heap, stack, data segment, etc.
- this part of the file pages needs to be written back to the disk file during memory recovery, and the unmodified file pages will be released directly.
- memory can be reclaimed through a swap mechanism.
- the system swaps anonymous pages in the memory to the swap area or swap file of the non-volatile storage device, thereby reclaiming the memory space occupied by this part of the anonymous page, realizing the expansion of the currently available The effect of 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 (LRU) algorithm, and the coldest memory page is compressed into compressed blocks and cached in the compressed data buffer area.
- LRU Least Recently Used
- the coldest compressed block is selected according to the LRU algorithm, and the coldest compressed block is decompressed and written into the swap area of the nonvolatile 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. When the process accesses the pages written in the swap area of the non-volatile storage device, it will The page is read into memory.
- the non-volatile storage devices used are usually flash storage devices. Flash storage devices have limited read and write times. The use of swap mechanism will affect flash. The lifespan of the storage device.
- the embodiments of the present application provide a memory swap method and device, which are applied to the field of memory management of electronic equipment, and solve the following problems:
- This application is a memory swap solution for anonymous pages.
- Running memory refers to the memory needed when the program is running. It can only temporarily store data for exchanging cache data with the CPU. Generally, it refers to random access memory (RAM).
- RAM random access memory
- Compressed data buffer area a memory space used to store pages/compressed blocks swapped out from running memory.
- Compressed page A memory page used to temporarily store compressed blocks when the compressed blocks are swapped out from the compressed data buffer area to the swap area of the non-volatile storage device.
- Swap sub-area the memory space used to temporarily store compressed blocks during the process of swapping out compressed blocks from the compressed data buffer area 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.
- Swap area of non-volatile storage device The space used to store swapped-out data on non-volatile storage devices such as disks.
- an embodiment of the present application provides a memory swap method, the method includes: 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 Compress blocks, and cache the n compressed blocks in a compressed data buffer area; organize at least one compressed block of the n compressed blocks into m write units, where m is an integer less than n and greater than 0 , The size of the write unit is an integer number of pages; the m write units are written to the swap area of the nonvolatile storage device at most m times, and at least one write unit is stored in the swap area A continuous space.
- the pages to be swapped out are compressed into compressed blocks, which reduces the amount of data written in the memory swap process, thereby indirectly reducing the number of writes; further, the compressed blocks are organized into write units In addition, at least one writing unit is written into a continuous space of the swap area each time.
- the amount of writing for each I/O operation is greatly increased, thereby reducing the number of writes in the memory swap process. Thereby reducing the impact on the life of the non-volatile storage device during the memory exchange process.
- n may be an integer equal to n.
- This application combines the compression of the page to be swapped out and the setting of the write unit size, and the above effect can be achieved in most cases.
- the aforementioned writing unit refers to a set of compressed blocks of the size of the writing unit. Taking a write unit of 4 pages as an example, the size of a write unit is 32K.
- this application will store at least one compressed block in a unit of 32K until all the compressed blocks to be swapped out are stored. It is understandable that all compressed blocks to be swapped out may occupy multiple 32K spaces.
- this application will write the compressed block into the swap area with 32K as the minimum unit.
- the system wants 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
- the total data volume of compressed blocks 7-10 is 32K.
- these 10 compressed blocks will be organized into 2 write units. If there is a 64K continuous space on the area, these two writing units can be written to the swap area at one time, otherwise they are written to the swap area twice.
- the size of the swap sub-area mentioned below is the same as the size of the write unit.
- the caller mentioned below can be an operating system or an application layer application.
- the caller may indicate a group identifier, and then select n pages to be swapped out with the group identifier.
- the grouping identifier includes process group identifiers, process identifiers, application identifiers, or access time that have similar resource usage behaviors.
- the grouping identifiers can also be other identifiers that can be thought of by those skilled in the art to group the pages to be swapped out. This application does not do it. Special restrictions.
- n can also be indicated by the caller.
- the caller can be an operating system or an application at the application layer, which is not limited in this application. In this application, the caller can decide when to call the method described in this application.
- the n least used pages can be selected according to the Least Recently Used (LRU) algorithm. It should be noted that the embodiment of the present application may also select the page to be swapped out according to other algorithms, such as the Least Frequently Used (LFU) algorithm, which is not limited in this application.
- LRU Least Recently Used
- the caller can select the coldest process/application page to swap out according to its own strategy, such as LRU, to avoid the page that has just been swapped out in a short time Was swapped in.
- LRU its own strategy
- the coldest process, the coldest memory page or the coldest compressed block mentioned above means the least used process, memory page or compressed block selected according to the LRU algorithm.
- selecting n pages to be swapped out can be executed when memory is insufficient for memory reclamation.
- the selection of n pages to be swapped out may also be executed periodically by the system, and the period is set by the system.
- selecting n pages to be swapped out can also be executed by the caller.
- a page to be swapped out corresponds to a compressed block after compression.
- the compression rate of the selected page to be swapped out is lower than the threshold, the original page can be directly saved without compression, and the pages with the compression rate lower than the threshold and the compressed block can be managed uniformly.
- the compression rate threshold is set by the system.
- buffering the n compressed blocks in a compressed data buffer area includes dividing the n compressed blocks into at least one group according to a group identifier and buffering the at least one group in the compressed data
- organizing at least one compressed block in the n compressed blocks into m writing units includes organizing the compressed blocks in the same group into at least one writing unit.
- the method before the organizing at least one compressed block of the n compressed blocks into m writing units, the method further includes selecting a partial group of the at least one group according to a second instruction, and
- the second instruction includes the group identifier, the group identifier includes process group identifiers, process identifiers, application identifiers, or access time with similar resource usage behaviors, and the second instruction is sent by the operating system or sent by an application at the application layer .
- the second indication further includes the number of compressed blocks or the amount of swapped out data, and the compressed amount or the amount of swapped out data indicates the number of compressed blocks to be swapped out or the amount of data in the group indicated by the group identifier.
- organizing at least one compressed block in the n compressed blocks into m write units includes arranging at least one compressed block in the n compressed blocks in descending order of the compressed blocks Organized into m writing units.
- this step includes two possible scenarios.
- One scenario is that at least one compressed block is selected after a group indicated by the group identifier, and the selected group corresponds to the compressed block ;
- Another scenario is that at least one compressed block is a compressed block without grouping. In both scenarios, compressed blocks can be organized into write units in order from large to small.
- the caller determines whether to select compression The block is swapped out to the swap area of the nonvolatile storage device. If yes, the caller indicates the group in which the compressed block is to be swapped out and the number of compressed blocks x through the group identifier. According to the LRU algorithm, select the least recently used x compressed blocks in the group indicated by the caller, where x is an integer greater than 0 and less than or equal to n, and the x compressed blocks are sorted in descending order and then organized into at least One write unit.
- the said that the x compressed blocks are sorted in descending order and then organized into writing units means that the compressed blocks will be sorted in descending order before being organized into writing units, and then The storage is performed according to the space corresponding to the current write unit; it does not mean that the storage order of the compressed blocks in the final write unit is sorted from largest to smallest.
- this application will sort these 10 compressed blocks into compressed block 1, compressed block 2, compressed block 3 until compressed according to the amount of data from large to small Block 10, and then store these 10 compressed blocks, then the storage situation of these 10 compressed blocks may be compressed blocks 1, 2, 3, 6, 4, 5, 7, 10, 8, 9, see the implementation for details Example 1 description.
- the caller in the process of caching the n compressed blocks in the compressed data buffer area, if the data volume of all compressed blocks in the compressed data buffer area is greater than or equal to a threshold, the caller is notified that 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 will indicate the group in which the compressed block to be swapped out and the size of the swapped out data through the group identifier.
- the least recently used compressed block in the group indicated by the caller is selected from cold to hot, until the total data volume of the selected compressed block is less than or equal to and the closest to the swapped out data size, assuming that The corresponding number of compressed blocks is x, and the x compressed blocks are sorted in descending order and then organized into at least one writing unit.
- the said that the x compressed blocks are sorted in descending order and then organized into writing units means that the compressed blocks will be sorted in descending order before being organized into writing units, and then The storage is performed according to the space corresponding to the current write unit; it does not mean that the storage order of the compressed blocks in the final write unit is sorted from largest to smallest.
- this application will sort these 10 compressed blocks into compressed block 1, compressed block 2, compressed block 3 until compressed according to the amount of data from large to small Block 10, and then store these 10 compressed blocks, then the storage situation of these 10 compressed blocks may be compressed blocks 1, 2, 3, 6, 4, 5, 7, 10, 8, 9, see the implementation for details Example 1 description.
- the caller in the process of caching the n compressed blocks in the compressed data buffer area, if the data volume of all compressed blocks in the compressed data buffer area is greater than or equal to a threshold, the caller is notified that The caller determines whether to select the compressed block to be swapped out to the swap area of the non-volatile storage device. If it is, the caller indicates the group in which the compressed block to be swapped out and the number of compressed blocks are located through the group identifier. According to the LRU algorithm, select the least recently used x compressed blocks in the group indicated by the caller, where x is an integer greater than 0 and less than or equal to n, and the x is the number of compressed blocks determined by the caller. Sort them from cold to hot in the order of least recently used and then organize them into at least one write unit.
- the caller in the process of caching the n compressed blocks in the compressed data buffer area, if the data volume of all compressed blocks in the compressed data buffer area is greater than or equal to a threshold, the caller is notified that 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 will indicate the group in which the compressed block to be swapped out and the size of the swapped out data through the group identifier.
- the least recently used compressed block in the group indicated by the caller is selected from cold to hot, until the total data volume of the selected compressed block is less than or equal to and the closest to the swapped out data size, assuming that The corresponding number of compressed blocks is x, and the x compressed blocks are sorted from cold to hot in the order of least recently used, and then organized into at least one write unit.
- embodiment of the present application may also select x compressed blocks according to other algorithms, such as LFU, which is not limited in the present application.
- Sorting directly organizes the selected compressed blocks into at least one write unit.
- the interface for swapping out compressed blocks to the swap area of the non-volatile storage device can be called at any time at the discretion of the caller, which is not limited by this application. Selecting the compressed block to be swapped out to the swap area of the non-volatile storage device when receiving the information that the data volume of all compressed blocks in the compressed data buffer area is greater than or equal to the threshold is just one of the scenarios.
- the format of the writing unit may be a temporary page.
- the system allocates a temporary page, and then stores the x compressed blocks to be swapped out as indicated by the caller into the temporary page one by one. If the temporary page space is insufficient, the system allocates another one The new temporary page is stored in the temporary page until the x compressed blocks to be swapped out indicated by the caller.
- 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. .
- the number of temporary pages y is counted, and the y temporary pages are swapped out to the swap area of the nonvolatile storage device.
- the compressed block to be swapped out occupies 8 temporary pages as an example, if the swap area has a continuous 32K space, the 8 pages will be swapped out into the 32K space at once. 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.
- the compressed block to be swapped out can be continuously stored, and the write unit is used as the smallest unit to perform read and write during the swap out process, which reduces the number of times of reading and writing, thereby reducing the impact on the life of the nonvolatile storage device.
- the form of the writing unit may also be a temporary swap subarea
- the size of the temporary swap subarea is an integer number of pages
- the size of the temporary swap subarea is set by the system.
- these m temporary swap sub-areas correspond to m writing units, and write to the swap area of the nonvolatile storage device at most m times.
- the size of the temporary swap sub-area that is, the size of the write unit is 32K as an example.
- the 5 write units will be divided into To write to the swap area 3 times, 3 write units are written once, and 1 write unit is written respectively in the other two times.
- the data volume of the compressed block to be swapped out is less than the size of a temporary swap sub-area, it may not be swapped out first, and wait until the space of a temporary swap sub-area is used up. , And then swap out the compressed block in the temporary swap sub-area to the swap area.
- the compressed block to be swapped out can be continuously stored, and the write unit is used as the smallest unit to read and write during the swap out process, which reduces the number of writes and reduces the impact on the life of the nonvolatile storage device.
- the method before the writing the m writing units into the swap area of the nonvolatile storage device at most m times, the method includes establishing a mapping table, and the mapping table includes the at least one The corresponding relationship between the page identifier of the compressed block and the 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.
- the space of the pre-swap cache is released, including all the aforementioned temporary pages Or the space of all temporary exchange sub-regions, and the space of the compressed block to be swapped out in the compressed data buffer area.
- the use of the swap area of the non-volatile storage device can be maintained through the 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 set by ⁇ swap subarea Start address, compressed block offset, compressed block length> triples to describe, which can realize the addressing of compressed blocks of any length.
- the swap area of the entire nonvolatile storage device is a partition, and the swap area is divided into a series of swap sub-areas.
- the compressed block is organized into at least one write unit when swapped out, and each write The size of the unit is the same as the size of the exchange sub-region. In this way, one writing unit can be written into one swap sub-area.
- the size of the m write units is less than Under the premise of the maximum read and write volume of one I/O, these m write units can be written to the swap area at one time, thereby reducing the number of reads and writes and reducing the impact on the life of the nonvolatile storage device during the memory swap process .
- the maximum amount of I/O read and write at a time is related to the limitation of non-volatile storage devices.
- the entire swap area of the nonvolatile storage device is a file
- 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. To store the compressed block swapped out.
- the embodiment of the present application further includes requesting page swap, including obtaining 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.
- Corresponding swap area address read the compressed block from the acquired swap area address, and store the compressed block in the compressed data buffer area.
- the execution timing of the requested page swap is determined by the caller, including when the page to be accessed by the process is not in the memory to trigger a page fault exception.
- requesting page swapping includes obtaining the page identifier of the page requested to be swapped in; judging whether the page requested to be swapped in is in the compressed data buffer area, and if the page requested to be swapped in is in the compressed data buffer area, then Find the compressed block corresponding to the page identifier according to the page identifier of the page requested to be swapped in, and decompress the compressed block corresponding to the page requested to be swapped in to a designated space.
- the designated space may be changed when the page is requested to be swapped in. Assigned by the caller. Release the space of the compressed data buffer area where the compressed block corresponding to the page requested to be swapped in is located.
- the requested compressed block is read from the swap area of the non-volatile storage device.
- one possible implementation is to determine whether the page requested to be swapped in is in the memory through the flag bit in the page identifier, if not in the memory, It may be in the compressed data buffer area or in the swap area of the non-volatile storage device; and then judge whether it is in the compressed data buffer or in the swap area of the non-volatile storage device through the mapping table.
- reading the compressed block corresponding to the page requested to be swapped in from the swap area of the non-volatile storage device includes changing according to the request
- the page identifier of the incoming page obtains the swap area address corresponding to the page identifier of the page requested to be swapped in from the mapping table; reads the compressed block from the obtained swap area address, and compresses the The block is stored in the compressed data buffer area.
- reading the compressed block corresponding to the page requested to be swapped in from the swap area of the nonvolatile storage device includes changing according to the request
- the page identifier of the incoming page obtains the swap area address corresponding to the page identifier of the page requested to be swapped in from the mapping table; reads the write in which the compressed block is located from the obtained swap area address Unit, and store the write unit in the compressed data buffer area.
- the state of the writing unit is marked as being swapped in when the writing unit where the compressed block is located is read.
- repeated reading of the same write unit/swap subarea multiple times can be avoided at the same time, which indirectly reduces the number of reads and writes, improves the exchange efficiency, and reduces the impact on the life of the nonvolatile storage device . It can be understood that when reading the writing unit where the compressed block is located, the state of the writing unit may not be marked as being swapped in.
- this embodiment of the present application further includes deleting the mapping table entry corresponding to the page identifier of the page requested to be swapped in; releasing the compressed block or writing unit/swap sub-area in a non-transitory Loss of space in the swap area of the storage device.
- the entire exchange subarea where the requested compressed block is located is read from the exchange area of the non-volatile storage device to the compressed data buffer area, which can read data in batches, reduce the number of reads, and reduce the need for non-volatile storage. The impact of the life of the equipment.
- releasing the space of the compressed block or the swap sub-area in the swap area of the non-volatile storage device can be achieved by setting the corresponding space of the compressed block or the swap sub-area in a swap area management structure such as an array. Set to idle to achieve.
- the execution timing of requesting page swapping may also be requesting page swapping 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 rules, such as the time period of application use, geographic location, frequency of application use, electronic device status such as network connection status, etc. and the model. Match, judge.
- the process of requesting page swap is the same as the foregoing content.
- page swapping is requested when the page to be accessed by the process is not in the memory to trigger a page fault exception, or it is predicted that the page of the specified group is about to be Requesting page swapping when accessed are only two of these scenarios, and this application does not restrict it.
- an embodiment of this application also provides a memory swap device, which includes a module for implementing one or more steps of the method.
- the realization of this module can be software or a combination of software and hardware.
- an embodiment of the present application provides a terminal, wherein the computer device includes a processor and a memory, the memory is used to store a software program, and the processor is used to read the software program stored in the memory and Perform the method provided by any one of the implementations of the first aspect.
- an embodiment of the present application provides a storage medium, which is characterized by including a computer program, which is used to implement any implementation manner of the first aspect when running on one or more processors Provided method.
- an embodiment of the present application provides a computer program product, which is characterized by including a computer program, which is used to implement any one of the first aspect when running on one or more processors The method provided by the method.
- FIG. 1 is a schematic structural diagram of a terminal device applied in an embodiment of this application
- FIG. 2 is a schematic flowchart of a memory swap method provided by an embodiment of the application
- FIG. 3 is a schematic diagram of a process of selecting a page to be swapped out according to an embodiment of the application
- FIG. 4 is a schematic diagram of a process of compressing selected pages to be swapped out into compressed blocks and grouping them according to the grouping identifier of the pages according to an embodiment of the application;
- Fig. 5 is a schematic diagram of a process of organizing selected compressed blocks into compressed pages and swapping them out to continuous space in the swap area of a nonvolatile storage device according to an embodiment of the application;
- FIG. 6 is a schematic diagram of the process of organizing selected compressed blocks into exchange sub-areas and then swapping out to continuous space in the exchange area of a non-volatile storage device according to an embodiment of the application;
- FIG. 7 is a schematic diagram of a page swapping process provided by an embodiment of the application.
- FIG. 8 is a schematic structural diagram of a computer system provided by an embodiment of the application.
- FIG. 9 is a schematic structural diagram of a memory swap device provided by an embodiment of the application.
- Fig. 10a is a schematic diagram of an example of memory exchange using the swap mechanism
- FIG. 10b is a schematic diagram of an example of a memory swap method provided by an embodiment of the application.
- plural means two or more.
- the term “and/or” or the character "/” in this application is only an association relationship describing the associated objects, indicating that there can be three relationships, for example, A and/or, or A/B, which can mean: alone There are three cases of A, A and B at the same time, and B alone.
- the memory swap method provided in the embodiments of the present application is mainly applied to terminal devices using the operating system of the Linux kernel.
- the terminal equipment can also be called User Equipment (UE), Mobile Station (MS), Mobile Terminal (Mobile Terminal), etc.
- the terminal can be equipped with a radio access network (Radio Access Network).
- Radio Access Network Radio Access Network
- RAN radio access network
- the ability to communicate with one or more core networks for example, the terminal can be a mobile phone (or called a "cellular" phone), or a mobile computer, etc., for example, the terminal can also be a portable or pocket-sized , Handheld, computer built-in or vehicle-mounted mobile devices.
- the memory management method provided in the embodiments of the present application can also be applied to other types of computer systems, such as household appliances, servers, and so on.
- FIG. 1 is a schematic structural diagram of a terminal device applied in an embodiment of this application.
- the terminal device 100 includes a memory 180, a processor 150, and a display device 140.
- the memory 180 stores a computer program
- the computer program includes an operating system program 182, an application program 181, and the like.
- the processor 150 is used to read the computer program in the memory 180 and then execute the method defined by the computer program.
- 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 read one or more application programs 181 to run the application on the terminal device.
- the processor 150 may include one or more processors.
- the processor 150 may include one or more central processors, or include one central processor and one graphics processor.
- the processor 150 includes multiple processors, the multiple processors may be integrated on the same chip, or each may be an independent chip.
- a processor may include one or more processing cores.
- the memory 180 also stores other data 183 in addition to the computer program.
- the other data 183 may include data generated after the operating system 182 or the application program 181 is run.
- the data includes system data (such as operating system configuration parameters) and User data, such as data cached by an application opened by the user, is typical user data.
- the storage 180 generally includes internal memory and external storage.
- the memory can be random access memory (RAM), read only memory (ROM), and cache (CACHE).
- External storage can be hard disk, CD, USB disk, floppy disk, flash memory or tape drive, etc.
- Computer programs are usually stored on external memory, and the processor loads the computer programs from external memory to memory before executing processing.
- the nonvolatile storage device swap area is the space on the external storage. When the page fault is abnormal, the corresponding page will be swapped from the external storage to the memory.
- the operating system program 182 includes a computer program that can implement the memory swap method provided in the embodiments 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 be equipped with the implementation of the present application. Examples provide swap-in and/or swap-out functions. Further, the operating system can open the call interface of the swap-in and/or swap-out function to the upper caller. After the processor 150 reads the application program 181 from the memory 180 and runs it, it can implement the memory by calling the interface. exchange.
- the terminal device 100 may also include an input device 130 for receiving input digital information, character information, or contact touch operations/non-contact gestures, and generating signal inputs related to user settings and function control of the terminal device 100.
- the terminal device 100 may also include a display device 140, which includes a display panel 141 for displaying information input by the user or information provided to the user, various menu interfaces of the terminal device 100, and the like.
- the touch panel 131 may cover the display panel 141 to form a touch display screen.
- the terminal device 100 may also include a power supply 190 for supplying power to 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, and so on.
- the terminal device 100 may also include a radio frequency (RF) circuit 110 for performing network communication with a wireless network device, and may also include a WiFi module 170 for performing WiFi communication with other devices.
- RF radio frequency
- the following embodiment will introduce the memory swap method provided by the embodiment of the present application, including page swapping out and page swapping in.
- the memory swap method provided by the embodiment of the present application may be implemented in the operating system program 182 shown in FIG. 1.
- the swap area of the nonvolatile storage device mentioned in the embodiment of the present application may be a flash memory.
- FIG. 2 is a schematic flowchart of a memory swap method provided by an embodiment of this application.
- the method includes:
- Step S201 Select n pages to be swapped out
- Step S202 Compress the n pages to be swapped out into n compressed blocks, and cache the n compressed blocks in a compressed data buffer area;
- Step S203 Organize at least one compressed block among the n compressed blocks into m writing units
- Step S204 Write the m write units to the swap area of the nonvolatile storage device at most m times, wherein at least one write unit is stored in a continuous space of the swap area.
- FIG. 10b an example schematic diagram of a memory swap method provided in an embodiment of the application:
- each page to be swapped out is compressed into a compressed block, and the compressed blocks are grouped and cached in the compressed data buffer area 1011 into groups 1 and 2 according to the group identifier such as the process identifier. ...Group g. Among them, the memory pages with the same group identifier will be cached in the same group after being compressed. As shown in FIG. 10b, the compressed blocks with the same group identifier are identified by the same image filling format.
- the square in the above diagonal filling format represents the compressed block of the memory page with the group identifier corresponding to group 1; the square in the lower diagonal filling format represents the compressed block of the memory page with the group identifier corresponding to the group 2;
- the square filled with vertical lines represents the compressed block of the memory page with the group identifier corresponding to the group g after compression.
- the amount of data written at one time is the upper limit of the amount of I/O read and write, and the upper limit is affected by the limitation of non-volatile storage devices.
- Figure 10a is a schematic diagram of an example of memory swap of the swap mechanism.
- the pages to be swapped out are swapped out from the memory 1010 according to the LRU algorithm, and the pages to be swapped out are compressed into compressed blocks and written into the compressed data buffer area 1011, as shown in Figure 10a, using different patterns to fill the format squares.
- a compressed block is selected according to the LRU algorithm and decompressed and then swapped out to the swap area 1021 of the nonvolatile storage device 1020.
- the page to be swapped out may be executed when the memory is insufficient for memory reclamation, or the system may execute it periodically. Triggered by the caller.
- the embodiment of this application will provide the caller with two interfaces, that is, the caller decides whether to execute swapping out the page to the compressed data buffer area or swapping out the compressed block to the non-volatile storage device. Exchange area.
- the caller may be the memory management module, process management module or application framework of the operating system.
- the modules involved in this embodiment include mem_cgroup, kswapd, zswap/zram, f2fs/nonvolatile storage device driver, etc., which are specifically as follows:
- this embodiment modifies the mem_cgroup grouping method to realize the mem_cgroup grouping by process; it can be configured to determine whether to group by process or by application, etc.;
- this embodiment adds the function of exchanging part or all of the same group, such as part or all of the pages of the same process;
- this embodiment adds the function of grouping compressed data by mem_cgroup and swapping out the same group of pages to the swap area of the non-volatile storage device;
- the swap file read-write and address management module is based on the f2fs file system; if the swapped out data is read and written through the swap area of the non-volatile storage device, the swap area read-write and address management module is based on The existing non-volatile storage device driver module is implemented.
- FIG. 3 is a schematic diagram of a specific process of selecting a specified number of associated pages to be swapped out in step S201 of the memory swap method provided in this embodiment of the application, including the following processes:
- Step S301 Receive an instruction sent by the caller, the instruction includes the group identifier and the number of pages n;
- Step S302 Select pages with the same group identifier according to the least recently used algorithm
- Step S303 For each page selected, determine whether the page is anonymous:
- step S304 If yes, go to step S304;
- step S306 If not, perform step S306;
- Step S304 Set the page as a dirty page
- Step S305 Add the page to the exchange cache
- Step S306 Determine whether the number of selected pages is less than n:
- mem_cgroup is configured to be grouped by application.
- the Activity Manager in the application framework finds that application A is frozen, which means that the probability of application A being used again in a short time is low, this application is called
- the provided interface notifies the application identifier of application A and the number of pages n that need to be swapped out to the embodiment of this application.
- the embodiment of this application selects n anonymous pages of application A according to the order of the pages from cold to hot. An anonymous page is marked as dirty and added to swap cache.
- mem_cgroup is configured to group by processes.
- the interface provided by this application is called, and the process A and the number of pages to be swapped out n are notified according to the memory recycling strategy determined by the memory management module.
- the embodiment of the 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.
- FIG. 4 is a schematic diagram of the specific process of compressing the n pages to be swapped out into n compressed blocks in step S202 of the memory swap method provided in this embodiment of the application, and grouping and buffering them in the compressed data buffer area according to the group identifier of the pages. , Including the following processes:
- Step S401 Compress the selected page into a compressed block, and mark the group identifier of the compressed block;
- Step S402 Buffer the compressed block group into the compressed data buffer area according to the group identifier
- Step S403 Determine whether the amount of cached data in the compressed data cache reaches the threshold:
- the aforementioned n anonymous pages are compressed one by one, and stored in the compressed data buffer area according to the group identifier.
- At least one compressed block of the n compressed blocks is organized into m writing units, and the m writing units are written to at most m times.
- the swap area of a non-volatile storage device at least one write unit is stored in a continuous space of the swap area.
- the size of the writing unit is one page.
- 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 compressed pages.
- the compressed page mentioned 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.
- Embodiment 1 Please refer to FIG. 5, in order to swap out the selected compressed block in the form of compressed page to the continuous space of the nonvolatile storage device swap area, in this embodiment, the writing unit is one compressed page. Including the following processes:
- Step S501 Select the coldest x compressed blocks of the specified process in the compressed data buffer area, where x is the number of compressed blocks determined by the caller or the total data amount of the x compressed blocks is less than or equal to and is closest to the caller Determine the amount of data to be swapped out;
- Step S502 Sort the compressed blocks from large to small
- Step S503 Allocate a temporary page in the memory, where the temporary page is used to store the compressed block so that it can be subsequently swapped out to the continuous space of the swap area of the nonvolatile storage device in batches;
- Step S504 Take out a compressed block in order
- Step S505 Query whether all allocated temporary pages have space for storage:
- step S506 allocate a new temporary page, and then continue to step S507;
- step S507 If yes, perform step S507;
- Step S507 Store the compressed block in a temporary page with space, and continue to perform step S508;
- Step S508 Determine whether there are unprocessed compressed blocks:
- Step S509 Calculate the number of temporary pages, and continue to perform step S510;
- Step S510 Determine whether the existing non-volatile storage device exchange sub-area space is sufficient:
- step S511 add a new exchange subarea for the process, and then continue to step S512;
- step S512 If yes, perform step S512;
- Step S512 Create a mapping table entry for the compressed block to be swapped out, and continue to perform step S513;
- Step S513 Write all temporary pages into the swap area of the nonvolatile storage device, and continue to perform step S514;
- Step S514 Release the temporarily allocated page and the space of the compressed block in the compressed data buffer area.
- the embodiment of the present application swaps out a specified packet such as the x number of application A described above when the memory management module is received.
- the coldest x compressed blocks in the compressed data cache can be selected according to the LRU algorithm, the compressed blocks are sorted in the order of the amount of data, and then the compressed blocks are saved in the compressed page in order.
- the compressed page is Temporary application is used to save pages of 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 to the continuous space of the swap area of the non-volatile storage device.
- the embodiment of the present application polls whether the allocated compressed page has space to store the current compressed block, until a compressed page is found for the current compressed block.
- the space of each compressed page can be fully utilized and storage efficiency can be improved.
- 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 are sorted into compressed blocks according to the amount of data from large to small. 1.
- Compression Block 2 compressed block 3 until compressed block 10, and then store these 10 compressed blocks in the compressed page in turn, the temporary storage of these 10 compressed blocks may be:
- Compressed page 1 stores compressed blocks 1, 2, 3, 6;
- Compressed page 2 stores compressed blocks 4, 5, 7, 10;
- Compressed page 3 stores compressed blocks 8,9.
- the memory management module When storing compressed block 5, the memory management module will sequentially determine whether the existing compressed page 1 has enough space. When it is found that the space of compressed page 1 is not enough to store, it will continue to determine whether the space of compressed page 2 is sufficient. At this time, the compressed page 2 The space is enough, so the compressed block 5 is stored in the compressed page 2;
- the embodiment of the application will allocate a swap sub-area for the three compressed pages in the swap area management structure that maintains the usage of the non-volatile storage device, and then write the three compressed pages into the swap area of the non-volatile storage device. Sub-area.
- the size of the writing unit is the size of a swap sub-area, such as 32K.
- 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 mentioned here refers to the 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 the multiple temporary swap sub-areas will be swapped out to the continuous space of the swap area of the non-volatile storage device.
- 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 temporary swap sub-area with a size of 32K.
- Embodiment 2 Please refer to FIG. 6, which is a schematic diagram of the process of batch swapping out selected compressed blocks to continuous space in the swap area of a non-volatile storage device in the form of temporary swap sub-areas according to this embodiment of the application, including the following processes:
- Step S601 Select the coldest x compressed blocks of the specified process in the compressed data buffer area, where x is the number of compressed blocks determined by the caller or the total data amount of the x compressed blocks is less than or equal to and is closest to the caller Determine the amount of data to be swapped out;
- Step S602 Allocate a temporary switching sub-area for the current packet
- Step S603 Take out a compressed block according to the page LRU order
- Step S604 Determine whether the space of the temporary swap subarea in the current memory is sufficient
- step S605 allocate a new temporary exchange sub-area for the process, and then continue to step S606;
- step S606 If yes, perform step S606;
- Step S606 Store the compressed block in the temporary exchange sub-area, and continue to perform step S607;
- Step S607 Determine whether there are unprocessed compressed blocks
- step S608 If not, perform step S608;
- Step S608 Create a mapping table entry for the compressed block to be swapped out, and continue to perform step S609;
- Step S609 Write all the temporary swap sub-areas into the swap area of the non-volatile storage device, and continue to perform step S610;
- Step S610 Release the temporarily allocated exchange sub-areas and the space of the compressed block in the compressed data cache.
- the embodiment of the present application swaps out a specified packet such as the x number of application A described above when the memory management module is received.
- the coldest x compressed blocks in the compressed data cache can be selected according to the LRU algorithm, a 32K temporary exchange subarea is applied for the compressed block, and the compressed block is selected from cold to hot according to the LRU algorithm and stored in the temporary exchange subarea 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 swap sub-areas, and the multiple temporary swap sub-areas will be swapped out to the continuous space of the swap area of the non-volatile storage device.
- compressed blocks 1, 2, and 3 are stored in temporary exchange subarea 1
- compressed blocks 4, 5 are stored in temporary exchange subarea 2.
- the memory management module allocates 2 swap sub-areas in the swap area management structure that maintains the usage of non-volatile storage devices, and then the temporary swap sub-area 1 and the temporary swap The content of sub-area 2 is written into these two exchange sub-areas.
- the page swap-in process is executed when a page fault exception is triggered when the process accesses a page that has been swapped out.
- 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, for example, swapped out from running memory to the swap area of a nonvolatile storage device, or swapped out from running memory to compressed data In the 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 based on the page identifier. Specifically, it can be determined by judging whether the page identifier is in the mapping table.
- the page identifier is not In the mapping table, it means that the page data is not in the swap area of the non-volatile storage device but in the compressed data cache area.
- Use the existing swap-in process otherwise, look up the swap area mapping table according to the page identifier to determine the non-volatile storage The device swap area address, and then read the corresponding page data from the nonvolatile storage device swap area and decompress it, and save the decompressed data in the newly allocated physical page when entering the page fault abnormal process.
- This process also includes deleting the The mapping table entry corresponding to the page identifier of the page requested to be swapped in; the space of the corresponding page data in the swap area of the non-volatile storage device is released.
- the compressed page where the requested compressed block is located or the entire swap subarea data is read from the swap area of the nonvolatile storage device to the compressed data cache area, the data of the compressed page or the swap subarea will be marked. The status is swapping in to avoid reading the same swap subarea multiple times at the same time.
- the requested compressed page or swap subarea is swapping in, it waits for the requested data to be swapped in.
- Embodiment 3 In the case of an abnormal page failure, the page swapping process.
- FIG. 7 is a schematic diagram of the page swapping process described in this embodiment, including the following processes:
- Step S701 Obtain the page identifier of the page requested to be swapped in;
- Step S702 Judge whether the page is in the compressed data cache
- step S707 If not, perform steps S703-S706, and then continue to step S707;
- step S707 If yes, go to step S707;
- Step S703 Acquire the swap area address of the non-volatile storage device from the mapping table according to the page identifier;
- Step S704 Read the requested compressed page or the entire exchange sub-region data to the compressed data cache, and mark that the page or sub-region is being swapped in;
- Step S705 Delete the mapping table entry
- Step S706 Release the space of the compressed page or sub-area in the swap area
- Step S707 Decompress the requested compressed block to the designated space, and continue to perform step S708;
- Step S708 Release the space of the compressed block in the compressed data cache.
- page 1 and page 2 of application A need to be swapped in as an example, in combination with the foregoing description, page 1 and page 2 have been swapped out to swap subarea 1 of the swap area of the nonvolatile storage device.
- mapping table is searched according to the page identifiers of page 1 and page 2, and the corresponding entry in the mapping table obtains the address of the exchange subarea 1 where the compressed block 1 and the compressed block 2 corresponding to page 1 and page 2 are located.
- Table 1 shows the comparison of the effect of page swapping out using the swap mechanism and the memory swap method provided in the embodiments of this application from the aspects of the amount of data swapped in, the amount of data swapped in, and the program startup time. It can be seen that:
- the memory exchange technology of the embodiment of the present application can reduce the amount of read and write of the non-volatile storage device, reduce the impact of memory exchange on the life of the non-volatile storage device, and can also improve the exchange performance.
- Program Swap out the amount of data The amount of data swapped in Program start time swap mechanism 42157MB 36010MB 822ms 32K batch swap out 17127MB 14521MB 337ms
- FIG. 8 is a schematic structural diagram of a computer system provided by an embodiment of this application.
- the computer system 800 may be a terminal device or other types of computer devices.
- 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 library layer 830, and a Linux operating system kernel layer 840.
- the operating system layer 860 in FIG. 8 can be considered as a specific implementation of the operating system 182 in FIG. 1, and the application layer 810 in FIG. 8 can be considered as a specific implementation of the application program 181 in FIG. 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, and so on.
- the system runtime library layer 830 includes interface management 831, multimedia library 832, and so on.
- the application framework layer 820 may include an activity manager (Activity Manager), a package manager (Package Manager), and the like.
- the application layer 810 may include application 1, application 2, and so on.
- the computer system 800 also includes a hardware layer 850.
- the hardware layer of the computer system 800 may include a memory 851, a non-volatile storage device 852 (equivalent to the memory 180 in FIG. 1), and the like.
- the computer system applying the memory swap method provided by the embodiment of the application can perform packet swapping in and/or swapping out after compressing the memory page, which can reduce the cost of nonvolatile storage devices.
- the amount of reads and writes improves the performance of the exchange process. At the same time, it can also reduce the number of reads and writes, thereby reducing the impact on the life of the nonvolatile storage device during the memory exchange process.
- the memory swap device 900 provided in this embodiment of the application, the memory swap device 900 is located in the operating system layer 860, and the device 900 includes:
- the selection module 910 is used to select n pages to be swapped out
- the compression module 920 is configured to compress the n pages to be swapped out into n compressed blocks, and cache the n compressed blocks in a compressed data buffer area;
- the organization module 930 is configured to organize at least one compressed block 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 Pages;
- the exchange module 940 is configured to write the m writing units to the exchange area of the nonvolatile storage device at most m times, wherein at least one writing unit is stored in a continuous space of the exchange area;
- the page swap-in module 950 is configured 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; from the obtained swap area
- the compressed block is read from the address, and the compressed block is stored in the compressed data buffer area.
- the device provided by the embodiment of the present application can simultaneously perform swap-out and swap-in processing of memory pages, or can perform swap-out processing of memory pages and swap-in processing of memory pages separately.
- the device embodiments described above are merely illustrative, and the units described as separate components may or may not be physically separate. Some or all of the modules can be selected according to actual needs to achieve the objectives of the solutions of the embodiments. In addition, in the accompanying drawings of the device embodiments provided in the present application, the modules may be communicatively connected through one or more communication buses or signal lines. Those of ordinary skill in the art can understand and implement without creative work.
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Human Computer Interaction (AREA)
- Memory System Of A Hierarchy Structure (AREA)
Abstract
一种内存交换方法及装置,用于电子设备的内存管理领域。其中,该方法包括选择n个待换出页面(S201);将所述n个待换出页面压缩成n个压缩块,并将所述n个压缩块缓存到压缩数据缓存区(S202);将所述n个压缩块中的至少一个压缩块组织成m个写入单位(S203);将所述m个写入单位分最多m次写入到非易失存储设备的交换区,其中至少一个写入单位存储在所述交换区的一段连续空间(S204)。通过该方法,将待换出页面压缩成压缩块,减少内存交换的写入数据量,从而间接减少了写入次数;将压缩块组织成写入单位并且每次写入至少一个写入单位到交换区的一段连续空间,每次I/O操作的写入量大大增加了,从而减少内存交换的写入次数,延长了非易失存储设备的使用寿命。
Description
本申请实施例涉及电子设备的内存管理领域,尤其是涉及内存交换的方法。
内存是计算机中重要的部件之一,它是与中央处理器(central processing unit,CPU)进行沟通的桥梁。计算机中所有程序的运行都是在内存中进行的,因此内存的性能对计算机的影响非常大。内存也被称为内存储器,其作用是用于暂时存放CPU中的运算数据,以及与硬盘等外部存储器交换的数据。计算机在运行时,CPU会把需要运算的数据调到内存中进行运算,当运算完成后CPU再将结果传送出来,内存的运行也决定了计算机的稳定运行。由于内存容量的限制,一般的计算设备上都会设计内存回收机制,在系统内存紧张时,通过回收部分已被占用的内存空间以满足新的内存分配请求。内存回收的对象主要分为文件页和匿名页(anonymous page)。匿名页是没有文件背景的页面,如堆,栈,数据段等。对于被修改了的文件页,在内存回收时需要将这部分文件页写回磁盘文件,而没有被修改的文件页将直接被释放。
对于匿名页,在现有技术中,可以通过交换(swap)机制进行内存回收。在匿名页的内存回收过程中,系统将内存中的匿名页交换到非易失存储设备的交换区或者交换文件中,从而回收该部分匿名页所占用的内存空间,实现了扩大当前可使用的内存空间的效果。swap机制的原理为,内存资源紧张时,根据近期最少使用算法(Least recently used,LRU)选择最冷的内存页面,将最冷的内存页面压缩成压缩块缓存到压缩数据缓存区,当压缩数据缓存区缓存数据量达到阈值时,根据LRU算法选择最冷的压缩块,将最冷的压缩块解压后写入非易失存储设备的交换区。由此可见,在内存资源紧张时,系统会频繁将内存中的页面写入到非易失存储设备的交换区中,当进程访问被写入非易失存储设备的交换区的页面时再将页面读取到内存中。对于嵌入式电子设备,例如手机或者平板电脑等,其使用的非易失存储设备通常为闪存(flash)类的存储器件,flash类的存储器件的读写次数受限,使用swap机制会影响flash类的存储器件的寿命。
发明内容
本申请实施例提供一种内存交换的方法和装置,应用于电子设备的内存管理领域,解决如下问题:
减少非易失存储设备读写次数,延长非易失存储设备的使用寿命。
本申请是针对匿名页的内存交换方案。
下面首先介绍一下本申请实施例中出现的术语。
运行内存:是指程序运行时需要的内存,只能临时存储数据用于与CPU交换高速缓存数据,一般多指随机存取存储器(Random Access Memory,RAM)。
压缩数据缓存区:用于存放从运行内存换出的页面/压缩块的一段内存空间。
压缩页:在压缩块从压缩数据缓存区被换出到非易失存储设备的交换区过程中,用于临时存储压缩块的内存页面。
交换子区域:在压缩块从压缩数据缓存区被换出到非易失存储设备的交换区过程中,用 于临时存储压缩块的内存空间,交换子区域的大小为整数个页面的大小,比如32K。
非易失存储设备的交换区:非易失存储设备如磁盘上用于存放换出数据的空间。
第一方面,本申请实施例提供一种内存交换的方法,该方法包括:选择n个待换出页面,所述n为大于0的整数;将所述n个待换出页面压缩成n个压缩块,并将所述n个压缩块缓存到压缩数据缓存区;将所述n个压缩块中的至少一个压缩块组织成m个写入单位,所述m为小于n且大于0的整数,所述写入单位的大小为整数个页面;将所述m个写入单位分最多m次写入到非易失存储设备的交换区,其中至少一个写入单位存储在所述交换区的一段连续空间。
通过该方法,在换出页面时,将待换出页面压缩成压缩块,减少内存交换过程中的写入数据量,从而间接减少了写入次数;进一步的,将压缩块组织成写入单位并且每次写入至少一个写入单位到交换区的一段连续空间,与现有技术相比,每次I/O操作的写入量大大增加了,从而减少内存交换过程中的写入次数,从而降低了内存交换过程中对非易失存储设备寿命的影响。
可以理解的是,m有可能是等于n的整数。但是这仅是在写入单位为一个页面的大小,并且每个待换出页面的对应的压缩块占用一个页面的情况下才会出现。本申请结合对待换出页面进行了压缩以及写入单位大小的设置,绝大多数情况下都能起到上述效果。
需要说明的是,前述写入单位是指写入单位大小的压缩块集合。以一个写入单位为4个页面为例,则一个写入单位的大小为32K。在页面换出过程中,本申请会以32K为单位对至少一个压缩块进行存储,直到要换出的所有压缩块都被存储。可以理解的是,要换出的所有压缩块可能占用多个32K空间。并且,本申请会以32K为最小单位将压缩块写入到交换区中。
举个具体的例子,假设系统要换出10个压缩块,分别为压缩块1,压缩块2……压缩块10。其中,压缩块1-6的总数据量为32K,压缩块7-10的总数据量为32K,则在换出过程中,这10个压缩块将被组织成2个写入单位,若交换区上有一段64K的连续空间,则这两个写入单位可以一次写入到交换区,否则分2次写入到交换区。
下文提到的交换子区域的大小与写入单位的大小相同。
下文提到的调用者可以操作系统也可以是应用层的应用。
在一种可能的实现方式中,在选择n个待换出页面时,可以由调用者指示分组标识,然后选择具有所述分组标识的n个待换出页面。其中,分组标识包括资源使用行为相近的进程组标识、进程标识、应用标识或者访问时间,分组标识还可以是其他本领域技术人员能想到能够对待换出页面进行分组的其他标识,本申请不做特殊限制。n也可以由调用者指示。调用者可以是操作系统或者应用层的应用,本申请不做限制。在本申请中,调用者可以决定调用本申请所述的方法的时机。
在确定分组标识后,例如,确定了选择A进程的n个待换出页面后,可以根据近期最少使用算法(Least recently used,LRU)选择最少使用的n个页面。需要说明的是,本申请实施例也可以根据其他算法比如最不经常使用算法(Least Frequently Used,LFU)选择待换出页面,本申请不做限制。
在这种方式下,由于调用者了解进程/应用等的冷热情况,调用者可以根据自身策略比如根据LRU选择最冷进程/应用的页面换出,避免刚换出的页面又在短时间内被换入。同时,在这种方式下,可以在选择待换出页面时选择具有相同分组标识的页面,为后续将具有相同分 组标识的页面分组缓存整体换出、减少交换过程中的读写次数、提高交换效率做铺垫。
可以理解的是,前述最冷进程、最冷内存页面或最冷压缩块中最冷的含义是根据LRU算法选择出来的最少使用的进程、内存页面或者压缩块。
在一种可能的实现方式中,根据LRU选择一个页面后,判断该页面是否为匿名页,若是匿名页,则将该页面标记为脏页,加入临时的交换缓存(swap cache),否则继续选择其他页面,直到选完n个页面。
在一种可能的实现方式中,选择n个待换出页面可以在内存不足进行内存回收时执行。
在一种可能的实现方式中,选择n个待换出页面也可以由系统周期性执行,所述周期由系统设置。
在另一种可能的实现方式中,选择n个待换出页面也可以由调用者决定执行时机。
在一种可能的实现方式中,一个待换出页面压缩后对应一个压缩块。当选择的待换出页面的压缩率低于阈值时,可以直接保存原页面而不进行压缩,并将这些压缩率低于阈值的页面与压缩块统一管理。所述压缩率阈值由系统设置。
在一种可能的实现方式中,将所述n个压缩块缓存到压缩数据缓存区包括根据分组标识将所述n个压缩块分成至少一个分组并将所述至少一个分组缓存到所述压缩数据缓存区,其中,每个分组中包含的压缩块具有相同的分组标识,所述分组标识包括资源使用行为相近的进程组标识、进程标识、应用标识或者访问时间。
在一种可能的实现方式中,将所述n个压缩块中至少一个压缩块组织成m个写入单位包括将同一个分组中的压缩块组织成至少一个写入单位。
在一种可能的实现方式中,在所述将所述n个压缩块中至少一个压缩块组织成m个写入单位之前还包括根据第二指示选择所述至少一个分组中的部分分组,所述第二指示包括所述分组标识,所述分组标识包括资源使用行为相近的进程组标识、进程标识、应用标识或者访问时间,所述第二指示是操作系统发送的或应用层的应用发送的。
所述第二指示还包括压缩块数量或换出数据量,所述压缩数量或换出数据量指示所述分组标识指示的分组中的待换出的压缩块的数量或数据量。
在一种可能的实现方式中,将所述n个压缩块中至少一个压缩块组织成m个写入单位包括将所述n个压缩块中至少一个压缩块按压缩块从大到小的顺序组织成m个写入单位。
可以理解的是,由于权力要求的引用关系可以看出,这个步骤包含两种可能的场景,一种场景为至少一个压缩块是选择一个分组标识指示的分组后,所选择的分组对应的压缩块;另一种场景为至少一个压缩块为没有分组情况下的压缩块。对这两种场景下压缩块都可以采用从大到小的顺序组织成写入单位。
具体的,在将所述n个压缩块缓存到压缩数据缓存区过程中,如果压缩数据缓存区中的所有压缩块的数据量大于等于阈值时,则通知调用者,由调用者确定是否选择压缩块换出到非易失存储设备的交换区。若是,则由调用者通过分组标识指示待换出压缩块所在的分组以及压缩块数量x。根据LRU算法,选择调用者指示的分组中近期最少使用的x个压缩块,所述x为大于0小于等于n的整数,将x个压缩块先按照从大到小的顺序排序再组织成至少一个写入单位。
需要说明的是,所述将x个压缩块先按照从大到小的顺序排序再组织成写入单位是指压缩块在组织成写入单位前会先按照从大到小的顺序排序,然后根据当前写入单位对应的空间情况进行存储;而不是指最终写入单位中的压缩块存储顺序是从大到小的排序。 举个具体的例子,假设内存管理模块指定换出10个压缩块,则本申请会将这10个压缩块按照数据量从大到小排序成压缩块1、压缩块2、压缩块3直到压缩块10,然后再将这10个压缩块进行存储,则这10个压缩块的存储情况可能为压缩块1,2,3,6,4,5,7,10,8,9,详见实施例一描述。
在一种可能的实现方式中,在将所述n个压缩块缓存到压缩数据缓存区过程中,如果压缩数据缓存区中的所有压缩块的数据量大于等于阈值时,则通知调用者,由调用者确定是否选择压缩块换出到非易失存储设备的交换区。若是,则由调用者通过分组标识指示待换出压缩块所在的分组以及换出数据量size。根据LRU算法,从冷到热依次选择调用者指示的分组中近期最少使用的压缩块,直到选择的压缩块的总数据量小于等于且又最接近于所述换出数据量size,假设此时对应的压缩块数量为x,将x个压缩块先按照从大到小的顺序排序再组织成至少一个写入单位。
需要说明的是,所述将x个压缩块先按照从大到小的顺序排序再组织成写入单位是指压缩块在组织成写入单位前会先按照从大到小的顺序排序,然后根据当前写入单位对应的空间情况进行存储;而不是指最终写入单位中的压缩块存储顺序是从大到小的排序。举个具体的例子,假设内存管理模块指定换出10个压缩块,则本申请会将这10个压缩块按照数据量从大到小排序成压缩块1、压缩块2、压缩块3直到压缩块10,然后再将这10个压缩块进行存储,则这10个压缩块的存储情况可能为压缩块1,2,3,6,4,5,7,10,8,9,详见实施例一描述。
在一种可能的实现方式中,在将所述n个压缩块缓存到压缩数据缓存区过程中,如果压缩数据缓存区中的所有压缩块的数据量大于等于阈值时,则通知调用者,由调用者确定是否选择压缩块换出到非易失存储设备的交换区。若是,则由调用者通过分组标识指示待换出压缩块所在的分组以及压缩块数量。根据LRU算法,选择调用者指示的分组中近期最少使用的x个压缩块,所述x为大于0小于等于n的整数,所述x为调用者确定的压缩块数量,将x个压缩块先按照近期最少使用的顺序从冷到热排序再组织成至少一个写入单位。
在一种可能的实现方式中,在将所述n个压缩块缓存到压缩数据缓存区过程中,如果压缩数据缓存区中的所有压缩块的数据量大于等于阈值时,则通知调用者,由调用者确定是否选择压缩块换出到非易失存储设备交换区。若是,则由调用者通过分组标识指示待换出压缩块所在的分组以及换出数据量size。根据LRU算法,从冷到热依次选择调用者指示的分组中近期最少使用的压缩块,直到选择的压缩块的总数据量小于等于且又最接近于所述换出数据量size,假设此时对应的压缩块数量为x,将x个压缩块先按照近期最少使用的顺序从冷到热排序再组织成至少一个写入单位。
需要说明的是,本申请实施例也可以根据其他算法比如LFU选择x个压缩块,本申请不做限制。
需要说明的是,前述将压缩块按照从大到小的顺序排序或者按照近期最少使用的顺序从冷到热排序,是为提高存储空间利用率的可选步骤,在实际实施过程中也可以不进行排序直接将选择的压缩块组织成至少一个写入单位。
需要说明的是,对于本申请实施例提供的将压缩块换出到非易失存储设备的交换区的接口,调用者可以自主决定在任何时候调用,本申请不做限制。在接收到压缩数据缓存区的所有压缩块的数据量大于等于阈值的信息时选择压缩块换出到非易失存储设备的交换区只是其中的一种场景。
具体的,写入单位的形式可以是一个临时页面。压缩块按照前述方法排好序后,由系统分配一个临时页面,然后将调用者指示的待换出的x个压缩块逐个存入所述临时页面,若临时页面空间不足,则系统再分配一个新的临时页面,直到调用者指示的待换出的x个压缩块都存入临时页面。系统在存入每个压缩块时会查询所有已分配的临时页面是否有空间存放当前压缩块,直到为当前压缩块找到临时页面,通过这个步骤可以充分利用每个临时页面的空间,提高存储效率。当调用者指示的待换出的x个压缩块都存入临时页面时,统计临时页面的数量y,将y个临时页面换出到非易失存储设备的交换区。以待换出的压缩块占用了8个临时页面为例,若交换区有一段32K的连续空间,则这8个页面将被一次性换出到这32K空间中。若交换区中没有连续的空间,则最差的情况下,这8个临时页面将分8次换出到交换区中。通过该方法,使得待换出压缩块能够连续存储,在换出过程中以写入单位为最小单位进行读写,减少了读写次数,从而降低了对非易失存储设备的寿命的影响。
在一种可能的实现方式中,写入单位的形式也可以是一个临时交换子区域,临时交换子区域的大小为整数个页面的大小,所述临时交换子区域的大小由系统设置。压缩块按照前述方法排好序后,由系统分配一个临时交换子区域,然后将调用者指示的待换出的x个压缩块逐个存入所述临时交换子区域,若临时交换子区域空间不足,则系统再分配一个新的临时交换子区域,直到x个压缩块都存入。假设此时临时交换子区域的总个数为m个,则这m个临时交换子区域对应m个写入单位,将分最多m次写入到非易失存储设备的交换区。以m=5,临时交换子区域大小即写入单位大小为32K为例,假设此时交换区中有一段96K的连续空间,其余的为32K的连续空间,则这5个写入单位将分为3次写入交换区,一次写入3个写入单位,另外两次分别各写入1个写入单位。需要说明的是,在这种实现方式下,若待换出的压缩块的数据量小于一个临时交换子区域的大小,则可以先不换出,等到一个临时交换子区域的空间被用完后,再将该临时交换子区域中的压缩块换出到交换区。
通过该方法,待换出压缩块能够连续存储,在换出过程中以写入单位为最小单位进行读写,减少写入次数,降低了对非易失存储设备的寿命的影响。
在一种可能的实现方式中,在所述将所述m个写入单位分最多m次写入到非易失存储设备的交换区之前包括建立映射表,所述映射表包含所述至少一个压缩块的页面标识符和所述至少一个压缩块的交换区地址的对应关系,其中,所述页面标识符为所述至少一个压缩块所对应的至少一个待换出页面的标识符,所述交换区地址为分配给所述至少一个压缩块的一段属于所述交换区的空间的地址,所述映射表用于页面换入时通过所述页面标识符获取所述交换区地址。
在一种可能的实现方式中,在将所述m个写入单位分最多m次写入到非易失存储设备的交换区后,释放所述预交换缓存的空间,包括前述的所有临时页面或者所有临时交换子区域的空间,以及所述待换出压缩块在压缩数据缓存区中的空间。
在一种可能的实现方式中,可以通过交换区管理结构比如数组来维护非易失存储设备的交换区的使用情况,压缩块在非易失存储设备交换区中的地址可以通过<交换子区域起始地址,压缩块偏移量,压缩块长度>三元组来描述,从而可以实现对任意长度的压缩块进行寻址。
具体的,整个非易失存储设备的交换区为一个分区,交换区被划分为一系列交换子区域,结合前述描述可知,压缩块在换出时组织成至少一个写入单位,每个写入单位的大小与交换 子区域的大小相同。这样,就可以将一个写入单位写入一个交换子区域。若压缩块在换出时被组织成了m个写入单位,且在非易失存储设备的交换区中存在一段m个写入单位大小的连续的空间,在m个写入单位的大小小于一次I/O最大读写量的前提下,则可以将这m个写入单位一次性写入到交换区中,从而减少读写次数,降低内存交换过程中对非易失存储设备寿命的影响。其中,一次I/O最大读写量与非易失存储设备的限制相关。
在一种可能的实现方式中,整个非易失存储设备交换区为一个文件,分配给所述至少一个压缩块的一段属于所述交换区的空间的地址包括从文件中分配一段连续的空间用于存储换出的压缩块。
在一种可能的实现方式中,本申请实施例还包括请求页面换入,包括根据被请求换入的页面的页面标识符从所述映射表获取所述被请求换入的页面的页面标识符对应的交换区地址;从获取的所述交换区地址中读取压缩块,并将所述压缩块存入所述压缩数据缓存区。
在一种可能的实现方式中,请求页面换入的执行时机由调用者决定,包括进程要访问的页面不在内存中触发缺页异常时。
具体的,请求页面换入包括获取被请求换入的页面的页面标识符;判断被请求换入的页面是否在压缩数据缓存区中,若被请求换入的页面在压缩数据缓存区中,则根据被请求换入的页面的页面标识符找到该页面标识符对应的压缩块,将被请求换入的页面对应的压缩块解压到指定空间,所述指定空间可以是在请求页面换入时由调用者分配的。释放所述被请求换入的页面对应的压缩块所在的压缩数据缓存区的空间。若被请求换入的页面不在压缩数据缓存区中,则从非易失存储设备的交换区读取请求的压缩块。其中,对于判断被请求换入的页面是否在压缩数据缓存区中,一种可能的实现方式是通过页面标识符中的标志位判断被请求换入的页面是否在内存中,若不在内存中,则可能在压缩数据缓存区中或者在非易失存储设备的交换区中;然后通过映射表判断是在压缩数据缓存中还是在非易失存储设备的交换区中。
在一种可能的实现方式中,若被请求换入的页面不在压缩数据缓存区中,则从非易失存储设备的交换区读取被请求换入的页面对应的压缩块包括根据被请求换入的页面的页面标识符从所述映射表获取所述被请求换入的页面的页面标识符对应的交换区地址;从获取的所述交换区地址中读取压缩块,并将所述压缩块存入所述压缩数据缓存区。
在一种可能的实现方式中,若被请求换入的页面不在压缩数据缓存区中,则从非易失存储设备的交换区读取被请求换入的页面对应的压缩块包括根据被请求换入的页面的页面标识符从所述映射表获取所述被请求换入的页面的页面标识符对应的交换区地址;从获取的所述交换区地址中读取所述压缩块所在的写入单位,并将所述写入单位存入所述压缩数据缓存区。
在一种可能的实现方式中,在读取所述压缩块所在的写入单位时标记所述写入单位的状态为正在换入。在这种实现方式下,可以避免同时多次对同一个所述写入单位/交换子区域进行重复读取,间接减少读写次数,提高交换效率,降低对非易失存储设备的寿命的影响。可以理解的是,在读取所述压缩块所在的写入单位时也可以不标记所述写入单位的状态为正在换入。
在一种可能的实现方式下,本申请实施例还包括删除所述被请求换入的页面的页面标识符对应的映射表项;释放所述压缩块或者写入单位/交换子区域在非易失存储设备的交换区的空间。
在该方法中,从非易失存储设备的交换区将请求的压缩块所在整个交换子区域数据读 到压缩数据缓存区,能够批量的读取数据,减少读取次数,降低对非易失存储设备的寿命的影响。
在一种可能的实现方式中,释放所述压缩块或者所述交换子区域在非易失存储设备交换区域的空间可以通过在交换区管理结构比如数组中将压缩块或者交换子区域的对应空间设置为空闲来实现。
在另一种可能的实现方式中,请求页面换入的执行时机也可以是在预测到特定分组的页面即将被访问时请求页面换入。所述预测可以基于对用户行为习惯或者应用历史使用规律训练的模型进行判断,比如根据应用程序使用的时间段、地理位置、应用程序使用的频率、电子设备的状态比如网络连接状态等与模型进行匹配、判断。请求页面换入的过程与前述内容一致。
需要说明的是,是否进行页面换入、什么时候执行页面换入可以由调用者决定,在进程要访问的页面不在内存中触发缺页异常时请求页面换入,或者预测到指定分组的页面即将被访问时请求页面换入仅是其中的两种场景,本申请不做限制。
第二方面,针对本申请提供的方法,本申请实施例还提供一种内存交换的装置,该装置包括实现方法的一个或多个步骤的模块。该模块的实现可以是软件或软硬结合。
第三方面,本申请实施例提供一种终端,其特征在于,所述计算机设备包括处理器和存储器,所述存储器用于存储软件程序,所述处理器用于读取存储在存储器的软件程序并执行第一方面的任意一种实现方式提供的方法。
第四方面,本申请实施例提供一种存储介质,其特征在于,包括计算机程序,所述计算机程序当在一个或多个处理器上运行的时候用于实现第一方面的任意一种实现方式提供的方法。
第五方面,本申请实施例提供一种计算机程序产品,其特征在于,包括计算机程序,所述计算机程序当在一个或多个处理器上运行的时候用于实现第一方面的任意一种实现方式提供的方法。
为了更清楚地说明本申请实施例或现有技术中的技术方案,下面将对实施例或现有技术描述中所需要使用的附图作简单地介绍,显而易见地,下面描述中的附图仅仅是本申请的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。
图1为本申请实施例应用的终端设备的结构示意图;
图2为本申请实施例提供的内存交换方法的流程示意图;
图3为本申请实施例提供的选择待换出页面的流程示意图;
图4为本申请实施例提供的将选择的待换出页面压缩成压缩块并按照页面的分组标识进行分组的流程示意图;
图5为本申请实施例提供的将选择的压缩块组织成压缩页的形式再换出到非易失存储设 备交换区的连续空间的流程示意图;
图6为本申请实施例提供的将选择的压缩块组织成交换子区域的形式再换出到非易失存储设备交换区的连续空间的流程示意图;
图7为本申请实施例提供的页面换入流程示意图;
图8为本申请实施例提供的计算机系统的结构示意图;
图9为本申请实施例提供的内存交换装置的结构示意图;
图10a为采用swap机制进行内存交换的示例示意图;
图10b为本申请实施例提供的内存交换方法的示例示意图。
下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例是本申请一部分实施例,而不是全部的实施例。基于本申请中的实施例,本领域普通技术人员在没有作出创造性劳动前提下所获得的所有其他实施例,都属于本申请保护的范围。
在本申请实施例的描述中,除非另有说明,“多个”的含义是两个或两个以上。本申请中的术语“和/或”或字符“/”,仅仅是一种描述关联对象的关联关系,表示可以存在三种关系,例如,A和/或,或A/B,可以表示:单独存在A,同时存在A和B,单独存在B这三种情况。
本申请实施例提供的内存交换方法主要应用于使用Linux内核的操作系统的终端设备。终端设备也可称之为用户设备(User Equipment,UE)、移动台(Mobile Station,MS)、移动终端(Mobile Terminal)等,可选的,该终端可以具备经无线接入网(Radio Access Network,RAN)与一个或多个核心网进行通信的能力,例如,终端可以是移动电话(或称为“蜂窝”电话)、或具有移动性质的计算机等,例如,终端还可以是便携式、袖珍式、手持式、计算机内置的或者车载的移动装置。应理解的是,除了终端设备以外,本申请实施例提供的内存管理方法也可以应用于其他类型的计算机系统,如家电设备、服务器等。
请参考图1,为本申请实施例应用的终端设备的结构示意图。如图1所示,终端设备100包括存储器180、处理器150以及显示设备140。存储器180存储计算机程序,该计算机程序包括操作系统程序182和应用程序181等。处理器150用于读取存储器180中的计算机程序,然后执行计算机程序定义的方法,例如处理器150读取操作系统程序182从而在该终端设备100上运行操作系统以及实现操作系统的各种功能,或读取一种或多种应用程序181,从而在该终端设备上运行应用。
处理器150可以包括一个或多个处理器,例如,处理器150可以包括一个或多个中央处理器,或者包括一个中央处理器和一个图形处理器。当处理器150包括多个处理器时,这多个处理器可以集成在同一块芯片上,也可以各自为独立的芯片。一个处理器可以包括一个或多个处理核。
另外,存储器180还存储有除计算机程序之外的其他数据183,其他数据183可包括操作系统182或应用程序181被运行后产生的数据,该数据包括系统数据(例如操作系统的配置参数)和用户数据,例如用户打开的应用程序所缓存的数据就是典型的用户数据。
存储器180一般包括内存和外存。内存可以为随机存储器(RAM),只读存储器(ROM),以及高速缓存(CACHE)等。外存可以为硬盘、光盘、USB盘、软盘、闪存或磁带机等。计算机程序通常被存储在外存上,处理器在执行处理前会将计算机程序从外存加载到内存。本申 请实施例中非易失存储设备交换区为外存上的空间。缺页异常时会将相应的页面从外存换入到内存中。
操作系统程序182中包含了可实现本申请实施例提供的内存交换方法的计算机程序,从而使得处理器150读取到该操作系统程序182并运行该操作系统后,该操作系统可具备本申请实施例提供的换入和/或换出功能。进一步的,该操作系统可以向上层调用者开放该换入和/或换出功能的调用接口,处理器150从存储器中180中读取应用程序181并运行后,就可以通过调用该接口实现内存交换。
终端设备100还可以包括输入设备130,用于接收输入的数字信息、字符信息或接触式触摸操作/非接触式手势,以及产生与终端设备100的用户设置以及功能控制有关的信号输入等。
终端设备100还可以包括显示设备140,显示设备140,包括显示面板141,用于显示由用户输入的信息或提供给用户的信息以及终端设备100的各种菜单界面等。在其他一些实施例中,触控面板131可覆盖显示面板141上,形成触摸显示屏。
除以上之外,终端设备100还可以包括用于给其他模块供电的电源190以及用于拍摄照片或视频的摄像头160。终端设备100还可以包括一个或多个传感器120,例如加速度传感器、光传感器等。终端设备100还可以包括无线射频(Radio Frequency,RF)电路110,用于与无线网络设备进行网络通信,还可以包括WiFi模块170,用于与其他设备进行WiFi通信。
下面的实施例将介绍本申请实施例提供的内存交换方法,包括页面换出和页面换入。本申请实施例提供的内存交换方法可以实现在图1所示的操作系统程序182中。本申请实施例中提到的非易失存储设备交换区可以是闪存。
针对页面换出:
请参考图2,为本申请实施例提供的内存交换方法的流程示意图,所述方法包括:
步骤S201、选择n个待换出页面;
步骤S202、将所述n个待换出页面压缩成n个压缩块,并将所述n个压缩块缓存到压缩数据缓存区;
步骤S203、将所述n个压缩块中的至少一个压缩块组织成m个写入单位;
步骤S204、将所述m个写入单位分最多m次写入到非易失存储设备的交换区,其中至少一个写入单位存储在所述交换区的一段连续空间。
具体的,请参考图10b为本申请实施例提供的内存交换方法的示例示意图:
从内存1010中选择n个待换出页面后,将每个待换出页面压缩成压缩块,根据分组标识例如进程标识,将压缩块在压缩数据缓存区1011中分组缓存成分组1,分组2……分组g。其中,具有相同分组标识的内存页压缩后将被缓存到同一个分组中,如图10b所示,通过相同的图像填充格式来标识具有相同分组标识的压缩块。如上对角线填充格式的方块表示具有分组1对应的分组标识的内存页压缩后的压缩块;下对角线填充格式的方块表示具有分组2对应的分组标识的内存页压缩后的压缩块;竖线填充格式的方块表示具有分组g对应的分组标识的内存页压缩后的压缩块。当调用者调用本方法时,指示换出分组1对应的n个压缩块,则将n个压缩块组织成m个写入单位,分最多m次写入到非易失存储设备1020的交换区1021中。分最多m次是因为交换区1021上可能存在多个比如5个写入单位大小的一段连续的空间,这可以一次性将这5个写入单位写入到交换区1021中。需要说明的是,一次写入的数据量上 线为一次I/O读写量的上限,该上限受非易失存储设备的限制影响。
作为对比,请参考图10a,为swap机制的内存交换的示例示意图。以内存不足为例。当内存不足时,根据LRU算法从内存1010中换出待换出页面,将待换出页面压缩成压缩块写入压缩数据缓存区1011中,如图10a,用不同的图案填充格式的方块来表示不同待换出页面对应的压缩块。当压缩数据缓存区的数据量达到阈值时,根据LRU算法选择一个压缩块解压后换出到非易失存储设备1020的交换区1021中。
其中,选择待换出的页面可能在内存不足进行内存回收时执行,也可能是系统周期性地执行。由调用者触发。
具体的,在该流程中,本申请实施例将向调用者提供两个接口,即由调用者决策是否执行将页面换出到压缩数据缓存区或者将压缩块换出到非易失存储设备的交换区。所述调用者可以是操作系统的内存管理模块、进程管理模块或者应用程序框架。在Linux操作系统内核中,本实施例涉及到的模块包括mem_cgroup、kswapd、zswap/zram、f2fs/非易失存储设备驱动等,具体如下:
在mem_cgroup模块中,本实施例修改了mem_cgroup的分组方式,实现了按进程进行mem_cgroup分组;可以通过配置,确定是按照进程或者按照应用等进行分组;
在kswapd模块中,本实施例新增了将同一分组比如同一进程部分或者全部页面一起交换的功能;
在zswap/zram模块中,本实施例新增了按mem_cgroup对压缩数据进行分组,以及将同组页面一起换出到非易失存储设备的交换区的功能;
若换出数据通过交换文件读写,则交换文件读写及地址管理模块基于f2fs文件系统实现;若换出数据通过非易失存储设备交换区读写,则交换区读写及地址管理模块基于现有的非易失存储设备驱动模块实现。
下面将以非易失存储设备是闪存为例,对本申请实施例提供的一种内存交换方法的各步骤进行详细说明。
请参考图3,为本申请实施例提供的内存交换方法的步骤S201选择具有关联性的指定数量的待换出页面的具体流程示意图,包括如下流程:
步骤S301、接收调用者发送的指示,指示包含了分组标识和页面数量n;
步骤S302、根据近期最少使用算法选择具有相同分组标识的页面;
步骤S303、针对选择的每个页面,判断是否匿名页:
若是,则执行步骤S304;
若否,则执行步骤S306;
步骤S304、将页面设置为脏页;
步骤S305、将页面加入交换缓存;
步骤S306、判断已选择页面数量是否小于n:
若是,则继续步骤S302;
若否,则结束本流程。
具体的,例如,mem_cgroup配置为按照应用进行分组,当应用程序框架中的活动管理器(Activity Manager)发现应用A被冻结,表示应用A短时间内被再次使用的几率较低,则调 用本申请提供的接口,将应用A的应用标识及需要换出的页面数量n通知本申请实施例,本申请实施例根据LRU算法根据页面从冷到热的顺序选择应用A的n个匿名页,将n个匿名页分别标记为脏页,加入swap cache。
又如,mem_cgroup配置为按照进程进行分组,当内存管理模块发现系统内存不足时,则调用本申请提供的接口,根据内存管理模块确定的内存回收策略将进程A及需要换出的页面数量n通知本申请实施例,本申请实施例根据LRU算法根据页面从冷到热的顺序选择进程A的n个匿名页,将n个匿名页分别标记为脏页,加入swap cache。
请参考图4,为本申请实施例提供的内存交换方法的步骤S202将所述n个待换出页面压缩成n个压缩块并按照页面的分组标识分组缓存到压缩数据缓存区的具体流程示意图,包括如下流程:
步骤S401、将选择的页面压缩成压缩块,标记压缩块的分组标识;
步骤S402、根据分组标识将压缩块分组缓存到压缩数据缓存区;
步骤S403、判断压缩数据缓存区中的缓存数据量是否达到阈值:
若是,通知调用者缓存数据量超过阈值;
若否,则结束该流程。
在本流程中,将前述n个匿名页逐个压缩,并按照分组标识保存在压缩数据缓存区,在保存过程中判断压缩数据缓存区中的缓存数据量是否达到阈值,若已达到阈值,则通知内存管理模块缓存数据量已超过阈值,由内存管理模块决策是否选择压缩数据缓存区中的压缩块换出到非易失存储设备交换区。若内存管理模块确定需要将压缩块换出到非易失存储设备的交换区,则执行下述S203-S204对应的流程。
对于本申请实施例提供的内存交换方法的步骤S203-S204将所述n个压缩块中至少一个压缩块组织成m个写入单位,将所述m个写入单位分最多m次写入到非易失存储设备的交换区,其中至少一个写入单位存储在所述交换区的一段连续空间。本申请实施例将从两个具体应用场景进行说明。
应用场景一,写入单位的大小为一个页面。
将选中的压缩块以压缩页的形式换出到非易失存储设备交换区的连续空间。这里所说的压缩页是指用于临时存储选中的压缩块的页面,一个压缩页的大小为一个内存页面的大小,即4K。选中的压缩块可能占用多个压缩页,多个压缩页将一起被换出到非易失存储设备交换区的连续空间。
下面通过具体的实施例,对将选择的压缩块以压缩页的形式换出到非易失存储设备交换区的连续空间进行详细说明。
实施例一:请参考图5,为将选择的压缩块以压缩页的形式换出到非易失存储设备交换区的连续空间的流程示意图,本实施例中写入单位一个压缩页。包括如下流程:
步骤S501、选择压缩数据缓存区中指定进程的最冷的x个压缩块,其中x为调用者确定的压缩块数量或者所述x个压缩块的总数据量小于等于且又最接近于调用者确定的换出数据量;
步骤S502、将压缩块从大到小排序;
步骤S503、在内存中分配一个临时页面,该临时页面用于存放压缩块,以便后续可以批量换出到非易失存储设备交换区的连续空间;
步骤S504、按顺序取出一个压缩块;
步骤S505、查询所有已分配临时页面是否有空间存放:
若否,则执行步骤S506:分配新的临时页面,然后继续步骤S507;
若是,则执行步骤S507;
步骤S507、将压缩块存入有空间的临时页面,继续执行步骤S508;
步骤S508、判断是否还有未处理压缩块:
若是,则返回步骤S504;
若否,则继续步骤S509;
步骤S509、计算临时页面数量,继续执行步骤S510;
步骤S510、判断现有非易失存储设备交换子区域空间是否足够:
若否,则执行步骤S511:为进程增加新的交换子区域,然后继续步骤S512;
若是,则执行步骤S512;
步骤S512、为待换出的压缩块建立映射表项,继续执行步骤S513;
步骤S513、将所有临时页面写入非易失存储设备交换区,继续执行步骤S514;
步骤S514、释放临时分配的页面以及压缩块在压缩数据缓存区的空间。
本流程结束。
在本流程中,若内存管理模块确定需要将压缩块换出到非易失存储设备交换区,则本申请实施例在接收到内存管理模块换出指定分组比如前文所述的应用A的x个压缩块后,可根据LRU算法选择压缩数据缓存中最冷的x个压缩块,将压缩块按照数据量从大到小的顺序排序,然后按顺序将压缩块保存到压缩页中,压缩页是临时申请用于保存压缩块的页面,一个压缩页的大小为一个内存页面的大小,即4K。选中的压缩块可能占用多个压缩页,多个压缩页将一起被换出到非易失存储设备交换区的连续空间。
需要说明的是,在存储过程中,本申请实施例会轮询已分配的压缩页是否有空间存放当前压缩块,直到为当前压缩块找到压缩页。通过这个步骤可以充分利用每个压缩页的空间,提高存储效率。举个具体的例子,假设内存管理模块指定换出10个压缩块,这10个压缩块将占用3个压缩页,将这10个压缩块按照数据量从大到小排序成压缩块1、压缩块2、压缩块3直到压缩块10,然后将这10个压缩块依次存入压缩页,则这10个压缩块的临时存储情况可能为:
压缩页1存储了压缩块1,2,3,6;
压缩页2存储了压缩块4,5,7,10;
压缩页3存储了压缩块8,9。
原因说明:压缩页1在存储了压缩块1,2,3后,剩余的空间不足以存储压缩块4,所以内存管理模块为压缩块4分配一个新的临时页面即压缩页2。
在存储压缩块5时,内存管理模块会依次判断已有的压缩页1是否有足够的空间,当发现压缩页1空间不足以存放时,则继续判断压缩页2空间是否足够,此时压缩页2空间足够,所以将压缩块5存储到压缩页2;
在存储压缩块6时,同理,依次判断已有的压缩块1是否有足够的空间,若足够, 则将压缩块6存入压缩页1,继续压缩块7的存储。以此类推。
本申请实施例将为这3个压缩页在维护非易失存储设备使用情况的交换区管理结构中分配一个交换子区域,然后将这3个压缩页写入非易失存储设备交换区的交换子区域。
应用场景二,写入单位的大小为一个交换子区域的大小,比如32K。
将选择的压缩块以交换子区域的形式换出到非易失存储设备交换区的连续空间。这里所说的交换子区域是指内存管理模块分配的用于临时存储选中的压缩块的大小为32K的空间。选择的压缩块可能占用多个临时交换子区域,多个临时交换子区域将一起被换出到非易失存储设备交换区的连续空间。
下面通过具体的实施例,对将选择的压缩块以32K大小的临时交换子区域的形式换出到非易失存储设备交换区的连续空间进行详细说明。
实施例二:请参考图6,为本申请实施例提供的将选中的压缩块以临时交换子区域的形式批量换出到非易失存储设备交换区的连续空间的流程示意图,包括如下流程:
步骤S601、选择压缩数据缓存区中指定进程的最冷的x个压缩块,其中x为调用者确定的压缩块数量或者所述x个压缩块的总数据量小于等于且又最接近于调用者确定的换出数据量;
步骤S602、为当前分组分配一个临时交换子区域;
步骤S603、按照页面LRU顺序取出一个压缩块;
步骤S604、判断当前内存中临时交换子区域空间是否足够;
若否,则执行步骤S605:为进程分配新的临时交换子区域,然后继续步骤S606;
若是,则执行步骤S606;
步骤S606、将压缩块存入临时交换子区域,继续执行步骤S607;
步骤S607、判断是否还有未处理压缩块;
若是,则返回步骤S603;
若否,则执行步骤S608;
步骤S608、为待换出的压缩块建立映射表项,继续执行步骤S609;
步骤S609、将所有临时交换子区域写入非易失存储设备交换区,继续执行步骤S610;
步骤S610、释放临时分配的交换子区域以及压缩块在压缩数据缓存中的空间。
本流程结束。
在本流程中,若内存管理模块确定需要将压缩块换出到非易失存储设备交换区,则本申请实施例在接收到内存管理模块换出指定分组比如前文所述的应用A的x个压缩块后,可根据LRU算法选择压缩数据缓存中最冷的x个压缩块,为压缩块申请一个32K的临时交换子区域,按照LRU算法从冷到热选出压缩块存入临时交换子区域,若当前交换子区域空间不足,则申请新的临时交换子区域。选中的压缩块可能占用多个临时交换子区域,多个临时交换子区域将一起被换出到非易失存储设备交换区的连续空间。举个具体的例子,假设在应用A中选中5个待换出的压缩块,其中,压缩块1,2,3存储在临时交换子区域1,压缩块4,5存储在临时交换子区域2,当所有的压缩块都存入临时交换子区域时,内存管理模块在维护非易失存储设备使用情况的交换区管理结构中分配2个交换子区域,然后将临时交换子区域 1和临时交换子区域2的内容写入这2个交换子区域。
以上为对本申请实施例提供的针对页面换出的一种内存交换方法的各步骤的详细说明。
针对页面换入:
页面换入流程是在进程访问已经换出的页面时触发缺页异常时执行。当发生缺页异常时,表示当前访问的内存地址对应的内存页面已从所述内存换出,如,从运行内存换出至非易失存储设备交换区,或从运行内存换出至压缩数据缓存中。操作系统内核会从页表中获取页表项,根据页面标识符判断请求换入的页面是否在压缩数据缓存中,具体的,可通过判断页面标识符是否在映射表中,若页面标识符不在映射表中,则表示页面数据不在非易失存储设备交换区中而是在压缩数据缓存区域中,使用现有的换入流程;否则根据页面标识符查找交换区映射表以确定非易失存储设备交换区地址,然后从非易失存储设备交换区中读取相应的页面数据并解压,将解压后的数据保存在进入缺页异常流程时新分配的物理页面中,这个过程还包括删除被请求换入的页面的页面标识符对应的映射表项;释放相应的页面数据在非易失存储设备交换区域的空间。
需要说明的是,在从非易失存储设备交换区域将请求的压缩块所在的压缩页或者整个交换子区域数据读到压缩数据缓存区时,会标记所述压缩页或者所述交换子区域的状态为正在换入,以避免同时多次对同一个交换子区域进行读取,当请求的压缩页或者交换子区域的状态为正在换入,则等待请求的数据被换入。
下面通过具体的实施例,对进程要访问的页面不在内存中触发缺页异常时请求页面换入的过程进行详细说明。
实施例三:缺页异常的情况下,页面换入流程。
请参考图7,为本实施例描述的页面换入流程示意图,包括如下流程:
步骤S701、获取被请求换入的页面的页面标识符;
步骤S702、判断页面是否在压缩数据缓存中;
若否,则执行步骤S703-S706,然后继续步骤S707;
若是,则执行步骤S707;
步骤S703、根据页面标识符从映射表获取非易失存储设备交换区地址;
步骤S704、读取请求的压缩页或者整个交换子区域数据到压缩数据缓存,并标记该页面或者子区域正在换入;
步骤S705、删除映射表项;
步骤S706、释放压缩页或者子区域在交换区中的空间;
步骤S707、将请求的压缩块解压到指定空间,继续执行步骤S708;
步骤S708、释放压缩块在压缩数据缓存中的空间。
本流程结束。
在本流程中,以应用A的页面1,2需要被换入为例,结合前述描述,页面1和页面2已被换出到非易失存储设备交换区的交换子区域1。
具体的,根据页面1和页面2的页面标识符查找映射表,在映射表中对应的表项获取到 页面1和页面2对应的压缩块1和压缩块2所在的交换子区域1的地址,从该地址对应的空间中将整个交换子区域1的内容读取到压缩数据缓存,在维护非易失存储设备交换区使用情况的交换区管理结构中将该交换子区域的状态标记为正在换入。压缩块1和压缩块2换入到压缩数据缓存后,删除其对应的映射表项,并释放交换子区域1的空间,即在交换区管理结构中将交换子区域1标记为空闲。因在换出时,已通过<交换子区域起始地址,压缩块偏移量,压缩块长度>三元组记录压缩块在非易失存储设备交换区的地址,所以当压缩块被换入到压缩数据缓存后,可根据起始位置及压缩块长度将压缩块1和压缩块2解压到压缩数据缓存,然后从压缩数据缓存换入到运行内存。到此,页面换入流程结束。
以上为对本申请实施例提供的针对页面换入的一种内存交换方法的详细说明。
本申请实施例中,一方面,由于在内存交换过程中使用页面压缩的方式,既减少非易失存储设备的读写量,又提高了交换数据的读写效率;另一方面,由于在内存交换过程中考虑了页面的关联性,通过页面/压缩块分组的方式,在换出时组织成写入单位,并且每次写入至少一个写入单位到非易失存储设备的交换区的连续空间,减少读写非易失存储设备的次数,提高交换过程的性能。
请参见表1,为针对同一终端上、同一组应用,采用swap机制进行内存交换与采用本申请实施例提供的内存交换的效果对比图。方案对比如下:
优化前:当待换出页面从压缩数据缓存区换出到非易失存储设备交换区时,直接将内存页面换出,没有进行页面压缩,也没有考虑页面之间的关联性,因此交换数据量大、读写次数多、数据吞吐量低。在终端场景中,由于使用闪存,这种频繁大量的读写操作对闪存的寿命带来很大压力。
优化后:将待换出页面进行压缩,减少了交换的数据量,提高了数据吞吐率;同时还考虑了页面之间的关联性,将相关页面在非易失存储设备交换区中连续存储,减少了读写次数。
表1中分别从换出的数据量、换入数据量、程序启动时间等方面,给出了采用swap机制进行页面换出与采用本申请实施例提供的内存交换的方法的效果对比,可见,本申请实施例的内存交换技术可以减少非易失存储设备的读写量,降低内存交换对非易失存储设备寿命的影响,并且还可以提高交换性能。
表1
| 方案 | 换出数据量 | 换入数据量 | 程序启动时间 |
| swap机制 | 42157MB | 36010MB | 822ms |
| 32K批量换出 | 17127MB | 14521MB | 337ms |
以上主要介绍了本申请实施例提供的方法的具体流程,下面将结合图8以Android操作系统为例,介绍本申请实施例提供的方法的实现位置和运行时状态,更具体的方法流程可参考前述实施例所述。
请参考图8,为本申请实施例提供的计算机系统的结构示意图。该计算机系统800可以是终端设备,也可以是其他类型的计算机设备。
该计算机系统包括应用层810和操作系统层860,该操作系统可以为Android操作系统。操作系统层860又分为应用程序框架层820、系统运行库层830和linux操作系统内核层840。 其中,图8中的操作系统层860可以认为是图1中操作系统182的一种具体实现,图8中的应用层810可以认为是图1中应用程序181的一种具体实现。Linux操作系统内核层840提供操作系统的核心和管理,包括内存管理841、非易失存储设备驱动842、文件系统843等。系统运行库层830是包括界面管理831、多媒体库832等。应用程序框架层820可包括活动管理器(Activity Manager)、包管理器(Package Manager)等。应用层810可包括应用程序1、应用程序2等。
另外,该计算机系统800还包括硬件层850。该计算机系统800的硬件层可以包括内存851和非易失存储设备852(相当于图1中的存储器180)等。
本申请前述任意一个实施例提供的方法均可以实现在图8所示的操作系统层860中。
通过以上介绍的装置和方法可以看出,应用本申请实施例提供的内存交换方法的计算机系统可以在实现对内存页面压缩后进行分组换入和/或换出,可以减少非易失存储设备的读写量,提高交换过程的性能,同时,也可以减少读写次数,从而降低内存交换过程中对非易失存储设备寿命的影响。
请参见图9所示,为本申请实施例提供的内存交换装置900,该内存交换装置900位于操作系统层860中,所述装置900包括:
选择模块910,用于选择n个待换出页面;
压缩模块920,用于将所述n个待换出页面压缩成n个压缩块,并将所述n个压缩块缓存到压缩数据缓存区;
组织模块930,用于将所述n个压缩块中的至少一个压缩块组织成m个写入单位,所述m为小于或等于n且大于0的整数,所述写入单位的大小为整数个页面;
交换模块940,用于将所述m个写入单位分最多m次写入到非易失存储设备的交换区,其中至少一个写入单位存储在所述交换区的一段连续空间;
页面换入模块950,用于根据被请求换入的页面的页面标识符从所述映射表获取所述被请求换入的页面的页面标识符对应的交换区地址;从获取的所述交换区地址中读取压缩块,并将所述压缩块存入所述压缩数据缓存区。
装置实施例中涉及到的方案的具体实现可参考前述方法实施例,在此不再赘述。
本申请实施例提供的装置可以同时执行内存页面的换出和换入处理,也可以单独执行内存页面的换出处理和内存页面的换入处理。
以上所描述的装置实施例仅仅是示意性的,其中所述作为分离部件说明的单元可以是或者也可以不是物理上分开的。可以根据实际的需要选择其中的部分或者全部模块来实现本实施例方案的目的。另外,本申请提供的装置实施例附图中,模块之间可通过一条或多条通信总线或信号线进行通信连接。本领域普通技术人员在不付出创造性劳动的情况下,即可以理解并实施。
以上所述,仅为本申请的一些具体实施方式,但本申请的保护范围并不局限于此。
Claims (25)
- 一种内存交换的方法,其特征在于,包括:选择n个待换出页面,所述n为大于0的整数;将所述n个待换出页面压缩成n个压缩块,并将所述n个压缩块缓存到压缩数据缓存区;将所述n个压缩块中的至少一个压缩块组织成m个写入单位,所述m为小于n且大于0的整数,所述写入单位的大小为整数个页面;将所述m个写入单位分最多m次写入到非易失存储设备的交换区,其中至少一个写入单位存储在所述交换区的一段连续空间。
- 根据权利要求1所述的方法,其特征在于,所述选择n个待换出页面包括:接收第一指示,所述第一指示中包含分组标识,所述分组标识包括资源使用行为相近的进程组标识、进程标识、应用标识或者访问时间,所述第一指示是操作系统发送的或应用层的应用发送的;选择具有所述分组标识的n个待换出页面。
- 根据权利要求1或2所述的方法,其特征在于,所述将所述n个压缩块缓存到压缩数据缓存区包括:根据分组标识将所述n个压缩块分成至少一个分组并将所述至少一个分组缓存到所述压缩数据缓存区,其中,每个分组中包含的压缩块具有相同的分组标识,所述分组标识包括资源使用行为相近的进程组标识、进程标识、应用标识或者访问时间;所述将所述n个压缩块中至少一个压缩块组织成m个写入单位包括:将同一个分组中的压缩块组织成至少一个写入单位。
- 根据权利要求3所述的方法,其特征在于,在所述将所述n个压缩块中至少一个压缩块组织成m个写入单位之前还包括:根据第二指示选择所述至少一个分组中的部分分组,所述第二指示包括所述分组标识,所述分组标识包括资源使用行为相近的进程组标识、进程标识、应用标识或者访问时间,所述第二指示是操作系统发送的或应用层的应用发送的。
- 根据权利要求4所述的方法,其特征在于,所述第二指示还包括压缩块数量或换出数据量,所述压缩数量或换出数据量指示所述分组标识指示的分组中的待换出的压缩块的数量或数据量。
- 根据权利要求1-5中任意一项所述的方法,其特征在于,所述将所述n个压缩块中至少一个压缩块组织成m个写入单位包括:将所述n个压缩块中至少一个压缩块按压缩块从大到小的顺序组织成m个写入单位。
- 根据权利要求1-5中任意一项所述的方法,其特征在于,所述将所述n个压缩块中至少一个压缩块组织成m个写入单位包括:将所述n个压缩块中至少一个压缩块按压缩块近期最少使用排序后从冷到热的顺序组织成m个写入单位。
- 根据权利要求1-7中任意一项所述的方法,其特征在于,在所述将所述m个写入单位分最多m次写入到非易失存储设备的交换区之前包括:建立映射表,所述映射表包含所述至少一个压缩块的页面标识符和所述至少一个压缩块的交换区地址的对应关系,其中,所述页面标识符为所述至少一个压缩块所对应的至少一个待换出页面的标识符,所述交换区地址为分配给所述至少一个压缩块的一段属于所述交换区的空间的地址,所述映射表用于页面换入时通过所述页面标识符获取所述交换区地址。
- 根据权利要求8所述的方法,其特征在于,还包括:根据被请求换入的页面的页面标识符从所述映射表获取所述被请求换入的页面的页面标识符对应的交换区地址;从获取的所述交换区地址中读取压缩块,并将所述压缩块存入所述压缩数据缓存区。
- 根据权利要求8所述的方法,其特征在于,还包括:根据被请求换入的页面的页面标识符从所述映射表获取所述被请求换入的页面的页面标识符对应的交换区地址;从获取的所述交换区地址中读取所述压缩块所在的写入单位,并将所述写入单位存入所述压缩数据缓存区。
- 根据权利要求10所述的方法,其特征在于,还包括:标记所述写入单位的状态为正在换入。
- 一种内存交换装置,其特征在于,包括选择模块、压缩模块、组织模块和交换模块:所述选择模块:用于选择n个待换出页面,所述n为大于0的整数;所述压缩模块:用于将所述n个待换出页面压缩成n个压缩块,并将所述n个压缩块缓存到压缩数据缓存区;所述组织模块:用于将所述n个压缩块中的至少一个压缩块组织成m个写入单位,所述m为小于或等于n且大于0的整数,所述写入单位的大小为整数个页面;所述交换模块:用于将所述m个写入单位分最多m次写入到非易失存储设备的交换区,其中至少一个写入单位存储在所述交换区的一段连续空间。
- 根据权利要求12所述的装置,其特征在于,所述选择模块具体用于:接收第一指示,所述第一指示中包含分组标识,所述分组标识包括资源使用行为相近的进程组标识、进程标识、应用标识或者访问时间,所述第一指示是操作系统发送的或应用层的应用发送的;选择具有所述分组标识的n个待换出页面。
- 根据权利要求12或13所述的装置,其特征在于:所述压缩模块具体用于:根据分组标识将所述n个压缩块分成至少一个分组并将所述至少一个分组缓存到所述压缩数据缓存区,其中,每个分组中包含的压缩块具有相同的分组标识,所述分组标识包括资 源使用行为相近的进程组标识、进程标识、应用标识或者访问时间;所述组织模块具体用于:将同一个分组中的压缩块组织成至少一个写入单位。
- 根据权利要求14所述的装置,其特征在于,所述组织模块具体用于:根据第二指示选择所述至少一个分组中的部分分组,所述第二指示包括所述分组标识,所述分组标识包括资源使用行为相近的进程组标识、进程标识、应用标识或者访问时间,所述第二指示是操作系统发送的或应用层的应用发送的。
- 根据权利要求15所述的装置,其特征在于,所述第二指示还包括压缩块数量或换出数据量,所述压缩数量或换出数据量指示所述分组标识指示的分组中的待换出的压缩块的数量或数据量。
- 根据权利要求12-16所述的装置,其特征在于,所述组织模块具体用于:将所述n个压缩块中至少一个压缩块按压缩块从大到小的顺序组织成m个写入单位。
- 根据权利要求12-16所述的装置,其特征在于,所述组织模块具体用于:将所述n个压缩块中至少一个压缩块按压缩块近期最少使用排序后从冷到热的顺序组织成m个写入单位。
- 根据权利要求12-18所述的装置,其特征在于,所述交换模块具体用于:建立映射表,所述映射表包含所述至少一个压缩块的页面标识符和所述至少一个压缩块的交换区地址的对应关系,其中,所述页面标识符为所述至少一个压缩块所对应的至少一个待换出页面的标识符,所述交换区地址为分配给所述至少一个压缩块的一段属于所述交换区的空间的地址,所述映射表用于页面换入时通过所述页面标识符获取所述交换区地址。
- 根据权利要求19所述的装置,其特征在于,还包括页面换入模块:所述页面换入模块用于:根据被请求换入的页面的页面标识符从所述映射表获取所述被请求换入的页面的页面标识符对应的交换区地址;从获取的所述交换区地址中读取压缩块,并将所述压缩块存入所述压缩数据缓存区。
- 根据权利要求19所述的装置,其特征在于,还包括页面换入模块:所述页面换入模块用于:根据被请求换入的页面的页面标识符从所述映射表获取所述被请求换入的页面的页面标识符对应的交换区地址;从获取的所述交换区地址中读取所述压缩块所在的写入单位,并将所述写入单位存入所述压缩数据缓存区。
- 根据权利要求21所述的装置,其特征在于,所述页面换入模块具体用于:标记所述写入单位的状态为正在换入。
- 一种终端,其特征在于,所述计算机设备包括处理器和存储器,所述存储器用于存储软件程序,所述处理器用于读取存储在存储器的软件程序并执行所述权利要求1-11任意一项的方法。
- 一种存储介质,其特征在于,包括计算机程序,所述计算机程序当在一个或多个处理器上运行的时候用于实现所述权利要求1-11任意一项的方法。
- 一种计算机程序产品,其特征在于,包括计算机程序,所述计算机程序当在一个或多个处理器上运行的时候用于实现所述权利要求1-11任意一项的方法。
Priority Applications (3)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| EP25207078.4A EP4711937A2 (en) | 2020-01-20 | 2020-09-07 | Memory swapping method and apparatus |
| EP20915435.0A EP4109280B1 (en) | 2020-01-20 | 2020-09-07 | Memory swapping method and apparatus |
| US17/867,880 US11928359B2 (en) | 2020-01-20 | 2022-07-19 | Memory swapping method and apparatus |
Applications Claiming Priority (2)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202010066109.0A CN113138941B (zh) | 2020-01-20 | 2020-01-20 | 内存交换的方法、装置 |
| CN202010066109.0 | 2020-01-20 |
Related Child Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| US17/867,880 Continuation US11928359B2 (en) | 2020-01-20 | 2022-07-19 | Memory swapping method and apparatus |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| WO2021147330A1 true WO2021147330A1 (zh) | 2021-07-29 |
Family
ID=76808876
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| PCT/CN2020/113676 Ceased WO2021147330A1 (zh) | 2020-01-20 | 2020-09-07 | 内存交换的方法、装置 |
Country Status (4)
| Country | Link |
|---|---|
| US (1) | US11928359B2 (zh) |
| EP (2) | EP4711937A2 (zh) |
| CN (2) | CN113138941B (zh) |
| WO (1) | WO2021147330A1 (zh) |
Families Citing this family (5)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US11698859B2 (en) * | 2019-12-27 | 2023-07-11 | Sk Hynix Nand Product Solutions Corp. | Direct map memory extension for storage class memory |
| US11868633B2 (en) * | 2021-08-06 | 2024-01-09 | Micron Technology, Inc. | Smart swapping and effective encoding of a double word in a memory sub-system |
| CN117573575A (zh) * | 2022-08-08 | 2024-02-20 | 中兴通讯股份有限公司 | 一种内存交换处理方法、装置、存储介质及电子装置 |
| US12248706B2 (en) * | 2022-10-26 | 2025-03-11 | SanDisk Technologies, Inc. | Data storage device and method for reducing flush latency |
| KR102561316B1 (ko) * | 2023-03-20 | 2023-07-31 | 메티스엑스 주식회사 | 전자 장치 및 이를 포함하는 컴퓨팅 시스템 |
Citations (6)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN101853219A (zh) * | 2010-04-30 | 2010-10-06 | 浙江大学 | 基于虚拟化平台的磁盘页面换入的方法 |
| CN102831069A (zh) * | 2012-06-30 | 2012-12-19 | 华为技术有限公司 | 内存处理方法、内存管理设备 |
| CN103729305A (zh) * | 2012-10-11 | 2014-04-16 | 财团法人工业技术研究院 | 虚拟机的存储器管理的方法和计算机系统 |
| CN104461393A (zh) * | 2014-12-09 | 2015-03-25 | 华中科技大学 | 一种闪存存储器的混合映射方法 |
| CN106030547A (zh) * | 2014-02-21 | 2016-10-12 | 微软技术许可有限责任公司 | 修改的存储器压缩 |
| US20190146912A1 (en) * | 2017-11-16 | 2019-05-16 | Micron Technology, Inc. | Namespace Change Propagation in Non-Volatile Memory Devices |
Family Cites Families (13)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US9195581B2 (en) | 2011-07-01 | 2015-11-24 | Apple Inc. | Techniques for moving data between memory types |
| US8832411B2 (en) * | 2011-12-14 | 2014-09-09 | Microsoft Corporation | Working set swapping using a sequentially ordered swap file |
| US10565099B2 (en) | 2012-12-28 | 2020-02-18 | Apple Inc. | Methods and apparatus for compressed and compacted virtual memory |
| CN104281528B (zh) * | 2013-07-09 | 2018-02-16 | 浙江大华技术股份有限公司 | 一种数据存储方法及装置 |
| US9772776B2 (en) * | 2014-02-27 | 2017-09-26 | Red Hat Israel, Ltd. | Per-memory group swap device |
| US10572378B2 (en) | 2014-03-20 | 2020-02-25 | Hewlett Packard Enterprise Development Lp | Dynamic memory expansion by data compression |
| CN105592108B (zh) | 2014-10-20 | 2019-03-26 | 新华三技术有限公司 | 在嵌入式系统中应用静态网站的方法及装置 |
| US9977598B2 (en) | 2014-10-27 | 2018-05-22 | Mediatek Inc. | Electronic device and a method for managing memory space thereof |
| KR101995433B1 (ko) * | 2017-09-13 | 2019-09-24 | 엘지전자 주식회사 | 디지털 디바이스 및 그 제어 방법 |
| CN109508301B (zh) * | 2017-09-14 | 2021-10-29 | 中国移动通信集团重庆有限公司 | 终端、应用数据的处理方法、数据处理设备及存储介质 |
| US20190370009A1 (en) * | 2018-06-03 | 2019-12-05 | Apple Inc. | Intelligent swap for fatigable storage mediums |
| US11392491B2 (en) * | 2018-06-27 | 2022-07-19 | Intel Corporation | Hardware-assisted paging mechanisms |
| CN110674051A (zh) * | 2019-09-24 | 2020-01-10 | 中国科学院微电子研究所 | 一种数据存储方法及装置 |
-
2020
- 2020-01-20 CN CN202010066109.0A patent/CN113138941B/zh active Active
- 2020-01-20 CN CN202510052819.0A patent/CN120104515A/zh active Pending
- 2020-09-07 EP EP25207078.4A patent/EP4711937A2/en active Pending
- 2020-09-07 WO PCT/CN2020/113676 patent/WO2021147330A1/zh not_active Ceased
- 2020-09-07 EP EP20915435.0A patent/EP4109280B1/en active Active
-
2022
- 2022-07-19 US US17/867,880 patent/US11928359B2/en active Active
Patent Citations (6)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN101853219A (zh) * | 2010-04-30 | 2010-10-06 | 浙江大学 | 基于虚拟化平台的磁盘页面换入的方法 |
| CN102831069A (zh) * | 2012-06-30 | 2012-12-19 | 华为技术有限公司 | 内存处理方法、内存管理设备 |
| CN103729305A (zh) * | 2012-10-11 | 2014-04-16 | 财团法人工业技术研究院 | 虚拟机的存储器管理的方法和计算机系统 |
| CN106030547A (zh) * | 2014-02-21 | 2016-10-12 | 微软技术许可有限责任公司 | 修改的存储器压缩 |
| CN104461393A (zh) * | 2014-12-09 | 2015-03-25 | 华中科技大学 | 一种闪存存储器的混合映射方法 |
| US20190146912A1 (en) * | 2017-11-16 | 2019-05-16 | Micron Technology, Inc. | Namespace Change Propagation in Non-Volatile Memory Devices |
Non-Patent Citations (1)
| Title |
|---|
| See also references of EP4109280A4 * |
Also Published As
| Publication number | Publication date |
|---|---|
| US20220350531A1 (en) | 2022-11-03 |
| EP4109280C0 (en) | 2025-11-26 |
| CN120104515A (zh) | 2025-06-06 |
| EP4711937A2 (en) | 2026-03-18 |
| CN113138941A (zh) | 2021-07-20 |
| EP4109280A4 (en) | 2023-10-25 |
| CN113138941B (zh) | 2025-01-24 |
| EP4109280A1 (en) | 2022-12-28 |
| US11928359B2 (en) | 2024-03-12 |
| EP4109280B1 (en) | 2025-11-26 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| US11531625B2 (en) | Memory management method and apparatus | |
| US11928359B2 (en) | Memory swapping method and apparatus | |
| US11593186B2 (en) | Multi-level caching to deploy local volatile memory, local persistent memory, and remote persistent memory | |
| EP4134829B1 (en) | Page swapping method, storage system, and electronic device | |
| US9792227B2 (en) | Heterogeneous unified memory | |
| KR102077149B1 (ko) | 메모리 관리 방법 및 장치 | |
| EP3974969B1 (en) | System startup method and related device | |
| JP2009525555A (ja) | 装置の非アクティブな期間の間、dramの未使用部分のリフレッシュを動作不能にすることによって電力消費量を低減すること | |
| US12293078B2 (en) | Storage space organization method and electronic device | |
| CN111309267B (zh) | 存储空间的分配方法、装置、存储设备及存储介质 | |
| CN110554837A (zh) | 易疲劳存储介质的智能交换 | |
| CN116089321A (zh) | 内存管理方法、装置、电子装置和存储介质 | |
| US12566561B2 (en) | Super block management method and apparatus | |
| EP4354305B1 (en) | Data exchange method and apparatus | |
| US9772776B2 (en) | Per-memory group swap device | |
| CN111078407B (zh) | 内存管理的方法、装置、存储介质及电子设备 | |
| CN119904347B (zh) | 显存管理方法、系统、芯片及电子设备 | |
| EP4742047A1 (en) | Method for managing shared cache, and electronic device | |
| CN121680722A (zh) | 内存调度方法、装置、电子设备及存储介质 | |
| CN121705197A (zh) | 一种基于混合颗粒度的地址映射管理方法及系统 | |
| CN119917025A (zh) | 数据写入方法、装置、电子设备和可读存储介质 | |
| CN119988034A (zh) | 内存分配方法、装置、电子设备及可读存储介质 | |
| WO2022021337A1 (zh) | 闪存控制方法和装置 |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| 121 | Ep: the epo has been informed by wipo that ep was designated in this application |
Ref document number: 20915435 Country of ref document: EP Kind code of ref document: A1 |
|
| NENP | Non-entry into the national phase |
Ref country code: DE |
|
| ENP | Entry into the national phase |
Ref document number: 2020915435 Country of ref document: EP Effective date: 20220822 |
|
| WWG | Wipo information: grant in national office |
Ref document number: 2020915435 Country of ref document: EP |