Flame graph generation method and device, storage medium and electronic device
By sampling the execution time and scheduling process of the target thread on the processor, a flame diagram integrating the first time and the second time ratio is generated, solving the problem of single flame diagram information and achieving more accurate performance analysis.
Patent Information
- Application Number
- CN202510592235.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-08
- Publication Date
- 2025-09-05
- Estimated Expiration
- 2045-05-08
AI Technical Summary
Existing flame graphs have single information in complex distributed systems or multi-task environments, resulting in low accuracy and difficulty in comprehensively evaluating performance bottlenecks.
By sampling the execution time and scheduling process of the target thread on the processor, a flame map containing the first time proportion information and the second time proportion information is generated, and the two information is integrated to generate the target flame map, reflecting the performance delay characteristics of the thread in the execution and waiting states.
Improved the accuracy of flame graphs, enabling more accurate identification of delays caused by function calls or system status, helping to quickly locate performance bottlenecks.
Smart Images

Figure CN120104440B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to computer technology, and more specifically, to a flame graph generation method and device, a storage medium, and an electronic device. Background Art
[0002] Flame graphs are a widely used visualization method for analyzing the performance of modern computer systems, revealing the temporal distribution of system operating states. Currently, existing flame graphs often only cover a single dimension of temporal distribution. However, in complex distributed systems or multi-tasking environments, relying solely on single-dimensional analysis often makes it difficult to comprehensively assess performance bottlenecks.
[0003] Currently, no effective solution has been proposed to the technical problem that the flame graph in related technologies contains only a single piece of information, resulting in relatively low accuracy of the flame graph. Summary of the Invention
[0004] The embodiments of the present application provide a flame graph generation method and apparatus, a storage medium, and an electronic device to at least solve the technical problem in the related art that the flame graph contains a single piece of information, resulting in relatively low accuracy of the flame graph.
[0005] According to one aspect of an embodiment of the present application, a method for generating a flame graph is provided, comprising: when a target thread is running, sampling time information of the target thread when it is executed on a processor to obtain first time proportion information corresponding to the target thread; sampling time information of when the target thread is called out of and into the processor to obtain second time proportion information corresponding to the target thread; and generating a target flame graph based on the first time proportion information and the second time proportion information, wherein the target flame graph is used to analyze performance delay information of the target thread.
[0006] Furthermore, sampling time information of a target thread when it is executed on a processor to obtain first time proportion information corresponding to the target thread includes: sampling and processing a clock cycle event in the processor to obtain a plurality of first sampling point information; when sampling the clock cycle event, obtaining first call stack information corresponding to the target thread at the sampling moment; and obtaining the first time proportion information based on the plurality of first sampling point information and the first call stack information.
[0007] Furthermore, sampling time information of when the target thread is called out of and into the processor to obtain second time proportion information corresponding to the target thread includes: sampling and processing a process switching event in the processor to obtain a plurality of second sampling point information; when sampling the process switching event, obtaining second call stack information corresponding to the target thread at the sampling moment; and obtaining the second time proportion information based on the plurality of second sampling point information and the second call stack information.
[0008] Furthermore, generating a target flame graph based on the first time proportion information and the second time proportion information includes: generating a first flame graph corresponding to when the target thread is executed on the processor based on the first time proportion information; generating a second flame graph corresponding to when the target thread is called out of and called into the processor based on the second time proportion information; and generating the target flame graph based on the first flame graph and the second flame graph.
[0009] Furthermore, generating, based on the first time proportion information, a first flame graph corresponding to when the target thread is executed on the processor includes: determining a first total duration based on a start count and an end count of a clock cycle event in the processor; calculating, based on the first time proportion information and the first total duration, to obtain first execution duration information of a function in first call stack information corresponding to a first sampling point in the plurality of first sampling point information; and generating the first flame graph based on the first execution duration information and the first total duration.
[0010] Furthermore, generating, based on the second time proportion information, a second flame graph corresponding to when the target thread is called out of and into the processor includes: determining a second total duration based on a start time and an end time of a process switch event in the processor; calculating, based on the second time proportion information and the second total duration, to obtain second execution duration information of a function in second call stack information corresponding to a second sampling point in the plurality of second sampling point information; and generating the second flame graph based on the second execution duration information and the second total duration.
[0011] Furthermore, generating the target flame graph based on the first flame graph and the second flame graph includes: determining a matching relationship between multiple first call stacks in the first flame graph and multiple second call stacks in the second flame graph, wherein the matching relationship includes: a first matching relationship and a second matching relationship, the first matching relationship indicating that the first call stack is the same as the second call stack, and the second matching relationship indicating that some function call relationships in the first call stack are the same as those in the second call stack; based on the matching relationship, merging the first flame graph and the second flame graph to obtain the target flame graph.
[0012] Further, based on the matching relationship, merging the first flame graph and the second flame graph to obtain the target flame graph includes: if there is a first target call stack in the multiple first call stacks and a second target call stack in the multiple second call stacks has a first matching relationship, merging the execution time of the first function in the second target call stack into the corresponding position of the first target call stack, and setting the execution time corresponding to the second function of the second target call stack at the top of the first target call stack, the first function is a function other than the top layer in the second target call stack, and the second function is the top layer function in the second target call stack; if the multiple first call stacks have a first matching relationship, merging the execution time of the first function in the second target call stack into the corresponding position of the first target call stack, and setting the execution time corresponding to the second function of the second target call stack at the top layer of the first target call stack, If a first target call stack in a call stack has a second matching relationship with a second target call stack among the multiple second call stacks, the execution duration of a third function in the second target call stack is merged into a corresponding position in the first target call stack, and the execution duration of a fourth function in the second target call stack is added to the first flame graph according to the position of the fourth function in the second target call stack. The third function is a function in the second target call stack that has the same call relationship as a function in the first target call stack, and the fourth function is a function in the second target call stack other than the third function. After the merging is completed, the merged first flame graph is scaled to obtain the target flame graph.
[0013] According to another aspect of an embodiment of the present application, a flame graph generation method is also provided, including: receiving a flame graph generation request triggered by a client; in a cloud server, based on the flame graph generation request, when a target thread is running, sampling the time information of the target thread when executing on the processor to obtain first time proportion information corresponding to the target thread; sampling the time information of the target thread being called out of the processor and called into the processor to obtain second time proportion information corresponding to the target thread; generating a target flame graph based on the first time proportion information and the second time proportion information, wherein the target flame graph is used to analyze the performance delay information of the target thread; and returning the target flame graph to the client.
[0014] According to another aspect of an embodiment of the present application, a flame graph generation device is also provided, including: a first sampling unit, for sampling time information of the target thread when the target thread is running on the processor, and obtaining first time proportion information corresponding to the target thread; a second sampling unit, for sampling time information of the target thread being called out of the processor and called into the processor, and obtaining second time proportion information corresponding to the target thread; a generation unit, for generating a target flame graph based on the first time proportion information and the second time proportion information, wherein the target flame graph is used to analyze the performance delay information of the target thread.
[0015] Furthermore, the first sampling unit includes: a first sampling module, used to sample and process the clock cycle event in the processor to obtain multiple first sampling point information; a first acquisition module, used to obtain the first call stack information corresponding to the target thread at the sampling moment when sampling the clock cycle event; and a first determination module, used to obtain the first time proportion information based on the multiple first sampling point information and the first call stack information.
[0016] Furthermore, the second sampling unit includes: a second sampling module, used to sample and process the process switching event in the processor to obtain multiple second sampling point information; a second acquisition module, used to obtain second call stack information corresponding to the target thread at the sampling moment when sampling the process switching event; and a second determination module, used to obtain the second time proportion information based on the multiple second sampling point information and the second call stack information.
[0017] Furthermore, the generation unit includes: a first generation module, configured to generate, based on the first time proportion information, a first flame graph corresponding to when the target thread is executed on the processor; a second generation module, configured to generate, based on the second time proportion information, a second flame graph corresponding to when the target thread is called out of and into the processor; and a third generation module, configured to generate the target flame graph based on the first flame graph and the second flame graph.
[0018] Furthermore, the first generation module includes: a first determination submodule, configured to determine a first total duration based on a start count and an end count of a clock cycle event in the processor; a first calculation submodule, configured to perform calculation based on the first time proportion information and the first total duration to obtain first execution duration information of a function in the first call stack information corresponding to a first sampling point in the multiple first sampling point information; and a first generation submodule, configured to generate the first flame graph based on the first execution duration information and the first total duration.
[0019] Furthermore, the second generation module includes: a second determination submodule, configured to determine a second total duration based on the start time and end time of the process switching event in the processor; a second calculation submodule, configured to perform calculation based on the second time proportion information and the second total duration to obtain second execution duration information of a function in the second call stack information corresponding to a second sampling point in the plurality of second sampling point information; and a second generation submodule, configured to generate the second flame graph based on the second execution duration information and the second total duration.
[0020] Furthermore, the third generation module includes: a third determination submodule, used to determine the matching relationship between multiple first call stacks in the first flame graph and multiple second call stacks in the second flame graph, wherein the matching relationship includes: a first matching relationship and a second matching relationship, the first matching relationship indicating that the first call stack is the same as the second call stack, and the second matching relationship indicating that some function call relationships in the first call stack are the same as those in the second call stack; a merging submodule, used to merge the first flame graph and the second flame graph based on the matching relationship to obtain the target flame graph.
[0021] Furthermore, the merging submodule includes: a first merging submodule for merging the execution time of the first function in the second target call stack into the corresponding position of the first target call stack if the matching relationship between the first target call stack in the multiple first call stacks and the second target call stack in the multiple second call stacks is a first matching relationship, and setting the execution time corresponding to the second function of the second target call stack at the top level of the first target call stack, the first function being a function other than the top level in the second target call stack, and the second function being the top level function in the second target call stack; a second merging submodule for merging the execution time of the first function in the second target call stack into the corresponding position of the first target call stack if the matching relationship between the first target call stack in the multiple first call stacks and the second target call stack in the multiple second call stacks is a first matching relationship If the matching relationship between the call stack and the second target call stack in the multiple second call stacks is a second matching relationship, the execution time of the third function in the second target call stack is merged into the corresponding position of the first target call stack, and the execution time of the fourth function is added to the first flame graph according to the position of the fourth function in the second target call stack, the third function is a function in the second target call stack that has the same call relationship with the function in the first target call stack, and the fourth function is a function in the second target call stack other than the third function; the adjustment sub-module is used to proportionally adjust the merged first flame graph after the merging is completed to obtain the target flame graph.
[0022] According to another aspect of an embodiment of the present invention, an electronic device is provided, including: a memory storing an executable program; and a processor for running the program, wherein any one of the above-mentioned flame graph generation methods is executed when the program is running.
[0023] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is provided, wherein the storage medium stores a program, wherein when the program is running, the device where the storage medium is located is controlled to execute any of the above-mentioned flame graph generation methods.
[0024] According to another aspect of an embodiment of the present invention, a computer program product is provided, including a computer program or instructions, which implements any of the above flame graph generation methods when executed by a processor.
[0025] In an embodiment of the present application, the following steps are adopted: when a target thread is running, time information of the target thread when it is executed on a processor is sampled to obtain first time proportion information corresponding to the target thread; time information of the target thread being called out of and into the processor is sampled to obtain second time proportion information corresponding to the target thread; based on the first time proportion information and the second time proportion information, a target flame graph is generated, wherein the target flame graph is used to analyze the performance delay information of the target thread, thereby solving the technical problem in the related art that the flame graph contains a single piece of information, resulting in relatively low accuracy of the flame graph.
[0026] In this solution, when the target thread is running, the time when the target thread is executing on the processor is sampled, as well as the waiting time when the target thread is scheduled out of the processor and scheduled back into the processor are sampled to obtain first time proportion information and second time proportion information. Finally, a target flame graph is generated based on the two types of time proportion information. The target flame graph obtained by integrating the first time proportion information and the second time proportion information can intuitively reflect the performance delay characteristics of the target thread in the execution and waiting states, overcome the problem of the singleness of traditional flame graph information, and can more accurately identify which function calls or system states are the cause of the delay, thereby achieving the effect of improving the accuracy of the flame graph. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0028] Figure 1 This is a hardware structure block diagram of a computer terminal provided according to the first embodiment of the present application;
[0029] Figure 2This is a flowchart of a method for generating a flame graph according to the first embodiment of the present application;
[0030] Figure 3 This is a diagram of a flame diagram according to the first embodiment of the present application. Figure 1 ;
[0031] Figure 4 This is a diagram of a flame diagram according to the first embodiment of the present application. Figure 2 ;
[0032] Figure 5 This is a diagram of a flame diagram according to the first embodiment of the present application. Figure 3 ;
[0033] Figure 6 This is a diagram of a flame diagram according to the first embodiment of the present application. Figure 4 ;
[0034] Figure 7 This is a flowchart of a method for generating a flame graph according to the second embodiment of the present application;
[0035] Figure 8 is a schematic diagram of a flame graph generation device provided in accordance with the third embodiment of the present application;
[0036] Figure 9 This is a structural block diagram of an electronic device provided according to Example 6 of the present application. DETAILED DESCRIPTION
[0037] In order to enable those skilled in the art to better understand the present invention, the following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts should fall within the scope of protection of this application.
[0038] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in a sequence other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0039] First, some nouns or terms that appear in the description of the embodiments of the present application are subject to the following interpretations:
[0040] Thread: A thread is the basic unit of operating system scheduling, representing an independent execution path. Each thread has its own execution context (such as register state and stack space), and is allocated CPU time slices by the operating system kernel based on the scheduling policy.
[0041] FlameGraph: A FlameGraph is a stack trace-based visualization tool that displays the call stack distribution during program execution. It displays the call stack in a time-scaled manner, with the width representing the percentage of time spent in that function or code path. FlameGraphs are widely used in performance analysis, helping developers quickly locate hotspot functions or bottleneck code.
[0042] Oncpu refers to the time period during which a thread actually executes on the CPU, specifically the time during which the thread occupies CPU resources for computation or processing. The length of oncpu time generally reflects the thread's demand for CPU resources. Excessive oncpu time may indicate a compute-intensive task or a CPU performance bottleneck.
[0043] Offcpu time refers to periods of time when a thread is not executing on the CPU, typically because the thread is in a wait state (such as due to I / O blocking, lock contention, or sleep). The distribution of offcpu time can help identify non-CPU-related performance issues in the system, such as disk latency, network latency, or resource contention.
[0044] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of the relevant regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0045] Example 1
[0046] According to an embodiment of the present application, a method for generating a flame graph is also provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0047] The method embodiment provided in the first embodiment of the present application can be executed in a mobile terminal, a computer terminal or a similar computing device. Figure 1The hardware structure block diagram of a computer terminal (or mobile device) for implementing a method for generating a flame graph is shown. Figure 1 As shown, the computer terminal (or mobile device) 10 may include a processor set 102 (the processor set 102 may include but is not limited to a processing device such as a microprocessor MCU (Microcontroller Unit) or a programmable logic device FPGA (Field-Programmable Gate Array), and the processor set 102 may include a processor set, Figure 1 102a, 102b, ..., 102n are used to illustrate), a memory 104 for storing data, and a transmission module 106 for communication functions. In addition, it may also include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which can be included as one of the ports of the BUS bus), a network interface, a power supply and / or a camera. It will be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the above electronic device. Figure 1 More or fewer components than shown, or with Figure 1 Different configurations shown.
[0048] It should be noted that the one or more processors 102 and / or other data processing circuits described above may generally be referred to herein as "data processing circuitry." This data processing circuitry may be embodied in whole or in part as software, hardware, firmware, or any other combination thereof. Furthermore, the data processing circuitry may be a single, independent processing module, or may be fully or partially integrated into any of the other components of the computer terminal 10 (or mobile device). As discussed in the embodiments of this application, this data processing circuitry functions as a processor control (e.g., selecting a variable resistor terminal path connected to an interface).
[0049] The memory 104 can be used to store software programs and modules of application software, such as the program instructions / data storage device corresponding to the flame graph generation method in the embodiment of the present application. The processor 102 executes various functional applications and data processing by running the software programs and modules stored in the memory 104, that is, implementing the above-mentioned flame graph generation method. The memory 104 may include a high-speed random access memory and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some examples, the memory 104 may further include a memory remotely located relative to the processor 102, and these remote memories may be connected to the computer terminal 10 via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0050] Transmission device 106 is used to receive or transmit data via a network. A specific example of such a network may include a wireless network provided by the communications provider of computer terminal 10. In one embodiment, transmission device 106 includes a network interface controller (NIC), which can be connected to other network devices via a base station to enable communication with the Internet. In another embodiment, transmission device 106 may be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0051] The display may be, for example, a touch screen liquid crystal display that enables a user to interact with a user interface of the computer terminal 10 (or mobile device).
[0052] Under the above operating environment, this application provides Figure 2 The flame graph shown is generated. Figure 2 This is a flow chart of a method for generating a flame graph according to the first embodiment of the present application. The method includes:
[0053] Step S201: When a target thread is running, time information of the target thread when it is executed on a processor is sampled to obtain first time proportion information corresponding to the target thread.
[0054] Optionally, a target thread for analysis is identified. During its lifecycle, the thread calls multiple functions, forming a path known as a call stack. While the target thread is running, time information is sampled while the target thread is executing on the processor. The processor may be a central processing unit (CPU). It should be noted that performance analysis tools can be used to sample events from the hardware performance monitoring unit (PMU) to obtain first time percentage information corresponding to the target thread. This first time percentage information represents the proportion of the time the target thread executed on the CPU relative to the total sampling time.
[0055] For example, whenever a PMU event occurs, within a set time period or under event trigger conditions, the currently executing function is sampled, recording relevant data such as the call stack information, event type, and timestamp. Throughout the sampling process, a large number of sampling points are accumulated, each containing information about the specific function being executed by the thread at that time and its context. After sampling is completed or the predetermined data volume is reached, the sampled data is statistically analyzed. The key metric in this statistical analysis is the number of sampling points in a function (or call stack), which directly reflects the frequency of function execution. The total number of sampling points and the number of sampling points corresponding to the function are calculated to obtain the first-time percentage information.
[0056] Before sampling with a performance analysis tool, you need to configure the tool, specifying the target thread to sample (by thread ID or process ID), and the sampling frequency or conditions. For example, you can set the performance analysis tool to sample once every millisecond.
[0057] Step S202: sampling the time information of the target thread being called out of the processor and being called into the processor to obtain second time proportion information corresponding to the target thread.
[0058] Optionally, the performance analysis tool can be used to sample the time points when the target thread is scheduled out of the CPU and rescheduled back to the CPU to obtain the above-mentioned second time share information. The time difference between the time point when the target thread is scheduled out of the CPU and the time point when it is rescheduled back to the CPU can be used as the blocking time of the function executed when the target thread is scheduled out of the CPU. Therefore, the above-mentioned second time share information can be obtained based on the time points when the target thread is scheduled out of the CPU and rescheduled back to the CPU. The second time share information represents the time distribution of the target thread in the offCPU (i.e., not executing on the processor) state, especially the blocking time percentage between the time when the thread is scheduled out of the CPU and the time when it is rescheduled back to the CPU due to waiting for resources, lock contention, I / O operation or other blocking events.
[0059] For example, by tracking kernel functions like finish_task_switch or schedule, you can sample the time points when a target thread is scheduled out of the CPU and back in. finish_task_switch or schedule is part of the operating system's scheduling mechanism and is called whenever a thread is scheduled out of the CPU or a new thread is brought in. Performance analysis tools can accurately record the timestamps of these events, along with the status information at the time, without affecting system operation. This includes which threads were scheduled out and which were scheduled in, as well as their PIDs (process IDs) and call stack information.
[0060] For example, use performance analysis tools to track the target thread's scheduling events, such as functions like finish_task_switch or schedule. Calls to these functions mark the transition of the thread state from running to waiting or vice versa. Whenever the target thread is scheduled off the CPU, the time point (i.e., the start of blocking) is recorded; similarly, when the thread is scheduled back onto the CPU, the time point (i.e., the end of blocking) is also recorded. This typically includes the thread ID, timestamp, and current call stack information. By calculating the time difference between these two time points, we can determine the time the target thread spent in the off-CPU state, between being scheduled off the CPU and being scheduled back onto the CPU. This duration is the blocking time. Based on the function's blocking time and the total off-CPU time during the entire sampling period, calculate the second time percentage for each function or call stack.
[0061] For example, whenever the finish_task_switch event occurs, sampling is performed to determine the dispatched function. Throughout the sampling process, a large number of sampling points are accumulated, each containing the dispatched function and its context information. After sampling ends (for example, when the target thread is dispatched back to the CPU) or reaches a predetermined data volume, the sampled data is statistically analyzed. The key metric is the number of sampling points in a function (or call stack), which directly reflects the frequency with which the function was dispatched. The second time percentage information is calculated based on the total number of sampling points and the number of sampling points corresponding to the function.
[0062] Step S203: Generate a target flame graph based on the first time proportion information and the second time proportion information, wherein the target flame graph is used to analyze performance delay information of the target thread.
[0063] Optionally, a target flame graph is generated based on the first time share information and the second time share information. For example, the oncpu and offcpu times of the functions involved in the target thread are calculated based on the first time share information and the second time share information, and then the above-mentioned target flame graph is generated based on the calculation results. It should be noted that the generated target flame graph distinguishes oncpu and offcpu times by different colors or styles, allowing users to see at a glance which parts of the delay are caused by the thread executing on the CPU and which parts of the delay are caused by the thread waiting or being scheduled out of the CPU. This intuitive visualization method greatly simplifies the understanding and analysis of thread performance delay information, helping users quickly locate bottlenecks that may cause slow system response.
[0064] In summary, when the target thread is running, the time when the target thread is executing on the processor is sampled, and the waiting time when the target thread is scheduled out of the processor and scheduled back into the processor is sampled to obtain first time proportion information and second time proportion information. Finally, a target flame graph is generated based on the two types of time proportion information. The target flame graph obtained by integrating the first time proportion information and the second time proportion information can intuitively reflect the performance delay characteristics of the target thread in the execution and waiting states, overcome the problem of the singleness of traditional flame graph information, and can more accurately identify which function calls or system states are the cause of the delay, thereby achieving the effect of improving the accuracy of the flame graph.
[0065] To improve the accuracy of obtaining the first time proportion information, in the flame graph generation method provided in the first embodiment of the present application, sampling the time information of the target thread when it is executed on the processor to obtain the first time proportion information corresponding to the target thread includes: sampling and processing the clock cycle events in the processor to obtain multiple first sampling point information; when sampling the clock cycle events, obtaining the first call stack information corresponding to the target thread at the sampling time; and obtaining the first time proportion information based on the multiple first sampling point information and the first call stack information.
[0066] Alternatively, the clock cycle events in the processor can be sampled using a performance analysis tool to obtain the aforementioned multiple first sampling point information. The first sampling point information can be a thread ID and a corresponding sampling point sequence number. The clock cycle event can be a cycle event (a cycle event is a type of event supported by a hardware performance monitoring unit that is used to detect the frequency or number of clock cycles of processor execution instructions). Sampling cycle events can be viewed as uniform sampling during the CPU execution process. A large number of sampling samples can reflect the distribution of CPU execution events of threads on different functions.
[0067] When the performance analysis tool samples clock cycle events, it also captures the target thread's call stack information at the time of sampling, known as the primary call stack. The call stack is a list of executing functions or code snippets. It tells you what the thread was doing at a given moment and how it arrived at its current execution location. This primary call stack information is crucial for analyzing a thread's activity path and execution efficiency.
[0068] Finally, first time share information is obtained by analyzing the collected information of the multiple first sampling points and the first call stack information. For example, the sampling points of the same first call stack may be aggregated, and the first time share information is obtained based on the ratio between the number of sampling points corresponding to the first call stack and the total number of sampling points. For example, the sampling points of the same call stack path may be aggregated to calculate the total execution time of the function in the target thread on the CPU, and then this time is compared with the total CPU execution time of the thread to obtain the execution time share of the target thread, i.e., the first time share information described above.
[0069] By sampling and acquiring call stack information, we can capture the actual execution status of threads on the CPU and quickly locate which ones consume the most CPU resources, thereby improving the accuracy of performance analysis.
[0070] To improve the accuracy of obtaining the second time proportion information, in the flame graph generation method provided in the first embodiment of the present application, sampling the time information of when the target thread is called out of the processor and called into the processor to obtain the second time proportion information corresponding to the target thread includes: sampling and processing the process switching event in the processor to obtain multiple second sampling point information; when sampling the process switching event, obtaining the second call stack information corresponding to the target thread at the sampling time; and obtaining the second time proportion information based on the multiple second sampling point information and the second call stack information.
[0071] Optionally, the performance analysis tool can be used to sample the process switching event in the processor to obtain the above-mentioned multiple second sampling point information. The second sampling point information can be the thread ID and the corresponding sampling point sequence number. The process switching event can be finish_task_switch. Finish_task_switch is an important function in the thread scheduling process. The finish_task_switch event records the PID of the thread scheduled out of the CPU and the PID of the thread scheduled into the CPU, as well as the timestamp and call stack when the event occurred. Unlike the cycle event, the finish_task_switch event is a full trace. Each time it is triggered, a trace record is generated. In this way, the performance analysis tool will fully record the time when the thread is scheduled into and out of the CPU. Therefore, multiple second sampling point information can be obtained by sampling finish_task_switch. For example, whenever the finish_task_switch event occurs, sampling will be performed to determine the function that was scheduled out.
[0072] When sampling the process switch event, the second call stack information corresponding to the target thread at the sampling time is obtained. Finally, based on the second sampling point information and the second call stack information, the time distribution of the target thread in the off-CPU state is calculated to obtain the second time proportion information. For example, the sampling points of the same second call stack can be aggregated, and the second time proportion information can be obtained based on the ratio between the number of sampling points corresponding to the second call stack and the total number of sampling points.
[0073] By fully tracking the timestamps and call stack information during the thread scheduling process, we can accurately calculate the time distribution of threads in the off-CPU state, and then optimize resource management, reduce blocking waits, or adjust scheduling policies to improve the continuity and efficiency of thread execution, thereby improving overall system performance.
[0074] In order to improve the accuracy of the target flame graph, in the flame graph generation method provided in the first embodiment of the present application, generating the target flame graph based on the first time proportion information and the second time proportion information includes: generating a first flame graph corresponding to when the target thread is executed on the processor based on the first time proportion information; generating a second flame graph corresponding to when the target thread is called out of the processor and called into the processor based on the second time proportion information; and generating the target flame graph based on the first flame graph and the second flame graph.
[0075] Optionally, a first flame graph corresponding to the target thread when it is executed on the processor is generated based on the first time proportion information obtained from the CPU clock cycle event sampling. The first flame graph is used to display the execution time distribution of the thread on the CPU. The width represents the execution time proportion of the function, the height represents the call level, and the call stack is displayed from bottom to top, thereby clearly revealing the hot spots and bottlenecks of the oncpu time.
[0076] Based on the second time percentage information sampled from thread entry and exit events (such as finish_task_switch) during the CPU scheduling process, a second flame graph is generated for the target thread when it is scheduled out of and into the processor. This second flame graph focuses on the distribution of time the thread is not executing on the CPU, namely, offcpu time. By analyzing call stack information, it shows the time the thread spends waiting for resources, blocking, or other non-computing activities, helping to identify the root causes of non-CPU-related performance issues.
[0077] Finally, the information from the first and second flame graphs is fused to generate the target flame graph. For example, the offcpu time segments are matched with the oncpu call stack paths to accumulate the offcpu time onto the corresponding oncpu call stack to generate the target flame graph.
[0078] By generating the first and second flame graphs and integrating them into the target flame graph, a complete and accurate view of the thread performance delay distribution can be obtained, which helps to more effectively solve performance bottlenecks in complex systems.
[0079] To improve the accuracy of the first flame graph, in the flame graph generation method provided in the first embodiment of the present application, generating the first flame graph corresponding to the target thread when executing on the processor based on the first time proportion information includes: determining a first total duration based on a start count and an end count of a clock cycle event in the processor; calculating based on the first time proportion information and the first total duration to obtain first execution duration information of a function in first call stack information corresponding to a first sampling point in multiple first sampling point information; and generating the first flame graph based on the first execution duration information and the first total duration.
[0080] Optionally, the first total duration is obtained by calculating the start count and end count of all clock cycle events. For example, if a count is counted every 1 second, the first total duration can be obtained based on the start count, end count, and counting period. It should be noted that counting begins when the target thread executes on the processor (determining the start count), stops when the target thread is called out of the processor, and continues counting until the target thread completes execution upon being called back in, obtaining the end count. Then, the first execution duration information of the function in the first call stack information corresponding to the first sampling point is calculated based on the first time percentage information and the first total duration. The first execution duration information is then calculated by multiplying the first time percentage information and the first total duration. Identical first call stacks are then aggregated, and finally, a first flame graph is generated based on the first execution duration information and the first total duration. Call stacks are stacked and displayed in the flame graph according to their execution duration, with the width representing the time percentage and the height representing the call hierarchy, arranged in bottom-up order. Each layer of the first flame graph represents a function or call point, and its width intuitively reflects the proportion of the function in the total execution time of the target thread, allowing users to quickly identify functions that are computationally intensive or time-consuming.
[0081] The first flame graph accurately reflects the execution time distribution of the target thread on the CPU, helping users to more accurately identify which functions or code segments are compute-intensive and their degree of CPU resource consumption.
[0082] To improve the accuracy of the second flame graph, in the flame graph generation method provided in the first embodiment of the present application, generating, based on the second time proportion information, the second flame graph corresponding to when the target thread is called out of and into the processor includes: determining a second total duration based on the start time and end time of the process switch event in the processor; calculating, based on the second time proportion information and the second total duration, to obtain second execution duration information of a function in second call stack information corresponding to a second sampling point in the plurality of second sampling point information; and generating the second flame graph based on the second execution duration information and the second total duration.
[0083] Optionally, by sampling processor events related to process scheduling (e.g., finish_task_switch, schedule), the precise timestamps of all these events are recorded. By analyzing the start and end times of these events, the time interval between when the target thread was scheduled out and when it was scheduled back into the CPU can be calculated, i.e., the second total duration.
[0084] According to the second time proportion information and combined with the second total duration, the offcpu time corresponding to the call stack path in the second sampling point information is calculated, that is, the second execution duration information. The second execution duration information is obtained by multiplying the second time proportion information and the second total duration. By aggregating and accumulating the offcpu time segments of the same call stack path, it is possible to quantify the time spent by the function when the target thread is not executed on the CPU, as well as their proportion in the total offcpu time. Finally, based on the calculated second execution duration information and the second total duration, a second flame graph is generated. Unlike the first flame graph, the second flame graph focuses on showing the offcpu time distribution of the target thread. It also uses the form of a call stack and is stacked from bottom to top. The width of each layer represents the offcpu time proportion of a specific call stack path, while the height indicates the depth of the call.
[0085] The second flame graph allows you to clearly see which functions cause threads to wait or be blocked for a long time, improving the accuracy of subsequent performance analysis.
[0086] How to obtain the target flame graph is crucial. Therefore, in the flame graph generation method provided in Example 1 of the present application, generating the target flame graph based on the first flame graph and the second flame graph includes: determining the matching relationship between multiple first call stacks in the first flame graph and multiple second call stacks in the second flame graph, wherein the matching relationship includes: a first matching relationship and a second matching relationship, the first matching relationship indicates that the first call stack is the same as the second call stack, and the second matching relationship indicates that some function call relationships in the first call stack are the same as those in the second call stack; based on the matching relationship, merging the first flame graph and the second flame graph to obtain the target flame graph.
[0087] Optionally, a matching relationship is determined between multiple first call stacks in the first flame graph and multiple second call stacks in the second flame graph. Call stack matching relationships are divided into two categories: a first matching relationship indicates that the first call stack and the second call stack are exactly the same, meaning that the threads are in the same execution context during oncpu and offcpu time. A second matching relationship indicates that some function call relationships in the first call stack are the same as those in the second call stack, that is, the call stack paths partially overlap. After determining the call stack matching relationship, the first flame graph and the second flame graph are merged based on the matching relationship to obtain the target flame graph.
[0088] In an optional embodiment, based on the matching relationship, the first flame graph and the second flame graph are merged to obtain the target flame graph, including: if the matching relationship between the first target call stack in the multiple first call stacks and the second target call stack in the multiple second call stacks is the first matching relationship, then the execution time of the first function in the second target call stack is merged into the corresponding position of the first target call stack, and the execution time corresponding to the second function of the second target call stack is set at the top of the first target call stack, the first function is the function other than the top layer in the second target call stack, and the second function is the top layer function in the second target call stack; if multiple If a matching relationship between a first target call stack and a second target call stack in multiple second call stacks is a second matching relationship in the first call stack, the execution time of the third function in the second target call stack is merged into the corresponding position of the first target call stack, and the execution time of the fourth function in the second target call stack is added to the first flame graph according to the position of the fourth function. The third function is a function in the second target call stack that has the same call relationship as the function in the first target call stack, and the fourth function is a function in the second target call stack other than the third function. After the merging is completed, the merged first flame graph is scaled to obtain the target flame graph.
[0089] Optionally, after determining the matching relationship of the call stack, the first flame graph and the second flame graph are merged based on the matching, including: when the matching relationship between the first target call stack and the second target call stack is the first matching relationship, it means that the two sets of call stacks are completely consistent. In this case, the execution time of the first function (that is, the function other than the top-level function) in the second target call stack will be merged into the corresponding position of the first target call stack, which is essentially superimposing the offcpu time information on the basis of the oncpu time distribution. The execution time of the second function (the top-level function of the second target call stack) is directly set at the top of the first target call stack and displayed in the form of an independent "spire" to ensure a clear distinction between the oncpu and offcpu states.
[0090] For example, Figure 3As shown, the first target call stack in the first flame graph includes function A-function B-function D, and the second target call stack in the second flame graph includes function A-function B-function D. The matching relationship between the first target call stack and the second target call stack is the first matching relationship. The schematic diagram after the matching relationship between the first target call stack and the second target call stack is merged is shown in FIG. Figure 3 As shown, the execution time of functions other than the top-level function is merged into the corresponding position of the first target call stack, and the execution time of the top-level function of the second target call stack is directly set at the top of the first target call stack and displayed in the form of an independent "top of the tower".
[0091] If a second match exists between the first target call stack and the second target call stack, this means that the two call stacks share some but not all matches. For this second match, the execution time of the third function in the second target call stack (i.e., the function with the same call relationship as the function in the first target call stack) is merged into the corresponding position in the first target call stack. Based on the position of the fourth function in the second target call stack, its execution time is added to the first flame graph at the appropriate level. This means that only the offcpu time is added to the longest common prefix of the oncpu time. The incompletely matched portion (i.e., the fourth function mentioned above) is presented as a separate rectangle in the flame graph to distinguish the difference between offcpu and oncpu times. It should be noted that if the offcpu fragment and oncpu information for a call stack completely match, the functions at the top of the tower are not merged. Different colors can be set to distinguish offcpu and oncpu times.
[0092] For example, Figure 4 As shown, the first target call stack in the first flame graph includes function A-function B-function D, and the second target call stack in the second flame graph includes function A-function B-function C. The matching relationship between the first target call stack and the second target call stack is the second matching relationship. The schematic diagram after the matching relationship between the first target call stack and the second target call stack is merged is shown in FIG. Figure 4 As shown, function A and function B are merged, and function D and function C are set at the top level.
[0093] For example, Figure 5 As shown, the first target call stack in the first flame graph includes function A-function B-function D-function E, and the second target call stack in the second flame graph includes function A-function B-function C. The matching relationship between the first target call stack and the second target call stack is the second matching relationship, so function A and function B are merged, function C is set to the third layer, and function E remains at the top layer.
[0094] Since the original first flame graph and the second flame graph respectively show the hotspot distribution of oncpu time and offcpu time, the total execution time of the function has changed after the merge because it includes both oncpu time and offcpu time. If the proportion is not adjusted, the width in the flame graph will not correctly reflect the proportion of the function in the combined oncpu time and offcpu time, which will cause distortion of the performance analysis results. Therefore, after the merge is completed, it is necessary to recalculate the width of the function in the target flame graph based on the new execution time information and total time to reflect its total proportion in oncpu and offcpu time, that is, to obtain the target flame graph by proportionally adjusting the first flame graph obtained by the merge.
[0095] In an optional embodiment, the thread PID to be sampled is specified, and then events reflecting both oncpu and offcpu information are sampled simultaneously based on the thread PID. For oncpu, cycle events are sampled. For offcpu, any technology that supports full thread scheduling tracking is acceptable, including but not limited to tracking scheduling-related functions such as finish_task_switch and schedule. The sampled information includes the timestamp, the PID of the thread scheduled out of the CPU, the PID of the thread scheduled into the CPU, and the call stack.
[0096] For oncpu, aggregate the call stack and calculate the oncpu hotspot ratio of the functions on the call stack. Find the earliest and latest times of all cycle events and calculate the time difference to obtain the total time. Then, for each function, calculate the oncpu time based on the hotspot ratio from the previous step.
[0097] For offcpu, extract the offcpu fragment from the offcpu event, calculate the time of the offcpu fragment, and aggregate the same call stack (the offcpu fragment time of the same call stack is accumulated). For the aggregated offcpu fragment, match the call stack in the oncpu information. Perhaps the offcpu fragment call stack cannot completely match the oncpu call stack, but try to match the longest prefix as much as possible. The unmatched suffix will appear as an independent "tower top", and the time of the offcpu fragment is added to the matching prefix. If the offcpu fragment completely matches a call stack of the oncpu information, the functions at the top of the tower are not merged, so that they can be distinguished in the flame graph. Recalculate the proportion of each function to the whole, draw the flame graph, and the functions at the top of the flame graph tower either belong to oncpu or offcpu, and the two are distinguished by different tones. Then the final effect diagram is as follows Figure 6 As shown, the orange color at the top level represents oncpu and the blue color represents offcpu.
[0098] The target flame graph not only shows the functions or call points that consume the most time, but also reveals their specific performance in both on-cpu and off-cpu states. This provides clear guidance for performance optimization. Users can optimize call stacks that spend a large proportion of off-cpu time, such as reducing resource wait times or adjusting on-cpu functions to improve computational efficiency.
[0099] In the flame graph generation method provided in Example 1 of the present application, by sampling the time information of the target thread when it is executed on the processor when the target thread is running, first time proportion information corresponding to the target thread is obtained; the time information of the target thread being called out of the processor and called into the processor is sampled to obtain second time proportion information corresponding to the target thread; based on the first time proportion information and the second time proportion information, a target flame graph is generated, wherein the target flame graph is used to analyze the performance delay information of the target thread, thereby solving the technical problem in the related art that the flame graph contains a single information, resulting in relatively low accuracy of the flame graph.
[0100] In this solution, when the target thread is running, the time when the target thread is executing on the processor is sampled, as well as the waiting time when the target thread is scheduled out of the processor and scheduled back into the processor are sampled to obtain first time proportion information and second time proportion information. Finally, a target flame graph is generated based on the two types of time proportion information. The target flame graph obtained by integrating the first time proportion information and the second time proportion information can intuitively reflect the performance delay characteristics of the target thread in the execution and waiting states, overcome the problem of the singleness of traditional flame graph information, and can more accurately identify which function calls or system states are the cause of the delay, thereby achieving the effect of improving the accuracy of the flame graph.
[0101] It should be noted that for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that this application is not limited by the order of the actions described, because according to this application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by this application.
[0102] Through the description of the above embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus the necessary general hardware platform. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, or the part that contributes to the existing technology, can be embodied in the form of a software product. The computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for enabling a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods of each embodiment of the present application.
[0103] Example 2
[0104] According to an embodiment of the present application, a method for generating a flame graph is also provided, such as Figure 7 As shown, the flame graph generation method includes:
[0105] Step S701: receiving a flame graph generation request triggered by a client;
[0106] In step S702, based on the flame graph generation request, in the cloud server, when the target thread is running, time information of the target thread executing on the processor is sampled to obtain first time proportion information corresponding to the target thread; time information of the target thread being called out of and into the processor is sampled to obtain second time proportion information corresponding to the target thread; and a target flame graph is generated based on the first time proportion information and the second time proportion information, wherein the target flame graph is used to analyze performance delay information of the target thread.
[0107] Step S703: Return the target flame graph to the client.
[0108] It should be noted that the method for generating the flame graph in the cloud server is the same as that in Example 1 and will not be described in detail here.
[0109] It should be noted that for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that this application is not limited by the order of the actions described, because according to this application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by this application.
[0110] Through the description of the above embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus the necessary general hardware platform. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, or the part that contributes to the existing technology, can be embodied in the form of a software product. The computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for enabling a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods of each embodiment of the present application.
[0111] Example 3
[0112] According to an embodiment of the present application, a flame graph generation device for implementing the above flame graph generation method is also provided, such as Figure 8 As shown, the device includes: a first sampling unit 801, a second sampling unit 802 and a generating unit 803.
[0113] The first sampling unit 801 is configured to sample time information of the target thread when the target thread is running on the processor to obtain first time proportion information corresponding to the target thread;
[0114] The second sampling unit 802 is configured to sample time information of when the target thread is called out of the processor and when it is called into the processor, and obtain second time proportion information corresponding to the target thread;
[0115] The generating unit 803 is configured to generate a target flame graph according to the first time proportion information and the second time proportion information, wherein the target flame graph is used to analyze the performance delay information of the target thread.
[0116] Optionally, in the flame graph generation device provided in Example 3 of the present application, the first sampling unit includes: a first sampling module, used to sample and process clock cycle events in the processor to obtain multiple first sampling point information; a first acquisition module, used to obtain first call stack information corresponding to the target thread at the sampling moment when sampling the clock cycle event; and a first determination module, used to obtain first time proportion information based on the multiple first sampling point information and the first call stack information.
[0117] Optionally, in the flame graph generation device provided in Example 3 of the present application, the second sampling unit includes: a second sampling module, used to sample and process the process switching event in the processor to obtain multiple second sampling point information; a second acquisition module, used to obtain second call stack information corresponding to the target thread at the sampling moment when sampling the process switching event; and a second determination module, used to obtain second time proportion information based on the multiple second sampling point information and the second call stack information.
[0118] Optionally, in the flame graph generation device provided in Example 3 of the present application, the generation unit includes: a first generation module, used to generate a first flame graph corresponding to when the target thread is executed on the processor based on the first time proportion information; a second generation module, used to generate a second flame graph corresponding to when the target thread is called out of the processor and called into the processor based on the second time proportion information; and a third generation module, used to generate a target flame graph based on the first flame graph and the second flame graph.
[0119] Optionally, in the flame graph generation device provided in Example 3 of the present application, the first generation module includes: a first determination submodule, used to determine the first total duration based on the start count and end count of the clock cycle event in the processor; a first calculation submodule, used to calculate based on the first time proportion information and the first total duration to obtain the first execution duration information of the function in the first call stack information corresponding to the first sampling point in the multiple first sampling point information; and the first generation submodule, used to generate the first flame graph based on the first execution duration information and the first total duration.
[0120] Optionally, in the flame graph generation device provided in Example 3 of the present application, the second generation module includes: a second determination submodule, used to determine a second total duration based on the start time and end time of the process switching event in the processor; a second calculation submodule, used to calculate based on the second time proportion information and the second total duration to obtain second execution duration information of a function in the second call stack information corresponding to a second sampling point in the multiple second sampling point information; and a second generation submodule, used to generate a second flame graph based on the second execution duration information and the second total duration.
[0121] Optionally, in the flame graph generation device provided in Example 3 of the present application, the third generation module includes: a third determination submodule, used to determine the matching relationship between multiple first call stacks in the first flame graph and multiple second call stacks in the second flame graph, wherein the matching relationship includes: a first matching relationship and a second matching relationship, the first matching relationship indicating that the first call stack is the same as the second call stack, and the second matching relationship indicating that some function call relationships in the first call stack are the same as the second call stack; a merging submodule, used to merge the first flame graph and the second flame graph based on the matching relationship to obtain a target flame graph.
[0122] Optionally, in the flame graph generation device provided in Example 3 of the present application, the merging submodule includes: a first merging submodule, which is used to merge the execution time of the first function in the second target call stack into the corresponding position of the first target call stack if the matching relationship between the first target call stack in the multiple first call stacks and the second target call stack in the multiple second call stacks is the first matching relationship, and set the execution time corresponding to the second function of the second target call stack at the top level of the first target call stack, the first function is a function other than the top level in the second target call stack, and the second function is the top level function in the second target call stack; a second merging submodule, which is used to merge the execution time of the first function in the second target call stack into the corresponding position of the first target call stack if the matching relationship between the first target call stack in the multiple first call stacks and the second target call stack in the multiple second call stacks is the first matching relationship If a matching relationship between a first target call stack and a second target call stack in multiple second call stacks is a second matching relationship in the first call stack, the execution time of the third function in the second target call stack is merged into the corresponding position of the first target call stack, and the execution time of the fourth function in the second target call stack is added to the first flame graph according to the position of the fourth function in the second target call stack. The third function is a function in the second target call stack that has the same call relationship with the function in the first target call stack, and the fourth function is a function in the second target call stack other than the third function. The adjustment sub-module is used to proportionally adjust the merged first flame graph after the merging is completed to obtain the target flame graph.
[0123] It should be noted that the first sampling unit 801, the second sampling unit 802, and the generation unit 803 described above correspond to steps S201 to S203 in the first embodiment. The examples and application scenarios implemented by the three units and the corresponding steps are the same, but are not limited to the contents disclosed in the first embodiment. It should be noted that the above modules, as part of the device, can be run in the computer terminal 10 provided in the first embodiment.
[0124] It should be noted that the preferred implementation scheme involved in the above embodiments of this application is the same as the scheme provided in Example 1, as well as the application scenario and implementation process, but is not limited to the scheme provided in Example 1.
[0125] Example 4
[0126] The embodiment of the present application may provide an electronic device, which may be any electronic device in a group of electronic device terminals. Optionally, in this embodiment, the electronic device may also be replaced by a terminal device such as a mobile terminal.
[0127] Optionally, in this embodiment, the electronic device may be located in at least one network device among a plurality of network devices of a computer network.
[0128] In this embodiment, the electronic device may execute the program code of the following steps in the flame graph generation method: when a target thread is running, sampling the time information of the target thread when it is executed on the processor to obtain first time proportion information corresponding to the target thread; sampling the time information of the target thread being called out of the processor and being called into the processor to obtain second time proportion information corresponding to the target thread; and generating a target flame graph based on the first time proportion information and the second time proportion information, wherein the target flame graph is used to analyze performance delay information of the target thread.
[0129] The electronic device may execute the program code of the following steps in the flame graph generation method: sampling time information of a target thread when it is executed on a processor to obtain first time proportion information corresponding to the target thread, including: sampling and processing clock cycle events in the processor to obtain multiple first sampling point information; when sampling the clock cycle events, obtaining first call stack information corresponding to the target thread at the sampling moment; and obtaining the first time proportion information based on the multiple first sampling point information and the first call stack information.
[0130] The electronic device can execute the program code of the following steps in the flame graph generation method: sampling the time information of the target thread being called out of the processor and being called into the processor to obtain the second time proportion information corresponding to the target thread, including: sampling and processing the process switching event in the processor to obtain multiple second sampling point information; when sampling the process switching event, obtaining the second call stack information corresponding to the target thread at the sampling time; and obtaining the second time proportion information based on the multiple second sampling point information and the second call stack information.
[0131] The electronic device can execute the program code of the following steps in the flame graph generation method: generating a target flame graph based on the first time proportion information and the second time proportion information includes: generating a first flame graph corresponding to when the target thread is executed on the processor based on the first time proportion information; generating a second flame graph corresponding to when the target thread is called out of the processor and called into the processor based on the second time proportion information; generating the target flame graph based on the first flame graph and the second flame graph.
[0132] The electronic device can execute program code for the following steps in the flame graph generation method: generating, based on the first time proportion information, a first flame graph corresponding to when the target thread is executed on the processor, including: determining a first total duration based on a start count and an end count of a clock cycle event in the processor; calculating, based on the first time proportion information and the first total duration, to obtain first execution duration information of a function in first call stack information corresponding to a first sampling point in multiple first sampling point information; and generating the first flame graph based on the first execution duration information and the first total duration.
[0133] The electronic device may execute program code for the following steps in the flame graph generation method: generating, based on the second time proportion information, a second flame graph corresponding to when the target thread is called out of and into the processor, including: determining a second total duration based on the start time and end time of a process switching event in the processor; calculating, based on the second time proportion information and the second total duration, to obtain second execution duration information of a function in second call stack information corresponding to a second sampling point in multiple second sampling point information; and generating a second flame graph based on the second execution duration information and the second total duration.
[0134] The above-mentioned electronic device can execute the program code of the following steps in the flame graph generation method: based on the first flame graph and the second flame graph, generating a target flame graph includes: determining the matching relationship between multiple first call stacks in the first flame graph and multiple second call stacks in the second flame graph, wherein the matching relationship includes: a first matching relationship and a second matching relationship, the first matching relationship indicates that the first call stack is the same as the second call stack, and the second matching relationship indicates that some function call relationships in the first call stack are the same as the second call stack; based on the matching relationship, merging the first flame graph and the second flame graph to obtain the target flame graph.
[0135] The electronic device may execute the program code of the following steps in the flame graph generation method: based on the matching relationship, merging the first flame graph and the second flame graph to obtain a target flame graph, including: if a first target call stack in the plurality of first call stacks and a second target call stack in the plurality of second call stacks have a first matching relationship, merging the execution time of a first function in the second target call stack into a corresponding position in the first target call stack, and setting the execution time corresponding to the second function in the second target call stack at the top of the first target call stack, where the first function is a function other than the top of the second target call stack, and the second function is the top of the second target call stack; if a first target call stack in the plurality of first call stacks and a second target call stack in the plurality of second call stacks have a second matching relationship, merging the execution time of a third function in the second target call stack into a corresponding position in the first target call stack, and adding the execution time of the fourth function to the first flame graph according to the position of the fourth function in the second target call stack, where the third function is a function in the second target call stack that has the same call relationship as the function in the first target call stack; after the merging is completed, scaling the merged first flame graph to obtain the target flame graph.
[0136] Optionally, FIG is a structural block diagram of an electronic device according to an embodiment of the present application. Figure 9 As shown, the electronic device 90 may include: one or more ( Figure 9(Only one is shown) a processor 902 and a memory 904. The electronic device 90 may further include a memory controller to control and manage the memory 904; the electronic device 90 may further include a peripheral interface to connect to a radio frequency module, an audio module, a display screen, etc.
[0137] The memory can be used to store software programs and modules, such as the program instructions / modules corresponding to the flame graph generation method and device in the embodiments of the present application. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory, that is, implementing the flame graph generation method described above. The memory may include a high-speed random access memory and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include a memory remotely located relative to the processor, and these remote memories may be connected to the electronic device 90 via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0138] The processor can call information and applications stored in the memory through a transmission device to perform the following steps: when a target thread is running, sampling time information of the target thread when it is executed on the processor to obtain first time proportion information corresponding to the target thread; sampling time information of when the target thread is called out of the processor and when it is called into the processor to obtain second time proportion information corresponding to the target thread; generating a target flame graph based on the first time proportion information and the second time proportion information, wherein the target flame graph is used to analyze performance delay information of the target thread.
[0139] Optionally, the processor may also execute program code for the following steps: sampling the time information of the target thread when it is executed on the processor to obtain first time proportion information corresponding to the target thread, including: sampling and processing the clock cycle events in the processor to obtain multiple first sampling point information; when sampling the clock cycle events, obtaining the first call stack information corresponding to the target thread at the sampling moment; and obtaining the first time proportion information based on the multiple first sampling point information and the first call stack information.
[0140] Optionally, the processor may further execute program code of the following steps: sampling time information of the target thread being called out of and into the processor to obtain second time proportion information corresponding to the target thread, including: sampling and processing a process switching event in the processor to obtain a plurality of second sampling point information; when sampling the process switching event, obtaining second call stack information corresponding to the target thread at the sampling moment; and obtaining the second time proportion information based on the plurality of second sampling point information and the second call stack information.
[0141] Optionally, the processor may further execute program code for the following steps: generating a target flame graph based on the first time proportion information and the second time proportion information, including: generating a first flame graph corresponding to when the target thread is executed on the processor based on the first time proportion information; generating a second flame graph corresponding to when the target thread is called out of and into the processor based on the second time proportion information; and generating a target flame graph based on the first flame graph and the second flame graph.
[0142] Optionally, the processor may further execute program code of the following steps: generating, based on the first time proportion information, a first flame graph corresponding to when the target thread is executed on the processor, including: determining a first total duration based on a start count and an end count of a clock cycle event in the processor; performing calculations based on the first time proportion information and the first total duration to obtain first execution duration information of a function in first call stack information corresponding to a first sampling point in multiple first sampling point information; and generating a first flame graph based on the first execution duration information and the first total duration.
[0143] Optionally, the processor may further execute program code for the following steps: generating, based on the second time proportion information, a second flame graph corresponding to when the target thread is called out of and into the processor, including: determining a second total duration based on the start time and end time of a process switching event in the processor; calculating, based on the second time proportion information and the second total duration, to obtain second execution duration information of a function in second call stack information corresponding to a second sampling point in multiple second sampling point information; and generating a second flame graph based on the second execution duration information and the second total duration.
[0144] Optionally, the processor may further execute program code of the following steps: generating a target flame graph based on the first flame graph and the second flame graph includes: determining a matching relationship between multiple first call stacks in the first flame graph and multiple second call stacks in the second flame graph, wherein the matching relationship includes: a first matching relationship and a second matching relationship, the first matching relationship indicating that the first call stack is the same as the second call stack, and the second matching relationship indicating that some function call relationships in the first call stack are the same as those in the second call stack; based on the matching relationship, merging the first flame graph and the second flame graph to obtain the target flame graph.
[0145] Optionally, the processor may also execute the following program code: based on the matching relationship, merge the first flame graph and the second flame graph to obtain the target flame graph including: if the matching relationship between the first target call stack in the multiple first call stacks and the second target call stack in the multiple second call stacks is the first matching relationship, then merge the execution time of the first function in the second target call stack into the corresponding position of the first target call stack, and set the execution time corresponding to the second function of the second target call stack at the top of the first target call stack, the first function is the function other than the top layer in the second target call stack, and the second function is the top layer in the second target call stack. layer function; if the matching relationship between the first target call stack in the multiple first call stacks and the second target call stack in the multiple second call stacks is the second matching relationship, then the execution time of the third function in the second target call stack is merged into the corresponding position of the first target call stack, and the execution time of the fourth function is added to the first flame graph according to the position of the fourth function in the second target call stack, the third function is the function in the second target call stack that has the same call relationship with the function in the first target call stack, and the fourth function is the function in the second target call stack other than the third function; after the merging is completed, the merged first flame graph is proportionally adjusted to obtain the target flame graph.
[0146] It can be understood by those skilled in the art that Figure 9 The structure shown is for illustration only, and the electronic device 90 may also be a terminal device such as a smart phone, a tablet computer, a PDA, a mobile Internet device (MID), or a PAD. Figure 9 It does not limit the structure of the above electronic device. For example, the electronic device 90 may also include Figure 9 More or fewer components (such as network interfaces, display devices, etc.) shown in, or with Figure 9 Different configurations shown.
[0147] A person skilled in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructing the hardware related to the terminal device through a program, and the program can be stored in a computer-readable storage medium, which may include: a flash drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.
[0148] Example 5
[0149] The embodiment of the present application further provides a computer program product. Optionally, in this embodiment, the computer program product can be used to store the program code executed by the flame graph generation method provided in the first embodiment.
[0150] Optionally, in this embodiment, the computer program product may be located in any computer terminal in a computer terminal group in a computer network, or in any mobile terminal in a mobile terminal group.
[0151] The serial numbers of the above embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.
[0152] In the above embodiments of the present application, the description of each embodiment has its own focus. For parts that are not described in detail in a certain embodiment, please refer to the relevant description of other embodiments.
[0153] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0154] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.
[0155] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0156] If the integrated unit 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 application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, server or network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, magnetic disk or optical disk, etc., various media that can store program code.
[0157] The above is only a preferred embodiment of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.
Claims
1. A method for generating a flame graph, characterized in that: include: When the target thread is running, sampling time information of the target thread when it is executed on the processor to obtain first time proportion information corresponding to the target thread; Sampling time information of when the target thread is called out of the processor and when it is called into the processor to obtain second time proportion information corresponding to the target thread; generating a target flame graph according to the first time proportion information and the second time proportion information, wherein the target flame graph is used to analyze performance delay information of the target thread; wherein determining a matching relationship between a plurality of first call stacks in a first flame graph and a plurality of second call stacks in a second flame graph, wherein the matching relationship includes: a first matching relationship and a second matching relationship, wherein the first matching relationship indicates that the first call stack is identical to the second call stack, and the second matching relationship indicates that some function call relationships in the first call stack are identical to those in the second call stack, wherein the first flame graph is generated based on the first time proportion information, and the second flame graph is generated based on the second time proportion information; Based on the matching relationship, the first flame graph and the second flame graph are merged to obtain the target flame graph; If a first target call stack in the multiple first call stacks and a second target call stack in the multiple second call stacks have a second matching relationship, the execution duration of the third function in the second target call stack is merged into the corresponding position of the first target call stack, and the execution duration of the fourth function in the second target call stack is added to the first flame graph according to the position of the fourth function in the second target call stack, where the third function is a function in the second target call stack that has the same call relationship as the function in the first target call stack, and the fourth function is a function in the second target call stack other than the third function.
2. The method according to claim 1, characterized in that Sampling time information of the target thread when it is executed on the processor to obtain first time proportion information corresponding to the target thread includes: Sampling a clock cycle event in the processor to obtain a plurality of first sampling point information; When sampling the clock cycle event, obtaining first call stack information corresponding to the target thread at the sampling moment; The first time proportion information is obtained based on the multiple first sampling point information and the first call stack information.
3. The method according to claim 2, characterized in that Sampling the time information of the target thread being called out of the processor and being called into the processor to obtain second time proportion information corresponding to the target thread includes: Sampling the process switching event in the processor to obtain a plurality of second sampling point information; When sampling the process switching event, obtaining second call stack information corresponding to the target thread at the sampling moment; The second time proportion information is obtained based on the plurality of second sampling point information and the second call stack information.
4. The method according to claim 3, characterized in that Generating a target flame graph according to the first time proportion information and the second time proportion information includes: generating, according to the first time proportion information, a first flame graph corresponding to when the target thread is executed on the processor; generating, based on the second time proportion information, a second flame graph corresponding to when the target thread is called out of and into the processor; The target flame graph is generated according to the first flame graph and the second flame graph.
5. The method according to claim 4, characterized in that Generating, based on the first time proportion information, a first flame graph corresponding to when the target thread is executed on the processor includes: Determining a first total duration based on a start count and an end count of clock cycle events in the processor; Calculating according to the first time proportion information and the first total duration to obtain first execution duration information of a function in the first call stack information corresponding to a first sampling point in the plurality of first sampling point information; The first flame graph is generated according to the first execution duration information and the first total duration.
6. The method according to claim 4, characterized in that Generating, based on the second time proportion information, a second flame graph corresponding to when the target thread is called out of the processor and called into the processor includes: Determining a second total duration based on a start time and an end time of a process switching event in the processor; Calculating according to the second time proportion information and the second total duration to obtain second execution duration information of a function in the second call stack information corresponding to a second sampling point in the plurality of second sampling point information; Generate the second flame graph according to the second execution duration information and the second total duration.
7. The method according to claim 6, characterized in that Based on the matching relationship, the first flame graph and the second flame graph are merged to obtain the target flame graph, including: If a matching relationship between a first target call stack in the multiple first call stacks and a second target call stack in the multiple second call stacks is a first matching relationship, then merging the execution time of the first function in the second target call stack into the corresponding position of the first target call stack, and setting the execution time corresponding to the second function of the second target call stack at the top of the first target call stack, the first function is a function other than the top layer in the second target call stack, and the second function is the top layer function in the second target call stack; After the merging is completed, the first flame graph obtained by the merging is scaled to obtain the target flame graph.
8. A method for generating a flame graph, characterized in that: include: Receive flame graph generation requests triggered by the client; In the cloud server, based on the flame graph generation request, when the target thread is running, time information of the target thread when executing on the processor is sampled to obtain first time proportion information corresponding to the target thread; Sampling time information of when the target thread is called out of and into the processor to obtain second time proportion information corresponding to the target thread; generating a target flame graph based on the first time proportion information and the second time proportion information, wherein the target flame graph is used to analyze performance delay information of the target thread; Returning the target flame graph to the client; wherein determining a matching relationship between a plurality of first call stacks in a first flame graph and a plurality of second call stacks in a second flame graph, wherein the matching relationship includes: a first matching relationship and a second matching relationship, wherein the first matching relationship indicates that the first call stack is identical to the second call stack, and the second matching relationship indicates that some function call relationships in the first call stack are identical to those in the second call stack, wherein the first flame graph is generated based on the first time proportion information, and the second flame graph is generated based on the second time proportion information; Based on the matching relationship, the first flame graph and the second flame graph are merged to obtain the target flame graph; If a first target call stack in the multiple first call stacks and a second target call stack in the multiple second call stacks have a second matching relationship, the execution duration of the third function in the second target call stack is merged into the corresponding position of the first target call stack, and the execution duration of the fourth function in the second target call stack is added to the first flame graph according to the position of the fourth function in the second target call stack, where the third function is a function in the second target call stack that has the same call relationship as the function in the first target call stack, and the fourth function is a function in the second target call stack other than the third function.
9. A flame graph generation device, characterized in that: include: A first sampling unit is configured to sample time information of the target thread when the target thread is running on the processor to obtain first time proportion information corresponding to the target thread; a second sampling unit, configured to sample time information of when the target thread is called out of and into the processor, to obtain second time proportion information corresponding to the target thread; a generating unit, configured to generate a target flame graph based on the first time proportion information and the second time proportion information, wherein the target flame graph is used to analyze the performance delay information of the target thread; The apparatus is further configured to: determine a matching relationship between a plurality of first call stacks in a first flame graph and a plurality of second call stacks in a second flame graph, wherein the matching relationship includes: a first matching relationship and a second matching relationship, wherein the first matching relationship indicates that the first call stack is identical to the second call stack, and the second matching relationship indicates that some function call relationships in the first call stack are identical to those in the second call stack, wherein the first flame graph is generated based on the first time proportion information, and the second flame graph is generated based on the second time proportion information; Based on the matching relationship, the first flame graph and the second flame graph are merged to obtain the target flame graph; If a first target call stack in the multiple first call stacks and a second target call stack in the multiple second call stacks have a second matching relationship, the execution duration of the third function in the second target call stack is merged into the corresponding position of the first target call stack, and the execution duration of the fourth function in the second target call stack is added to the first flame graph according to the position of the fourth function in the second target call stack, where the third function is a function in the second target call stack that has the same call relationship as the function in the first target call stack, and the fourth function is a function in the second target call stack other than the third function.
10. A computer-readable storage medium, characterized in that The computer-readable storage medium includes a stored program, wherein when the program is executed, the device where the storage medium is located is controlled to execute the flame graph generation method according to any one of claims 1 to 8.
11. An electronic device, characterized in that: include: a memory storing an executable program; A processor, configured to run the program, wherein the program, when running, executes the flame graph generation method according to any one of claims 1 to 8.
12. A computer program product, characterized in that The method comprises a computer program or an instruction, which, when executed by a processor, implements the method for generating a flame graph according to any one of claims 1 to 8.