Trace data persistence system and method based on CODESYS

By introducing modules such as the persistence coordinator, record persistence manager, and global write manager into the CODESYS platform, the shortcomings of CODESYS's native Trace function in data persistence and management have been resolved. This has enabled efficient and reliable Trace data storage, supports long-term recording and fault backtracking, and meets the needs of industrial automation.

CN122018819APending Publication Date: 2026-05-12ZHONGKE TIMES (SHENZHEN) COMPUTER SYST CO LTD
View PDF 7 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
ZHONGKE TIMES (SHENZHEN) COMPUTER SYST CO LTD
Filing Date
2026-04-13
Publication Date
2026-05-12

AI Technical Summary

Technical Problem

The existing CODESYS native Trace function has significant shortcomings in data persistence, long-term storage, data management and integrity assurance, making it difficult to meet the needs of industrial automation for long-term recording of production data, fault backtracking and system monitoring.

Method used

By working together with the persistence coordinator, record persistence manager, global write manager, and file writer, persistent storage of trace data is achieved. A lock-free circular buffer, adaptive load control, and multi-level rollback mechanism are used to ensure efficient caching and stable writing of data, supporting data management in multiple applications and multiple trace scenarios.

Benefits of technology

It achieves efficient and reliable persistent storage of trace data, meeting the needs of industrial automation for long-term recording of production data, fault backtracking, and system monitoring, with performance loss of less than 5%, robust fault tolerance, and flexible configuration management.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122018819A_ABST
    Figure CN122018819A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of industrial automation, and provides a Trace data persistence system and method based on CODESYS, and the system comprises a persistence coordinator which corresponds to each Trace in a CODESYS native Trace function, and is used for obtaining a configuration parameter corresponding to the current Trace; the record persistence manager is used for generating a write-in task when the lock-free annular buffer area reaches a buffer area refreshing threshold value or a timeout condition; the global write-in manager adopts a singleton mode and is used for queuing write-in tasks according to task type allocation priorities and dynamically allocating the write-in tasks to idle file writers according to performance optimization parameters and system loads; and the file writers are dynamically allocated by the global write manager and used for executing actual disk write, and the file writers adaptively select a memory mapping mode or a traditional buffer mode to perform data persistence according to the write mode, the system capability and the file size.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of industrial automation technology, and in particular to a system and method for persistent trace data based on CODESYS. Background Technology

[0002] In the field of industrial automation control, PLCs (Programmable Logic Controllers) are widely used as core control devices in various production and manufacturing scenarios. CODESYS, as a PLC programming development environment that conforms to the IEC 61131-3 standard, has become one of the mainstream software development platforms in the field of industrial automation due to its openness, cross-platform compatibility, and rich functional components.

[0003] The CODESYS platform has a built-in Trace function for real-time acquisition and monitoring of variable changes during PLC operation. This function, by configuring the sampling period and trigger conditions, can graphically display the real-time waveforms of variables, facilitating program debugging, performance analysis, and fault diagnosis for developers. However, the native Trace function is primarily geared towards online debugging scenarios. The existing CODESYS native Trace function has significant shortcomings in data persistence, long-term storage, data management, and integrity assurance, making it difficult to meet the needs of industrial automation for long-term recording of production data, fault backtracking, quality traceability, and system monitoring. Summary of the Invention

[0004] In view of this, the present disclosure provides a CODESYS-based Trace data persistence system, method, electronic device, and computer-readable storage medium to solve the technical problem that the data processing capability of the original CODESYS Trace function in the prior art cannot meet the production needs due to limited capacity.

[0005] A first aspect of this disclosure provides a CODESYS-based Trace data persistence system applied to an industrial automation control system, comprising: a persistence coordinator, corresponding to each Trace in the native CODESYS Trace function, for acquiring configuration parameters corresponding to the current Trace, the configuration parameters including at least a persistence switch, a write mode, a disk protection threshold, a buffer refresh threshold, and performance optimization parameters; wherein the persistence coordinator enables or disables the generation of data receiving and writing tasks by the record persistence manager according to the persistence switch, the persistence coordinator monitors the remaining disk space according to the disk protection threshold, and controls the record persistence manager to stop generating write tasks when the space is below the disk protection threshold; and a record persistence manager, corresponding to each data record, including a lock-free circular buffer for receiving and caching data when the persistence switch is enabled. From the data records of CODESYS native Trace, when the lock-free circular buffer reaches the buffer refresh threshold or timeout condition, the record persistence manager generates a write task; the global write manager, using a singleton pattern, receives write tasks, prioritizes them according to task type, queues them, and dynamically allocates them to idle file writers based on performance optimization parameters and system load; the file writer, dynamically allocated by the global write manager, performs the actual disk write, and adaptively selects memory mapping mode or traditional buffer mode for data persistence based on the write mode, system capacity, and file size; among these, the persistence coordinator, record persistence manager, global write manager, and file writer are all integrated with CODESYS native Trace functionality through an extension mechanism without modifying the CODESYS native Trace code.

[0006] A second aspect of this disclosure provides a CODESYS-based Trace data persistence method applied to an industrial automation control system, comprising: obtaining configuration parameters corresponding to the current Trace through a persistence coordinator, the configuration parameters including at least a persistence switch, a write mode, a disk protection threshold, a buffer refresh threshold, and performance optimization parameters; enabling or disabling data reception and write task generation of the record persistence manager according to the persistence switch, and monitoring the remaining disk space according to the disk protection threshold, controlling the record persistence manager to stop write task generation when the space is below the disk protection threshold; and receiving and caching data from the native CODESYS Trace through an unlocked circular buffer maintained by the record persistence manager when the persistence switch is enabled. According to the records, when the lock-free circular buffer reaches the buffer refresh threshold or timeout condition, a write task is generated through the record persistence manager; the write task is received through the global write manager, which prioritizes and queues the write tasks according to task type, and dynamically allocates them to idle file writers based on performance optimization parameters and system load; the actual disk write is performed through the file writer, which adaptively selects memory mapping mode or traditional buffer mode for data persistence based on the write mode, system capacity, and file size; the persistence coordinator, record persistence manager, global write manager, and file writer are all integrated with the CODESYS native Trace function through an extension mechanism without modifying the CODESYS native Trace code.

[0007] A third aspect of this disclosure provides an electronic device including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the method described above.

[0008] A fourth aspect of this disclosure provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps of the above-described method.

[0009] The beneficial effects of this disclosed embodiment compared with the prior art are as follows: The technical solution of this disclosed embodiment obtains configuration parameters through a persistent coordinator, generates write tasks by recording data cached by a persistent manager, and distributes write tasks to file writers through a global write manager. Without modifying the original CODESYS Trace code, it achieves persistent storage of Trace data. This can make up for the obvious defects of the original CODESYS Trace function in terms of data persistence, long-term storage, data management and integrity assurance, and meet the needs of industrial automation for long-term recording of production data, fault backtracking, quality traceability and system monitoring. Attached Figure Description

[0010] To more clearly illustrate the technical solutions in the embodiments of this disclosure, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this disclosure. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0011] Figure 1 This is a schematic diagram of the structure of a CODESYS-based Trace data persistence system provided in an embodiment of this disclosure; Figure 2 This is a schematic diagram of another CODESYS-based Trace data persistence system provided in this embodiment of the disclosure; Figure 3 This is a flowchart illustrating a method for persisting trace data based on CODESYS, as provided in an embodiment of this disclosure. Figure 4 This is a schematic diagram of the data writing process provided in an embodiment of this disclosure; Figure 5 This is a schematic diagram of the exception handling process provided in the embodiments of this disclosure; Figure 6 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this disclosure. Detailed Implementation

[0012] In the following description, specific details such as particular system architectures and techniques are set forth for illustrative purposes and not for limitation, so as to provide a thorough understanding of the embodiments of this disclosure. However, those skilled in the art will understand that this disclosure may also be implemented in other embodiments without these specific details. In other instances, detailed descriptions of well-known systems, apparatuses, circuits, and methods have been omitted so as not to obscure the description of this disclosure with unnecessary detail.

[0013] The CODESYS-based Trace data persistence system and method according to embodiments of this disclosure will now be described in detail with reference to the accompanying drawings.

[0014] Figure 1 This is a schematic diagram of a CODESYS-based trace data persistence system provided in an embodiment of this disclosure. Figure 1 As shown, this CODESYS-based trace data persistence system is applied to industrial automation control systems, including: The persistence coordinator 101 corresponds to each Trace in the native Trace function of CODESYS. It is used to obtain the configuration parameters corresponding to the current Trace. The configuration parameters include at least the persistence switch, write mode, disk protection threshold, buffer refresh threshold, and performance optimization parameters. The persistence coordinator enables or disables the recording persistence manager 102's data reception and write task generation according to the persistence switch. The persistence coordinator monitors the remaining disk space according to the disk protection threshold and controls the recording persistence manager to stop writing task generation when it is below the disk protection threshold.

[0015] The record persistence manager 102, corresponding to each data record, includes a lock-free circular buffer for receiving and caching data records from the CODESYS native Trace when the persistence switch is enabled. When the lock-free circular buffer reaches the buffer refresh threshold or timeout condition, the record persistence manager generates a write task.

[0016] The global write manager 103, which adopts a singleton pattern, is used to receive write tasks, assign priority to write tasks according to task type, queue them, and dynamically allocate them to idle file writers based on performance optimization parameters and system load.

[0017] File writer 104 is dynamically allocated by the global write manager and is used to perform actual disk writes. The file writer adaptively selects either memory-mapped mode or traditional buffered mode for data persistence based on the write mode, system capabilities, and file size. Among them, the persistence coordinator, record persistence manager, global write manager, and file writer are all integrated with the original CODESYS Trace function through an extension mechanism without modifying the original CODESYS Trace code.

[0018] In the technical solution of this disclosure embodiment, the persistence coordinator controls the start and stop of data acquisition and disk space monitoring according to configuration parameters to ensure automatic protection of the system when resources are limited; the record persistence manager implements low-latency data caching through a lock-free circular buffer; the global write manager ensures efficient processing of write tasks through priority scheduling and load balancing; and the file writer adaptively selects the write mode to balance performance and compatibility. The overall solution achieves a seamless extension of the native Trace function, solves the problems of data volatility and limited capacity of the native Trace, and supports 24 / 7 continuous recording.

[0019] In this embodiment of the disclosure, the lock-free circular buffer in the record persistence manager uses CAS atomic operations to maintain the write index, read index, and entry count; the nodes of the lock-free circular buffer are aligned to 64 bytes through cache lines; and the lock-free circular buffer is equipped with a sequence number mechanism.

[0020] By employing CAS atomic operations to maintain the buffer index, lock-free concurrent access is achieved, avoiding the context switching overhead and deadlock risk associated with traditional locking mechanisms. Aligning cache lines to 64 bytes places frequently accessed variables from different CPU cores in different cache lines, effectively preventing false sharing and improving concurrency performance in multi-core environments. A sequence number mechanism resolves the common ABA problem in CAS operations, ensuring the correctness and consistency of data operations. These techniques collectively achieve high-concurrency, low-latency data buffering, minimizing the impact of data persistence on PLC real-time tasks; for example, performance degradation can be less than 5%.

[0021] In this embodiment of the disclosure, the global write manager further includes: an adaptive load control module, used to monitor CPU utilization in real time and automatically increase the sleep time of write threads or reduce the number of batches when CPU utilization exceeds a preset threshold; and a priority scheduling module, used to assign different priorities to write tasks according to task type, wherein timeout tasks and forced write tasks have higher priority than normal write tasks.

[0022] By using an adaptive load control module to monitor CPU utilization in real time and dynamically adjust the sleep time of write threads and the number of batches, intelligent adjustment of system load is achieved. Under high load, write activity is proactively reduced to avoid affecting the execution of PLC real-time tasks. A priority scheduling module assigns different priorities based on task type, ensuring that timeout tasks and forced write tasks are processed first, avoiding the loss or delay of critical data. The combination of these two approaches achieves resource balance between write tasks and real-time tasks, guaranteeing stable system operation in high-frequency data acquisition scenarios.

[0023] In this embodiment, files are named in the format {application name}.{trace name}_{timestamp}_{serial number}.trace.csv, and directories are organized hierarchically by application and trace name. Specifically, the file directory structure of the file writer is organized hierarchically by application name and trace name, with each trace having an independent configuration file and storage directory; the file writer uses file pre-allocation technology to pre-allocate contiguous disk space for the files to be written, and the pre-allocation size is dynamically determined based on system memory and file size evaluation values.

[0024] The hierarchical directory structure, organized by application and trace names, enables clear classification and independent management of data files across multiple applications and traces, facilitating user location and retrieval of historical data. Each trace has its own independent configuration file and storage directory, supporting differentiated configurations for different traces and enhancing system flexibility. File pre-allocation technology pre-allocates contiguous disk space, effectively reducing file system fragmentation and improving sequential write performance. The pre-allocation size is dynamically determined based on system memory and file size assessments, ensuring write performance while avoiding excessive memory consumption, thus achieving a balance between performance and resources.

[0025] In this embodiment of the disclosure, the system adopts a multi-level rollback mechanism: when the memory-mapped write of the file writer fails, it automatically rolls back to the traditional buffer write; when the file writer fails to write the file, it attempts to reopen the file or rebuild the buffer; when the buffer of the record persistence manager is full, it automatically expands the capacity or discards the oldest data according to the configuration; and atomic operations are used to ensure data integrity for critical state updates.

[0026] Through a multi-level fallback mechanism, the system can automatically switch to alternative solutions when encountering different levels of failure: when memory mapping fails, it falls back to traditional buffered writing, ensuring the continuity of the writing function; when file writing fails, it attempts to reopen the file or rebuild the buffer, enhancing the system's fault tolerance; when the buffer is full, it automatically expands its capacity or discards the oldest data according to the configuration, avoiding system crashes or data blockages; critical state updates use atomic operations, ensuring the consistency of data state in a concurrent environment. These mechanisms collectively build the system's robustness, ensuring that data is not lost and the system does not crash under abnormal conditions, achieving high reliability.

[0027] In this embodiment of the disclosure, the persistence coordinator supports a configuration management mechanism: each trace corresponds to an independent INI configuration file, which supports online modification of the configuration; if a file is missing during configuration loading, it is automatically generated according to the default configuration; the persistence coordinator passes the configuration parameters to the record persistence manager, the global write manager, and the file writer.

[0028] By using an independent INI configuration file for each trace, configuration and code are separated, allowing users to flexibly adjust persistence behavior according to different scenarios. Online configuration modification is supported, enabling dynamic parameter adjustments without system restarts, thus enhancing system maintenance convenience. If a configuration file is missing during loading, it is automatically generated using the default configuration, lowering the user's learning curve and achieving out-of-the-box usability. The persistence coordinator uniformly transmits configuration parameters to all components, ensuring configuration consistency and correctness. These mechanisms collectively achieve configuration flexibility, ease of use, and dynamism, enabling the system to adapt to diverse industrial application scenarios.

[0029] In this embodiment of the disclosure, the persistence coordinator includes a disk space monitoring module, which monitors the remaining disk space simultaneously using both a remaining percentage threshold and an absolute size threshold. When the remaining space is lower than either threshold, the disk space monitoring module triggers a protection action, which includes: stopping the recording of write tasks generated by the persistence manager, stopping the global write manager from receiving new tasks, and automatically deleting the oldest file in circular overwrite mode to free up space.

[0030] By simultaneously monitoring disk space using both remaining percentage and absolute size thresholds, dual protection for disk resources is achieved, effectively triggering protection regardless of disk size. When the disk space falls below either threshold, the system automatically stops generating and receiving write tasks, preventing data loss or system anomalies due to disk exhaustion. In circular overwrite mode, the oldest files are automatically deleted to free up space, enabling automatic recycling of storage space and supporting long-term continuous recording without manual intervention. These mechanisms ensure the long-term stable operation of the system and prevent failures caused by storage resource depletion.

[0031] like Figure 2 The diagram illustrates a CODESYS-based Trace data persistence system, in which a host computer 210 sends operation commands to the CODESYS Runtime system 220. The host computer 210 has a CODESYS Trace management interface, which includes trace package configuration, real-time monitoring, and historical playback. The CODESYS Runtime system 220 includes a native Trace function module 211 and a persistent recording and playback enhancement layer 212. The persistent recording and playback enhancement layer 212 interacts with the native Trace function module 211 via interface calls, obtaining data generated by the native Trace through extended interfaces. The native Trace function module 211 includes functions such as variable access, trigger management, data acquisition, and memory buffering. The persistent recording and playback enhancement layer 212 has file management, buffer management, and configuration management functions. The file management function includes file writing strategies, memory mapping technology, and asynchronous queue writing; the buffer management function includes lock-free circular buffering, CAS atomic operations, and exponential backoff strategies; and the configuration management function includes configuration parsing, configuration verification, and configuration updating.

[0032] In this embodiment, the global write manager may further include a priority calculation module for calculating task priorities based on the task type of the write task. Specifically, the priority calculation module employs the following priority allocation strategy: when the write task is a timeout task, a first priority value, such as 100, is assigned; when the write task is a forced write task, a second priority value, such as 200, is assigned; and when the write task is a normal write task, a third priority value, such as 50, is assigned. The first and second priority values ​​are both greater than the third priority value, and the second priority value is greater than the first priority value. Furthermore, the priority calculation module also adjusts the task priority based on the priority of the data record corresponding to the write task, so that write tasks for high-priority data records receive higher scheduling priority. Through the above priority calculation mechanism, timeout tasks and forced write tasks are ensured to be processed with priority, avoiding the loss or delay of critical data.

[0033] In this embodiment, the global write manager may further include a CPU utilization monitoring module and an adaptive sleep control module. The CPU utilization monitoring module monitors the CPU utilization of write threads in real time. Specifically, it calculates CPU utilization by statistically analyzing the activity and idle times of write threads over a preset time period. The calculation formula is: CPU utilization = Activity time / (Active time + Idle time) × 100%. The adaptive sleep control module dynamically adjusts the sleep time of write threads based on CPU utilization: when CPU utilization exceeds a first preset threshold, the adaptive sleep control module increases the sleep time of write threads to reduce the resource consumption of write activities; when CPU utilization is below a second preset threshold, the adaptive sleep control module decreases the sleep time of write threads to improve write throughput. The adaptive sleep control module also sets a maximum sleep time limit to prevent buffer overflow caused by excessively long sleep times. Through the above mechanism, intelligent adjustment of system load is achieved, proactively reducing write activity under high load to avoid affecting the execution of PLC real-time tasks.

[0034] In this embodiment, the file writer may further include a system capability detection module, used to detect the current operating environment's support for memory mapping during system startup or before the first use of memory mapping mode. Specifically, the system capability detection module detects memory mapping support through the following steps: creating a temporary file; calling a memory mapping function to attempt to map the temporary file to the process address space; if the mapping is successful, it determines that the current system supports memory mapping mode and releases the mapping resources and the temporary file; if the mapping fails, it determines that the current system does not support memory mapping mode and records the detection result to the system capability flag. The file writer adaptively selects the writing mode based on the system capability flag: when the system capability flag indicates support for memory mapping and the size of the file to be written meets preset conditions, the memory mapping mode is selected; otherwise, the traditional buffering mode is selected. Through the above detection mechanism, it is ensured that the system can run stably under different hardware platforms and operating system environments, enhancing the system's portability and compatibility.

[0035] In this embodiment, the lock-free circular buffer in the record persistence manager is implemented using a custom structure. The structure includes: a write index field (atomic type storage) to record the position of the next data to be written; a read index field (atomic type storage) to record the position of the next data to be read; a count field (atomic type storage) to record the number of data entries currently stored in the buffer; a sequence number field (atomic type storage) to prevent the ABA problem in CAS operations; a status field (atomic type storage) to record the current state of the buffer; a data entry array to store the actual data records; a capacity field to record the maximum capacity of the buffer; start and end timestamp fields to record the time range of data in the buffer; and a linked list pointer field to organize multiple buffers into a linked list structure. The structure is aligned to 64 bytes using cache lines to ensure that the write index, read index, and count fields are located in different cache lines, avoiding false sharing problems in multi-core environments. The record persistence manager maintains the write index, read index, and count fields through CAS atomic operations, achieving lock-free concurrent access.

[0036] In this embodiment, the system may further include a resource limit management module for obtaining resource limit information of the current operating environment during system startup. Specifically, the resource limit management module obtains process address space limits and file size limits through system calls and stores the obtained limit values ​​in the system capability structure. When initializing the memory-mapped write mode, the file writer calculates the upper limit of the pre-allocated size based on the address space limit in the system capability structure: upper limit of pre-allocated size = min(expected file size, address space limit × 80%), avoiding system resource exhaustion due to excessive virtual address space occupation by memory mapping. When the address space limit is unlimited, the file writer determines the upper limit of the pre-allocated size based on the system's physical memory size. Through the above resource limit management mechanism, reasonable utilization of system resources is achieved, preventing system instability caused by excessive resource occupation.

[0037] All of the above-mentioned optional technical solutions can be combined in any way to form the optional embodiments of this application, and will not be described in detail here.

[0038] According to the CODESYS-based Trace data persistence system of this disclosure, configuration parameters are obtained through a persistence coordinator, data is cached and written tasks are generated through a recording persistence manager, and write tasks are allocated to file writers through a global write manager. This achieves persistent storage of Trace data without modifying the original CODESYS Trace code. This can make up for the obvious deficiencies of the original CODESYS Trace function in terms of data persistence, long-term storage, data management and integrity assurance, and meet the needs of industrial automation for long-term recording of production data, fault backtracking, quality traceability and system monitoring.

[0039] The technical solution of this disclosure constructs a high-performance, highly reliable industrial data persistence system through an extension mechanism without modifying the original CODESYS Trace code. At the data acquisition level, a lock-free circular buffer based on CAS atomic operations, combined with cache line alignment technology, effectively avoids false sharing issues in multi-core environments, achieving concurrent writes with zero lock contention. Combined with an intelligent batch processing mechanism to dynamically merge small I / O operations, it significantly reduces the number of system calls. Adaptive load control monitors CPU utilization in real time, automatically adjusting thread sleep time and batch size under high load to ensure that the impact of data persistence on PLC real-time tasks is less than 5%. In terms of file and storage management, a hierarchical directory structure of "application / trace name / timestamp" is used to achieve clear data organization, supporting both cyclic overwrite and full-stop write strategies. Disk space is monitored through dual thresholds of remaining percentage and absolute size, and file pre-allocation technology reduces file system fragmentation, ensuring the stability of long-term continuous recording. The system boasts robust fault tolerance and compatibility, adaptively adjusting its operational strategy through multi-level rollback mechanisms and system compatibility checks at startup. It also maintains all native Trace APIs unchanged, controlling persistence functionality through compiler macros and runtime INI switches to ensure seamless integration with existing projects. Regarding security and reliability, it utilizes memory barriers to guarantee operation order, a sequence number mechanism to prevent ABA problems, and limits memory mapping size and employs multi-level threshold management to ensure the system always operates within safe boundaries.

[0040] The technical solution of this disclosure realizes equipment fault diagnosis and root cause analysis by tracing historical data, provides performance trend data to support process optimization, meets the quality traceability requirements of key production data in the context of Industry 4.0, and replaces manual recording with automated recording and broadcasting, significantly reducing on-site maintenance costs; in the future, it can evolve towards remote / cloud storage, data compression, encrypted storage and distributed storage to meet the needs of industrial IoT with larger scale and higher security.

[0041] The following are embodiments of the methods disclosed herein, which can be executed based on the system embodiments disclosed herein. The methods provided in these embodiments can be executed by any electronic device with computer processing capabilities, such as an industrial control computer. The CODESYS-based Trace data persistence method described below corresponds to and can be referred to in relation to the CODESYS-based Trace data persistence system described above. For details not disclosed in the method embodiments of this disclosure, please refer to the system embodiments disclosed herein.

[0042] Figure 3 This is a flowchart illustrating a method for persisting trace data based on CODESYS, provided in an embodiment of this disclosure. Figure 3As shown, this CODESYS-based Trace data persistence method is applied to industrial automation control systems, including: Step S301: Obtain the configuration parameters corresponding to the current Trace through the persistence coordinator. The configuration parameters include at least the persistence switch, write mode, disk protection threshold, buffer flush threshold, and performance optimization parameters.

[0043] Step S302: Enable or disable the generation of data receiving and writing tasks for the record persistence manager according to the persistence switch, monitor the remaining disk space according to the disk protection threshold, and control the record persistence manager to stop generating writing tasks when the space is below the disk protection threshold.

[0044] Step S303: When the persistence switch is enabled, data records from the native CODESYS Trace are received and cached through the lock-free circular buffer maintained by the persistence manager.

[0045] Step S304: When the lockless circular buffer reaches the buffer refresh threshold or timeout condition, a write task is generated by the record persistence manager.

[0046] Step S305: Receive write tasks through the global write manager, queue the write tasks according to task type priority, and dynamically allocate them to idle file writers based on performance optimization parameters and system load.

[0047] Step S306: Perform actual disk writing through the file writer, wherein the file writer adaptively selects either memory mapping mode or traditional buffer mode for data persistence based on the writing mode, system capabilities, and file size; wherein the persistence coordinator, record persistence manager, global write manager, and file writer are all integrated with the CODESYS native Trace function through an extension mechanism without modifying the CODESYS native Trace code.

[0048] The technical solution of this disclosure achieves persistent storage of CODESYS native Trace data through a complete process of configuration acquisition, data buffering, task generation, priority scheduling, and adaptive writing. The configuration acquisition step enables flexible runtime behavior control; the lock-free circular buffer ensures low-latency caching in high-frequency data acquisition scenarios; priority scheduling and load balancing ensure resource balance between write tasks and real-time tasks; and the adaptive write mode selection balances performance and compatibility. The overall method solves the problems of volatility and limited capacity of native Trace data without modifying the native Trace code, achieving long-term, continuous, and reliable industrial data recording and playback storage.

[0049] like Figure 4As shown, in the data writing process, producer thread 401, lock-free buffer 402, buffer manager 403, and write thread 404 interact. Lock-free buffer 402 obtains the write position from producer thread 401 via CAS and returns the write index. Producer thread 401 writes data to the lock-free buffer and performs atomic updates to the count. Lock-free buffer 402 sends a buffer status check command to buffer manager 403. If the buffer is not full, buffer manager 403 replies to continue filling. If the buffer is full, lock-free buffer 402 commits the buffer to buffer manager 403. Buffer manager 403 adds a write task to write thread 404, and write thread 404 asynchronously writes to the file.

[0050] like Figure 5 As shown, the exception handling process includes the following steps: Step S501: Monitor and detect abnormalities.

[0051] Step S502: Determine the exception type.

[0052] Step S503, the exception type is insufficient disk space.

[0053] Step S504: Determine the protection mode. If it is cyclic overwrite, proceed to step S513; if it is stop recording, proceed to step S515.

[0054] Step S505, the exception type is too high memory usage.

[0055] Step S506: Reduce the buffer size.

[0056] Step S507, the exception type is write performance degradation.

[0057] Step S508, switch write mode Step S509: Switch memory mapping to buffered write.

[0058] Step S510, performance recovery test.

[0059] Step S511, the exception type is configuration error.

[0060] Step S512: Use the default configuration.

[0061] Step S513: Delete the oldest file.

[0062] Step S514: Reduce the sampling frequency.

[0063] Step S515: Stop writing new files.

[0064] Step S516: Determine if the recovery was successful. If yes, proceed to step S520; otherwise, proceed to step S519.

[0065] Step S517: Record configuration errors.

[0066] Step S518: Release space.

[0067] Step S519: Record the error log.

[0068] Step S520: Restore to normal mode.

[0069] Step S521, state restored.

[0070] Step S522, continue monitoring.

[0071] Since the functional modules of the CODESYS-based Trace data persistence method in the example embodiments of this disclosure correspond to the steps of the CODESYS-based Trace data persistence system in the example embodiments described above, for details not disclosed in the method embodiments of this disclosure, please refer to the CODESYS-based Trace data persistence system embodiments described above.

[0072] According to the CODESYS-based Trace data persistence method of this disclosure, configuration parameters are obtained through a persistence coordinator, data is cached and written tasks are generated through a recording persistence manager, and write tasks are allocated to file writers through a global write manager. This achieves persistent storage of Trace data without modifying the original CODESYS Trace code. This can make up for the obvious deficiencies of the original CODESYS Trace function in terms of data persistence, long-term storage, data management and integrity assurance, and meet the needs of industrial automation for long-term recording of production data, fault backtracking, quality traceability and system monitoring.

[0073] It should be understood that the sequence number of each step in the above embodiments does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this disclosure.

[0074] Figure 6 This is a schematic diagram of the electronic device 6 provided in an embodiment of this disclosure. Figure 6 As shown, the electronic device 6 of this embodiment includes a processor 601, a memory 602, and a computer program 603 stored in the memory 602 and executable on the processor 601. When the processor 601 executes the computer program 603, it implements the steps in the various method embodiments described above. Alternatively, when the processor 601 executes the computer program 603, it implements the functions of each module in the various device embodiments described above.

[0075] Electronic device 6 can be a desktop computer, laptop, handheld computer, cloud server, or other electronic device. Electronic device 6 may include, but is not limited to, processor 601 and memory 602. Those skilled in the art will understand that... Figure 6 This is merely an example of electronic device 6 and does not constitute a limitation on electronic device 6. It may include more or fewer components than shown, or different components.

[0076] The processor 601 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc.

[0077] The memory 602 can be an internal storage unit of the electronic device 6, such as a hard disk or RAM of the electronic device 6. The memory 602 can also be an external storage device of the electronic device 6, such as a plug-in hard disk, Smart Media Card (SMC), Secure Digital (SD) card, Flash Card, etc., equipped on the electronic device 6. The memory 602 can also include both internal and external storage units of the electronic device 6. The memory 602 is used to store computer programs and other programs and data required by the electronic device.

[0078] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the above-described division of functional units and modules is merely an example. In practical applications, the above functions can be assigned to different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiments can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0079] If the integrated module is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments can also be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program may include computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium may include: any entity or device capable of carrying computer program code, recording media, USB flash drives, portable hard drives, magnetic disks, optical disks, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signals, telecommunication signals, and software distribution media, etc. It should be noted that the content included in the computer-readable medium may be appropriately added or removed according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, the computer-readable medium does not include electrical carrier signals and telecommunication signals.

[0080] The above embodiments are only used to illustrate the technical solutions of this disclosure, and are not intended to limit it. Although this disclosure 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. Such 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 disclosure, and should all be included within the protection scope of this disclosure.

Claims

1. A trace data persistence system based on CODESYS, applied to industrial automation control systems, characterized in that, include: The persistence coordinator, corresponding to each Trace in the native CODESYS Trace function, is used to obtain the configuration parameters corresponding to the current Trace. The configuration parameters include at least the persistence switch, write mode, disk protection threshold, buffer refresh threshold, and performance optimization parameters. The persistence coordinator enables or disables the recording persistence manager's data reception and write task generation based on the persistence switch. The persistence coordinator monitors the remaining disk space based on the disk protection threshold and controls the recording persistence manager to stop writing task generation when the space is below the disk protection threshold. The record persistence manager, corresponding to each data record, includes a lock-free circular buffer for receiving and caching data records from the CODESYS native Trace when the persistence switch is enabled. When the lock-free circular buffer reaches the buffer refresh threshold or timeout condition, the record persistence manager generates a write task. The global write manager, which adopts a singleton pattern, is used to receive the write tasks, assign priorities to the write tasks according to task type, queue them, and dynamically allocate them to idle file writers based on the performance optimization parameters and system load. The file writer is dynamically allocated by the global write manager and is used to perform actual disk writing. The file writer adaptively selects either memory mapping mode or traditional buffer mode for data persistence based on the write mode, system capabilities, and file size. The persistence coordinator, the record persistence manager, the global write manager, and the file writer are all integrated with the original CODESYS Trace functionality through an extension mechanism without modifying the original CODESYS Trace code.

2. The system according to claim 1, characterized in that, The lock-free circular buffer in the record persistence manager uses CAS atomic operations to maintain write indexes, read indexes, and entry counts; the nodes of the lock-free circular buffer are aligned to 64 bytes through cache lines; and the lock-free circular buffer is equipped with a sequence number mechanism.

3. The system according to claim 1, characterized in that, The global write manager also includes: an adaptive load control module, which monitors CPU utilization in real time and automatically increases the sleep time of write threads or reduces the number of batches when CPU utilization exceeds a preset threshold; and a priority scheduling module, which assigns different priorities to write tasks according to task type, wherein timeout tasks and forced write tasks have higher priority than normal write tasks.

4. The system according to claim 1, characterized in that, The file directory structure of the file writer is organized hierarchically by application name and trace name, and each trace has an independent configuration file and storage directory; the file writer uses file pre-allocation technology to pre-allocate contiguous disk space for the file to be written, and the pre-allocation size of the file pre-allocation technology is dynamically determined based on the system memory and file size evaluation value.

5. The system according to claim 1, characterized in that, The system employs a multi-level rollback mechanism: when the memory-mapped write of the file writer fails, it automatically rolls back to traditional buffered write; when the file write of the file writer fails, it attempts to reopen the file or rebuild the buffer; when the buffer of the record persistence manager is full, it automatically expands the capacity or discards the oldest data according to the configuration; and atomic operations are used for critical state updates to ensure data integrity.

6. The system according to claim 1, characterized in that, The persistence coordinator supports a configuration management mechanism: each trace corresponds to an independent INI configuration file, which supports online configuration modification; if a file is missing during configuration loading, it will be automatically generated according to the default configuration; the persistence coordinator passes configuration parameters to the record persistence manager, the global write manager, and the file writer.

7. The system according to claim 1, characterized in that, The persistent coordinator includes a disk space monitoring module, which is used to monitor the remaining disk space simultaneously using both remaining percentage threshold and absolute size threshold. When the remaining space falls below any threshold, the disk space monitoring module triggers a protection action, which includes: stopping the record persistence manager from generating write tasks, stopping the global write manager from receiving new tasks, and automatically deleting the oldest file in circular overwrite mode to free up space.

8. A method for persisting trace data based on CODESYS, applied to industrial automation control systems, characterized in that... include: The configuration parameters corresponding to the current Trace are obtained through the persistence coordinator. The configuration parameters include at least the persistence switch, write mode, disk protection threshold, buffer flush threshold, and performance optimization parameters. Enable or disable the generation of data receiving and writing tasks for the record persistence manager according to the persistence switch, and monitor the remaining disk space according to the disk protection threshold. When the remaining disk space is lower than the disk protection threshold, control the record persistence manager to stop generating writing tasks. When the persistence switch is enabled, data records from the native CODESYS Trace are received and cached through the lock-free circular buffer maintained by the record persistence manager; When the unlocked circular buffer reaches the buffer refresh threshold or timeout condition, a write task is generated through the record persistence manager. The write task is received through the global write manager, the write task is queued according to the priority of the task type, and dynamically allocated to the idle file writer according to the performance optimization parameters and system load. The actual disk write is performed by the file writer, wherein the file writer adaptively selects either a memory-mapped mode or a conventional buffered mode for data persistence based on the write mode, system capabilities, and file size. The persistence coordinator, the record persistence manager, the global write manager, and the file writer are all integrated with the original CODESYS Trace functionality through an extension mechanism without modifying the original CODESYS Trace code.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the method as described in claim 8.

10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method as described in claim 8.