Function execution time consumption analysis method, electronic equipment and computer program product
By using the printing functions and tag writing string technology of renamed encapsulation processing in kernel space, the problem of high cost of execution of kernel space function and single analysis dimension in the prior art is solved, and the lightweight function execution time-consuming analysis is realized, reducing the impact of system performance and enhancing analysis capabilities.
Patent Information
- Application Number
- CN202411895896.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-20
- Publication Date
- 2025-04-11
AI Technical Summary
The prior art when performing time-consuming detection of functions in kernel space, the performance is expensive and the analysis dimension is single, and it cannot provide a complete system context.
By writing strings with tags in the user space of the operating system, rename and encapsulation of the initial printing function, generate the target printing function, and insert it into the kernel source code of the function to be analyzed, obtain and parse the execution log data to calculate the execution time of the function.
It realizes the time-consuming execution of analytical functions in the kernel space in a lightweight printing manner, reduces the negative impact on system performance, and enhances the scenario application capabilities of the analysis scheme.
Smart Images

Figure CN119806987A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of operating systems, and in particular to a method for analyzing the time consumption of function execution, an electronic device, and a computer program product. Background Art
[0002] In the field of software development, especially in system-level programming, it is crucial to have insight into the execution efficiency of kernel functions. Traditional methods for detecting the time-consuming execution of kernel functions, such as using print functions (such as printk functions) to perform timestamp recording and log output, are intuitive and easy to implement, but this method also has strong limitations.
[0003] First, the performance cost of the above traditional kernel function execution time-consuming detection method is high. In the kernel hot path (i.e., the frequently executed critical path, such as the processor scheduling path and the memory allocation path, etc.), if the above method is used to print logs frequently, it will significantly increase the operating burden of the system. In other words, frequent input and output operations not only consume precious computing resources, but also may cause system performance degradation, and in severe cases even cause system freezes or crashes.
[0004] Secondly, the traditional kernel function execution time detection method has a single analysis dimension. The above method only detects the function execution time. When developers analyze the abnormal function execution time, they cannot obtain the complete system context and it is difficult to determine whether the abnormal time is caused by a specific system state.
[0005] As can be seen from the above, how to detect the execution time of a function in kernel space with low performance consumption has become one of the important technical issues in the relevant technical field. Currently, no effective solution has been proposed for the above problem. Summary of the invention
[0006] The embodiments of the present application provide a method for analyzing the time consumption of function execution, an electronic device, and a computer program product, so as to at least solve the technical problem in the related technical field that the solution of analyzing the time consumption of kernel space function execution seriously affects the system performance and is not conducive to application.
[0007] According to one aspect of an embodiment of the present application, a method for analyzing function execution time consumption is provided, which is applied to the kernel space of an operating system, including: writing a string based on a tag in the user space of the operating system, renaming and encapsulating an initial printing function to obtain a target printing function, wherein the target printing function is used for lightweight printing in the kernel space; inserting the target printing function into the initial kernel source code corresponding to the function to be analyzed to obtain the target kernel source code; obtaining execution log data generated by the execution of the target kernel source code; parsing the execution log data to obtain the execution time consumption of the function to be analyzed.
[0008] Optionally, the function execution time analysis method also includes: obtaining a marker tracking file corresponding to a marker tracking tool in a user space; parsing graphic display data in the marker tracking file to obtain a marker write character string.
[0009] Optionally, based on the tag writing string, the initial printing function is renamed and encapsulated to obtain the target printing function, including: using a static limiting field to limit the scope of the initial printing function; renaming the initial printing function to the tag writing string to obtain the renamed printing function; based on a preset identifier variable, encapsulating the renamed printing function to obtain the target printing function.
[0010] Optionally, based on a preset identifier variable, encapsulating the renamed printing function to obtain a target printing function includes: based on the identifier variable and at least one variable corresponding to the initial printing function, encapsulating the renamed printing function to obtain the target printing function.
[0011] Optionally, the candidate values of the identifier variable include: a first identifier and a second identifier, wherein the first identifier is used to identify the start execution time of the function to be analyzed, and the second identifier is used to identify the end execution time of the function to be analyzed.
[0012] Optionally, the initial kernel source code includes a target execution code corresponding to the function to be analyzed; inserting the target printing function into the initial kernel source code corresponding to the function to be analyzed to obtain the target kernel source code includes: assigning a first identifier to an identifier variable of the target printing function to obtain a first code to be inserted; assigning a second identifier to an identifier variable of the target printing function to obtain a second code to be inserted; inserting the first code into a first position in the initial kernel source code, and inserting the second code into a second position in the initial kernel source code to obtain the target kernel source code, wherein the first position is the previous line position of the first line of execution code in the target execution code, and the second position is the next line position of the last line of execution code in the target execution code.
[0013] Optionally, parsing the execution log data to obtain the execution time of the function to be analyzed includes: using a tag parsing tool corresponding to the tag tracking tool in the user space to parse the execution log data to determine the start execution time and the end execution time of the function to be analyzed; and obtaining the execution time based on the start execution time and the end execution time.
[0014] Optionally, the function execution time analysis method also includes: obtaining system operation data corresponding to the execution process of the function to be analyzed; using a visualization analysis tool corresponding to the mark tracking tool in the user space to perform a visualization analysis on the execution log data and the system operation data to generate data to be displayed; and displaying the data to be displayed in a graphical user interface corresponding to the operating system.
[0015] According to another aspect of an embodiment of the present application, an electronic device is also provided, including a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to execute any one of the above-mentioned function execution time analysis methods.
[0016] According to another aspect of an embodiment of the present application, a computer program product is also provided, including a computer program, which implements the function execution time consumption analysis method according to any one of the above items when executed by a processor.
[0017] In an embodiment of the present application, in the kernel space of the operating system, based on the mark writing string in the user space of the operating system, the initial printing function is renamed and encapsulated to obtain a target printing function, wherein the target printing function is used for lightweight printing in the kernel space; the target printing function is inserted into the initial kernel source code corresponding to the function to be analyzed to obtain the target kernel source code; the execution log data generated by the execution of the target kernel source code is obtained; the execution log data is parsed to obtain the execution time of the function to be analyzed. From the above, the embodiment of the present application generates a target printing function in the kernel space based on the mark writing string and the initial printing function, and the target printing function is used to track the execution time of the function to be analyzed in the kernel space, achieving the purpose of analyzing the time consumption of the function to be analyzed in the kernel space in a lightweight printing manner, thereby achieving the technical effect of reducing the negative impact of the execution time analysis scheme of the function to be analyzed in the kernel space on the system performance and enhancing the scene application capability of the execution time analysis scheme, thereby solving the technical problem in the related technical field that the scheme of executing the time consumption analysis of the kernel space function seriously affects the system performance and is not conducive to application. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] 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 the related descriptions of the embodiments are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0019] Figure 1 It is a hardware structure block diagram of a computing terminal for implementing a function execution time consumption analysis method according to an embodiment of the present application;
[0020] Figure 2 is a flow chart of a method for analyzing time consumption of function execution according to an embodiment of the present application;
[0021] Figure 3 It is a structural block diagram of a function execution time consumption analysis device according to an embodiment of the present application. DETAILED DESCRIPTION
[0022] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments only include a part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of this application.
[0023] 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 sequence. 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 an order 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.
[0024] According to an embodiment of the present application, a method embodiment of a function execution time analysis method is 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.
[0025] First, the operating environment of the above method embodiment is exemplarily described. Figure 1 is a hardware structure block diagram of a computing terminal for implementing a method for analyzing function execution time consumption according to an embodiment of the present application, such as Figure 1 As shown, a computing terminal 10 (for example, a computer terminal, a mobile smart terminal, a vehicle terminal, or a cloud computing virtual terminal, etc.) may include: one or more processors 102, a memory 104 for storing data, and a transmission device 106 for implementing a communication function, wherein each processor 102 may include but is not limited to a processing component such as a microprocessor (Microcontroller Unit, referred to as MCU) or a programmable logic device (Field Programmable Gate Array, referred to as FPGA).
[0026] The computing terminal 10 may further include: a display device 110, an input / output device 108, a Universal Serial Bus (USB) port (the USB port may be used as one of the ports of a computer bus, not shown in the figure), a network interface (not shown in the figure), a power supply (not shown in the figure) and a camera (not shown in the figure). A person skilled in the art may understand that Figure 1 The structure of the computing terminal 10 shown is only for illustration and does not impose a strict limitation on the structure of the computing terminal 10. For example, the computing terminal 10 may also include Figure 1 More or fewer components than those shown in FIG. 1 , or computing terminal 10 may have the same components as those shown in FIG. Figure 1 Different categories of components are shown.
[0027] It should be noted that the one or more processors 102 and / or other data processing circuits in the computing terminal 10 described above may be embodied in whole or in part as software, hardware, firmware, or any other combination thereof. In addition, the data processing circuit may be a single independent processing module, or may be incorporated in whole or in part into any of the other components in the computing terminal 10 (or mobile device).
[0028] 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 function execution time analysis 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, the above-mentioned function execution time analysis method is realized. 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 instances, the memory 104 may further include a memory remotely arranged relative to the processor 102, and these remote memories may be connected to the computing 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.
[0029] The transmission device 106 is used to receive or send data via a network. The specific example of the above network may include a wireless network provided by a communication provider of the computing terminal 10. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, referred to as NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 can be a radio frequency (Radio Frequency, referred to as RF) module, which is used to communicate with the Internet wirelessly.
[0030] Under the above operating environment, the embodiment of the present application provides the following Figure 2 The function shown executes a time-consuming analysis method, which is applied to the kernel space of the operating system. Figure 2 is a flow chart of a method for analyzing the time consumption of function execution according to an embodiment of the present application, such as Figure 2 As shown, the method includes the following implementation steps:
[0031] Step S201, based on the mark writing string in the user space of the operating system, the initial printing function is renamed and encapsulated to obtain a target printing function, wherein the target printing function is used for lightweight printing in the kernel space;
[0032] Step S202, inserting the target printing function into the initial kernel source code corresponding to the function to be analyzed to obtain the target kernel source code;
[0033] Step S203, obtaining execution log data generated by executing the target kernel source code;
[0034] Step S204, parsing the execution log data to obtain the execution time of the function to be analyzed.
[0035] In an embodiment of the present application, in the kernel space of the operating system, based on the mark writing string in the user space of the operating system, the initial printing function is renamed and encapsulated to obtain a target printing function, wherein the target printing function is used for lightweight printing in the kernel space; the target printing function is inserted into the initial kernel source code corresponding to the function to be analyzed to obtain the target kernel source code; the execution log data generated by the execution of the target kernel source code is obtained; the execution log data is parsed to obtain the execution time of the function to be analyzed. From the above, the embodiment of the present application generates a target printing function in the kernel space based on the mark writing string and the initial printing function, and the target printing function is used to track the execution time of the function to be analyzed in the kernel space, achieving the purpose of analyzing the time consumption of the function to be analyzed in the kernel space in a lightweight printing manner, thereby achieving the technical effect of reducing the negative impact of the execution time analysis scheme of the function to be analyzed in the kernel space on the system performance and enhancing the scene application capability of the execution time analysis scheme, thereby solving the technical problem in the related technical field that the scheme of executing the time consumption analysis of the kernel space function seriously affects the system performance and is not conducive to application.
[0036] The operating system is divided into user space and kernel space. The user space is the environment where applications and processes run, while the kernel space is where the operating system core runs and handles direct access and management of hardware resources. The above method provided in the embodiment of the present application can be applied to the kernel space.
[0037] The above operating system can be Windows operating system, macOS, iOS, Linux operating system, Android operating system, Chrome OS, Unix operating system, etc.
[0038] The following uses the Android operating system as an example to further illustrate the optional steps included in the above method steps provided in the embodiment of the present application.
[0039] As an optional implementation, the above function execution time consumption analysis method may further include the following execution steps:
[0040] Step S251, obtaining a marker tracking file corresponding to a marker tracking tool in a user space;
[0041] Step S252, parsing the graphic display data in the tag tracking file to obtain a tag writing character string.
[0042] The marker tracking tool in the user space can be a system performance tracking tool, such as the systrace tool. The systrace tool is a commonly used performance analysis tool in the Android operating system, which is used to record system and application events to help developers detect possible performance bottlenecks in the system.
[0043] In the systrace tool, the trace data is recorded in a marker trace file. The path of the marker trace file is usually " / sys / kernel / debug / tracing / trace_marker". This marker trace file stores all trace events. When developers use the systrace tool for trace analysis in user space, the systrace tool will read the data in this marker trace file.
[0044] Execute the marker tracing tool in user space, which automatically reads and collects the tracing data generated by kernel space, and the data is recorded in the marker tracing file. For example, in Android, when developers want to diagnose the performance problem of a specific application or system service, they start the systrace tool and set the tracing range through specific command line parameters. The systrace tool will start collecting event data related to these tracing ranges and store them in the " / sys / kernel / debug / tracing / trace_marker" file.
[0045] In the user space of the Android operating system, the tracing code is used to record the execution time of the function. In fact, it is to write strings in the "B|XXX" and "E|XXX" formats to the " / sys / kernel / debug / tracing / trace_marker" file. After parsing with the systrace tool, the time between the two writes can be displayed, and the function execution time can be obtained.
[0046] For example, execute the following command in the shell command line:
[0047] echo"B|9999|hello_world"> / sys / kernel / debug / tracing / trace_marker;
[0048] sleep 0.1;
[0049] echo"E|9999"> / sys / kernel / debug / tracing / trace_marker.
[0050] After execution, the systrace tool is used to display the time taken to execute the function.
[0051] In the text content of the above " / sys / kernel / debug / tracing / trace_marker" file, the graphical display data includes the following two lines of data:
[0052] First line: <...>-22374(22374)
[000] ....11731.102088:tracing_mark_write:B|9999|hello_world;
[0053] Second line: <...>-22374(22374)
[002] ....11731.239367:tracing_mark_write:E|9999.
[0054] Thus, "tracing_mark_write" in the above-mentioned graphic display data can be determined as the mark write character string.
[0055] In some exemplary embodiments, the above-mentioned graphical display data may be information displayed after the systrace tool parses the trace data into a graphical view. These graphical display data can intuitively display the occurrence time, duration, and relationship between different events (such as function execution events). In the graphical view of the systrace tool, each event or function call is represented as a line on the timeline, the length represents the duration, and the position represents the occurrence time.
[0056] For example, the developer sees a timeline of a function call in the graphical results of the systrace tool, which shows that the execution time of the function is 100 milliseconds, and there is a significant increase in memory usage at a specific time point. In order to deeply analyze the details of this function call, the developer can parse the graphical data generated by the systrace tool and trace back to the original marker write strings, such as "B|9999|func_name" and "E|9999". These strings help locate the corresponding function call in the kernel, and then analyze the execution flow and kernel status of the function.
[0057] Through the technical solutions provided by the above steps S251 to S252, the embodiment of the present application can realize efficient collection and analysis of function execution time and system status marks generated in the kernel from user space. This technical effect is crucial for performance analysis and problem diagnosis.
[0058] As an optional implementation, in the above step S201, based on the mark writing string, the initial printing function is renamed and encapsulated to obtain the target printing function, and the following execution steps may also be included:
[0059] Step S211, using a static limiting field to limit the scope of the initial printing function;
[0060] Step S212, renaming the initial printing function into a mark-writing string to obtain a renamed printing function;
[0061] Step S213: encapsulate the renamed printing function based on the preset identifier variable to obtain the target printing function.
[0062] The above statically qualified field may be a "static" field. In C / C++ programming, static qualification is used to declare variables or functions so that their scope is limited. For variables, the static qualifier makes the variable a static local variable, and its value will be saved even if the function exits. For functions, the static qualifier makes the function visible only in the file in which it is defined and cannot be called by other files.
[0063] The initial print function may be a trace_printk() function in the kernel space. The initial print function can implement lightweight printing in the kernel space. The trace_printk() function is scoped using a static qualifier field, and the trace_printk() function is declared as a static function to limit its visibility so that it can only be called in the current file or module. For example, in the kernel source code, the trace_printk() function may be declared as "static" to limit the scope of the trace_printk() function.
[0064] Furthermore, the trace_printk() function is renamed with the string "tracing_mark_write", and the resulting renamed printing function is tracing_mark_write(), which has the same function as the trace_printk() function.
[0065] By renaming the initial print function, the renamed print function can generate a tag write string that meets the parsing requirements of the systrace tool, such as "B| <pid>|<function_name>" and ”E| <pid>|<function_name> ", used to record the start and end time of a function in kernel space.
[0066] The preset identifier variable may be a variable used to generate a mark writing string. The target printing function encapsulates the renamed printing function and can realize a higher level function.
[0067] Based on the preset identifier variable, "tracing_mark_write()" is encapsulated to create a new function "tracing_start_end()", which accepts parameters such as the start tag "B", the end tag "E" and the function name "__func__", calls the "tracing_mark_write()" function internally, and generates a complete "B| <pid>|<function_name>" and ”E| <pid>|<function_name> " formatted markers are written into the string. For example, at the beginning and end of the kernel function "test_func()", "tracing_start_end('B',(char*)__func__);" and "tracing_start_end('E',(char*)__func__);" are called respectively to record the start and end time of the function call.
[0068] In an exemplary application scenario, the trace module of the kernel space provides a trace_printk() function, which can realize lightweight printing and can be used in the kernel hot path, but the log printed by the trace_printk() function will not be displayed in a graphical way, and it is impossible to analyze it in combination with the system operation status. The usage of the trace_printk() function is as follows.
[0069] void test_func_a()
[0070] {
[0071] trace_printk("hello_world");
[0072] }
[0073] The log data printing format of the trace_printk() function is as follows.
[0074] <...>-22374(22374)
[000] ....12191.058345:test_func_a:hello_world
[0075] It is easy to find that the print format of the log data of the trace_printk() function is similar to the format of the graphical display data. In this regard, after renaming the trace_printk() function to tracing_mark_write(), the log data in the following print format is obtained.
[0076] <...>-22374(22374)
[000] ....12191.058345:tracing_mark_write:hello_world
[0077] Therefore, after the name change, the similarity between the log data of the renamed printing function and the above-mentioned graphic display data can be improved.
[0078] Through the technical solutions provided by the above steps S211 to S213, the embodiment of the present application can realize time-consuming lightweight recording of function execution in the kernel space, and integrate these records into the tracing data of systrace in the form of marked write strings, and finally present them to the developer in a graphical form.
[0079] As an optional implementation, in the above step S213, based on the preset identifier variable, the renamed printing function is encapsulated to obtain the target printing function, which may also include the following execution steps:
[0080] Step S2131: based on the identifier variable and at least one variable corresponding to the initial printing function, encapsulate the renamed printing function to obtain a target printing function.
[0081] Furthermore, the renamed printing function tracing_mark_write() is encapsulated, and "B| <pid>” and “E| <pid>", construct the target printing function tracing_start_end().
[0082] Specifically, the above identifier variable may be a variable of string type, named be.
[0083] The encapsulation implementation process of the above target printing function is as follows.
[0084] static noinline int tracing_mark_write(const char*buf)
[0085] {
[0086] trace_printk(buf);
[0087] return 0;
[0088] }
[0089] void tracing_start_end(char be,char*str,int val)
[0090] {
[0091] char buf
[256] ;
[0092] snprintf(buf,sizeof(buf),"%c|%d|%s\n",be,current->tgid,str);
[0093] tracing_mark_write(buf);
[0094] }
[0095] It should be noted that the above identifier variable be takes the value of "B" or "E", the variable str represents the string content originally to be printed by the function, and the variable val represents the value originally to be printed by the function. The above variables str and val are at least one variable corresponding to the initial printing function.
[0096] In an exemplary application scenario, encapsulation processing: encapsulation of the "tracing_start_end()" function means that it will accept an identifier variable (start or end mark) and at least one variable (usually a function name and the current process ID), and internally call the "tracing_mark_write()" function to construct and write a mark write string that meets the format requirements. For example, in the kernel source code, when the "tracing_start_end()" function is called, it will generate and write "B|" according to the passed in identifier variable "'B'" or "'E'", the function name "__func__" and the current process ID "current->tgid". <pid>|<function_name> "or" E| <pid>|<function_name> " tag is written into the string.
[0097] Through the encapsulation processing of step S2131, the technical solution of the embodiment of the present application achieves the following technical effects: standardized tag writing string generation, simplified kernel source code modification, and lightweight performance impact of the function time consumption analysis solution.
[0098] As an optional implementation, the candidate values of the identifier variable include: a first identifier and a second identifier, wherein the first identifier is used to identify the start execution time of the function to be analyzed, and the second identifier is used to identify the end execution time of the function to be analyzed.
[0099] The first identifier may be "B", which is used to identify the start time of the function to be analyzed. In the context of tracing and performance analysis, "B" stands for "Begin". When a function starts executing, it leaves a mark in the tracing log, indicating that the execution time of the function will be measured from this moment.
[0100] The second identifier may be "E". In contrast to the first identifier, "E" stands for "End", which is used to mark the end of the function execution, that is, the time point when the function completes all its operations. In the log, the "E" tag is paired with the "B" tag to calculate the actual execution time of the function.
[0101] In the kernel source code, by inserting the code that calls the "tracing_mark_write()" function at the entry and exit of the function to be analyzed, and using "tracing_mark_write()" as the target printing function, the identifier variable can be used to record the execution time of the function.
[0102] By using "B" and "E" as identifier variables, the embodiment of the present application can record and analyze the execution of kernel functions in a low-intrusive, high-performance manner, providing a powerful tool for system-level performance optimization and problem location.
[0103] As an optional implementation, the initial kernel source code includes a target execution code corresponding to the function to be analyzed; in the above step S202, the target print function is inserted into the initial kernel source code corresponding to the function to be analyzed to obtain the target kernel source code, and the following execution steps may also be included:
[0104] Step S221, assigning a first identifier to the identifier variable of the target print function to obtain a first code to be inserted;
[0105] Step S222, assigning a second identifier to the identifier variable of the target print function to obtain a second code to be inserted;
[0106] Step S223, inserting the first code into the first position in the initial kernel source code, and inserting the second code into the second position in the initial kernel source code, to obtain the target kernel source code, wherein the first position is the position of the previous line of the first line of execution code in the target execution code, and the second position is the position of the next line of execution code in the last line of execution code in the target execution code.
[0107] The initial kernel source code may be an unmodified kernel source code, including the original implementation of all kernel functions and process control logic. The function to be analyzed may be a function in a specific kernel module or file that needs to be subjected to performance analysis and time measurement. The execution code of the function to be analyzed is the target execution code.
[0108] For example, the initial kernel source code is as follows.
[0109] void test_func_b()
[0110] {
[0111] XXX / / Function execution content (that is, target execution code)
[0112] }
[0113] Based on the above target printing function tracing_start_end(), the identifier variable is assigned to "B" to obtain the following first code.
[0114] tracing_start_end('B',(char*)__func__)
[0115] Assign the identifier variable to "B" and obtain the following second code.
[0116] tracing_start_end('E',(char*)__func__)
[0117] The first code is inserted before the target execution code, and the second code is inserted after the target execution code, to obtain the following target kernel source code.
[0118] void test_func_b()
[0119] {
[0120] tracing_start_end('B',(char*)__func__);
[0121] XXX / / Function execution content
[0122] tracing_start_end('E',(char*)__func__);
[0123] }
[0124] In an exemplary application scenario, the above constructed tracing_start_end() call codes are inserted inside the function to be analyzed, specifically at the beginning and end of the function. The first code (the call of the "B" identifier) is inserted before the first line of execution code of the target execution code, while the second code (the call of the "E'" identifier) is inserted after the last line of execution code of the target execution code. In this way, during each call of the function, its start and end time will be automatically recorded, and the corresponding mark writing string will be generated for subsequent performance analysis.
[0125] Through the above steps S221 to S223, the embodiment of the present application records the execution time of functions by inserting specific codes into the kernel source code, and combines the graphical display of system status information to provide an efficient and intuitive kernel performance analysis method, which helps to quickly locate and solve performance problems in complex systems.
[0126] As an optional implementation, in the above step S204, parsing the execution log data to obtain the execution time of the function to be analyzed may also include the following execution steps:
[0127] Step S241, using a tag parsing tool corresponding to the tag tracking tool in the user space to parse the execution log data, and determine the start execution time and the end execution time of the function to be analyzed;
[0128] Step S242, obtaining the execution time according to the execution start time and the execution end time.
[0129] Through the above method steps of the embodiment of the present application, the printing format of the execution log data of the target print function can be made consistent with the format of the above graphic display data. Based on this, a tag parsing tool (such as the systrace parsing website) is used to parse the above execution log data to determine the start and end execution times of the function to be analyzed, and further calculate the execution time of the function.
[0130] In an exemplary application scenario, in user space, the trace log generated by the kernel (stored in the " / sys / kernel / debug / tracing / trace_marker" file) is read through marker tracing tools such as systrace. Subsequently, the marker parsing tool parses these log data to identify the "B" and "E" markers and their corresponding process IDs and function names, thereby determining the start and end execution times of the function to be analyzed. For example, for the "B|9999|test_func" record in the log, the parsing tool will recognize that this is the start execution marker of the function "test_func" in the process with process ID "9999", and record the corresponding system timestamp. Similarly, the "E|9999|test_func" record is parsed as a marker for the end of function execution.
[0131] Furthermore, the tag parsing tool determines the start and end execution times of the function to be analyzed, and it calculates the time difference between the two to obtain the execution time of the function. This time is usually measured in milliseconds (ms), microseconds (us), or nanoseconds (ns). For example, if the start execution time of the function "test_func" is "1234567890ns" and the end execution time is "1234568880ns", the tag parsing tool will calculate the execution time of the function as "1000ns".
[0132] Through the above steps, the technical solution of the embodiment of the present application effectively parses the function execution time consumption data generated by the kernel in the user space, and realizes efficient timestamp extraction. Specifically, the tag parsing tool can quickly and accurately extract the timestamp information corresponding to the "B" and "E" tags from the tracking log, without the need to manually consult massive logs, thereby improving the efficiency of performance analysis. The embodiment of the present application can also achieve accurate execution time measurement. Specifically, based on the precise timestamp, the actual time consumption of each function call can be calculated. This is crucial for diagnosing performance bottlenecks, understanding function call patterns, and optimizing system response time.
[0133] As an optional implementation, the above function execution time consumption analysis method may further include the following execution steps:
[0134] Step S261, obtaining system operation data corresponding to the execution process of the function to be analyzed;
[0135] Step S262, using a visual analysis tool corresponding to the tag tracking tool in the user space to perform visual analysis on the execution log and the system operation data to generate data to be displayed;
[0136] Step S263: display the data to be displayed in the graphical user interface corresponding to the operating system.
[0137] The above system operation data may be operation status data of various aspects of the system during the execution of the function to be analyzed, including but not limited to CPU usage, memory usage, I / O operations, interruption status, preemption status, etc. System operation data is crucial for understanding the context of function execution and its impact on the overall performance of the system.
[0138] In an exemplary application scenario, during the execution of the function to be analyzed, the kernel itself will continuously generate log data of the system operation status. This data will be recorded in the kernel's tracing log (stored in the " / sys / kernel / debug / tracing / trace_marker" file) and includes all events related to system performance. For example, in the Android system, events such as CPU scheduling, memory allocation, network I / O, and disk I / O will be recorded to provide rich information for subsequent performance analysis.
[0139] The above-mentioned visual analysis tool can be a graphical analysis component in the systrace tool. The visual analysis tool can convert the tracking log and system operation data into intuitive graphical information to facilitate user understanding and analysis.
[0140] In an exemplary application scenario, in user space, the systrace tool converts the trace logs (including execution logs and system operation data) generated by the kernel into a graphical timeline representation. This process includes identifying the start and end markers of the function, extracting system operation status events, and combining these events with the function execution time on the timeline. For example, when the systrace tool parses the "B" and "E" markers, it converts them into the start and end points on the timeline, and displays the system operation data such as CPU load and memory usage in the same period as the function execution, generating data to be displayed.
[0141] Furthermore, in the Android operating system, the graphical user interface of the systrace tool will display a series of timelines, with bars of different colors and shapes representing different system events or function executions, and an overlay chart of system resource usage (such as CPU and memory usage). Developers or system engineers can quickly locate performance bottlenecks and understand how system resources are affected by function calls by viewing these charts.
[0142] Through the above steps S261 to S263, the technical solution provided in the embodiment of the present application realizes a comprehensive analysis of system operation data and function execution time, and displays it to the user in a graphical manner.
[0143] When encountering performance bottlenecks in Android or similar complex operating system environments, developers can adopt the technical solution provided by the embodiment of the present application, and achieve lightweight recording of the time-consuming function execution by embedding "B" and "E" tags in the kernel key functions. The embodiment of the present application can not only accurately capture the execution time of a single function, but also synchronously track system operation data, such as CPU usage, memory occupancy, etc., providing users with a comprehensive performance analysis picture. With the help of visual analysis tools, such as the graphical user interface of the systrace tool, these recorded data are converted into intuitive timelines and resource occupancy trend charts, making performance analysis clear at a glance. For example, if the CPU usage or memory occupancy rate rises sharply during the execution of a kernel function, this will directly point to the performance problems that may exist in the function, such as excessive resource consumption or inefficient algorithms. The graphical display method greatly improves the diagnostic efficiency of technicians, helps technicians quickly locate problems in complex systems, and understand the impact of problem functions on overall performance, so that they can optimize code in a targeted manner and improve system response speed and stability. What is particularly critical is that the marking and recording performed by this method at the kernel level has minimal intrusion on system performance, ensuring that when deployed in a production environment, in-depth and accurate performance monitoring can be performed without interfering with the normal operation of the system. Overall, the technical solution of the embodiment of the present application provides an efficient, intuitive and low-intrusive solution for system-level performance optimization through graphical and comprehensive analysis, which is particularly suitable for deployment in production environments that require high stability and response speed, greatly accelerating the identification and repair process of performance problems.
[0144] 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 countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0145] It should be noted that, for the above method embodiments, for the sake of simplicity, the technical solutions in the method embodiments are described as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the order of actions in the described action combinations, because according to the present application, some of the above steps can be performed in other orders or simultaneously. In addition, those skilled in the art should also be aware that the embodiments described in the specification of this application belong to preferred embodiments, and the actions and modules involved are not necessarily necessary to implement the technical solutions of the present application.
[0146] According to an embodiment of the present invention, a device embodiment of a function execution time consumption analysis device is also provided, and the function execution time consumption analysis device is used to implement the above method embodiment and various optional implementation methods of the method embodiment, and the technical content that has been described in the previous text is no longer repeated in the device embodiment. It should be noted that in the following related descriptions of the device embodiment, "module" can be software, hardware, or a combination of software and hardware for implementing a specified function.
[0147] Figure 3 is a structural block diagram of a function execution time consumption analysis device according to an embodiment of the present application, such as Figure 3 As shown, the function execution time analysis device includes: a processing module 301, which is used to write a string based on a tag in the user space of the operating system, rename and encapsulate the initial printing function, and obtain a target printing function, wherein the target printing function is used for lightweight printing in the kernel space; an insertion module 302, which is used to insert the target printing function into the initial kernel source code corresponding to the function to be analyzed, and obtain the target kernel source code; an acquisition module 303, which is used to obtain the execution log data generated by the execution of the target kernel source code; and a parsing module 304, which is used to parse the execution log data to obtain the execution time of the function to be analyzed.
[0148] Optionally, the acquisition module 303 is further used to: acquire a mark tracking file corresponding to the mark tracking tool in the user space; and parse the graphic display data in the mark tracking file to obtain a mark writing character string.
[0149] Optionally, the processing module 301 is also used to: use a static limit field to limit the scope of the initial printing function; rename the initial printing function into a marker and write it into a string to obtain a renamed printing function; based on a preset identifier variable, encapsulate the renamed printing function to obtain a target printing function.
[0150] Optionally, the processing module 301 is further configured to: encapsulate the renamed printing function based on the identifier variable and at least one variable corresponding to the initial printing function to obtain a target printing function.
[0151] Optionally, in the above-mentioned function execution time analysis device, the candidate values of the identifier variable include: a first identifier and a second identifier, wherein the first identifier is used to identify the start execution time of the function to be analyzed, and the second identifier is used to identify the end execution time of the function to be analyzed.
[0152] Optionally, the initial kernel source code includes a target execution code corresponding to the function to be analyzed; the above-mentioned insertion module 302 is also used to: assign a first identifier to the identifier variable of the target printing function to obtain a first code to be inserted; assign a second identifier to the identifier variable of the target printing function to obtain a second code to be inserted; insert the first code into a first position in the initial kernel source code, and insert the second code into a second position in the initial kernel source code to obtain a target kernel source code, wherein the first position is the previous line position of the first line of execution code in the target execution code, and the second position is the next line position of the last line of execution code in the target execution code.
[0153] Optionally, the above-mentioned analysis module 304 is also used to: use the mark analysis tool corresponding to the mark tracking tool in the user space to parse the execution log data, determine the start execution time and the end execution time of the function to be analyzed; and obtain the execution time based on the start execution time and the end execution time.
[0154] Optionally, the above-mentioned function execution time analysis device also includes a visualization module, which is used to: obtain system operation data corresponding to the execution process of the function to be analyzed; use a visualization analysis tool corresponding to the mark tracking tool in the user space to perform visualization analysis on the execution log data and the system operation data to generate data to be displayed; and display the data to be displayed in the graphical user interface corresponding to the operating system.
[0155] It should be noted that the above-mentioned processing module 301, insertion module 302, acquisition module 303 and parsing module 304 correspond to steps S201 to S204 in the method embodiment. The four modules are the same as the instances and application scenarios implemented by the corresponding steps, but are not limited to the contents disclosed in the above-mentioned method embodiment.
[0156] It should be noted that the various modules mentioned in the above device embodiments can be implemented by software, hardware, or a combination of software and hardware. For example, when hardware is used to implement the above modules, the various modules can be set in the same processor, or the various modules can be set in different processors in any combination. For another example, the above modules can be hardware components or software components stored in a memory (e.g., memory 104) and processed by one or more processors (e.g., processors 102a, 102b, ..., 102n), and the above modules can also be run as part of the device on a processor such as Figure 1 In the computing terminal 10 shown.
[0157] According to an embodiment of the present invention, an embodiment of an electronic device is also provided. The electronic device includes a memory and a processor. The memory stores a computer program. The processor is configured to run the computer program to implement the above-mentioned function execution time consumption analysis method.
[0158] According to an embodiment of the present invention, a computer-readable storage medium is also provided, the computer-readable storage medium including a stored program, wherein when the program is running, the device where the computer-readable storage medium is located is controlled to implement the above-mentioned function execution time consumption analysis method.
[0159] Optionally, the above-mentioned computer storage media may include but are not limited to: hard disk drive (HDD), solid state drive (SSD), USB flash drive, optical disk, memory card, cloud storage media and network storage device (NAS).
[0160] Optionally, the above-mentioned computer-readable storage medium can be configured to store a computer program for performing the following steps: writing a string based on a tag in the user space of the operating system, renaming and encapsulating the initial printing function, and obtaining a target printing function, wherein the target printing function is used for lightweight printing in the kernel space; inserting the target printing function into the initial kernel source code corresponding to the function to be analyzed, and obtaining the target kernel source code; obtaining the execution log data generated by the execution of the target kernel source code; parsing the execution log data to obtain the execution time of the function to be analyzed.
[0161] According to an embodiment of the present invention, a computer program product is also provided. The computer program product includes a computer program, and when the computer program is executed by a processor, the above-mentioned function execution time consumption analysis method can be implemented.
[0162] Optionally, the computer program product may provide a function execution time consumption analysis service based on the function execution time consumption analysis method.
[0163] Optionally, in this embodiment, the computer program product may be a set of instructions and codes pre-written according to the function execution time analysis method. The computer program product may run on various computer platforms, including personal computers, servers, mobile devices, etc.
[0164] Optionally, in this embodiment, the instructions and codes corresponding to the computer program product are used to implement the following method steps: based on the tag writing string in the user space of the operating system, the initial printing function is renamed and encapsulated to obtain the target printing function, wherein the target printing function is used for lightweight printing in the kernel space; the target printing function is inserted into the initial kernel source code corresponding to the function to be analyzed to obtain the target kernel source code; the execution log data generated by the execution of the target kernel source code is obtained; the execution log data is parsed to obtain the execution time of the function to be analyzed.
[0165] In the above-mentioned multiple embodiments of the present application, the description of each embodiment has different emphases. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0166] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of multiple modules can be a logical function division, and there can be any other possible division methods when actually implemented in the application scenario; for another example, multiple modules (or units or components in the modules) can be combined with each other and can be integrated into another system. For example, some features of the method embodiments described above can be ignored or skipped.
[0167] It should be noted that in the above-mentioned embodiments, the modules, components or units described as separate parts can be physically separated or physically integrated. The parts displayed as modules or units can be physical modules or physical units, or virtual modules or virtual units, that is, multiple modules or multiple units can be in the same position, or distributed in multiple positions or multiple spaces. In the application scenario, according to the actual needs of the scenario, some or all of the multiple modules or multiple units can be selected to implement the technical solutions of the embodiments of the present application, thereby achieving the corresponding technical objectives.
[0168] In particular, for an integrated functional module or functional unit, if it is implemented in the form of a software functional unit and sold or used as an independent product, the module or functional unit can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application can essentially or in other words, the part that contributes to the prior art or the whole or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes a number of instructions for a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application.
[0169] The above is only a preferred implementation 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.< / pid> < / pid> < / pid> < / pid> < / pid> < / pid> < / pid> < / pid>
Claims
1. A method for analyzing the time consumption of function execution, characterized in that: Applicable to the kernel space of the operating system, including: Based on the mark writing string in the user space of the operating system, the initial printing function is renamed and encapsulated to obtain a target printing function, wherein the target printing function is used to perform lightweight printing in the kernel space; Inserting the target printing function into the initial kernel source code corresponding to the function to be analyzed to obtain the target kernel source code; Obtaining execution log data generated by executing the target kernel source code; The execution log data is parsed to obtain the execution time of the function to be analyzed.
2. The method for analyzing the time consumption of function execution according to claim 1, characterized in that: The function execution time consumption analysis method also includes: Obtaining a marker tracking file corresponding to the marker tracking tool in the user space; The graphic display data in the tag tracking file is parsed to obtain the tag writing character string.
3. The method for analyzing the time consumption of function execution according to claim 1, characterized in that: Based on the mark writing string, the initial printing function is renamed and encapsulated to obtain the target printing function including: Using static limiting fields to limit the scope of the initial printing function; Rename the initial print function to the mark-writing character string to obtain a renamed print function; Based on the preset identifier variable, the renamed printing function is encapsulated to obtain the target printing function.
4. The method for analyzing the time consumption of function execution according to claim 3, characterized in that: Based on the preset identifier variable, the renamed printing function is encapsulated to obtain the target printing function including: Based on the identifier variable and at least one variable corresponding to the initial print function, the renamed print function is encapsulated to obtain the target print function.
5. The method for analyzing the time consumption of function execution according to claim 3 or 4, characterized in that: The candidate values of the identifier variable include: a first identifier and a second identifier, wherein the first identifier is used to identify the start execution time of the function to be analyzed, and the second identifier is used to identify the end execution time of the function to be analyzed.
6. The method for analyzing the time consumption of function execution according to claim 5, characterized in that: The initial kernel source code includes the target execution code corresponding to the function to be analyzed; Inserting the target printing function into the initial kernel source code corresponding to the function to be analyzed, and obtaining the target kernel source code comprises: Assign the first identifier to the identifier variable of the target print function, and obtain the first code to be inserted; Assign the identifier variable of the target print function the second identifier to obtain the second code to be inserted; The first code is inserted into a first position in the initial kernel source code, and the second code is inserted into a second position in the initial kernel source code to obtain the target kernel source code, wherein the first position is a position of a line above the first line of execution code in the target execution code, and the second position is a position of a line below the last line of execution code in the target execution code.
7. The method for analyzing the time consumption of function execution according to claim 1, characterized in that: Parsing the execution log data to obtain the execution time of the function to be analyzed includes: Using a tag parsing tool corresponding to the tag tracking tool in the user space to parse the execution log data, and determine the start execution time and the end execution time of the function to be analyzed; The execution time is obtained according to the execution start time and the execution end time.
8. The method for analyzing the time consumption of function execution according to claim 7, characterized in that: The function execution time consumption analysis method also includes: Obtaining system operation data corresponding to the execution process of the function to be analyzed; Using a visual parsing tool corresponding to the tag tracking tool in the user space, visually parsing the execution log data and the system operation data to generate data to be displayed; The data to be displayed is displayed in a graphical user interface corresponding to the operating system.
9. An electronic device, characterized in that: It comprises a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the function execution time consumption analysis method as described in any one of claims 1 to 8.
10. A computer program product, characterized in that The invention comprises a computer program, which, when executed by a processor, implements the method for analyzing the time consumption of function execution according to any one of claims 1 to 8.