A log storage method and system of an embedded device and a storage medium
By employing a multi-level bit counting method and a ring storage mechanism, the problems of Flash lifespan, retrieval efficiency, and time processing in embedded device log storage are solved, achieving efficient and reliable log storage and retrieval.
Patent Information
- Application Number
- CN202611288264.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-24
- Publication Date
- 2026-11-24
AI Technical Summary
Existing log storage technologies for embedded devices suffer from issues such as Flash lifespan bottlenecks, low log retrieval efficiency, complex and error-prone time processing, and a conflict between real-time performance and data integrity.
A multi-level bit counting method is used to divide the flash memory space, and the number of times the device is powered on and the running time are recorded separately. Combined with ring storage and cache writing mechanism, the flash erase and write frequency is reduced, and the log pointer is maintained efficiently and the timestamp is accurate.
It significantly extends the lifespan of Flash memory, improves log retrieval speed and timestamp accuracy, and ensures system real-time performance and data integrity.
Smart Images

Figure CN123157616A_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of embedded system data storage technology, and specifically relates to a log storage method, system and storage medium for embedded devices. Background Technology
[0002] Embedded devices need to record system logs, fault information, historical data, etc., during operation for fault location and status analysis. Current technologies mostly use NOR Flash (non-volatile flash memory) as the non-volatile storage medium, but this presents the following technical problems: 1. Flash memory lifespan bottleneck: NOR Flash memory typically has a write / erase cycle lifespan of only around 100,000 times. Current technologies usually accumulate runtime by writing once per second, which quickly depletes the Flash memory's lifespan. Although methods such as cached batch writing exist, they cannot fundamentally solve the problem of frequent write / erase cycles caused by long-term operation.
[0003] 2. Low log retrieval efficiency: In a bare-metal environment without a file system, logs are mostly stored in a circular queue. Querying historical logs for a specific power-on cycle requires a full disk scan, which takes several seconds or even tens of seconds, seriously affecting debugging efficiency.
[0004] 3. Time processing is complex and error-prone: Log timestamps involve leap years, month-crossing, and year-crossing calculations, and reverse calculations (deduce historical times from the current time) are extremely prone to errors. After time synchronization, the timestamps of existing logs become out of sync with the system clock, resulting in disordered historical log times.
[0005] 4. Conflict between real-time performance and data integrity: Direct writing to Flash will block real-time tasks, while using cached asynchronous writing carries the risk of log loss due to a full cache. Summary of the Invention
[0006] To address the aforementioned issues, this application provides a log storage method, system, and storage medium for embedded devices, characterized by low write / erase frequency and high efficiency.
[0007] The purpose of this disclosure is to provide a log storage method for embedded devices, including... The flash memory space in the log storage module is divided into multiple areas, including a power-on count recording area and a runtime recording area. The power-on count recording area and the running time recording area are respectively divided into multiple consecutive sectors of preset size, forming corresponding multi-level bit counting areas; Each time the device is powered on, the number of power-on times is accumulated, starting from the first level bit count area in the corresponding multi-level bit count area of the power-on count recording area. A time update operation is performed every second, starting from the first level bit count area in the corresponding multi-level bit count area of the running time recording area, to accumulate the total running time of the device.
[0008] Optionally, each time the device is powered on, starting from the first level of the multi-level bit counter area in the power-on count recording area, the cumulative number of device power-on counts includes, Read the current bit of the current byte in the current level bit counter area. If the bit is 1, write 0 to it, which means 1 time. If the current byte is already 0x00, move to the next byte; if the current level bit counter is full, carry over to the next level bit counter, while erasing the previous level bit counter and resetting the pointer.
[0009] Optionally, a time update operation is performed once per second, starting from the first level bit count area in the corresponding multi-level bit count area of the running time recording area, and the total running time of the device is accumulated, including... Read the current bit of the current byte in the current level bit counter area every second. If the bit is 1, write 0 to it, which represents 1 second. If the current byte is already 0x00, move to the next byte; if the current level bit counter is full, carry over to the next level bit counter, while erasing the previous level bit counter and resetting the pointer.
[0010] Optionally, the cumulative number of device power-ups and the cumulative total device uptime both satisfy the following formula: P = First-level count + Second-level count × (S × 8) + ... + nth-level count × (S × 8)^(n-1); The i-th level count = current byte index × 8 + the number of zeros written in the current byte; In the formula, S is the total number of bytes in each sector, n represents the total number of multi-level bit counting areas in the power-on count recording area or running time recording area, and i represents the i-th level bit counting area, where i is an integer and i∈n.
[0011] Optionally, the plurality of regions further includes a log entry offset address recording area for recording the pointer value of the current write position, wherein, The log entry offset address recording area is divided into two sectors, namely the first-level bit counting area and the second-level bit counting area. The first-level bit counting area records the low-order bits of the pointer, and the second-level bit counting area records the high-order bits of the pointer. Each time a log entry is written, a log pointer update operation is performed, incrementing the first-level count by one; when the first-level bit count area is full, the count is carried over to the second-level bit count area, and the first-level bit count area is erased at the same time. When reading the current pointer: Pointer value = First level count + Second level count × (S × 8).
[0012] Optionally, the plurality of areas also includes a log directory area, in which a directory entry is created each time the device is powered on; wherein, the directory entry includes the power-on sequence number, the running time value at the start of this power-on, the historical log start pointer, the fault log start pointer, the time synchronization time, and the CRC checksum.
[0013] Optionally, the power-on sequence number is obtained from the power-on count record area during this power-on update; The power-on time is updated during this power-on update, and the relative time of this power-on is obtained from the runtime recording area; The log start address is obtained from the log entry offset address recording area during this power-on update; The number of log entries is updated on the next power-on, using the current data in the log entry offset address recording area and subtracting the log start address from the last power-on. Time synchronization: The time synchronization function updates continuously. It obtains the current absolute time of year, month, day, hour, minute, and second through time synchronization. It calculates and records the absolute time of power-on by using the difference between the current relative time and the relative time at the time of power-on. The checksum uses a CRC8 checksum value to determine whether the data entry is valid.
[0014] In this embodiment of the disclosure, the directory entries are stored by taking the power-on sequence number and supporting circular overlay.
[0015] In this embodiment of the disclosure, when time synchronization occurs, the synchronization time in the current power-on directory entry is updated, and the CRC is recalculated.
[0016] Optionally, it also includes using the synchronization time in the current power-on catalog entry or the synchronization time in the historical catalog entry to call the rules for converting seconds to dates to obtain the absolute time.
[0017] Optionally, it also includes periodically checking the cache, retrieving the oldest entry in timestamp order, and performing a lock-free flash write operation; When the buffer is full, the oldest timestamp entry is automatically overwritten, and a warning message is output.
[0018] Optionally, it also includes log queries and directory indexes, including, The user enters the power-on sequence number N to query; Traverse the log directory area, retrieve the log entry containing power-on sequence number N from the directory, obtain the log starting address and the number of entries, and read the log entry directly from the offset address in the flash memory. If a time synchronization mechanism exists, convert the relative timestamp to an absolute time for display; If the directory read fails or there is no directory, a full scan of the log entry storage area is performed, and log entries are read one by one and their CRC is verified.
[0019] Another objective of this disclosure is to provide a log storage system for an embedded device, comprising, The power-on counting module is used to accumulate the number of times the device is powered on each time it is powered on, starting from the first level of the multi-level bit counting area in the power-on count recording area; The runtime accumulation module is used to perform a time update operation once per second, starting from the first level bit count area in the corresponding multi-level bit count area of the runtime recording area, and accumulates the total runtime of the device. The directory management module is used to create a directory entry in the directory area each time the device is powered on. The directory entry includes the power-on sequence number, the running time value at the start of this power-on, the historical log start pointer, the fault log start pointer, the time synchronization time, and the CRC checksum. The time conversion module is used for converting between relative and absolute time. The log storage module is used to store log entries in flash memory using a ring storage method. The cache write module is used to periodically check the cache area, retrieve the oldest entry in timestamp order, and perform lock-free flash write operations; and when the cache area is full, it automatically overwrites the oldest entry with the oldest timestamp and outputs a warning message.
[0020] Another objective of this disclosure is to provide a computer-readable storage medium, characterized in that the computer-readable storage medium stores computer instructions for causing the computer to perform the methods described above.
[0021] Compared with the prior art, this application has the following advantages: By applying a multi-level bit counting method to the three key stages of runtime accumulation, power-on count recording, and log pointer maintenance, the Flash erase / write frequency of each module is reduced from the second level or per operation level to the hour level or tens of thousands of operations level, with a theoretical lifespan of over 100 years.
[0022] Furthermore, the power-on directory index reduces the log query time for a specific power-on cycle from several seconds to milliseconds, and supports time synchronization and traceability, ensuring the absolute time accuracy of historical logs, which greatly improves log retrieval speed.
[0023] Other features and advantages of this application will be set forth in the description which follows, and will be apparent in part from the description, or may be learned by practicing the application. The objectives and other advantages of this application may be realized and obtained by means of the structures pointed out in the description, claims and drawings. Attached Figure Description
[0024] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0025] Figure 1 A schematic flowchart of a log storage method for an embedded device according to an embodiment of this disclosure is shown; Figure 2 A schematic diagram illustrating the principle of multi-level bit counting runtime accumulation in an embodiment of this disclosure is shown. Figure 3 A schematic diagram of a directory entry structure according to an embodiment of this disclosure is shown; Figure 4 A time conversion flowchart according to an embodiment of this disclosure is shown; Figure 5 A schematic diagram of a log cache writing mechanism in an embodiment of this disclosure is shown; Figure 6 This illustration shows a log storage area partitioning diagram according to an embodiment of the present disclosure; Figure 7 A log storage system architecture diagram of an embedded device according to an embodiment of this disclosure is shown. Detailed Implementation
[0026] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0027] like Figure 1As shown in the embodiments of this disclosure, a log storage method for an embedded device is introduced. The method includes: first, dividing the flash memory space in the log storage module into multiple regions, including a power-on count recording region and a runtime recording region; second, dividing the power-on count recording region and the runtime recording region into multiple consecutive sectors of a preset size, forming corresponding multi-level bit counting regions; then, each time the device is powered on, starting from the first level bit counting region in the corresponding multi-level bit counting region of the power-on count recording region, accumulating the number of power-on cycles; finally, performing a time update operation once per second, starting from the first level bit counting region in the corresponding multi-level bit counting region of the runtime recording region, accumulating the total device runtime. The above method reduces the number of flash erase / write cycles from "once per second" or "once per log entry" to "once per few thousand operations," achieving an exponential extension of lifespan.
[0028] Specifically, the basic principle of the multi-level bit counting method involves utilizing the characteristic of NOR Flash (non-volatile memory) where each bit is 1 after erasure and can be changed to 0 after writing (but irreversibly), and using bit counting to record the accumulated value. The 8 bits of a byte are considered as 8 counting bits, with an initial value of 0xFF (8 ones). Each count is written by changing the next bit from 1 to 0. When the byte becomes 0x00, it indicates that 8 counts have been recorded. By cascading multiple sectors, the counting range can be greatly expanded. For example, the size of the first-level sector is defined as S bytes, where S can be 4096, i.e., S = 4KB (kilobytes), but it is not limited to this and depends on the sector size of the FLASH, with 4KB being the most common. Then, the first-level sector can record S × 8 counts. When the first-level sector is full, a carry is triggered: a bit in the second-level sector is written to 0, and the first-level sector is erased to start again. The second-level sector is the same size and can record S × 8 × S × 8 counts. Similarly, a three-level sector can record (S × 8)^3 counts. For example, for a 4KB sector, this value is approximately (32768)^3 = 3.5 × 10^13, far exceeding the device's lifespan. The number of sectors is not limited to the three mentioned above; other sector numbers, such as 2 or 4, are also applicable to this disclosure.
[0029] In this embodiment of the disclosure, such as Figure 6 As shown, the flash memory space in the log storage module is divided into five areas: a power-on count recording area, a runtime recording area, a log directory area, a log entry offset address recording area, and a log entry storage area. Each area is further divided into different sectors, serving as different levels of bit counting areas.
[0030] In this embodiment, a three-level bit counting method is used to accumulate the total device runtime, and it is updated every second; however, it is not limited to this, and two-level bit counting methods are also applicable to the accumulation of the total device runtime in this disclosure. Specifically, the runtime recording area in the NOR Flash is divided into three consecutive sectors (i.e., 3 sectors), which are respectively used as the first-level bit counting area, the second-level bit counting area, and the third-level bit counting area. A time update operation is performed once per second: the current bit of the current byte in the first-level bit counting area is read; if the bit is 1, it is written as 0, representing 1 second; if the current byte is already 0x00, it moves to the next byte; if the first-level bit counting area is full, it carries over to the second-level bit counting area, and simultaneously erases the first-level bit counting area and resets the pointer. Similarly, when the second-level bit counting area is full, it carries over to the third-level bit counting area. When reading the runtime, the calculation formula is: g_runtime_sec (runtime) = Level 1 count + Level 2 count × (S × 8) + Level 3 count × (S × 8)^2.
[0031] The first-level count is calculated as: Current byte index (the index is a tag variable used for addressing) × 8 + the number of zeros written in the current byte. Using a 4KB sector as an example, the maximum count for the first level is 4096 × 8 = 32768 seconds, the maximum count for the second level is 32768 × 32768 = 1,073,741,824 seconds, and the maximum count for the third level is 32768 × 32768 × 32768 = 35,184,372,088,832 seconds, with a cumulative maximum count time of approximately 1.115 million years. Therefore, this runtime counting method far exceeds the device's lifespan, improving the reliability of the accumulated runtime. For example, as shown... Figure 2 As shown, the three-level sector structure of the runtime accumulation area is as follows: The first-level sector (i.e., the first-level bit counter area) is 4KB in size, organized by bytes, and initially all bits are 0xFF. Each write operation writes the next bit to 0. One byte can record for 8 seconds. When all bytes in the first-level sector are 0x00, a carry is triggered. During a carry, the entire first-level sector (4KB) is erased, and the offset pointer is moved to the next byte. The first-level bit counter area has only one 4KB sector, and the second-level and third-level bit counter areas also each have only one 4KB sector. When the first-level bit counter area is full, one bit in the second-level bit counter area is written to 0, and the first-level bit counter area is reset. When the second-level bit counter area is full, a carry is moved to the third-level bit counter area, and the second-level bit counter area is reset.
[0032] In this embodiment, a two-level bit counting method is used to accumulate the number of device power-on times, but it is not limited to two levels; three-level, four-level, etc., are also applicable to this disclosure. Each time the device is powered on, a power-on count update operation is performed. Its implementation logic is exactly the same as the runtime accumulation, where each count represents one power-on. For example, the power-on count recording area in the NOR Flash is divided into two consecutive sectors (i.e., 2 sectors), serving as the first-level bit counting area and the second-level bit counting area, respectively. Each time the device is powered on, an update operation is performed: the current bit of the current byte in the first-level bit counting area is read; if the bit is 1, it is written as 0, representing 1 second; if the current byte is already 0x00, it moves to the next byte; if the first-level bit counting area is full, it carries over to the second-level bit counting area, and simultaneously erases the first-level bit counting area and resets the pointer. The calculation formula is: Number of power-on cycles = First-stage count + Second-stage count × (S × 8).
[0033] The first-level count is calculated as: Current byte index (the index is a tag variable used for addressing) × 8 + the number of zeros written in the current byte. Using a 4KB sector as an example, the maximum first-level count is 4096 × 8 = 32768 seconds, and the maximum second-level count is 32768 × 32768 = 1,073,741,824 seconds.
[0034] The above method avoids erasing and writing the entire sector every time it is powered on, reducing the number of flash erase / write cycles from "once per power-on" to "once every 32,768 power-on cycles". This prevents repeated power-on cycles in case of a fault from causing frequent flash (flash memory) erasures, and greatly extends the lifespan of the storage area.
[0035] In this embodiment, the log storage area adopts a circular storage method, which requires recording the pointer value of the current write position. It also uses a multi-level bit counting method to record the pointer value. When the count of the previous level sector is full, the pointer is carried over to the next level sector, and the previous level sector is erased simultaneously. Traditional solutions directly store this pointer in Flash memory, requiring the pointer position (the location of the pointer in Flash memory) to be erased and rewritten every time a log is written, severely reducing its lifespan.
[0036] In this embodiment, the pointer value recorded using a multi-level bit counting method can be a two-level bit counting method. Specifically, the PARA area (log entry offset address recording area) is divided into two sectors. The first-level sector records the low-order bits of the pointer (the low-order bits of the cumulative count: the first-level count), and the second-level sector records the high-order bits (the high-order bits of the cumulative count: the first-level count + the second-level count × (S × 8)). Each time a log entry is written, a log pointer update operation is performed, incrementing the first-level bit count by one, using the same method as the multi-level bit counting method described above. When the first-level bit count area is full, the count is carried over to the second-level bit count area, and the first-level bit count area (the corresponding sector) is erased. However, this method is not limited to two-level bit counting; other bit counting methods are also applicable to this disclosure.
[0037] When reading the current pointer, the pointer is obtained by adding the first-level count to the second-level count and then multiplying it by (S×8).
[0038] Reducing the number of erase / write operations for pointer records from "once per log entry written" to "once every 32,768 log entries written" significantly improves lifespan for frequently written log types (such as real-time logs).
[0039] In this embodiment of the disclosure, a directory entry is created in the log directory area each time the device is powered on for quick log retrieval. For example... Figure 6 In this context, the log directory area is also divided into two consecutive sectors. Each directory entry includes: power-on sequence number, the running time value at the start of this power-on, the historical log start pointer, the fault log start pointer, the synchronization time, and the CRC checksum. For example, such as... Figure 3 As shown, the following parameters are specified: Power-on sequence number (4 bytes): This is the power-on update, obtained from the power-on count record area. Power-on time (4 bytes): This is the relative time of this power-on update, obtained from the runtime record area. Log start address (4 bytes): This is the log entry offset address record area. Log entry count (4 bytes): This is the log entry count for the next power-on update, i.e., each time power is powered on, the log entry count in the previous power-on directory area is updated by subtracting the log start address from the previous power-on from the current data in the log entry offset address record area. Time synchronization time (7 bytes): This is updated during time synchronization. The current absolute time (year, month, day, hour, minute, second) is obtained through time synchronization. The absolute time of the power-on time is calculated and recorded by the difference between the current relative time and the relative time of the power-on time. The absolute time of any subsequent relative time can be derived from the absolute time of the power-on time. Checksum (1 byte): CRC8 (8-bit Cyclic Redundancy Check) checksum value, used to determine whether the data entry is valid.
[0040] Furthermore, directory entries are stored using a modulo operation based on the power-on sequence number, supporting circular overlay. Additionally, when time synchronization occurs, the synchronization time in the current power-on directory entry is updated, and the CRC is recalculated.
[0041] In this embodiment of the disclosure, a unified time conversion based on the seconds of the 1970 epoch is used; wherein, a date and time structure is defined, including year, month, day, hour, minute, and second. However, it is not limited to this, and time recording based on other epochs is also used in this disclosure.
[0042] Implement a date-to-seconds conversion function: based on 00:00 on January 1, 1970, accumulate the year, month, day, hour, minute, and second, and include a built-in leap year determination rule.
[0043] Implement a function to convert seconds to date: restore the date by successively decrementing the year and month. Logs are stored only with relative timestamps (relative time since system startup).
[0044] When the log is displayed, the absolute time is obtained by calling the seconds → date conversion based on the time synchronization in the current power-on directory (or the time synchronization in the history directory).
[0045] Specifically, such as Figure 4 As shown, upon each power-on, the current total power-on runtime is retrieved from the runtime recording area and recorded in the directory as the initial power-on relative time Z (relative to the total runtime of the first power-on, in seconds, not in a year-month-day format). During operation, the relative time is continuously updated, and the time recorded in the log is the relative time.
[0046] When an external system performs a time synchronization operation at a certain moment (correcting the year, month, and day of the power-on time; the year, month, and day of any subsequent moment can be calculated using relative time), it obtains the current absolute time Xr (the absolute time is transmitted through the external system's time synchronization, while the relative time is updated in real time by the log module) and the current relative time X. Using X and Xr, the absolute time Zr of the relative time Z can be derived using the 1970 epoch method, and Zr is then updated into the time synchronization period for this power-on.
[0047] By using the relative time Z and absolute time Zr at power-on, the absolute time Nr of any relative time N can be derived, which is the absolute time of each log record, for reporting or display. This method simplifies the design of the system timeline, separating relative and absolute time. Time synchronization can be initiated at any time, and whether or not synchronization is performed does not affect system recording and operation. System operation is based on relative time, maintaining independence and reducing the likelihood of timeline confusion. The unified time conversion based on the 1970s second count completely eliminates leap year errors and borrowing logic, ensuring unified forward and reverse calculations, concise logic, and strong portability.
[0048] For log recording methods, log storage requires sequential operations and involves erasing and writing to Flash, which is time-consuming. Real-time recording would introduce uncertainty, impacting system real-time performance. In this embodiment, when the upper-layer application calls the log write interface, it quickly stores the log data (including the current relative timestamp) into a circular SRAM (Static Random Access Memory) buffer and returns immediately. The background task periodically checks the buffer, retrieves the oldest entry in timestamp order, and performs a lock-free Flash write operation. When the buffer is full, it automatically overwrites the oldest timestamp entry and outputs a warning message. Figure 5 As shown, in addition to providing a real-time logging interface, the system also provides a cached, non-blocking logging interface. Application-layer logs are first written to an SRAM cache, with a short and fixed time interval, preventing system blocking. The background log task periodically writes the cached logs to Flash memory in a first-in, first-out (FIFO) manner, ensuring both system real-time performance and the order and accuracy of log recording. This two-level cached non-blocking write mechanism ensures that log recording does not block real-time tasks; when the cache is full, the oldest entry is overwritten to prevent data loss.
[0049] Furthermore, the method also includes log querying and directory indexing; specifically, when querying the log for a specific power-on sequence number, it first attempts to directly obtain the log start pointer and length for that power-on cycle through the directory entry. If the directory entry is valid, it directly locates and reads the log. If the directory entry is invalid (overwritten or CRC error), it degrades to a full disk scan. Figure 6 The diagram shows the log module range division. The fast log retrieval process includes the following steps: The user enters the power-on sequence number N to query.
[0050] Traverse the directory area, retrieve the log entry containing power-on sequence number N, obtain the log starting address and entry number, and read the log entry directly from the offset address in Flash. If there is a time synchronization function, the relative timestamp can be converted to an absolute time for display.
[0051] If the directory reading fails or there is no directory, the entire log data area is scanned, log entries are read one by one and CRC is checked. If the power-on sequence number matches, the entry is displayed. A matching power-on sequence number means that the log entry belongs to the log entry with power-on sequence number N, and can be displayed.
[0052] like Figure 7As shown in the embodiments of this disclosure, an embedded device log storage system capable of executing the above-described method is also disclosed. The system includes a power-on counting module, a runtime accumulation module, a directory management module, a time conversion module, a log storage module, and a cache writing module. The power-on counting module accumulates the number of power-on cycles each time the device is powered on, starting from the first level bit count area in the multi-level bit count area corresponding to the power-on cycle recording area. The runtime accumulation module performs a time update operation once per second, accumulating the total device runtime starting from the first level bit count area in the multi-level bit count area corresponding to the runtime recording area. The directory management module updates the log storage system in the directory each time the device is powered on. The recording area creates a directory entry; the directory entry includes the power-on sequence number, the running time value at the start of this power-on, the historical log start pointer, the fault log start pointer, the synchronization time, and the CRC checksum; the time conversion module is used for mutual conversion between relative time and absolute time; the log storage module is used to store log entries in flash memory based on a ring storage method, and new log entries will overwrite the oldest log entries. Each log entry includes the power-on sequence number and the time of occurrence; the cache writing module is used to periodically check the cache area, retrieve the oldest entries in timestamp order, and perform lock-free flash memory write operations; and when the cache area is full, it automatically overwrites the oldest timestamp entry and outputs a warning message.
[0053] This disclosure also discloses a computer-readable storage medium storing computer instructions for causing the computer to perform the methods described above.
[0054] Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.
Claims
1. A log storage method of an embedded device, characterized by, include The flash memory space in the log storage module is divided into multiple areas, including a power-on count recording area and a runtime recording area. The power-on count recording area and the running time recording area are respectively divided into multiple consecutive sectors of preset size, forming corresponding multi-level bit counting areas; Each time the device is powered on, the number of power-on times is accumulated, starting from the first level bit count area in the corresponding multi-level bit count area of the power-on count recording area. A time update operation is performed every second, starting from the first level bit count area in the corresponding multi-level bit count area of the running time recording area, to accumulate the total running time of the device.
2. The log storage method for an embedded device according to claim 1, characterized in that, Each time the device is powered on, starting from the first level of the multi-level bit counter area in the power-on count recording area, the cumulative number of device power-on counts includes, Read the current bit of the current byte in the current level bit counter area. If the bit is 1, write 0 to it, which means 1 time. If the current byte is already 0x00, move to the next byte; if the current level bit counter is full, carry over to the next level bit counter, erase the previous level bit counter and reset the pointer.
3. The log storage method for an embedded device according to claim 1, characterized in that, A time update operation is performed once per second, starting from the first level bit count area in the corresponding multi-level bit count area of the running time recording area. The total running time of the device includes... Read the current bit of the current byte in the current level bit counter area every second. If the bit is 1, write 0 to it, which represents 1 second. If the current byte is already 0x00, move to the next byte; if the current level bit counter is full, carry over to the next level bit counter, while erasing the previous level bit counter and resetting the pointer.
4. The log storage method for an embedded device according to claim 2 or 3, characterized in that, The cumulative number of device power-ups and the cumulative total device uptime both satisfy the following formula: P = First-level count + Second-level count × (S × 8) + ... + nth-level count × (S × 8)^(n-1); The i-th level count = current byte index × 8 + the number of zeros written in the current byte; In the formula, S is the total number of bytes in each sector, n represents the total number of multi-level bit counting areas in the power-on count recording area or running time recording area, and i represents the i-th level bit counting area, where i is an integer and i∈n.
5. The log storage method for an embedded device according to claim 4, characterized in that, The multiple areas also include a log entry offset address recording area, used to record the pointer value of the current write position, wherein, The log entry offset address recording area is divided into two sectors, namely the first-level bit counting area and the second-level bit counting area. The first-level bit counting area records the low-order bits of the pointer, and the second-level bit counting area records the high-order bits of the pointer. Each time a log entry is written, a log pointer update operation is performed, incrementing the first-level count by one; when the first-level bit count area is full, the count is carried over to the second-level bit count area, and the first-level bit count area is erased at the same time. When reading the current pointer: Pointer value = First level count + Second level count × (S × 8).
6. The log storage method for an embedded device according to claim 5, characterized in that, The multiple areas also include a log directory area. Each time the device is powered on, a directory entry is created in the directory area. The directory entry is stored modulo the power-on sequence number and supports circular overlay. When time synchronization occurs, the time synchronization time in the current power-on directory entry is updated and the CRC is recalculated. The directory entries include the power-on sequence number, the starting running time value of this power-on, the historical log start pointer, the fault log start pointer, the time synchronization time, and the CRC checksum; among them, The power-on sequence number is obtained from the power-on count record area in this power-on update; The power-on time is updated during this power-on update, and the relative time of this power-on is obtained from the runtime recording area; The log start address is obtained from the log entry offset address recording area during this power-on update; The number of log entries is updated on the next power-on, using the current data in the log entry offset address recording area and subtracting the log start address from the last power-on. Time synchronization: The time synchronization function updates continuously. It obtains the current absolute time of year, month, day, hour, minute, and second through time synchronization. It calculates and records the absolute time of power-on by using the difference between the current relative time and the relative time at the time of power-on, according to the rules for converting seconds to dates. The checksum uses a CRC8 checksum value to determine whether the data entry is valid.
7. The log storage method for an embedded device according to claim 6, characterized in that, It also includes periodically checking the cache, retrieving the oldest entry in timestamp order, and performing lock-free flash write operations; When the buffer is full, the oldest timestamp entry is automatically overwritten, and a warning message is output.
8. The log storage method for an embedded device according to claim 7, characterized in that, It also includes log queries and directory indexes, including, The user enters the power-on sequence number N to query; Traverse the log directory area, retrieve the log entry containing power-on sequence number N from the directory, obtain the log starting address and the number of entries, and read the log entry directly from the offset address in the flash memory. If a time synchronization mechanism exists, convert the relative timestamp to an absolute time for display; If the directory read fails or there is no directory, a full scan of the log entry storage area is performed, and log entries are read one by one and their CRC is verified.
9. A log storage system for an embedded device, characterized in that, include, The power-on counting module is used to accumulate the number of times the device is powered on each time it is powered on, starting from the first level of the multi-level bit counting area in the power-on count recording area; The runtime accumulation module is used to perform a time update operation once per second, starting from the first level bit count area in the corresponding multi-level bit count area of the runtime recording area, and accumulates the total runtime of the device. The directory management module is used to create a directory entry in the directory area each time the device is powered on. The directory entry includes the power-on sequence number, the running time value at the start of this power-on, the historical log start pointer, the fault log start pointer, the time synchronization time, and the CRC checksum. The time conversion module is used for converting between relative and absolute time. The log storage module is used to store log entries in flash memory using a ring storage method. The cache write module is used to periodically check the cache area, retrieve the oldest entry in timestamp order, and perform lock-free flash write operations; and when the cache area is full, it automatically overwrites the oldest entry with the oldest timestamp and outputs a warning message.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions for causing the computer to perform the method according to any one of claims 1 to 8.
