RTOS inert processor context switching method

Through software technology, the register information in the embedded system is obtained and managed, and the context switching of RTOS lazy processors is realized, which solves the problem of low performance under the lack of hardware support, reduces hardware resource overhead and improves system performance.

CN119105844APending Publication Date: 2024-12-10CHIPEASY TECHNOLOGY (SHANGHAI) LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202310685980.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-06-09
Publication Date
2024-12-10

AI Technical Summary

Technical Problem

In embedded systems, especially on dedicated computing acceleration systems, when hardware support is lacking, the task context switching performance of RTOS is seriously affected, and adding dedicated hardware resources does not have the optimal solution.

Method used

Through software technology, the context switching method of RTOS lazy processor is realized, resource information other than general registers is obtained, and the storage and recovery functions are generated, the register information is stored using memory arrays, and the lazy context switching logic is added to the real-time operating system, and the dynamic transformation of saving is transformed. and restore operations.

Benefits of technology

Reduces chip size and hardware resource overhead, improves system performance, and avoids performance losses without hardware support.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119105844A_ABST
    Figure CN119105844A_ABST
Patent Text Reader

Abstract

The invention discloses an RTOS inert processor context switching method, and relates to the technical field of electric digital data processing. According to the RTOS inert processor context switching method provided by the invention, task analysis is carried out in a software mode, special hardware does not need to be used for supporting, and the chip size and hardware resource overhead can be reduced. Moreover, the inert context switching logic is added in the context switching function of the real-time operating system to read the content of the second array and realize the dynamic conversion of storage and recovery operations, so that the defect when no hardware support exists can be overcome, and the system performance is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to the technical field of electronic digital data processing, in particular to a context switching method for an RTOS inert processor. Background Art

[0002] In embedded systems, especially dedicated computing acceleration systems, the central processing unit (CPU) will have relatively more register groups, such as vector, single instruction multiple data (SIMD), etc. If the system software is developed based on a real-time operating system (RTOS), these resources will be used as shared resources and need to be saved and restored every time the task context is switched.

[0003] There are two ways to do context switching in current embedded systems:

[0004] The first type: hardware-assisted lazy context switching. Usually, this type of hardware will provide some hardware resources to assist the software in context switching, so as to save system overhead.

[0005] The second type: On CPU systems without hardware assistance, since the operating system (OS) cannot know the modification of resources during software operation, it often uses indiscriminate context switching, which greatly affects system performance.

[0006] Based on the above description, the first case has the best effect and performance due to hardware support. However, there are also some disadvantages, that is, it consumes additional hardware resources. At the same time, in some systems, these modules used for computing acceleration are often only accessed by a few tasks. Adding dedicated hardware resources for support is obviously not the optimal solution for scenarios such as Yi embedded systems that have high PPA requirements. In the second case, due to the lack of hardware resources, the software design and implementation is often simple and crude, and context switches are performed when all context switches occur, which also seriously affects the performance of the entire system. Summary of the invention

[0007] In order to solve the above problems existing in the prior art, the present invention provides an RTOS lazy processor context switching method.

[0008] To achieve the above object, the present invention provides the following solutions:

[0009] A RTOS lazy processor context switching method is implemented based on software technology; the method comprises:

[0010] Based on the central processing unit used by the real-time operating system, resource information is obtained; the resource information is register information saved by the process when the context is switched except for the general registers in the current system;

[0011] Determine instruction information for modifying the resource information, and store the instruction information and the resource information in the same file;

[0012] Numbering each resource information and defining a macro; the macro is used to indicate the number of resource information of the central processing unit;

[0013] Generate a save function and a restore function for each register according to the instruction information, the register information number and the macro;

[0014] When the context is switched, the save function is used to save the value of the register of the current task;

[0015] When the task is about to be scheduled for execution, the restore function is used to restore the value of the register;

[0016] Determine the memory space and alignment parameters of each group of registers according to the resource information and the instruction information;

[0017] The memory space, the alignment parameter, the saved register value of the current task and the restored register value are stored in a first array, so that the first array contains data used by each group of register information when context switching and memory-related data;

[0018] Modify the stack allocation and setting function of the real-time operating system, and add logic to allocate memory corresponding to the resources for each task; the set function is used to read the content of the second array to find the usage information of the resources corresponding to the task;

[0019] Adding lazy context switching logic in the context switching function of the real-time operating system; the lazy context switching logic is used to read the second array content;

[0020] Based on the file storing the instruction information and the resource information, scanning the code paths of all tasks, determining the hardware resources used by the tasks to obtain analysis information, and storing the analysis information in the second array to obtain a new second array;

[0021] Using the new second array, return to execute "modify the real-time operating system stack allocation and setup function, and add logic to allocate memory of corresponding resources for each task" to generate an ELF file with context switch information.

[0022] Optionally, the resource information includes floating-point register information and vector register information.

[0023] Optionally, based on a central processing unit used by the real-time operating system, resource information is obtained, including:

[0024] Acquire a central processing unit library; the central processing unit library is designed by an EDA tool; extract all register information corresponding to the central processing unit used by the current system from the central processing unit library;

[0025] The information of registers other than general registers saved during context switching is extracted from all the extracted register information to obtain the resource information.

[0026] Optionally, the instruction information for modifying the resource information is determined by a system application performance standard evaluation criterion.

[0027] Optionally, the instruction information and the resource information are stored in the same json file.

[0028] Optionally, each element in the second array is used to record a bitmap table and an address pointer table; each bit in the bitmap table is used to indicate whether the task will modify the corresponding resources during operation; each address in the address pointer table is used to indicate the memory address of the corresponding resource information.

[0029] Optionally, in the process of the lazy context switching logic reading the content of the second array, when the corresponding value in the bitmap table in the task information is 1, a save action and a restore action corresponding to the resource information are performed;

[0030] When the corresponding value in the bitmap table in the task information is 0, the resource information stored in the first array is read, and the save function and the restore function are found based on the read resource information.

[0031] Optionally, based on a file storing the instruction information and the resource information, scanning code paths of all tasks, determining hardware resources used by the tasks to obtain analysis information includes:

[0032] First compile the real-time operating system and application code into an ELF file;

[0033] By scanning the functions for registering tasks in the code, all tasks in the real-time operating system are found, and the entry function of each task is found by analyzing the compiled ELF file;

[0034] Based on the entry function of each task and combined with the instruction information, all the calling paths of this task are found through C code scanning and analysis tools;

[0035] Parse the assembly code of all call paths, determine whether the relevant code path will modify all registers except general registers based on resource information, and record them to obtain analysis information.

[0036] A computer storage medium is used to store a computer program; the computer program is used to implement the RTOS lazy processor context switching method provided above.

[0037] According to the specific embodiments provided by the present invention, the present invention discloses the following technical effects:

[0038] The RTOS lazy processor context switching method provided by the present invention performs task analysis by software, does not need to use dedicated hardware to support, and can reduce chip volume and hardware resource overhead. In addition, the present invention adds lazy context switching logic to the context switching function of the real-time operating system to read the second array content and realize dynamic conversion of save and restore operations, which can solve the disadvantages when there is no hardware support and improve system performance. BRIEF DESCRIPTION OF THE DRAWINGS

[0039] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0040] Figure 1 The present invention provides a flowchart of the RTOS lazy processor context switching method. DETAILED DESCRIPTION

[0041] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions 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. Based on the embodiments of 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.

[0042] The purpose of the present invention is to provide an RTOS lazy processor context switching method, which can reduce chip size, reduce hardware resource overhead, solve the disadvantages when there is no hardware support, and improve system performance.

[0043] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.

[0044] The RTOS lazy processor context switching method provided by the present invention is implemented based on software technology, such as Figure 1 As shown, the method includes:

[0045] Step 100: Based on the central processing unit used by the real-time operating system, obtain resource information. Resource information is the register information saved by the process when the context switches, except for the general registers in the current system. Resource information includes floating-point register information and vector register information. In this step, according to the actual situation of the chip, the listed resource information can be saved to the corresponding file. Among them, the enumeration method can be manual enumeration or automatic extraction. Generally, the automatic extraction method is suitable for CPUs designed by EDA tools, such as the EDA tool based on the FARMC processor description language provided by Xin Yihui, which can find all register information through the corresponding CPU library. Based on this, when the automatic extraction method is adopted, the implementation process of this step can be:

[0046] Get the CPU library. The CPU library is designed by EDA tools.

[0047] Extract all register information corresponding to the CPU used by the current system from the CPU library.

[0048] The information of registers other than general registers saved during context switching is extracted from all the extracted register information to obtain resource information.

[0049] Step 101: Determine instruction information for modifying resource information, and store the instruction information and resource information in the same file. This file can be a json file, which is used as input information when scanning and analyzing the code path.

[0050] Among them, it is mainly to find all the instruction information that will modify the above resource content by checking the spec. Specifically, there are two ways to determine the instruction information: manual determination and automatic determination. Among them, the manual determination method is to check all the instructions from the chip manual to see whether the relevant instructions will modify the corresponding registers. The automatic determination method is to call the relevant interface through the function library provided by the EDA tool, analyze the operand (operation object) of the instruction, and determine whether the instruction will modify the register.

[0051] Step 102: Number each resource information and define a macro. This macro is recorded as CONTEXT_MODULE_CNT, which is used to indicate the number of resource information of the central processing unit. For example, a system has floating point registers and vector registers, plus general purpose registers, so there are a total of 3 groups of register resources on the CPU. General purpose registers (GPR) are required to be saved by all tasks, so the macro CONTEXT_MODULE_CNT is 2.

[0052] Step 103: Generate a save function and a restore function for each register according to the instruction information, the register information number and the macro. The instruction information, the register information number and the macro are the context resources that need to be saved and restored when the task is switched.

[0053] This step mainly involves writing the save function and restore function for each register based on the resource information and instruction information. For example, for the floating-point registers of riscv, the following two functions need to be written:

[0054] Function 1:

[0055] asm_func save_fpu_contexte

[0056] fsw f0,0(a0)

[0057] fsw f1,4(a0)

[0058] fsw f2,8(a0)

[0059] …

[0060] end_func save_fpu_context

[0061] Function 2:

[0062] asm_func restore_fpu_context

[0063] fld f0,0(a0)

[0064] fld f1,4(a0)

[0065] fld f2,8(a0)

[0066] end_func restore_fpu_context

[0067] Step 104: When the context is switched, a save function is used to save the value of the register of the current task.

[0068] Step 105: When the task is about to be scheduled for execution, a restore function is used to restore the value of the register.

[0069] Step 106: Determine the memory space and alignment parameters of each group of registers according to the resource information and instruction information.

[0070] Step 107: Storing the memory space, alignment parameters, the saved register values ​​of the current task and the restored register values ​​in a first array, so that the first array contains the data used by each group of register information when performing context switching and memory-related data.

[0071] Step 108: Modify the stack allocation and setting function of the real-time operating system, and add logic to allocate memory for corresponding resources for each task. The set function is used to read the content of the second array and find the resource usage information of the corresponding task. Among them, an empty array (i.e., the initial second array) is first defined to save the information of the task using resources. Assume that the name of this array is task_context_info. The content of this array will be modified in the following steps, and the empty array is to allow the entire code to compile. Based on this, each element in the second array is used to record a bitmap table and an address pointer table. Each bit in the bitmap table is used to indicate whether this task will modify the corresponding resource during operation. Each address in the address pointer table is used to represent the memory address of the corresponding resource information.

[0072] If the corresponding value in the bitmap is 0, it means that this resource does not need to be switched during task scheduling, and memory allocation is not required. The execution procedure of this step is as follows:

[0073] struct task_context_info{

[0074] unsigned charbitmap[CONTEXT_MODULE_CNT];

[0075] void**context_mem[CONTEXT_MODULE_CNT];

[0076] }

[0077] Step 109: Add lazy context switching logic to the context switching function of the real-time operating system. The lazy context switching logic is used to read the content of the second array.

[0078] In the process of the lazy context switching logic reading the content of the second array, when the corresponding value in the bitmap table in the task information is 1, a save action and a restore action corresponding to the resource information are performed.

[0079] When the corresponding value in the bitmap table in the task information is 0, the resource information stored in the first array is read, and the save function and the restore function are found based on the read resource information.

[0080] Step 110: Based on the file storing instruction information and resource information, scan the code paths of all tasks, determine the hardware resources used by the tasks to obtain analysis information, and store the analysis information in the second array to obtain a new second array.

[0081] Among them, based on the file storing instruction information and resource information, the code paths of all tasks are scanned to determine the hardware resources used by the tasks to obtain analysis information, including:

[0082] First compile the real-time operating system and application code into an ELF file.

[0083] By scanning the functions that register tasks in the code, all tasks in the real-time operating system can be found, and the entry function of each task can be found by analyzing the compiled ELF file.

[0084] Based on the entry function of each task and combined with the instruction information, all calling paths of this task are found through C code scanning and analysis tools.

[0085] Parse the assembly code of all call paths, determine whether the relevant code path will modify all registers except general registers based on resource information, and record them to obtain analysis information.

[0086] Step 111: modify the stack allocation and setting function of the real-time operating system so that the set function reads the new second array content and finds the resource usage information of the corresponding task.

[0087] The following is an explanation of the implementation process of the RTOS lazy processor context switching method provided by the present invention based on a specific implementation case. The implementation process of the implementation case is:

[0088] Step 1: Based on the central processing unit (CPU) used by the current system, list the resources that the process needs to save when switching contexts, in addition to general registers. According to the actual situation of the chip, list them and save them to the corresponding file. These resources usually include: floating-point registers, vector registers, etc. The enumeration method can be manual or automatically extracted. Generally, the automatic extraction method is suitable for CPUs designed by EDA tools, such as the EDA tool based on the FARMC processor description language provided by Xinyihui, which can find all register information through the corresponding CPU library.

[0089] Step 2: Based on the resource information obtained in step 1, check the spec. (international system application performance standard evaluation criteria) to find all the instruction information that will modify the above resource information. This instruction information includes: the name of the instruction and the opcode (operation code) of the instruction.

[0090] These instruction information needs to be stored in a file together with the register information found in step 1. This file can be a json file, which is used as input information for step 10 and is needed when scanning and analyzing the code path.

[0091] Step 3: Number each group of register information listed, and define a macro for the real-time operating system, recorded as CONTEXT_MODULE_CNT. This macro CONTEXT_MODULE_CNT indicates how many resource information the CPU has in total. For example, if a real-time operating system has floating-point registers, vector registers, and general registers, then there are three groups of register resources on the CPU. GPR is required to be saved by all tasks, so the macro CONTEXT_MODULE_CNT is 2.

[0092] Step 4: Based on the instruction information, register information number and macro (context resources that need to be saved and restored when the task is switched) obtained in steps 2 and 3, write the save function and restore function for each (or each group of) registers.

[0093] The save function is used to save the register value of the current task when the context is switched, and the restore function is used to restore the register value when the task is about to be scheduled for execution.

[0094] Step 5: Based on the information obtained in Step 2 and Step 3, calculate the memory space size and alignment parameters required for each group of registers. The special term for describing registers in the CPU is called register file. The register file contains the size of each register and how many registers this register file contains. The calculation method is to multiply the number of registers by the size of each register.

[0095] Step 6: The information collected in steps 4 and 5 is stored in an array (i.e., the first array). This array contains the data required for each group of register resources when performing context switching and the required memory-related data.

[0096] Step 7: Modify the stack allocation and setting function of the real-time operating system, and add logic to allocate memory for the corresponding resources for each task. This function will read the array content defined in step 11 (i.e., the second array content) to find the usage information of the resources corresponding to the task. If the corresponding bitmap is 0, it means that this resource does not need to be switched during task scheduling, and memory allocation is not required.

[0097] Step 8: Add lazy context switching logic to the system context switching function in the operating system (OS). This logic reads the array information saved in step 11. If the corresponding value in the bitmap table in the task information is 1, then the corresponding save and restore actions are performed. If it is 0, the corresponding resource switching process does not need to be performed. The save and restore functions can be found by reading the resource information saved in step 6.

[0098] Step 9: First define an empty array to store the information about the resources used by the task. Assume that the name of this array (i.e. the initial second array) is task_context_info. The content of this array will be modified in step 10. The empty array is to allow the entire code to compile. Each element in the array needs to record the following information:

[0099] A bitmap table, where each bit in the table indicates whether the task will modify the corresponding resource during execution.

[0100] An address pointer table, each address in this table represents the memory address of the corresponding resource.

[0101] Step 10: Scan the code paths of all tasks to determine which hardware resources the tasks will use. This step can be broken down into the following steps:

[0102] Step 10-1: First compile the RTOS and application code into a complete ELF file.

[0103] Step 10-2: Find all the tasks in the system by scanning the function for registering tasks in the code (each RTOS provides a way to register tasks), and find the entry function of each task by analyzing the ELF file.

[0104] Step 10-3: Based on the entry function of each task and the information modeled in step 2, find all the calling paths of this task through C code scanning and analysis related tools.

[0105] Step 10-4: Parse the assembly code of all paths, determine whether the relevant code path will modify all registers except general registers based on the resource information extracted in step 1, and record them.

[0106] Step 11: Store the information analyzed in step 10 into the task_context_info array.

[0107] Step 12: Recompile the code again using the new task_context_info array to generate an ELF file with real context switch information.

[0108] Based on this, the solution provided by the present invention has the following advantages compared with the prior art:

[0109] 1. Traditional lazy context switching mechanisms need to rely on hardware for assistance, but in embedded systems, especially dedicated computing systems, some specific CPU resources are often only used by one or two tasks, which can be analyzed by software without the need for dedicated hardware support. Based on this, the present invention is implemented based on software technology, which can reduce the chip size while reducing hardware resource overhead.

[0110] 2. In a system without hardware resource support, indiscriminate save and restore operations are often used when switching tasks. When the system frequently switches tasks, it will cause great performance loss. Based on this, the present invention can solve the disadvantages when there is no hardware support and improve system performance by realizing dynamic switching of save and restore.

[0111] In addition, when the above-mentioned RTOS lazy processor context switching method is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art or the part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory, random access memory, disk or optical disk and other media that can store program code.

[0112] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.

[0113] The principles and implementation methods of the present invention are described in this article using specific examples. The description of the above embodiments is only used to help understand the method and core idea of ​​the present invention. At the same time, for those skilled in the art, according to the idea of ​​the present invention, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as limiting the present invention.

Claims

1. A RTOS lazy processor context switching method, characterized in that: Based on software technology implementation; the method includes: Based on the central processing unit used by the real-time operating system, resource information is obtained; the resource information is register information saved by the process when the context is switched except for the general registers in the current system; Determine instruction information for modifying the resource information, and store the instruction information and the resource information in the same file; Numbering each resource information and defining a macro; the macro is used to indicate the number of resource information of the central processing unit; Generate a save function and a restore function for each register according to the instruction information, the register information number and the macro; When the context is switched, the save function is used to save the value of the register of the current task; When the task is about to be scheduled for execution, the restore function is used to restore the value of the register; Determine the memory space and alignment parameters of each group of registers according to the resource information and the instruction information; The memory space, the alignment parameter, the saved register value of the current task and the restored register value are stored in a first array, so that the first array contains data used by each group of register information when context switching and memory-related data; Modify the stack allocation and setting function of the real-time operating system, and add logic to allocate memory corresponding to the resources for each task; the set function is used to read the content of the second array to find the usage information of the resources corresponding to the task; Adding lazy context switching logic in the context switching function of the real-time operating system; the lazy context switching logic is used to read the second array content; When the corresponding value in the bitmap table of the task information is 1, the save action and restore action corresponding to the resource information are performed; When the corresponding value in the bitmap table in the task information is 0, the resource information stored in the first array is read, and a save function and a restore function are found based on the resource information read; Based on the file storing the instruction information and the resource information, scanning the code paths of all tasks, determining the hardware resources used by the tasks to obtain analysis information, and storing the analysis information in the second array to obtain a new second array; Using the new second array, return to execute "modify the real-time operating system stack allocation and setup function, and add logic to allocate memory of corresponding resources for each task" to generate an ELF file with context switch information.

2. The RTOS lazy processor context switching method according to claim 1, characterized in that: The resource information includes floating point register information and vector register information.

3. The RTOS lazy processor context switching method according to claim 2, characterized in that: Based on the CPU used by the real-time operating system, obtain resource information, including: Obtaining a central processing unit library; the central processing unit library is designed by an EDA tool; Extracting all register information corresponding to the central processor used by the current system from the central processor library; The information of registers other than general registers saved during context switching is extracted from all the extracted register information to obtain the resource information.

4. The RTOS lazy processor context switching method according to claim 1, characterized in that: The instruction information for modifying the resource information is determined by using the system application performance standard evaluation criteria.

5. The RTOS lazy processor context switching method according to claim 1, characterized in that: The instruction information and the resource information are stored in the same json file.

6. The RTOS lazy processor context switching method according to claim 1, characterized in that: Each element in the second array is used to record a bitmap table and an address pointer table; each bit in the bitmap table is used to indicate whether the task will modify the corresponding resource during operation; each address in the address pointer table is used to indicate the memory address of the corresponding resource information.

7. The RTOS lazy processor context switching method according to claim 6, characterized in that: In the process of the lazy context switching logic reading the content of the second array, when the corresponding value in the bitmap table in the task information is 1, a save action and a restore action corresponding to the resource information are performed; When the corresponding value in the bitmap table in the task information is 0, the resource information stored in the first array is read, and the save function and the restore function are found based on the read resource information.

8. The RTOS lazy processor context switching method according to claim 1, characterized in that: Based on the file storing the instruction information and the resource information, the code paths of all tasks are scanned to determine the hardware resources used by the tasks to obtain analysis information, including: First compile the real-time operating system and application code into an ELF file; By scanning the functions for registering tasks in the code, all tasks in the real-time operating system are found, and the entry function of each task is found by analyzing the compiled ELF file; Based on the entry function of each task and combined with the instruction information, all the calling paths of this task are found through C code scanning and analysis tools; Parse the assembly code of all call paths, determine whether the relevant code path will modify all registers except general registers based on resource information, and record them to obtain analysis information.

9. A computer storage medium, characterized in that Used to store computer programs; the computer programs are used to implement the RTOS lazy processor context switching method as described in any one of claims 1-8.

Citation Information

Cited By

  • Operating system scheduling method supporting RISC-V vector register resource dynamic management

    CN121957687A