A Flash-based log access method, device, storage medium, and terminal
By obtaining timed logs in real time in Flash storage and calculating the target storage address, and using the backup area to achieve erecting redundancy, the problem of low log storage and query efficiency in the prior art is solved, the risk of log erasing and Flash corruption is reduced, and the storage and query efficiency is improved.
Patent Information
- Application Number
- CN202111425600.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-26
- Publication Date
- 2025-06-10
- Estimated Expiration
- 2041-11-26
AI Technical Summary
When performing log storage and query in Flash storage, there are problems such as slow reading and writing speed, limited number of writes, and inability to modify locally, resulting in large-scale log rinsing when the time modification deviation is large, which takes a long time and may cause damage to the Flash backup area. At the same time, the index storage method is inefficient when querying logs, which can easily lead to system crashes.
By obtaining timed logs in real time in Flash storage and calculating the target Flash storage address based on preset cycles and log time, the backup area is used to achieve erecting redundancy, reducing log erecting and Flash corruption caused by time modification.
Reduces log rewritten and Flash corruption caused by time modification, improves log query and storage efficiency, reduces the risk of system crashes, and saves time to find the location of the target log record content.
Smart Images

Figure CN114328280B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and particularly to a method, device, storage medium and terminal for Flash-based log access and storage. Background Art
[0002] With the rapid development of the single-chip microcomputer industry, more and more devices are becoming smarter, and the requirements for single-chip microcomputers are also getting higher and higher. Most devices have the function of storing operation logs of the device to save operation status, environmental information, fault information, configuration information, user operation information, etc. Log records are generally relatively long and occupy a large amount of storage space.
[0003] In the prior art, when storing logs, the logs are sequentially stored in a continuous Flash space through a log index. After the log storage is full, all the logs are erased and then written again. When it is necessary to read the logs, the log address is found through the index for sequential reading, or the log information at a specified time among all the logs is searched through algorithms such as the binary search method.
[0004] However, due to the problems of slow read and write speed, limited write times, and inability to modify locally in Flash, when storing timed logs according to the index, if the current time is modified to a historical time, the logs after the modified historical time need to be deleted, and all the logs in the sector where the historical time is located need to be read first, then locally written to the backup area, and then written to the specified sector. If the time modification deviation is large, multiple sectors may be continuously deleted, which takes a long time. If frequent time calibration is caused by device reasons, it will cause damage to the backup area due to multiple writes in Flash.
[0005] At the same time, the method of storing according to the index will cause great trouble in querying logs. Because the corresponding time cannot be known, it can only be queried according to the index. When it is necessary to query the logs at a specified time, it is necessary to view the time of each log one by one and then return the logs that meet the conditions. However, the number of logs is often very large. Therefore, such a method is not only time-consuming and inefficient, but also likely to cause the system to crash. Summary of the Invention
[0006] The embodiments of the present application provide a method, device, storage medium and terminal for Flash-based log access and storage. To provide a basic understanding of some aspects of the disclosed embodiments, a simple summary is given below. This summary part is not a general review, nor is it intended to identify key / important constituent elements or delineate the protection scope of these embodiments. Its sole purpose is to present some concepts in a simple form as a prelude to the subsequent detailed description.
[0007] In a first aspect, the embodiments of the present application provide a method for Flash-based log access and storage, the method including:
[0008] Obtain the timed logs generated by the system in real time according to a preset period; wherein, the timed logs include log time, log index, and target log record content;
[0009] Determine the time conversion unit, and generate the total duration after converting the log time based on the time conversion unit;
[0010] Calculate the target Flash storage address where the target log record content is located according to the preset period and the total duration;
[0011] When there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, determine the next sector corresponding to the target sector as the backup area, and write the target log record content to the target Flash storage address based on the backup area, and erase the data in the backup area.
[0012] Optionally, the time conversion unit is seconds;
[0013] Generating the total duration after converting the log time based on the time conversion unit includes:
[0014] Determine the starting moment of the current year of the log time;
[0015] Calculate the first time second value between the log time and the starting moment of the current year;
[0016] Calculate the second time second value passed from the previous historical year of the starting moment of the current year to the initial year moment; or,
[0017] Load the year time table, and obtain the second time second value passed from the previous historical year of the starting moment of the current year to the initial year moment from the year time table;
[0018] Add the first time second value and the second time second value to generate the total duration.
[0019] Optionally, calculating the target Flash storage address where the target log record content is located according to the preset period and the total duration includes:
[0020] Convert the preset period to the third time second value;
[0021] Determine the maximum number of logs that can be obtained within the preset period;
[0022] Calculate the storage log index according to the total duration, the third time second value, and the maximum number of logs;
[0023] Calculate the total amount of logs that can be stored in each sector of the Flash;
[0024] Calculate the sector displacement number and index position where the storage log index is located according to the storage log index and the total amount of storable logs;
[0025] Calculate the target Flash storage address where the target log record content is located according to the sector displacement number and index position where the storage log index is located.
[0026] Optionally, calculate the storage log index according to the total duration, the third time second value, and the maximum number of logs, including:
[0027] Calculate the target ratio of the total duration to the third time second value;
[0028] Calculate the remainder of the target ratio divided by the maximum number of logs;
[0029] Determine the remainder as the storage log index;
[0030] Among them, the calculation formula for the storage log index is: Ir = (S / C) % M; S is the total duration after converting the log time in the timed log, C is the time second value after converting the preset period, and M is the maximum number of logs that can be stored in the sector.
[0031] Optionally, the method further includes:
[0032] When there is no data in the target sector where the target Flash storage address is located, write the target log record content to the target Flash storage address;
[0033] Or,
[0034] When there is data in the target sector where the target Flash storage address is located and the target Flash storage address belongs to the starting address of the target sector, erase the data in the target sector and write the target log record content to the target Flash storage address.
[0035] Optionally, the method further includes:
[0036] When receiving a log query instruction, obtain the query start time and end time;
[0037] Determine the log query time range;
[0038] When the start time and end time are within the log query time range, calculate the query start position and end position according to the query start time and end time;
[0039] Read the logs one by one according to the query start position and end position and return them.
[0040] Optionally, the method further includes:
[0041] Obtain the starting index value of the log to be queried;
[0042] When the start time and the end time are within the log query time range, the logs are read one by one according to the starting index value of the logs to be queried and returned.
[0043] In a second aspect, an embodiment of the present application provides a Flash-based log access device, which includes:
[0044] A timed log acquisition module, configured to acquire the timed logs generated by the system in real time according to a preset period; wherein, the timed logs include log time, log index, and target log record content;
[0045] A total duration generation module, configured to determine a time conversion unit, and generate a total duration after converting the log time based on the time conversion unit;
[0046] A Flash storage address calculation module, configured to calculate the target Flash storage address where the target log record content is located according to the preset period and the total duration;
[0047] A target log record content writing module, configured to, when there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, determine the next sector corresponding to the target sector as a backup area, write the target log record content to the target Flash storage address based on the backup area, and erase the data in the backup area.
[0048] In a third aspect, an embodiment of the present application provides a computer storage medium, which stores multiple instructions, and the instructions are suitable for being loaded and executed by a processor to perform the above method steps.
[0049] In a fourth aspect, an embodiment of the present application provides a terminal, which may include: a processor and a memory; wherein, the memory stores a computer program, and the computer program is suitable for being loaded and executed by the processor to perform the above method steps.
[0050] The technical solution provided by the embodiment of the present application may include the following beneficial effects:
[0051] In the embodiment of the present application, the Flash-based log access device first obtains the timed logs generated by the system in real time according to a preset period; wherein, the timed logs include log time, log index, and target log record content. Then, it determines the time conversion unit, converts the log time based on the time conversion unit to generate the total duration, and calculates the target Flash storage address where the target log record content is located according to the preset period and the total duration. Finally, when there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, it determines the next sector corresponding to the target sector as the backup area, writes the target log record content to the target Flash storage address based on the backup area, and erases the data in the backup area. The present application calculates the target Flash storage address where the target log record content is located based on the log time. Due to the continuity of time, it can reduce the accidents of large-area log rewriting caused by time modification, and at the same time reduce the number of times of erasing the Flash due to time modification. The present application uses a backup area to achieve write-erase redundancy, reduce the probability of the backup area being written badly due to backing up in the same area, and reduce the waste of resources caused by separately opening multiple backup areas.
[0052] It should be understood that the above general description and the following detailed description are only exemplary and explanatory, and cannot limit the present invention. Brief Description of the Drawings
[0053] The accompanying drawings herein are incorporated into the specification and constitute a part of this specification, showing embodiments consistent with the present invention, and are used together with the specification to explain the principles of the present invention.
[0054] Figure 1 is a schematic flowchart of a Flash-based log access method provided by an embodiment of the present application;
[0055] Figure 2 is a schematic block diagram of the timestamp conversion process of a timed log provided by an embodiment of the present application;
[0056] Figure 3 is a schematic block diagram of the address calculation process for saving the log storage content provided by an embodiment of the present application;
[0057] Figure 4 is a schematic block diagram of the process of storing a timed log provided by an embodiment of the present application;
[0058] Figure 5 is a schematic flowchart of a Flash-based log access process provided by an embodiment of the present application;
[0059] Figure 6 is a schematic flowchart of another Flash-based log access method provided by an embodiment of the present application;
[0060] Figure 7 It is a schematic flowchart of a process for reading timing logs from Flash provided by an embodiment of the present application;
[0061] Figure 8 It is a schematic structural diagram of a log access device based on Flash provided by an embodiment of the present application;
[0062] Figure 9 It is a schematic structural diagram of a terminal provided by an embodiment of the present application. Detailed implementation manners
[0063] The following description and the accompanying drawings fully illustrate specific embodiments of the present invention, enabling those skilled in the art to practice them.
[0064] It should be clear that the described embodiments are only some embodiments of the present invention, not all of them. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts belong to the scope of protection of the present invention.
[0065] When the following description refers to the accompanying drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present invention. On the contrary, they are only examples of devices and methods consistent with some aspects of the present invention as detailed in the appended claims.
[0066] In the description of the present invention, it should be understood that terms such as "first", "second", etc. are only used for descriptive purposes and cannot be construed as indicating or implying relative importance. For those of ordinary skill in the art, the specific meanings of the above terms in the present invention can be understood according to specific circumstances. In addition, in the description of the present invention, unless otherwise specified, "a plurality" means two or more. "And / or" describes the association relationship of associated objects and indicates that three relationships can exist. For example, A and / or B can represent: A exists alone, A and B exist simultaneously, and B exists alone. The character " / " generally represents an "or" relationship between the associated objects before and after.
[0067] The present application provides a Flash-based log access method, device, storage medium, and terminal to solve the problems existing in the above-related technical problems. In the technical solution provided by the present application, the present application calculates the target Flash storage address where the target log record content is located based on the log time. Due to the continuity of time, accidents of large-area log erasure caused by time modification can be reduced, and at the same time, the number of times of erasing the Flash due to time modification can be reduced, and the time required to find the location where the target log record content is located can be saved. The present application adopts a backup area to achieve write-erase redundancy, reduce the probability of the backup area being written badly due to backing up in the same area, and reduce the waste of resources caused by separately opening multiple backup areas. The following will be described in detail with exemplary embodiments.
[0068] Next, the Flash-based log access method provided by the embodiments of the present application will be introduced in detail with reference to the Figure 1 - attached Figure 7 drawings. This method can be implemented depending on a computer program and can run on a Flash-based log access device based on the von Neumann architecture. This computer program can be integrated into an application or run as an independent tool class application.
[0069] Please refer to Figure 1 , which is a schematic flowchart of a Flash-based log access method provided by an embodiment of the present application. As Figure 1 shown, the method of the embodiment of the present application may include the following steps:
[0070] S101, obtain the timed logs generated by the system in real time according to a preset period;
[0071] Among them, the timed logs include log time, log index, and target log record content; the target log record content may be running status, environmental information, fault information, configuration information, user operation information, etc.
[0072] Generally, the preset period is a preset log acquisition interval, which can be once every 5 minutes or once every 1 minute. The specific time of this period can be set according to the actual scenario. There is no limitation here. In embedded development, it is often necessary to store data in a single-chip microcomputer and hope that these data will not be lost when the single-chip microcomputer loses power. Currently, external flash Flash or internal Flash of the single-chip microcomputer is usually used for data storage. If cost is not considered, this solution can be used on EEPROM, with faster reading and writing, longer lifespan, and no need to use a backup area, but it is expensive and has a small memory.
[0073] In a possible implementation, when obtaining the timing log generated by the system in real time according to a preset period, first, when receiving the starter startup instruction, obtain the preset time period, then set the countdown duration for the timer according to the preset time period, and then start the countdown of the timer. When the countdown ends, obtain the timing log generated by the system, and continuously obtain it in a loop.
[0074] S102. Determine the time conversion unit, and generate the total duration after converting the log time based on the time conversion unit;
[0075] Among them, the time conversion unit can be hours, minutes, and seconds, or other time as the storage address conversion unit, such as days, months, days, years. In this way, the limitation is greater.
[0076] In a possible implementation, when the determined time conversion unit is seconds and the conversion is performed in seconds, first determine the starting moment of the current year of the log time, then calculate the first time second value between the log time and the starting moment of the current year, then calculate the second time second value passed from the previous historical year of the starting moment of the current year to the initial year moment, and finally sum the first time second value and the second time second value to generate the total duration.
[0077] Further, or load the year time table, and obtain the second time second value passed from the previous historical year of the starting moment of the current year to the initial year moment from the year time table. For example Figure 2 As shown, each timing log has a unique timestamp. The method of converting the timestamp to a second counter is to calculate the number of seconds elapsed from the default starting moment of the computer, 00:00:00 on January 1, 01, to the current timestamp, calculated according to 366 days in a leap year, 365 days in a common year, 24 hours a day, 60 minutes an hour, and 60 seconds a minute, to calculate the total number of seconds S to the current time. This process needs to first calculate the number of seconds elapsed from the current time to the start of the current year (00:00:01 on January 1), and then add the total number of seconds elapsed from (the current year - 1) year to the default starting moment of the computer. If the memory space is sufficient, the number of seconds elapsed before the beginning of each typical year can be written into a table and directly extracted during calculation to speed up the calculation. For example, it is 1609430400 seconds at 00:00:00 on January 1, 2021, and 1577808000 seconds at 00:00:00 on January 1, 2020.
[0078] It should be noted that using seconds as the storage address conversion unit has strong scalability and can be used for storing timing logs of various time lengths.
[0079] S103. Calculate the target Flash storage address where the target log record content is located according to the preset period and the total duration;
[0080] In the embodiment of the present application, when calculating the target Flash storage address where the target log record content is located, first convert the preset period into the third time second value, then determine the maximum number of logs that can be obtained within the preset period, then calculate the storage log index according to the total duration, the third time second value, and the maximum number of logs, and then calculate the total amount of logs that can be stored in each sector of the Flash. Secondly, calculate the sector displacement number and index position where the storage log index is located according to the storage log index and the total amount of logs that can be stored. Finally, calculate the target Flash storage address where the target log record content is located according to the sector displacement number and index position where the storage log index is located.
[0081] Specifically, when calculating the storage log index according to the total duration, the third time second value, and the maximum number of logs, first calculate the target ratio of the total duration to the third time second value, then calculate the remainder of the target ratio and the maximum number of logs, and finally determine the remainder as the storage log index. The calculation formula of the storage log index is: S is the total duration after converting the log time in the timed log, C is the time second value after converting the preset period, M is the maximum number of logs that can be stored in the sector, and % is the remainder symbol.
[0082] For example Figure 3 as shown Figure 3 is a flow chart of converting the target Flash storage address provided by the present application. After obtaining the total number of seconds of the log time of the timed log, it is necessary to convert the total number of seconds into the address stored in the Flash. First, convert the preset period for obtaining the timed log into seconds C, and then calculate the storage log index Ir corresponding to the total number of seconds as Ir = (S / C) % M (the maximum number of loop-recorded timed logs M). Since the minimum erasure unit of the Flash is a sector, and one sector is 4096 bytes, for storage convenience, the logs are not stored across sectors to reduce the number of sector erasures for log modification. The number of logs stored in each sector of the Flash is Fc = 4096 / L (the length of a single log). The sector displacement number where the current storage log index Ir is located is: Fs = (Ir - 1) / Fc + 1, and the current sector index position Fi = (Ir - 1) % Fc. Therefore, the current Flash storage address D where the log is located can be calculated by combining the above parameters as D = Fc * 4096 + Fi * L + Ds (the start address of the timed log address storage).
[0083] It should be noted that the length of a single log is less than the sector length of 4096 bytes. The start address of the log is at the start position of the sector, otherwise the start position offset needs to be added for calculation.
[0084] Further, after obtaining the Flash storage address where the current log is located, the target log record content can be written to the corresponding location in the Flash according to the Flash storage address where the current log is located.
[0085] S104. When there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, determine the next sector corresponding to the target sector as the backup area, write the target log record content to the target Flash storage address based on the backup area, and erase the data in the backup area.
[0086] In a possible implementation, when there is no data in the target sector where the target Flash storage address is located, directly write the target log record content to the target Flash storage address.
[0087] In another possible implementation, when there is data in the target sector where the target Flash storage address is located and the target Flash storage address belongs to the starting address of the target sector, erase the data in the target sector, and write the target log record content to the target Flash storage address.
[0088] In another possible implementation, when there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, determine the next sector corresponding to the target sector as the backup area, write the target log record content to the target Flash storage address based on the backup area, and erase the data in the backup area.
[0089] Specifically, when writing the target log record content to the target Flash storage address based on the backup area, first delete the content of the sectors after the target sector, then determine the next sector of the target sector as the backup area, then write the data between the starting position of the target sector and the target log content to the backup area, then delete the content of the target sector, and then copy the data written to the backup area back to the target sector. After completion, delete the data in the backup area.
[0090] For example Figure 4 as shown Figure 4It is a schematic block diagram of a timed log storage process provided by this application. When storing data according to the target Flash storage address, first determine whether there is data at the target Flash storage address. If there is no data, it means it can be written, and directly write the data to the target location. If there is data, then it is necessary to determine whether the target Flash storage address is the start position of a Flash sector. If it is at the start position of the sector, it means that the subsequent data in this sector belongs to old data or future data, and it can be directly erased. After erasing, write the data to the start position of this sector. If the target Flash storage address does not belong to the start position of the sector, it is necessary to back up the data before this position because the data belongs to the most recent data and cannot be cleared. Therefore, use the next sector as the backup area because the data in the next sector belongs to the earliest remaining data and can be deleted for backing up the data in this sector.
[0091] Further, when writing the target log record content to the target Flash storage address based on the backup area, first erase the content of the sector after the target sector, then read the data before the target Flash storage address position in the target sector and write it to the next sector, then erase the target sector, then read the data in the next sector and write it to the target sector, then write the log data to the specified position, and erase the data in the next sector to prevent abnormal reading next time, completing data backup and modification.
[0092] For example Figure 5 as shown Figure 5 It is a schematic block diagram of a log access process based on Flash provided by this application. First, the computer generates a timed log, which contains a log index, a log time, and the target log record content. Then convert the log timestamp into a numerical value of seconds, then convert the second value into a physical address according to the current timed storage interval, and then store the log in Flash according to the address.
[0093] In the embodiment of this application, by storing based on the storage address calculated according to time, each time log can have a fixed storage position, the log record of the target time can be quickly found, and at the same time, the logs in each sector can be strictly stored in chronological order, reducing the problems of deleting logs or being unable to query due to time disorder, which is suitable for timed log storage.
[0094] In the embodiment of the present application, the Flash-based log access device first obtains the timed logs generated by the system in real time according to a preset period; wherein, the timed logs include log time, log index, and target log record content, then determines the time conversion unit, and generates the total duration after converting the log time based on the time conversion unit, and then calculates the target Flash storage address where the target log record content is located according to the preset period and the total duration. Finally, when there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, the next sector corresponding to the target sector is determined as the backup area, and the target log record content is written to the target Flash storage address based on the backup area, and the data in the backup area is erased. The present application calculates the target Flash storage address where the target log record content is located based on the log time. Due to the continuity of time, accidents of large-area log erasure due to time modification can be reduced, and at the same time, the number of times of erasing the Flash due to time modification can be reduced, and the search speed of the target Flash storage address can be improved. The present application uses a backup area to achieve write-erase redundancy, reduces the probability of the backup area being written badly due to backing up in the same area, and reduces the waste of resources caused by separately opening multiple backup areas.
[0095] Please refer to Figure 6 , which is a schematic flowchart of another Flash-based log access method provided by the embodiment of the present application. As Figure 6 shown, the method of the embodiment of the present application may include the following steps:
[0096] S201, when receiving a log query instruction, obtain the query start time and end time;
[0097] S202, determine the log query time range;
[0098] S203, when the start time and end time are within the log query time range, calculate the query start position and end position according to the query start time and end time;
[0099] S204, read the logs one by one according to the query start position and end position and return them.
[0100] Among them, the traditional method of reading according to the index is supported, and it can also be read according to time, which is more practical and can improve the log query and reading speed.
[0101] In a possible implementation manner, for example Figure 7As shown, when reading based on time, first determine whether the query time is within the current log storage range. If the query start time is greater than the latest log time or the query end time is less than the oldest log time in the log record, there is no log to query, and directly return no log. If the query start time is less than the log start time, the log start position is the query start address. After obtaining the address for starting the log query, read the log data in the address. If the read fails or there is no log data, read the data of the next sector to prevent the data in the read sector from being erased. If the next sector also fails to read or there is no data, return query failure. If there is data, continue; determine the read log time. If it is less than the query start time, discard it and continue to query the next one. If it is greater than the query end time, the query ends. Otherwise, return the query result and continue to query the next one until the last log is queried or the condition is not met.
[0102] In another possible implementation, when reading by index value, the starting index value of the log to be queried is first obtained, and then when the start time and the end time are within the log query time range, the logs are read one by one according to the starting index value of the log to be queried and returned.
[0103] Specifically, when querying based on an index, the starting position to be queried is directly calculated based on the index value, and the calculation method is D=(In(query starting index value)-1) / Fc(number of logs per sector)*4096+DS(log starting position)+(In-1)%Fc*L.
[0104] In an embodiment of the present application, a Flash-based log access device first obtains the timing log generated by the system in real time according to a preset period; wherein the timing log includes log time, log index and target log record content, then determines the time conversion unit, and converts the log time based on the time conversion unit to generate a total duration, and then calculates the target Flash storage address where the target log record content is located according to the preset period and the total duration, and finally, when there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, the next sector corresponding to the target sector is determined as a backup area, and the target log record content is written to the target Flash storage address based on the backup area, and the data in the backup area is erased. The present application calculates the target Flash storage address where the target log record content is located based on the log time. Due to the continuity of time, it can reduce the accident of large-scale log erasure due to time modification, and at the same time reduce the number of times Flash is erased due to time modification. The present application adopts a backup area to achieve erasure redundancy, reduce the probability of bad backup area writing due to backup in the same area, and reduce the waste of resources caused by opening up multiple backup areas separately.
[0105] The following are embodiments of the device of the present invention, which can be used to execute the method embodiments of the present invention. For details not disclosed in the device embodiments of the present invention, please refer to the method embodiments of the present invention.
[0106] Please refer to Figure 8 , which shows a schematic structural diagram of a Flash-based log access device provided by an exemplary embodiment of the present invention. The Flash-based log access device can be implemented as all or part of a terminal through software, hardware, or a combination of both. The device 1 includes a timed log acquisition module 10, a total duration generation module 20, a Flash storage address calculation module 30, and a target log record content writing module 40.
[0107] The timed log acquisition module 10 is used to acquire the timed logs generated by the system in real time according to a preset period; wherein, the timed logs include log time, log index, and target log record content;
[0108] The total duration generation module 20 is used to determine a time conversion unit and generate a total duration after converting the log time based on the time conversion unit;
[0109] The Flash storage address calculation module 30 is used to calculate the target Flash storage address where the target log record content is located according to the preset period and the total duration;
[0110] The target log record content writing module 40 is used to, when there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, determine the next sector corresponding to the target sector as the backup area, write the target log record content to the target Flash storage address based on the backup area, and erase the data in the backup area.
[0111] It should be noted that when the above-mentioned Flash-based log access device provided in the embodiment executes the Flash-based log access method, only the above-mentioned division of each functional module is used for illustration. In practical applications, the above functions can be allocated to different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to complete all or part of the above-described functions. In addition, the above-mentioned Flash-based log access device provided in the embodiment and the method embodiment of the Flash-based log access belong to the same concept, and the implementation process thereof is detailed in the method embodiment, which will not be elaborated here.
[0112] The serial numbers of the above embodiments of the present application are only for description and do not represent the advantages and disadvantages of the embodiments.
[0113] In an embodiment of the present application, the Flash-based log access device first obtains the timing logs generated by the system in real time according to a preset period; wherein, the timing logs include log time, log index, and target log record content, then determines the time conversion unit, and generates the total duration after converting the log time based on the time conversion unit, and then calculates the target Flash storage address where the target log record content is located according to the preset period and the total duration. Finally, when there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, the next sector corresponding to the target sector is determined as the backup area, and the target log record content is written to the target Flash storage address based on the backup area, and the data in the backup area is erased. In the present application, the target Flash storage address where the target log record content is located is calculated based on the log time. Due to the continuity of time, accidents of large-area log rewriting due to time modification can be reduced, and at the same time, the number of times of erasing the Flash due to time modification can be reduced. The present application adopts a backup area to achieve write-erase redundancy, reduce the probability of the backup area being written badly due to backing up in the same area, and reduce the waste of resources caused by separately opening multiple backup areas.
[0114] The present invention also provides a computer-readable medium, on which program instructions are stored, and when the program instructions are executed by a processor, the Flash-based log access method provided by each of the above method embodiments is implemented.
[0115] The present invention also provides a computer program product containing instructions, which when running on a computer, causes the computer to execute the Flash-based log access method of each of the above method embodiments.
[0116] Please refer to Figure 9 , which is a schematic structural diagram of a terminal provided by an embodiment of the present application. As Figure 9 shown, the terminal 1000 may include: at least one processor 1001, at least one network interface 1004, a user interface 1003, a memory 1005, and at least one communication bus 1002.
[0117] Among them, the communication bus 1002 is used to realize the connection and communication between these components.
[0118] Among them, the user interface 1003 may include a display screen (Display), a camera (Camera), and optionally the user interface 1003 may further include a standard wired interface and a wireless interface.
[0119] Among them, the network interface 1004 may optionally include a standard wired interface and a wireless interface (such as a WI-FI interface).
[0120] Among them, the processor 1001 may include one or more processing cores. The processor 1001 connects various parts within the entire electronic device 1000 through various interfaces and circuits. By running or executing instructions, programs, code sets, or instruction sets stored in the memory 1005, and by calling the data stored in the memory 1005, it performs various functions of the electronic device 1000 and processes data. Optionally, the processor 1001 may be implemented in at least one hardware form of digital signal processing (DSP), field-programmable gate array (FPGA), or programmable logic array (PLA). The processor 1001 may integrate a combination of one or several of a central processing unit (CPU), a graphics processing unit (GPU), and a modem, etc. Among them, the CPU mainly processes the operating system, user interface, application programs, etc.; the GPU is responsible for rendering and drawing the content to be displayed on the display screen; the modem is used to process wireless communication. It can be understood that the above-mentioned modem may not be integrated into the processor 1001 and may be implemented separately by a single chip.
[0121] Among them, the memory 1005 may include random access memory (RAM) and may also include read-only memory. Optionally, the memory 1005 includes a non-transitory computer-readable storage medium. The memory 1005 can be used to store instructions, programs, code, code sets, or instruction sets. The memory 1005 may include a program storage area and a data storage area. Among them, the program storage area may store instructions for implementing the operating system, instructions for at least one function (such as touch function, sound playback function, image playback function, etc.), instructions for implementing the above-mentioned various method embodiments, etc.; the data storage area may store the data involved in the above-mentioned various method embodiments. Optionally, the memory 1005 may also be at least one storage device located far from the aforementioned processor 1001. As Figure 9 shown, the memory 1005, as a computer storage medium, may include an operating system, a network communication module, a user interface module, and a Flash-based log access application program.
[0122] In Figure 9In the terminal 1000 shown, the user interface 1003 is mainly used to provide an interface for the user to input and obtain the data input by the user; while the processor 1001 can be used to call the Flash-based log access application program stored in the memory 1005 and specifically perform the following operations:
[0123] Obtain the timing logs generated by the system in real time according to a preset period; wherein, the timing logs include log time, log index, and target log record content;
[0124] Determine the time conversion unit, and generate the total duration after converting the log time based on the time conversion unit;
[0125] Calculate the target Flash storage address where the target log record content is located according to the preset period and the total duration;
[0126] When there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, determine the next sector corresponding to the target sector as the backup area, write the target log record content to the target Flash storage address based on the backup area, and erase the data in the backup area.
[0127] In one embodiment, when the processor 1001 executes to generate the total duration after converting the log time based on the time conversion unit, it specifically performs the following operations:
[0128] Determine the starting moment of the current year of the log time;
[0129] Calculate the first time second value between the log time and the starting moment of the current year;
[0130] Load the year time table;
[0131] Obtain the second time second value passed from the previous historical year of the current year starting moment to the initial year moment;
[0132] Generate the total duration after adding the first time second value and the second time second value.
[0133] In one embodiment, when the processor 1001 executes to calculate the target Flash storage address where the target log record content is located according to the preset period and the total duration, it specifically performs the following operations:
[0134] Convert the preset period into the third time second value;
[0135] Determine the maximum number of logs that can be obtained within the preset period;
[0136] Calculate the storage log index according to the total duration, the third time second value, and the maximum number of logs;
[0137] Calculate the total amount of loggable data for each sector in the Flash.
[0138] Calculate the sector displacement number and index position of the storage log index based on the storage log index and the total amount of loggable data.
[0139] Calculate the target Flash storage address where the target log record content is located based on the sector displacement number and index position of the storage log index.
[0140] In one embodiment, when the processor 1001 executes to calculate the storage log index according to the total duration, the third time second value, and the maximum number of logs, it specifically performs the following operations:
[0141] Calculate the target ratio of the total duration to the third time second value.
[0142] Calculate the remainder of the target ratio divided by the maximum number of logs.
[0143] Determine the remainder as the storage log index.
[0144] Wherein, the calculation formula of the storage log index is: Ir = (S / C) % M; S is the total duration after converting the log time in the timed log, C is the time second value after converting the preset period, and M is the maximum number of logs that can be stored in the sector.
[0145] In one embodiment, when the processor 1001 executes to, when there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, determine the next sector corresponding to the target sector as the backup area, write the target log record content to the target Flash storage address based on the backup area, and erase the data in the backup area, it specifically performs the following operations:
[0146] When there is no data in the target sector where the target Flash storage address is located, write the target log record content to the target Flash storage address.
[0147] Or,
[0148] When there is data in the target sector where the target Flash storage address is located and the target Flash storage address belongs to the starting address of the target sector, erase the data in the target sector and write the target log record content to the target Flash storage address.
[0149] In one embodiment, the processor 1001 also performs the following operations:
[0150] When receiving a log query instruction, obtain the query start time and end time.
[0151] Determine the log query time range.
[0152] When the start time and the end time are within the log query time range, calculate the query start position and the end position according to the query start time and the end time;
[0153] Read the logs one by one according to the query start position and the end position and return them.
[0154] In one embodiment, the processor 1001 also performs the following operations:
[0155] Obtain the starting index value of the log to be queried;
[0156] When the start time and the end time are within the log query time range, read the logs one by one according to the starting index value of the log to be queried and return them.
[0157] In the embodiment of the present application, the Flash-based log access device first obtains the timed logs generated by the system in real time according to a preset period; wherein, the timed logs include log time, log index, and target log record content, then determines the time conversion unit, and generates the total duration after converting the log time based on the time conversion unit, and then calculates the target Flash storage address where the target log record content is located according to the preset period and the total duration. Finally, when there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, determine the next sector corresponding to the target sector as the backup area, write the target log record content to the target Flash storage address based on the backup area, and erase the data in the backup area. The present application calculates the target Flash storage address where the target log record content is located based on the log time. Due to the continuity of time, it can reduce the accidents of large-area log erasure due to time modification, and at the same time reduce the number of times of erasing the Flash due to time modification. The present application uses the backup area to achieve write-erase redundancy, reduce the probability of the backup area being written badly due to backing up in the same area, and reduce the waste of resources caused by separately opening multiple backup areas.
[0158] Those of ordinary skill in the art can understand that all or part of the processes of implementing the above method embodiments can be completed by instructing relevant hardware through a computer program. The program for Flash-based log access can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the above method embodiments. Among them, the storage medium can be a magnetic disk, an optical disc, a read-only memory, or a random access memory, etc.
[0159] The above-disclosed are only the preferred embodiments of the present application. Of course, the scope of rights of the present application cannot be limited thereby. Therefore, equivalent changes made according to the claims of the present application still fall within the scope covered by the present application.
Claims
1. A Flash-based log access method, characterized in that, the method includes: Obtaining the timed logs generated by the system in real time according to a preset period; wherein, the timed logs include log time, log index, and target log record content; Determining a time conversion unit, and generating a total duration after converting the log time based on the time conversion unit; wherein, the generating a total duration after converting the log time based on the time conversion unit includes: Determining the starting moment of the current year of the log time; Calculating the first time second value between the log time and the starting moment of the current year; Loading the year time table, and obtaining the second time second value passed from the previous historical year of the starting moment of the current year to the initial year moment from the year time table; Adding the first time second value and the second time second value to generate a total duration; Calculating the target Flash storage address where the target log record content is located according to the preset period and the total duration; wherein, the calculating the target Flash storage address where the target log record content is located according to the preset period and the total duration includes: Converting the preset period into a third time second value; Determining the maximum number of logs that can be obtained within the preset period; Calculating a storage log index according to the total duration, the third time second value, and the maximum number of logs; Calculating the total amount of logs that can be stored in each sector of the Flash; Calculating the sector displacement number and index position where the storage log index is located according to the storage log index and the total amount of logs that can be stored; Calculating the target Flash storage address where the target log record content is located according to the sector displacement number and index position where the storage log index is located; wherein, the calculating a storage log index according to the total duration, the third time second value, and the maximum number of logs includes: Calculating a target ratio of the total duration to the third time second value; Calculating the remainder of the target ratio and the maximum number of logs; Determining the remainder as the storage log index; When there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the starting address of the target sector, determining the next sector corresponding to the target sector as a backup area, writing the target log record content to the target Flash storage address based on the backup area, and erasing the data in the backup area; wherein, writing the target log record content to the target Flash storage address based on the backup area includes: Erasing the content of the sector after the target sector, reading the data before the target Flash storage address position in the target sector and writing it to the next sector, erasing the target sector, reading the data in the next sector and writing it to the target sector, and then writing the log data to the specified position and erasing the data in the next sector.
2. The method according to claim 1, characterized in that, the time conversion unit is seconds; the method further includes: Calculate the second time in seconds elapsed from the start time of the previous historical year of the start time of the current year to the start time of the initial year.
3. The method according to claim 1, wherein, The calculation formula for storing the log index is as follows: S is the total duration after converting the log time in the timed log, C is the number of seconds after converting the preset period, and M is the maximum number of logs that can be stored in the sector.
4. The method according to claim 1, wherein, The method further includes: When there is no data in the target sector where the target Flash storage address is located, write the target log record content to the target Flash storage address; Or, When there is data in the target sector where the target Flash storage address is located and the target Flash storage address belongs to the start address of the target sector, erase the data in the target sector and write the target log record content to the target Flash storage address.
5. The method according to claim 1, wherein, The method further includes: When a log query instruction is received, obtain the query start time and end time; Determine the log query time range; When the start time and end time are within the log query time range, calculate the query start position and end position according to the query start time and end time; Read the logs one by one according to the query start position and end position and return them.
6. The method according to claim 5, wherein, The method further includes: Obtain the starting index value of the log to be queried; When the start time and end time are within the log query time range, read the logs one by one according to the starting index value of the log to be queried and return them.
7. A Flash-based log access device implemented using the method according to any one of claims 1-6, wherein, The device includes: A timed log acquisition module for acquiring the timed logs generated by the system in real time according to a preset period; wherein, the timed logs include log time, log index, and target log record content; A total duration generation module for determining the time conversion unit and generating the total duration after converting the log time based on the time conversion unit; A Flash storage address calculation module for calculating the target Flash storage address where the target log record content is located according to the preset period and the total duration; A target log record content writing module for, when there is data in the target sector where the target Flash storage address is located and the target Flash storage address does not belong to the start address of the target sector, determining the next sector corresponding to the target sector as the backup area, writing the target log record content to the target Flash storage address based on the backup area, and erasing the data in the backup area.
8. A computer storage medium, wherein, The computer storage medium stores multiple instructions, and the instructions are adapted to be loaded and executed by a processor to perform the method according to any one of claims 1-6.
9. A terminal, wherein, including: A processor and a memory; wherein, the memory stores a computer program, and the computer program is adapted to be loaded and executed by the processor to perform the method according to any one of claims 1-6.
Citation Information
Patent Citations
Method and system for storage of data in non-volatile media
CN101903866A
Log recording method and device applied to small-capacity storage area and medium
CN112199053A