A thread-based program execution process analysis method and system
Through a thread-based program execution process analysis method, using eBPF to intercept sched_switch events and visualization modules, the duration of system calls is analyzed, which solves the problem of insufficient understanding of the program execution process in the existing technology and achieves the effect of program optimization.
Patent Information
- Application Number
- CN202211651466.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-21
- Publication Date
- 2025-09-05
- Estimated Expiration
- 2042-12-21
AI Technical Summary
Existing technologies make it difficult to fully understand the program execution process, especially the slow execution caused by dependency library vulnerabilities or improper interfaces. Code-level analysis is not sufficient to optimize the program.
A thread-based program execution process profiling method obtains thread context switch time and system calls, classifies and summarizes the duration of system calls, uses eBPF to intercept sched_switch events, and combines it with a visualization module to display the profiling results.
By analyzing the duration of system calls, it helps developers understand the program execution process, identify performance bottlenecks, and optimize program execution efficiency.
Smart Images

Figure CN115774612B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a thread-based program execution process analysis method and system. Background Art
[0002] A program is a sequence of instructions designed to solve a specific problem. Each instruction in a program specifies a set of basic operations that the machine must perform. Currently, understanding the program execution process primarily focuses on understanding the program code. However, program code execution involves the collaborative work of multiple services or components, such as calling dependent libraries. This process can lead to library vulnerabilities or improper use of library interfaces, resulting in slow execution. Understanding program execution speed solely at the code level cannot help developers fully understand the program execution process, so understanding the code level is of limited help in program optimization. Therefore, a method for analyzing the program execution process is needed. Summary of the Invention
[0003] In view of the above technical problems existing in the prior art, the present invention provides a method and system for analyzing program execution process based on threads, which analyzes the duration of each system call during program execution based on threads.
[0004] The present invention discloses a method for analyzing a program execution process based on threads, the method comprising: obtaining the time of thread context switching and a first thread number during the program execution process; obtaining the last system call of the context switch based on the first thread number; and obtaining the duration of the system call based on the time of two context switches after the system call.
[0005] Preferably, the method of analyzing according to the type of system call includes:
[0006] Classifying the system call to obtain the type of the system call;
[0007] The durations of the system calls are summarized according to the types to obtain the total durations of the various system call types.
[0008] Preferably, the system call includes any one of the following types or a combination thereof: input and output class, network class, file class, system suspension class and execution class.
[0009] Preferably, the program execution process is analyzed using the threshold and the total duration to obtain the factors affecting the program execution duration.
[0010] Preferably, the total duration is used to analyze the program execution process to obtain the factors affecting the program execution duration.
[0011] Preferably, the sched_switch event is intercepted based on eBPF to obtain the context switch time and the first thread number; and the system call is obtained based on eBPF.
[0012] Preferably, a second thread number and an execution log of the interception program are obtained, and the second thread number, the execution log and the first thread number are associated.
[0013] Preferably, the system call includes a network call, and the duration of the DNS query is obtained according to two context switching times after the network call.
[0014] The present invention also provides a system for implementing the above-mentioned program execution process analysis method, including an interception module and a system call analysis module, wherein the interception module is used to obtain the time of thread context switching and the first thread number, and obtain the last system call of the context switch based on the first thread number; the system call analysis module is used to obtain the duration of the system call based on the time of two context switches after the system call; and based on the duration, the program execution process is analyzed.
[0015] Preferably, the system further comprises a code parsing module, and the code parsing module is used to interpret the code of the program execution event in the CPU.
[0016] Preferably, the system further comprises a visualization module, and the visualization module is used to display the duration or total duration of each system call or type during the program execution process.
[0017] Compared with the prior art, the present invention has the following beneficial effects:
[0018] The first context switch of the current thread after a system call allows the CPU to execute other threads. The second context switch allows the system to start executing the current thread. The time difference between the two context switches reflects the duration of the system call, and the duration of each system call has a significant impact on the total execution time of the program. By analyzing the duration of system calls during program execution, it is beneficial to understand the program at the execution level and optimize the program execution process. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 It is a flow chart of the thread-based program execution process analysis method of the present invention;
[0020] Figure 2 It is a system logic block diagram of the present invention. DETAILED DESCRIPTION
[0021] To make the objectives, technical solutions, and advantages of the embodiments of the present invention more clear, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are 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 making creative efforts shall fall within the scope of protection of the present invention.
[0022] The present invention will be described in further detail below with reference to the accompanying drawings:
[0023] A thread-based program execution process analysis method, such as Figure 1 As shown, the method includes:
[0024] Step 101: Obtain the time and first thread number of a thread context switch during program execution. The time and first thread number of the context switch can be obtained by intercepting a sched_switch event based on eBPF.
[0025] Step 102: According to the first thread number, obtain the last system call of the context switch. The system call can be obtained based on eBPF, such as read, write, network call, etc. The system call is used as the reason for the thread switch.
[0026] Step 103: Obtain the duration of the system call based on the time between the two context switches after the system call; and analyze the program execution process based on the duration. During the program execution process, excessive system load should be avoided to reduce passive context switches and improve the accuracy of the analysis.
[0027] The first context switch of the current thread after a system call occurs when the system call needs to wait for resources, causing the CPU to execute other threads. The second context switch occurs when the system starts executing the current thread. The time difference between the two context switches reflects the duration of the system call, and the duration of each system call has a significant impact on the total execution time of the program. By analyzing the duration of system calls during program execution, it is beneficial to understand the program at the execution level and optimize the program execution process.
[0028] In step 103 , the time difference between the second switching time and the first switching time may be used as the duration, or the time difference between the second switching time and the system call may be used as the duration.
[0029] In step 103, the method for analyzing the system call type is as follows:
[0030] Step 201: classify the system call to obtain the type of the system call;
[0031] Step 202: Summarize the durations of the system calls according to the types to obtain the total durations of the various system call types.
[0032] In a specific embodiment, the following classification is adopted: read and write events are classified as input and output class (IO class); according to the file operator, if it is the socket class, it is the network class; according to the file operator, if it is the file class, it is the file class; for Futex, the event is classified as the system suspension class; for the execution event on the CPU, it is classified as the execution class (onCPU class), but not limited to this, for example, the epoll class.
[0033] The program execution process can also be analyzed using the utilization threshold and the total duration to identify factors affecting program execution time. If the duration of file reads exceeds the threshold, this is considered a factor affecting program execution time, and a warning is issued to the developer, prompting them to optimize file reads.
[0034] Example 1
[0035] Step 301: intercept the operating system's sched_switch through an eBPF-based interception program (trance) to obtain the thread CPU switch event and the switching time point. The thread CPU switch is caused by the program execution process actively giving up the CPU, or performing resource waiting operations such as network read and write, disk read and write, etc., which leads to thread switching.
[0036] The interceptor is modified to record the second thread number and log of the interceptor in each execution life cycle, and associate the second thread number with the first thread number. The interceptor is run multiple times during the execution of the main program, so it may have multiple thread numbers.
[0037] Step 302: For each thread, record the last system call, such as vfs.read, when the sched_switch event occurred. This allows you to determine the cause of the thread CPU switch. Because the operating system kernel schedules at the thread granularity, a thread switch must be caused by something happening to that thread. Understanding the cause of a CPU switch can help developers understand how a program executes. For example, if a VFS.read system call causes a program thread to switch from a CPU execution state to a resource wait state, the cause of the program thread switch can be determined.
[0038] However, existing technologies such as APM work at the application code layer and cannot detect kernel layer events. There is no way to know why the program switches from the CPU execution state to the CPU waiting state.
[0039] Step 303: Utilize the interception program to interpret the system call.
[0040] For example, during a thread's network call, the thread performing the network call performs DNS domain name resolution / query, resulting in a CPU switch. After the DNS is resolved, the CPU switches again, initiating a network call. By recording the time of the thread's CPU switch, you can determine the duration of the system call. For example, if the DNS domain name duration is long, you can confirm that the problem is caused by DNS resolution, not the network call, and has nothing to do with network quality, allowing you to directly identify the root cause of the problem.
[0041] Step 304: Use Perf_event to obtain the code interpretation of the program's CPU execution event; use Elasticsearch to display and issue an alarm on the duration of each system call in the program's technical process.
[0042] In a specific test, the duration of epoll is 1607.5ms, network class is 0.39ms, system suspend class is 3.79ms, execution class is 44.87ms, file class is 2305.56ms, and other class is 0.24ms. This shows that the epoll and file class system calls take up too much time (as a percentage of the total time) and should be optimized.
[0043] Example 2
[0044] A system for implementing the above program execution process analysis method, such as Figure 2 As shown, it includes an interception module 1 and a system call analysis module 2.
[0045] The interception module 1 is used to obtain the time of the thread context switch and the first thread number, and obtain the last system call of the context switch based on the first thread number; the system call analysis module 2 is used to obtain the duration of the system call based on the two context switch times after the system call; based on the duration, the program execution process is analyzed.
[0046] The system may further include a code parsing module 3 and a visualization module 4. The code parsing module 3 is used to interpret the code of the program execution event in the CPU. The visualization module 4 is used to display the duration or total duration of each system call or type during the program execution.
[0047] The above are merely preferred embodiments of the present invention and are not intended to limit the present invention. Those skilled in the art will readily appreciate that various modifications and variations of the present invention are possible. Any modifications, equivalent substitutions, or improvements made within the spirit and principles of the present invention shall be included within the scope of protection of the present invention.
Claims
1. A thread-based program execution process analysis method, characterized in that: The method comprises: Get the time of thread context switching and the first thread number during program execution; According to the first thread number, obtaining the last system call of the context switch; Obtaining the duration of the system call according to two context switch times after the system call; Classifying the system call to obtain the type of the system call; Summarize the duration of the system calls according to the types to obtain the total duration of each type; The total duration is used to analyze the program execution process and obtain the factors affecting the program execution duration.
2. The program execution process analysis method according to claim 1, characterized in that: The system call includes any one of the following types or a combination thereof: input and output type, network type, file type, system suspension type and execution type.
3. The program execution process analysis method according to claim 1, characterized in that: Based on eBPF, the sched_switch event is intercepted to obtain the context switch time and the first thread number; based on eBPF, the system call is obtained.
4. The program execution process analysis method according to claim 3, characterized in that: Intercept sched_switch events and system calls through interception programs; Obtain a second thread number and an execution log of the interception program, and associate the second thread number, the execution log, and the first thread number.
5. The program execution process analysis method according to claim 1, characterized in that: The system calls include network calls, The duration of the DNS query is obtained based on the two context switching times after the network call.
6. A system for executing the program execution process analysis method according to any one of claims 1 to 5, characterized in that: Including interception module and system call analysis module, The interception module is used to obtain the time of thread context switching and the first thread number, and obtain the last system call of the context switch according to the first thread number; The system call analysis module is used to obtain the duration of the system call according to the two context switching times after the system call; and analyze the program execution process based on the duration.
7. The system according to claim 6, characterized in that It also includes a code parsing module, which is used to interpret the code of the program CPU execution event.
8. The system according to claim 6, wherein: It also includes a visualization module, which is used to display the duration or total duration of each system call or type during program execution.
Citation Information
Patent Citations
Performance monitoring method and apparatus
CN107168844A
Operation monitoring method and device, electronic device and computer-readable storage medium
CN109324946A