Task processing method, system, device, medium and program
Patent Information
- Application Number
- CN202611472365.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-21
- Publication Date
- 2026-10-27
AI Technical Summary
[0004]本申请提供一种任务处理方法、系统、设备、介质及程序,以解决相关技术中因仅扣除中断执行时长而导致高优先级执行单元的抢占时间仍混入测时结果、因对不同抢占来源分别统计而导致嵌套或重叠抢占区间被重复扣除、以及因全任务常驻统计而导致系统开销大等问题
本申请实施例通过控制接口在线选定单一目标任务,在目标任务最外层入口开启有效测量窗口并清零共享干扰对象,使测量窗口与目标任务的执行实例严格绑定,避免了跨样本残留污染;通过将调度优先级高于目标任务的高优先级执行单元和中断服务例程统一映射至同一共享干扰对象,由共享嵌套计数值记录抢占嵌套深度,在计数值由0变为1时记录干扰起点、由1回落为0时累计干扰时长,使相互嵌套或重叠的多个抢占区间按照时间并集累计一次,避免了因对不同抢占来源分别统计而导致的重复扣除;通过在目标任务最外层出口统一结算,以结束时刻与起始时刻之差减去干扰累计时长得到净执行时间,使测时结果能够真实反映目标任务自身的执行时间,提高了测时准确性;通过单目标在线选择的测量方式避免了全任务常驻统计带来的系统开销。
Smart Images

Figure CN122985646A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of embedded software testing technology, and in particular to a task processing method, system, device, medium and program. Background Technology
[0002] In preemptive real-time operating systems (such as AUTOSAR OS), different tasks have different priorities, and low-priority tasks can be preempted multiple times by higher-priority execution units or interrupt service routines during operation.
[0003] In related technologies, the timing method records the time at the task entry and exit points respectively, and takes the difference between the two as the execution time. This method can obtain relatively accurate results in a non-preemptive environment. However, in a preemptive environment, the recorded time span includes multiple time segments occupied by other execution units, which leads to repeated calculation of preemption interference time and distorted measurement results. Based on inaccurate task load, CPU resources may be incorrectly allocated, resulting in performance imbalance of other tasks or the overall system. Summary of the Invention
[0004] This application provides a task processing method, system, device, medium, and program to solve problems in related technologies, such as the preemption time of high-priority execution units still being mixed into the timing results due to only deducting the interruption execution time, the repeated deduction of nested or overlapping preemption intervals due to separate statistics for different preemption sources, and the large system overhead caused by the constant statistics of all tasks.
[0005] The first aspect of this application provides a task processing method, comprising the following steps: acquiring a target task selected online through a control interface; opening a valid measurement window at the outermost entry point of the target task, initializing a shared interference object and recording the start time of the target task, wherein the shared interference object includes an interference start point, a cumulative interference duration, and a shared nesting count value; within the valid measurement window, for multiple preemption events that preempt the target task on the processor core where the target task resides, updating the shared nesting count value in response to the start or end of the preemption event; updating the interference start point and the cumulative interference duration according to the jump state of the shared nesting count value to update the shared interference object, and determining the cumulative interference duration according to the shared interference object, wherein the preemption event includes a high-priority execution unit and an interrupt service routine; when the outermost exit point of the target task is detected, closing the valid measurement window, reading the end time of the target task, and calculating the net execution time of the target task after removing preemption interference based on the start time, end time, and cumulative interference duration.
[0006] Optionally, updating the shared interference object based on the jump state of the shared nested count value further includes: recording the interference start point when the jump state of the shared nested count value jumps from a first value to a non-first value at the start of any preemption event; updating the cumulative interference duration based on the interference start point and the current time when the jump state of the shared nested count value jumps from a non-first value to a first value at the end of any preemption event; wherein, for jumps of other preemption events triggered between the start and end of any preemption event, only the shared nested count value is updated.
[0007] Optionally, before updating the shared interference object based on multiple preemption events, the process includes: obtaining a priority comparison result between the multiple preemption events and the target task; if the priority of the preemption event is higher than that of the target task, then updating the shared interference object.
[0008] Optionally, the priority comparison result is determined based on at least one of the following: operating system scheduling configuration, priority mapping table, or priority determination function.
[0009] Optionally, after acquiring the target task selected online through the control interface, the process includes: when switching target tasks online, entering the critical protection region, disabling the valid measurement window corresponding to the previous target task, and marking the measurement samples corresponding to the previous target task that have not yet been completed at the time of switching as invalid; clearing the statistical objects and shared interference objects of the current target task, and updating the measurement generation identifier of the target task; exiting the critical protection region, and taking effect from the next complete outermost instance of the current target task; wherein, when performing the interference start operation or interference end operation within the valid measurement window, the shared interference object is updated when the measurement generation identifier of the current measurement target task matches the measurement generation identifier of the measurement event.
[0010] Optionally, a target task statistical object corresponding to the target task and a shared interference object used by multiple preemption sources are set. The target task statistical object includes: target task outer layer depth, target task start time, net execution time, sample validity flag, and measurement generation identifier. The shared interference object includes: a flag indicating the status of the valid measurement window, interference start point, interference cumulative duration, and shared nesting count value. The target task outer layer depth and the shared nesting count value are independent of each other. The sample validity flag is used to indicate the validity of the measurement sample of the current target task, and the measurement generation identifier is used to distinguish different measurement batches.
[0011] Optionally, when an abnormal shared nested count value is detected, the shared interference object is reset and the measurement sample of the current target task is marked as invalid.
[0012] Optionally, when the interference interval recorded by the shared interference object crosses the boundary of the effective measurement window, the termination time of the interference interval is truncated to the closing time of the effective measurement window.
[0013] Optionally, after each valid measurement is completed, the minimum execution time of the target task is updated based on the minimum of the historical effective net execution time, the maximum execution time of the target task is updated based on the maximum of the historical effective net execution time, and the average execution time of the target task is updated based on the historical effective net execution time and the total number of valid samples.
[0014] Optionally, the start time, end time, and interference origin are obtained by a monotonically increasing counter; the fixed time of a single counter read is obtained by executing multiple times through an empty measurement path to obtain a calibration value, which represents the fixed time required for a single time measurement call and the corresponding counter read operation; when calculating the net execution time of the target task, the corresponding calibration value is deducted according to the number of counter reads executed within the effective measurement window; when an abnormal timestamp order or a difference exceeding the preset range is detected, exception handling is triggered, and the current sample is marked as invalid.
[0015] Optionally, the task processing method further includes: determining the mapping relationship between task functions and task identifiers through an automatic instrumentation program; deploying entry timing calls at the entry point of the target task and corresponding exit timing calls at each exit point of the target task according to the mapping relationship between task functions and task identifiers, wherein the entry timing calls and exit timing calls have the same task identifier and the same measurement generation identifier; if the automatic instrumentation program identifies an existing timing call marker, it will not deploy the entry timing calls and exit timing calls again; after detecting that the automatic instrumentation program has completed deployment, verifying the number of entry timing calls and the number of exit timing calls of the target task, as well as the task identifier of each timing call, if the number or any of the task identifiers does not match, an error warning is generated.
[0016] A second aspect of this application provides a task processing system, including: a target task selection unit, configured to select a target task online via a control interface and output the target task's identifier, current measurement generation identifier, and target task window status; and a target task timing unit, configured to receive the target task's identifier and current measurement generation identifier, clear the target task statistical object and shared interference object at the outermost entry point of the target task and record the start time, and record the end time at the outermost exit point of the target task, wherein the target task statistical object includes at least the target task's identifier, current measurement generation identifier, and sample validity flag, and the shared interference object includes... The system includes an effective measurement window flag, interference start point, cumulative interference duration, and shared nesting count value. The shared interference statistics unit includes a shared interference object and a shared nesting count value, which are read and written by the high-priority execution unit and interrupt service routine that actually preempt the target task on the processor core. The update operation on the shared interference object is performed only when the generational identifier carried by the event that triggers the interference update is consistent with the current measurement generational identifier. The settlement unit is used to calculate the net execution time of the target task at the outermost exit of the target task based on the start and end times recorded by the target task timing unit and the cumulative interference duration of the shared interference statistics unit.
[0017] A third aspect of this application 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 program to perform a task processing method as described in the above embodiments.
[0018] A fourth aspect of this application provides a computer-readable storage medium having a computer program stored thereon, which is executed by a processor to perform the task processing method as described in the above embodiments.
[0019] A fifth aspect of this application provides a computer program product, including a computer program or instructions, which, when executed, implement the task processing method as described in the above embodiments.
[0020] Therefore, this application has at least the following beneficial effects: This application embodiment selects a single target task online through a control interface, opens an effective measurement window at the outermost entry point of the target task and clears the shared interference object, so that the measurement window is strictly bound to the execution instance of the target task, avoiding cross-sample residual pollution; by uniformly mapping high-priority execution units and interrupt service routines with scheduling priorities higher than the target task to the same shared interference object, the preemption nesting depth is recorded by the shared nesting count value, the interference start point is recorded when the count value changes from 0 to 1, and the cumulative interference duration is recorded when it falls back from 1 to 0, so that multiple nested or overlapping preemption intervals are accumulated once according to the union of time, avoiding duplicate deductions caused by separately counting different preemption sources; by uniformly settling at the outermost exit of the target task, the net execution time is obtained by subtracting the cumulative interference duration from the difference between the end time and the start time, so that the timing result can truly reflect the execution time of the target task itself, improving the timing accuracy; the measurement method of online selection of a single target avoids the system overhead caused by the constant statistics of the entire task.
[0021] Additional aspects and advantages of this application will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of this application. Attached Figure Description
[0022] The above and / or additional aspects and advantages of this application will become apparent and readily understood from the following description of the embodiments taken in conjunction with the accompanying drawings, wherein: Figure 1 This is a flowchart of a task processing method provided according to an embodiment of this application; Figure 2 This is a timing diagram for calculating the net execution time when the target task is preempted multiple times according to the embodiments of this application; Figure 3 This is a block diagram of a task processing system provided according to an embodiment of this application; Figure 4 This is a schematic diagram of the structural interaction of a task processing system according to an embodiment of this application; Figure 5 This is a schematic diagram of the structure of an electronic device provided according to an embodiment of this application. Detailed Implementation
[0023] The embodiments of this application are described in detail below. Examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and intended to explain this application, and should not be construed as limiting this application.
[0024] In preemptive real-time operating systems (such as AUTOSAR OS), different tasks have different priorities, and low-priority tasks can be preempted multiple times by higher-priority execution units or interrupt service routines during execution. Traditional timing methods record the time at the task entry and exit points, and use the difference between the two as the execution time. This method can obtain relatively accurate results in non-preemptive environments, but in preemptive environments, the recorded time span includes multiple time segments occupied by other execution units, making it difficult to accurately reflect the net execution time of the target task itself.
[0025] In related technologies, task entry and exit times are recorded at task switching hooks, and interrupt execution duration is accumulated at the entry and exit points of interrupt service routines to deduct the interrupt duration from the apparent task time. While this method can deduct the time occupied by interrupt service routines, it does not include the preemption time of high-priority execution units in the deduction scope. This results in the preemption time of high-priority execution units still being mixed in with the timing results, leading to an inflated measured execution time. Furthermore, when multiple preemption intervals are nested or overlapped, this method accumulates the time for each interrupt interval separately, without merging the nested or overlapping parts into a time union. This causes the same time period to be repeatedly included in the interference accumulation duration, further affecting the measurement accuracy.
[0026] Another approach employs different timing methods—stop timing, resume timing, or independent timing—to handle different preemption sources. While this method achieves some measurement effectiveness in scenarios with a single preemption source, it fails when multiple preemption sources are nested or overlapping. Because the start and end operations of interference from different sources are independent, it's impossible to merge nested or overlapping preemption intervals into a time union. Separate statistics result in duplicate deductions for the same time period, leading to underestimation of the measurement results. Furthermore, this method is designed for continuous monitoring of all tasks, requiring unified management of execution time for all tasks, resulting in significant system overhead and making it unsuitable for scenarios requiring precise online measurement of a single target task.
[0027] In addition, other related technologies focus on global CPU (Central Processing Unit) load statistics or instrumentation to record jitter information. Global CPU load statistics focus on the overall system resource utilization, and its timing results include the execution time of all tasks and interrupts, making it impossible to obtain the net execution time of a single target task. Instrumentation to record jitter information focuses on task switching latency and jitter analysis, and its timing results also include the preemption time of high-priority execution units and interrupt service routines, leading to an inflated measured execution time. Furthermore, this approach lacks a unified deduction mechanism for nested or overlapping preemption intervals.
[0028] In summary, the existing technology lacks a timing scheme that can select a single target task online, unify multiple preemption sources into the same shared interference object, and uniformly deduct nested or overlapping preemption intervals by time union.
[0029] To address the aforementioned issues, this application adopts the following technical solution: A single target task is selected online, with multiple preemption sources sharing the same interference statistics object. A time union of preemption intervals is formed through a shared nested counter. The time is reset at the outermost entry point and settled at the outermost exit point of the target task. The control interface selects a current target task; a valid measurement window is opened at the outermost entry point of this task, resetting the shared interference cumulative time, interference start point, and shared nested counter, and recording the task start point; within this window, all eligible high-priority tasks and interrupt service routines call the same set of interference start and interference end operations; the window is closed at the outermost exit point of the target task, and the net execution time is settled uniformly. When switching target tasks online, the original measurement window is first closed, and incomplete samples are marked as invalid. Then, the measurement status of the new target task is cleared, ensuring that the switch takes effect from the next complete instance of the new target task.
[0030] The task processing methods, systems, devices, media, and programs of this application are described below with reference to the accompanying drawings.
[0031] Specifically, Figure 1 This is a flowchart of a task processing method provided according to an embodiment of this application.
[0032] like Figure 1 As shown, the task processing method includes the following steps: In step S101, the target task selected online through the control interface is obtained.
[0033] The control interface refers to the interface used by the external control system to measure the behavior of the system. This interface must at least provide target task selection and measurement result reading functions. The target task refers to the task selected online through the control interface, which currently has a net execution time to be measured.
[0034] It is understood that the embodiments of this application select a single target task online through the control interface, rather than performing permanent statistics on all tasks in the system. This avoids the system overhead caused by full task statistics. The online selection method allows the measurement target to be switched as needed without modifying the code or recompiling, thus improving measurement flexibility.
[0035] It should be noted that online selection in this application refers to dynamically selecting the target task during system operation without stopping the system or recompiling and deploying the code.
[0036] Specifically, the control interface provides a target task switching function. External tools select the current task to be tested by calling this function and passing in the target task identifier. After selection, the system records the current target task identifier and only responds to the test calls corresponding to that task identifier during subsequent testing. Test calls for non-target tasks are ignored.
[0037] In some embodiments of this application, the system maintains a current target task identifier variable. When an external tool passes in the target task identifier through the control interface, the system updates the variable to the passed-in value. Subsequent timing calls at task entry and exit points first compare the identifier of the currently executing task with the variable value; timing logic is only executed if they match. Timing calls for non-target tasks return directly without recording any time information.
[0038] It should be noted that there is a one-to-one mapping between target task identifiers and task functions. This mapping can be established by an automatic instrumentation program during the deployment phase. The instrumentation program scans the task definitions in the code and, based on the mapping between task functions and task identifiers, deploys time-test calls at the task entry and exit points. The mapping table can be implemented using data structures such as enumeration types, arrays, or hash tables. For example, in one embodiment of this application, task identifiers are defined using an enumeration type, and the system arranges the task identifiers from high to low according to known scheduling priorities.
[0039] For example, in an AUTOSAR OS-based system, there are multiple periodic tasks, including a 0.5ms task, a 1ms task, an IMU task, a 5ms ASIL task, a 5ms QM task, a 25ms task, and an EEPROM task. When it is necessary to measure the net execution time of the 25ms task, an external tool passes the identifier corresponding to the 25ms task through the control interface, and the system updates the current target task identifier to that value. Afterward, timing calls only execute recording and calculation logic at the entry and exit points of the 25ms task; timing calls for other tasks return directly.
[0040] In this embodiment, after obtaining the target task selected online through the control interface, the process includes: when switching target tasks online, entering a critical protection region, disabling the valid measurement window corresponding to the previous target task, and marking the measurement samples corresponding to the previous target task that have not yet been completed at the time of switching as invalid; clearing the statistical objects and shared interference objects of the current target task, and updating the measurement generation identifier of the target task; exiting the critical protection region, and taking effect from the next complete outermost instance of the current target task; wherein, when performing an interference start operation or interference end operation within the valid measurement window, the shared interference object is updated when the measurement generation identifier of the current measurement target task matches the measurement generation identifier of the measurement event.
[0041] The critical protection region refers to the code execution interval protected by mechanisms such as interrupt disabling, scheduling locks, or atomic operations. It ensures the consistency of shared resources during switching and prevents inconsistencies caused by preemption events. The effective measurement window is a time interval controlled by the outermost depth of the target task. Preemption event timing calls take effect when the window is open and are ignored when it is closed. This window is only open at the outermost entry point and closed at the outermost exit point of the target task. The statistical object refers to the data structure corresponding to the target task, including at least the outermost depth of the target task, the target task start time, net execution time, sample validity flag, and measurement generation identifier. The shared interference object refers to the interference statistics data structure jointly read and written by multiple preemption sources, including at least the effective measurement window flag, interference start point, cumulative interference duration, and shared nested count value. The measurement generation identifier is an identifier value used to distinguish different measurement batches. It is generated by the control interface when selecting or switching target tasks, and the new value generated each time is different from the current value.
[0042] It is understood that the embodiments of this application ensure the atomicity of the switching operation through the critical protection region, prevent old preemption events from contaminating new samples through the measurement generation identifier matching mechanism, and enable the target task after the switch to take effect from the next complete outermost instance, thus ensuring the isolation of measurement samples before and after the switch and the accuracy of measurement results after the switch.
[0043] Specifically, when an external tool passes in a new target task identifier through the control interface, the system enters a critical protection region during online target task switching. The critical protection region ensures the consistency of shared resources during the switching process, preventing interruptions caused by preemption events and resulting in inconsistent states. The system disables the current target task's corresponding valid measurement window, preventing it from receiving new preemption event records, and marks any incomplete measurement samples at the time of switching as invalid, excluding them from statistical value updates. The system clears the current target task's statistical objects and shared interference objects, updates the target task's measurement generation identifier (i.e., generates a new measurement generation identifier and assigns it to the switched target task), and exits the critical protection region. The switched target task takes effect from the next complete outermost instance; the currently executing task instance is discarded and does not participate in statistics. The new target task begins measurement from the next outermost entry point, ensuring that each measurement sample corresponds to a complete execution instance.
[0044] In some embodiments of this application, the system maintains a global measurement generational identifier variable, initially set to 0. When a target task is selected for the first time, this variable is incremented by 1 and assigned to the target task. Each time a switch occurs, this variable is incremented by 1 and assigned to the new target task. During preemption events, when recording interference, the measurement generational identifier of the current target task is read and recorded in the interference state corresponding to the preemption event. During the interference termination operation, the generational identifier recorded in the preemption event is compared with the generational identifier of the current target task; only if they match is the cumulative interference duration incremented.
[0045] For example, suppose the system is currently measuring a 25ms task with a generational identifier of 1. During the execution of the 25ms task, an external tool switches the target task to a 5ms QM task via a control interface. The system enters a critical protection region, disables the valid measurement window for the 25ms task, marks any incomplete measurement samples from the current 25ms task as invalid, clears the statistical objects and shared interference objects for the 25ms task, updates the generational identifier to 2 and assigns it to the 5ms QM task, and then exits the critical protection region. If a preemptive event was triggered during the execution of the 25ms task, this preemptive event recorded a generational identifier of 1 at the start of its interference. After the switch, this preemptive event ends, and an interference termination operation is performed. At this point, its generational identifier is 1, while the current target task's generational identifier is 2. Since they are inconsistent, the termination operation of this preemptive event does not update the shared interference objects. The measurement results of the 5ms QM task are unaffected by this old preemptive event. When the next complete instance of the 5ms QMTASK begins execution, its entry timing call detects that the current task identifier matches the target task identifier and that the current outer layer depth is 0. Therefore, it opens a valid measurement window, records the start time, and begins measurement from a clean state. The timing call at the subsequent exit of the 25ms TASK detects that the task identifier does not match the current target task identifier and returns directly without executing the settlement logic.
[0046] In this embodiment, the method further includes: determining the mapping relationship between task functions and task identifiers through an automatic instrumentation program; deploying an entry timing call at the entry point of the target task and deploying corresponding exit timing calls at each exit point of the target task according to the mapping relationship between the task functions and task identifiers, wherein the entry timing call and the exit timing call have the same task identifier and the same measurement generation identifier; if the automatic instrumentation program identifies an existing timing call marker, it will not deploy the entry timing call and the exit timing call again; after detecting that the automatic instrumentation program has completed deployment, verifying the number of entry timing calls and the number of exit timing calls of the target task, as well as the task identifier of each timing call, if the number or any of the task identifiers does not match, an error warning is generated.
[0047] The automatic instrumentation program refers to a script tool used to automatically deploy timing calls in the code. The mapping between task identifiers and task functions is established by the automatic instrumentation program through code scanning. Entry timing calls are timing calls deployed at the entry point of the target task, while exit timing calls are timing calls deployed at each exit point of the target task. Entry and exit timing calls have the same task identifier and the same measurement generation identifier to ensure that entry and exit pairs belong to the same task and the same measurement batch. Timing call markers are identification information recorded by the automatic instrumentation program when deploying timing calls, used to identify whether a location has already been deployed.
[0048] It is understood that the embodiments of this application deploy timing calls in pairs at the entry and exit points of the target task through an automatic instrumentation program, ensuring that the timing logic covers every exit path of the task; by identifying existing timing call markers, duplicate deployment is avoided, ensuring the idempotency of the instrumentation process; by verifying the number of entry calls, the number of exit calls, and the task identifier, instrumentation omissions or errors can be detected in a timely manner, ensuring the integrity and accuracy of the timing data.
[0049] It should be noted that the timing call in this application (e.g., CpuLoad_Task in this embodiment) is only an exemplary implementation. In other embodiments of this application, the timing call can also be implemented by calling the native time measurement interface provided by the operating system, or by using an operating system hook function, or by directly reading the hardware timer register. All of the above-mentioned different implementation methods are reasonable variations of the technical solution of this application in the art and do not constitute a limitation on the scope of protection of this application.
[0050] In some embodiments of this application, the automatic instrumentation program scans the code using regular expression matching or abstract syntax tree analysis to identify task definitions and exit points. For compilation environments that do not support abstract syntax tree analysis, text pattern matching can be used to locate task function boundaries and exit statements by identifying keywords and code structure. Regardless of the scanning method used, the automatic instrumentation program must ensure that the entry timing call and the exit timing call have the same task identifier and the same measurement generation identifier.
[0051] For example, suppose the target task is a 25ms task with the task identifier CPULOAD_TK_P25. The automatic instrumentation program scans the code to determine the function entry points and all exit points of the 25ms task, including the return statements at the end of functions and the call locations of TerminateTask. The automatic instrumentation program deploys the entry test call CpuLoad_Task(TRUE, CPULOAD_TK_P25) at the function entry point and deploys the exit test call CpuLoad_Task(FALSE, CPULOAD_TK_P25) before each exit point. After deployment, the automatic instrumentation program verifies that the number of entry test calls is 1, the number of exit test calls is 3, and the task identifier of each test call is CPULOAD_TK_P25, then the verification passes. If, after instrumentation, it is found that the number of entry calls is 1 and the number of exit calls is 2, but the task actually has 3 exit points, it indicates an omission. The automatic instrumentation program generates an error warning to prompt the developers to check.
[0052] For example, in another scenario, the automatic instrumentation program instrumentes a task, and each exit point deploys a corresponding exit timing call. However, the task identifier of one of the exit timing calls is incorrectly set to another task identifier. During verification, it is found that the task identifier of this exit timing call is inconsistent with the task identifier of other timing calls. The automatic instrumentation program generates an error warning, indicating that there is a problem with the task identifier mismatch, to avoid the entry and exit pairing error causing invalid timing data.
[0053] In other embodiments of this application, in addition to verifying whether the number of entry and exit points matches and whether the task identifiers are consistent, the automatic instrumentation program can further verify whether each exit position has a corresponding exit time-test call overridden. If an exit path is found to be uninstrumented, an error warning containing the exit position information is generated for developers to confirm whether additional instrumentation is needed or whether the exit position should be modified to a unified exit.
[0054] In step S102, an effective measurement window is opened at the outermost entry point of the target task, the shared interference object is initialized, and the start time of the target task is recorded. The shared interference object includes the interference start point, the cumulative interference duration, and the shared nesting count value.
[0055] The outermost entry point refers to the moment when the outermost depth of the target task changes from 0 to 1, i.e., the very beginning of this execution instance of the target task. The outermost exit point refers to the moment when the outermost depth of the target task changes from 1 to 0, i.e., the final exit point of this execution instance of the target task. The interference start point refers to the time point at which the current preemption interval begins. The cumulative interference duration refers to the cumulative duration of the union of the times of all preemption intervals within the effective measurement window. The shared nesting count value refers to the count of the current preemption nesting level, used to merge nested or overlapping preemption intervals into a time union.
[0056] It is understood that the embodiments of this application control the opening and closing of the effective measurement window by controlling the outer depth of the target task, and clear the shared interference object only when the depth changes from 0 to 1, so that the effective measurement window strictly corresponds to the complete execution instance of the target task, avoiding cross-contamination of measurement data between different execution instances.
[0057] Specifically, when the target task entry point is executed, the system first checks if the identifier of the currently executing task matches the identifier of the current target task. If they match, it further checks the outer layer depth of the target task. The outer layer depth of the target task records the number of layers the target task has been called; it increments by 1 when the entry point is called and decrements by 1 when the exit point is executed. When the outer layer depth of the target task changes from 0 to 1, it is determined to be the outermost entry point, and the following operations are performed: open a valid measurement window to enable the timing calls of subsequent preemption events; clear the shared interference object, that is, clear the interference start point, the cumulative interference duration, and the shared nesting count value to zero; read the current counter value as the start time of the target task. When the outer layer depth of the target task changes from 1 to 0, it is determined to be the outermost exit point, and the settlement operation is performed.
[0058] If the target task involves nested calls during execution, and the outer depth changes from 1 to 2 and then back to 1, the operations of opening the window, clearing the shared interference object, and recording the start time are not repeated. These operations are only performed when the depth changes from 0 to 1, ensuring that the start time and interference state of each measurement sample are clean.
[0059] In this embodiment, a target task statistical object corresponding to the target task and a shared interference object used by multiple preemption sources are set up. The target task statistical object includes: target task outer layer depth, target task start time, net execution time, sample validity flag, and measurement generation identifier. The shared interference object includes: a flag indicating the status of the valid measurement window, interference start point, interference cumulative duration, and shared nesting count value. The target task outer layer depth and the shared nesting count value are independent of each other. The sample validity flag is used to indicate the validity of the measurement sample of the current target task, and the measurement generation identifier is used to distinguish different measurement batches.
[0060] Among them, the target task statistical object refers to the data structure corresponding to the current target task; the outer layer depth of the target task is used to record the number of layers that the target task is called. It is incremented by 1 when entering and decremented by 1 when exiting. The effective measurement window is only opened when the depth changes from 0 to 1, and closed and the settlement is executed when it changes from 1 to 0; the target task start time is used to record the counter value at the outermost entry point; the net execution time is used to store the measurement results after the current sample is settled.
[0061] The shared interference object refers to the interference statistics data structure that is read and written by multiple preemption sources; the effective measurement window flag is used to indicate whether the current measurement window is open; the interference start point is used to record the time point at which the current preemption interval begins, which is determined by the current moment when the shared nested count value changes from 0 to 1.
[0062] It is understood that the embodiments of this application decouple the execution state of the target task from the occurrence state of the preemption event by designing the outer depth of the target task and the shared nesting count value to be independent of each other, thereby avoiding the confusion caused by the mixed management of the two states and improving the maintainability and reliability of the system.
[0063] It should be noted that the target task outer depth and the shared nesting count are independent of each other. The target task outer depth is controlled only by the target task's own entry and exit points, and is used to manage the opening and closing of the valid measurement window. The shared nesting count is controlled only by the start and end of the preemption event, and is used to manage the statistics of nested interference. Both update independently and do not affect each other, but they coordinate through the valid measurement window flag: the update of the shared nesting count takes effect when the window is open, and is ignored when the window is closed.
[0064] In step S103, within the effective measurement window, for multiple preemption events that preempt the target task on the processor core where the target task resides, the shared nesting count value is updated in response to the start or end of the preemption event; the interference start point and interference cumulative duration are updated according to the jump state of the shared nesting count value to update the shared interference object; the interference cumulative duration is determined according to the shared interference object, wherein the preemption event includes high-priority execution units and interrupt service routines.
[0065] In this context, a preemption event refers to the interruption of the target task's execution by a high-priority execution unit or interrupt service routine with a scheduling priority higher than the target task. A high-priority execution unit is an execution entity with a scheduling priority higher than the current target task. In the AUTOSAR operating system, each task is assigned a different scheduling priority, with higher values indicating higher priority (the specific meaning of the value depends on the operating system implementation). An interrupt service routine is a handler triggered by a hardware interrupt. In embedded real-time systems, interrupts have high hardware-level priority and can immediately interrupt any currently executing task (including high-priority execution units), switching to the interrupt service routine. The interrupt service routine has a higher priority than all task-level priorities.
[0066] It is understood that the embodiments of this application record the preemption nesting depth by sharing the nesting count value, and record the interference start point when 0 becomes 1 and the cumulative interference duration when 1 falls back to 0, so that all nested or overlapping preemption intervals are accumulated only once according to the union of time, avoiding duplicate deductions caused by separately counting different preemption sources, and improving the accuracy of timing.
[0067] Specifically, when any preemption event begins, an interference start operation is performed: the shared nesting count is incremented by 1. If the shared nesting count was 0 before incrementing, meaning the jump state changed from 0 to 1, then the current moment is recorded as the interference start point. If the shared nesting count was not 0 before incrementing, meaning the jump state changed from non-zero to non-zero, then the interference start point is not recorded, and only the shared nesting count is incremented.
[0068] When any preemption event ends, an interference termination operation is performed: the shared nesting count is decremented by 1. If the shared nesting count becomes 0 after decrementing, i.e., the jump state changes from 1 to 0, the duration of this preemption interval is calculated based on the current time and the interference start point, and this duration is added to the cumulative interference duration. If the shared nesting count becomes non-zero after decrementing, i.e., the jump state changes from non-zero to non-zero, the interference duration is not added, only the shared nesting count is decremented.
[0069] Based on the above logic, when multiple preemption sources are nested or overlapped, the start of interference is recorded when the shared nesting count changes from 0 to 1. Inner nested preemption events only increment the count, without repeatedly recording the interference start point. As the innermost preemption event exits at each level, and the shared nesting count drops back from 1 to 0, the difference between the current time and the earliest recorded interference start point is added to the cumulative interference duration. Thus, all nested or overlapping preemption intervals are merged into a time union, the duration of which equals the total duration from the start of the earliest preemption to the end of the latest preemption, with no repeated accumulation of inner nested intervals.
[0070] For example, such as Figure 2As shown, assume the apparent time span of a 25ms task is 100 timing units. During the execution of the target task, there are three preemptive intervals: the first preemptive interval is from time 20 to time 50, the second preemptive interval is from time 35 to time 45, and the third preemptive interval is from time 40 to time 60.
[0071] At the start of the first preemption interval, the shared nesting count value changes from 0 to 1, and the starting point of the interference is recorded at time 20. At the start of the second preemption interval (time 35), the shared nesting count value is currently 1 (the first preemption interval has not yet ended), and the count value changes from 1 to 2, without recording a new starting point of interference. At the start of the third preemption interval (time 40), the shared nesting count value is currently 2, and the count value changes from 2 to 3, without recording a new starting point of interference.
[0072] The second preemption interval ends first (time 45), the shared nesting count value changes from 3 to 2, and the interference duration is not accumulated at this time. The first preemption interval then ends (time 50), the shared nesting count value changes from 2 to 1, and the interference duration is not accumulated at this time. The third preemption interval ends last (time 60), the shared nesting count value changes from 1 to 0, and the difference between the current time 60 and the interference start point 20 (40 timing units) is added to the cumulative interference duration.
[0073] In this embodiment, updating the shared interference object based on the jump state of the shared nested count value further includes: recording the interference start point when the jump state of the shared nested count value jumps from a first value to a non-first value in response to the start of any preemption event; updating the cumulative interference duration based on the interference start point and the current time when the jump state of the shared nested count value jumps from a non-first value to a first value in response to the end of any preemption event; wherein, for the jumps of other preemption events caused between the start and end of any preemption event, only the shared nested count value is updated.
[0074] Here, the first value refers to the initial state value of the shared nested counter, indicating that there are currently no preemptive events. The first value can be 0. A non-first value refers to any positive integer other than the first value, indicating that at least one preemptive event is currently executing. The jump state of the shared nested counter refers to the change in the counter value before and after the update, including jumping from the first value to a non-first value, from a non-first value to the first value, and from a non-first value to a non-first value, for example, jumping from 1 to 2.
[0075] It is understood that in this embodiment of the application, the interference start point is recorded when the shared nested count value jumps from the first value to a non-first value, and the interference duration is accumulated when it jumps from a non-first value to the first value. The jump between non-first values only updates the count value, thereby realizing the time combination and set-up of nested or overlapping preemptive intervals and avoiding duplicate deduction.
[0076] It should be noted that the mechanism for determining the jump state between the first value and non-first values has the same technical meaning as the mechanism for determining the jump state between 0 and non-zero values in this application.
[0077] Specifically, when a preemption event begins, the system performs an interference start operation, increments the shared nesting counter by 1, and determines whether to record the interference start point based on the jump state of the counter: if the counter jumps from the first value to a non-first value, it indicates that the current state changes from no preemption to preemption, and the current moment is recorded as the interference start point; if the counter jumps from a non-first value to a non-first value, such as from 1 to 2, it indicates that a preemption event already exists, and the newly occurring preemption event is a nested entry, and the interference start point is not recorded, only the counter value is updated. When a preemption event ends, the system performs an interference termination operation, decrements the shared nested counter by 1, and determines whether to update the cumulative interference duration based on the counter's transition status: If the counter jumps from a non-first value to the first value, indicating a change from preemption to non-preemption, the duration of the current preemption interval is calculated based on the current time and the interference start point, and this duration is added to the cumulative interference duration; if the counter jumps from a non-first value to another non-first value, such as from 2 to 1, indicating that other preemption events still exist and the nested exit has not yet fully ended, the interference duration is not added, only the counter is updated. For any preemption event transitions between the start and end of a preemption event, i.e., changes in the counter value within a non-first value range at the start or end of a nested preemption event, only the shared nested counter is updated; the recording of the interference start point and the accumulation of the cumulative interference duration are not performed.
[0078] In this embodiment of the application, before updating the shared interference object based on multiple preemption events, the process includes: obtaining a priority comparison result between the multiple preemption events and the target task; if the priority of the preemption event is higher than that of the target task, then the shared interference object is updated.
[0079] The priority comparison result is determined based on at least one of the following: operating system scheduling configuration, priority mapping table, or priority determination function.
[0080] It is understood that this application embodiment, by performing priority determination before updating the shared interference object, ensures that only execution units and interrupt service routines with scheduling priorities higher than the target task are included in the interference time, thus avoiding interference from low-priority or equal-priority tasks on the measurement results and improving the accuracy of net execution time measurement. Simultaneously, by providing three optional priority acquisition methods—operating system scheduling configuration, priority mapping table, and priority determination function—this application embodiment can adapt to different operating systems and priority configuration scenarios, improving the versatility and flexibility of the solution.
[0081] It should be noted that for priority determination methods based on operating system scheduling configurations, different operating systems may employ different priority encoding rules. In some operating systems, a larger value indicates higher priority; in others, a smaller value indicates higher priority. The system should perform the correct comparison based on the specific operating system's scheduling configuration. For priority determination methods using a priority mapping table, the mapping table can be established during system initialization or dynamically updated at runtime. The priority determination function can calculate priorities by considering multiple factors (such as task type, deadline, resource usage, etc.), making it suitable for scenarios with complex priority configurations.
[0082] In this embodiment, when an abnormal shared nesting count value is detected, the shared interference object is reset, and the measurement sample of the current target task is marked as invalid.
[0083] Among them, abnormal shared nested count value refers to a state in which the shared nested count value does not conform to the normal operation logic.
[0084] It is understood that the embodiments of this application, through the shared nested count value anomaly detection and reset mechanism, avoid invalid data pollution of statistical results caused by instrumentation mismatch, interrupted nesting anomaly, or code execution error, thus ensuring the validity of the timing data and the reliability of the statistical values.
[0085] It should be noted that abnormal situations involving the shared nesting count include, but are not limited to: the shared nesting count becoming negative after being decremented by 1 during the interference termination operation, indicating the existence of an unpaired interference termination operation; the shared nesting count exceeding the maximum nesting depth supported by the system after being incremented by 1 during the interference start operation; the shared nesting count remaining non-zero after the effective measurement window is closed, indicating the existence of an unclosed interference interval crossing the window boundary; and unexpected jumps in the shared nesting count, such as jumping directly from 3 to 0 without a gradual decrease. Resetting the shared interference object means clearing the interference start point, the cumulative interference duration, and the shared nesting count, and setting the effective measurement window flag to the closed state. After a sample is marked as invalid, it will not participate in the statistical value update, including not updating the minimum execution time, maximum execution time, and average execution time.
[0086] Specifically, during the interference start or end operation, the system performs a validity check after each update of the shared nesting count. If the shared nesting count is less than 0, greater than the maximum nesting depth supported by the system, or an unexpected jump occurs, the shared nesting count is determined to be abnormal. Upon determining an abnormality, the system immediately triggers the exception handling procedure, resets the shared interference object, and marks the measurement samples of the current target task as invalid. Invalid samples are skipped in subsequent statistical value updates and are not included in the calculation of the minimum, maximum, and average values.
[0087] In step S104, when the outermost exit of the target task is detected, the effective measurement window is closed, the end time of the target task is read, and the net execution time of the target task after removing preemptive interference is calculated based on the start time, end time, and cumulative interference duration. Specifically, when the interference interval recorded by the shared interference object crosses the boundary of the effective measurement window, the end time of the interference interval is truncated to the closing time of the effective measurement window.
[0088] The outermost exit refers to the moment when the outermost depth of the target task changes from 1 to 0, which is the final exit point of this execution instance of the target task.
[0089] It is understood that the embodiments of this application use unified settlement at the outermost exit of the target task to obtain the net execution time by subtracting the cumulative duration of interference from the difference between the end time and the start time, so that the timing results can truly reflect the execution time of the target task itself and avoid the impact of preemption interference on the timing results.
[0090] Specifically, when the target task exits, the system first checks if the identifier of the currently executing task matches the identifier of the current target task. If they match, the outer depth is decremented by 1. When the outer depth changes from 1 to 0, it is determined to be the outermost exit, and the following settlement operations are performed: First, the effective measurement window is closed, and the receipt of new preemption event records is stopped. After the window is closed, subsequent timing calls for preemption events return directly without updating the shared interference object; second, it checks if there are any unclosed interference intervals in the shared interference object, i.e., it checks if the shared nesting count value is 0. If the shared nesting count value is not 0, it indicates that there is an unclosed interference interval that crosses the boundary of the effective measurement window. The termination time of the interference interval is truncated to the closing time of the effective measurement window, i.e., the window closing time is used as the termination time of the interference interval. The duration of the interference interval is calculated based on the window closing time and the interference start point, and this duration is added to the cumulative interference duration; then, the current counter value is read as the end time; finally, based on the start time, end time, and cumulative interference duration, the net execution time of the target task is calculated according to the following formula: ,
[0091] in, Indicates the net execution time of the target task. Indicates the end time of the target task. Indicates the start time of the target task. To define the preemption interval formed by the i-th eligible high-priority task or interrupt service routine within the effective measurement window, m represents the total number of preemption intervals within the effective measurement window. Represents the union of the preempted intervals from the 1st to the mth. A measure of the duration of the union of time intervals.
[0092] For example, such as Figure 2 As shown, assume the apparent time span of a 25ms task is 100 timing units. During the execution of the target task, there are three preemptive intervals: the first preemptive interval is from time 20 to time 50, the second preemptive interval is from time 35 to time 45, and the third preemptive interval is from time 40 to time 60. The shared nested counter value merges the three preemptive intervals into a time union [20, 60], and the cumulative interference duration is 60 - 20 = 40 timing units. Therefore, the net execution time = 100 - 40 = 60 timing units. This application avoids duplicate deductions and obtains the correct net execution time through the time union mechanism.
[0093] In this embodiment, after each valid measurement is completed, the minimum execution time of the target task is updated based on the minimum value among the historical valid net execution times, the maximum execution time of the target task is updated based on the maximum value among the historical valid net execution times, and the average execution time of the target task is updated based on the historical valid net execution time and the total number of valid samples.
[0094] Among them, minimum execution time refers to the minimum net execution time among all valid samples, maximum execution time refers to the maximum net execution time among all valid samples, and average execution time refers to the arithmetic mean of the net execution times of all valid samples. The total number of valid samples refers to the cumulative number of all measurement samples marked as valid since the system started or the statistics were reset to zero.
[0095] Understandably, by updating the minimum execution time, maximum execution time, and average execution time after each effective measurement, statistical characteristics of the net execution time of the target task are provided. This makes it easier for developers to assess the range of execution time fluctuations, worst-case execution time, and typical execution time of the target task, providing data support for task scheduling optimization and timing verification.
[0096] It should be noted that the average execution time in this application is calculated using the arithmetic mean. In embodiments where only a smoothed time index is required, a recursive smoothing method can also be used to calculate the average, such as an exponentially weighted moving average. This application does not impose any restrictions on this method.
[0097] In some embodiments of this application, when switching target tasks via a control interface, the system can reset the current target task's statistical values, including resetting the minimum execution time, maximum execution time, average execution time, and total number of valid samples, so that the new target task accumulates statistical values from zero. In other embodiments, the system can also retain historical statistical values during switching for external tools to read and compare.
[0098] In this embodiment, the start time, end time, and interference start point are obtained by a monotonically increasing counter; the fixed time consumption of a single reading of the counter is obtained by executing multiple times through an empty measurement path to obtain a calibration value, which represents the fixed time consumption required for a single time measurement call and the corresponding counter reading operation; when calculating the net execution time of the target task, the corresponding calibration value is deducted according to the number of counter readings executed within the effective measurement window; when an abnormal timestamp order or a difference exceeding a preset range is detected, exception handling is triggered, and the current sample is marked as invalid.
[0099] It is understood that the embodiments of this application eliminate the influence of the timing operation itself on the measurement results by deducting the calibration value, thereby improving the accuracy of timing. By detecting abnormal timestamp order and differences exceeding the preset range, data anomalies during the timing process can be detected in a timely manner, avoiding invalid data from contaminating the statistical results.
[0100] In some embodiments of this application, the fixed time consumption for a single counter read is used to obtain a calibration value through multiple executions of an empty measurement path. An empty measurement path refers to a measurement path that does not contain any business logic code and only performs timing calls at the entry and exit points. By executing the timing process multiple times on this empty measurement path, the total time consumption obtained is the fixed time overhead required for a single timing call and the corresponding counter read operation; this fixed time overhead is the calibration value.
[0101] When calculating the net execution time of the target task, the corresponding calibration value is deducted based on the number of counter reads performed within the effective measurement window. Specifically, each counter read generates the time cost of a calibration value. The number of counter reads performed within the effective measurement window includes one read at the start time, one read at the end time, and one read each time an interference start point is recorded. The system multiplies the number of reads by the calibration value during settlement to obtain the total measurement time cost, and deducts this total measurement time cost from the net execution time.
[0102] In other embodiments of this application, when an abnormal timestamp order or a difference exceeding a preset range is detected, exception handling is triggered, and the current sample is marked as invalid. An abnormal timestamp order refers to a illogical sequence of timestamps, including but not limited to situations where the end time is earlier than the start time, or the interference start point is later than the current time. A difference exceeding a preset range means that the difference between two timestamps exceeds a pre-defined reasonable range, such as a net execution time greater than the maximum possible execution time of the target task (e.g., greater than the target task cycle), or a time difference between two adjacent counter reads exceeding the maximum counting range of the counter.
[0103] It should be noted that, in this embodiment, the timing reference is a monotonically increasing hardware counter or a system clock derived therefrom. The count width, overflow wraparound, count frequency conversion, and read overhead are all determined and calibrated in the specific implementation. In this embodiment, the count value is obtained by reading the STM (State Machine) register, and the debug pin or external reference output can be used for measurement verification. This application does not limit the specific clock address, specific MCU (Microcontroller Unit), or pin; other types of hardware counters or system clocks may also be used as the timing reference in other embodiments.
[0104] In some embodiments of this application, the system also provides a fault injection extension function. After enabling fault injection through the control interface, the system injects preset simulation data into the sensor sampling data to simulate the task operation state under high load or abnormal operating conditions, so as to verify the stability of the timing mechanism under complex load conditions. This fault injection extension is independent of the core logic of net execution time measurement.
[0105] According to the task processing method proposed in this application, a single target task is selected online through a control interface. An effective measurement window is opened at the outermost entry point of the target task, and the shared interference object is cleared. High-priority execution units and interrupt service routines with scheduling priorities higher than the target task are uniformly mapped to the same shared interference object. The preemption nesting depth is recorded by a shared nesting count value. The interference start point is recorded only when the shared nesting count value changes from 0 to 1, and the cumulative interference duration is recorded when it falls back to 0. Multiple nested or overlapping preemption intervals are accumulated once according to the union of their times, and a unified settlement is performed at the outermost exit point of the target task. The net execution time of the target task is obtained by subtracting the cumulative interference duration from the difference between the end time and the start time. This solves the technical problems in related technologies where nested or overlapping preemption intervals are repeatedly deducted due to separate statistics for different preemption sources, and the technical problems of high system overhead due to constant statistics across all tasks, thus improving the accuracy of the net execution time measurement of the target task.
[0106] Next, the task processing system proposed according to the embodiments of this application is described with reference to the accompanying drawings.
[0107] Figure 3 This is a block diagram of a task processing system provided according to an embodiment of this application.
[0108] like Figure 3 As shown, the task processing system 30 includes: a target task selection unit 301, a target task timing unit 302, a shared interference statistics unit 303, and a settlement unit 304.
[0109] The target task selection unit 301 is used to acquire the target task selected online through the control interface; the target task timing unit 302 is used to open an effective measurement window at the outermost entry point of the target task, initialize the shared interference object, and record the start time of the target task, wherein the shared interference object includes the interference start point, the cumulative interference duration, and the shared nesting count value; the shared interference statistics unit 303 is used to update the shared interference object based on multiple preemption events that preempt the target task on the processor core where the target task is located within the effective measurement window, and determine the cumulative interference duration based on the shared interference object, wherein the preemption events include high-priority execution units and interrupt service routines; the settlement unit 304 is used to close the effective measurement window when the outermost exit point of the target task is detected, read the end time of the target task, and calculate the net execution time of the target task after removing preemption interference based on the start time, end time, and cumulative interference duration.
[0110] like Figure 4 As shown, the interaction relationships between the units are as follows: The target task selection unit 301 receives the selection instruction from the external tool through the control interface and outputs the selected target task identifier to the target task timing unit 302. The target task timing unit 302, based on the target task identifier, opens a valid measurement window at the outermost entry point of the target task and notifies the shared interference statistics unit 303 to clear the shared interference object. During the validity period of the measurement window, the high-priority execution unit and the interrupt service routine, as preemption sources, write interference start information and interference end information to the shared interference statistics unit 303 through the same interface. The shared interference statistics unit 303 records the interference start point or accumulates the cumulative interference duration based on the jump status of the shared nested count value and outputs the cumulative interference duration to the settlement unit 304. When the target task timing unit 302 detects the outermost exit point of the target task, it outputs the end time to the settlement unit 304 and notifies the shared interference statistics unit 303 to close the valid measurement window. The settlement unit 304 calculates the net execution time based on the start time, end time, and cumulative interference duration. The entire system operates based on a unified monotonic timing benchmark to ensure consistency across timestamps.
[0111] According to the task processing method proposed in this application, a single target task is selected online through a control interface. An effective measurement window is opened at the outermost entry point of the target task, and the shared interference object is cleared. High-priority execution units and interrupt service routines with scheduling priorities higher than the target task are uniformly mapped to the same shared interference object. The preemption nesting depth is recorded by a shared nesting count value. The interference start point is recorded only when the shared nesting count value changes from 0 to 1, and the cumulative interference duration is recorded when it falls back to 0. Multiple nested or overlapping preemption intervals are accumulated once according to the union of their times, and a unified settlement is performed at the outermost exit point of the target task. The net execution time of the target task is obtained by subtracting the cumulative interference duration from the difference between the end time and the start time. This solves the technical problems in related technologies where nested or overlapping preemption intervals are repeatedly deducted due to separate statistics for different preemption sources, and the technical problems of high system overhead due to constant statistics across all tasks, thus improving the accuracy of the net execution time measurement of the target task.
[0112] The following is combined with Figure 4 The task processing method provided in one embodiment of this application will be described in detail below: Figure 4 This is a schematic diagram of the structural interaction of a task processing system provided according to an embodiment of this application.
[0113] like Figure 4 As shown, in this embodiment, the system runs on AUTOSAR OS, and the tasks include 0.5ms TASK, 1ms TASK, IMU TASK, 5ms ASIL TASK, 5ms QM TASK, 25ms TASK, and EEPROM TASK. The definition, core function, and actual usage of each interface involved in this application are shown in Table 1 below: Table 1 Interface Definition and Function Description
[0114] In this embodiment, the mapping relationship between task identifiers and task functions is implemented through an enumeration type. The system arranges the task identifiers from high to low according to the known scheduling priority. As shown in Table 2, the correspondence between the task identifier enumeration, the corresponding actual task, the priority value, the task definition position, and the current instrumentation status in this embodiment is as follows: Table 2 Task Priority and Staking Target Identification Table
[0115] In Table 2 above, the CpuLoad enumeration is arranged from high to low scheduling priority. A larger value indicates a higher priority. When the target task is a 25ms task, the 0.5ms task (priority 140), 1ms task (priority 120), IMU task (priority 100), 5ms ASIL task (priority 90), and 5ms QM task (priority 89) are all high-priority execution units with higher priorities than the target task. The EEPROM task (priority 40) has a lower priority than the 25ms task and therefore does not constitute a source of preemption interference. It should be noted that this application does not limit the enumeration values to represent priorities; in other embodiments, the operating system scheduler priority can be read or an explicit mapping table can be used to determine the priority relationship.
[0116] The following is combined with Figure 4 The interaction relationships between the modules shown are described in detail, and a specific implementation process of one embodiment of this application is described in detail.
[0117] (1) Pile driving deployment stage: In this embodiment, the automatic instrumentation program uses the CpuLoadTest.py script. This script scans the generated code for TASK() definitions and reads the mapping relationship between task functions and task identifiers. The script inserts a CpuLoad_Task(TRUE, taskId) call at the entry point of the target task and a CpuLoad_Task(FALSE, taskId) call before each unified exit point or before calling TerminateTask(). Exit points include return statements at the end of functions, calls to task termination functions, and other execution path endpoints that can cause the task to exit. The automatic instrumentation program is idempotent: it identifies existing paired markers during repeated executions without re-inserting them. After instrumentation, the automatic instrumentation program verifies whether the number of entry and exit calls for each target function matches the task identifier, and reports errors when it finds missing, over-inserted, or prematurely returned paths that cannot be covered.
[0118] (2) Selecting the target task phase: External tools (such as diagnostic instruments, debuggers, or calibration tools) call the `CpuLoad_SwitchTask(taskId)` function through the control interface, passing in the target task identifier. In this embodiment, the external tool passes in `CPULOAD_TK_P25`, selecting a 25ms task as the current target task. The system updates the current target task identifier to `CPULOAD_TK_P25` and generates the current measurement generation identifier (e.g., 1).
[0119] (3) Target task entry stage: When the 25ms task is scheduled for execution, it enters its entry point. The instrumentation call `CpuLoad_Task(TRUE, CPULOAD_TK_P25)` at the entry point is executed. The system detects that the current task identifier matches the current target task identifier, and the outermost depth of the current target task is 0. At this point, it is determined to be the outermost entry point, and the system performs the following operations: opens a valid measurement window; clears the interference start point, cumulative interference duration, and shared nesting count value in the shared interference object; and records the current counter value as the start time `T_start`. Subsequently, the outermost depth of the target task changes from 0 to 1.
[0120] (4) Preemptive phase of execution process: Assume that during the execution of the 25ms task, a 0.5ms task (a high-priority execution unit) becomes ready and preempts execution. The instrumentation call CpuLoad_Interrupt(TRUE) at the entry point of the 0.5ms task is executed. The system detects that the valid measurement window is open and that the priority (140) of the 0.5ms task is higher than the priority (80) of the 25ms task, and performs the interference start operation: the shared nesting count value changes from 0 to 1, and the current time is recorded as the interference start point (e.g., time 20).
[0121] During the execution of the 0.5ms task, an interrupt service routine is triggered and interrupts the task. The instrumentation call `CpuLoad_Interrupt(TRUE)` at the entry point of this interrupt service routine is executed. The system initiates the interference: the shared nesting count changes from 1 to 2, and no new interference start point is recorded. When the interrupt service routine ends, the instrumentation call `CpuLoad_Interrupt(FALSE)` at its exit point is executed, and the shared nesting count changes from 2 to 1. The interference duration is not accumulated at this point. After the 0.5ms task resumes execution and ends, the instrumentation call `CpuLoad_Interrupt(FALSE)` at its exit point is executed, and the shared nesting count changes from 1 to 0. At this point, based on the difference between the current time (e.g., time 50) and the interference start point (time 20), 30 timing units are accumulated to the total interference duration.
[0122] If a second preemption interval exists (e.g., a 5ms QM TASK preempted from time 40 to time 60), when its interference start operation is executed at time 40, the shared nested count value is currently 1 (because the 0.5ms TASK has not yet ended), and the count value changes from 1 to 2, without recording a new interference start point. When its interference end operation is executed at time 60, the shared nested count value changes from 1 to 0, and the difference between the current time 60 and the interference start point 20 (40 timing units) is added to the cumulative interference duration. At this time, the end time 50 of the original first preemption interval has been overwritten, and the final cumulative interference duration is 40 timing units, which is the duration of the union of the two preemption intervals [20, 60].
[0123] (5) Target task export settlement stage: When the 25ms task completes and exits, it reaches its exit point. The instrumentation call `CpuLoad_Task(FALSE, CPULOAD_TK_P25)` at the exit point is executed. The system detects that the current task identifier matches the target task identifier and performs an outer depth decrement operation. The outer depth changes from 1 to 0, indicating it is the outermost exit point, and the following settlement operations are performed: close the valid measurement window; check if the shared nesting count value is 0; if not, truncate the interference interval to the window closing time; read the current counter value as the end time. ; Calculate the net execution time. If =1000, =1100, =40, then =100-40=60 timing units. After settlement, update the statistics: if the current net execution time is less than the recorded minimum execution time, update the minimum value; if it is greater than the recorded maximum execution time, update the maximum value, and update the average execution time using the arithmetic mean.
[0124] (6) Switching to the target task phase: If, during the execution of a 25ms task, an external tool calls `CpuLoad_SwitchTask(CPULOAD_TK_P5_QM)` via the control interface to request a switch to a 5ms QM task, the system performs the following switching operations: enters the critical protection region; disables the valid measurement window corresponding to the 25ms task; marks current measurement samples that have not yet been completed at the time of switching as invalid; clears the statistical objects (outer layer depth, start time, net execution time, etc.) and shared interference objects (interference start point, cumulative interference duration, shared nesting count value) of the current target task; generates a new measurement generation identifier (e.g., 2) and assigns it to the 5ms QM task; exits the critical protection region. The switched 5ms QM task takes effect from the next complete outermost instance. If a preemption event is triggered during the execution of the 25ms task, the generation identifier carried when the preemption event records its interference start point is 1. After the switchover is complete, the preemption event ends and the interference ends. At this time, the generational identifier it carries is 1, while the generational identifier of the current target task is 2. The two are inconsistent. The end operation of the preemption event does not update the shared interference object. The measurement results of the 5ms QM TASK are not affected by the old preemption event.
[0125] It should be noted that this specific embodiment is only an exemplary implementation of this application. In other real-time operating systems or different MCU platforms, the specific implementation of other interfaces and parameters such as timing interface and task identifier can be adjusted accordingly, which does not constitute a limitation on the scope of protection of this application.
[0126] Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of this application. The electronic device may include: The memory 501, the processor 502, and the computer program stored on the memory 501 and capable of running on the processor 502.
[0127] When the processor 502 executes the program, it implements the task processing method provided in the above embodiments.
[0128] Furthermore, electronic devices also include: Communication interface 503 is used for communication between memory 501 and processor 502.
[0129] The memory 501 is used to store computer programs that can run on the processor 502.
[0130] The memory 501 includes high-speed RAM memory, as well as non-volatile memory, such as at least one disk storage device.
[0131] If the memory 501, processor 502, and communication interface 503 are implemented independently, then the communication interface 503, memory 501, and processor 502 can be interconnected via a bus to complete communication between them. The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of representation, Figure 5 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0132] Optionally, in a specific implementation, if the memory 501, processor 502, and communication interface 503 are integrated on a single chip, then the memory 501, processor 502, and communication interface 503 can communicate with each other through an internal interface.
[0133] Processor 502 is a central processing unit (CPU), an application specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of this application.
[0134] This application also provides a computer-readable storage medium storing a computer program or instructions thereon, which, when executed by a processor, implements the task processing method described above.
[0135] This application also provides a computer program product, including a computer program or instructions, which, when executed, implement the task processing method described above.
[0136] In the description of this specification, the references to terms such as "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., indicate that a specific feature, structure, material, or characteristic described in connection with that embodiment or example is included in at least one embodiment or example of this application. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples. Moreover, without contradiction, those skilled in the art can combine and integrate the different embodiments or examples described in this specification, as well as the features of different embodiments or examples.
[0137] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one of that feature. In the description of this application, "N" means at least two, such as two, three, etc., unless otherwise explicitly specified.
[0138] Any process or method described in the flowchart or otherwise herein can be understood as representing a module, segment, or portion of code comprising one or N executable instructions for implementing custom logic functions or processes, and the scope of the preferred embodiments of this application includes additional implementations in which functions may be performed not in the order shown or discussed, including substantially simultaneously or in reverse order depending on the functions involved, as should be understood by those skilled in the art to which embodiments of this application pertain.
[0139] It should be understood that the various parts of this application can be implemented using hardware, software, firmware, or a combination thereof. In the above embodiments, the N steps or methods can be implemented using software or firmware stored in memory and executed by a suitable instruction execution system. For example, if implemented in hardware, as in another embodiment, it can be implemented using any one or more of the following techniques known in the art: discrete logic circuits having logic gates for implementing logical functions on data signals, application-specific integrated circuits (ASICs) having suitable combinational logic gates, programmable gate arrays (PGAs), field-programmable gate arrays (FPGAs), etc.
[0140] Those skilled in the art will understand that all or part of the steps of the methods described in the above embodiments can be implemented by a program instructing related hardware, and the program can be stored in a computer-readable storage medium. When executed, the program includes one or a combination of the steps of the method embodiments.
Claims
1. A task processing method, characterized in that, Includes the following steps: Retrieve the target task selected online via the control interface; An effective measurement window is opened at the outermost entry point of the target task, a shared interference object is initialized, and the start time of the target task is recorded. The shared interference object includes the interference start point, the cumulative interference duration, and the shared nesting count value. Within the effective measurement window, for multiple preemption events that preempt the target task on the processor core where the target task resides, the shared nesting count value is updated in response to the start or end of the preemption event; the interference start point and interference cumulative duration are updated according to the jump state of the shared nesting count value to update the shared interference object, and the interference cumulative duration is determined according to the shared interference object, wherein the preemption event includes high-priority execution units and interrupt service routines; When the outermost exit of the target task is detected, the effective measurement window is closed, the end time of the target task is read, and the net execution time of the target task after removing preemption interference is calculated based on the start time, the end time and the cumulative duration of interference.
2. The task processing method according to claim 1, characterized in that, The step of updating the shared interference object based on the jump state of the shared nesting count value further includes: When any preemption event begins, the starting point of the interference is recorded when the jump state of the shared nested counter value jumps from the first value to a non-first value. When the shared nested count value changes from a non-first value to the first value after any preemption event ends, the cumulative interference duration is updated according to the interference start point and the current time. For other preemption events triggered between the start and end of any preemption event, only the shared nested count value is updated.
3. The task processing method according to claim 1, characterized in that, Before updating the shared interference object based on the multiple preemption events, the following is included: Obtain the priority comparison results of multiple preemption events and target tasks; If the priority of the preemption event is higher than that of the target task, then the shared interference object is updated.
4. The task processing method according to claim 3, characterized in that, The priority comparison result is determined based on at least one of the following: operating system scheduling configuration, priority mapping table, or priority determination function.
5. The task processing method according to claim 1, characterized in that, After obtaining the target task selected online via the control interface, the process includes: When switching the target task online, the system enters the critical protection zone, disables the valid measurement window corresponding to the previous target task, and marks the measurement samples corresponding to the previous target task that have not yet been completed at the time of switching as invalid. Clear the statistical objects and shared interference objects of the current target task, and update the measurement generation identifier of the target task; Exit the critical protection region and take effect from the next complete outermost instance of the current target task; wherein, when performing an interference start operation or interference end operation within the effective measurement window, the shared interference object is updated when the measurement generation identifier of the current measurement target task matches the measurement generation identifier of the measurement event.
6. The task processing method according to any one of claims 1-5, characterized in that, Set up a target task statistics object corresponding to the target task and a shared interference object used by multiple preemption sources; The target task statistics include: target task outer depth, target task start time, net execution time, sample validity indicator, and measurement generation identifier; The shared interference object includes: a flag indicating the state of the effective measurement window, the interference start point, the cumulative interference duration, and a shared nested count value; The outer depth of the target task is independent of the shared nesting count value, wherein the sample validity flag is used to indicate the validity of the measurement sample of the current target task, and the measurement generation identifier is used to distinguish different measurement batches.
7. The task processing method according to claim 1, characterized in that, Also includes: When an abnormal shared nested count value is detected, the shared interference object is reset, and the measurement sample of the current target task is marked as invalid.
8. The task processing method according to claim 1, characterized in that, Also includes: When the interference interval recorded by the shared interference object crosses the boundary of the effective measurement window, the termination time of the interference interval is truncated to the closing time of the effective measurement window.
9. The task processing method according to claim 1, characterized in that, After each valid measurement is completed, the minimum execution time of the target task is updated based on the minimum of the historical valid net execution time, the maximum execution time of the target task is updated based on the maximum of the historical valid net execution time, and the average execution time of the target task is updated based on the historical valid net execution time and the total number of valid samples.
10. The task processing method according to claim 1, characterized in that, The start time, end time, and interference start point are obtained by a monotonically increasing counter; The fixed time consumption of a single reading of the counter is obtained by executing multiple times through an empty measurement path to obtain a calibration value, and the calibration value represents the fixed time consumption required for a single time measurement call and the corresponding counter reading operation. When calculating the net execution time of the target task, the corresponding calibration value is deducted based on the number of counter reads performed within the effective measurement window; When an abnormal timestamp order or a difference exceeding a preset range is detected, exception handling is triggered, and the current sample is marked as invalid.
11. The task processing method according to claim 1, characterized in that, Also includes: The mapping relationship between task functions and task identifiers is determined through an automatic instrumentation program; Based on the mapping relationship between task functions and task identifiers, an entry timing call is deployed at the entry point of the target task, and a corresponding exit timing call is deployed at each exit point of the target task. The entry timing call and the exit timing call have the same task identifier and the same measurement generation identifier. If the automatic instrumentation program recognizes an existing timing call flag, it will not repeatedly deploy the entry timing call and the exit timing call. After the automatic instrumentation program is detected to have completed deployment, the number of entry time trial calls and the number of exit time trial calls for the target task are verified, as well as the task identifier of each time trial call. If any of the numbers or task identifiers do not match, an error warning is generated.
12. A task processing system, characterized in that, The system is used to process the task processing method according to any one of claims 1-11, wherein the system includes: a target task selection unit, a target task timing unit, a shared interference statistics unit, and a settlement unit, wherein... The target task selection unit is used to select a target task online through a control interface and output the identifier of the target task, the current generational identifier, and the window status of the target task. The target task timing unit is used to receive the identifier of the target task and the current measurement generation identifier, clear the target task statistical object and the shared interference object at the outermost entry of the target task and record the start time, and record the end time at the outermost exit of the target task. The target task statistical object includes at least the identifier of the target task, the current measurement generation identifier and the sample validity flag, and the shared interference object includes a valid measurement window flag, interference start point, interference cumulative duration and shared nested count value. The shared interference statistics unit includes a shared interference object and a shared nested count value that are read and written by the high-priority execution unit and interrupt service routine that actually preempt the target task on the processor core. The update operation on the shared interference object is performed only when the generational identifier carried by the event that triggers the interference update when the window is open is consistent with the current measured generational identifier. The settlement unit is used to calculate the net execution time of the target task at the outermost exit of the target task based on the start and end times recorded by the target task timing unit and the cumulative interference duration of the shared interference statistics unit.
13. An electronic device, comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to perform the task processing method as described in any one of claims 1-11.
14. A computer-readable storage medium having a computer program or instructions stored thereon, characterized in that, When the computer program or instructions are executed by a processor, they are used to implement the task processing method as described in any one of claims 1-11.
15. A computer program product, comprising a computer program or instructions, characterized in that, When the computer program or instructions are executed, they implement the task processing method as described in any one of claims 1-11.