Storage power-down data storage method
By dividing the data storage and log recording areas in non-volatile memory, recording reverse operation instructions, and performing rollback operations in case of abnormal restart, the problem of data inconsistency caused by system power failure is solved, ensuring data integrity and system reliability.
Patent Information
- Application Number
- CN202511085444.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-04
- Publication Date
- 2025-11-18
AI Technical Summary
When the system loses power under abnormal conditions, the data in the non-volatile memory is prone to inconsistency or incompleteness, leading to data loss or system startup failure, which affects system reliability and data integrity.
The non-volatile memory is divided into a data storage area and a log recording area. Inverse operation instructions are recorded and a rollback operation is performed in case of abnormal restart. The atomicity of status flags is ensured through atomic operations. Combined with data verification and power protection mechanisms, data integrity is ensured.
It enables precise rollback of data to its pre-update state after an abnormal restart, eliminating uncertain states, ensuring the atomicity of data updates and the reliability and observability of the system, and avoiding data loss and system failure.
Abstract
Description
Technical Field
[0001] This invention relates to the field of data storage technology, specifically a method for saving data in a memory when power is off. Background Technology
[0002] In modern electronic devices, embedded systems, servers, and industrial control, non-volatile memory (NDRAM) is widely used to store critical configuration parameters, operational status information, and transactional data because it retains data even after power loss. Common NDRAMs include flash memory, ferroelectric memory, magnetoresistive memory, and random access memory (RAM) powered by batteries or capacitors. However, during operation, the system may encounter unexpected power outages, voltage drops, hardware failures, or software crashes. These anomalies typically occur during data write operations, easily leading to inconsistent or incomplete data in the memory, i.e., partial write problems. For example, if a power outage occurs while the system is updating data in a storage unit, after the old data has been erased and before the new data is fully written, the storage unit will be in a damaged or unknown state, resulting in the permanent loss of valuable data or system startup failure, severely impacting system reliability and data integrity. Summary of the Invention
[0003] In view of the problems existing in the prior art, the present invention discloses a method for saving data in a memory when power is off, the technical solution of which includes the following steps: (1) Divide the non-volatile memory into independent data storage area and log recording area; (2) When a data update request is received, the inverse operation instruction corresponding to the update operation is written into the log recording area; (3) Perform a data update operation to write the new data to the target address of the data storage area; (4) Mark the inverse operation instruction in the log recording area as completed through atomic operations; (5) When the system restarts abnormally, scan the log recording area. If there are unmarked reverse operation instructions, execute the instruction to roll back the data; if all reverse operation instructions are marked as completed, clear the log recording area.
[0004] As a preferred technical solution of the present invention, the reverse operation instruction written in step (2) contains the original data value of the target address.
[0005] As a preferred technical solution of the present invention, the atomic operation in step (4) realizes the status flag update through a hardware-supported single instruction write operation.
[0006] As a preferred technical solution of the present invention, a data verification code is set in the data storage area to verify the data integrity upon restart. If the verification fails, a log rollback mechanism is triggered.
[0007] As a preferred embodiment of the present invention, the non-volatile memory is FRAM, MRAM or battery-backed NVRAM.
[0008] As a preferred technical solution of the present invention, a power protection step is set up. Specifically, when the voltage is lower than the threshold, an emergency interruption is triggered, and the backup capacitor energy is used to complete the operations of steps (2) to (4) first.
[0009] As a preferred embodiment of the present invention, the log recording area uses a circular queue structure to store the reverse operation instructions, and the queue head pointer state is updated to atomic operation.
[0010] As a preferred technical solution of the present invention, the operation timestamp is written in step (2) at the same time, and the rollback is performed according to the timestamp order when restarting.
[0011] The beneficial effects of this invention are as follows: By recording the reverse operation instructions, after an abnormal restart, by executing the incomplete reverse operation instructions, the partially updated data can be accurately rolled back to the valid state before the update. The atomic operation status marking ensures the "atomicity" of the two key links of log recording and status marking. That is, either both log recording and status marking are successful (indicating that the data update is successful and no rollback is needed), or neither is successful (indicating that the data update is incomplete and rollback is needed). This fundamentally eliminates uncertain states such as "log recording is successful but status marking is incomplete" or "status marking fails", making the recovery logic clear and reliable. Detailed Implementation
[0012] Example 1
[0013] This invention discloses a method for saving data in a memory when power is off. The technical solution includes the following steps: (1) Divide the non-volatile memory into independent data storage area and log recording area; (2) When a data update request is received, the inverse operation instruction corresponding to the update operation is written into the log recording area; (3) Perform a data update operation to write the new data to the target address of the data storage area; (4) Mark the inverse operation instruction in the log recording area as completed through atomic operations; (5) When the system restarts abnormally, scan the log recording area. If there are unmarked reverse operation instructions, execute the instruction to roll back the data; if all reverse operation instructions are marked as completed, clear the log recording area.
[0014] As a preferred embodiment of the present invention, the non-volatile memory is a battery-backed NVRAM.
[0015] As a preferred technical solution of the present invention, the written reverse operation instruction contains the original data value of the target address, ensuring the accuracy of the rollback operation, reducing the amount of log recording and accelerating the rollback process. When the timestamp record is abnormal, the original value can be used as the final recovery basis, providing double protection.
[0016] As a preferred technical solution of the present invention, the atomic operation in step (4) realizes the status mark update through the hardware-supported single instruction write operation, eliminating the risk of "intermediate state". The single instruction write operation is indivisible, and even if the power is lost, there will be no "half-write" state. If the mark is successfully updated, the data update will definitely be completed. If the mark is not updated, the system will definitely trigger a rollback after restarting.
[0017] As a preferred technical solution of the present invention, a data verification code is set in the data storage area to verify the data integrity upon restart. If the verification fails, a log rollback mechanism is triggered to build a dual protection system, transforming passive data loss into active repair and avoiding manual intervention.
[0018] As a preferred technical solution of the present invention, the log recording area uses a circular queue structure to store the reverse operation instructions, and the state of the queue head pointer is updated to atomic operation. The circular queue combined with the atomic pointer greatly improves the reliability.
[0019] As a preferred technical solution of the present invention, the operation timestamp is written in step (2) at the same time, and the rollback is performed according to the timestamp order when restarting, which can effectively ensure the consistency of data logic, enhance the observability of the system, and realize accurate state reconstruction.
[0020] Components not described in detail in this article are existing technologies.
[0021] While the specific embodiments of the present invention have been described in detail above, the present invention is not limited to the above embodiments. Within the scope of knowledge possessed by those skilled in the art, various changes can be made without departing from the spirit of the present invention, and modifications or variations without creative effort are still within the protection scope of the present invention.
Claims
1. A method for saving data in a memory when power is off, characterized in that, Includes the following steps: (1) Divide the non-volatile memory into independent data storage area and log recording area; (2) When a data update request is received, the inverse operation instruction corresponding to the update operation is written into the log recording area; (3) Perform a data update operation to write the new data to the target address of the data storage area; (4) Mark the inverse operation instruction in the log recording area as completed through atomic operations; (5) When the system restarts abnormally, scan the log recording area. If there are unmarked reverse operation instructions, execute the instruction to roll back the data; if all reverse operation instructions are marked as completed, clear the log recording area.
2. The method for saving data in a memory after power failure according to claim 1, characterized in that: The reverse operation instruction written in step (2) contains the original data value of the target address.
3. The method for saving data in a memory after power failure according to claim 1, characterized in that: The atomic operation in step (4) updates the state flag through a hardware-supported single-instruction write operation.
4. The method for saving data in a memory when power is lost according to claim 1, characterized in that: Set a data checksum in the data storage area to verify data integrity upon restart. If the verification fails, trigger the log rollback mechanism.
5. A method for saving data in a memory after power failure according to claim 1, characterized in that: The non-volatile memory is FRAM, MRAM, or battery-backed NVRAM.
6. A method for saving data in a memory when power is lost, as described in claim 1, characterized in that: It also includes power protection steps.
7. A method for saving data in a memory when power is lost, as described in claim 6, characterized in that: The power protection steps include: (1) When the voltage is below the threshold, an emergency interrupt is triggered; (2) The backup capacitor energy is used to preferentially complete the operations in steps (2) to (4) of claim 1.
8. A method for saving data in a memory when power is lost, as described in claim 1, characterized in that: The log recording area uses a circular queue structure to store inverse operation instructions, and the queue head pointer state is updated to atomic operation.
9. A method for saving data in a memory when power is lost, as described in claim 1, characterized in that: In step (2), an operation timestamp is written simultaneously, and the rollback is executed according to the timestamp order upon restart.