Method and device for querying function execution duration and computer equipment

By inserting code that captures the function call stack before and after the function, and combining it with sampling results, the accuracy problem of profiler tools when querying function execution time is solved, achieving accurate restoration of function execution time and understanding of performance bottlenecks.

CN120407353APending Publication Date: 2025-08-01HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410146257.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-01-31
Publication Date
2025-08-01

AI Technical Summary

Technical Problem

In existing technologies, profiler tools tend to miss functions with shorter execution times when querying function execution durations, making it difficult for developers to accurately understand the actual running status of the program.

Method used

By inserting code that captures the function call stack before and after a function, instrumentation techniques are used to insert code for capturing the function call stack during compilation or runtime. Combined with the first and second sampling results, the execution time of the function can be accurately reconstructed.

Benefits of technology

It enables accurate reconstruction of function execution time with fewer samplings, helping developers better understand program performance bottlenecks and improve development efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120407353A_ABST
    Figure CN120407353A_ABST
Patent Text Reader

Abstract

The invention provides a method and device for querying function execution duration and computer equipment, and the method comprises the steps: executing a first code, and inserting codes used for executing a capture function call stack in front of and behind a first function of the first code; determining a first sampling result and a second sampling result, the first sampling result being a sampling result determined by capturing the function call stack by using a first sampling interval in the process of executing the first code, and the second sampling result being a sampling result determined by executing a code inserted before and after the first function and used for capturing the function call stack; a third sampling result is determined according to the first sampling result and the second sampling result, the third sampling result comprises the execution duration of the first function, and the execution duration of some functions can be precisely restored through the method provided by the invention; therefore, developers can perform setting according to own requirements to obtain the execution duration of the interested function.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computers, and more particularly, to a method, an apparatus, and a computer device for querying the execution duration of a function. Background Art

[0002] Currently, developers can use a profiler tool to collect the time occupied by a processor during the execution of a function in a program running process to discover the performance bottleneck of the program. Developers can view the function call stack during the execution of the program and the execution time of a certain function through the profiler tool. However, the profiler tool will lose many functions with short execution times during the collection process, resulting in developers being unable to understand the true running situation of the program. Based on this, how to quickly and accurately query the execution duration of a function has become a technical problem to be solved urgently. Summary of the Invention

[0003] The present application provides a method, an apparatus, and a computer device for querying the execution duration of a function, which can accurately restore the execution duration of some functions. Furthermore, developers can set according to their own needs to obtain the execution duration of functions of interest.

[0004] In a first aspect, a method for querying the execution duration of a function is provided. The method includes: executing first code, where the first code includes a first function, and code for executing to capture a function call stack is inserted before and after the first function, and the first code is target code; determining a first sampling result and a second sampling result, where the first sampling result is a sampling result determined by capturing the function call stack at a first sampling interval during the execution of the first code, and the second sampling result is a sampling result determined by executing the code for executing to capture the function call stack inserted before and after the first function; determining a third sampling result according to the first sampling result and the second sampling result, where the third sampling result includes the execution duration of the first function.

[0005] In an embodiment of the present application, when executing the target code of a program, the function call stack can be captured at a first sampling interval to determine a first sampling result. And during the execution process of the target code of the program, by using the instrumentation technique, code for capturing the function call stack is inserted before and after the first function, so that during the execution process of the program, the function call stack corresponding to the first function can be captured again when the first function starts to execute and when the first function stops executing respectively to determine a second sampling result, and a third sampling result is output according to the first sampling result and the second sampling result. The third sampling result can not only roughly restore the execution duration of most functions, but also accurately restore the execution duration of the first function. Through the method for querying the execution duration of a function provided by the embodiment of the present application, developers can set according to their own needs to obtain the execution duration of functions of interest.

[0006] In addition, the code of the function call stack for grabbing before and after the first function can be inserted during the compilation to generate the first code, or can also be inserted during the execution of the first code. When the code of the function call stack for grabbing is inserted during the compilation to generate the first code, since there is no need to determine whether instrumentation is required, better performance can be achieved when running the first code. When the code of the function call stack for grabbing is inserted during the execution of the first code, since it is only necessary to determine whether the function meets the preset rules during the execution of the first code, repeated compilation can be avoided.

[0007] Combined with the first aspect, in some implementation manners of the first aspect, before executing the first code, the method further includes: obtaining second code, where the second code is the source code corresponding to the first code; compiling the second code to generate the first code, and during the compilation of the second code, inserting the code for grabbing the function call stack before and after the source code corresponding to the first function according to a preset first rule.

[0008] Combined with the first aspect, in some implementation manners of the first aspect, before executing the first code, the method further includes: obtaining second code, where the second code is the source code corresponding to the first code; compiling the second code to generate the first code; after generating the first code, inserting the code for grabbing the function call stack before and after the first function in the first code according to a preset second rule.

[0009] Combined with the first aspect, in some implementation manners of the first aspect, the code for executing the function call stack for grabbing before and after the first function is inserted during the execution of the first code according to a preset third rule.

[0010] In the embodiments of the present application, the code of the function call stack for grabbing before and after the first function can be inserted during the compilation to generate the first code, or can also be inserted during the execution of the first code. When the code of the function call stack for grabbing is inserted during the compilation to generate the first code, since there is no need to determine whether instrumentation is required, better performance can be achieved when running the first code. When the code of the function call stack for grabbing is inserted during the execution of the first code, since it is only necessary to determine whether the function meets the preset rules during the execution of the first code, repeated compilation can be avoided.

[0011] Combined with the first aspect, in some implementation manners of the first aspect, the first function is a function whose actual execution duration is less than a first threshold.

[0012] Combined with the first aspect, in some implementation manners of the first aspect, the first sampling interval is greater than a second threshold.

[0013] In combination with the first aspect, in certain implementations of the first aspect, the method further includes: displaying a first flame graph, wherein, in the first flame graph, the start time and end time of the first function are determined according to the second sampling result, and the start time and end time of other functions outside the first function are determined according to the first sampling result.

[0014] In a second aspect, there is provided an apparatus, which includes an execution unit and a sampling unit. The execution unit is configured to execute a first code, and the first code includes a first function, and code for executing a capture of a function call stack is inserted before and after the first function. The first code is a target code. The sampling unit is configured to determine a first sampling result and a second sampling result. The first sampling result is a sampling result determined by capturing the function call stack at a first sampling interval during the execution of the first code, and the second sampling result is a sampling result determined by executing the code for capturing the function call stack inserted before and after the first function. The execution unit is further configured to determine a third sampling result according to the first sampling result and the second sampling result, and the third sampling result includes the execution duration of the first function.

[0015] In combination with the second aspect, in certain implementations of the second aspect, the apparatus further includes a processing unit, and the processing unit is configured to: obtain a second code, where the second code is a source code corresponding to the first code; compile the second code to generate the first code, and during the compilation of the second code, according to a preset first rule, insert the code for capturing the function call stack before and after the source code corresponding to the first function.

[0016] In combination with the second aspect, in certain implementations of the second aspect, the apparatus further includes a processing unit, and the processing unit is configured to: obtain a second code, where the second code is a source code corresponding to the first code; compile the second code to generate the first code; after generating the first code, according to a preset second rule, insert the code for capturing the function call stack before and after the first function in the first code.

[0017] In combination with the second aspect, in certain implementations of the second aspect, the apparatus further includes a processing unit, and the code for executing the capture of the function call stack before and after the first function is inserted by the processing unit according to a preset third rule.

[0018] In combination with the second aspect, in certain implementations of the second aspect, the first function is a function whose actual execution duration is less than a first threshold.

[0019] In combination with the second aspect, in certain implementations of the second aspect, the first sampling interval is greater than a second threshold.

[0020] In combination with the second aspect, in some implementations of the second aspect, the device further includes a display unit configured to display a first flame graph, wherein in the first flame graph, the start time and end time of the first function are determined according to the second sampling result, and the start time and end time of other functions other than the first function are determined according to the first sampling result.

[0021] In a third aspect, a computer device is provided, which includes one or more processors; one or more memories; the one or more memories store one or more computer programs, and the one or more computer programs include instructions that, when executed by the one or more processors, cause the above aspects or any possible implementation of the above aspects to be executed.

[0022] In a fourth aspect, a computer-readable storage medium is provided, which includes a computer program or instructions that, when run on a computer, cause the method of the first aspect and any possible implementation of the first aspect to be executed.

[0023] In a fifth aspect, a computer program product is provided, which includes a computer program or instructions that, when run on a computer, cause the method of the first aspect and any possible implementation of the first aspect to be executed.

[0024] In a sixth aspect, a computer program is provided, which, when run on a computer, causes the method in the first aspect and any possible implementation thereof to be executed.

[0025] For the beneficial effects of the second aspect to the sixth aspect, please refer to the beneficial effects of the first aspect, and will not be repeated here. Description of the Drawings

[0026] Figure 1 is a schematic diagram of an application scenario provided by an embodiment of the present application.

[0027] Figure 2 is a schematic diagram of determining the execution time of a function at a fixed sampling frequency provided by an embodiment of the present application.

[0028] Figure 3 is a schematic flowchart of a method for querying the execution duration of a function provided by an embodiment of the present application.

[0029] Figure 4 is a schematic diagram of the first sampling result and the second sampling result provided by an embodiment of the present application.

[0030] Figure 5 is a schematic flowchart of a method for querying the execution duration of a function provided by an embodiment of the present application.

[0031] Figure 6 It is a schematic flowchart of a method for querying the execution duration of a function provided by an embodiment of the present application.

[0032] Figure 7 It is a schematic flowchart of a method for querying the execution duration of a function provided by an embodiment of the present application.

[0033] Figure 8 It is a schematic structural diagram of a device for querying the execution duration of a function provided by an embodiment of the present application.

[0034] Figure 9 It is a schematic hardware structure diagram of a computer device in an embodiment of the present application. Detailed implementation manners

[0035] Next, the technical solutions in the present application will be described with reference to the accompanying drawings.

[0036] In the description of the embodiments of the present application, unless otherwise specified, " / " means "or". For example, A / B may represent A or B; herein, "and / or" is only a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B may represent: A exists alone, A and B exist simultaneously, and B exists alone. In addition, in the description of the embodiments of the present application, "a plurality of" means two or more than two.

[0037] Hereinafter, the terms "first" and "second" are only used for descriptive purposes and cannot be understood as indicating or implying relative importance or implicitly specifying the quantity of the indicated technical features. Thus, features defined with "first" and "second" may explicitly or implicitly include one or more of such features. In the description of this embodiment, unless otherwise specified, the meaning of "a plurality of" is two or more than two.

[0038] In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or device that includes a series of steps or units is not limited to the listed steps or units, but optionally further includes steps or units not listed, or optionally further includes other steps or units inherent to these processes, methods, products, or devices.

[0039] In the embodiments of the present application, words such as "exemplary" or "for example" are used to represent examples, illustrations, or explanations. Any embodiment or design solution described as "exemplary" or "for example" in the embodiments of the present application should not be construed as being more preferred or having more advantages than other embodiments or design solutions. Rather, the use of words such as "exemplary" or "for example" is intended to present relevant concepts in a specific manner for easy understanding.

[0040] In this application, "at least one (item)" means one or more. "A plurality" means two or more. "At least two (items)" means two, three or more. "And / or" is used to describe the relationship between related objects and indicates that three relationships can exist. For example, "A and / or B" can mean: only A exists, only B exists, and both A and B exist at the same time. Here, A and B can be singular or plural. The character " / " generally indicates that the related objects before and after are in an "or" relationship. "At least one (of the following)" or a similar expression means any combination of these items, including any combination of single items or plural items. For example, at least one (of) a, b, and (or) c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, and c can be single or multiple. "When..." and "if" both mean that corresponding processing will be performed under certain objective circumstances, not limited to time, and do not require a judgment action when implemented, nor do they mean the existence of other limitations.

[0041] References to "one embodiment" or "some embodiments" etc. described in this specification mean that specific features, structures or characteristics described in connection with that embodiment are included in one or more embodiments of this application. Thus, statements such as "in one embodiment", "in some embodiments", "in other some embodiments", "in still other embodiments" etc. that appear in different places in this specification do not necessarily all refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in another way. The terms "comprise", "include", "have" and their variants all mean "include but not limited to", unless otherwise specifically emphasized in another way.

[0042] For the sake of easy understanding, relevant terms and related concepts involved in the embodiments of this application are introduced below. The terms used in the embodiments of this application are only used to explain the specific embodiments of this application and are not intended to limit this application.

[0043] Source code: It refers to the original code written by developers, usually written in a high-level programming language and saved with a specific file extension (such as.c,.cpp,.java). The source code contains information such as the logic, algorithms, variables, and function definitions of the program.

[0044] Object code: It refers to the code generated after processing the source code through a compiler, etc. The object code includes the instructions specified by the developer in the source code. The object code is machine-readable, or rather, machine-executable. For different programming languages, the object code can be different. For example, the object code of JavaScript can be bytecode, which can be interpreted and executed in an interpreter.

[0045] It is understandable that for the same function, its source code and object code are different.

[0046] Compilation: Refers to the process of converting source code into object code. In the embodiments of the present application, no specific limitations are imposed on the specific types of source code and object code. For example, the source code can be JavaScript (JS) code, and the object code can be bytecode.

[0047] Stack frame: Refers to the stack space separately allocated for a function call. The stack frame corresponding to each function can be used to save local variables of the function, parameters passed to the called function, return values returned by the function, and information such as the return address of the function. The construction of the stack frame occurs when a function is called. Each function corresponds to a stack frame. When a function is called, a corresponding stack frame will be generated synchronously and pushed onto the function call stack. On the contrary, when a function call ends and returns to the upper-level function that called it, the corresponding stack frame will be popped from the function call stack.

[0048] Runtime: Refers to the period during which a program interacts with computer hardware and the operating system during the program's running process, that is, during the execution of the code. It is an important stage in the program execution process, involving aspects such as program loading, execution, management, and interaction with the external environment. There is a runtime library corresponding to the runtime, which is the environment that supports the program's operation.

[0049] Instrumentation: Is a technique for dynamically modifying code and can be used to monitor, record, and analyze the running state of a program. Instrumentation can be implemented at compile time or runtime. In other words, instrumentation can be understood as inserting some probes into the program, which can also be called "detectors", and essentially are code segments for information collection.

[0050] Figure 1 Shows a schematic diagram of the application scenario provided by the embodiments of the present application.

[0051] As Figure 1 shown, developers can use the profiler tool to output the performance analysis results of the program. The performance analysis results of the profiler tool correspond to accessing the function call stack during the program execution process, and thus can determine the execution time of the function corresponding to each stack frame in the function call stack. Developers can discover the performance bottlenecks of the program based on the display results of the profiler tool. Currently, when determining the execution time of a function, the profiler tool usually sets a timer to record the function call stack of the target thread at a fixed sampling frequency. Through regular sampling, the profiler tool can obtain multiple sampling records and organize these multiple sampling records, thereby determining the execution time of the function.

[0052] Figure 2A schematic diagram showing the determination of the execution time of a function at a fixed sampling frequency provided by an embodiment of the present application is shown.

[0053] Figure 2 (a) in shows the actual change of the function call stack during the execution of the program. Through this function call stack, it is possible to clearly know the execution duration of each function corresponding to each stack frame. As Figure 2 (a) in shows that during the execution of the program, the stack frames in the function call stack are constantly changing. Among them, stack frame #1 corresponds to function #1, stack frame #2 corresponds to function #2, stack frame #3 corresponds to function #3, stack frame #4 corresponds to function #4, and stack frame #5 corresponds to function #5. Taking Figure 2 the function call stack on the left side of (a) in as an example, function #1 is called first, and stack frame #1 is pushed into the function call stack and is at the bottom of the stack. After that, function #1 calls function #2, and stack frame #2 is pushed into the function call stack and is above stack frame #1. After that, function #2 calls function #3 and function #4 respectively at different time periods, and stack frame #3 and stack frame #4 are pushed into the function call stack and are above stack frame #2. When function #2 finishes calling function #3 and function #4, stack frame #3 and stack frame #4 are removed from the function call stack. When function #1 finishes calling function #2, stack frame #2 is removed from the function call stack.

[0054] The horizontal axis in the figure is used to represent the duration. Through the horizontal axis, the time of the stack frame in the function call stack can be determined. Furthermore, through the time of the stack frame in the function call stack, the execution time of the function can be determined. As can be seen from Figure 2 (a) in, stack frame #1 and stack frame #2 have a relatively long time in the function call stack, indicating that function #1 and function #2 have a relatively long execution time. Among them, stack frame #1 has a time of 12 unit times in the function call stack, and stack frame #2 has a time of 9 unit times in the function call stack. Stack frame #3, stack frame #4, and stack frame #5 have a relatively short time in the function call stack, indicating that function #3, function #4, and function #5 have a relatively short execution time. Among them, stack frame #3 has a time of 3 unit times in the function call stack, and stack frame #4 and stack frame #5 have a time of 2 unit times in the function call stack.

[0055] In order to obtain the execution duration of different functions, the function call stack can be recorded, and the execution duration of the function can be determined according to multiple sampling records. It is not difficult to understand that during sampling, the sampling interval will have a great impact on the final result.

[0056] The sampling interval can also be determined through the horizontal axis in the figure. As Figure 2 shown by the top horizontal axis in (b) in, the sampling interval is 4 times the unit time. When the sampling interval is 4 times the unit time, the stack frame situation in the function call stack at each sampling time can be represented by Table 1.

[0057] Stack frame situation in the function call stack at each sampling in Table 1

[0058] Sampling order Stack frames in the function call stack Sampling order Stack frames in the function call stack 1 No stack frame 7 Stack frame #1, stack frame #2, and stack frame #5 2 Stack frame #1 and stack frame #2 8 Stack frame #1 3 Stack frame #1 and stack frame #2 9 Stack frame #1 4 Stack frame #1 and stack frame #2 10 Stack frame #1, stack frame #2, and stack frame #3 5 No stack frame 11 Stack frame #1 and stack frame #2 6 Stack frame #1, stack frame #2, and stack frame #3 12 No stack frame

[0059] As can be seen from Table 1, at the first sampling, there is no stack frame in the function call stack. At the second to fourth samplings, both stack frame #1 and stack frame #2 are in the function call stack. At the fifth sampling, stack frame #1 and stack frame #2 are not in the function call stack, indicating that stack frame #1 and stack frame #2 are pushed into the function call stack during the sampling interval between the first sampling and the second sampling, and are removed from the function call stack during the sampling interval between the fourth sampling and the fifth sampling. Since the time points when stack frame #1 and stack frame #2 are pushed into and removed from the function call stack cannot be accurately obtained, therefore, the time point corresponding to the second sampling can be determined as the time point when stack frame #1 and stack frame #2 are first pushed into the function call stack, and the time point corresponding to the fifth sampling can be determined as the time point when stack frame #1 and stack frame #2 are first removed from the function call stack.

[0060] By analogy, the time point when stack frame #1 is second pushed into the function call stack is the time point corresponding to the sixth sampling, and the time point when it is second removed from the function call stack is the time point corresponding to the twelfth sampling.

[0061] The time point when stack frame #2 is second pushed into the function call stack is the time point corresponding to the sixth sampling, and the time point when it is second removed from the function call stack is determined as the time point corresponding to the eighth sampling.

[0062] The time point when stack frame #2 is third pushed into the function call stack is the time point corresponding to the tenth sampling, and the time point when it is second removed from the function call stack is determined as the time point corresponding to the twelfth sampling.

[0063] The time point when stack frame #3 is first pushed into the function call stack is the time point corresponding to the sixth sampling, and the time point when it is first removed from the function call stack is determined as the time point corresponding to the seventh sampling.

[0064] The time point when stack frame #3 is second pushed into the function call stack is the time point corresponding to the tenth sampling, and the time point when it is second removed from the function call stack is determined as the time point corresponding to the eleventh sampling.

[0065] The time point when stack frame #5 is first pushed into the function call stack is the time point corresponding to the seventh sampling, and the time point when it is first removed from the function call stack is determined as the time point corresponding to the eighth sampling.

[0066] Through the above results, we can get as Figure 2The sampling result shown in (b) of , in which the execution duration of the first execution of Function #1 and Function #2 is 12 unit times, the second execution duration of Function #1 is 24 unit times, the second execution duration of Function #2 is 8 unit times, the third execution duration of Function #2 is 8 unit times, the first execution duration of Function #3 is 4 unit times, the second execution duration of Function #3 is 4 unit times, and the first execution duration of Function #5 is 4 unit times.

[0067] Comparing the actual function execution situation of the program, it can be clearly seen that there is a large difference between the sampling result and the actual situation. For example, Function #3 actually executed 2 times, but the sampling result only reflects that Function #3 executed 1 time. Similarly, Function #1 actually executed 3 times, but the sampling result only reflects that Function #1 executed 2 times.

[0068] For another example, Function #4 actually executed 2 times, but the sampling result did not reflect the execution of Function #4.

[0069] For another example, the single execution duration of Function #3 is 3 unit times, and the single execution duration of Function #5 is 2 unit times. However, in the sampling result, the single execution durations of both Function #3 and Function #5 are 4 unit times.

[0070] The reason for the large difference between the sampling result and the actual situation is that the sampling interval is large, resulting in the stack frames corresponding to some functions with short execution times (for example, the execution time is less than 1 sampling interval) not being sampled. To improve the accuracy of the sampling result, the sampling accuracy can be increased, that is, the sampling interval can be reduced, such as Figure 2 shown in (c) of with a sampling interval of 2 unit times and as Figure 2 shown in (d) of with a sampling interval of 1 unit time.

[0071] As Figure 2 shown by the horizontal axis at the top of (c) and (d) of , the sampling interval can be gradually reduced from 4 times the unit time to 2 times the unit time or 1 time the unit time. When the sampling interval is gradually reduced, the obtained sampling result will be more in line with the actual execution duration of the function. However, when the sampling interval is reduced, the number of samplings will increase significantly, which will seriously affect the performance.

[0072] To sum up, there are the following deficiencies in currently determining the execution duration of each function using a fixed sampling frequency:

[0073] (1) When the sampling interval is large, the true function call situation may not be obtained, and some functions with short execution durations (which can also be called small methods) will be ignored, and the determined execution duration will also have a large gap compared with the actual execution duration.

[0074] (2) When the sampling interval is small, although the accuracy can be improved to restore the real function call situation and the execution duration of each function, the number of samplings will increase significantly, seriously affecting the performance of the program.

[0075] Based on this, the embodiment of the present application provides a method for querying the execution duration of a function, which can restore the real function call situation and the execution duration of each function with fewer samplings, and can better help developers understand the performance bottleneck of the program and improve the development efficiency of developers.

[0076] Figure 3 The schematic flowchart of the method for querying the execution duration of a function provided by the embodiment of the present application is shown, as Figure 3 shown, the method 300 includes:

[0077] S301, obtain the source code of the program.

[0078] Among them, the source code of the program can be written by a developer using a high-level programming language. The high-level language used by the developer can be Basic, Java, C, C++, Python, JavaScript, etc., and the embodiment of the present application is not limited thereto. The source code of the program can include multiple functions, and the multiple functions can correspond to one or more types, where the multiple functions include a first function.

[0079] S302, compile the source code of the program to generate the target code of the program. Among them, during the process of compiling the source code of the program, according to a preset first rule, code for capturing the function call stack is inserted before and after the first function.

[0080] Specifically, after obtaining the source code of the program, the source code of the program can be compiled to generate the target code of the program. During the process of compiling the source code of the program, lexical analysis and syntax analysis will be performed on the source code of the program. The embodiment of the present application can, based on lexical analysis and syntax analysis, according to a preset first rule, through the instrumentation technique, insert code for capturing the function call stack before and after the first function, so that during the process of executing the target code of the program, when the first function is executed (it can also be said that the first function is called), the code for capturing the function call stack will be executed to capture the function call stack corresponding to the first function once, and when the first function is no longer executed (it can also be said that the first function is no longer called), the code for capturing the function call stack will also be executed to capture the function call stack corresponding to the first function again.

[0081] In the embodiments of the present application, inserting code during source code compilation may be referred to as source code insertion. The code inserted into the source code may be written in a high-level programming language. For example, when executing step S302, JS code for capturing the function call stack is inserted before and after the first function.

[0082] It can be understood that executing the first function refers to executing the target code compiled from the source code of the first function.

[0083] It can also be understood that the insertion before and after the first function in the embodiment of the present application can be understood as inserting a section of code for capturing the function call stack before and after the first function, respectively, so that the function call stack corresponding to the first function can be captured once when the first function is executed and when the first function is no longer executed.

[0084] It can also be understood that in the embodiments of the present application, there is no specific limitation on the type of the target code of the program. The target code of the program can be an intermediate code such as bytecode, or it can be a machine code.

[0085] In the embodiment of the present application, there is no specific limitation on the preset first rule, and developers can set it according to their own needs. Several possible implementation methods are introduced as examples below.

[0086] For example, developers can pre-set the function names corresponding to the functions that need to be instrumented according to their own needs, so that after obtaining the program source code, they can find the corresponding functions in the program source code according to preset rules and perform instrumentation before and after the corresponding functions.

[0087] For example, let's take the first preset rule of inserting stubs before and after a function named BB. Assuming the program source code includes function #1, function #2, and function #3, where function #1 and function #2 are named AA, and function #3 is named BB, then after obtaining the program source code, you can insert stubs before and after function #3.

[0088] For example, developers can pre-set the function types corresponding to the functions that need to be instrumented according to their own needs, so that after obtaining the program source code, they can find the corresponding function in the program source code according to the preset first rule and perform instrumentation before and after the corresponding function.

[0089] S303: Execute the target code of the program and determine a first sampling result and a second sampling result, wherein the first sampling result is a sampling result determined by capturing a function call stack at a first sampling interval during the execution of the target code of the program, and the second sampling result is a sampling result determined by executing code inserted before and after the first function for executing the captured function call stack.

[0090] Specifically, after the target code is compiled and generated, the target code of the program can be executed, and during the execution of the target code, the function call stack can be captured at the first sampling interval to determine the first sampling result, and the function call stack can be captured by executing the code for capturing the function call stack to determine the second sampling result.

[0091] Reference Figure 4 Schematic diagrams of the first sampling result and the second sampling result provided by the embodiments of the present application are shown.

[0092] Figure 4 In (a) of [reference], it shows the actual change situation of the function call stack during the execution of the program. As Figure 4 shown in (a) of [reference], during the execution of the program, the stack frames in the function call stack are constantly changing. Among them, stack frame #1 corresponds to function #1, stack frame #2 corresponds to function #2, stack frame #3 corresponds to function #3, stack frame #4 corresponds to function #4, and stack frame #5 corresponds to function #5. Assume that function #4 is the first function in step S302, that is, code for capturing the function call stack is inserted before and after function #4 through the inlining technique, so that when function #4 is executed, the function call stack is captured once, and when function #4 is no longer executed, the function call stack corresponding to the first function is captured again.

[0093] As Figure 4 shown in (b) of [reference], when the sampling interval is 4 times the unit time, the first sampling result can be obtained, and the situation of the function call stack at each sampling can be represented by Table 1.

[0094] It should be understood that the description of (b) in [reference] can be referred to the description of (b) in [another reference]. For the sake of brevity, it will not be repeated here. Figure 4 The description of (b) in [reference] can be referred to the description of (b) in [another reference]. For the sake of brevity, it will not be repeated here. Figure 2 For the sake of brevity, it will not be repeated here.

[0095] As Figure 4 shown in (c) of [reference], since code for capturing the function call stack is inserted before and after function #4, during the running of the program, the function call stack can be captured 4 times, and the stack frame situation in the function call stack at each sampling can be represented by Table 2.

[0096] Table 2 Stack frame situation in the function call stack at each sampling

[0097] Sampling order Stack frames in the function call stack 1 Stack frame #1, stack frame #2, and stack frame #4 2 Stack frame #1 and stack frame #2 3 Stack frame #1, stack frame #2, and stack frame #4 4 Stack frame #1 and stack frame #2

[0098] As can be seen from Table 2, at the first sampling and the third sampling, stack frames #1, #2, and #4 are all in the function call stack. In the second sampling and the fourth sampling, stack frame #4 is not in the function call stack. However, since the code for capturing the function call stack is inserted before and after the first function corresponding to stack frame #4, therefore, the time interval between the first sampling and the second sampling is the execution duration of the first execution of the first function, and the time interval between the third sampling and the fourth sampling is the execution duration of the second execution of the first function.

[0099] From the above results, the sampling results as shown in (c) of Figure 4 can be obtained. In this sampling result, the execution duration of the first execution of the first function is 2 unit times, and the execution duration of the second execution of the first function is 2 unit times, which is exactly the same as the actual execution situation.

[0100] It can be understood that when capturing the function call stack to determine the first sampling result and the second sampling result, there may be a situation where the sampling time points overlap. For example, the sampling time point corresponding to the sampling order of 11 in Table 1 overlaps with the sampling time point corresponding to the sampling order of 4 in Table 2. In this case, two samplings can be performed separately and the two samplings can be used as the elements for determining the first sampling result and the second sampling result respectively, or one sampling can be performed and the one sampling can be used as the element for determining the first sampling result and the second sampling result.

[0101] S304. Output a third sampling result according to the first sampling result and the second sampling result, and the third sampling result includes the execution duration of the first function.

[0102] Specifically, after determining the first sampling result and the second sampling result, the first sampling result and the second sampling result can be sorted according to the sampling time sequence. The stack frame situation in the function call stack at each sampling can be represented by Table 3.

[0103] Table 3 Stack frame situation in the function call stack at each sampling

[0104] Sampling order Stack frames in the function call stack Sampling order Stack frames in the function call stack 1 No stack frame 9 Stack frame #1, stack frame #2, and stack frame #5 2 Stack frame #1 and stack frame #2 10 Stack frame #1 3 Stack frame #1 and stack frame #2 11 Stack frame #1 4 Stack frame #1, stack frame #2, and stack frame #4 12 Stack frame #1, stack frame #2, and stack frame #3 5 Stack frame #1 and stack frame #2 13 Stack frame #1, stack frame #2, and stack frame #4 6 Stack frame #1 and stack frame #2 14 Stack frame #1 and stack frame #2 7 None 15 Stack frame #1 and stack frame #2 8 Stack frame #1, stack frame #2, and stack frame #3 16 No stack frame

[0105] It can be understood that Table 3 is obtained from Table 1 and Table 2 according to the sampling time sequence. Referring to the analysis of Table 1 in the above text, as shown in Figure 4The third sampling result shown in (d) thereof, in which the execution duration of the first execution of Function #1 and Function #2 is 12 unit times, the execution duration of the second execution of Function #1 is 24 unit times, the execution duration of the second execution of Function #2 is 8 unit times, the execution duration of the third execution of Function #2 is 8 unit times, the execution duration of the first execution of Function #3 is 4 unit times, the execution duration of the second execution of Function #3 is 2 unit times, the execution duration of the first execution of Function #4 (i.e., the first function) is 2 unit times, the execution duration of the second execution of Function #4 is 2 unit times, and the execution duration of the first execution of Function #5 is 4 unit times.

[0106] Comparison Figure 4 in (d) and Figure 2 in (b), it can be seen that in the case of adopting the same sampling interval, in the embodiment of the present application, by means of instrumentation, the actual execution duration of Function #4 can be accurately obtained with only 4 additional sampling times.

[0107] In the embodiment of the present application, when executing the program, the function call stack can be captured at the first sampling interval to determine the first sampling result. And when compiling the source code of the program, by means of instrumentation technology, code for capturing the function call stack is inserted before and after the first function in the source code, so that during the execution of the program, the function call stack corresponding to the first function can be captured again when the first function is executed and when the first function is no longer executed respectively to determine the second sampling result, and the third sampling result is output according to the first sampling result and the second sampling result. The third sampling result can roughly restore the execution duration of most functions and can also accurately restore the execution duration of the first function. Through the method for querying the execution duration of functions provided by the embodiment of the present application, developers can set according to their own needs to obtain the execution duration of functions of interest.

[0108] In some embodiments, the actual execution duration of the first function is less than the first threshold. For example, the first threshold is 10 ms.

[0109] In some embodiments, the first sampling time interval is greater than the second threshold. For example, the second threshold is 20 ms.

[0110] In the embodiment of the present application, by inserting code for capturing the function call stack before and after functions with shorter execution durations, the execution duration of functions with shorter execution durations can be accurately determined, and thus functions with shorter execution durations will not be missed even when a larger sampling interval is adopted.

[0111] In some embodiments, after determining the third sampling result, a first flame graph may also be displayed, where the first flame graph is used to indicate the execution duration of each function in the target code. The start time and end time of the first function may be determined according to the second sampling result, and the start time and end time of other functions except the first function may be determined according to the first sampling result. In other words, the start time and end time of the first function are actual and accurate.

[0112] Figure 5 FIG. shows a schematic flowchart of another method for querying the execution duration of a function provided by an embodiment of the present application. As Figure 5 shown, the method 500 includes:

[0113] S501, obtain the source code of the program.

[0114] The source code includes a first function. It should be understood that the specific description of S501 can refer to the description of S301. For the sake of brevity, it will not be repeated here.

[0115] S502, compile the source code of the program to generate the target code of the program.

[0116] S503, according to a preset second rule, insert code for capturing the function call stack before and after the target code corresponding to the first function in the target code.

[0117] Specifically, after obtaining the source code of the program and compiling the source code to generate the target code of the program, the target code can be analyzed for syntax, semantics, etc., and according to a preset second rule, code for the function call stack is inserted before and after the target code corresponding to the first function in the target code.

[0118] In the embodiments of the present application, inserting stubs into the compiled code is referred to as target code stubbing. The code inserted into the target code can be target code. For example, when executing step S503, bytecode for capturing the function call stack is inserted before and after the first function.

[0119] It can be understood that since there are differences between the target code and the source code after compiling the source code into the target code in S502, the preset second rule in S503 is determined according to the syntax, semantics, etc. of the target code.

[0120] S504, execute the target code of the program and determine the first sampling result and the second sampling result. The first sampling result is the sampling result obtained by capturing the function call stack at a first sampling interval during the execution of the target code of the program, and the second sampling result is the sampling result obtained by executing the code for capturing the function call stack inserted before and after the target code corresponding to the first function.

[0121] S505 outputs a third sampling result according to the first sampling result and the second sampling result, where the third sampling result includes the execution duration of the first function.

[0122] It should be understood that for the specific descriptions of S504 and S505, reference can be made to the descriptions of S303 and S304 above. For the sake of brevity, they will not be elaborated here.

[0123] In the embodiments of the present application, when executing the target code of the program, the function call stack can be captured at the first sampling interval to determine the first sampling result. And after compiling the source code of the program into the target code, code for capturing the function call stack is inserted before and after the target code corresponding to the first function through the instrumentation technique, so that during the execution of the program, the function call stack corresponding to the first function can be captured again when the first function starts to execute and when the first function stops executing respectively to determine the second sampling result, and the third sampling result is output according to the first sampling result and the second sampling result. The third sampling result can roughly restore the execution durations of most functions and can also accurately restore the execution duration of the first function. Through the method for querying the execution duration of functions provided by the embodiments of the present application, developers can set according to their own needs to obtain the execution duration of the functions they are interested in.

[0124] In addition, in Figure 3 the embodiments shown, code for capturing the function call stack can be inserted into the source code. In Figure 5 the embodiments shown, code for capturing the function call stack can be inserted into the target code. The method for querying the execution duration of functions provided by the embodiments of the present application can perform instrumentation at different times, enabling developers to choose different methods according to their own habits.

[0125] In some embodiments, the actual execution duration of the first function is less than the first threshold.

[0126] In some embodiments, the first sampling time interval is greater than the second threshold.

[0127] In the embodiments of the present application, by inserting code for capturing the function call stack before and after functions with short execution durations, the execution durations of functions with short execution durations can be accurately determined. Thus, even when using a relatively large sampling interval, functions with short execution durations will not be missed.

[0128] In some embodiments, after determining the third sampling result, a first flame graph can be further displayed. The first flame graph is used to indicate the execution durations of the various functions in the target code. Among them, the start time and end time of the first function can be determined according to the second sampling result, and the start time and end time of other functions except the first function can be determined according to the first sampling result.

[0129] In the method for querying the execution duration of a function introduced above, instrumentation can be performed in the source code or in the target code. These two instrumentation methods can be collectively referred to as compile-time instrumentation. However, in the embodiments of the present application, it is not limited thereto. As described above, instrumentation can also be performed at runtime. Therefore, in some other embodiments of the present application, instrumentation can also be performed when the program is executed, which will be specifically introduced below.

[0130] Figure 6 FIG. shows a schematic flowchart of a method for querying the execution duration of a function provided by an embodiment of the present application. As Figure 6 shown, the method 600 includes:

[0131] S601, obtaining the target code of the program.

[0132] Specifically, the source code of the program can be preprocessed, compiled, etc. to obtain the target code of the program, and the target code of the program includes a first function.

[0133] S602, executing the target code of the program, and during the process of executing the target code of the program, according to a preset third rule, inserting code for capturing the function call stack before and after the first function.

[0134] Specifically, during the process of executing the target code of the program, when the first function is called, it can be determined whether to insert code for capturing the function call stack before and after the first function in combination with the preset third rule. When it is determined that the first function meets the preset third rule, code for capturing the function call stack can be inserted before and after the first function.

[0135] For example, if the source code of the program is JS code and the target code of the program is bytecode, since the bytecode is executed in an interpreter, the code for capturing the function call stack can be code written in the C++ language.

[0136] S603, determining a first sampling result and a second sampling result, where the first sampling result is the sampling result determined by capturing the function call stack at a first sampling interval during the process of executing the target code of the program, and the second sampling result is the sampling result determined by executing the code for capturing the function call stack inserted before and after the first function.

[0137] Specifically, during the process of executing the target code of the program, the function call stack can be captured at a first sampling interval to determine the first sampling result, and the function call stack can be captured by executing the code for capturing the function call stack inserted before and after the first function to determine the second sampling result.

[0138] S604, output a third sampling result according to the first sampling result and the second sampling result, where the third sampling result includes the execution duration of the first function.

[0139] It should be understood that for the specific descriptions of S603 and S604, reference can be made to the descriptions of S303 and S304 above. For the sake of brevity, they will not be elaborated here.

[0140] In the embodiments of the present application, when executing the target code of the program, the function call stack can be captured at the first sampling interval to determine the first sampling result. And during the execution of the target code, by using the instrumentation technique, code for capturing the function call stack is inserted before and after the target code corresponding to the first function, so that during the execution of the program, the function call stack corresponding to the first function can be captured again when the first function starts to execute and when the first function stops executing respectively to determine the second sampling result, and the third sampling result is output according to the first sampling result and the second sampling result. The third sampling result can roughly restore the execution duration of most functions and can also accurately restore the execution duration of the first function. Through the method for querying the execution duration of functions provided by the embodiments of the present application, developers can set according to their own needs to obtain the execution duration of the functions they are interested in.

[0141] In addition, in Figure 6 the embodiments shown, runtime instrumentation is adopted, that is, instrumentation is performed during the execution of the target code. Compared with compile-time instrumentation, developers only need to set different preset rules according to their own needs, and they can determine whether a function meets different preset rules during the execution of the target code. Even if the developers' needs change, there is no need to perform different instrumentations on the source code multiple times, and repeated compilation can be avoided.

[0142] In some embodiments, the actual execution duration of the first function is less than the first threshold.

[0143] In some embodiments, the first sampling time interval is greater than the second threshold.

[0144] In the embodiments of the present application, by inserting code for capturing the function call stack before and after functions with short execution durations, the execution durations of functions with short execution durations can be accurately determined. Furthermore, even when a relatively large sampling interval is adopted, functions with short execution durations will not be missed.

[0145] In some embodiments, after determining the third sampling result, a first flame graph can also be displayed. The first flame graph is used to indicate the execution durations of the various functions in the target code. Among them, the start time and end time of the first function can be determined according to the second sampling result, and the start time and end time of other functions except the first function can be determined according to the first sampling result.

[0146] Figure 7 The schematic flowchart of the method for executing the query function duration provided by the embodiment of the present application is shown, as Figure 7 shown, the method 700 includes:

[0147] S701, execute the first code, the first code is the target code, the first code includes a first function, and code for executing the capture of the function call stack is inserted before and after the first function.

[0148] In some embodiments, before executing S701, the method includes:

[0149] Obtain the second code, the second code is the source code corresponding to the first code;

[0150] Compile the second code to generate the first code. Among them, during the process of compiling the second code, according to the preset first rule, code for capturing the function call stack is inserted before and after the source code corresponding to the first function.

[0151] After obtaining the second code, the second code can be compiled to generate the first code. During the process of compiling the second code, lexical analysis and syntax analysis will be performed on the second code. According to the preset first rule, through the instrumentation technology, code for capturing the function call stack is inserted before and after the first function in the second code.

[0152] In some embodiments, before executing S701, the method includes:

[0153] Obtain the second code, the second code is the source code corresponding to the first code; <&

[0154] Compile the second code to generate the first code;

[0155] After generating the first code, according to the preset second rule, code for capturing the function call stack is inserted before and after the first function.

[0156] After obtaining the second code and compiling the second code to generate the first code, syntax, semantics, etc. analysis can be performed on the first code, and according to the preset second rule, code for the function call stack is inserted before and after the first function in the first code.

[0157] In some embodiments, the code for executing the capture of the function call stack before and after the first function is inserted during the execution of the first code according to the preset third rule.

[0158] During the execution of the first code, when the first function is called, it is possible to determine whether it is necessary to insert code for capturing the function call stack before and after the first function in combination with a preset third rule. When it is determined that the first function meets the preset third rule, code for capturing the function call stack can be inserted before and after the first function.

[0159] S702, determine the first sampling result and the second sampling result. Among them, the first sampling result is the sampling result determined by capturing the function call stack at the first sampling interval during the execution of the first code, and the second sampling result is the sampling result determined by executing the code for capturing the function call stack inserted before and after the first function.

[0160] S703, determine the third sampling result according to the first sampling result and the second sampling result. Among them, the third sampling result includes the execution duration of the first function.

[0161] It should be understood that for the specific descriptions of S702 and S703, reference can be made to the above text. For the sake of brevity, it will not be elaborated here.

[0162] In the embodiments of the present application, when executing the target code of the program, the function call stack can be captured at the first sampling interval to determine the first sampling result. And during the execution of the target code of the program, by using the instrumentation technique, code for capturing the function call stack is inserted before and after the first function, so that during the execution of the program, the function call stack corresponding to the first function can be captured again when the first function is executed and when the first function is no longer executed respectively to determine the second sampling result, and the third sampling result is output according to the first sampling result and the second sampling result. The third sampling result can roughly restore the execution duration of most functions and can also accurately restore the execution duration of the first function. Through the method for querying the execution duration of functions provided by the embodiments of the present application, developers can set according to their own needs to obtain the execution duration of the functions they are interested in.

[0163] In addition, the code for capturing the function call stack before and after the first function can be inserted during the compilation of the first code, or can also be inserted during the execution of the first code. When the code for capturing the function call stack is inserted during the compilation of the first code, since there is no need to add a judgment on whether instrumentation is required during runtime, better performance can be achieved when running the first code. When the code for capturing the function call stack is inserted during the execution of the first code, since it is only necessary to set a preset rule to judge whether the function meets the preset rule during the execution of the first code, developers only need to set different preset rules according to their own needs. Even if the developers' needs change, there is no need to perform multiple different instrumentations on the source code, and repeated compilation can be avoided.

[0164] In some embodiments, the actual execution duration of the first function is less than the first threshold.

[0165] In some embodiments, the first sampling time interval is greater than the second threshold.

[0166] In the embodiments of the present application, by inserting code for capturing the function call stack before and after a function with a short execution duration, the execution duration of the function with a short execution duration can be accurately determined. Furthermore, even when a relatively large sampling interval is adopted, functions with short execution durations will not be missed.

[0167] In some embodiments, after determining the third sampling result, a first flame graph may also be displayed. The first flame graph is used to indicate the execution durations of various functions in the target code. Among them, the start time and end time of the first function may be determined according to the second sampling result, and the start time and end time of other functions except the first function may be determined according to the first sampling result.

[0168] Figure 8 FIG. shows a schematic structural diagram of a device for querying the execution duration of a function provided by an embodiment of the present application, as Figure 8 shown. The device 800 includes:

[0169] An execution unit 810, configured to execute first code, where the first code includes a first function, and code for executing and capturing the function call stack is inserted before and after the first function. The first code is target code.

[0170] A sampling unit 820, configured to determine a first sampling result and a second sampling result. Among them, the first sampling result is a sampling result determined by capturing the function call stack at a first sampling interval during the execution of the first code, and the second sampling result is a sampling result determined by executing the code for capturing the function call stack inserted before and after the first function.

[0171] The sampling unit 820 is further configured to determine a third sampling result according to the first sampling result and the second sampling result. Among them, the third sampling result includes the execution duration of the first function.

[0172] In some embodiments, the device 800 further includes a processing unit 830. The processing unit 830 is configured to obtain second code, where the second code is source code corresponding to the first code;

[0173] The processing unit 830 is further configured to compile the second code to generate the first code. During the compilation of the second code, according to a preset first rule, the code for capturing the function call stack is inserted before and after the source code corresponding to the first function.

[0174] In some embodiments, the processing unit 830 is further configured to, after generating the first code, insert the code for capturing the function call stack before and after the first function according to a preset second rule.

[0175] In some embodiments, the processing unit 830 is configured to insert the code for executing the capture of the function call stack before and after the first function according to a preset third rule.

[0176] In some embodiments, the first function is a function whose actual execution duration is less than a first threshold.

[0177] In some embodiments, the first sampling interval is greater than a second threshold.

[0178] Specifically, the specific execution process of the apparatus 800 can be referred to the description above and will not be elaborated here.

[0179] In some embodiments, the apparatus 800 further includes a display unit 840, and the display unit 840 is configured to display a first flame graph, where the first flame graph is used to indicate the execution duration of each function in the target code. Among them, the start time and end time of the first function can be determined according to the second sampling result, and the start time and end time of other functions except the first function can be determined according to the first sampling result.

[0180] Figure 9 FIG. shows a schematic hardware structure diagram of a computer device in an embodiment of the present application. As Figure 9 shown, the device 900 includes a processor 901, a memory 902, an interface circuit 903, and a bus 904. Among them, the processor 901, the memory 902, and the interface circuit 903 perform data transmission through the bus 904. The memory 902 stores instructions, and when the instructions are executed by the processor 901, the processor 901 executes the technical solutions in the above embodiments. The implementation principle and technical effects are similar to those of the related embodiments of the above method and will not be elaborated here.

[0181] An embodiment of the present application provides a computer program product, which, when running on a computer device, enables the computer device to execute the technical solutions in the above embodiments. The implementation principle and technical effects are similar to those of the related embodiments of the above method and will not be elaborated here.

[0182] An embodiment of the present application provides a readable storage medium, where the readable storage medium contains instructions, and when the instructions are running on a computer device, the computer device is enabled to execute the technical solutions in the above embodiments. The implementation principle and technical effects are similar and will not be elaborated here.

[0183] An embodiment of the present application provides a chip, which is used to execute instructions. When the chip runs, it executes the technical solutions in the above embodiments. The implementation principle and technical effects are similar and will not be elaborated here.

[0184] Those of ordinary skill in the art can realize that the units and algorithm steps of each example described in combination with the embodiments disclosed herein can be implemented by electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Professional technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the embodiments of the present application.

[0185] Those skilled in the art can clearly understand that for the convenience and simplicity of description, the specific working processes of the systems, devices, and units described above can refer to the corresponding processes in the foregoing method embodiments and will not be elaborated here.

[0186] In several embodiments provided by the present application, it should be understood that the disclosed systems, devices, and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of the units is only a logical function division. In actual implementation, there may be other division methods. For example, 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 displayed or discussed couplings or direct couplings or communication connections to each other can be through some interfaces. The indirect couplings or communication connections of the devices or units can be in electrical, mechanical, or other forms.

[0187] The units described as separate components may or may not be physically separated. The components displayed as units may or may not be physical units, that is, they can be located in one place, or they can be distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0188] In addition, the functional units in each embodiment of the present application can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit.

[0189] When the above-mentioned functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiments of the present application, in essence, or the part that contributes to the prior art, or a part of this technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in the embodiments of the present application. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical discs that can store program codes.

[0190] As described above, it is only the specific implementation manner of the embodiments of the present application, but the protection scope of the embodiments of the present application is not limited thereto. Any person skilled in the art within the technical scope disclosed by the embodiments of the present application can easily think of changes or substitutions, which should all be covered within the protection scope of the embodiments of the present application. Therefore, the protection scope of the embodiments of the present application shall be subject to the protection scope of the claims.

Claims

1. A method for querying the execution duration of a function, characterized in that, The method includes: Execute the first code, where the first code includes a first function, and code for executing a capture of the function call stack is inserted before and after the first function, and the first code is the target code; Determine a first sampling result and a second sampling result, where the first sampling result is the sampling result determined by capturing the function call stack at a first sampling interval during the execution of the first code, and the second sampling result is the sampling result determined by executing the code for capturing the function call stack inserted before and after the first function; Determine a third sampling result according to the first sampling result and the second sampling result, where the third sampling result includes the execution duration of the first function.

2. The method according to claim 1, characterized in that, Before executing the first code, the method further includes: Obtain a second code, where the second code is the source code corresponding to the first code; Compile the second code to generate the first code, where during the compilation of the second code, according to a preset first rule, the code for capturing the function call stack is inserted before and after the source code corresponding to the first function.

3. The method according to claim 1, wherein Before executing the first code, the method further includes: Obtain a second code, where the second code is the source code corresponding to the first code; Compile the second code to generate the first code; After generating the first code, according to a preset second rule, insert the code for capturing the function call stack before and after the first function in the first code.

4. The method according to claim 1, wherein The code for executing the capture of the function call stack before and after the first function is inserted during the execution of the first code according to a preset third rule.

5. The method according to any one of claims 1 to 4, characterized in that The first function is a function with an actual execution duration less than a first threshold.

6. The method according to any one of claims 1 to 5, characterized in that, The first sampling interval is greater than a second threshold.

7. The method according to any one of claims 1 to 6, characterized in that The method further includes: Display a first flame graph, where in the first flame graph, the start time and end time of the first function are determined according to the second sampling result, and the start time and end time of other functions except the first function are determined according to the first sampling result.

8. A device, characterized in that, The apparatus includes an execution unit and a sampling unit, where the execution unit is configured to execute a first code, the first code includes a first function, and code for executing a capture of the function call stack is inserted before and after the first function, and the first code is the target code; The sampling unit is configured to determine a first sampling result and a second sampling result, where the first sampling result is the sampling result determined by capturing the function call stack at a first sampling interval during the execution of the first code, and the second sampling result is the sampling result determined by executing the code for capturing the function call stack inserted before and after the first function; The execution unit is further configured to determine a third sampling result according to the first sampling result and the second sampling result, where the third sampling result includes the execution duration of the first function.

9. The device according to claim 8, characterized in that, The apparatus further includes a processing unit, and the processing unit is configured to: Obtain a second code, where the second code is the source code corresponding to the first code; Compile the second code to generate the first code, wherein, during the compilation of the second code, according to a preset first rule, insert the code for capturing the function call stack before and after the source code corresponding to the first function.

10. The device according to claim 8, characterized in that, The device further includes a processing unit, and the processing unit is configured to: Obtain a second code, where the second code is the source code corresponding to the first code; Compile the second code to generate the first code; After generating the first code, according to a preset second rule, insert the code for capturing the function call stack before and after the first function in the first code.

11. The device according to claim 8, characterized in that, The device further includes a processing unit, and the code for executing the capture of the function call stack before and after the first function is inserted by the processing unit according to a preset third rule.

12. The device according to any one of claims 8 to 11, characterized in that, The first function is a function whose actual execution duration is less than a first threshold.

13. The device according to any one of claims 8 to 12, characterized in that, The first sampling interval is greater than a second threshold.

14. The device according to any one of claims 8 to 13, characterized in that, The device further includes a display unit, and the display unit is configured to display a first flame graph, wherein, in the first flame graph, the start time and end time of the first function are determined according to the second sampling result, and the start time and end time of other functions except the first function are determined according to the first sampling result.

15. A computer-readable storage medium, characterized in that, A computer program is stored thereon, and when the computer program is executed by a computer, it enables the implementation of the method according to any one of claims 1 to 7.

16. A chip, characterized in that, The chip includes a processor and a data interface, and the processor reads the instructions stored on the memory through the data interface to execute the method according to any one of claims 1 to 7.

17. A computer device, characterized in that, Comprising one or more processors; one or more memories; the one or more memories store one or more computer programs, and the one or more computer programs include instructions, and when the instructions are executed by the one or more processors, the method according to any one of claims 1 to 7 is executed.

18. A computer program product, characterized in that, When the computer program product runs on a computer, it enables the computer to execute the method according to any one of claims 1 to 7.