Universal continuous performance analysis method and system based on ebpf stack backtracking

By using EBPF stack backtracking technology to collect and reconstruct function call chains to generate flame graphs, the problems of limited application scenarios and complex systems in existing technologies are solved. This enables cross-language performance profiling and reduces system overhead and complexity.

CN121387401APending Publication Date: 2026-01-23科来网络技术股份有限公司
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511627748.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-11-07
Publication Date
2026-01-23

AI Technical Summary

Technical Problem

Existing continuous performance profiling methods have limitations such as limited application scenarios, inability to be applied to programs written in different languages, the need to deploy multiple agent programs, increased system overhead, intrusiveness, and high system complexity.

Method used

By using EBPF stack backtracking technology, the Linux perf event system triggers the EBPF program at a certain frequency to collect call stack information on the CPU. The entire function call chain is backtracked from the currently executing kernel stack frame. Combined with the program's symbolic information, the function call chain is restored to a human-understandable form, and a flame graph is generated for performance profiling.

Benefits of technology

It enables program performance profiling applicable to all interpreted languages, eliminating the need for repeated deployment of agent programs, reducing system overhead, and making the system simple and non-intrusive.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121387401A_ABST
    Figure CN121387401A_ABST
Patent Text Reader

Abstract

The invention discloses a universal persistent performance analysis method and system based on ebpf stack backtracking, and the method comprises the steps: analyzing basic information corresponding to a user business program process based on a user mode program, and transmitting the basic information to a kernel mode program; based on a kernel mode program, triggering an ebpf program according to a preset sampling frequency through a perf event system of Linux, and collecting call stack information on a CPU (Central Processing Unit); according to the call stack information, backtracking from the currently executed kernel stack frame all the time to obtain a function call link; analyzing the function call link, mapping the function call link into a code function symbol, and storing the code function symbol; and generating a program performance overhead flame map within a preset time according to the sample data, analyzing process performance, and guiding optimization of program codes. The method is suitable for all interpreted language programs, the application scene is wider, each machine does not need to deploy multiple agents repeatedly, the system overhead is greatly reduced, only one agent program exists, and the system is simpler.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of application performance profiling technology, specifically to a general continuous performance profiling method and system based on EBPF stack backtracking. Background Technology

[0002] In a microservice architecture, developers need to maintain a massive number of service components, and different services may be written in different programming languages. When performance issues occur in online services, developers have to delve into the code and use various tools to debug with breakpoints in order to find the performance bottleneck code.

[0003] The existing patent with publication number CN117240695A collects thread scheduling information and stack information using an extended Berkeley Packet Filter (ebpf) to generate the CPU usage and off-CPU times of threads. It also requires dynamically modifying the bytecode of the Java program through a Java proxy to obtain the mapping between Java threads and Linux threads. Furthermore, it is suitable for observing Java programs.

[0004] The existing patent with publication number CN119292885A is generally applicable to compiled languages, such as C and C++, but cannot support interpreted languages.

[0005] The above-mentioned existing technologies have the following drawbacks:

[0006] (1) Limited application scenarios: The existing patent with publication number CN117240695A is only applicable to Java programs, and the existing patent with publication number CN119292885A is only applicable to the continuous analysis of compiled languages ​​such as C and C++, and cannot be extended to programs of other development languages.

[0007] (2) Performance issues: For different process spaces (such as k8s), the existing patent with publication number CN117240695A requires the deployment of multiple agent programs, which increases the system overhead.

[0008] (3) High complexity: The existing patent with publication number CN117240695A requires the integration of data from multiple proxy programs and dynamic modification of the Java bytecode of the user's business program. It is intrusive and the system is highly complex. Summary of the Invention

[0009] The technical problem this invention aims to solve is that existing continuous performance profiling methods suffer from limitations such as limited application scenarios, the need to deploy multiple agent programs for different process spaces, increased system overhead, poor performance, intrusiveness, and high system complexity. This invention provides a general continuous performance profiling method based on EBPF stack backtracking. It triggers the EBPF program at a certain frequency through the Linux perf event system, collects call stack information from the CPU, and backtracks the entire function call chain from the currently executing kernel stack frame. Finally, it combines this with the program's symbolic information to reconstruct a human-understandable function call chain. Based on the frequency of function occurrences in each process sampled over a period of time, a flame graph is generated for continuous process performance profiling. This solves the problems of the aforementioned existing methods.

[0010] This invention is achieved through the following technical solution:

[0011] In a first aspect, this invention provides a general continuous performance profiling method based on EBPF stack backtracking, the method comprising:

[0012] Based on user-space programs, the basic information corresponding to user business program processes is parsed and passed to kernel-space programs;

[0013] Based on kernel-mode programs, the ebpf program is triggered at a preset sampling frequency through the Linux perf event system, and the call stack information on the CPU is collected; based on the call stack information, the function call chain is obtained by tracing back from the currently executing kernel stack frame.

[0014] The function call chain is parsed, mapped to code function symbols, and stored; a flame graph of program performance overhead within a preset time period is generated based on sample data; and the process performance is analyzed based on the flame graph to guide the optimization of program code.

[0015] Furthermore, the basic information includes the memory layout of the process, the memory allocation of the process, and the corresponding language type; the language type includes compiled languages ​​and interpreted languages;

[0016] When the language type is an interpreted language, the parsed basic data also includes the structure of the corresponding threads and stack frames in the interpreter.

[0017] Furthermore, based on user-space programs, the basic information corresponding to the user business program process is parsed, including:

[0018] The backtracking table parsing process in compiled languages ​​involves the following steps:

[0019] Based on user-space programs, read user business program files;

[0020] Locate the .eh_frame section in the user's business application file according to the ELF specification;

[0021] Based on the .eh_frame segment, the basic information of each stack frame is parsed sequentially and recorded in the backtrace table;

[0022] After parsing, the basic information of the backtrace table is passed to the kernel-mode program;

[0023] Interpreted language parsing involves the following steps:

[0024] Find out the implementation logic and specifications of different versions of different interpreters, and how they are mapped to system threads;

[0025] According to the specification, the corresponding thread, stack frame, and stack layout are parsed from the interpreter, and a variable that records the address of the previous stack is also recorded.

[0026] Based on the language type and version information, the above parsing rules are passed to the kernel-mode program.

[0027] Furthermore, based on the kernel-mode program, the ebpf program is triggered at a preset sampling frequency through the Linux perf event system, and call stack information on the CPU is collected. Based on the call stack information, the function call chain is obtained by tracing back from the currently executing kernel stack frame, including:

[0028] Based on kernel-mode programs, configure the CPU clock of Linux's perf event system and mount the ebpf program, and configure the sampling frequency;

[0029] The EBPF program is triggered according to the sampling frequency, and the call stack information on the CPU is collected.

[0030] Based on the call stack information and the backtracking entry point, the address of the instruction being executed by the current process of the user's business program is obtained;

[0031] Based on the instruction address, the memory allocation information injected by the user-mode program, and the corresponding language type, the type of the currently executing program can be obtained;

[0032] Depending on the language type, different EBPF backtracking procedures are invoked to perform backtracking and obtain the function call chain.

[0033] Furthermore, depending on the language type, different EBPF backtracking procedures are invoked to perform backtracking and obtain the function call chain, including:

[0034] The backtracking process for compiled languages ​​includes:

[0035] A1, records the current instruction address into the function call chain;

[0036] A2, use the current instruction address to find the information of the current stack frame from the backtrace table;

[0037] A3. Calculate the memory address of the previous stack frame according to the backtracking rules of the current stack frame, and restore the register state of the previous stack frame; then return to step A1, and continue until all stack frame positions have been backtracked.

[0038] A4, the function call chain stores the code address of each call;

[0039] A5 returns the function call chain to the user-space program;

[0040] The backtracking process of interpreted languages ​​includes:

[0041] B1 retrieves the interpreter's thread information from the memory address corresponding to the kernel thread;

[0042] B2, retrieves the currently executing stack frame from the thread information;

[0043] B3 retrieves the currently executing code information from the stack frame and records it in the function call chain;

[0044] B4: Obtain the address of the previous stack frame from the stack frame, restore the previous stack frame, return to step B1, and continue until all stack frames have been parsed.

[0045] B5 returns the function call chain to the user-mode program.

[0046] Furthermore, the function call chain is parsed and mapped to code function symbols, including:

[0047] For compiled languages, each address in the function call chain is a line of code, and the following process is executed for each address:

[0048] Based on the information and addresses obtained from the memory layout parsing, locate the memory block where the currently running instruction is located and the mapped file;

[0049] Using the address and the base address of the corresponding memory block, we can obtain the offset of the code at that address in the file.

[0050] According to ELF rules, the file symbol table is read from the mapped file, and the corresponding symbol is found in the file symbol table according to the offset, so that the symbol information of the code can be restored and recorded.

[0051] For interpreted languages, the interpreter maintains a global symbol table. For each address in the function call chain, the interpreter retrieves the symbol information from the global symbol table based on that address and saves it.

[0052] Secondly, this invention also provides a general-purpose continuous performance profiling system based on EBPF stack backtracking, the system comprising:

[0053] The first parsing unit is used to parse the basic information corresponding to the user business program process based on the user-space program, and pass the basic information to the kernel-space program.

[0054] The backtracking unit is used to trigger the ebpf program according to a preset sampling frequency based on the kernel-mode program and through the Linux perf event system, and to collect the call stack information on the CPU; based on the call stack information, it traces back from the currently executing kernel stack frame to obtain the function call chain;

[0055] The second parsing unit is used to parse the function call chain, map it into code function symbols, and store them.

[0056] The program performance profiling unit is used to generate a program performance cost flame graph within a preset time period based on sample data. Based on the program performance cost flame graph, the process performance is analyzed to guide the optimization of program code.

[0057] Furthermore, the basic information includes the memory layout of the process, the memory allocation of the process, and the corresponding language type; the language type includes compiled languages ​​and interpreted languages;

[0058] When the language type is an interpreted language, the parsed basic data also includes the structure of the corresponding threads and stack frames in the interpreter.

[0059] Thirdly, the present invention provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the above-mentioned general continuous performance profiling method based on EBPF stack backtracking.

[0060] Fourthly, the present invention provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the aforementioned general continuous performance profiling method based on EBPF stack backtracking.

[0061] Compared with the prior art, the present invention has the following advantages and beneficial effects:

[0062] This invention presents a general continuous performance profiling method and system based on EBPF stack backtracking. The method is applicable to programs in all interpreted languages, has a wider range of applications, eliminates the need to repeatedly deploy multiple agents on each machine, greatly reduces system overhead, and only requires one agent program, making the system simpler. Attached Figure Description

[0063] The accompanying drawings, which are included to provide a further understanding of embodiments of the invention and form part of this application, do not constitute a limitation thereof. In the drawings:

[0064] Figure 1 This is a flowchart of the general continuous performance profiling method based on EBPF stack backtracking implemented in this invention;

[0065] Figure 2 This is a schematic diagram illustrating how the address information of each process is obtained and passed to the kernel-mode program according to the present invention.

[0066] Figure 3 This is a schematic diagram of the kernel-mode stack backtracking of the present invention;

[0067] Figure 4 This is a block diagram of the general continuous performance profiling system based on EBPF stack backtracking, as described in this invention. Detailed Implementation

[0068] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the embodiments and accompanying drawings. The illustrative embodiments and descriptions of this invention are only for explaining this invention and are not intended to limit this invention.

[0069] The core principle of this invention is as follows: The Linux perfEvent system triggers the Extended Berkeley Packet Filter (eBPF) program at a certain frequency to collect call stack information from the CPU. It traces the entire function call chain back from the currently executing kernel stack frame, and finally, combined with the program's symbolic information, reconstructs a human-understandable function call chain. User-mode and kernel-mode programs exchange data via an eBPF Map. Based on the frequency of function occurrences in each process sampled over a period of time, a flame graph is generated for continuous process performance profiling.

[0070] The agent program of this invention runs with root privileges (root privileges are the highest privileges in a Linux system, allowing access to information about all user-space processes). Each Linux system only needs to run one agent program to obtain information about all user business programs. Moreover, this invention has zero intrusion into user business programs, requiring no modification to any code or bytecode of the user programs.

[0071] The method of this invention is applicable to programs of all interpreted languages, with a wider range of application scenarios. Each machine does not need to repeatedly deploy multiple agents, greatly reducing system overhead, and there is only one agent program, making the system simpler.

[0072] Example 1

[0073] like Figure 1 As shown, this invention presents a general continuous performance profiling method based on EBPF stack backtracking, which includes:

[0074] Step 1: Based on the user-mode program, parse the basic information corresponding to the user business program process and pass the basic information to the kernel-mode program. The basic information includes the memory layout of the process, the memory allocation of the process, and the corresponding language type. The language type includes compiled languages ​​and interpreted languages. When the language type is an interpreted language, the parsed basic data also includes the structure of the threads and stack frames in the corresponding interpreter.

[0075] Specifically, the process of parsing the memory layout in the basic information is as follows:

[0076] When a Linux system executes a user application, it first loads the application's code into memory. The system also maintains a memory mapping file that indicates which memory addresses correspond to the instruction information of a specific file. By reading this memory mapping file, ` / proc / {process_id} / maps`, we can determine which memory addresses a process uses and which file each memory address corresponds to.

[0077] This invention obtains the address information of each process and passes it to the kernel-mode program. For example... Figure 2 As shown.

[0078] Specifically, the parsing process for the programming language type in the basic information is as follows:

[0079] This invention attempts to parse the executable file / proc / process ID / exe of each process according to the specifications of different programming languages. If successful, it can determine the programming language type and version information of the process.

[0080] It should be noted that the process of code execution is to switch from one piece of code A to another piece of code B, and after B is executed, it will return to A. Each piece of code in the program is called a function. When this function is executed by the Linux system, it is called a stack frame at the Linux system level, and the place where these stack frames are stored is called the stack.

[0081] In this embodiment, backtracking in a compiled language requires knowing "which function called the current function, and which function called it from the previous function," i.e., the complete call chain (e.g., main() → A() → B() → current function). The backtracking table records how to find the previous calling function from the current function.

[0082] If the user application has frame pointer enabled, then when tracing back each stack frame, you only need to read the value address of the frame pointer register to obtain the information of the previous stack frame.

[0083] If the user application does not enable frame pointers, it is necessary to parse the .eh_frame information in the user application file to construct it. The .eh_frame records the stack frame information of each function. The .eh_frame (Exception HandlingFrame) refers to the exception handling frame segment, which is a region in the ELF file used to store the unrolling information of the function call stack, so that the program can correctly perform stack backtracking and resource cleanup when an exception occurs.

[0084] Therefore, the specific steps for parsing backtrack tables in compiled languages ​​include:

[0085] Based on user-space programs, read user business program files;

[0086] Locate the .eh_frame section in the user's business application file according to the ELF specification; ELF (Executable and Linkable Format) refers to an executable and linkable format.

[0087] Based on the .eh_frame segment, the basic information of each stack frame is parsed sequentially and recorded in the backtrace table;

[0088] After parsing, the basic information of the backtrace table is passed to the kernel program.

[0089] In this embodiment, the code of the compiled language is executed directly by the CPU, while the code of the interpreted language needs to be translated into CPU-executable instructions by an intermediate interpreter.

[0090] Therefore, we need to pay attention to how the intermediate interpreter translates and executes the code.

[0091] The interpreter has its own stack frames and stack, and the translation and execution of code is accomplished by mapping the interpreter's threads, stack frames, and stack to system-level threads.

[0092] Therefore, the specific steps of interpretable language parsing include:

[0093] Find out the implementation logic and specifications of different versions of different interpreters, and how they are mapped to system threads;

[0094] According to the specification, the corresponding thread, stack frame, and stack layout are parsed from the interpreter, and a variable that records the address of the previous stack is also recorded.

[0095] Based on the language type and version information, the above parsing rules are passed to the kernel-mode program.

[0096] For example, examining the code and design specifications of the Python 3.10 interpreter reveals that Python threads are defined as PyThreadState, stack frames as PyFrameObject, and the offsets of various variables within the object are also specified. When analyzing a Python program, these parsing rules are simply passed to the kernel program, which then performs the analysis according to these rules.

[0097] Step 2: Based on the kernel-mode program, the ebpf program is triggered at a preset sampling frequency through the Linux perf event system, and the call stack information on the CPU is collected; based on the call stack information, the function call chain is obtained by tracing back from the currently executing kernel stack frame.

[0098] In this embodiment, step 2 specifically includes:

[0099] Based on kernel-mode programs, configure the CPU clock of Linux's perf event system and mount the ebpf program, and configure the sampling frequency;

[0100] The EBPF program is triggered according to the sampling frequency, and the call stack information on the CPU is collected.

[0101] Based on the call stack information and the backtracking entry point, the address of the instruction being executed by the current process of the user's business program is obtained;

[0102] Based on the instruction address, the memory allocation information injected by the user-mode program, and the corresponding language type, the type of the currently executing program can be obtained;

[0103] Depending on the language type, different EBPF backtracking procedures are invoked to perform backtracking and obtain the function call chain.

[0104] In the above technical solution, before backtracking, we register a kernel ebpf program (unwinder) with the kernel for backtracking different types of languages. The kernel-mode ebpf entry program is mounted through the CPU clock of the Linux perfEvent system, and a certain sampling frequency is configured (e.g., 20 times / s). In this way, the entry program for ebpf stack sampling and backtracking will be triggered 20 times per second.

[0105] Backtracking at the beginning:

[0106] 1. At the entry point, we can obtain the instruction address (PC) of the user business program currently being executed by the CPU, the current register state, stack frame information and the corresponding process ID. Combined with the information obtained from user-mode parsing, we can know what type of language program is being run.

[0107] 2. Depending on the language, we call the corresponding unwinder to perform the backtracking. Specifically, the backtracking process is as follows:

[0108] (1) The backtracking process of compiled languages ​​includes:

[0109] A1, records the current instruction address into the function call chain;

[0110] A2, use the current instruction address to find the information of the current stack frame from the backtrace table;

[0111] A3. Calculate the memory address of the previous stack frame according to the backtracking rules of the current stack frame, and restore the register state of the previous stack frame; then return to step A1, and continue until all stack frame positions have been backtracked.

[0112] A4, the function call chain stores the code address of each call;

[0113] A5 returns the function call chain to the user-space program;

[0114] (2) The backtracking process of interpreted languages ​​includes:

[0115] When an interpreted language is detected, the following is executed according to the parsing rules passed to the kernel program:

[0116] B1 retrieves the interpreter's thread information from the memory address corresponding to the kernel thread;

[0117] B2, retrieves the currently executing stack frame from the thread information;

[0118] B3 retrieves the currently executing code information from the stack frame and records it in the function call chain;

[0119] B4: Obtain the address of the previous stack frame from the stack frame, restore the previous stack frame, return to step B1, and continue until all stack frames have been parsed.

[0120] B5 returns the function call chain to the user-mode program.

[0121] Based on the address of the instruction being executed by the user's current process, the memory allocation information injected into user space, and the corresponding language type, the type of the currently executing program is determined. Different EBPF backtracking procedures are then invoked for different language types to perform backtracking. For example... Figure 3 As shown.

[0122] Step 3: Parse the function call chain, map it to code function symbols, and store them;

[0123] In this embodiment, parsing the function call chain and mapping it to code function symbols includes:

[0124] (1) For compiled languages, each address in the function call chain is a line of code, and the following process is executed for each address:

[0125] a) Based on the information and addresses obtained from memory layout parsing, locate which memory block the currently running instruction is located in and which file it is mapped to;

[0126] b) Using the address and the base address of the corresponding memory block, obtain the offset of the code corresponding to that address in the file;

[0127] c) According to ELF rules, read the file symbol table .dynsym from the mapped file, and find the corresponding symbol from the file symbol table according to the offset, so as to restore the symbol information of the code and record it;

[0128] (2) For interpreted languages, the interpreter maintains a global symbol table. For each address in the function call chain (for different languages, the address may be an address, a file line number, or an offset), the interpreter can retrieve the symbol information from the global symbol table based on the address and save it.

[0129] In this embodiment, the collected symbolic data is stored for use in step 4.

[0130] Step 4: Generate a flame graph of program performance overhead within a preset time period based on the sample data. Analyze the process performance based on the flame graph, show the overhead percentage of each function, and guide the optimization of program code.

[0131] In practice, the backtracking process is illustrated below using a Python program as an example:

[0132] The execution of Python code relies on the underlying Python interpreter. Python code is compiled into bytecode, with each code block corresponding to a PyCodeObject object. CPython is a stack-based interpreter. It primarily uses a for loop implemented within the _PyEval_EvalFrameDefault function to call C code and execute the corresponding Python instructions one by one.

[0133] Each Python thread in CPython has a corresponding PyThreadState object, which is stored in the kernel thread's tsd (Thread-Specific Data). PyThreadState contains a reference to the currently executing Python stack frame (PyFrameObject), and each stack frame contains a reference to the previous stack frame (PyFrameObject->f_back). We can use this f_back to chain the entire Python call chain and implement stack backtracking.

[0134] In Python, each thread corresponds to a kernel thread, and code execution is ultimately performed on the CPU through the kernel thread. This invention proposes a method for continuous performance profiling of Python programs based on EBPF and implemented through stack backtracking.

[0135] First, by parsing the Python executable file in user space, the data information needed for stack backtracking is extracted and passed to kernel space via ebpf Map.

[0136] The information required for stack backtracking includes the following:

[0137] The base address of the kernel thread (obtained via task_struct->thread.fsbase).

[0138] The offset of the kernel TSD memory address relative to the thread base address (tsd_offset).

[0139] The fixed size of each object in a TSD (tsd_item_size).

[0140] The location of PyThreadState in CPython's tsd (py_thread_tsd_index).

[0141] The offset of PyFrameObject in PyThreadState (py_frame_offset).

[0142] Offsets of various properties in PyFrameObject.

[0143] In CPython, the starting address of the _PyEval_EvalFrameDefault function corresponds to the address range.

[0144] Second, configure the CPU clock of the Linux perfEvent system and mount the ebpf program, and configure a certain sampling frequency (e.g., 20 times / s). This way, the ebpf stack acquisition backtracking program will be triggered 20 times per second.

[0145] When the eBPF entry point is triggered, C code is executed. We check if the address of the current register PC instruction is within the range. If the range is 0x12345000 to 0x12347000, this range information tells the eBPF program that if the PC address falls between 0x12345000 and 0x12347000, the program is executing Python bytecode and needs to use the Python interpreter's stack unrolling logic. If it's not within the range, it's code of another language type, and the unrolled function of that language is executed via an EBPF tail call. (EBPF tail calls are a calling mechanism of EBPF. Because there is a limit to the number of instructions that can be loaded into the kernel (4096 in older systems, increased to 1 million in kernel 5.1 and later), the call stack of an eBPF program cannot be too deep, or the code logic of an eBPF program cannot be too long, otherwise it cannot be loaded. Tail calls can circumvent this limitation.)

[0146] If the expansion of another language (C language) has finished, the Python code will begin executing the Python expansion logic:

[0147] 1) Get the current task_struct structure and get the thread base address: thread_base = task_struct->thread.fsbase.

[0148] 2) Get the address of PyThreadState: py_thread_add = thread_base + tsd_offset.

[0149] 3) Obtain the address of PyFrameObject: py_frame = py_thread_add + py_frame_offset; Now that we have PyFrameObject, we can perform stack backtracking.

[0150] 4) Stack backtracking, code execution is shown in Table 1.

[0151] Table 1

[0152]

[0153] This completes the kernel-mode stack backtracking. After the stack backtracking is complete, the current call stack (including Python stack frames and C code stack frames) is pushed to the user-space program for symbol resolution.

[0154] Third, symbol analysis

[0155] The purpose of symbol table resolution is to map instruction information to the symbol table, and ultimately find the function and line number in the file corresponding to the code.

[0156] The parsing process of a Python stack frame involves obtaining a `py_codeobject`. A `py_codeobject` typically represents a function or object and contains the name of the code block, the mapping between instructions and source file line numbers. We use the `py_codeobject` of the `Frame` to parse the information needed for this code block, and then use `lineno` to parse the line number of the current code corresponding to the current frame in user space. After parsing, this `py_codeobject` is saved and can be used directly later without further parsing.

[0157] The parsing of C stack frames is achieved through the address-to-function symbol mapping described above.

[0158] Fourth, data storage and analysis

[0159] By storing the collected data, we can generate a flame graph of program performance overhead over a period of time, showing the overhead percentage of each function, thereby guiding the optimization of program code.

[0160] The above method is illustrated using Python as an example. For other interpreted languages, the same approach is used to map kernel threads to user threads, parse the corresponding stack and thread layout, and implement stack backtracking and symbolic resolution using symbolic information.

[0161] This invention enables performance profiling that supports cross-language calls, such as Python calling C code; it also supports performance profiling for interpreted and compiled languages. Furthermore, it requires no additional modifications or code insertion into the application; simply running an agent program on the system allows for the collection and continuous profiling of all processes without any intrusion.

[0162] Example 2

[0163] like Figure 4 As shown, the difference between this embodiment and Embodiment 1 is that this embodiment provides a general continuous performance profiling system based on EBPF stack backtracking. This system corresponds one-to-one with the general continuous performance profiling method based on EBPF stack backtracking in Embodiment 1. The system includes:

[0164] The first parsing unit is used to parse the basic information corresponding to the user business program process based on the user-space program, and pass the basic information to the kernel-space program.

[0165] The backtracking unit is used to trigger the ebpf program according to a preset sampling frequency based on the kernel-mode program and through the Linux perf event system, and to collect the call stack information on the CPU; based on the call stack information, it traces back from the currently executing kernel stack frame to obtain the function call chain;

[0166] The second parsing unit is used to parse the function call chain, map it into code function symbols, and store them.

[0167] The program performance profiling unit is used to generate a program performance cost flame graph within a preset time period based on sample data. Based on the program performance cost flame graph, the process performance is analyzed to guide the optimization of program code.

[0168] As a further implementation, the basic information includes the memory layout of the process, the memory allocation of the process, and the corresponding language type; the language type includes compiled languages ​​and interpreted languages;

[0169] When the language type is an interpreted language, the parsed basic data also includes the structure of the corresponding threads and stack frames in the interpreter.

[0170] The execution process of each unit can be carried out according to the general continuous performance profiling method based on EBPF stack backtracking in Example 1, and will not be described in detail in this example.

[0171] Meanwhile, the present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the above-mentioned general continuous performance profiling method based on EBPF stack backtracking.

[0172] Meanwhile, the present invention also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the aforementioned general continuous performance profiling method based on EBPF stack backtracking.

[0173] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0174] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0175] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0176] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0177] The specific embodiments described above further illustrate the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

Claims

1. A general continuous performance profiling method based on EBPF stack backtracking, characterized in that, The method includes: Based on the user-space program, the basic information corresponding to the user business program process is parsed and the basic information is passed to the kernel-space program; Based on kernel-mode programs, the ebpf program is triggered at a preset sampling frequency through the Linux perf event system, and the call stack information on the CPU is collected; based on the call stack information, the function call chain is obtained by tracing back from the currently executing kernel stack frame. The function call chain is parsed, mapped to code function symbols, and stored; a flame graph of program performance overhead within a preset time period is generated based on sample data; and the process performance is analyzed based on the flame graph to guide the optimization of program code.

2. The general continuous performance profiling method based on EBPF stack backtracking as described in claim 1, characterized in that, The basic information includes the memory layout of the process, the memory allocation of the process, and the corresponding language type; the language type includes compiled languages ​​and interpreted languages. When the language type is an interpreted language, the parsed basic data also includes the structure of the corresponding threads and stack frames in the interpreter.

3. The general continuous performance profiling method based on EBPF stack backtracking as described in claim 1, characterized in that, Based on user-space programs, the basic information corresponding to the user business program process is parsed, including: The backtracking table parsing process in compiled languages ​​involves the following steps: Based on user-space programs, read user business program files; Locate the .eh_frame section in the user service program file according to the ELF specification; Based on the .eh_frame segment, the basic information of each stack frame is parsed sequentially and recorded in the backtrace table; After parsing, the basic information of the backtrace table is passed to the kernel-mode program; Interpreted language parsing involves the following steps: Find out the implementation logic and specifications of different versions of different interpreters, and how they are mapped to system threads; According to the specification, the corresponding thread, stack frame, and stack layout are parsed from the interpreter, and a variable that records the address of the previous stack is also recorded. Based on the language type and version information, the above parsing rules are passed to the kernel-mode program.

4. The general continuous performance profiling method based on EBPF stack backtracking as described in claim 1, characterized in that, Based on kernel-mode programs, the ebpf program is triggered at a preset sampling frequency through Linux's perf event system, and call stack information on the CPU is collected. Based on the call stack information, the function call chain is obtained by tracing back from the currently executing kernel stack frame, including: Based on kernel-mode programs, configure the CPU clock of Linux's perf event system and mount the ebpf program, and configure the sampling frequency; The EBPF program is triggered according to the sampling frequency, and the call stack information on the CPU is collected. Based on the call stack information, the address of the instruction being executed by the current process of the user's business program is obtained according to the backtracking entry point; Based on the instruction address, the memory allocation information injected by the user-mode program, and the corresponding language type, the type of the currently executing program is obtained; Depending on the language type, different EBPF backtracking procedures are invoked to perform backtracking and obtain the function call chain.

5. The general continuous performance profiling method based on EBPF stack backtracking as described in claim 4, characterized in that, Depending on the language type, different EBPF backtracking procedures are invoked to perform backtracking and obtain the function call chain, including: The backtracking process for compiled languages ​​includes: A1, records the current instruction address into the function call chain; A2, use the current instruction address to find the information of the current stack frame from the backtrace table; A3. Calculate the memory address of the previous stack frame according to the backtracking rules of the current stack frame, and restore the register state of the previous stack frame; then return to step A1, and continue until all stack frame positions have been backtracked. A4, the function call chain stores the code address of each call; A5 returns the function call chain to the user-space program; The backtracking process of interpreted languages ​​includes: B1 retrieves the interpreter's thread information from the memory address corresponding to the kernel thread; B2, retrieves the currently executing stack frame from the thread information; B3 retrieves the currently executing code information from the stack frame and records it in the function call chain; B4: Obtain the address of the previous stack frame from the stack frame, restore the previous stack frame, return to step B1, and continue until all stack frames have been parsed. B5 returns the function call chain to the user-mode program.

6. The general continuous performance profiling method based on EBPF stack backtracking as described in claim 1, characterized in that, The function call chain is parsed and mapped to code function symbols, including: For compiled languages, each address in the function call chain is a line of code, and the following process is executed for each address: Based on the information and addresses obtained from the memory layout parsing, locate the memory block where the currently running instruction is located and the mapped file; Using the address and the base address of the corresponding memory block, we can obtain the offset of the code at that address in the file. According to ELF rules, the file symbol table is read from the mapped file, and the corresponding symbol is found in the file symbol table according to the offset, so that the symbol information of the code can be restored and recorded. For interpreted languages, the interpreter maintains a global symbol table. For each address in the function call chain, the interpreter retrieves the symbol information from the global symbol table based on that address and saves it.

7. A general-purpose continuous performance profiling system based on EBPF stack backtracking, characterized in that, The system includes: The first parsing unit is used to parse the basic information corresponding to the user business program process based on the user-mode program, and pass the basic information to the kernel-mode program. The backtracking unit is used to trigger the ebpf program according to a preset sampling frequency based on the kernel-mode program and through the Linux perf event system, and to collect the call stack information on the CPU; based on the call stack information, it backtracks from the currently executing kernel stack frame to obtain the function call chain; The second parsing unit is used to parse the function call chain, map it into code function symbols, and store them; The program performance profiling unit is used to generate a program performance cost flame graph within a preset time period based on sample data. Based on the program performance cost flame graph, the process performance is analyzed to guide the optimization of program code.

8. The general continuous performance profiling method based on EBPF stack backtracking as described in claim 7, characterized in that, The basic information includes the memory layout of the process, the memory allocation of the process, and the corresponding language type; the language type includes compiled languages ​​and interpreted languages. When the language type is an interpreted language, the parsed basic data also includes the structure of the corresponding threads and stack frames in the interpreter.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the general continuous performance profiling method based on EBPF stack backtracking as described in any one of claims 1 to 6.

10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by the processor, it implements the general continuous performance profiling method based on EBPF stack backtracking as described in any one of claims 1 to 6.

Citation Information

Patent Citations

  • Continuous performance analysis method and device based on eBPF, electronic equipment and storage medium

    CN117240695A

  • Performance analysis method, electronic equipment, storage medium and program product

    CN119292885A