A cache garbage collection method and storage medium thereof
Through the call determination of blockchain list, the data erasing and recycling process of NAND Flash memory is optimized, the data delay and system complexity problems in the prior art are solved, and the processing performance and storage space utilization of the memory are improved.
Patent Information
- Application Number
- CN202510067048.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-16
- Publication Date
- 2025-04-08
- Estimated Expiration
- 2045-01-16
AI Technical Summary
Existing NAND Flash memory requires frequent erasing of data before writing operations, resulting in increased system complexity and data delays, and generates junk data to occupy storage space and reduce performance.
Through call determination between multiple blockchain lists, blocks that really need to be erased first are accurately determined, thereby limiting the optional range of blocks that need to be urgently recovered, reducing the recycling and processing of different blocks during the read and write process, reducing the delay in data response, and improving the processing performance of NAND Flash memory.
It realizes that when different blocks are not frequently recycled, data response delay is reduced, NAND Flash memory processing performance is improved, and the storage space occupied by junk data is reduced.
Smart Images

Figure CN119473931B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer cache, and in particular to a cache garbage collection method and a storage medium thereof. Background Art
[0002] With the development of the Internet of Things, smart homes and mobile Internet, the popularity of embedded systems and mobile devices continues to increase. These devices usually need to use Flash storage to store data and programs. Flash storage has the advantages of high speed, low power consumption and reliability, so it has been widely used in embedded and mobile devices. However, the management of Flash storage has challenges in different hardware platforms and operating system environments, such as complex data erasure, data management, memory life management and performance optimization.
[0003] In the prior art, NAND Flash memory needs to frequently erase the original data before performing a new write operation each time. This erasing process is relatively slow and needs to be performed before the data is written, which may increase system complexity and data latency. In addition, garbage data will be generated during data deletion and writing operations. This garbage data will occupy storage space and reduce performance. Summary of the invention
[0004] The main purpose of the embodiments of the present application is to propose a cache garbage recovery method and a storage medium thereof, which accurately determines the blocks that really need to erase data first through call judgment between multiple blockchain tables, thereby limiting the optional range of blocks that need to be urgently recovered, eliminating the need to frequently recycle different blocks during the reading and writing process, reducing the latency of data response, and improving the processing performance of NAND Flash memory.
[0005] To achieve the above object, a first aspect of an embodiment of the present application proposes a cache garbage collection method, comprising:
[0006] In response to the cache read and write operation being completed, a first blockchain table is obtained, and it is determined whether the first blockchain table is an empty chain table, the first blockchain table being used to store blocks that are determined to contain invalid pages in a scenario where the flash memory triggers power failure recovery or blocks that have read errors in a scenario where flash memory data is read;
[0007] When the first blockchain list is not an empty list, extract the first target block at the head of the first blockchain list, and determine whether the first target block is in use;
[0008] When the first target block is in use, the first target block is placed at the end of the first blockchain list;
[0009] When the first target block is not in use, determining whether the first target block is stored in the second blockchain table, the second blockchain table being used to store blocks containing invalid pages in the cache partition;
[0010] When the second blockchain table stores the first target block, all physical pages containing valid data in the first target block are recovered, and after all physical pages containing valid data in the first target block are recovered, the data in the first target block is erased.
[0011] Further, in some embodiments, determining whether the first target block is in use includes:
[0012] Get the second target block currently in use;
[0013] determining whether the physical address of the first target block is consistent with the physical address of the second target block;
[0014] When the physical address of the first target block is consistent with the physical address of the second target block, determining that the first target block is in use;
[0015] When the physical address of the first target block is inconsistent with the physical address of the second target block, it is determined that the first target block is not in use.
[0016] Furthermore, in some embodiments, reclaiming all physical pages containing valid data in the first target block includes:
[0017] Deleting the first target block in the second blockchain table;
[0018] According to the last physical page of the first target block, a first mapping array is constructed, wherein each array element of the first mapping array stores a first logical page address corresponding to each physical page of the first target block;
[0019] Sequentially obtain the first physical page address corresponding to each first logical page address in the logical page mapping table and the second physical page address corresponding to each physical page in the first target block in the flash memory, and determine the physical page whose corresponding second physical page address is equal to the first physical page address in the first target block as the first valid page, where the first valid page is used to represent the physical page containing valid data in the first target block;
[0020] Reading the corresponding first user data from the flash memory in sequence according to each first valid page, writing each first user data into the storage space of the second target block, increasing the number of invalid pages of the first target block by one, updating each third physical page address in the logical page mapping table, and writing each first logical page address into the second mapping array, when all physical pages of the second target block complete the writing operation, writing the second mapping array into the last physical page of the second target block, the first user data is the user data of the first valid page, and the third physical page address is used to indicate each physical page address where each first user data is written into the second target block;
[0021] According to the total number of invalid pages in the first target block, all valid pages are recovered in the first target block, and the first target block is placed in the corresponding sequence position of the second blockchain table;
[0022] The third target block is searched and extracted in the second blockchain table, and the third target block is placed in the sequence position corresponding to the third blockchain table. All physical pages in the third target block are determined to be invalid pages. The third blockchain table is used to store blocks in an idle state.
[0023] Further, in some embodiments, erasing data in the first target block includes:
[0024] Determine whether the third blockchain table stores the first target block;
[0025] When the third blockchain table stores the first target block, the first target block is deleted from the third blockchain table, and data in the first target block is erased.
[0026] Furthermore, in some embodiments, before writing each first user data into the storage space of the second target block, the cache garbage collection method further includes:
[0027] Calculate the total number of invalid pages belonging to the first target block in the logical page mapping table according to the first mapping array to obtain a first number value;
[0028] Counting the total number of pages determined to be invalid in the first target block to obtain a second number value;
[0029] determining whether the first quantity value is equal to the second quantity value;
[0030] When the first quantity value is not equal to the second quantity value, a log warning message is output.
[0031] Further, in some embodiments, before obtaining the first blockchain table, the cache garbage collection method further includes:
[0032] Counting the total number of blocks in the cache partition that are in an idle state to obtain a fourth quantity value;
[0033] When the fourth quantity value is within the preset recycling interval, garbage collection is performed on the fourth target block, and the fourth target block is stored in the head of the second blockchain table;
[0034] The garbage collection process for the fourth target block includes the following steps:
[0035] Counting the total number of all physical pages in the fourth target block to obtain a fifth number value;
[0036] Counting the total number of pages determined to be invalid in the fourth target block to obtain a sixth number value;
[0037] Determining the number of pages to be reclaimed in the fourth target block according to a ratio between the fifth quantity value and the sixth quantity value;
[0038] According to the number of pages to be recycled, the pages to be recycled in the fourth target block are recycled, and after the recycling of the pages to be recycled in the fourth target block is completed, the data in the fourth target block is erased.
[0039] Further, in some embodiments, reclaiming the to-be-reclaimed pages in the fourth target block includes:
[0040] Extract the fourth target block from the head of the second blockchain table;
[0041] According to the last physical page of the fourth target block, a third mapping array is constructed, wherein each array element of the third mapping array corresponds to and stores the second logical page address corresponding to each physical page of the fourth target block;
[0042] Sequentially obtain the fourth physical page address in the logical page mapping table corresponding to each second logical page address and the fifth physical page address in the flash memory corresponding to each to-be-reclaimed page in the first target block, and determine the physical page in the fourth target block whose corresponding fifth physical page address is equal to the fourth physical page address as the second valid page, where the second valid page is used to indicate the to-be-reclaimed page in the fourth target block that contains valid data;
[0043] Reading corresponding second user data from the flash memory in sequence according to each second valid page, writing each second user data into the storage space of the second target block, increasing the number of invalid pages of the fourth target block by one, updating each sixth physical page address in the logical page mapping table, and writing each second logical page address into the second mapping array, when all physical pages of the second target block complete the write operation, writing the second mapping array into the last physical page of the second target block, the second user data is the user data of the second valid page, and the sixth physical page address is used to indicate each physical page address where each second user data is written into the second target block;
[0044] According to the sixth quantity value, reclaiming valid pages in the fourth target block in a number equal to the sixth quantity value, and placing the fourth target block in a sequence position corresponding to the second blockchain table;
[0045] The third target block is searched and extracted in the second blockchain table, and the third target block is placed in the sequence position corresponding to the third blockchain table, wherein all physical pages in the third target block are determined to be invalid pages, and the third blockchain table is used to store blocks in an idle state.
[0046] Furthermore, in some embodiments, before writing each second user data into the storage space of the second target block, the cache garbage collection method further includes:
[0047] Calculate the total number of invalid pages belonging to the fourth target block in the logical page mapping table according to the second mapping array to obtain a seventh number value;
[0048] determining whether the sixth quantity value is equal to the seventh quantity value;
[0049] When the sixth quantity value is not equal to the seventh quantity value, a log warning message is output.
[0050] Further, in some embodiments, erasing data in the fourth target block includes:
[0051] When a write operation is performed on the head block of the third blockchain table, determining whether the head block of the third blockchain table is a fourth target block;
[0052] When the head block of the third blockchain table is the fourth target block, the data in the fourth target block is erased.
[0053] To achieve the above-mentioned purpose, the second aspect of an embodiment of the present application proposes a storage medium, which is a computer-readable storage medium. The storage medium stores a computer program, and when the computer program is executed by a processor, the cache garbage collection method of the above-mentioned first aspect embodiment is implemented.
[0054] In the embodiments of the present application, the following beneficial effects are achieved: the present application obtains a first blockchain list in response to the completion of a cache read and write operation, and determines whether the first blockchain list is an empty list, wherein the first blockchain list is used to store blocks that are determined to contain invalid pages in a scenario where a flash memory triggers power failure recovery, or blocks that have read errors in a scenario where flash memory data is read; when the first blockchain list is not an empty list, extract the first target block at the head of the first blockchain list, and determine whether the first target block is in use; when the first target block is in use, place the first target block at the end of the first blockchain list; when the first target block is not in use, determine whether the second blockchain list stores a The first target block and the second blockchain table are used to store blocks containing invalid pages in the cache partition; when the second blockchain table stores the first target block, all physical pages containing valid data in the first target block are recovered, and after all physical pages containing valid data in the first target block are recovered, the data in the first target block is erased, so that the blocks that really need to be erased first can be accurately determined through the call judgment between the first blockchain table and the second blockchain table, further limiting the optional range of blocks that need to be urgently recovered, eliminating the need to frequently recover different blocks during the reading and writing process, reducing the latency of data response, and improving the processing performance of the NAND Flash memory. BRIEF DESCRIPTION OF THE DRAWINGS
[0055] Figure 1 is a flow chart of a cache garbage collection method provided by some embodiments of the present application;
[0056] Figure 2 is a flowchart of determining whether a first target block is in use provided by some embodiments of the present application;
[0057] Figure 3 is a flowchart of reclaiming all physical pages in a first target block provided by some embodiments of the present application;
[0058] Figure 4 is a schematic diagram of block operations of a second blockchain table provided in some embodiments of the present application;
[0059] Figure 5 is a schematic diagram of merging data of a first target block into a second target block provided by some embodiments of the present application;
[0060] Figure 6 is a flowchart of erasing data in a first target block provided by some embodiments of the present application;
[0061] Figure 7 Some embodiments of the present application provide Figure 3 The flowchart before step S304;
[0062] Figure 8 This is a flowchart before obtaining the first blockchain table provided in some embodiments of the present application;
[0063] Fig. 9 is a schematic diagram of blocks in a cache partition provided by some embodiments of the present application;
[0064] Fig.10 Some embodiments of the present application provide Figure 8 Flowchart of step S802 in FIG.
[0065] Fig.11 is a schematic diagram of determining pages to be recycled in a block provided by some embodiments of the present application;
[0066] Fig.12 is a flowchart of reclaiming to-be-reclaimed pages in the fourth target block provided by some embodiments of the present application;
[0067] Fig.13 Some embodiments of the present application provide Fig.12 Flow chart of step S1204;
[0068] Fig.14 is a flowchart of erasing data in a fourth target block provided by some embodiments of the present application;
[0069] Fig.15 A schematic diagram of the structure of a cache garbage collection system provided in some embodiments of the present application;
[0070] Fig.16 It is a schematic diagram of the hardware structure of an electronic device provided in some embodiments of the present application. DETAILED DESCRIPTION
[0071] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0072] In the description of the present application, it should be understood that descriptions involving orientation, such as up, down, front, back, left, right, etc., indicating orientations or positional relationships, are based on the orientations or positional relationships shown in the accompanying drawings, and are only for the convenience of describing the present application and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore should not be understood as a limitation on the present application.
[0073] It should also be noted that in the description of this application, "several" means more than one, "more" means more than two, "greater than", "less than", "exceed", etc. are understood to exclude the number itself, and "above", "below", "within", etc. are understood to include the number itself. If there is a description of "first" or "second", it is only used to distinguish the technical features, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features or implicitly indicating the order of the indicated technical features.
[0074] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used herein are only for the purpose of describing the embodiments of this application and are not intended to limit this application.
[0075] In the description of the present application, the description with reference to the terms "one embodiment", "some embodiments", "illustrative embodiments", "examples", "specific examples", or "some examples" means that the specific features, structures, materials, or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present application. In this specification, the schematic representation of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials, or characteristics described may be combined in any one or more embodiments or examples in a suitable manner.
[0076] First, some nouns involved in this application are analyzed:
[0077] NAND Flash memory is a non-volatile memory widely used to store data, which will not be lost even after power failure. With its high density, low cost and high write / erase performance, it has become the core technology of devices such as solid-state drives (SSDs), USB flash drives, and SD cards. Among them, NAND Flash uses block-level operations when writing and erasing data.
[0078] A linked list is a data structure used to store a series of linear data elements. Unlike an array, the elements in a linked list are not stored continuously, but each element is linked together through pointers or references. The basic component of a linked list is a node, and each node consists of two parts: a cache space (used to store specific data) and a pointer space (used to store a pointer to the next node or a pointer to the previous node).
[0079] Block is one of the basic units of NAND Flash memory. Each block consists of multiple pages. Usually, a page is the smallest read and write unit, and a block is the smallest erase unit.
[0080] In the prior art, NAND Flash memory needs to frequently erase the original data before performing a new write operation each time. This erasing process is relatively slow and needs to be performed before the data is written, which may increase system complexity and data latency. In addition, garbage data will be generated during data deletion and writing operations. This garbage data will occupy storage space and reduce performance.
[0081] Based on this, the embodiment of the present application provides a cache garbage collection method and a storage medium thereof, which obtains a first blockchain list in response to the completion of the cache read and write operation, and determines whether the first blockchain list is an empty list, and the first blockchain list is used to store blocks that are determined to contain invalid pages in the scenario where the flash memory triggers power failure recovery or blocks that have read errors in the scenario where the flash memory data is read; when the first blockchain list is not an empty list, extract the first target block at the head of the first blockchain list, and determine whether the first target block is in use; when the first target block is in use, put the first target block at the end of the first blockchain list; when the first target block is not in use, determine Whether the second blockchain table stores the first target block, the second blockchain table is used to store blocks containing invalid pages in the cache partition; when the second blockchain table stores the first target block, all physical pages containing valid data in the first target block are recovered, and after all physical pages containing valid data in the first target block are recovered, the data in the first target block is erased, so that the blocks that really need to be erased first can be accurately determined through the call judgment between the first blockchain table and the second blockchain table, further limiting the optional range of blocks that need to be urgently recovered, eliminating the need to frequently recover different blocks during the reading and writing process, reducing the latency of data response, and improving the processing performance of the NAND Flash memory.
[0082] A cache garbage collection method and a storage medium thereof provided in an embodiment of the present application are specifically described through the following embodiments.
[0083] In the first aspect, a cache garbage collection method in an embodiment of the present application is first described.
[0084] Reference Figure 1 As shown, Figure 1 It is a flowchart of a cache garbage collection method provided in some embodiments of the present application. The cache garbage collection method may include but is not limited to steps S101 to S105.
[0085] Step S101: In response to the completion of the cache read and write operations, a first blockchain table is obtained, and it is determined whether the first blockchain table is an empty chain table.
[0086] Among them, the first blockchain table is used to store blocks that are determined to contain invalid pages in the scenario where the flash memory triggers power failure recovery or blocks that have read errors in the scenario of reading flash memory data.
[0087] In a possible implementation, in response to the completion of the cache read and write operation, it means that the cache system has completed writing or reading data to the NAND Flash, and then needs to update the data in the cache, then obtain the first blockchain table, and determine whether the first blockchain table is an empty linked list, that is, determine whether the rear drive pointer of the head node in the first blockchain table is null.
[0088] Step S102: When the first blockchain list is not an empty list, extract the first target block at the head of the first blockchain list, and determine whether the first target block is in use.
[0089] Among them, the first target block is the block pointed to by the follower pointer of the head node in the first blockchain table.
[0090] In a possible implementation, when the first blockchain table is not an empty linked list, that is, the rear-drive pointer of the head node in the first blockchain table is not null, the first target block is extracted from the head of the first blockchain table, that is, the front-drive pointer and the rear-drive pointer of the first target block are both set to null, and then, the block pointed to by the rear-drive pointer of the original first target block is set as the new head block, the front-drive pointer of the new head block is pointed to the head node, and the rear-drive pointer of the head node pointer is pointed to the new head block, and finally, it is determined whether the first target block is in use.
[0091] Step S103: When the first target block is in use, the first target block is placed at the end of the first blockchain table.
[0092] In a possible implementation, when the first target block is in use, the first target block is placed at the end of the first blockchain list, that is, the rear pointer of the last block of the first blockchain list points to the first target block, and the front pointer of the first target block points to the last block of the first blockchain list.
[0093] Step S104: When the first target block is not in use, determining whether the second blockchain table stores the first target block.
[0094] The second blockchain table is used to store blocks containing invalid pages in the cache partition.
[0095] Specifically, when the first target block is not in use, it is determined whether the second blockchain table stores the first target block, that is, whether the first target block is found in the second blockchain table.
[0096] Step S105: When the second blockchain table stores the first target block, all physical pages containing valid data in the first target block are recovered, and after all physical pages containing valid data in the first target block are recovered, the data in the first target block is erased.
[0097] Specifically, when the second blockchain table stores the first target block, all physical pages containing valid data in the first target block are recovered, and after all physical pages containing valid data in the first target block are recovered, the data in the first target block is erased.
[0098] Reference Figure 2 As shown, Figure 2 It is a flowchart of determining whether the first target block is in use provided by some embodiments of the present application. The method of determining whether the first target block is in use may include but is not limited to steps S201 to S205.
[0099] Step S201: Acquire the second target block currently in use.
[0100] In a possible implementation, a target block currently in use is obtained from the cache as the second target block.
[0101] Step S202: Determine whether the physical address of the first target block is consistent with the physical address of the second target block.
[0102] In a possible implementation, it is determined whether the physical address of the first target block is consistent with the physical address of the second target block.
[0103] Step S203: When the physical address of the first target block is consistent with the physical address of the second target block, it is determined that the first target block is in use.
[0104] In a possible implementation, when the physical address of the first target block is consistent with the physical address of the second target block, it is determined that the first target block is in use.
[0105] Step S204: When the physical address of the first target block is inconsistent with the physical address of the second target block, it is determined that the first target block is not in use.
[0106] In a possible implementation, when the physical address of the first target block is inconsistent with the physical address of the second target block, it is determined that the first target block is not in use.
[0107] Reference Figure 3 As shown, Figure 3A flowchart of recovering all physical pages containing valid data in a first target block provided by some embodiments of the present application, wherein the method of recovering all physical pages containing valid data in a first target block may include but is not limited to steps S301 to S306.
[0108] Step S301: Delete the first target block in the second blockchain table.
[0109] In a possible implementation, a head node in the second blockchain table is determined, and the head node is used as a starting point to traverse each block in the second blockchain table in sequence according to the direction of the rear-drive pointer of the head node. If the currently traversed block is consistent with the first target block, the position of the currently traversed block in the second blockchain table is determined to be X, and then the first target block is deleted, that is, the front-drive pointer and the rear-drive pointer of the currently traversed block are set to null, and then the rear-drive pointer of the block at position X-1 is pointed to the block at position X+1, and the front-drive pointer of the block at position X+1 is pointed to the block at position X-1.
[0110] Step S302: construct a first mapping array according to the last physical page of the first target block.
[0111] Each array element of the first mapping array stores a first logical page address corresponding to each physical page of the first target block, and the array length of the first mapping array is equal to the total number of physical pages of the first target block-1.
[0112] It should be noted that the first logical page addresses corresponding to each physical page are stored in the last physical page of the first target block, wherein the i-th element of the first mapping array corresponds to the logical page address of the i-th physical page of the first target block.
[0113] In a possible implementation, based on the total number of physical pages of the first target block being n, an empty array with an array length of n-1 is constructed, and the first mapping array gc.map_data_1 is obtained by reading the last physical page of the first target block, where gc.map_data_1[i] represents the i-th array element in the first mapping array, and the value range of i is [0, n-1].
[0114] Step S303: sequentially obtain the first physical page address corresponding to each first logical page address in the logical page mapping table and the second physical page address corresponding to each physical page in the first target block in the flash memory, and determine the physical page in the first target block whose second physical page address is equal to its corresponding first physical page address as the first valid page.
[0115] The first valid page is used to indicate a physical page containing valid data in the first target block.
[0116] In a possible implementation, according to the logical page mapping table, each first logical page address in the first mapping array is read in the logical page mapping table, and each physical page in the first target block corresponds to a second physical page address in the flash memory, and then the second physical page address of each physical page of the first target block is compared with its corresponding first physical page address. When the second physical page address is equal to the first physical page address corresponding to the first logical page address read in the logical page mapping table, the physical page is determined to be the first valid page. For example, when the first logical page address corresponding to the i-th physical page is read, the first physical page address is taken out from the logical page mapping table, and the first physical page address corresponding to the logical page is compared with the first physical page address corresponding to the second physical page address. The physical page address includes a mapping block ID for indicating the position of the first target block and a mapping page ID for indicating the position of the page in the first target block. Then, the second physical page address corresponding to each physical page in the first target block in the flash memory is read. The second physical page address includes the block ID of the first target block and the physical page ID of the first target block. Then, the mapping block ID is compared with the block ID of the first target block, and the mapping page ID is compared with the i-th physical page ID in the first target block. If the mapping block ID is the same as the block ID of the first target block, and the mapping page ID is also the same as the i-th physical page ID in the first target block, it indicates that the i-th physical page of the first target block is the first valid page. If both are different, it indicates that the i-th physical page of the first target block is an invalid page.
[0117] Step S304: read the corresponding first user data from the flash memory in sequence according to each first valid page, write each first user data into the storage space of the second target block, increment the number of invalid pages of the first target block by one, update each third physical page address in the logical page mapping table, write each first logical page address into the second mapping array, and when all physical pages of the second target block complete the write operation, write the second mapping array to the last physical page of the second target block.
[0118] The first user data is user data of a first valid page, and the third physical page address is used to indicate each physical page address where each first user data is written into the second target block.
[0119] Specifically, the corresponding first user data is read from the flash memory in sequence according to each first valid page, and each first user data is written to the storage space of the second target block, the number of invalid pages of the first target block is incremented by one, and each third physical page address is updated in the logical page mapping table (that is, the mapping block ID and mapping page ID of each physical page corresponding to the second target block are updated), and each first logical page address is written into the second mapping array gc.map_data_2. When all physical pages of the second target block complete the write operation, the second mapping array gc.map_data_2 is written to the last physical page of the second target block.
[0120] It should be noted that the user data stored in the flash memory of the invalid page in step S303 is invalid data, and the user data will not be written into the storage space of the second target block.
[0121] It should also be noted that if the i-th array element value of gc.map_data_1 is 0xFFFF, it indicates that the i-th physical page data of the first target block is not filled with user data, and the data will not be written to the storage space of the physical page of the second target block.
[0122] Step S305: According to the total number of invalid pages in the first target block, all valid pages are recovered, and the first target block is placed in the corresponding sequence position of the second blockchain table.
[0123] It should be noted that since all valid pages are recycled and the number of invalid pages increases to the total number of all physical pages in the first target block, the first target block will be placed at the head of the second blockchain table.
[0124] In one possible implementation, refer to Figure 4 As shown, Figure 4 This is a schematic diagram of the block operation of the second blockchain table provided by some embodiments of the present application. When the invalid_page_count of the first target block is 2, and the invalid_page_count of the first target block is the smallest among the invalid_page_count of each block in the second blockchain table, the first target block is placed at the second-to-last position of the second blockchain table, that is, the predecessor pointer of the last block of the second blockchain table points to the first target block, the successor pointer of the first target block points to the last block of the second blockchain table, the successor pointer of the block at the original second-to-last position points to the first target block, and the predecessor pointer of the first target block points to the block at the original second-to-last position.
[0125] In one possible implementation, referring again to Figure 4As shown, when the sixth quantity value invalid_page_count of the first target block is 32, and the invalid_page_count of the first target block is the largest among the invalid_page_count of each block in the second blockchain table, the first target block is placed at the head of the second blockchain table, that is, the rear-drive pointer of the head node of the second blockchain table points to the first target block, the rear-drive pointer of the first target block points to the block originally at the head, the front-drive pointer of the block originally at the head points to the first target block, and the front-drive pointer of the first target block points to the head node of the second blockchain table.
[0126] Among them, the blocks in the second blockchain table are arranged in order from large to small according to the invalid_page_count in each block.
[0127] At the same time, it should be noted that, referring to Figure 5 As shown, Figure 5 This is a schematic diagram of merging the data of the first target block into the second target block provided by some embodiments of the present application. In the process of placing the first target block into the sequence position corresponding to the second blockchain table, all physical page data containing valid data of the first target block will also be merged into the storage space of the physical page of the second target block, thereby realizing data recovery processing of the physical page.
[0128] Step S306: Search and extract the third target block in the second blockchain table, and place the third target block in the corresponding sequence position in the third blockchain table.
[0129] Among them, all physical pages in the third target block are determined to be invalid pages, and the third blockchain table is used to store blocks in an idle state.
[0130] In a possible implementation, the head node in the second blockchain table is determined, and the head node is used as the starting point. Each block in the second blockchain table is traversed in sequence according to the pointer pointing to the head node. If all physical pages in the currently traversed block are determined to be invalid pages, the currently traversed block is determined to be the third target block, and the position of the third target block in the second blockchain table is determined to be X. Then, the predecessor pointer and the successor pointer of the third target block are both set to null. Then, the successor pointer of the block at the X-1 position is pointed to the block at the X+1 position, and the predecessor pointer of the block at the X+1 position is pointed to the block at the X-1 position. Finally, the predecessor pointer of the third target block is pointed to the sequence position block corresponding to the third blockchain table, and the successor pointer of the sequence position block corresponding to the third blockchain table points to the third target block.
[0131] Reference Figure 6 As shown, Figure 6This is a flowchart of erasing data in a first target block provided by some embodiments of the present application. The method of erasing data in a first target block may include but is not limited to steps S601 to S602.
[0132] Step S601: Determine whether the third blockchain table stores the first target block.
[0133] In a possible implementation, a head node in the third blockchain table is determined, and with the head node as the starting point, each block in the third blockchain table is traversed in sequence according to the pointer pointing to the head node, and in each traversal, it is determined whether the currently traversed block is consistent with the first target block. If the currently traversed block is consistent with the first target block, it is determined that the third blockchain table stores the first target block. If each block in the third blockchain table is inconsistent with the first target block, it is determined that the third blockchain table does not store the first target block.
[0134] Step S602: When the third blockchain table stores the first target block, the first target block is deleted from the third blockchain table, and the data in the first target block is erased.
[0135] In a possible implementation, when the third blockchain table stores the first target block, the position of the first target block in the third blockchain table is determined to be X, and then the predecessor pointer and the successor pointer of the first target block are both set to null, and then the successor pointer of the block at position X-1 is pointed to the block at position X+1, and the predecessor pointer of the block at position X+1 is pointed to the block at position X-1.
[0136] Reference Figure 7 As shown, Figure 7 Some embodiments of the present application provide Figure 3 In the flowchart before step S304, the cache garbage collection method may include but is not limited to steps S701 to S704.
[0137] Step S701: Calculate the total number of invalid pages belonging to the first target block in the logical page mapping table according to the first mapping array to obtain a first number value.
[0138] In a possible implementation, according to the first mapping array, the invalid mark of each physical page belonging to the first target block in the logical page mapping table is calculated, and the number of invalid marks invalid_page_num is accumulated to obtain a first quantity value.
[0139] Step S702: Count the total number of invalid pages in the first target block to obtain a second number value.
[0140] In a possible implementation, in the first target block, the total number of invalid pages determined to be invalid_page_count is counted to obtain a second number value.
[0141] Step S703: Determine whether the first quantity value is equal to the second quantity value.
[0142] Specifically, it is determined whether the first quantity value is equal to the second quantity value.
[0143] Step S704: When the first quantity value is not equal to the second quantity value, output log warning information.
[0144] Specifically, when the first quantity value is not equal to the second quantity, it indicates that an invalid page in the first target block has an error, and then a log warning message is output, thereby enabling the user to quickly understand and locate the first target block where the error currently occurs.
[0145] Reference Figure 8 As shown, Figure 8 This is a flowchart before obtaining the first blockchain table provided in some embodiments of the present application. The cache garbage collection method may include but is not limited to steps S801 to S802.
[0146] Step S801: Count the total number of blocks in the cache partition that are in an idle state to obtain a fourth number value.
[0147] Specifically, the total number of free_block blocks in the cache partition that are in an idle state is counted to obtain a fourth quantity value.
[0148] It should be noted that, refer to Fig. 9 As shown, Fig. 9 This is a schematic diagram of blocks in a cache partition provided in some embodiments of the present application. A flash memory partition is divided into multiple physical blocks, one part of which is usable blocks and the other part is bad blocks. The usable blocks are further divided into unused blocks and used blocks, and the unused blocks include blocks in an idle state and blocks in other states.
[0149] Step S802: When the fourth quantity value is within the preset recycling interval, garbage collection is performed on the fourth target block.
[0150] Among them, the fourth target block is stored in the head of the second blockchain table.
[0151] Specifically, when the fourth number value is in the preset recycling interval [start_gc_free_blocks, stop_gc_free_blocks], garbage collection is performed on the fourth target block, wherein start_gc_free_blocks is the number of blocks for starting garbage collection, and stop_gc_free_blocks is the number of blocks for stopping garbage collection.
[0152] In one possible implementation, start_gc_free_blocks = (block_nums-total_blocks) / 5, stop_gc_free_blocks = (block_nums-total_blocks)*2 / 3, wherein block_nums is the total number of blocks of the flash partition, and total_blocks is the number of available blocks of the flash partition, which means that when the proportion of free blocks in the minimum remaining blocks is [1 / 5, 2 / 3], garbage collection is allowed, and then garbage collection is performed on the fourth target block.
[0153] Reference Fig.10 As shown, Fig.10 Some embodiments of the present application provide Figure 8 In the flowchart of step S802, the cache garbage collection method may include but is not limited to steps S1001 to S1004.
[0154] Step S1001: Count the total number of all physical pages in the fourth target block to obtain a fifth number value.
[0155] Specifically, the total number of all physical pages in the fourth target block, pages_per_block, is counted to obtain a fifth number value.
[0156] Step S1002: Count the total number of pages determined to be invalid in the fourth target block to obtain a sixth number value.
[0157] Specifically, the total number of invalid pages determined to be invalid pages in the fourth target block is counted to obtain a sixth number value.
[0158] Step S1003: Determine the number of pages to be reclaimed in the fourth target block according to the ratio between the fifth number value and the sixth number value.
[0159] In one possible implementation, refer to Fig.11 As shown, Fig.11Schematic diagram of determining the to-be-reclaimed pages of a block provided by some embodiments of the present application. A cache partition is divided into multiple physical blocks. The page size of a 128MB NAND Flash memory is 2kb and the block size is 128kb, that is, each block contains 64 pages. The to-be-reclaimed pages of the fourth target block are determined by the following formula:
[0160] Fifth quantity value / sixth quantity value 1 / 2 (the fifth quantity value 32), it is determined that the number of pages to be reclaimed in the fourth target block is 4;
[0161] Fifth quantity value / sixth quantity value 1 / 4 (the fifth quantity value 16), it is determined that the number of pages to be reclaimed in the fourth target block is 8;
[0162] Fifth quantity value / sixth quantity value 1 / 8 (the fifth quantity value 8), it is determined that the number of pages to be reclaimed in the fourth target block is 16;
[0163] Fifth quantity value / sixth quantity value 1 / 8 (the fifth quantity value 8), it is determined that the number of pages to be reclaimed in the fourth target block is 0xFFFF (all the physical pages of the fourth target block are pages to be reclaimed).
[0164] It should be noted that, in the above-mentioned process of reclaiming all physical pages containing valid data in the first target block, all physical pages containing valid data in the first target block are confirmed as pages to be reclaimed, and the number of reclaimed pages in the first target block is set to 0xFFFF, indicating that all valid pages are reclaimed.
[0165] Step S1004: reclaiming the pages to be reclaimed in the fourth target block according to the number of pages to be reclaimed, and after the pages to be reclaimed in the fourth target block are reclaimed, erasing the data in the fourth target block.
[0166] In a possible implementation, the to-be-reclaimed pages in the fourth target block are reclaimed, and after the to-be-reclaimed pages in the fourth target block are reclaimed, the data in the fourth target block is erased.
[0167] Reference Fig.12 As shown, Fig.12 It is a flowchart of reclaiming the to-be-reclaimed pages in the fourth target block provided by some embodiments of the present application. The method of reclaiming the to-be-reclaimed pages in the fourth target block may include but is not limited to steps S1201 to S1206.
[0168] Step S1201: Extract the fourth target block from the head of the second blockchain table.
[0169] Among them, the fourth target block is the block pointed to by the follower pointer of the head node in the second blockchain table.
[0170] In a possible implementation, the fourth target block is extracted from the head of the second blockchain table, that is, the predecessor pointer and the successor pointer of the fourth target block are both set to null, and then, the block pointed to by the successor pointer of the original fourth target block is set as the new head block, the predecessor pointer of the new head block is pointed to the head node, and the successor pointer of the head node pointer is pointed to the new head block.
[0171] Step S1202: construct a third mapping array according to the last physical page of the fourth target block.
[0172] Each array element of the third mapping array stores a second logical page address corresponding to each physical page of the fourth target block, and the array length of the third mapping array is equal to the total number of physical pages of the fourth target block-1.
[0173] It should be noted that the last physical page of the fourth target block stores the logical page addresses of the physical pages of the fourth target block, ie, the second logical page address.
[0174] In a possible implementation, based on the total number of physical pages of the fourth target block being m, an empty array with an array length of m is constructed, and the third mapping array gc.map_data_3 is obtained by reading the last physical page of the fourth target block, where gc.map_data_3[i] represents the i-th array element in the third mapping array, and the value range of i is [0, n-1].
[0175] Step S1203: sequentially obtain the fourth physical page address in the logical page mapping table corresponding to each second logical page address and the fifth physical page address in the flash memory corresponding to each to-be-reclaimed page in the first target block, and determine the physical page in the fourth target block whose corresponding fifth physical page address is equal to the fourth physical page address as the second valid page.
[0176] The second valid page is used to indicate a page to be recycled that contains valid data in the fourth target block.
[0177] In a possible implementation, according to the logical page mapping table, the fourth physical page address corresponding to each second logical page address in the first mapping array is read in the logical page mapping table, and the fifth physical page address corresponding to each physical page in the fourth target block in the flash memory is read, and then the fifth physical page address of each physical page of the fourth target block is compared with the fourth physical page address corresponding thereto. When the fifth physical page address is equal to the fourth physical page address corresponding to the second logical page address read thereto in the logical page mapping table, it is determined that the physical page is the second valid page. For example, when the second logical page address corresponding to the i-th physical page is read, the fourth physical page address is taken out from the logical page mapping table, and the fourth physical page address corresponding to the logical page is taken out from the logical page mapping table. The physical page address includes a mapping block ID for indicating the position of the fourth target block and a mapping page ID for indicating the position of the page in the fourth target block. Then, the fifth physical page address corresponding to each physical page in the fourth target block in the flash memory is read. The fifth physical page address includes the block ID of the fourth target block and the physical page ID of the fourth target block. Then, the mapping block ID is compared with the block ID of the fourth target block, and the mapping page ID is compared with the i-th physical page ID in the fourth target block. If the mapping block ID is the same as the block ID of the fourth target block, and the mapping page ID is also the same as the i-th physical page ID in the fourth target block, it indicates that the i-th physical page of the fourth target block is the second valid page. If both are different, it indicates that the i-th physical page of the fourth target block is an invalid page.
[0178] Step S1204: read the corresponding second user data from the flash memory in sequence according to each second valid page, write each second user data into the storage space of the second target block, increment the number of invalid pages of the fourth target block by one, update each sixth physical page address in the logical page mapping table, and write each second logical page address into the second mapping array; when all physical pages of the second target block complete the write operation, write the second mapping array to the last physical page of the second target block.
[0179] The second user data is user data of a second valid page, and the sixth physical page address is used to indicate each physical page address where each second user data is written into the second target block.
[0180] Specifically, the corresponding second user data is read from the flash memory in sequence according to each second valid page, and each second user data is written to the storage space of the second target block, the number of invalid pages of the fourth target block is incremented by one, and each sixth physical page address is updated in the logical page mapping table (that is, the mapping block ID and mapping page ID of each physical page corresponding to the second target block are updated), and each second logical page address is written into the second mapping array gc.map_data_2. When all physical pages of the second target block complete the write operation, the second mapping array gc.map_data_2 is written to the last physical page of the second target block.
[0181] In addition, the user data stored in the flash memory of the invalid page in step S1203 is invalid data, and the user data will not be written into the storage space of the second target block.
[0182] Step S1205: According to the sixth quantity value, a number of valid pages equal to the sixth quantity value is recovered in the fourth target block, and the fourth target block is placed in the sequence position corresponding to the second blockchain table.
[0183] It should be noted that, since only some valid pages are recycled, the fourth target block will be sorted according to the number of invalid pages and placed in the corresponding position of the second blockchain table.
[0184] In a possible implementation, when the sixth quantity value invalid_page_count of the fourth target block is 32, and the invalid_page_count of the fourth target block is the largest among the invalid_page_counts of the blocks in the second blockchain table, the fourth target block is placed at the head of the second blockchain table, that is, the rear-drive pointer of the head node of the second blockchain table points to the fourth target block, the rear-drive pointer of the fourth target block points to the block originally at the head, the front-drive pointer of the block originally at the head points to the fourth target block, and the front-drive pointer of the fourth target block points to the head node of the second blockchain table.
[0185] Step S1206: Search and extract the third target block in the second blockchain table, and place the third target block in the corresponding sequence position in the third blockchain table.
[0186] Among them, all physical pages in the third target block are determined to be invalid pages, and the third blockchain table is used to store blocks in an idle state.
[0187] In a possible implementation, the head node in the second blockchain table is determined, and the head node is used as the starting point. Each block in the second blockchain table is traversed in sequence according to the pointer pointing to the head node. If all physical pages in the currently traversed block are determined to be invalid pages, the currently traversed block is determined to be the third target block, and the position of the third target block in the second blockchain table is determined to be X. Then, the predecessor pointer and the successor pointer of the third target block are both set to null. Then, the successor pointer of the block at the X-1 position is pointed to the block at the X+1 position, and the predecessor pointer of the block at the X+1 position is pointed to the block at the X-1 position. Finally, the predecessor pointer of the third target block is pointed to the sequence position block corresponding to the third blockchain table, and the successor pointer of the sequence position block corresponding to the third blockchain table points to the third target block.
[0188] Reference Fig.13 As shown, Fig.13 Some embodiments of the present application provide Fig.12 In the flowchart of step S1204, the cache garbage collection method may include but is not limited to steps S1301 to S1303.
[0189] Step S1301: Calculate the total number of invalid pages belonging to the fourth target block in the logical page mapping table according to the second mapping array to obtain a seventh number value.
[0190] In a possible implementation, according to the second mapping array, the invalid mark of each physical page belonging to the fourth target block in the logical page mapping table is calculated, and the number of invalid marks invalid_page_num is accumulated to obtain a seventh quantity value.
[0191] Step S1302: Determine whether the sixth quantity value is equal to the seventh quantity value.
[0192] Specifically, it is determined whether the first quantity value is equal to the second quantity value.
[0193] Step S1303: When the sixth quantity value is not equal to the seventh quantity value, output a log warning message.
[0194] Specifically, when the sixth quantity value is not equal to the seventh quantity value, it indicates that an invalid page error occurs in the fourth target block, and then a log warning message is output, thereby enabling the user to quickly understand and locate the first target block where an error currently occurs.
[0195] Reference Fig.14 As shown, Fig.14 This is a flowchart of erasing data in the fourth target block provided by some embodiments of the present application. The method of erasing data in the fourth target block may include but is not limited to steps S1401 to S1402.
[0196] Step S1401: when a write operation is performed on a header block of the third blockchain table, determining whether the header block of the third blockchain table is a fourth target block.
[0197] Among them, the header block of the third blockchain table is the block pointed to by the rear-drive pointer of the head node of the third blockchain table.
[0198] Specifically, when a write operation is performed on the header block of the third blockchain table, it is determined whether the header block of the third blockchain table is the fourth target block.
[0199] Step S1402: When the header block of the third blockchain table is the fourth target block, erase the data in the fourth target block.
[0200] Specifically, when the header block of the third blockchain table is the fourth target block, the data in the fourth target block is erased.
[0201] In a second aspect, the present application embodiment also provides a cache garbage collection system, referring to Fig.15 , Fig.15 A schematic diagram of the structure of a cache garbage collection system provided in some embodiments of the present application, the cache garbage collection system 1500 includes:
[0202] The data acquisition module 1501 is used to obtain a first blockchain table in response to the completion of the cache read and write operation. The first blockchain table is used to store blocks that are determined to contain invalid pages in the scenario where the flash memory triggers power failure recovery or blocks that have read errors in the scenario where the flash memory data is read.
[0203] The blockchain table processing module 1502 is used to determine whether the first blockchain table is an empty chain list; the blockchain table processing module 1502 is also used to put the first target block at the end of the first blockchain table when the first target block is in use; the blockchain table processing module 1502 is also used to put the first target block at the end of the first blockchain table when the first target block is in use; the blockchain table processing module 1502 is also used to determine whether the second blockchain table stores the first target block when the first target block is not in use, and the second blockchain table is used to store blocks containing invalid pages in the cache partition.
[0204] The block recycling module 1503 is used to recycle all physical pages in the first target block when the second blockchain table stores the first target block, and after all physical pages in the first target block are recycled, erase the data in the first target block.
[0205] The cache garbage collection system 1500 and the cache garbage collection method are based on the same inventive concept. The above process describes the cache garbage collection system 1500 of the embodiment of the present application. In response to the completion of the cache read and write operation, the first blockchain table is obtained, and it is determined whether the first blockchain table is an empty chain list. The first blockchain table is used to store blocks that are determined to contain invalid pages in the scenario of flash memory triggering power failure recovery or blocks that have read errors in the scenario of reading flash memory data; when the first blockchain table is not an empty chain list, the first target block is extracted from the head of the first blockchain table, and it is determined whether the first target block is in use; when the first target block is in use, the first target block is placed in the first blockchain table. at the end of ; when the first target block is not in use, determining whether the second blockchain table stores the first target block, the second blockchain table being used to store blocks containing invalid pages in the cache partition; when the second blockchain table stores the first target block, reclaiming all physical pages in the first target block, and after all physical pages in the first target block are reclaimed, erasing the data in the first target block, thereby accurately determining the blocks that really need to be erased first through the call judgment between the first blockchain table and the second blockchain table, further limiting the optional range of blocks that need to be urgently recycled, eliminating the need to frequently recycle different blocks during the reading and writing process, reducing the latency of data response, and improving the processing performance of the NAND Flash memory.
[0206] The embodiment of the present application also provides an electronic device, the electronic device includes a memory and a processor, the memory stores a computer program, and the processor implements the above cache garbage collection method and / or cache data reading method when executing the computer program. The electronic device can be any smart terminal including a mobile phone, a tablet computer, a car computer, etc.
[0207] See also Fig.16 , Fig.16 : is a schematic diagram of the hardware structure of an electronic device provided in some embodiments of the present application, and the electronic device includes:
[0208] The processor 1601 may be implemented by a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (Application Specific Integrated Circuit, ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the cache garbage collection method and / or cache data reading method provided in the embodiments of the present application;
[0209] The memory 1602 may be implemented in the form of a read-only memory (ROM), a static storage device, a dynamic storage device, or a random access memory (RAM). The memory 1602 may store an operating system and other application programs. When the technical solution provided in the embodiments of this specification is implemented by software or firmware, the relevant program code is stored in the memory 1602, and the processor 1601 calls and executes the cache garbage collection method and / or cache data reading method provided in the embodiments of this application;
[0210] Input / output interface 1603, used to implement information input and output;
[0211] Communication interface 1604, used to realize communication interaction between the device and other devices, which can be realized through wired mode (such as USB, network cable, etc.) or wireless mode (such as mobile network, WIFI, Bluetooth, etc.);
[0212] A bus 1605 that transmits information between the various components of the device (e.g., the processor 1601, the memory 1602, the input / output interface 1603, and the communication interface 1604);
[0213] The processor 1601 , the memory 1602 , the input / output interface 1603 and the communication interface 1604 are connected to each other in communication within the device via the bus 1605 .
[0214] An embodiment of the present application also provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, the cache garbage collection method and / or cache data reading method provided in the embodiment of the present application are provided.
[0215] The memory, as a non-transient computer-readable storage medium, can be used to store non-transient software programs and non-transient computer executable programs. In addition, the memory may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some embodiments, the memory may optionally include a memory remotely disposed relative to the processor, and these remote memories may be connected to the processor via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0216] The embodiments described in the embodiments of the present application are intended to more clearly illustrate the technical solutions of the embodiments of the present application and do not constitute a limitation on the technical solutions provided in the embodiments of the present application. Those skilled in the art will appreciate that with the evolution of technology and the emergence of new application scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0217] Those skilled in the art will appreciate that the technical solutions shown in the figures do not constitute a limitation on the embodiments of the present application, and may include more or fewer steps than shown in the figures, or a combination of certain steps, or different steps.
[0218] The device embodiments described above are merely illustrative, and the units described as separate components may or may not be physically separated, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0219] Those skilled in the art will appreciate that all or some of the steps in the methods disclosed above, and the functional modules / units in the systems and devices may be implemented as software, firmware, hardware, or a suitable combination thereof.
[0220] The terms "first", "second", "third", "fourth", etc. (if any) in the specification of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0221] It should be understood that in the present application, "at least one (item)" means one or more, and "plurality" means two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can mean: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the objects associated before and after are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple.
[0222] In the several embodiments provided in the present application, it should be understood that the disclosed systems and methods can be implemented in other ways. For example, the system embodiments described above are merely schematic. For example, the division of the above units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.
[0223] The units described above as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0224] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0225] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-accessible storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including multiple instructions to enable a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of various embodiments of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, referred to as ROM), random access memory (Random Access Memory, referred to as RAM), disk or optical disk and other media that can store programs.
[0226] The preferred embodiments of the present invention are described above with reference to the accompanying drawings, but the scope of the rights of the present invention is not limited thereto. Any modification, equivalent substitution and improvement made by a person skilled in the art without departing from the scope and essence of the present invention should be within the scope of the rights of the present invention.
Claims
1. A cache garbage collection method, characterized in that: include: In response to the cache read and write operations being completed, a first blockchain table is obtained, and it is determined whether the first blockchain table is an empty chain list, the first blockchain table being used to store blocks that are determined to contain invalid pages in a scenario where the flash memory triggers power failure recovery or blocks that have read errors in a scenario where flash memory data is read; When the first blockchain list is not an empty list, extracting a first target block at the head of the first blockchain list, and determining whether the first target block is in use; When the first target block is in use, placing the first target block at the end of the first blockchain list; When the first target block is not in use, determining whether the first target block is stored in a second blockchain table, the second blockchain table being used to store blocks containing invalid pages in a cache partition; When the second blockchain table stores the first target block, reclaiming all physical pages containing valid data in the first target block, and after all physical pages containing valid data in the first target block are reclaimed, erasing the data in the first target block; The step of reclaiming all physical pages containing valid data in the first target block comprises the following steps: Deleting the first target block in the second blockchain table; According to the last physical page of the first target block, a first mapping array is constructed, wherein each array element of the first mapping array stores a first logical page address corresponding to each physical page in the first target block; Sequentially obtain the first physical page address corresponding to each of the first logical page addresses in the logical page mapping table and the second physical page address corresponding to each physical page in the first target block in the flash memory, and determine the physical page in the first target block whose corresponding second physical page address is equal to the first physical page address as a first valid page, where the first valid page is used to represent a physical page in the first target block that contains valid data; Reading the corresponding first user data from the flash memory in sequence according to each of the first valid pages, and writing each of the first user data into the storage space of the second target block, and increasing the number of invalid pages of the first target block by one, and updating each of the third physical page addresses in the logical page mapping table, and writing each of the first logical page addresses into the second mapping array, when all the physical pages of the second target block complete the writing operation, writing the second mapping array into the last physical page of the second target block, the first user data is the user data of the first valid page, and the third physical page address is used to indicate each of the physical page addresses where each of the first user data is written into the second target block; According to the total number of pages determined to be invalid in the first target block, all valid pages are recovered in the first target block, and the first target block is placed in the sequence position corresponding to the second blockchain table; The third target block is searched and extracted in the second blockchain table, and the third target block is placed in the sequence position corresponding to the third blockchain table, all physical pages in the third target block are determined to be invalid pages, and the third blockchain table is used to store blocks in an idle state.
2. The cache garbage collection method according to claim 1, characterized in that: The determining whether the first target block is in use includes: Get the second target block currently in use; Determining whether the physical address of the first target block is consistent with the physical address of the second target block; When the physical address of the first target block is consistent with the physical address of the second target block, determining that the first target block is in use; When the physical address of the first target block is inconsistent with the physical address of the second target block, it is determined that the first target block is not in use.
3. The cache garbage collection method according to claim 1, characterized in that: The erasing the data in the first target block includes: Determine whether the third blockchain table stores the first target block; When the third blockchain table stores the first target block, the first target block is deleted from the third blockchain table, and data in the first target block is erased.
4. The cache garbage collection method according to claim 1, characterized in that: Before writing each of the first user data into the storage space of the second target block, the cache garbage collection method further includes: Calculating the total number of invalid pages belonging to the first target block in the logical page mapping table according to the first mapping array to obtain a first number value; Counting the total number of pages determined to be invalid in the first target block to obtain a second number value; determining whether the first quantity value is equal to the second quantity value; When the first quantity value is not equal to the second quantity value, a log warning message is output.
5. The cache garbage collection method according to claim 1, characterized in that: Before obtaining the first blockchain table, the cache garbage collection method further includes: Counting the total number of blocks in the cache partition that are in an idle state to obtain a fourth quantity value; When the fourth quantity value is within a preset recycling interval, garbage collection is performed on a fourth target block, and the fourth target block is stored in the head of the second blockchain table; The garbage collection process for the fourth target block includes the following steps: Counting the total number of all physical pages in the fourth target block to obtain a fifth number value; Counting the total number of pages determined to be invalid in the fourth target block to obtain a sixth number value; determining the number of pages to be reclaimed of the fourth target block according to a ratio between the fifth number value and the sixth number value; The pages to be reclaimed in the fourth target block are reclaimed according to the number of pages to be reclaimed, and after the pages to be reclaimed in the fourth target block are completely reclaimed, the data in the fourth target block is erased.
6. The cache garbage collection method according to claim 5, characterized in that: The reclaiming the to-be-reclaimed pages in the fourth target block includes: Extracting the fourth target block at the head of the second blockchain table; According to the last physical page of the fourth target block, a third mapping array is constructed, wherein each array element of the third mapping array stores the second logical page address corresponding to each physical page of the fourth target block; Sequentially obtain the fourth physical page address in the logical page mapping table corresponding to each of the second logical page addresses and the fifth physical page address in the flash memory corresponding to each of the to-be-reclaimed pages in the first target block, and determine the physical page in the fourth target block whose corresponding fifth physical page address is equal to the fourth physical page address as a second valid page, wherein the second valid page is used to indicate the to-be-reclaimed page in the fourth target block that contains valid data; Reading corresponding second user data from the flash memory in sequence according to each of the second valid pages, and writing each of the second user data into the storage space of the second target block, and increasing the number of invalid pages of the fourth target block by one, and updating each sixth physical page address in the logical page mapping table, and writing each of the second logical page addresses into the second mapping array, when all physical pages of the second target block complete the writing operation, writing the second mapping array into the last physical page of the second target block, the second user data is the user data of the second valid page, and the sixth physical page address is used to indicate each physical page address where each of the second user data is written into the second target block; According to the sixth quantity value, reclaiming valid pages in the fourth target block in a number equal to the sixth quantity value, and placing the fourth target block in a sequence position corresponding to the second blockchain table; The third target block is searched and extracted in the second blockchain table, and the third target block is placed in a sequence position corresponding to the third blockchain table, wherein all physical pages in the third target block are determined to be invalid pages, and the third blockchain table is used to store blocks in an idle state.
7. The cache garbage collection method according to claim 6, characterized in that: Before writing each of the second user data into the storage space of the second target block, the cache garbage collection method further includes: Calculating the total number of invalid pages belonging to the fourth target block in the logical page mapping table according to the second mapping array to obtain a seventh number value; determining whether the sixth quantity value is equal to the seventh quantity value; When the sixth quantity value is not equal to the seventh quantity value, a log warning message is output.
8. The cache garbage collection method according to claim 5, characterized in that: Erasing the data in the fourth target block includes: When a write operation is performed on the header block of the third blockchain table, determining whether the header block of the third blockchain table is the fourth target block; When the header block of the third blockchain table is the fourth target block, data in the fourth target block is erased.
9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the cache garbage collection method according to any one of claims 1 to 8 is implemented.
Citation Information
Patent Citations
Log-structured storage systems
CN111183450A
Cache data writing method, cache data reading method and storage medium thereof
CN119046189A