An asynchronous log processing method for a bare-run program of a relay protection device

By using an asynchronous log processing method with a fixed-length memory pool and a cache queue, the log output problem in the interrupted environment of the bare-run program of the relay protection device is solved, achieving efficient and safe log output, which is suitable for multiple interruption concurrency scenarios.

CN116048914BActive Publication Date: 2026-11-10BEIJING SIFANG JIBAO ENG TECH +1
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202211674783.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-26
Publication Date
2026-11-10
Estimated Expiration
2042-12-26

AI Technical Summary

Technical Problem

In the bare-metal program of relay protection device, existing technology has difficulty in achieving safe and high-speed log output in the context of nested interrupts, especially when multiple interrupts are called concurrently, the correctness and efficiency of log output cannot be guaranteed.

Method used

A fixed-length memory pool is used to manage the log memory space. Log addresses are cached through a cache queue, and the log cache queue is consumed by a background program in a polling manner to achieve asynchronous log output. It supports concurrent calls to the log output interface by interrupt functions to ensure thread safety.

Benefits of technology

In a nested interrupt environment, the correctness and high performance of concurrent log output from multiple interrupts are achieved, reducing the performance overhead of interrupt functions and improving the throughput and flexibility of log output.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116048914B_ABST
    Figure CN116048914B_ABST
Patent Text Reader

Abstract

An asynchronous log processing method for a bare-metal relay protection device program is applied in both the foreground and background programs of the device. The method includes: providing a thread-safe log interface that can asynchronously output log information via interrupt function calls; the log output interface requesting log storage space from a fixed-length log memory pool and quickly formatting it according to a fixed format encoding; storing the formatted log memory address in a log cache queue; the background program polling the cached log queue, decoding the fixed-format log, formatting it into a standard string for output; and finally, releasing the log memory back to the log memory pool. This invention ensures thread safety of the log output interface call even in interrupt-nested operation modes for bare-metal relay protection devices, maintaining the correctness of output when multiple interrupts concurrently output logs. It addresses the need for high-speed concurrent log output, significantly improving the debugging efficiency and security of device development.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of embedded communication technology, and more specifically to an asynchronous log processing method for a bare-metal relay protection device program. Background Technology

[0002] In relay protection devices, to ensure safe, reliable, and high real-time performance, the CPU program typically runs in a bare-metal mode. In this bare-metal program, real-time preemption is generally achieved through nested interrupts. In this environment, log output cannot use the standard printing interface provided by the embedded development environment. Firstly, the standard printing interface's formatting operation is very time-consuming and cannot be called within an interrupt function. Secondly, the standard printing interface itself is not thread-safe. Therefore, the standard printing interface cannot meet the log printing requirements of the bare-metal relay protection device program within its interrupt functions.

[0003] Message-driven asynchronous logging implementations are typically based on a multi-threaded operating system environment. Multiple threads concurrently calling the log output interface rely on mutexes provided by the operating system to ensure thread safety and guarantee the correctness of log output. This is achieved using a message queue-like approach. For example, patent application CN202210347868.3 employs a double-buffering mechanism, enabling simultaneous reading and writing by multiple threads, effectively preventing mutual blocking and improving the efficiency of asynchronous log processing. While this type of asynchronous logging system improves log output efficiency, its implementation depends on operating system support and cannot be achieved in a bare-metal environment.

[0004] Patent application CN202210149737.4 describes a multi-core AMP architecture chip for relay protection devices. It divides the CPU core into a bare core and a system core, with the log system using different interfaces for log output. Log storage is achieved through shared memory between the bare core and the system core, ultimately handled by the system core. For non-interrupt program logs, the bare core's `printf` interface is used to encode the log content into ASCII, then it's placed in shared memory and printed serially. For interrupt program logs, the `logmsg` interface is used to queue the log content. A dedicated information processing task then encodes the queue data into ASCII, places it in shared memory, and prints it serially. This method relies on the system core for final output. In single-core mode or when deploying multiple bare core programs in an AMP architecture, log output is not possible. Furthermore, the log output interface in interrupt programs does not support variable parameters, limiting its usability.

[0005] The existing log management technologies disclosed above mainly achieve high-speed log output through memory buffering and asynchronous log processing. However, in the bare-metal relay protection device program, the above methods are difficult to meet the high-concurrency log output requirements, especially in scenarios where the bare-metal relay protection device program supports interrupt nesting. Summary of the Invention

[0006] To address the shortcomings of existing technologies, the present invention aims to provide an asynchronous log processing method for the bare-running program of a relay protection device, thereby solving the problem that the bare-running program of a relay protection device cannot output logs safely and at high speed under interruption conditions.

[0007] To achieve the above objectives, the present invention adopts the following technical solution:

[0008] An asynchronous log processing method for a bare-metal relay protection device program is disclosed. Primarily used in the CPU-based bare-metal relay protection device program, it supports concurrent calls to the log output interface via interrupt functions, ensuring thread safety of the output interface. A fixed-length memory pool is used to manage the log memory space, and a cached queue is used to cache log addresses. Simultaneously, a background program consumes the log cache queue through polling and ultimately outputs the log information, achieving asynchronous log output for the bare-metal relay protection device program. The asynchronous log output steps include:

[0009] Step 1: Create a fixed-length log memory pool to cache log content;

[0010] Step 2: Create an asynchronous log caching queue;

[0011] Step 3: The interrupt function concurrently calls the log output interface;

[0012] Step 4: The background program polls and executes the log output function to output the log content.

[0013] Preferably, step 1 includes:

[0014] Step 1.1: Based on the maximum memory usage per log entry, the number of log cache entries, and the memory space required to reserve the linked list pointers for the linked list management memory pool, calculate the size of the fixed-length memory pool and dynamically allocate the fixed-length log memory pool space in the heap memory.

[0015] Step 1.2: Initialize the allocated log memory space, ensuring that the memory data is cleared;

[0016] Step 1.3: Create a free list, initialize the list pointers reserved in the fixed-length memory blocks of the memory pool, and add the memory space address to the free list.

[0017] Preferably, in step 1.1, the maximum memory usage of a single log entry includes: a fixed-size log header information and a maximum log output space, and the maximum memory usage of a single log entry is defined according to application requirements.

[0018] Preferably, in step 1.3, the addition and deletion operations of the free list adopt a lock-free strategy, and the lock-free implementation of the addition and deletion operations of the free list is guaranteed by the CAS mechanism provided by the CPU hardware.

[0019] Preferably, in step 2, the asynchronous log cache queue is a lock-free circular queue that uses a bounded array to achieve multiple writes and single reads through the CAS mechanism provided by the CPU hardware layer.

[0020] Preferably, in step 2, the queue depth is set according to the program's processing performance and actual output throughput.

[0021] Preferably, step 3 includes:

[0022] Step 3.1: Allocate log memory space from the fixed-length memory pool using the memory pool allocation function;

[0023] Step 3.2: Initialize the log header information, which includes basic information, log length, and format string length; the basic information includes: log level, timestamp, tag, and calling module ID;

[0024] Step 3.3: Parse the memory data corresponding to the variable parameters according to the format string data, format the log memory according to the correspondence between the format string and the memory usage of the variable parameters, and copy the variable parameter data back to the log memory space according to the actual memory usage of the variable parameters; update the log length information and format string length information contained in the log header information;

[0025] Step 3.4: Add the address of the log memory storage space to the circular queue of the asynchronous log cache.

[0026] Preferably, in step 3.4, when an interrupt occurs and a pointer is added to the circular queue simultaneously, the CAS instruction provided by the CPU hardware layer is used to ensure thread safety of concurrent call interfaces.

[0027] Preferably, step 4 includes:

[0028] Step 4.1: The background program polls the circular queue of the log cache to check if it is empty, and the background program reads the data from the circular queue as a single consumer;

[0029] Step 4.2: If the log cache queue is not empty, read the address of the cached log in the queue, read the log header from the memory at that address, obtain the format string length information from the header, obtain the content of the format string based on the length information, parse the format string information according to the relationship between the variable parameter memory space and the variable parameter described in Step 3.3, obtain the actual memory information of the variable parameter, and format the log content into a standard string using the standard log formatting function.

[0030] Step 4.3: Output the formatted string through the log backend module. The output method can include: file, serial port, or network data packet.

[0031] Step 4.4: After the log output is complete, call the memory release interface to return the memory space occupied by the log memory to the fixed-length log memory pool.

[0032] Preferably, memory release is achieved through the CAS mechanism provided by the CPU hardware layer, enabling lock-free operation when updating the free list and ensuring the atomicity of the free list update operation.

[0033] Compared with the prior art, the present invention has the following beneficial effects:

[0034] 1. The method mentioned in this invention can ensure the correctness of log output results and maintain high performance when multiple interrupts output logs concurrently in a bare-metal program running in an interrupt nested runtime environment.

[0035] 2. This invention significantly improves the processor performance overhead of outputting logs in interrupt functions. It completes asynchronous log output through an independent background program. The asynchronous mode not only improves the throughput of multiple interrupts outputting logs simultaneously, but also greatly improves the overall performance of log output in bare-metal programs by dividing the time-consuming log formatting string operation into two steps. The interrupt function only performs simple formatting operations on the log content, while the standard string formatting operation is completed in the background log output function. Attached Figure Description

[0036] Figure 1 This is a flowchart of the asynchronous log output process provided by the present invention;

[0037] Figure 2 This is a detailed implementation flowchart of the asynchronous log processing method for the bare-run program of the relay protection device provided by the present invention. Detailed Implementation

[0038] The present invention will now be described in detail with reference to specific embodiments. These embodiments will help those skilled in the art to further understand the present invention, but do not limit the invention in any way. It should be noted that those skilled in the art can make several changes and improvements without departing from the concept of the present invention. These all fall within the scope of protection of the present invention.

[0039] It should be understood that the step numbers used in the text are for ease of description only and are not intended to limit the order in which the steps are performed.

[0040] It is worth noting that one of the core concepts of this invention in improving upon existing technologies lies in the initialization of the asynchronous log processing method for the bare-metal relay protection device program. This initialization includes: creating a log storage area and allocating a fixed-length memory pool within the storage area to store log header information and the actual log output content; creating a log circular queue to receive and buffer log information sent by multiple interrupts; creating a background program to poll the log circular queue to obtain log content, perform formatted string processing on the log content, and finally output the logs. Specific log output methods include network, file, serial port, and other methods. Users can call the log output interface as needed to complete the output of log information. After initialization, the bare-metal relay protection device program can call the log output interface simultaneously in interrupt and non-interrupt contexts, buffer log information through the circular queue, and implement asynchronous log output in the background processing function.

[0041] More specifically, such as Figure 1 As shown, an embodiment of the present invention provides an asynchronous log processing method for a bare-running program of a relay protection device, comprising the following steps:

[0042] Step 1: Create a fixed-length log memory pool to cache log content. Manage the memory pool using a free list and leverage the CAS (Compare And Swap) mechanism provided by the CPU's underlying hardware to implement a lock-free interface for adding and deleting items from the free list. This ensures thread safety for concurrent calls to the memory pool's allocation and release mechanisms in a multi-threaded environment. It also ensures that multiple interrupts can concurrently add and delete items from the head pointer of the free list.

[0043] As one of the beneficial technical effects of this invention, the fixed-length memory pool can also ensure that memory fragmentation does not occur during long-term log operation. Furthermore, by controlling the timing of log memory release, the fixed-length memory pool can be fully utilized to cache log data. Applications can customize caching strategies to implement log caching functionality. Compared to storing log information through a queue, the fixed-length memory pool is not limited by the first-in-first-out (FIFO) characteristic of queues, offering greater flexibility and scalability for program implementation.

[0044] In a preferred but non-limiting embodiment of the present invention, step 1 includes the following steps:

[0045] Step 1.1: Based on the program's configuration of the log space memory pool size and the maximum memory usage of a single log entry, the memory space of the linked list pointers that need to be reserved for the memory pool is managed through a linked list, and the fixed-length log memory pool space is dynamically allocated in the heap memory.

[0046] Furthermore, the maximum memory footprint for a single log entry includes a fixed-size log header. The log content itself includes the log header, which contains the log format string length and the actual log content length. The log header may include custom extended fields.

[0047] Step 1.2: Initialize a fixed-length memory pool to ensure that all memory data is cleared.

[0048] Step 1.3: Create a free list, initialize the list pointers reserved in the fixed-length memory blocks of the memory pool, and add the memory space to the free list. The addition and deletion operations in the free list adopt a lock-free strategy, ensuring lock-free implementation through the CAS mechanism provided by the processor hardware.

[0049] Step 2: Create a circular queue for log caching. Use the CAS mechanism provided by the CPU hardware to implement a lock-free circular queue with multiple writes and single reads using a bounded array.

[0050] One of the beneficial technical effects of this invention is that multiple writers require the queue write operation to be implemented as a lock-free operation. Log output can only be completed through a background program; therefore, only one reader consumes queue data, so the queue read operation does not need to be implemented through a lock-free mechanism, thus improving the execution efficiency of the read interface.

[0051] Furthermore, the queue depth can be set according to the program's processing performance and actual output throughput.

[0052] Step 3: The interrupt function concurrently calls the log output interface. The log output interface supports variable parameters and is thread-safe. When multiple interrupt functions output logs at the same time, the log content is buffered through a circular queue.

[0053] In a preferred but non-limiting embodiment of the present invention, step 3 includes the following steps:

[0054] Step 3.1: Allocate log memory space from the fixed-length memory pool using the memory pool allocation function.

[0055] Step 3.2: Initialize log header information.

[0056] Furthermore, the log header information includes basic information and log length, format string length, etc. The basic information may include log level, timestamp, tag, calling module ID, etc.

[0057] Step 3.3: Parse the memory data corresponding to the variable parameters according to the format string data. The standard string formatting operation for variable parameters is not performed, which is time-consuming. Instead, the log memory is formatted by parsing the variable parameter type and according to the memory usage correspondence between the format string and the variable parameter. The variable parameter content is copied to the log memory space allocated from the memory pool according to the actual memory usage of the variable parameter. The log length information and format string length information contained in the log header information are updated.

[0058] Step 3.4: Add the address of the log memory storage space to the circular queue of the asynchronous log cache. When an interrupt simultaneously adds a pointer to the circular queue, this is achieved through the CAS instruction provided by the processor's underlying layer, ensuring thread safety for concurrent interface calls. CAS is implemented by the CPU's underlying hardware; different CPUs have different implementation mechanisms. CAS guarantees the atomicity of read and write operations.

[0059] Step 4: The background polling and execution log output function consumes the log circular queue and outputs the log content asynchronously.

[0060] In a preferred but non-limiting embodiment of the present invention, step 4 includes the following steps:

[0061] Step 4.1: The background program polls the circular queue of the log cache to check if it is empty. The background program reads data from the circular queue as a single consumer.

[0062] Step 4.2: If the log cache queue is not empty, read the cached log address in the queue, read the log header from the memory at that address, obtain the format string length information from the header, obtain the content of the format string based on the length information, parse the format string information according to the relationship between the variable parameter memory space and the variable parameter described in Step 3.3, obtain the actual memory information of the variable parameter, and format the log content into a standard string using the standard log formatting function.

[0063] Step 4.3: Output the formatted string through the log backend module. The output method can be a file, serial port, or network data packet.

[0064] Step 4.4: After the log output is complete, call the memory release interface to return the memory space occupied by the log to the fixed-length log memory pool. Memory release is achieved through CPU CAS to implement lock-free operation when updating the free list, ensuring the atomicity of the free list update operation.

[0065] The above description represents the preferred embodiments of the present invention. It should be noted that those skilled in the art can make various improvements and modifications without departing from the principles of the present invention, and these improvements and modifications are also considered to be within the scope of protection of the present invention.

Claims

1. An asynchronous log processing method for a bare-running program of a relay protection device, characterized in that, In a bare-metal environment of a relay protection device, foreground and background programs that support nested interrupts call the log output interface to asynchronously output logs, including the following steps: Step 1: Create a fixed-length log memory pool to cache log content. The fixed-length memory pool is managed through a free linked list. At the same time, the CAS mechanism provided by the CPU's underlying hardware is used to implement a lock-free interface for adding and deleting items in the linked list. Step 2: Create an asynchronous log cache queue, which is a lock-free circular queue that uses a bounded array to implement multiple writes and single reads through the CAS mechanism provided by the CPU hardware. Step 3: Concurrently call the interrupt function to the log output interface: Step 3.1: Allocate log memory space from the fixed-length memory pool using the memory pool allocation function; Step 3.2: Initialize the log header information, which includes basic information, log length, and format string length; the basic information includes: log level, timestamp, tag, and calling module ID; Step 3.3: Parse the memory data corresponding to the variable parameters according to the format string data, format the log memory according to the correspondence between the format string and the memory usage of the variable parameters, copy the variable parameter data back to the variable parameter memory space according to the actual memory usage of the variable parameters, and update the log length information and format string length information contained in the log header information; Step 3.4: Add the address of the log memory storage space to the circular queue of the asynchronous log cache. When an interrupt occurs and a pointer is added to the circular queue at the same time, the CAS instruction provided by the CPU hardware is used to ensure the thread safety of concurrent call interfaces. Step 4: The background program polls and executes the log output function, outputting the log content, including: Step 4.1: The background program polls the circular queue of the log cache to check if it is empty, and the background program reads the data from the circular queue as a single consumer; Step 4.2: If the log cache queue is not empty, read the address of the cached log in the queue, read the log header from the memory at that address, obtain the format string length information from the header, obtain the content of the format string based on the length information, parse the format string information according to the relationship between the variable parameter memory space and the variable parameter described in Step 3.3, obtain the actual memory information of the variable parameter, and format the log content into a standard string using the standard log formatting function. Step 4.3: Output the formatted string through the log backend module; Step 4.4: After the log output is completed, the memory release interface is called to return the memory space occupied by the log memory to the fixed-length log memory pool. The memory release is achieved through the CAS mechanism provided by the CPU hardware to realize lock-free operation when updating the free list, ensuring the atomicity of the free list update operation.

2. The asynchronous log processing method for a bare-running program of a relay protection device according to claim 1, characterized in that: Step 1 includes: Step 1.1: Based on the maximum memory usage per log entry, the number of log cache entries, and the memory space required to reserve the linked list pointers for the linked list management memory pool, calculate the size of the fixed-length memory pool and dynamically allocate the fixed-length log memory pool space in the heap memory. Step 1.2: Initialize the allocated log memory space, ensuring that the memory data is cleared; Step 1.3: Create a free list, initialize the list pointers reserved in the fixed-length memory blocks of the memory pool, and add the memory space address to the free list.

3. The asynchronous log processing method for a bare-running program of a relay protection device according to claim 2, characterized in that: In step 1.1, the maximum memory usage of a single log entry includes: a fixed-size log header and the maximum log output space. The maximum memory usage of a single log entry is defined according to application requirements.

4. The asynchronous log processing method for a bare-running program of a relay protection device according to claim 1, characterized in that: In step 2, the queue depth is set according to the program's processing performance and actual output throughput.

5. The asynchronous log processing method for a bare-running program of a relay protection device according to claim 1, characterized in that: In step 4.3, the output methods include: file, serial port, or network data packet.

Citation Information

Patent Citations

  • A method and system for recording logs of a bare kernel program of a relay protection device

    CN114546680B

  • Asynchronous log processing method based on message driving

    CN114817185A

  • Log service method and system based on embedded real-time system

    CN108089971A

  • Implementation method for asynchronous log recording of embedded operating system

    CN112948136A