Statistical method for CPU occupancy rate of SMP architecture real-time operating system, electronic equipment and medium
By using timers to generate Tick interrupts in the operating system, recording and correcting the running time of tasks, the problem of strong real-time task CPU occupancy measurement distortion is solved, and the accurate measurement of strong real-time task CPU occupancy is achieved.
Patent Information
- Application Number
- CN202411359520.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-27
- Publication Date
- 2025-03-07
AI Technical Summary
In the prior art, there is distortion in the CPU occupancy measurement of strong real-time tasks, which mainly due to the high interrupt priority and short trigger period, resulting in distortion of the task information collected by Tick ISR.
By periodically generating Tick interrupts in the operating system, recording the start and ending Tick interrupt count value and timer count value of the task, and correcting the count value of the task switching type and the unswitched type, calculating the cumulative running time of the task and the actual total CPU running time, thereby accurately measuring the CPU occupancy rate.
Accurate measurement of CPU occupancy of strong real-time tasks is achieved, and the measurement distortion problem in the prior art is solved, without relying on excess external hardware devices, and without the need to create statistical tasks that occupy CPU running time separately.
Smart Images

Figure CN119576711A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of multi-task operating systems, and in particular to a method for counting CPU occupancy of a real-time operating system with an SMP architecture, an electronic device and a medium. Background Art
[0002] With the increase in the complexity and difficulty of combat missions, high-performance processors composed of multiple isomorphic ARM Cortex-A cores are gradually being used in the field of missile-borne flight control. At the same time, with the widespread application of embedded RTOS (Real Time Operating System) in the field of missile-borne flight control, by deploying embedded RTOS (such as SylixOS, VxWorks, RT-Thread, etc.), complex multi-core systems can be uniformly managed in the form of SMP (Symmetric Multiprocessing), abstracting collaborative chip hardware, and coordinating resource supply and demand to enable efficient operation of applications. Time-sensitive strong real-time tasks (or threads, the same below) are usually triggered by external high-frequency interrupts and are closely related to critical control links. The ability to correctly perceive the CPU occupancy rate of strong real-time tasks is crucial to coordinating the allocation of CPU computing resources and evaluating whether the expected combat effectiveness is achieved.
[0003] The CPU occupancy statistics method for RTOS tasks in a multi-core SMP architecture is usually that after the user gives the CPU occupancy statistics instruction, the operating system traverses all currently running tasks on all processor cores in each Tick ISR (Interrput Service Routine) to indicate that the task has completed a Tick cycle execution. This method has the following problems:
[0004] 1) The interrupt priority associated with a strong real-time task is often higher than the Tick priority, and the trigger cycle is often at the ms level or even hundreds of us level, which is much lower than the Tick cycle of a normal operating system;
[0005] 2) Some RTOS application scenarios do not allow interrupt nesting configuration to reduce timing complexity. Even if the non-Tick interrupt has a lower priority, there is still a situation where the Tick ISR is delayed;
[0006] 3) Various interrupts and Tick interrupts are not generated by the same clock source, which means that the possibility of various interrupts occurring at the same time cannot be avoided through software and hardware design;
[0007] For the above reasons, the tasks currently running on all processor cores collected in each Tick ISR are distorted. When the interrupt associated with a strong real-time task is half of the Tick period, such as Figure 1As shown in the figure, since the execution time of the strong real-time task just avoids each Tick, the strong real-time task is not collected and the CPU occupancy rate of the task is 0%; Figure 2 As shown in the figure, since the strong real-time task is interrupted by Tick during its execution, each Tick collects data about the execution of the strong real-time task, and the CPU occupancy rate of the task is (100 / N)% (N represents the number of processor cores in the SMP architecture). Summary of the invention
[0008] The present invention provides a method for counting CPU occupancy rate of a real-time operating system with an SMP architecture, an electronic device and a medium, which can solve the technical problem of distortion in measuring CPU occupancy rate of strong real-time tasks in the prior art.
[0009] According to one aspect of the present invention, a method for counting CPU occupancy of a real-time operating system with an SMP architecture is provided, the method comprising:
[0010] S1, the operating system uses the timer to periodically generate Tick interrupts. Each time a Tick interrupt is generated, the Tick interrupt count value increases by 1;
[0011] S2, after the operating system receives the CPU occupancy statistics command, in the subsequent Tick interrupt, it is determined whether the current Tick interrupt is the first one in the current statistical period. If yes, it goes to S3, if not, it goes to S4;
[0012] S3, obtain the task to be run, record the current Tick interrupt count value as the starting Tick interrupt count value of the task to be run in this scheduling, and record the current timer count value as the starting timer count value of the task to be run in this scheduling;
[0013] S4, determine whether the current Tick interrupt occurs for the middle time in the current statistical period, if yes, go to S5, if no, go to S6;
[0014] S5, obtaining a list of ready tasks, and determining whether the current task is the highest priority task in the list of ready tasks, if yes, proceeding to S8, if no, proceeding to S9;
[0015] S6, judging whether the current Tick interruption is the last one to occur in the current statistical period, and if the current Tick interruption is the last one to occur in the current statistical period, going to S7;
[0016] S7, obtain the current task, use the current Tick interrupt count value as the termination Tick interrupt count value of the current task in this scheduling, use the current timer count value as the termination timer count value of the current task in this scheduling, and go to S10;
[0017] S8, the task unswitched type count value is increased by 1, the current task is set as a waiting task, and the waiting task site is restored;
[0018] S9, the task switching type count value is increased by 1, the current Tick interrupt count value is recorded as the termination Tick interrupt count value of the current task in this scheduling and the starting Tick interrupt count value of the task to be run in the next scheduling, the current timer count value is recorded as the termination timer count value of the current task in this scheduling and the starting timer count value of the task to be run in the next scheduling, the highest priority task is set as the task to be run, and the task to be run is restored;
[0019] S10, end the statistics, calculate the running time of each observed task in each scheduling according to the starting Tick interrupt count value, the starting timer count value, the ending Tick interrupt count value and the ending timer count value of each observed task in each scheduling, and calculate the cumulative running time of each observed task in the current statistical period according to the running time of each observed task in each scheduling;
[0020] S11, correcting the accumulated running time of each observed task in the current statistical period according to the task switching type count value, the corresponding time of the task switching type, the task non-switching type count value, and the corresponding time of the task non-switching type to obtain the corrected accumulated running time of each observed task in the current statistical period;
[0021] S12, calculating the actual total CPU running time in the current statistical period according to the first Tick interrupt count value and the corresponding timer count value after receiving the CPU occupancy statistics command and the N+1th Tick interrupt count value and the corresponding timer count value, where N represents the statistical duration in the CPU occupancy statistics command, and the unit is the Tick interrupt cycle;
[0022] S13, calculating the CPU occupancy rate of each observed task in the current statistical period according to the corrected cumulative running time of each observed task in the current statistical period and the actual total CPU running time in the current statistical period.
[0023] Further, the task switching type includes an interrupt-level task switching type and a task-level task switching type, and the task non-switching type includes an interrupt-level task non-switching type and a task-level task non-switching type.
[0024] Furthermore, the running time of each observed task in each scheduling is calculated according to the starting Tick interrupt count value, the starting timer count value, the ending Tick interrupt count value and the ending timer count value of each observed task in each scheduling by the following formula:
[0025]
[0026] In the above formula, T runtime,i represents the running time of the observed single task in the i-th scheduling, T start1,i Represents the starting Tick interrupt count value of the observed single task in the i-th scheduling, T end1,i It represents the termination Tick interrupt count value of the observed single task in the i-th scheduling, T start2,i represents the starting timer count value of the observed single task in the i-th scheduling, T end2,i represents the termination timer count value of the observed single task in the i-th scheduling, T timer Indicates the timer count value corresponding to a single Tick interrupt cycle.
[0027] Furthermore, the cumulative running time of each task observed in the current statistical period is calculated according to the running time of each task observed in each scheduling by the following formula:
[0028]
[0029] In the above formula, T runtime,all It represents the cumulative running time of the single task observed in the current statistical period, and M represents the total number of scheduling times of the single task observed in the current statistical period.
[0030] Furthermore, the cumulative running time of each observed task in the current statistical period is corrected according to the task switching type count value, the corresponding time of the task switching type, the task non-switching type count value, and the corresponding time of the task non-switching type by the following formula to obtain the corrected cumulative running time of each observed task in the current statistical period:
[0031] T′ runtime,all =T runtime,all -T int_nosched *N int_nosched -T task_nosched *N task_nosched , -T int_sched *N int_sched -T task_sched *N task_sched
[0032] In the above formula, T′ runtime,all Indicates the corrected cumulative running time of a single task in the current statistical period, T int_nosched Indicates the corresponding time when the interrupt level task does not switch type, N int_nosched Indicates that the interrupt level task has not switched type count value, T task_nosched Indicates the corresponding time when the task level task does not switch type, Ntask_nosched Indicates the task-level task unswitched type count value, T int_sched Indicates the corresponding time of interrupt-level task switching type, N int_sched Indicates the interrupt level task switching type count value, T task_sched Indicates the corresponding time of task-level task switching type, N task_sched Indicates the task-level task switch type count value.
[0033] Furthermore, the actual total CPU running time in the current statistical period is calculated according to the first Tick interrupt count value and the corresponding timer count value after receiving the CPU occupancy statistics command and the N+1th Tick interrupt count value and the corresponding timer count value by the following formula:
[0034] T cpu =T X+N -T X ,
[0035] T X =X*T tick +T X,timer / T timer ,
[0036] T X+N =(X+N)*T tick +T X+N,timer / T timer ,
[0037] In the above formula, T cpu Indicates the actual total CPU running time in the current statistical period, T X Indicates the time corresponding to the first Tick interrupt after receiving the CPU usage statistics command, T X+N Indicates the time corresponding to the N+1th Tick interrupt after receiving the CPU occupancy statistics command, X indicates the first Tick interrupt count value after receiving the CPU occupancy statistics command, T tick Indicates the Tick interrupt cycle, T X,timer Indicates the timer count value corresponding to the first Tick interrupt generated after receiving the CPU occupancy statistics command, T X+N,timer Indicates the timer count value corresponding to the time when the N+1th Tick interrupt occurs after the CPU usage statistics command is received.
[0038] Furthermore, the CPU occupancy rate of each task observed in the current statistical period is calculated according to the corrected cumulative running time of each task observed in the current statistical period and the actual total CPU running time in the current statistical period by the following formula:
[0039]
[0040] In the above formula, a represents the CPU usage of a single task in the current statistical period.
[0041] According to another aspect of the present invention, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the steps of the CPU occupancy statistics method proposed above in the present invention are implemented.
[0042] According to another aspect of the present invention, a non-transitory computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of the CPU occupancy statistics method proposed in the present invention are implemented.
[0043] The technical solution of the present invention is applied to provide a method, electronic device and medium for CPU occupancy statistics of a real-time operating system with an SMP architecture. The method does not rely on redundant external hardware devices, does not need to create a separate statistical task that occupies the CPU running time, and uses the timer that generates the Tick interrupt in the operating system. Through the high-precision statistics of the observed task running time scheduler level and the calibration of the total CPU running time, the running time of the observed task and the total CPU running time are displayed with precise granularity, which can achieve accurate measurement of the CPU occupancy of strong real-time tasks. The method can be extended to a wider range of embedded RTOS fields with similar application scenarios, especially complex timing scenarios of multi-core SMP architectures, and can provide a high-precision and high-accuracy method for calculating the CPU occupancy of strong real-time tasks for missile-borne flight control scenarios and real-time operating systems based on SMP architectures. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] The included drawings are used to provide a further understanding of the embodiments of the present invention, which constitute a part of the specification, are used to illustrate the embodiments of the present invention, and together with the text description, explain the principles of the present invention. Obviously, the drawings in the following description are only some embodiments of the present invention, and for ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0045] Figure 1 A schematic diagram showing that a strong real-time task is not being executed when a Tick interrupt occurs;
[0046] Figure 2 A schematic diagram showing that a strong real-time task is being executed when a Tick interrupt occurs;
[0047] Figure 3 A schematic diagram of a statistical process based on task running time observed at a scheduling point according to a specific embodiment of the present invention is shown;
[0048] Figure 4 A schematic diagram showing the statistical principle of the total CPU running time based on the Tick ISR provided according to a specific embodiment of the present invention is shown;
[0049] Figure 5 A schematic diagram of a process flow for designing a Tick ISR for an operating system provided according to a specific embodiment of the present invention is shown. DETAILED DESCRIPTION
[0050] It should be noted that, in the absence of conflict, the embodiments in this application and the features in the embodiments can be combined with each other. The technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. The following description of at least one exemplary embodiment is actually only illustrative and is by no means intended to limit the present invention and its application or use. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0051] It should be noted that the terms used herein are only for describing specific embodiments and are not intended to limit the exemplary embodiments according to the present application. As used herein, unless the context clearly indicates otherwise, the singular form is also intended to include the plural form. In addition, it should be understood that when the terms "comprise" and / or "include" are used in this specification, it indicates the presence of features, steps, operations, devices, components and / or combinations thereof.
[0052] Unless otherwise specifically stated, the relative arrangement of the parts and steps described in these embodiments, numerical expressions and numerical values do not limit the scope of the present invention. At the same time, it should be understood that, for ease of description, the sizes of the various parts shown in the accompanying drawings are not drawn according to the actual proportional relationship. The technology, method and equipment known to ordinary technicians in the relevant field may not be discussed in detail, but in appropriate cases, the technology, method and equipment should be regarded as a part of the authorization specification. In all examples shown and discussed here, any specific value should be interpreted as being merely exemplary, rather than as a limitation. Therefore, other examples of exemplary embodiments may have different values. It should be noted that similar reference numerals and letters represent similar items in the following drawings, so once a certain item is defined in an accompanying drawing, it does not need to be further discussed in subsequent drawings.
[0053] Different from the traditional method of collecting the currently running tasks of each processor core in the Tick interrupt to approximately characterize that the task has been executed in the current Tick cycle, according to a specific embodiment of the present invention, a method for counting the CPU occupancy rate of a real-time operating system of an SMP architecture is provided, and the method includes:
[0054] S1, the operating system uses the timer to periodically generate Tick interrupts. Each time a Tick interrupt is generated, the Tick interrupt count value increases by 1;
[0055] S2, after the operating system receives the CPU occupancy statistics command, in the subsequent Tick interrupt, it is determined whether the current Tick interrupt is the first one in the current statistical period. If yes, it goes to S3, if not, it goes to S4;
[0056] S3, obtain the task to be run, record the current Tick interrupt count value as the starting Tick interrupt count value of the task to be run in this scheduling, and record the current timer count value as the starting timer count value of the task to be run in this scheduling;
[0057] S4, determine whether the current Tick interrupt occurs for the middle time in the current statistical period, if yes, go to S5, if no, go to S6;
[0058] S5, obtaining a list of ready tasks, and determining whether the current task is the highest priority task in the list of ready tasks, if yes, proceeding to S8, if no, proceeding to S9;
[0059] S6, judging whether the current Tick interruption is the last one to occur in the current statistical period, and if the current Tick interruption is the last one to occur in the current statistical period, going to S7;
[0060] S7, obtain the current task, use the current Tick interrupt count value as the termination Tick interrupt count value of the current task in this scheduling, use the current timer count value as the termination timer count value of the current task in this scheduling, and go to S10;
[0061] S8, the task unswitched type count value is increased by 1, the current task is set as a waiting task, and the waiting task site is restored;
[0062] S9, the task switching type count value is increased by 1, the current Tick interrupt count value is recorded as the termination Tick interrupt count value of the current task in this scheduling and the starting Tick interrupt count value of the task to be run in the next scheduling, the current timer count value is recorded as the termination timer count value of the current task in this scheduling and the starting timer count value of the task to be run in the next scheduling, the highest priority task is set as the task to be run, and the task to be run is restored;
[0063] S10, end the statistics, calculate the running time of each observed task in each scheduling according to the starting Tick interrupt count value, the starting timer count value, the ending Tick interrupt count value and the ending timer count value of each observed task in each scheduling, and calculate the cumulative running time of each observed task in the current statistical period according to the running time of each observed task in each scheduling;
[0064] S11, correcting the accumulated running time of each observed task in the current statistical period according to the task switching type count value, the corresponding time of the task switching type, the task non-switching type count value, and the corresponding time of the task non-switching type to obtain the corrected accumulated running time of each observed task in the current statistical period;
[0065] S12, calculating the actual total CPU running time in the current statistical period according to the first Tick interrupt count value and the corresponding timer count value after receiving the CPU occupancy statistics command and the N+1th Tick interrupt count value and the corresponding timer count value, where N represents the statistical duration in the CPU occupancy statistics command, and the unit is the Tick interrupt cycle;
[0066] S13, calculating the CPU occupancy rate of each observed task in the current statistical period according to the corrected cumulative running time of each observed task in the current statistical period and the actual total CPU running time in the current statistical period.
[0067] By applying this configuration method, a method for counting the CPU occupancy rate of a real-time operating system with an SMP architecture is provided. The method does not rely on redundant external hardware devices, and does not require the creation of a separate statistical task that occupies the CPU running time. The timer that generates the Tick interrupt in the operating system is used. Through high-precision statistics of the observed task running time scheduler level and calibration of the total CPU running time, the running time of the observed task and the total CPU running time are displayed with precise granularity, which can achieve accurate measurement of the CPU occupancy rate of strong real-time tasks. The method can be extended to a wider range of embedded RTOS fields with similar application scenarios, especially complex timing scenarios of multi-core SMP architectures, and can provide a high-precision and high-accuracy method for counting the CPU occupancy rate of strong real-time tasks for missile-borne flight control scenarios and real-time operating systems based on SMP architectures. Compared with the prior art, the technical solution of the present invention can solve the technical problem of distortion in measuring the CPU occupancy rate of strong real-time tasks in the prior art.
[0068] In practical applications, the hardware timer used to generate the Tick interrupt of the present invention has the following characteristics: it is implemented by a counter with automatic refill capability; the current count value of the counter in the timer can be read at any time; the hardware timer has a global sharing feature and can be accessed by all processor cores; and the validity of the count value update moment is guaranteed. In addition, for the scheduling point, in the embodiment of the present invention, calling the operating system service (operating system service not designed for interrupts) and various interrupt exit links are used as scheduling points that may trigger task scheduling.
[0069] Further, in the embodiment of the present invention, the running time of each observed task in each scheduling is calculated according to the starting Tick interrupt count value, the starting timer count value, the ending Tick interrupt count value and the ending timer count value of each observed task in each scheduling by the following formula:
[0070]
[0071] In the above formula, T runtime,i represents the running time of the observed single task in the i-th scheduling, T start1,i Represents the starting Tick interrupt count value of the observed single task in the i-th scheduling, T end1,i It represents the termination Tick interrupt count value of the observed single task in the i-th scheduling, T start2,i represents the starting timer count value of the observed single task in the i-th scheduling, T end2,i represents the termination timer count value of the observed single task in the i-th scheduling, T timer Indicates the timer count value corresponding to a single Tick interrupt cycle, where T start1,i and T end1,i is the theoretical value.
[0072] If the observed single task runs in multiple schedules in the current statistical period, the running time in multiple schedules is accumulated to obtain the cumulative running time in the current statistical period. That is, the embodiment of the present invention calculates the cumulative running time of each observed task in the current statistical period according to the running time of each task in each schedule by the following formula:
[0073]
[0074] In the above formula, T runtime,all It represents the cumulative running time of the single task observed in the current statistical period, and M represents the total number of scheduling times of the single task observed in the current statistical period.
[0075] Furthermore, in an embodiment of the present invention, the task switching type includes an interrupt-level task switching type and a task-level task switching type, and the task non-switching type includes an interrupt-level task non-switching type and a task-level task non-switching type, wherein the interrupt-level task switching type refers to an interruption occurring and a task switching occurring, the task-level task switching type refers to no interruption occurring but entering the operating system scheduling point and a task switching occurring, the interrupt-level task non-switching type refers to an interruption occurring and no task switching occurring, and the task-level task non-switching type refers to no interruption occurring but entering the operating system scheduling point and no task switching occurring. The real-time operating system relies on an event triggering mechanism to determine which ready task to execute immediately through the scheduler according to the scheduling strategy, as shown in the following program. Non-one-time execution tasks in the operating system basically exist in the form of an infinite loop of while(1), that is, the task is abstracted to exclusively use the entire processor core. Therefore, if the method of measuring the start and end time is established by staking inside the execution body, it is obviously impossible to correctly reflect the actual execution status of the task. As Figure 3 As shown, in this method, the start and end time of the observed task is recorded at the scheduling point. When it is determined that the current task is not the highest priority task that is ready, that is, when the scheduler wants to switch the CPU to the highest priority task that is ready as the task to be run, the current timer count value and the current tick interrupt count value of the operating system are read as the end time of the current task in this scheduling, and also as the start time of the task to be run in the next scheduling, and the task switching type count (interrupt level or task level) is increased at the same time. For the process where no task switching occurs, it may be caused by an operating system service call or interrupt, and only the task non-switching type count (interrupt level or task level) is increased.
[0076]
[0077]
[0078] That is to say, in the embodiment of the present invention, the cumulative running time of each observed task in the current statistical period is corrected according to the task switching type count value, the corresponding time of the task switching type, the task non-switching type count value, and the corresponding time of the task non-switching type by the following formula to obtain the corrected cumulative running time of each observed task in the current statistical period:
[0079] T′ runtime,all =T runtime,all -T int_nosched *N int_nosched -T task_nosched *N task_nosched , -T int_sched *N int_sched -T task_sched *N task_sched
[0080] In the above formula, T′runtime,all Indicates the corrected cumulative running time of a single task in the current statistical period, T int_nosched Indicates the corresponding time when the interrupt level task does not switch type, N int_nosched Indicates that the interrupt level task has not switched type count value, T task_nosched Indicates the corresponding time when the task level task does not switch type, N task_nosched Indicates the task-level task unswitched type count value, T int_sched Indicates the corresponding time of interrupt-level task switching type, N int_sched Indicates the interrupt level task switching type count value, T task_sched Indicates the corresponding time of task-level task switching type, N task_sched Represents the task-level task switching type count value. Among them, time T int_nosched , T task_nosched , T int_sched and T task_sched It is usually measured and given by the operating system BSP and can also be ignored.
[0081] In practical applications, for the above variable T start1,i , T end1,i , T start2,i , T end2,i , T runtime,i , T int_nosched 、N int_nosched , T task_nosched 、N task_nosched , T int_sched 、N int_sched , T task_sched and N task_sched , make full use of the redundant member variables in the TCB (Task Control Block) of the task observed by the operating system to represent the meaning of the aforementioned variables or modify the TCB of the task observed by the operating system to add the aforementioned variables to achieve it.
[0082] The above steps can be used to obtain the accurate cumulative running time of the observed task within the statistical period, and then the actual total CPU running time within the current statistical period is calculated. The statistics of the CPU occupancy rate of the observed task should not introduce other interference factors that occupy CPU resources. Therefore, in the embodiment of the present invention, when the operating system receives the CPU occupancy rate statistics command issued by the user, the time when the first subsequent Tick interrupt is generated is used as the starting time of the actual total CPU running time, and the time when the N+1th Tick interrupt is generated is used as the end time, where N is the statistical duration specified by the user, and the unit is the Tick interrupt cycle, that is, the statistical duration is an integer multiple of the Tick interrupt cycle. Figure 4As shown in the figure, the dotted line represents the theoretical occurrence time of TickISR (Tick interrupt). Due to the preemption of higher priority interrupts associated with strong real-time tasks, the actual TickISR occurrence time is represented by a solid arrow. Delay 1, Delay 2, and Delay 3 will produce delays. Therefore, the actual total CPU running time is not an integer multiple of the Tick interrupt, but is based on the end time T after the delay. X+N - The delayed start time T X The actual total CPU running time counted = T X+N -T X If the hardware timer is an incremental counter, the actual total CPU running time in the current statistical period is calculated using the following formula based on the first Tick interrupt count value and the corresponding timer count value after receiving the CPU occupancy statistics command, as well as the N+1th Tick interrupt count value and the corresponding timer count value:
[0083] T cpu =T X+N -T X ,
[0084] T X =X*T tick +T X,timer / T timer ,
[0085] T X+N =(X+N)*T tick +T X+N,timer / T timer ,
[0086] In the above formula, T cpu Indicates the actual total CPU running time in the current statistical period, T X Indicates the time corresponding to the first Tick interrupt after receiving the CPU usage statistics command, T X+N Indicates the time corresponding to the N+1th Tick interrupt after receiving the CPU occupancy statistics command, X indicates the first Tick interrupt count value after receiving the CPU occupancy statistics command, T tick Indicates the Tick interrupt cycle, T X,timer Indicates the timer count value corresponding to the first Tick interrupt generated after receiving the CPU occupancy statistics command, T X+N,timer Indicates the timer count value corresponding to the time when the N+1th Tick interrupt occurs after the CPU usage statistics command is received.
[0087] Further, the CPU occupancy of the observed task = (the running time of the observed task) / (the total running time of the CPU) * 100%. Based on the above embodiment, in the embodiment of the present invention, the CPU occupancy of each observed task in the current statistical period is calculated according to the corrected cumulative running time of each observed task in the current statistical period and the actual total running time of the CPU in the current statistical period by the following formula:
[0088]
[0089] In the above formula, a represents the CPU usage of a single task in the current statistical period.
[0090] In addition, in the embodiment of the present invention, the Tick ISR of the operating system based on multi-core SMP is designed to bind only a single processor core instead of all processor cores, so as to unify the Tick interrupt time of multiple processor cores in a low resource occupation manner. The Tick ISR is used to define the start and end time of the total CPU running time. Figure 5 As shown, in the Tick ISR exit phase of the operating system, the first occurrence of the Tick interrupt, the middle occurrence of the Tick interrupt, and the last occurrence of the Tick interrupt within the statistical cycle are judged. When the Tick interrupt occurs for the first time, the start time T of the task to be run on each core is found start1 and T start2 Fill in. The processing method when Tick interrupt occurs in the middle is the same as Figure 3 , when the Tick interrupt occurs for the last time, find the completion time T of the current running task of each core end1 and T end2 Fill in, summarize all tasks during the statistics period and complete the printout.
[0091] According to another aspect of the present invention, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the steps of the CPU occupancy statistics method proposed above in the present invention are implemented.
[0092] According to another aspect of the present invention, a non-transitory computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of the CPU occupancy statistics method proposed in the present invention are implemented.
[0093] In summary, the present invention provides a method, electronic device and medium for counting the CPU occupancy rate of a real-time operating system with an SMP architecture. The method does not rely on redundant external hardware devices, does not need to separately create a statistical task that occupies the CPU running time, and utilizes the timer that generates the Tick interrupt in the operating system. Through the high-precision statistics of the observed task running time scheduler level and the calibration of the total CPU running time, the running time of the observed task and the total CPU running time are displayed with precise granularity, which can achieve accurate measurement of the CPU occupancy rate of strong real-time tasks. The method can be extended to a wider range of embedded RTOS fields with similar application scenarios, especially complex timing scenarios of multi-core SMP architectures, and can provide a high-precision and high-accuracy method for counting the CPU occupancy rate of strong real-time tasks for missile-borne flight control scenarios and real-time operating systems based on SMP architectures. Compared with the prior art, the technical solution of the present invention can solve the technical problem of distortion in the measurement of the CPU occupancy rate of strong real-time tasks in the prior art.
[0094] For ease of description, spatially relative terms such as "above", "above", "on the upper surface of", "above", etc. may be used here to describe the spatial positional relationship between a device or feature and other devices or features as shown in the figure. It should be understood that spatially relative terms are intended to include different orientations of the device in use or operation in addition to the orientation described in the figure. For example, if the device in the accompanying drawings is inverted, the device described as "above other devices or structures" or "above other devices or structures" will be positioned as "below other devices or structures" or "below other devices or structures". Thus, the exemplary term "above" can include both "above" and "below". The device can also be positioned in other different ways (rotated 90 degrees or in other orientations), and the spatially relative descriptions used here are interpreted accordingly.
[0095] In addition, it should be noted that the use of terms such as "first" and "second" to limit components is only for the convenience of distinguishing the corresponding components. If not otherwise stated, the above terms have no special meaning and therefore cannot be understood as limiting the scope of protection of the present invention.
[0096] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for calculating CPU occupancy rate of a real-time operating system of an SMP architecture, characterized in that: The method comprises: S1, the operating system uses the timer to periodically generate Tick interrupts. Each time a Tick interrupt is generated, the Tick interrupt count value increases by 1; S2, after the operating system receives the CPU occupancy statistics command, in the subsequent Tick interrupt, it is determined whether the current Tick interrupt is the first one in the current statistical period. If yes, it goes to S3, if not, it goes to S4; S3, obtain the task to be run, record the current Tick interrupt count value as the starting Tick interrupt count value of the task to be run in this scheduling, and record the current timer count value as the starting timer count value of the task to be run in this scheduling; S4, determine whether the current Tick interrupt occurs for the middle time in the current statistical period, if yes, go to S5, if no, go to S6; S5, obtaining a list of ready tasks, and determining whether the current task is the highest priority task in the list of ready tasks, if yes, proceeding to S8, if no, proceeding to S9; S6, judging whether the current Tick interruption is the last one to occur in the current statistical period, and if the current Tick interruption is the last one to occur in the current statistical period, going to S7; S7, obtain the current task, use the current Tick interrupt count value as the termination Tick interrupt count value of the current task in this scheduling, use the current timer count value as the termination timer count value of the current task in this scheduling, and go to S10; S8, the task unswitched type count value is increased by 1, the current task is set as a waiting task, and the waiting task site is restored; S9, the task switching type count value is increased by 1, the current Tick interrupt count value is recorded as the termination Tick interrupt count value of the current task in this scheduling and the starting Tick interrupt count value of the task to be run in the next scheduling, the current timer count value is recorded as the termination timer count value of the current task in this scheduling and the starting timer count value of the task to be run in the next scheduling, the highest priority task is set as the task to be run, and the task to be run is restored; S10, end the statistics, calculate the running time of each observed task in each scheduling according to the starting Tick interrupt count value, the starting timer count value, the ending Tick interrupt count value and the ending timer count value of each observed task in each scheduling, and calculate the cumulative running time of each observed task in the current statistical period according to the running time of each observed task in each scheduling; S11, correcting the accumulated running time of each observed task in the current statistical period according to the task switching type count value, the corresponding time of the task switching type, the task non-switching type count value, and the corresponding time of the task non-switching type to obtain the corrected accumulated running time of each observed task in the current statistical period; S12, calculating the actual total CPU running time in the current statistical period according to the first Tick interrupt count value and the corresponding timer count value after receiving the CPU occupancy statistics command and the N+1th Tick interrupt count value and the corresponding timer count value, wherein N represents the statistical duration in the CPU occupancy statistics command, and the unit is the Tick interrupt cycle; S13, calculating the CPU occupancy rate of each observed task in the current statistical period according to the corrected cumulative running time of each observed task in the current statistical period and the actual total CPU running time in the current statistical period.
2. The method according to claim 1, characterized in that: The task switching type includes an interrupt level task switching type and a task level task switching type, and the task non-switching type includes an interrupt level task non-switching type and a task level task non-switching type.
3. The method according to claim 2, characterized in that The running time of each task observed in each scheduling is calculated by the following formula based on the starting Tick interrupt count value, starting timer count value, ending Tick interrupt count value and ending timer count value of each task observed in each scheduling: In the above formula, T runtime,i represents the running time of the observed single task in the i-th scheduling, T start1,i Represents the starting Tick interrupt count value of the observed single task in the i-th scheduling, T end1,i It represents the termination Tick interrupt count value of the observed single task in the i-th scheduling, T start2,i represents the starting timer count value of the observed single task in the i-th scheduling, T end2,i represents the termination timer count value of the observed single task in the i-th scheduling, T timer Indicates the timer count value corresponding to a single Tick interrupt cycle.
4. The method according to claim 3, characterized in that The cumulative running time of each task in the current statistical period is calculated using the following formula based on the running time of each task in each scheduling: In the above formula, T runtime,all It represents the cumulative running time of the single task observed in the current statistical period, and M represents the total number of scheduling times of the single task observed in the current statistical period.
5. The method according to claim 4, characterized in that The following formula is used to correct the cumulative running time of each observed task in the current statistical period according to the task switching type count value, the corresponding time of the task switching type, the task non-switching type count value, and the corresponding time of the task non-switching type to obtain the corrected cumulative running time of each observed task in the current statistical period: In the above formula, T r ' untime,all Indicates the corrected cumulative running time of a single task in the current statistical period, T int_nosched Indicates the corresponding time when the interrupt level task does not switch type, N int_nosched Indicates that the interrupt level task has not switched type count value, T task_nosched Indicates the corresponding time when the task level task does not switch type, N task_nosched Indicates the task-level task unswitched type count value, T int_sched Indicates the corresponding time of interrupt-level task switching type, N int_sched Indicates the interrupt level task switching type count value, T task_sched Indicates the corresponding time of task-level task switching type, N task_sched Indicates the task-level task switch type count value.
6. The method according to claim 5, characterized in that The actual total CPU running time in the current statistical period is calculated by the following formula according to the first Tick interrupt count value and the corresponding timer count value after receiving the CPU occupancy statistics command and the N+1th Tick interrupt count value and the corresponding timer count value: T cpu =T X+N -T X , T X =X*T tick +T X,timer / T timer , T X+N =(X+N)*T tick +T X+N,timer / T timer , In the above formula, T cpu Indicates the actual total CPU running time in the current statistical period, T X Indicates the time corresponding to the first Tick interrupt after receiving the CPU usage statistics command, T X+N Indicates the time corresponding to the N+1th Tick interrupt after receiving the CPU occupancy statistics command, X indicates the first Tick interrupt count value after receiving the CPU occupancy statistics command, T tick Indicates the Tick interrupt cycle, T X,timer Indicates the timer count value corresponding to the first Tick interrupt generated after receiving the CPU occupancy statistics command, T X+N,timer Indicates the timer count value corresponding to the time when the N+1th Tick interrupt occurs after the CPU usage statistics command is received.
7. The method according to claim 6, characterized in that The CPU usage of each task in the current statistical period is calculated using the following formula based on the corrected cumulative running time of each task in the current statistical period and the actual total CPU running time in the current statistical period: In the above formula, a represents the CPU usage of a single task in the current statistical period.
8. An electronic device, characterized in that: The method comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the steps of the CPU occupancy statistics method according to any one of claims 1 to 7 are implemented.
9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the CPU occupancy statistics method according to any one of claims 1 to 7 are implemented.
Citation Information
Cited By
Method and device for calculating load rate of autonomous controllable direct current control protection device, and medium
CN120955596A