Fragmented file arrangement method, management device, flash memory storage device and terminal
Patent Information
- Application Number
- CN202380071278.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2022-11-22
- Filing Date
- 2023-09-06
- Publication Date
- 2025-05-13
AI Technical Summary
In the existing technology, the defragmentation operation of flash memory storage devices is limited by firmware algorithms, resulting in delayed performance recovery and inability to effectively solve the storage space shortage problem of flash memory storage devices.
The management device actively queries the fragmentation status of the flash storage device, and sends instructions to the flash controller to defragment it when necessary to ensure that the physical addresses of fragmented files are continuous, thereby restoring the performance of the storage device in a timely manner.
It achieves timely recovery of the performance of flash storage devices, improves read and write performance, extends the life of the storage device, and avoids defragmentation delays under the limitations of firmware algorithms.
Smart Images

Figure CN119998795A_ABST
Abstract
Description
Fragmented file arrangement method, management device, flash memory storage device and terminal
[0001] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office on November 22, 2022, with application number 202211469161.6 and invention name “Method for organizing fragmented files, management device, flash memory storage device and terminal”, the entire contents of which are incorporated by reference into this application. Technical Field
[0002] The present application relates to the field of terminal technology, and in particular to a method for organizing fragmented files, a management device, a flash memory storage device, and a terminal. Background Art
[0003] Android-based terminals (e.g., mobile phones) are becoming the mainstream in the terminal market. Currently, Android systems typically use embedded multi-media cards (eMMC) or universal flash storage (UFS) as flash memory devices.
[0004] Among them, eMMC and UFS belong to different types of flash memory storage devices. A flash memory storage device contains one or more storage blocks for storing data in a flash memory storage manner. A feature of a flash memory device is that the original data needs to be erased before writing data. With the long-term use of the terminal, repeated operations such as reading, writing, and erasing make the fragmentation of flash memory devices more and more serious. A large amount of fragmented data is stored in the flash memory device, occupying the limited storage space in the flash memory device, thereby causing the storage space in the flash memory device to be tight. When the storage space of the flash memory device is insufficient, in order to reclaim the storage space, the terminal needs to perform a garbage collection (GC) operation on the flash memory device. The purpose of the GC operation is to reclaim the physical pages that have been invalidated in the storage block and reuse the invalidated physical pages to store data.
[0005] In the related art, the terminal's GC operation on the flash memory device depends on the firmware algorithm inside the flash memory device. However, since the firmware algorithm has restrictions, the flash memory device must meet these restrictions before it can be defragmented. Therefore, the firmware algorithm cannot perform GC operations on the flash memory device in a timely manner, resulting in the performance of the flash memory device not being restored in a timely manner.
[0006] Summary of the Invention
[0007] The embodiments of the present application provide a method for organizing fragmented files, a management device, a flash memory storage device, and a terminal, which are used to solve the problem that the performance of the flash memory storage device cannot be restored in a timely manner.
[0008] To achieve the above objectives, the embodiments of the present application adopt the following technical solutions:
[0009] In a first aspect, a method for organizing fragmented files is provided, which is applied to a terminal having a management device and a flash memory storage device, the flash memory storage device including a flash memory controller and a storage medium, and the storage medium storing fragmented files. The method comprises: the management device sends a first instruction to the flash memory controller, the first instruction being used to query the fragmentation status of the fragmented files stored on the storage medium; the management device receives a first message from the flash memory controller, the first message being used to indicate the fragmentation status of the fragmented files stored on the storage medium; in response to the first message, when the fragmentation status indicates that the physical addresses corresponding to the fragmented files stored on the storage medium are discontinuous, the management device sends a second instruction to the flash memory controller; the second instruction being used to instruct the flash memory controller to organize the fragmented files on the storage medium.
[0010] Based on the first aspect, the management device can actively send a first instruction to the flash memory controller to query the fragmentation status of the fragmented files stored on the storage medium. Accordingly, the flash memory controller returns the fragmentation status of the fragmented files stored on the storage medium to the management device. When the fragmentation status indicates that the physical addresses corresponding to the fragmented files stored on the storage medium are discontinuous, the management device can send a second instruction to the flash memory controller to instruct the flash memory controller to organize the fragmented files on the storage medium. In this way, the management device can control the flash memory controller to organize the fragmented files on the storage medium when the physical addresses corresponding to the fragmented files stored on the storage medium are discontinuous, so that the fragmented files on the storage medium can be organized in a timely manner, and the performance of the flash memory storage device can be restored in a timely manner.
[0011] In a possible implementation of the first aspect, the management device sends a first instruction to the flash memory controller, including: the management device obtains a first storage space and a first duration; wherein the first storage space is used to indicate the available storage space of the storage medium, and the first duration is used to indicate the interval between the current moment and the last time the flash memory controller sorted out fragmented files on the storage medium; if the management device determines that the first storage space matches the first duration, the management device sends the first instruction to the flash memory controller.
[0012] In this implementation, the management device can obtain the available storage space of the storage medium, i.e., the first storage space, and the interval between the current moment and the last time the flash memory controller defragmented files on the storage medium, i.e., the first duration. Based on this, if the management device determines that the first storage space matches the first duration, the management device sends a first instruction to the flash memory controller. In other words, the present application will only send the first instruction to the flash memory controller to query the fragmentation status of the flash memory storage device if it determines that the available storage space of the storage medium matches the interval between the current moment and the last time the flash memory controller defragmented files on the storage medium, thereby avoiding the management device from frequently sending the first instruction to the flash memory controller, which would affect the lifespan of the management device.
[0013] In a possible implementation of the first aspect, the management device determines that the first storage space matches the first duration, including: the management device queries the target duration corresponding to the first storage space in the stored registration information; the target duration is a pre-configured interval between two adjacent defragmentation operations on the storage medium; if the first duration is greater than or equal to the target duration, the management device determines that the first storage space matches the first duration; wherein the registration information records the correspondence between the first storage space and the target duration.
[0014] In this implementation, the management device can be pre-configured with a correspondence between the first storage space and the target duration. In this way, the management device can determine whether the first storage space matches the first duration based on the acquired first storage space and the first duration, thereby enhancing the accuracy of the matching between the first storage space and the first duration.
[0015] In a possible implementation of the first aspect, the management device sends a first instruction to the flash memory controller, including: if the terminal is in the screen-off and charging state, and the duration of the terminal in the screen-off state meets the first preset duration, the management device sends the first instruction to the flash memory controller; or, within the preset time period, if the terminal is in the screen-off and charging state, the management device sends the first instruction to the flash memory controller.
[0016] In this implementation, the management device can send a first instruction to the flash memory controller when the terminal is in the screen-off and charging state and the duration of the terminal in the screen-off state meets the first preset duration; or when the terminal is in the screen-off and charging state within the preset time period, the management device can prevent the terminal from affecting the flash memory controller in organizing fragmented files stored on the storage medium when the screen is on or the battery is low.
[0017] In a possible implementation of the first aspect, the method further includes: the management device receives a second message from the flash memory controller; the second message is used to indicate that the physical addresses corresponding to the fragmented files stored on the storage medium are continuous; in response to the second message, the management device sends a third instruction to the flash memory controller; the third instruction is used to instruct the flash memory controller to stop organizing the fragmented files on the storage medium.
[0018] In this implementation, when the flash memory controller finishes organizing the fragmented files stored on the storage medium, the flash memory controller will also send a second message to the management device. Afterwards, the management device responds to the second message and sends a third instruction to the flash memory controller to instruct the flash memory controller to stop organizing the fragmented files on the storage medium, which is beneficial to improving the life of the flash memory storage device.
[0019] In a possible implementation of the first aspect, the method further includes: if the management device does not receive the second message within a first preset time period, the management device sends a third instruction to the flash memory controller; wherein the second message is used to indicate that the physical addresses corresponding to the fragmented files stored on the storage medium are continuous; and the third instruction is used to instruct the flash memory controller to stop organizing the fragmented files on the storage medium.
[0020] In this implementation, if the management device does not receive the second message within the first preset time period, the management device sends a third instruction to the flash memory controller, that is, the time period for the flash memory controller to organize the fragmented files stored on the storage medium exceeds the first preset time period. The management device can instruct the flash memory controller to stop organizing the fragmented files on the storage medium, thereby improving the life of the flash memory storage device.
[0021] In a possible implementation of the first aspect, before the management device receives the second message from the flash memory controller, the method also includes: the management device sends a fourth instruction to the flash memory controller; the fourth instruction is used to query the flash memory controller for the progress of defragmenting files on the storage medium; the management device receives a third message from the flash memory controller; the third message is used to indicate that the progress of defragmenting files on the storage medium is not completed; in response to the third message, the management device resends the fourth instruction to the flash memory controller until the second message from the flash memory controller is received.
[0022] In this implementation, the management device may also send a fourth instruction to the flash memory controller to query the flash memory controller for the progress of defragmenting files on the storage medium. If the flash memory controller returns an incomplete progress, the management device resends the fourth instruction to the flash memory controller. If the flash memory controller returns a completed progress, the management device sends a second instruction to the flash memory controller, instructing the flash memory controller to stop defragmenting files on the storage medium. This prevents the flash memory controller from defragmenting files on the storage medium for a long time, thereby extending the life of the flash memory storage device.
[0023] In a possible implementation of the first aspect, the management device sends a second instruction to the flash memory controller, including: the management device calls the storage management service in the application framework layer, and sends the second instruction to the storage device driver in the kernel layer; wherein, the method also includes: the management device calls the storage device driver in the kernel layer, and drives the flash memory controller to organize fragmented files on the storage medium.
[0024] In a possible implementation of the first aspect, the management device calls the storage management service in the application framework layer and sends a second instruction to the storage device driver in the kernel layer, including: the management device calls the storage management service in the application framework layer and sends the second instruction to the storage device driver in the kernel layer through the vold process.
[0025] In a second aspect, a method for organizing fragmented files is provided, which is applied to a terminal having a management device and a flash memory storage device, wherein the flash memory storage device includes a flash memory controller and a storage medium, and the fragmented files are stored on the storage medium; the method includes: the flash memory controller receives a first instruction from the management device; the first instruction is used to query the fragmentation status of the fragmented files stored on the storage medium; in response to the first instruction, the flash memory controller sends a first message to the management device; the first message is used to indicate the fragmentation status of the fragmented files stored on the storage medium; when the fragmentation status indicates that the physical addresses corresponding to the fragmented files stored on the storage medium are discontinuous, the flash memory controller receives a second instruction from the management device; in response to the second instruction, the flash memory controller organizes the fragmented files on the storage medium.
[0026] In a possible implementation of the second aspect, the method further includes: if the flash memory storage device meets a preset sorting condition, the flash memory controller sorts the fragmented files on the storage medium based on a firmware algorithm inside the flash memory storage device; wherein the preset sorting condition includes: the available storage space of the storage medium is less than the preset storage space; or, the flash memory storage device meets a preset cycle, and the preset cycle is the cycle for the flash memory controller set in the firmware algorithm to sort the fragmented files on the storage medium.
[0027] In a possible implementation of the second aspect, the method further includes: the flash memory controller sends a second message to the management device; the second message is used to indicate that the physical addresses corresponding to the fragmented files stored on the storage medium are continuous; the flash memory controller receives a third instruction from the management device; in response to the third instruction, the flash memory controller stops organizing the fragmented files on the storage medium.
[0028] In a possible implementation of the second aspect, the method further includes: if the time length for the flash memory controller to organize the fragmented files on the storage medium is greater than a first preset time length, the flash memory controller receives a third instruction from the management device; in response to the third instruction, the flash memory controller stops organizing the fragmented files on the storage medium.
[0029] In a possible implementation of the second aspect, the flash memory controller sends a second message to the management device, including: the flash memory controller receives a fourth instruction from the management device; in response to the fourth instruction, the flash memory controller queries the progress of the flash memory controller in sorting out fragmented files on the storage medium; if the flash memory controller has not completed the sorting of fragmented files on the storage medium, the flash memory controller re-receives the fourth instruction from the management device until the flash memory controller completes the sorting of fragmented files on the storage medium, and the flash memory controller sends the second message to the management device.
[0030] In a third aspect, a management device is provided for use in a terminal having a flash memory device, the flash memory device including a flash memory controller and a storage medium, wherein the storage medium stores fragmented files. The management device is capable of implementing the functions described in the first aspect. The functions may be implemented in hardware or by hardware executing corresponding software. The hardware or software includes one or more modules corresponding to the functions described above.
[0031] In a fourth aspect, a flash memory device is provided for use in a terminal having a management device. The flash memory device includes a flash memory controller and a storage medium, wherein the storage medium stores fragmented files. The flash memory device is capable of implementing the functions described in the second aspect. The functions can be implemented through hardware or through hardware executing corresponding software. The hardware or software includes one or more modules corresponding to the functions described above.
[0032] In a fifth aspect, a management device is provided, which is applied to a terminal with a flash memory storage device, the flash memory storage device includes a flash memory controller and a storage medium, and fragmented files are stored on the storage medium; the management device includes a memory and one or more processors; the memory and the processor are coupled; the memory is used to store computer program code, and the computer program code includes computer instructions; when the processor executes the computer instructions, the management device performs the following steps: the management device sends a first instruction to the flash memory controller, and the first instruction is used to query the fragmentation status of the fragmented files stored on the storage medium; the management device receives a first message from the flash memory controller, and the first message is used to indicate the fragmentation status of the fragmented files stored on the storage medium; in response to the first message, when the fragmentation status indicates that the physical addresses corresponding to the fragmented files stored on the storage medium are discontinuous, the management device sends a second instruction to the flash memory controller; the second instruction is used to instruct the flash memory controller to organize the fragmented files on the storage medium.
[0033] In a possible implementation of the fifth aspect, when the processor executes a computer instruction, the management device further performs the following steps: the management device obtains a first storage space and a first duration; wherein the first storage space is used to indicate the available storage space of the storage medium, and the first duration is used to indicate the interval between the current moment and the last time the flash memory controller sorted out the fragmented files on the storage medium; if the management device determines that the first storage space matches the first duration, the management device sends a first instruction to the flash memory controller.
[0034] In a possible implementation of the fifth aspect, when the processor executes computer instructions, the management device specifically performs the following steps: the management device queries the target duration corresponding to the first storage space in the stored registration information; the target duration is a pre-configured interval between two adjacent times of organizing fragmented files on the storage medium; if the first duration is greater than or equal to the target duration, the management device determines that the first storage space matches the first duration; wherein the registration information records the correspondence between the first storage space and the target duration.
[0035] In a possible implementation of the fifth aspect, when the processor executes computer instructions, the management device specifically performs the following steps: if the terminal is in the screen-off and charging state, and the duration of the terminal in the screen-off state meets the first preset duration, the management device sends a first instruction to the flash memory controller; or, within the preset time period, if the terminal is in the screen-off and charging state, the management device sends a first instruction to the flash memory controller.
[0036] In a possible implementation of the fifth aspect, when the processor executes a computer instruction, the management device further performs the following steps: the management device receives a second message from the flash memory controller; the second message is used to indicate that the physical addresses corresponding to the fragmented files stored on the storage medium are continuous; in response to the second message, the management device sends a third instruction to the flash memory controller; the third instruction is used to instruct the flash memory controller to stop organizing the fragmented files on the storage medium.
[0037] In a possible implementation of the fifth aspect, when the processor executes a computer instruction, the management device also performs the following steps: if the management device does not receive the second message within a first preset time period, the management device sends a third instruction to the flash memory controller; wherein the second message is used to indicate that the physical addresses corresponding to the fragmented files stored on the storage medium are continuous; and the third instruction is used to instruct the flash memory controller to stop organizing the fragmented files on the storage medium.
[0038] In a possible implementation of the fifth aspect, when the processor executes the computer instructions, the management device further performs the following steps: before the management device receives the second message from the flash memory controller, the method also includes: the management device sends a fourth instruction to the flash memory controller; the fourth instruction is used to query the flash memory controller for the progress of sorting out fragmented files on the storage medium; the management device receives a third message from the flash memory controller; the third message is used to indicate that the progress of sorting out fragmented files on the storage medium is not completed; in response to the third message, the management device resends the fourth instruction to the flash memory controller until the second message from the flash memory controller is received.
[0039] In a possible implementation of the fifth aspect, when the processor executes a computer instruction, the management device specifically performs the following steps: the management device calls the storage management service in the application framework layer and sends a second instruction to the storage device driver in the kernel layer; wherein, the method also includes: the management device calls the storage device driver in the kernel layer and drives the flash memory controller to organize fragmented files on the storage medium.
[0040] In a possible implementation of the fifth aspect, when the processor executes the computer instruction, the management device specifically performs the following steps: the management device calls the storage management service in the application framework layer, and sends a second instruction to the storage device driver in the kernel layer through the vold process.
[0041] In a sixth aspect, a flash memory device is provided, which is applied to a terminal with a management device, the flash memory device includes a flash memory controller and a storage medium, and fragmented files are stored on the storage medium; the flash memory device includes a memory and one or more processors; the memory and the processor are coupled; the memory is used to store computer program code, and the computer program code includes computer instructions; when the processor executes the computer instructions, the flash memory device performs the following steps: the flash memory controller receives a first instruction from the management device; the first instruction is used to query the fragmentation status of the fragmented files stored on the storage medium; in response to the first instruction, the flash memory controller sends a first message to the management device; the first message is used to indicate the fragmentation status of the fragmented files stored on the storage medium; when the fragmentation status indicates that the physical addresses corresponding to the fragmented files stored on the storage medium are discontinuous, the flash memory controller receives a second instruction from the management device; in response to the second instruction, the flash memory controller organizes the fragmented files on the storage medium.
[0042] In a possible implementation of the sixth aspect, when the processor executes a computer instruction, the flash memory device further performs the following steps: if the flash memory device meets a preset sorting condition, the flash memory controller sorts the fragmented files on the storage medium based on a firmware algorithm inside the flash memory device; wherein the preset sorting condition includes: the available storage space of the storage medium is less than the preset storage space; or, the flash memory device meets a preset cycle, and the preset cycle is the cycle for the flash memory controller set in the firmware algorithm to sort the fragmented files on the storage medium.
[0043] In a possible implementation of the sixth aspect, when the processor executes a computer instruction, the flash memory storage device also performs the following steps: the flash memory controller sends a second message to the management device; the second message is used to indicate that the physical addresses corresponding to the fragmented files stored on the storage medium are continuous; the flash memory controller receives a third instruction from the management device; in response to the third instruction, the flash memory controller stops organizing the fragmented files on the storage medium.
[0044] In a possible implementation of the sixth aspect, when the processor executes a computer instruction, the flash memory storage device also performs the following steps: if the time length for the flash memory controller to organize the fragmented files on the storage medium is greater than a first preset time length, the flash memory controller receives a third instruction from the management device; in response to the third instruction, the flash memory controller stops organizing the fragmented files on the storage medium.
[0045] In a possible implementation of the sixth aspect, when the processor executes a computer instruction, the flash memory storage device specifically performs the following steps: the flash memory controller receives a fourth instruction from the management device; in response to the fourth instruction, the flash memory controller queries the progress of the flash memory controller in sorting out the fragmented files on the storage medium; if the flash memory controller has not completed the sorting of the fragmented files on the storage medium, the flash memory controller re-receives the fourth instruction from the management device until the flash memory controller completes the sorting of the fragmented files on the storage medium, and the flash memory controller sends a second message to the management device.
[0046] In the seventh aspect, a terminal is provided, which includes a management device and a flash memory storage device, the flash memory storage device includes a flash memory controller and a storage medium, and the storage medium stores fragmented files; the management device is used to execute the method described in the first aspect or any one of the first aspects; the flash memory controller is used to execute the method described in the second aspect or any one of the second aspects.
[0047] In an eighth aspect, a chip system is provided, comprising at least one processor and at least one interface circuit; the processor and the interface circuit are interconnected through lines; wherein the processor is used to run computer instructions to implement the above-mentioned first aspect or any one of the methods in the first aspect; or to implement the above-mentioned second aspect or any one of the methods in the second aspect.
[0048] In the ninth aspect, a computer-readable storage medium is provided, in which instructions are stored. When the computer-readable storage medium is run on a computer, the computer can execute any of the methods in the first aspect; or execute any of the methods in the second aspect.
[0049] In a tenth aspect, a computer program product comprising instructions is provided, which, when executed on a computer, enables the computer to execute any of the methods described in the first aspect above; or execute any of the methods described in the second aspect above.
[0050] Among them, the technical effects brought about by any design method in the second to tenth aspects can refer to the technical effects brought about by different design methods in the first aspect, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0051] FIG1 is a schematic diagram of the structure of a terminal provided in an embodiment of the present application;
[0052] FIG2 is a schematic diagram of a principle of defragmentation provided by an embodiment of the present application;
[0053] FIG3 is a schematic diagram of the hardware structure of a mobile phone provided in an embodiment of the present application;
[0054] FIG4 is a software framework diagram of a mobile phone provided in an embodiment of the present application;
[0055] FIG5 is a flow chart of a method for organizing fragmented files provided in an embodiment of the present application;
[0056] FIG6 is a flow chart of another method for organizing fragmented files provided in an embodiment of the present application;
[0057] FIG7 is a flow chart of another method for organizing fragmented files provided in an embodiment of the present application;
[0058] FIG8 is a schematic diagram illustrating the read and write performance of a flash memory device provided in an embodiment of the present application;
[0059] FIG9 is a flow chart of another method for organizing fragmented files provided in an embodiment of the present application;
[0060] FIG10 is a flow chart of another method for organizing fragmented files according to an embodiment of the present application;
[0061] FIG11 is a schematic structural diagram of a chip system provided in an embodiment of the present application. DETAILED DESCRIPTION
[0062] In order to enable those skilled in the art to better understand the solutions of the embodiments of the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only some embodiments of the present application, not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without making creative efforts should fall within the scope of protection of this application.
[0063] In the following, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the technical features indicated. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the features. In the description of the embodiments of this application, unless otherwise specified, "plurality" means two or more.
[0064] Before describing the embodiments of the present application, the relevant terms involved in the embodiments of the present application are first explained.
[0065] Fragmented files refer to fragmented data on a flash storage device. Files are fragmented when they are spread across many different storage areas on the flash storage device. Fragmented files are the result of discontinuous application or file storage. That is, different parts of a given application or file are not stored in a contiguous set of storage blocks on the flash storage device.
[0066] At present, existing defragmentation solutions for flash memory devices are generally based on the fstrim command, which uses the file system's virtual space cleaning method to perform defragmentation. For example, the terminal sends the fstrim command to the flash memory device at the file system layer to inform the flash memory device which address space is virtual. However, after the flash memory device receives the command to clean up the virtual space sent by the terminal, the timing of triggering the flash memory device to perform defragmentation depends on the device's internal firmware algorithm. For example, the firmware algorithm is set to perform defragmentation periodically (such as every 10 hours or 15 hours). In this case, if the timing to trigger the flash memory device to perform defragmentation has not yet arrived (i.e., it has not arrived 10 hours or 15 hours), even if the flash memory device receives the command to clean up the virtual space sent by the terminal, the flash memory device will not perform defragmentation.
[0067] Alternatively, the firmware algorithm is set to defragment when the storage space of the flash memory device is insufficient (such as 20% of the available storage space remains). In this case, if the time to trigger the flash memory device to defragment has not arrived (that is, the remaining capacity of the available storage space has not reached 20%), even if the flash memory device receives a command to clear the virtual space sent by the terminal, the flash memory device will not defragment.
[0068] Obviously, due to the restrictions set by the firmware algorithm, the flash memory device must wait until the restrictions are met before defragmenting after receiving the command from the terminal to clear the occupied space. As a result, existing defragmentation solutions for flash memory devices cannot defragment the flash memory device (i.e., perform GC operations) in a timely manner, resulting in the performance of the flash memory device not being restored in a timely manner. This, in turn, affects the read and write performance of the flash memory device.
[0069] To address the aforementioned issues, the present application provides a method for defragmenting fragmented files, which can be applied to a terminal having a management device and a flash memory device. In an embodiment of the present application, the management device can proactively query the fragmentation status of the flash memory device. When fragmented files stored on the flash memory device require defragmentation, the management device can issue an instruction to the flash memory device. After the flash memory device receives the instruction from the management device, it can respond to the instruction and perform defragmentation. In this way, by proactively querying the fragmentation status of the flash memory device and issuing an instruction to the flash memory device when fragmented files stored on the flash memory device require defragmentation, the management device enables the flash memory device to defragment the fragmented files in a timely manner, thereby resolving the issue of delayed performance recovery of the flash memory device.
[0070] The method provided in the embodiment of the present application can be applied to a terminal. As shown in FIG1 , the terminal includes a management device 10 and a flash memory device 20 (or FLASH memory device).
[0071] It should be noted that the management device 10 can be integrated into the terminal; or the management device 10 can also be a device independent of the terminal, which is not limited in this application. In the following embodiments, the management device 10 is integrated into the terminal as an example.
[0072] The management device 10 is the control center of the terminal, which can control the terminal to perform various functions and process data. The management device 10 can be a chip with a memory 102. For example, the management device 10 can be a system on chip (SoC). For example, as shown in Figure 1, the management device 10 can include a processor 101 and a memory 102. The processor 101 can be a central processing unit (CPU). The processor 101 can read stored instructions or data from the memory 102 to control the terminal to perform various functions and process data.
[0073] The flash memory device 20 is primarily used to store software and data in terminals (such as mobile phones and tablets), such as operating system software, application software, graphics, and documents. Still as shown in FIG1 , the flash memory device 20 may include a flash controller 201 and at least one storage medium 202. The flash memory device 20 may be an eMMC storage device or a UFS storage device; the storage medium may be a NAND flash memory chip for storing data. In some embodiments, the storage medium 202 may include M storage blocks, each of which is used to store software or data in the terminal. M is a positive integer.
[0074] The flash memory controller 201 can be responsible for internal and external control and communication. On one hand, the flash memory controller 201 can be responsible for operations such as reading, writing, and erasing the storage medium in the flash memory device 20, as well as address space management. On the other hand, the flash memory controller 201 can be responsible for communication between the flash memory device 20 and the management device 10 in the terminal. For example, the flash memory controller 201 can receive and respond to commands from the management device 10 in the terminal, storing data to be stored by the management device 10 in the terminal on the corresponding storage medium, and retrieving data to be read by the management device 10 in the terminal from the corresponding storage medium and sending it to the management device 10 in the terminal.
[0075] Illustratively, as shown in FIG1 , the management device 10 may send an instruction to the flash memory controller 201 included in the flash memory device 20 through the processor 101. For example, the processor 101 may actively send a first instruction to the flash memory controller 201 to query the fragmentation status of the flash memory device 20. Then, the flash memory controller 201 returns a first message to the processor 101 in response to the first instruction, that is, returns the fragmentation status of the flash memory device 20. In some embodiments, when the fragmentation status returned by the flash memory controller 201 to the processor 101 is the first status, the processor 101 sends a second instruction to the flash memory controller 201 in response to the first message to instruct the flash memory controller 201 to perform defragmentation. Furthermore, the flash memory controller 201 performs defragmentation in response to the second instruction. In an embodiment of the present application, the flash memory controller 201 performing defragmentation refers to the flash memory controller 201 defragmenting fragmented files on the storage medium. In other embodiments, when the fragmentation status returned by the flash memory controller 201 to the processor 101 is the second status, the processor 101 will not send the second instruction to the flash memory controller 201, that is, the processor 101 will not instruct the flash memory controller 201 to perform defragmentation.
[0076] The fragmentation state includes a first state and a second state; the first state may indicate that the physical addresses corresponding to the fragmented files stored on the storage medium are discontinuous; the second state may indicate that the physical addresses corresponding to the fragmented files stored on the storage medium are continuous.
[0077] In some embodiments, as shown in (1) of FIG2 , it is assumed that the storage medium includes M (e.g., 36) storage blocks, and multiple fragmented files are stored in N (e.g., 14) storage blocks. Then, as shown in (1) of FIG2 , the physical addresses corresponding to the multiple fragmented files stored in N storage blocks are not continuous in the N storage blocks. N is a positive integer, and N≤M.
[0078] In an embodiment of the present application, when multiple fragmented files are stored in N storage blocks, each of the multiple fragmented files corresponds to a physical address, wherein the physical address of any fragmented file is the physical address of the fragmented file stored in the N storage blocks.
[0079] The size of a fragmented file stored in any one of the N storage blocks is equal to the size of the storage space of the storage block. On this basis, when the size of a fragmented file is equal to the size of the storage space of a storage block, the fragmented file can be stored in one storage block. Alternatively, when the sum of the sizes of multiple fragmented files is equal to the size of the storage space of one storage block, the multiple fragmented files can be stored in one storage block. Alternatively, when the size of a fragmented file is greater than the size of the storage space of one storage block, the fragmented file can be stored in two or more storage blocks. This application does not impose any limitation on this.
[0080] It should be noted that when multiple fragmented files are stored in one storage block, the first physical addresses corresponding to the multiple fragmented files in the storage block are also not continuous.
[0081] In some embodiments, after the flash memory controller 201 organizes the fragmented files on the storage medium, the physical addresses corresponding to the fragmented files stored on the storage medium are discontinuous. For example, as shown in (2) in FIG2 , the physical addresses corresponding to the fragmented files stored in multiple storage blocks are continuous, that is, the fragmented files are stored in a centralized manner in multiple storage blocks.
[0082] In some embodiments, the flash memory controller 201 performs defragmentation based on the sizes of the multiple fragmented files. In other embodiments, the flash memory controller 201 may select an unoccupied storage block from the M storage blocks for defragmentation. In yet other embodiments, the flash memory controller 201 may select a storage block with the largest remaining storage space from the N storage blocks as a target storage block, and then store the multiple fragmented files in the target storage block. Of course, the flash memory controller 201 may also perform defragmentation in other ways, which is not limited in the embodiments of the present application. As long as the physical addresses corresponding to the fragmented files stored in the storage medium can be made continuous, they shall fall within the scope of protection of the embodiments of the present application.
[0083] The above-mentioned terminal may be, for example, a mobile phone, a wearable device, an augmented reality (AR) or virtual reality (VR) device, a tablet computer, a laptop computer, an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (PDA), or any other terminal. Of course, in the following embodiments, there is no limitation on the specific form of the terminal.
[0084] As shown in FIG3 , the terminal in the embodiment of the present application may be a mobile phone 300. The embodiment will be described in detail below using mobile phone 300 as an example. It should be understood that the illustrated mobile phone 300 is merely an example of the terminal, and that mobile phone 300 may have more or fewer components than shown in the figure, may combine two or more components, or may have a different component configuration.
[0085] As shown in Figure 3, the mobile phone 300 may specifically include: a management device 10 (wherein, the management device 10 may include a processor 310 (such as the processor 101 shown in Figure 1 above)), an external memory interface 320, an internal memory 321 (wherein, the internal memory 321 may include the flash memory storage device 20 shown in Figure 1), a universal serial bus (USB) interface 330, an antenna 1, an antenna 2, a mobile communication module 350, a wireless communication module 360, an audio module 370, a speaker 370A, a receiver 370B, a microphone 370C, an earphone interface 370D, a sensor module 380, etc.
[0086] The processor 310 may include one or more processing units. For example, the processor 310 may include an application processor (AP), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, a memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU). The different processing units may be independent devices or integrated into one or more processors.
[0087] Processor 310 may also include a memory for storing instructions and data. In some embodiments, the memory in processor 310 is a cache memory. This memory can store instructions or data that have just been used or are being recycled by processor 310. If processor 310 needs to use the same instruction or data again, it can directly access the memory. This avoids duplicate accesses, reduces processor 310 latency, and thus improves system efficiency.
[0088] In some embodiments, the processor 310 may include one or more interfaces. The interfaces may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface.
[0089] In the embodiment of the present application, the above-mentioned processor 310 may be the processor 101 included in the management device 10 in the embodiment of the present application.
[0090] The wireless communication function of the mobile phone 300 can be implemented through the antenna 1, the antenna 2, the mobile communication module 350, the wireless communication module 360, the modem processor and the baseband processor.
[0091] Antenna 1 and Antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in mobile phone 300 can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve antenna utilization. For example, antenna 1 can be reused as a diversity antenna for a wireless local area network. In other embodiments, the antennas can be used in conjunction with a tuning switch.
[0092] The mobile communication module 350 can provide solutions for wireless communications including 2G / 3G / 4G / 5G applied to the mobile phone 300. The mobile communication module 350 may include at least one filter, a switch, a power amplifier, a low noise amplifier (LNA), etc. The mobile communication module 350 can receive electromagnetic waves from the antenna 1, and filter, amplify, and process the received electromagnetic waves, and transmit them to the modulation and demodulation processor for demodulation. The mobile communication module 350 can also amplify the signal modulated by the modulation and demodulation processor, and convert it into electromagnetic waves for radiation through the antenna 1. In some embodiments, at least some of the functional modules of the mobile communication module 350 can be set in the processor 310. In some embodiments, at least some of the functional modules of the mobile communication module 350 can be set in the same device as at least some of the modules of the processor 310.
[0093] The wireless communication module 360 can provide wireless communication solutions including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared (IR), etc., which are applied to the mobile phone 300. The wireless communication module 360 can be one or more devices that integrate at least one communication processing module. The wireless communication module 360 receives electromagnetic waves via the antenna 2, frequency modulates and filters the electromagnetic wave signals, and sends the processed signals to the processor 310. The wireless communication module 360 can also receive the signal to be sent from the processor 310, frequency modulate it, amplify it, and convert it into electromagnetic waves for radiation through the antenna 2.
[0094] In some embodiments, antenna 1 of mobile phone 300 is coupled to mobile communication module 350, and antenna 2 is coupled to wireless communication module 360, so that mobile phone 300 can communicate with a network and other devices via wireless communication technologies. The wireless communication technologies may include global system for mobile communications (GSM), general packet radio service (GPRS), code division multiple access (CDMA), wideband code division multiple access (WCDMA), time-division code division multiple access (TD-SCDMA), long term evolution (LTE), BT, GNSS, WLAN, NFC, FM, and / or IR technology. The GNSS may include a global positioning system (GPS), a global navigation satellite system (GLONASS), a Beidou navigation satellite system (BDS), a quasi-zenith satellite system (QZSS) and / or a satellite based augmentation system (SBAS).
[0095] The external memory interface 320 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the mobile phone 300. The external memory card communicates with the processor 310 via the external memory interface 320 to implement data storage functions. For example, files such as music and videos can be stored on the external memory card.
[0096] The internal memory 321 can be used to store computer executable program codes, which include instructions. The processor 310 executes various functional applications and data processing of the mobile phone 300 by running the instructions stored in the internal memory 321. The internal memory 321 may include a program storage area and a data storage area. Among them, the program storage area can store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc. The data storage area can store data created during the use of the mobile phone 300 (such as audio data, a phone book, etc.), etc. In addition, the internal memory 321 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc.
[0097] Of course, the mobile phone 300 may also include a charging management module, a power management module, a battery, buttons, indicators, and one or more SIM card interfaces, etc., and the embodiment of the present application does not impose any restrictions on this.
[0098] Furthermore, the hardware shown in the mobile phone 300 can run an operating system such as Android or IOS, and this embodiment of the application does not impose any restrictions on this. Taking the Android operating system as an example, as shown in Figure 4, the Android operating system can be divided into four layers, from high to low, namely the application layer 401, the application framework layer 402 (i.e., the framework layer), the system runtime library layer 403 (i.e., the libraries layer), and the Linux kernel layer 404 (i.e., the kernel layer).
[0099] The Linux kernel layer 404 can be used to control functions such as security, memory management, process management, network stack, and driver model of the mobile phone 300. The Linux kernel layer 204 also serves as an abstraction layer between the hardware (e.g., the flash memory device 20, etc.) and the software stack, hiding specific hardware details and providing unified services to the upper layers (the system runtime layer 403, the application framework layer 402, and the application layer 401).
[0100] The system runtime layer 403 includes some C / C++ libraries, such as the media library, the system C library, and the display management library (surface manager). These libraries can be used by different components in the Android system. The system runtime layer 403 can provide services to developers through the framework layer 402.
[0101] The framework layer 402 provides developers with a framework that allows them to fully access the application programming interface (API) used by the application. Specifically, the framework layer 402 provides a large number of APIs for developing applications. By calling the corresponding APIs, an app that meets relevant business needs can be constructed.
[0102] The application layer 401 mainly includes APPs written in Java language. When the user operates the operation interface on the APP, the user interacts with the system runtime layer 403 or the Linux kernel layer 404 by calling the relevant API in the framework layer 402 to implement the functions corresponding to the operation interface.
[0103] 5 , a system server process runs in the framework layer 402. The system server process can provide almost all system services for the mobile phone 300, such as power management service (PMS), activity management service (AMS), window management service (WMS), network management service (NMS), input method management service (IMS), and storage management service.
[0104] As shown in FIG5 , the framework layer 402 also runs a vold (volume daemon) process that can be used to manage storage classes in the mobile phone 300 (such as USB, SD card, and flash memory storage devices).
[0105] Exemplarily, the system service thread (such as the storage manager service thread) can communicate with the vold process through a binder service or a socket service.
[0106] All of the above system services are located in the Java framework space and in the system server process. The vold process is located in the Native framework space, and all elements in the vold process are located in the user space.
[0107] 5 , the vold process is located between the Linux kernel layer 404 and the storage manager service in the framework layer 402, and serves as a bridge connecting the Linux kernel layer 404 and the storage manager service in the framework layer 402. For example, after the vold process is started, communication between the Linux kernel layer 404 and the storage manager service in the framework layer 402 can be achieved.
[0108] As shown in Figure 5, the Linux kernel layer 404 can be used to control the storage device driver function of the mobile phone 300. The Linux kernel layer 404 introduces an abstraction layer between the user space (such as the vold process) and the storage device driver. This abstraction layer can be a virtual file system (VFS) layer. Among them, the VFS can provide a system interface for the user space (such as the vold process). The vold process can make a system call (system call) to invoke the system interface provided by the VFS to achieve communication with the storage device driver in the Linux kernel layer 404.
[0109] The storage device driver is used to drive the hardware of the mobile phone 300 (such as the flash memory device 20) to perform corresponding functions. For example, the storage device driver can send corresponding operation instructions to the flash memory device 20 to drive the flash memory device 20 to defragment.
[0110] Furthermore, as shown in FIG6 , the system server process mainly runs the life cycle (broadcast receivers), the storage management service (storage manager service) and the file system virtual cleaning service (fstrim service idler).
[0111] Among them, the broadcast receivers are used to broadcast the status information of the mobile phone 300. For example, the screen status of the mobile phone 300 is broadcast. The storage manager service is used to monitor the status information of the mobile phone 300. For example, when the storage manager service monitors that the screen of the mobile phone 300 is off, the storage manager service detects whether the mobile phone 300 is in a charging state. If the mobile phone 300 is in a charging state, on the one hand, the storage manager service sends a notification to the vold process through the binder communication mechanism. For example, the storage manager service can call a preset interface (such as runidleMaint) to send a notification to the vold process to notify the vold process that the mobile phone 300 is in a screen-off and charging state.
[0112] On the other hand, the storage manager service sends a request to the fstrim service idler to request the virtual occupied physical address. Then, after receiving the request from the storage manager service, the fstrim service idler returns the virtual occupied physical address to the storage manager service. For example, the storage manager service can call a preset interface (such as scheduleFstrim) to send a request to the fstrim service idler. Correspondingly, the fstrim service idler can call a preset interface (such as runidleMaint) to return the virtual occupied physical address to the storage manager service. Among them, the virtual occupied physical address described here refers to the physical address where the fragmented file is stored in the storage block. In this way, in an embodiment of the present application, the fstrim service idler returns the virtual occupied physical address to the storage manager service, that is, the strim service idler returns the fragmentation status of the flash memory storage device to the storage manager service, and gives the fragmentation status as needing to be sorted. Then, the storage manager service continues to execute the following steps according to the virtual occupied physical address returned by the fstrim service idler, such as continuing to send notifications to the vold process, and then the vold process sends instructions to the Linux kernel layer 404 to instruct the flash memory controller to perform defragmentation.
[0113] Still as shown in FIG6 , the vold process mainly runs the vold native service, the device defragmentation service (runDeGc), and the trim driver.
[0114] The vold native service receives notifications from the storage manager service, specifically, a notification that the screen of mobile phone 300 is off and charging. After receiving the notification, the vold native service returns a result to the storage manager service, indicating that the notification has been received.
[0115] Furthermore, after the vold native service receives the notification from the storage manager service, the vold native service can detect whether the duration that the mobile phone 300 is in the screen-off state meets the preset duration (such as 5 minutes or 10 minutes). If the duration that the mobile phone 300 is in the screen-off state meets the preset duration, the vold native service calls the preset interface (such as runidleMaint) to start runDeGc and the function (trim). For example, after runDeGc is started, it can send an instruction to the storage device driver (storage device driver) in the Linux kernel layer 404 to defragment the flash memory storage device; after trim is started, it can send the virtual occupied physical address to the storage device driver in the Linux kernel layer 404.
[0116] Still as shown in Figure 6, the storage device driver includes a first UFS driver (also called a getDeFregStatus driver), a second UFS driver (also called a trigDeGc driver), and a trim driver. The first UFS driver is used to obtain the fragmentation status from the flash memory device; the second UFS driver is used to drive the flash memory device to perform defragmentation; and the trim driver is used to send the virtual occupied physical addresses to the flash memory device. Furthermore, the flash memory device can perform defragmentation based on the virtual occupied physical addresses sent by the trim driver.
[0117] Exemplarily, after runDeGc is started, it issues an instruction to the first UFS driver to defragment the flash memory device. After receiving the instruction from runDeGc, the first UFS driver obtains the fragmentation status from the flash memory device. Then, after the first UFS driver obtains the fragmentation status of the flash memory device, if the fragmentation status of the flash memory device indicates that defragmentation is required, the first UFS driver issues a drive instruction to the second UFS driver. After receiving the drive instruction from the first UFS driver, the second UFS driver drives the flash memory device to defragment.
[0118] For example, after runDeGc is started, it calls a preset interface (such as manual_gc) to issue a defragmentation instruction to the first UFS driver. After receiving the instruction issued by runDeGc, the first UFS driver calls a preset interface (such as getDeFregStatus) to obtain the fragmentation status from the flash memory device. Then, after the first UFS driver obtains the fragmentation status of the flash memory device, if the fragmentation status of the flash memory device indicates that defragmentation is required, the first UFS driver calls a preset interface (trigDeGc) to issue a driver instruction to the second UFS driver. The second UFS driver drives the flash memory controller to defragment through the CMD (command) command.
[0119] The present invention provides a fragmented file defragmentation method applicable to a terminal having a management device and a flash memory device. The flash memory device includes a flash memory controller and a storage medium, wherein the storage medium includes M memory blocks. For ease of description, the present invention uses the management device and the flash memory controller as examples to illustrate the technical solution provided by the present invention.
[0120] As shown in FIG7 , the method provided in the embodiment of the present application may include the following steps.
[0121] S501: In response to a specified condition being met, the management device sends a first instruction to the flash memory controller.
[0122] The first instruction is used to query the fragmentation status of the flash memory device. The management device may be a CPU in a mobile phone.
[0123] Exemplarily, as shown in FIG. 5 and FIG. 6 , the management device may issue the first instruction to the flash memory controller via runDeGc called by a preset interface (such as getDeFregStatus) included in a storage device driver (storage device driver) in the Linux kernel layer 404 .
[0124] In some embodiments, the first instruction sent by the management device to the flash memory controller may be, for example: Read Fregmentation Status.
[0125] Accordingly, the flash memory controller receives a first instruction from the management device.
[0126] In some embodiments, in order to avoid affecting the flash memory controller's ability to organize storage space on the storage medium, the management device may send a first instruction to the flash memory controller in response to a specified condition being met. The specified condition may include: the mobile phone being in a screen-off state and / or the mobile phone's battery level meeting a preset condition.
[0127] For example, the management device can send the first instruction to the flash memory controller when the mobile phone is in the screen-off state. In this way, when the management device inquires that the flash memory needs to be defragmented, since the screen is in the off state at this time, defragmentation is performed at this time, which is conducive to improving the efficiency of defragmentation.
[0128] Exemplarily, the management device can send a first instruction to the flash memory controller when the phone is in a screen-off state. Exemplarily, as shown in FIG6 , the management device can register with broadcast receivers to monitor phone status information by running the storage manager service of framework layer 402. When the storage manager service detects that the phone is screen-off, the management device sends the first instruction to the flash memory controller by running the storage manager service of framework layer 402.
[0129] Furthermore, since the flash memory controller consumes a lot of power when defragmenting, that is, the flash memory controller requires a higher power level from the mobile phone, the management device can send a first instruction to the flash memory controller when the mobile phone's power level information meets a preset condition. The preset condition can be that the mobile phone is in a charging state; or that the mobile phone's power level is greater than a preset power level (e.g., 20%).
[0130] In an embodiment of the present application, the management device may send a first instruction to the flash memory controller when the mobile phone is in a screen-off state and / or the battery level information satisfies a preset condition. The following embodiment uses an example in which the management device sends a first instruction to the flash memory controller when the mobile phone is in a screen-off state and the battery level information satisfies a preset condition.
[0131] In some embodiments of the present application, when the management device determines that the mobile phone is in the screen-off and charging state, it can trigger the mobile phone to start a timer. When the timer expires, the management device sends a first instruction to the flash memory controller. The timer duration can be set according to specific needs (e.g., 5 minutes or 10 minutes) and is not limited.
[0132] In other words, when the management device determines that the mobile phone is in the screen-off and charging state, and the duration of the mobile phone's screen-off state (such as the second preset duration) meets 5 minutes or 10 minutes, the management device sends a first instruction to the flash memory controller.
[0133] In other embodiments of the present application, when the management device determines that the mobile phone is in the screen-off state and the battery level of the mobile phone is greater than 20%, it can trigger the mobile phone to start a timer to start timing. When the duration of the mobile phone's screen-off state meets a second preset duration, the management device sends a first instruction to the flash memory controller.
[0134] In some other embodiments of the present application, when the mobile phone is in the screen-off and charging state, and the current time is within a preset time period (e.g., 3:00-6:00 a.m.), the management device sends a first instruction to the flash memory controller. Of course, the management device can also send the first instruction to the flash memory controller when the mobile phone is in the screen-off state, the mobile phone battery level is greater than 20%, and the current time is within the preset time period.
[0135] It should be noted that the above is merely an example of when the management device sends the first instruction to the flash memory controller and does not constitute a limitation of this application. Of course, the management device can also send the first instruction to the flash memory controller at other appropriate times, as long as it does not affect the flash memory controller's organization of storage space on the storage medium, and thus falls within the scope of protection of this application.
[0136] To prevent the flash memory controller from frequently defragmenting the flash memory device, which would affect the lifespan of the flash memory device, the management device may further detect a first storage space and a first duration of the flash memory device. If the management device determines that the first storage space matches the first duration, the management device sends a first instruction to the flash memory controller. The first storage space indicates the available storage space of the flash memory device, and the first duration indicates the duration between the current moment and the last time the flash memory controller defragmented the storage space of the flash memory device.
[0137] In some embodiments, matching the first storage space with the first duration means that, in the first storage space, the first duration is greater than or equal to a target duration. The target duration is a pre-configured time interval between two consecutive defragmentations of the storage space of the flash memory device. The target duration is set to avoid frequent defragmentation of the flash memory device.
[0138] That is to say, in an embodiment of the present application, in the first storage space, when the interval between the current moment of the flash memory device and the last time the storage space of the flash memory device was defragmented is greater than or equal to the target duration, the management device sends a first instruction to the flash memory controller, and the flash memory controller performs defragmentation, thereby avoiding the problem of frequent defragmentation by the flash memory controller, which affects the life of the flash memory device.
[0139] For example, the management device may query the stored registration information for a target duration corresponding to the first storage space. If the first duration is greater than or equal to the target duration, the management device determines that the first storage space matches the first duration, and then sends the first instruction to the flash memory controller. Conversely, if the first duration is less than the target duration, the management device determines that the first storage space does not match the first duration, and then does not send the first instruction to the flash memory controller, i.e., does not execute S501.
[0140] In some embodiments, the management device stores registration information that records the correspondence between the first storage space and the target duration. The correspondence can be stored in the management device in the form of a table or array. For example, the correspondence can be as shown in Table 1 below.
[0141] Table 1
[0142] It should be noted that the correspondence shown in Table 1 is only an example, and of course it can also include first storage spaces of other sizes and corresponding first durations, which are not limited in this application. In this way, the management device can quickly determine whether the first storage space matches the first duration based on the above Table 1. For example, if the first storage space is 20% GB and the first duration is 5 hours, according to Table 1, since the target duration corresponding to the first storage space of 20% GB is 4 hours, and the first duration is 5 hours, the first duration is greater than the target duration, and the first storage space matches the first duration.
[0143] In this way, the management device can send the first instruction to the flash memory controller when the first storage space matches the first time length. That is, under the first storage space, when the interval between the flash memory controller and the last defragmentation is greater than the target time length, the management device sends the first instruction to the flash memory controller, thereby avoiding the problem of frequent defragmentation of the flash memory controller, which affects the life of the flash memory storage device.
[0144] It should be noted that in the embodiment of the present application, the management device may send the first instruction to the flash memory controller when the mobile phone is in the screen-off state, the battery level information of the mobile phone meets the preset conditions, and the first storage space matches the first duration; alternatively, the management device may send the first instruction to the flash memory controller when the first storage space matches the first duration. This application is not limited to this.
[0145] S502: In response to the first instruction, the flash memory controller scans data stored in the flash memory device to obtain a fragmentation status of the flash memory device.
[0146] In some embodiments, the fragmentation state of a flash memory device includes a first state and a second state. The first state may indicate that the physical addresses corresponding to the fragmented files stored on the storage medium are discontinuous; the second state may indicate that the physical addresses corresponding to the fragmented files stored on the storage medium are continuous. That is, in embodiments of the present application, when the fragmentation state of the flash memory device is the first state, it indicates that the fragments of the flash memory device need to be defragmented; when the fragmentation state of the flash memory device is the second state, it indicates that the fragments of the flash memory device do not need to be defragmented.
[0147] The fragmentation status returned by the flash memory controller to the processor 101 may be represented by a character segment. For example, the fragmentation status returned by the flash memory controller to the processor 101 may be "0" or "1." "0" may indicate that the fragmentation status of the flash memory device 20 is the first status, and "1" may indicate that the fragmentation status of the flash memory device 20 is the second status.
[0148] Exemplarily, as shown in FIG6 , the management device may execute the first UFS driver and call a preset interface (such as getDeFregStatus) to obtain the fragmentation status from the flash memory device.
[0149] S503: The flash memory controller sends a first message to the management device.
[0150] The first message is used to indicate the fragmentation state of the flash memory device, for example, to indicate that the fragmentation state of the flash memory device is the first state or the second state.
[0151] Accordingly, the management device receives the first message from the flash memory controller.
[0152] Exemplarily, the first message may be, for example, Fregmentation Status.
[0153] In some embodiments, the fragmentation status returned by the flash memory controller to the management device can be represented by a character segment. For example, the flash memory controller can return "0" or "1" to the management device. "0" can indicate that the fragmentation status of the flash memory device is a first state, and "1" can indicate that the fragmentation status of the flash memory device is a second state. In other words, the first message sent by the flash memory controller to the management device carries the character segment "0" or "1" used to indicate the fragmentation status of the flash memory device. That is, the first message includes the character segment "0" or "1."
[0154] In some embodiments, based on S502, after the flash memory controller obtains the fragmentation status of the flash memory storage device, the flash memory controller returns the fragmentation status of the flash memory storage device to the management device. If the fragmentation status of the flash memory device returned by the flash memory controller to the management device is a first status, that is, the first message sent by the flash memory controller to the management device includes the character segment "0", then the management device can determine based on the first message that the physical addresses corresponding to the fragmented files stored in the storage medium are discontinuous, so that the management device can respond to the first message and instruct the flash memory controller to sort out the fragmented files on the storage medium. Correspondingly, if the fragmentation status of the flash memory device returned by the flash memory controller to the management device is a second status, that is, the first message sent by the flash memory controller to the management device includes the character segment "1", then the management device can determine based on the first message that the physical addresses corresponding to the fragmented files stored in the storage medium are continuous, so that the management device can respond to the first message without instructing the flash memory controller to sort out the fragmented files on the storage medium.
[0155] S504: In response to the first message, the management device sends a second instruction to the flash memory controller.
[0156] The second instruction is used to instruct the flash memory controller to organize the storage space of the storage medium.
[0157] Correspondingly, the flash memory controller receives a second instruction from the management device.
[0158] Exemplarily, the second instruction may be, for example, starting host start device defragmentation (Host start defrag).
[0159] S505 : In response to the second instruction, the flash memory controller organizes storage space of the storage medium.
[0160] Exemplarily, after the flash memory controller organizes the storage space of the storage medium, that is, after the flash memory controller performs defragmentation, the physical addresses corresponding to the multiple fragmented files stored in the storage medium are continuous.
[0161] To sum up, in an embodiment of the present application, the management device can actively send a first instruction to the flash memory controller to query the fragmentation status of the flash memory storage device; when the fragmentation status of the flash memory storage device is the first state, it indicates that the physical addresses corresponding to multiple fragmented files stored in the storage medium are discontinuous, and then the management device sends a second instruction to the flash memory controller. Since the second instruction is used to instruct the flash memory controller to organize the storage space of the storage medium, the flash memory controller can organize the storage space of the storage medium after receiving the second instruction of the management device, so that the fragments of the flash memory storage device can be organized in time, and the read and write performance of the flash memory storage device can be restored in time.
[0162] In some embodiments, as shown in FIG8 , repeated experiments have shown that by adopting the technical solution provided in the embodiment of the present application, the fragments of the flash memory storage device can be timely defragmented and the read and write performance of the flash memory storage device can be restored in a timely manner, so that the read and write performance of the flash memory storage device can be improved by more than 30%.
[0163] In some embodiments, after the management device sends the second instruction to the flash memory controller, i.e., after the management device instructs the flash memory controller to defragment the storage space of the storage medium, the management device may periodically query the flash memory controller for the progress of defragmenting the storage space of the storage medium. For example, as shown in FIG9 , after the management device sends the second instruction to the flash memory controller, the method further includes the following steps.
[0164] S506: The management device periodically sends a fourth instruction to the flash memory controller.
[0165] The fourth instruction is used to query the progress of the flash memory controller in organizing the storage space of the storage medium.
[0166] Exemplarily, the fourth instruction may be, for example, Check defrag progress.
[0167] S507 : In response to the fourth instruction, the flash memory controller queries the progress of organizing the storage space of the storage medium.
[0168] S508: The flash memory controller sends the progress of organizing the storage space of the storage medium to the management device.
[0169] Accordingly, the management device receives the storage space arrangement progress of the storage medium from the flash memory controller.
[0170] In some embodiments, the progress of organizing the storage space of the storage medium sent by the flash memory controller to the management device may be, for example, in progress or completed.
[0171] Exemplarily, if the progress of organizing the storage space of the storage medium sent by the flash memory controller to the management device is "in progress", the management device re-sends the fourth instruction to the flash memory controller, ie, re-executes steps S506-S508.
[0172] It should be noted that when the flash memory controller determines that the physical addresses corresponding to multiple fragmented files stored on the storage medium are discontinuous, it indicates that the flash memory controller has not yet completed the process of defragmenting the storage space of the storage medium. Furthermore, the flash memory controller sends a message to the management device indicating that the progress of defragmenting the storage space of the storage medium is "in progress."
[0173] In some embodiments, when the flash memory controller queries that the progress of organizing the storage space of the storage medium is complete, the flash memory controller continues to perform the following steps (eg, perform S509 ).
[0174] S509: The flash memory controller sends a second message to the management device.
[0175] The second message is used to indicate that the second physical addresses corresponding to the multiple fragmented files in the Z storage blocks are continuous. That is, the second message is used to indicate that the storage space of the storage medium has been organized. Exemplarily, the second message sent by the flash memory controller to the management device can be, for example, "completed."
[0176] Correspondingly, the management device receives a second message from the flash memory controller.
[0177] As shown in FIG9 , in an embodiment of the present application, after the management device sends the second instruction to the flash memory controller, that is, after the management device instructs the flash memory controller to defragment the storage space of the storage medium, the management device can set a loop operation so that the management device can periodically query the flash memory controller on the progress of defragmenting the storage space of the storage medium. In other words, in this loop operation, the management device can periodically send a fourth instruction to the flash memory controller to query the flash memory controller on the progress of defragmenting the storage space of the storage medium. If the progress of defragmenting the storage space of the storage medium returned by the flash memory controller to the management device is "in progress", the management device continues to send the fourth instruction to the flash memory controller until the progress of defragmenting the storage space of the storage medium returned by the flash memory controller to the management device is "completed", indicating that the flash memory controller has completed the defragmentation, and then the management device can end the loop operation.
[0178] Furthermore, as shown in FIG10 , after the flash memory controller finishes organizing the storage space of the storage medium, ie, when the progress of organizing the storage space of the storage medium returned by the flash memory controller to the management device is “completed”, the method further includes the following steps.
[0179] S510 : In response to the second message, the management device sends a third instruction to the flash memory controller.
[0180] The third instruction is used to instruct the flash memory controller to stop organizing the storage space of the storage medium.
[0181] Correspondingly, the flash memory controller receives the third instruction from the management device.
[0182] Exemplarily, the third instruction may be, for example, Stop defrag.
[0183] S511 : In response to the third instruction, the flash memory controller stops organizing the storage space of the storage medium.
[0184] In some embodiments, after the management device sends the second instruction to the flash memory controller, it can trigger the mobile phone to start a timer to start timing. When the timer times out, if the flash memory controller does not send a second message to the management device, the management device actively sends a third instruction to the flash memory controller to instruct the flash memory controller to stop organizing the storage space of the storage medium.
[0185] For example, the duration of the timer can be set according to specific needs (e.g., set to the first preset duration). Therefore, in this embodiment of the present application, if the management device does not receive the second message from the flash memory controller within the first preset duration, the management device proactively sends a third instruction to the flash memory controller.
[0186] In this way, it is possible to avoid the problem that the flash memory controller takes too long to organize the storage space of the storage medium, which increases the function of the flash memory device and thus affects the life of the flash memory device.
[0187] It should be noted that the contents recorded in each embodiment of the present application can explain and illustrate the technical solutions in other embodiments of the present application, and the technical features recorded in each embodiment can also be applied in other embodiments and combined with the technical features in other embodiments to form a new solution. The present application only lists a few embodiments for illustration, and does not mean that the present application is limited to this.
[0188] An embodiment of the present application provides a management device, which is applied to a terminal having a flash memory storage device, the flash memory device including a flash memory controller and a storage medium, the storage medium including M storage blocks; the management device includes a memory and one or more processors, the memory stores computer program code, the computer program code includes computer instructions, and when the computer instructions are executed by the processor, the management device can perform the various functions or steps performed by the management device in the above embodiment.
[0189] An embodiment of the present application provides a flash memory storage device, which is used in a terminal with a management device. The flash memory storage device includes a flash memory controller and a storage medium. The storage medium stores computer program code, and the computer program code includes computer instructions. When the computer instructions are executed by the flash memory controller, the flash memory controller can perform the various functions or steps performed by the flash memory controller in the above embodiment.
[0190] An embodiment of the present application provides a terminal including a management device and a flash memory device; the terminal also includes a display screen, a memory, and one or more processors; the memory stores computer program code, which includes computer instructions. When the computer instructions are executed by the processor, the terminal can perform the various functions or steps performed by the mobile phone in the above embodiments. The structure of the terminal can refer to the structure of the mobile phone shown in Figure 3.
[0191] The present application also provides a chip system, as shown in FIG11 . The chip system 1800 includes at least one processor 1801 and at least one interface circuit 1802. The processor 1801 may be the processor 310 shown in FIG3 in the above embodiment. The interface circuit 1802 may be, for example, an interface circuit between the processor 310 and an external memory, or an interface circuit between the processor 310 and the internal memory 321.
[0192] The above-mentioned processor 1801 and interface circuit 1802 can be interconnected through lines. For example, the interface circuit 1802 can be used to receive signals from other devices (such as the memory of an electronic device). For another example, the interface circuit 1802 can be used to send signals to other devices (such as the processor 1801). Exemplarily, the interface circuit 1802 can read the instructions stored in the memory and send the instructions to the processor 1801. When the instructions are executed by the processor 1801, the management device can execute the various steps executed by the management device in the above-mentioned embodiment, or the flash memory controller can execute the various steps executed by the flash memory controller in the above-mentioned embodiment. Of course, the chip system can also include other discrete devices, which is not specifically limited in the embodiments of the present application.
[0193] Through the description of the above implementation methods, technical personnel in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.
[0194] In the several embodiments provided in this application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the modules or units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another device, 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 through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0195] The units described as separate components may or may not be physically separate, and the components shown as units may be one physical unit or multiple physical units, that is, they may be located in one place or distributed in multiple places. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0196] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0197] 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 readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially 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, which is stored in a storage medium and includes several instructions for enabling a device (which can be a single-chip microcomputer, chip, etc.) or a processor (processor) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0198] The above content is only a specific embodiment of this application, but the scope of protection of this application is not limited to this. Any changes or replacements within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.
Claims
1. A method for organizing fragmented files, characterized in that: Applied to a terminal having a management device and a flash memory device, the flash memory device including a flash memory controller and a storage medium, the storage medium storing fragmented files, the method comprising: The management device sends a first instruction to the flash memory controller; the first instruction is used to query the fragmentation status of the fragmented file stored on the storage medium; The management device receives a first message from the flash memory controller; the first message is used to indicate the fragmentation status of the fragmented file stored on the storage medium; In response to the first message, when the fragmentation status indicates that the physical addresses corresponding to the fragmented files stored on the storage medium are discontinuous, the management device sends a second instruction to the flash memory controller; the second instruction is used to instruct the flash memory controller to organize the fragmented files on the storage medium.
2. The method according to claim 1, characterized in that The management device sends a first instruction to the flash memory controller, including: The management device obtains a first storage space and a first duration, wherein the first storage space is used to indicate the available storage space of the storage medium, and the first duration is used to indicate the interval between the current moment and the last time the flash memory controller defragmented files on the storage medium; If the management device determines that the first storage space matches the first duration, the management device sends the first instruction to the flash memory controller.
3. The method according to claim 2, characterized in that The management device determines that the first storage space matches the first duration, including: The management device searches the stored registration information for a target duration corresponding to the first storage space; the target duration is a pre-configured interval between two consecutive defragmentation operations on the storage medium; If the first duration is greater than or equal to the target duration, the management device determines that the first storage space matches the first duration; The registration information records the corresponding relationship between the first storage space and the target duration.
4. The method according to any one of claims 1 to 3, characterized in that The management device sends a first instruction to the flash memory controller, including: If the terminal is in a screen-off and charging state, and the duration of the terminal being in the screen-off state meets a first preset duration, the management device sends a first instruction to the flash memory controller; or, Within a preset time period, if the terminal is in a screen-off and charging state, the management device sends a first instruction to the flash memory controller.
5. The method according to claim 1, wherein The method further comprises: The management device receives a second message from the flash memory controller; the second message is used to indicate that the physical addresses corresponding to the fragmented files stored on the storage medium are continuous; In response to the second message, the management device sends a third instruction to the flash memory controller; the third instruction is used to instruct the flash memory controller to stop defragmenting the fragmented files on the storage medium.
6. The method according to claim 1, characterized in that The method further comprises: If the management device does not receive the second message within the first preset time period, the management device sends a third instruction to the flash memory controller; The second message is used to indicate that the physical addresses corresponding to the fragmented files stored on the storage medium are continuous; and the third instruction is used to instruct the flash memory controller to stop sorting the fragmented files on the storage medium.
7. The method according to claim 5, characterized in that Before the management device receives the second message from the flash memory controller, the method further includes: The management device sends a fourth instruction to the flash memory controller; the fourth instruction is used to query the flash memory controller for progress in sorting out the fragmented files on the storage medium; The management device receives a third message from the flash memory controller; the third message is used to indicate that the progress of defragmenting the fragmented files on the storage medium is incomplete; In response to the third message, the management device resends the fourth instruction to the flash memory controller until the second message from the flash memory controller is received.
8. The method according to any one of claims 1 to 7, characterized in that The management device sends a second instruction to the flash memory controller, including: The management device calls the storage management service in the application framework layer and sends the second instruction to the storage device driver in the kernel layer; The method further comprises: The management device calls the storage device driver of the kernel layer to drive the flash memory controller to organize the fragmented files on the storage medium.
9. The method according to claim 8, characterized in that The management device calls the storage management service in the application framework layer and sends the second instruction to the storage device driver in the kernel layer, including: The management device calls the storage management service in the application framework layer and sends the second instruction to the storage device driver in the kernel layer through the vold process.
10. A method for organizing fragmented files, characterized in that: The method is applied to a terminal having a management device and a flash memory device, wherein the flash memory device includes a flash memory controller and a storage medium, and the storage medium stores fragmented files; the method includes: The flash memory controller receives a first instruction from the management device; the first instruction is used to query the fragmentation status of the fragmented file stored on the storage medium; In response to the first instruction, the flash memory controller sends a first message to the management device; the first message is used to indicate the fragmentation status of the fragmented file stored on the storage medium; In a case where the fragmentation status indicates that physical addresses corresponding to the fragmented files stored on the storage medium are discontinuous, the flash memory controller receives a second instruction from the management device; In response to the second instruction, the flash memory controller defragments the fragmented files on the storage medium.
11. The method according to claim 10, characterized in that The method further comprises: If the flash memory device meets the preset arrangement condition, the flash memory controller arranges the fragmented files on the storage medium based on the firmware algorithm inside the flash memory device; Among them, the preset sorting conditions include: the available storage space of the storage medium is less than the preset storage space; or the flash memory storage device meets the preset cycle, and the preset cycle is the cycle of the flash memory controller set in the firmware algorithm to sort out the fragmented files on the storage medium.
12. The method according to claim 10 or 11, characterized in that The method further comprises: The flash memory controller sends a second message to the management device; the second message is used to indicate that the storage The physical addresses corresponding to the fragmented files stored on the medium are continuous; The flash memory controller receives a third instruction from the management device; In response to the third instruction, the flash memory controller stops defragmenting the fragmented files on the storage medium.
13. The method according to claim 10 or 11, characterized in that The method further comprises: If the time length for the flash memory controller to organize the fragmented files on the storage medium is greater than a first preset time length, the flash memory controller receives a third instruction from the management device; In response to the third instruction, the flash memory controller stops defragmenting the fragmented files on the storage medium.
14. The method according to claim 12, characterized in that The flash memory controller sends a second message to the management device, including: The flash memory controller receives a fourth instruction from the management device; In response to the fourth instruction, the flash memory controller queries the progress of the flash memory controller in defragmenting the fragmented files on the storage medium; If the flash memory controller has not completed the defragmentation of the fragmented files on the storage medium, the flash memory controller re-receives the fourth instruction from the management device until the flash memory controller completes the defragmentation of the fragmented files on the storage medium, and the flash memory controller sends the second message to the management device.
15. A management device, characterized in that: Applied to a terminal having a flash memory device, the flash memory device includes a flash memory controller and a storage medium, and the storage medium stores fragmented files; wherein the management device is used to execute the method according to any one of claims 1 to 9.
16. A flash memory device, characterized in that: Applied to a terminal having a management device, the flash memory storage device includes a flash memory controller and a storage medium, and the storage medium stores fragmented files; wherein the flash memory controller is used to execute the method according to any one of claims 10-14.
17. A terminal, characterized in that: include: A management device and a flash memory storage device; the flash memory storage device includes a flash memory controller and a storage medium, and the storage medium stores fragmented files; wherein the management device is used to execute the method as described in any one of claims 1-9; the flash memory controller is used to execute the method as described in any one of claims 10-14.
18. A chip system, characterized in that: The method comprises at least one processor and at least one interface circuit; the processor and the interface circuit are interconnected via a line; wherein the processor is used to execute computer instructions to implement the method according to any one of claims 1 to 9; or to implement the method according to any one of claims 10 to 14.