Code file processing method and function execution performance monitoring method

By injecting hook functions during the compilation process of the Unity compiler to monitor function performance, it solves the problems of excessively large application installation files and increased running time in the existing technology, achieves precise control and efficient performance monitoring, and reduces the cost of the application. Volume and operating losses.

CN118733047APending Publication Date: 2024-10-01WUHAN XINGJI MEIZU TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202410741483.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-07
Publication Date
2024-10-01

AI Technical Summary

Technical Problem

The existing technology causes the application installation file to be too large and the running time to increase during the function performance monitoring process, and cannot accurately control the functions that need to be monitored, affecting the performance and efficiency of the application.

Method used

Compile the set language code into an intermediate code file by monitoring the Unity compiler, obtain and parse all functions, inject hook functions into the functions that need to be monitored based on the preset injection rules, generate a new intermediate code file, and build the application installation file. And call the hook function at runtime for performance monitoring.

Benefits of technology

It reduces the amount of code in the application installation file, reduces the size of the application, improves operating efficiency, and supports selecting business codes for monitoring by namespace, reducing performance loss.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118733047A_ABST
    Figure CN118733047A_ABST
Patent Text Reader

Abstract

The invention relates to a code file processing method and a function execution performance monitoring method, and relates to the technical field of computers.The method comprises the steps that a monitoring Unity uses a first compiler to compile a first set language code into a first intermediate code file; when it is detected that generation of the first intermediate code file is finished, the first intermediate code file is obtained and analyzed, and all functions in the first intermediate code file are obtained; on the basis of all the functions and a preset injection rule, determining a function of which the performance needs to be monitored from all the functions, injecting a hook function into the function of which the performance needs to be monitored, and generating a second intermediate code file on the basis of the function of which the performance needs to be monitored after the hook function is injected; and constructing an application installation file of the application based on the second intermediate code file. According to the method, the function needing performance monitoring can be accurately controlled, the code quantity in the installation file is reduced, the size of the application program installation file is reduced, and interference to the code compiling process is avoided.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to the field of computer technology, and in particular to a method for processing a code file and a method for monitoring function execution performance. Background Art

[0002] In order to improve the performance of an application, it is often necessary to monitor the performance of at least some of the functions that need to be called during the application's operation, so as to obtain the execution performance of the corresponding functions for analyzing and optimizing the corresponding functions. Existing technologies for monitoring the performance of functions often lead to problems such as the installation file of the developed application being too large or the application's operation time increasing. Summary of the invention

[0003] The code file processing method and function execution performance monitoring method provided by the embodiments of the present invention can at least accurately control the functions whose performance needs to be monitored, reduce the amount of code in the application installation file, reduce the size of the application installation file, and improve the operating efficiency of the application.

[0004] According to one aspect of the present application, a method for processing a code file is provided, the method comprising: monitoring Unity using a first compiler to compile a first set language code into a first intermediate code file; when detecting that the generation of the first intermediate code file is completed, acquiring and parsing the first intermediate code file to obtain all functions in the first intermediate code file; based on all the functions and preset injection rules, determining functions that need to monitor performance from all the functions and injecting a hook function into the functions that need to monitor performance, the hook function being used to generate running performance data of the functions that need to monitor performance during the process of the application executing the functions that need to monitor performance; generating a second intermediate code file based on the functions that need to monitor performance after the injection of the hook function; and constructing an application installation file of the application based on the second intermediate code file.

[0005] According to another aspect of the present application, a method for monitoring function execution performance is also provided, the method comprising: obtaining an application installation file of an application; the application installation file is constructed based on a second intermediate code file; the second intermediate code file is an application installation file device, and when detecting that Unity uses a first compiler to compile a first set language code into a first intermediate code file, the device obtains and parses the first intermediate code file to obtain all functions in the first intermediate code file; based on all the functions and preset injection rules, determining the functions whose performance needs to be monitored from all the functions and injecting a hook function for the functions whose performance needs to be monitored into the first intermediate code file; installing the application based on the application installation file; and calling the hook function to monitor the execution performance during the running of the application.

[0006] According to another aspect of the present application, an electronic device is provided, comprising: a memory, a processor, and a computer program stored in the memory, wherein the processor implements the above method when executing the computer program.

[0007] According to another aspect of the present application, a computer-readable storage medium is provided, wherein a computer program is stored in the computer-readable storage medium, and the computer program implements the above method when executed by a processor.

[0008] The processing method of the code file provided by the embodiment of the present invention monitors Unity to use the first compiler to compile the first set language code into the first intermediate code file; when it is detected that the generation of the first intermediate code file is completed, the first intermediate code file is obtained and parsed to obtain all functions in the first intermediate code file; based on the all functions and the preset injection rules, the function that needs to monitor the performance is determined from the all functions and the hook function is injected into the function that needs to monitor the performance, and the hook function is used to generate the running performance data of the function that needs to monitor the performance during the process of the application executing the function that needs to monitor the performance; based on the function that needs to monitor the performance after the injection of the hook function, a second intermediate code file is generated; based on the second intermediate code file, the application installation file of the application is constructed. The embodiment of the present invention generates the running performance data of the function that needs to monitor the performance by injecting the hook function in a targeted manner, and can accurately control the function that needs to monitor the performance. By accurately controlling the function that needs to monitor the performance, compared with comprehensively monitoring all functions, it can not only reduce the amount of code in the application installation file, reduce the volume of the application installation file, but also improve the running efficiency of the application. In addition, when it is detected that Unity has finished compiling the first setting language code into the first intermediate code using the first compiler, a hook function for the function whose performance needs to be monitored is injected into the first intermediate code, thereby avoiding interference with the code compilation process. BRIEF DESCRIPTION OF THE DRAWINGS

[0009] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the prior art descriptions. Obviously, the drawings described below are only some embodiments of the present invention, and for ordinary technicians in this field, other embodiments can be obtained based on these drawings without creative work.

[0010] Figure 1 It is a flow chart of a method for processing a code file according to an embodiment of the present invention;

[0011] Figure 2 It is a flowchart of the existing function execution performance capture solution;

[0012] Figure 3 It is a flow chart of the processing method of existing code files;

[0013] Figure 4 It is a data processing flow chart of a method for processing a code file according to an embodiment of the present invention;

[0014] Figure 5 It is a data processing flow chart of a method for monitoring function execution performance according to an embodiment of the present invention;

[0015] Figure 6 It is a schematic diagram of data usage of a method for monitoring function execution performance according to an embodiment of the present invention;

[0016] Figure 7 It is a flow chart of a method for monitoring function execution performance of an embodiment of the present invention;

[0017] Figure 8 It is a schematic diagram of the display interface content of the method for monitoring the function execution performance of an embodiment created by the present invention;

[0018] Fig. 9 It is a structural schematic diagram of the electronic device created by the present invention;

[0019] Fig.10 It is the import Mono Cecil tool display interface created by the present invention;

[0020] Fig.11 This is a schematic diagram of the DLL file acquisition path created by the present invention;

[0021] Fig.12 It is a schematic diagram of the IL instruction created by the present invention. DETAILED DESCRIPTION

[0022] The embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although certain embodiments of the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be construed as being limited to the embodiments described herein, which are instead provided to provide a more thorough and complete understanding of the present invention. It should be understood that the drawings and embodiments of the present invention are only for exemplary purposes and are not intended to limit the scope of protection of the present invention.

[0023] When developing applications for Unity (a real-time 3D interactive content creation and operation platform), Unity's Profiler is usually used to capture performance data and analyze the time consumption of the business's own functions. Figure 2As shown in the flowchart of the existing function execution performance capture solution, the existing Profiler performance data capture cost is too high, and it is necessary to build a development package, which increases the package size by 20MB-30MB. In addition, the average time consumed by the captured function is 10 times higher than the normal operation. All upper-level codes will be monitored, which is not conducive to the release of stable versions. The monitoring process has a great loss in performance, and the CPU time consumption increases by 10 times. In addition, since all upper-level codes will be monitored, the corresponding business code cannot be specified, the developer cannot intervene, and most of the detailed function time consumption data cannot be obtained. If the developer wants to perform code performance analysis in the user's production environment, it will be affected.

[0024] Based on this, the present invention provides a method for processing code files and a method for monitoring function execution performance. The method optimizes the overall process through IL HOOK (Intermediate Language Hook, a dynamic modification technology for .NET framework applications) technology, injects time-consuming detection code in the compilation stage, outputs the time-consuming Log (log file) locally and uses the Log analysis client to filter and generate charts, and remote users report data to the server through a point-of-care interface for big data analysis. Compared with the original solution, the advantage is that the package size is reduced by 20MB-30MB, 10 times the performance loss is saved, and the corresponding business injection is supported by namespace selection, making it more flexible and lightweight, local and remote debugging more convenient, and the compilation stage allows business personnel to modify without perception.

[0025] In an embodiment of the present invention, a method for processing a code file is provided. Figure 1 is a flowchart of a method for processing a code file according to an embodiment of the present application. Figure 1 The method steps involved are described.

[0026] Step S101, monitoring Unity using a first compiler to compile a first set language code into a first intermediate code file.

[0027] In the embodiment of the present invention, the first set language code is a source code generated by using a programming language, wherein the programming language may be a .NET (a development framework) language supported by Unity, such as C#, Boo, JavaScript (UnityScript), etc.

[0028] Unity uses the first compiler to compile the source code into IL (Intermediate Language) code, wherein the first compiler may include but is not limited to: JIT (Just-In-Time) compiler, C# compiler (csc.exe / Roslyn), VB.NET compiler (vbc.exe), F# compiler (fsc.exe), etc., that is, Unity uses the first compiler to compile the first set language code to obtain the first intermediate code file. Among them, the type of the first compiler can be selected according to actual needs. The first code file is a DLL (Dynamic Link Library) file.

[0029] In an embodiment of the present invention, the process of compiling the first intermediate code file in Unity is monitored. When it is detected that Unity has finished compiling the first set language code into the first intermediate code using the first compiler, a hook function for the function whose performance needs to be monitored is injected into the first intermediate code, thereby avoiding interference with the code compilation process.

[0030] It should be noted that DLL is a file format for storing IL code in .NET assemblies, which allows IL code to be encapsulated and reused.

[0031] Step S102, when it is detected that the generation of the first intermediate code file is completed, the first intermediate code file is acquired and parsed to obtain all functions in the first intermediate code file.

[0032] In the embodiment of the present invention, when it is detected that the generation of the first intermediate code file is completed, the IL code in the DLL can be viewed through a tool such as ILDASM (IL disassembler), that is, the first intermediate code file is obtained.

[0033] In the IL code, each method usually begins with metadata describing the basic information of the method, such as method name, return type, parameter list, access modifier (public, private, etc.) and method attributes (such as .static, .virtual). With this information, the signature of each function can be determined, that is, the first intermediate code file is parsed to obtain all functions in the first intermediate code file.

[0034] Step S103, based on all the functions and preset injection rules, determine the function that needs to monitor performance from all the functions and inject a hook function into the function that needs to monitor performance, wherein the hook function is used to generate running performance data of the function that needs to monitor performance during the process of the application executing the function that needs to monitor performance.

[0035] In an embodiment of the present invention, the injection rules can be used to filter out qualified functions from all functions, and the specific filtering conditions can be set according to actual needs, so as to determine the functions whose performance needs to be monitored from all the functions based on these filtering conditions, that is, based on all the functions and preset injection rules, determine the functions whose performance needs to be monitored from all the functions.

[0036] The IL code can be modified through tools or custom programs, that is, a hook function can be injected into the function that needs to monitor performance. The hook function can be injected into each function that needs to monitor performance, or it can be injected into a specific function that needs to monitor performance, so as to achieve precise control of the function that needs to monitor performance. By precisely controlling the function that needs to monitor performance, compared with comprehensive monitoring of all functions, it can not only reduce the amount of code in the application installation file and the size of the application installation file, but also improve the running efficiency of the application. Among them, the hook function is used to generate the running performance data of the function that needs to monitor performance during the process of the application executing the function that needs to monitor performance. Among them, the running performance data may include but is not limited to: running time, memory usage and other data.

[0037] Step S104, generating a second intermediate code file based on the function whose performance needs to be monitored after the hook function is injected.

[0038] In the embodiment of the present invention, the function whose performance needs to be monitored after the hook function is injected is the modified IL code. IL Hook modifies the intermediate representation of the program, and these modifications can generate machine language codes of various platforms when the program is executed, depending on the target hardware architecture of the program.

[0039] The modified IL code can be recompiled using ILASM (IL Assembler) or more advanced libraries such as Mono.Cecil, dnlib, etc. to obtain a second intermediate code file.

[0040] The type of the second intermediate code file depends on the operating system and processor architecture on which the application is running. For example, the second intermediate code file may be a C++ code file.

[0041] Step S105: construct an application installation file of the application based on the second intermediate code file.

[0042] In an embodiment of the present invention, based on the application installation file, after the application is installed, during the operation of the application, the hook function in the second intermediate code file can be called to monitor the execution performance of the function whose performance needs to be monitored.

[0043] The processing method of the code file provided by the embodiment of the present invention monitors Unity to use the first compiler to compile the first set language code into the first intermediate code file; when it is detected that the generation of the first intermediate code file is completed, the first intermediate code file is obtained and parsed to obtain all functions in the first intermediate code file; based on the all functions and the preset injection rules, the function that needs to monitor the performance is determined from the all functions and the hook function is injected into the function that needs to monitor the performance, and the hook function is used to generate the running performance data of the function that needs to monitor the performance during the process of the application executing the function that needs to monitor the performance; based on the function that needs to monitor the performance after the injection of the hook function, a second intermediate code file is generated; based on the second intermediate code file, the application installation file of the application is constructed. The embodiment of the present invention generates the running performance data of the function that needs to monitor the performance by injecting the hook function in a targeted manner, and can accurately control the function that needs to monitor the performance. By accurately controlling the function that needs to monitor the performance, compared with comprehensively monitoring all functions, it can not only reduce the amount of code in the application installation file, reduce the volume of the application installation file, but also improve the running efficiency of the application. In addition, when it is detected that Unity has finished compiling the first setting language code into the first intermediate code using the first compiler, a hook function for the function whose performance needs to be monitored is injected into the first intermediate code, thereby avoiding interference with the code compilation process.

[0044] In a possible implementation, the injection rules include: determining that the type function is an abstract class function or an interface function, and not injecting the hook function; or, determining that the method class function is a constructor, an abstract method function, a virtual function, a get function, a set function, and not injecting the hook function; or, determining that the function contains a customized tag, and not injecting the hook function.

[0045] The injection rules can be used to filter out functions that meet certain conditions from all functions of the first intermediate code file as functions that need to monitor performance, or filter out functions that do not meet certain conditions from all functions, so that the remaining functions are functions that need to monitor performance. Functions that need to monitor performance are functions that need to monitor performance. In an embodiment of the present invention, the injection rules are used to filter out functions that do not need to be injected with hook functions from all functions. The first intermediate code file is a DLL file. After obtaining the DLL file, it can be parsed to obtain the types of all classes and all functions and variables in the class. The corresponding injection rules can refer to the following:

[0046] The injection rules include: determining whether the type judgment function is an abstract class function or an interface function, and not injecting the hook function. This can be achieved by judging whether the type of the class is an abstract class or an interface.

[0047] Alternatively, the injection rules include: determining whether the method class function is a constructor, an abstract method function, a virtual function, a get function, or a set function, and not injecting a hook function. This can be achieved by determining whether the current method is any one of: a constructor, an abstract method function, a virtual function, a get function, or a set function.

[0048] Alternatively, the injection rule includes: determining a function with a custom tag and not injecting the hook function. Determining whether the method is marked with a tag, for example, marked with the "[HideAnalysis]" tag.

[0049] In a possible implementation, injecting a hook function into the function that needs to monitor performance includes: traversing the IL instructions of the function that needs to monitor performance, determining the start position and the end position of the function body of the function that needs to monitor performance, wherein the hook function includes a begin function and an end function; based on the start position, injecting the begin function; based on the end position, injecting the end function.

[0050] In an embodiment of the present invention, the begin function can be used to intercept the system data of the function that needs to monitor performance at the first moment, for example, intercepting timestamp data and memory data. The end function can be used to intercept the system data of the function that needs to monitor performance at the second moment, for example, intercepting timestamp data and memory data, wherein the first moment is earlier than the second moment. The IL instructions of the function that needs to monitor performance are traversed to determine the start position and the end position of the function body of the function that needs to monitor performance. Based on the start position, the begin function is injected, thereby using the begin function to intercept the system data of the running time of the start position. Based on the end position, the end function is injected, thereby using the end function to intercept the system data of the running time of the end position.

[0051] In this step, by injecting a hook function into the function that needs to monitor performance, the injection cost is saved compared to the solution of building development. In addition, based on the start position, the begin function is injected; based on the end position, the end function is injected, so that the function that needs to monitor performance can be flexibly customized, which is convenient for controlling the process of collecting production environment data.

[0052] In one possible implementation, the instructions of the function that needs to monitor performance are traversed to determine the start position and the end position of the function body of the function that needs to monitor performance, including: traversing the IL instructions of the function that needs to monitor performance, determining the position before the first line of IL instructions corresponding to the first function that needs to monitor performance in the IL instructions as the start position, and determining the position before the end statement position in the IL instructions as the end position.

[0053] In the embodiment of the present invention, refer to Fig.12 The schematic diagram of the IL instruction shown in FIG. 1 may include multiple functions that need to monitor performance, wherein the first line of IL instructions corresponding to the first function that needs to monitor performance is “IL_000a”, and the function in the red rectangular box above is the inserted begin function. Fig.12 In the IL instruction shown, the end statement is "ret", and the position before the end statement position in the IL instruction is determined to be the end position, and the function in the red rectangular box below is the inserted end function.

[0054] In this possible implementation, since the hook function is only executed before and after the function call that needs to monitor performance is called, by monitoring the function that needs to detect performance through the hook function, the running performance data of the function that needs to monitor performance can be collected with the least impact on the application performance.

[0055] In a possible implementation, the method further includes: when the application is running, receiving an execution instruction sent by the server, and determining that the bool variable in the hook function is true, executing the hook function to obtain the running performance data; the method further includes: sending the running performance data to the server.

[0056] In an embodiment of the present invention, the hook function includes a bool variable, and the value of the bool variable can be true or false. When the bool variable is true, it indicates that it is necessary to enable monitoring for the function whose performance needs to be monitored. When the bool variable is false, it indicates that it is not necessary to enable monitoring for the function whose performance needs to be monitored. When the application is running, an execution instruction sent by the server is received, and the execution instruction can be used to determine whether the value of the bool variable is true or false. On the server side, the execution instruction is sent for the configuration of whether the current device has enabled monitoring, so that developers can obtain corresponding data even in user usage scenarios, reducing the cost of test reproduction.

[0057] The following describes the implementation of the code file processing method and the function execution performance monitoring method in conjunction with a data processing flow chart of the function execution performance monitoring method using a specific embodiment.

[0058] For the hook method in Unity under the new IL2CPP (Intermediate Language to C++ Converter) compilation mode, see Figure 3 The flowchart of the processing method of the existing code file is shown. In the existing compilation stage, C# is converted to DLL and then directly converted to C++ in the IL2CPP stage. Figure 4 The data processing flow chart of the processing method of the code file shown in the figure, the embodiment of the present invention analyzes the compilation and packaging process, registers a callback in the IL compilation completion stage, and the callback implements the Begin and End injection of each or specific function for the corresponding DLL. Specifically, it can be completed in the IPostBuildPlayerScriptDLLs stage of Unity. At this time, Unity will compile the DLL product, and inject the Begin function and the End function at the beginning and end of the business DLL function through Mono.Cecil. After the injection is completed, Unity will enter the next stage, that is, the process of converting DLL to CPP. It mainly monitors whether C# has completed the DLL conversion based on the IPostBuildPlayerScriptDLLs callback provided by Unity, which does not interfere with the entire compilation process and achieves decoupling. Finally, the corresponding APK (Android Package Kit) product is compiled. It should be noted that the embodiment of the present invention is also applicable to the Linux platform. See Figure 6 The diagram shows the data usage of the function execution performance monitoring method. The APK can flexibly monitor and collect user performance indicators through compilation configuration and remote dynamic configuration switch, and the monitored data can be output to the log to facilitate developers to debug locally through the LOG visualization tool. In the remote user stage, the server can complete data reporting for big data analysis.

[0059] See also Figure 5 The data processing flow chart of the function execution performance monitoring method is shown in the figure, and the specific steps of the embodiment of the present invention are described below.

[0060] See also Fig.10 The Mono Cecil tool import interface shown in the figure first uses Unity's PackageManager to import the Mono Cecil tool into the corresponding project, then creates a custom HookEditor.cs file (as an injection program, which includes the Mono Cecil tool) in the special directory Assets / Editor, and writes the corresponding injection rules in it. The function logic is to obtain the path of the corresponding script compilation product dll file, through

[0061] AssemblyDefinition.ReadAssembly(path,readerParameters) completes the reading of the dll library. If it is in editor mode (in the development stage, when modifying the script, put it in this directory for easy visual debugging), the path of the dll is "Library\\ScriptAssemblies\\Assembly-CSharp.dll". See Fig.11 The DLL file acquisition path diagram shown in the figure shows that in the mode of compiling executable files (in the packaging stage, it has been encapsulated in the device and the file is being executed), the dll path is "Temp\\StagingArea\\Data\\Managed\\Assembly-CSharp.dll". There are different assembly dlls in the ScriptAssemblies and Managed directories, which can be selected according to user needs.

[0062] After getting the dll, you can parse and get the types of all classes and all functions and variables in the class. The corresponding injection rules are as follows:

[0063] (1) Filtering places where injection is not allowed: Determine whether the type of the class is an abstract class or interface, determine whether the current method is (constructor, abstract method, virtual function, get, set method), and determine whether the method is labeled [HideAnalysis]

[0064] mark;

[0065] (2) Inject HookUtil.Begin("{methodName}") before the beginning of the function body; inject HookUtil.End("{methodName}") before the end of the function and ret, that is, return and the end; traverse all IL instructions of the current method, insert Begin before the first method implementation position of the method body according to the array index 0, and determine whether the current IL instruction is ret. If so, insert the End instruction before it. For fault tolerance, other statements representing the end, such as OpCodes.Leave, OpCodes.Leave_S, OpCodes.Endfinally, OpCodes.Throw, OpCodes.Rethrow, will also be judged. That is, insert it before the return statement in try-catch and if-else to ensure that complex return logic is also injected into the End statement. It is understandable that a function program may contain multiple end positions. The above-mentioned ret, Leave, Endfinally, Throw, Rethrow, etc. are all statements representing the end. If the current IL instruction is a statement such as ret or Leave representing the end, then the End instruction is inserted before each statement such as ret or Leave. This improves the accuracy of performance monitoring.

[0066] Each line of HookUtil.Begin("{methodName}") or HookUtil.End("{methodName}") function to be inserted needs to be arranged according to the rules of the Mono Cecil tool. Each HookUtil call function requires two lines of code rules to define and insert two IL instructions, one representing the OpCodes.Ldstr string of {class name.function name}, and the other OpCodes.Call reference method. When inserted, the corresponding class will be automatically referenced in the file header using, so there is no need to worry about reference errors. For example:

[0067] The artificial C# pseudo code is as follows:

[0068] private void OnDestroy()

[0069] {

[0070] HookUtil.Begin("{methodName}");

[0071] / / Business code implementation

[0072] HookUtil.End("{methodName}");

[0073] }

[0074] Through the IL decompilation tool, we can see the IL instructions as follows Fig.12 shown.

[0075] HookUtil.Begin("{methodName}") and HookUtil.End("{methodName}") both capture the current timestamp and memory usage. After Begin captures, End will capture and subtract the timestamp and memory of Bigin to get the time and memory usage of this function. After calculation, End will add 1 to the number of times the function is used, and save the data used this time in the dictionary for uploading to the server statistics.

[0076] To obtain the callback for dll compilation completion in editor mode and compile mode, you need to create a custom PreIL2CPP.cs in the special directory Assets / Editor and use the following solutions: (1) In editor mode: write a custom static function in this class, and call the hook method in HookEditor.cs to pass in the dll path in editor mode. Add the [UnityEditor.Callbacks.DidReloadScripts(0)] tag before this method. Every time the business script is modified, the Unity editor will automatically compile a new dll, and after the compilation is complete, it will call the function with this tag. (2) In compile mode: this class needs to inherit the IPostBuildPlayerScriptDLLs interface and implement the OnPostBuildPlayerScriptDLLs function. In this function, call the hook method in HookEditor.cs to pass in the dll path in compile mode. After Unity completes converting C# to a DLL library, it will call this function before starting the IL2CPP compiler to convert C++.

[0077] OnPostBuildPlayerScriptDLLs method, at this time you can start injecting HookUtil.Begin("{methodName}") and HookUtil.End("{methodName}") functions.

[0078] After the injection is completed, if the executable file is deployed to a machine with a network connection, the time consumption, memory usage, and number of calls of these functions will be automatically collected after running and reported to the server in batches.

[0079] In the embodiment of the present invention, the package body increases by only 1KB and the running time reaches the nanosecond level which is negligible. It can also be injected according to the compilation DLL, supports IL2CPP and Mono compilation processes, and supports any software stage locally and remotely in the cloud.

[0080] The present invention provides a method for processing code files, which can participate in function performance monitoring at any software stage, greatly improve the source range of data acquisition and develop faster positioning, reduce at least 20MB of package size and negligible performance loss in nanoseconds.

[0081] The embodiment of the invention also provides a method for monitoring function execution performance. Figure 7 is a flow chart of a method for monitoring function execution performance according to an embodiment of the present application. Figure 7 The method steps involved are described.

[0082] Step S701, obtaining an application installation file of an application. The application installation file is constructed based on a second intermediate code file; the second intermediate code file is an application installation file. When the device detects that Unity uses a first compiler to compile a first set language code into a first intermediate code file, it obtains and parses the first intermediate code file to obtain all functions in the first intermediate code file; based on all the functions and preset injection rules, the function that needs to monitor performance is determined from all the functions and a hook function for the function that needs to monitor performance is injected into the first intermediate code file.

[0083] Step S702: installing the application based on the application installation file.

[0084] Step S703: during the running of the application program, calling the hook function to monitor the execution performance.

[0085] In the embodiment of the present invention, the first set language code is a source code generated by using a programming language, wherein the programming language may be a .NET (a development framework) language supported by Unity, such as C#, Boo, JavaScript (UnityScript), etc.

[0086] Unity uses the first compiler to compile the source code into IL (Intermediate Language) code, wherein the first compiler may include but is not limited to: JIT (Just-In-Time) compiler, C# compiler (csc.exe / Roslyn), VB.NET compiler (vbc.exe), F# compiler (fsc.exe), etc., that is, Unity uses the first compiler to compile the first set language code to obtain the first intermediate code file. Among them, the type of the first compiler can be selected according to actual needs. The first code file is a DLL (Dynamic Link Library) file.

[0087] In an embodiment of the present invention, the process of compiling the first intermediate code file in Unity is monitored. When it is detected that Unity has finished compiling the first set language code into the first intermediate code using the first compiler, a hook function for the function whose performance needs to be monitored is injected into the first intermediate code, thereby avoiding interference with the code compilation process.

[0088] It should be noted that DLL is a file format for storing IL code in .NET assemblies, which allows IL code to be encapsulated and reused.

[0089] In the embodiment of the present invention, when it is detected that the generation of the first intermediate code file is completed, the IL code in the DLL can be viewed through a tool such as ILDASM (IL disassembler), that is, the first intermediate code file is obtained.

[0090] In the IL code, each method usually begins with metadata describing the basic information of the method, such as method name, return type, parameter list, access modifier (public, private, etc.) and method attributes (such as .static, .virtual). With this information, the signature of each function can be determined, that is, the first intermediate code file is parsed to obtain all functions in the first intermediate code file.

[0091] In an embodiment of the present invention, the injection rules can be used to filter out qualified functions from all functions, and the specific filtering conditions can be set according to actual needs, so as to determine the functions whose performance needs to be monitored from all the functions based on these filtering conditions, that is, based on all the functions and preset injection rules, determine the functions whose performance needs to be monitored from all the functions.

[0092] The IL code can be modified through tools or custom programs, that is, a hook function can be injected into the function that needs to monitor performance. The hook function can be injected into each function that needs to monitor performance, or it can be injected into a specific function that needs to monitor performance, so as to achieve precise control of the function that needs to monitor performance. By precisely controlling the function that needs to monitor performance, compared with comprehensive monitoring of all functions, it can not only reduce the amount of code in the application installation file and the size of the application installation file, but also improve the running efficiency of the application. Among them, the hook function is used to generate the running performance data of the function that needs to monitor performance during the process of the application executing the function that needs to monitor performance. Among them, the running performance data may include but is not limited to: running time, memory usage and other data.

[0093] In the embodiment of the present invention, the function whose performance needs to be monitored after the hook function is injected is the modified IL code. IL Hook modifies the intermediate representation of the program, and these modifications can generate machine language codes of various platforms when the program is executed, depending on the target hardware architecture of the program.

[0094] The modified IL code can be recompiled using ILASM (IL Assembler) or more advanced libraries such as Mono.Cecil, dnlib, etc. to obtain a second intermediate code file.

[0095] The type of the second intermediate code file depends on the operating system and processor architecture on which the application is running. For example, the second intermediate code file may be a C++ code file.

[0096] In an embodiment of the present invention, based on the application installation file, after the application is installed, during the operation of the application, the hook function in the second intermediate code file can be called to monitor the execution performance of the function whose performance needs to be monitored.

[0097] See also Figure 8 The diagram shows the content of the display interface of the function execution performance monitoring method, which shows the visualization tool in the Unity client. Combined with this method, local shielding can be achieved and performance monitoring of functions that need to be monitored can be enabled. The corresponding data can then be viewed and captured in the log, which is convenient for developers to filter and locate.

[0098] The embodiment of the present invention includes obtaining an application installation file of an application; the application installation file is constructed based on a second intermediate code file; the second intermediate code file is obtained by injecting a hook function for a function that needs to monitor performance into the first intermediate code file when the application installation file device detects that Unity uses a first compiler to compile a first setting language code into a first intermediate code file; installing the application based on the application installation file; and calling the hook function to monitor the execution performance during the operation of the application. The embodiment of the present invention generates the operation performance data of the function that needs to monitor performance by injecting a hook function in a targeted manner, and can accurately control the function that needs to monitor performance. By accurately controlling the function that needs to monitor performance, compared with comprehensively monitoring all functions, it can not only reduce the amount of code in the application installation file, reduce the volume of the application installation file, but also improve the operation efficiency of the application. In addition, when it is detected that Unity uses a first compiler to compile the first setting language code into a first intermediate code, the hook function for the function that needs to monitor performance is injected into the first intermediate code, avoiding interference with the code compilation process.

[0099] In a possible implementation, the hook function includes a begin function and an end function, and the begin function and the end function are respectively injected into the beginning and end positions of the function body of the function whose performance needs to be monitored; during the running of the application, the begin function is called to obtain a first timestamp and a first heap memory when the application starts running; and, the end function is called to obtain a second timestamp and a second heap memory when the application ends running; based on the first timestamp, the first heap memory, the second timestamp and the second heap memory, the execution performance of the function whose performance needs to be monitored is obtained.

[0100] In this possible implementation, the begin function can be used to intercept the system data of the function whose performance needs to be monitored at the first moment, for example, intercepting timestamp data and memory data. The end function can be used to intercept the system data of the function whose performance needs to be monitored at the second moment, for example, intercepting timestamp data and memory data, wherein the first moment is earlier than the second moment. The IL instructions of the function whose performance needs to be monitored are traversed to determine the start position and the end position of the function body of the function whose performance needs to be monitored. Based on the start position, the begin function is injected, thereby using the begin function to intercept the system data at the runtime of the start position. Based on the end position, the end function is injected, thereby using the end function to intercept the system data at the runtime of the end position.

[0101] During the running of the application, the begin function is called to obtain the first timestamp and the first heap memory when the application starts running; and the end function is called to obtain the second timestamp and the second heap memory when the application ends running; based on the first timestamp, the first heap memory, the second timestamp and the second heap memory, the execution performance of the function that needs to be monitored is obtained. For example, the difference between the second timestamp and the first timestamp can be used as the running time of the function, and the difference between the second heap memory and the second heap memory can be used as the memory usage data of the function.

[0102] It should be noted that heap memory is a major area in computer science used to dynamically allocate memory when a program is running. It is relative to stack memory. Both are key memory types used by programs during execution, but their management methods, uses, and life cycles are different.

[0103] In a possible implementation, the method further includes: recording execution performance data during the performance monitoring process of the hook function; generating a corresponding record log based on the execution performance data and sending the record log to a server.

[0104] In this possible implementation, the log can be used for visualization processing, so as to finally display the execution performance data in a visual manner. The log is sent to a server, so as to perform big data analysis on the execution performance data.

[0105] An embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and the computer program implements the above method when executed by a processor.

[0106] An embodiment of the present invention further provides an electronic device, comprising: a memory, a processor, and a computer program stored in the memory, wherein the processor implements the above method when executing the computer program.

[0107] refer to Fig. 9, a block diagram of an electronic device that can be used as a server or client of an embodiment of the invention will now be described, which is an example of hardware devices that can be applied to various aspects of the invention. The electronic device is intended to represent various forms of digital electronic computer devices, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the invention described and / or required herein.

[0108] like Fig. 9 As shown, the electronic device includes a computing unit 901, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 902 or a computer program loaded from a storage unit 908 into a random access memory (RAM) 903. In the RAM 903, various programs and data required for the operation of the electronic device can also be stored. The computing unit 901, the ROM 902, and the RAM 903 are connected to each other via a bus 904. An input / output (I / O) interface 905 is also connected to the bus 904.

[0109] Multiple components in the electronic device are connected to the I / O interface 905, including: an input unit 906, an output unit 907, a storage unit 908, and a communication unit 909. The input unit 906 can be any type of device that can input information to the electronic device, and the input unit 906 can receive input digital or character information, and generate key signal input related to user settings and / or function control of the electronic device. The output unit 907 can be any type of device that can present information, and can include but is not limited to a display, a speaker, a video / audio output terminal, a vibrator, and / or a printer. The storage unit 908 can include but is not limited to a disk, an optical disk. The communication unit 909 allows the electronic device to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks, and can include but is not limited to a modem, a network card, an infrared communication device, and / or a wireless communication transceiver, such as a Bluetooth device, a WiFi device, a WiMax device, a cellular communication device, and / or the like.

[0110] The computing unit 901 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the computing unit 901 include, but are not limited to, a CPU, a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing units, various computing units running machine learning model algorithms, digital signal processors (DSPs), and any appropriate processors, controllers, microcontrollers, etc. The computing unit 901 performs the various methods and processes described above. For example, in some embodiments, the method embodiments created by the present invention may be implemented as a computer program, which is tangibly contained in a machine-readable medium, such as a storage unit 908. In some embodiments, part or all of the computer program may be loaded and / or installed on an electronic device via ROM 902 and / or a communication unit 909. In some embodiments, the computing unit 901 may be configured to perform the above method in any other appropriate manner (e.g., by means of firmware).

[0111] The computer program for implementing the method of the present invention can be written in any combination of one or more programming languages. These computer programs can be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that the computer program, when executed by the processor or controller, enables the functions / operations specified in the flow chart and / or block diagram to be implemented. The computer program can be executed entirely on the machine, partially on the machine, partially on the machine as a stand-alone software package and partially on a remote machine, or entirely on a remote machine or server.

[0112] In the context of the present invention, the machine-readable medium can be a tangible medium that can contain or store a program for use by an instruction execution system, device or equipment or used in combination with an instruction execution system, device or equipment. The machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. The machine-readable signal medium can include, but is not limited to, electronic, magnetic, optical, electromagnetic or infrared systems, devices or equipment, or any suitable combination of the above. A more specific example of a machine-readable storage medium can include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.

[0113] It should be noted that the term "including" and its variations used in the embodiments of the present invention are open inclusions, that is, "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one other embodiment"; the term "some embodiments" means "at least some embodiments". The modifications of "one" and "multiple" mentioned in the embodiments of the present invention are illustrative and not restrictive. Those skilled in the art should understand that unless otherwise clearly indicated in the context, it should be understood as "one or more".

[0114] 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 the embodiments created by the present invention 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 relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.

[0115] The various steps described in the method implementation methods provided by the embodiments of the present invention can be performed in different orders and / or in parallel. In addition, the method implementation methods may include additional steps and / or omit the steps shown. The scope of protection of the present invention is not limited in this respect.

[0116] The term "embodiment" in this specification refers to specific features, structures or characteristics described in conjunction with the embodiment that can be included in at least one embodiment of the invention. The appearance of this phrase in various places in the specification does not necessarily mean the same embodiment, nor does it mean that it is mutually exclusive with other embodiments and is independent or optional. The various embodiments in this specification are described in a related manner, and the same and similar parts between the various embodiments refer to each other. In particular, for the device, equipment, and system embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts refer to the partial description of the method embodiment.

[0117] The above-described embodiments only express several implementation methods of the present invention, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of protection. It should be pointed out that, for a person of ordinary skill in the art, several modifications and improvements can be made without departing from the concept of the present invention, and these all belong to the scope of protection of the present invention. Therefore, the scope of protection of the present invention shall be subject to the attached claims.

Claims

1. A method for processing a code file, comprising: Monitor Unity using a first compiler to compile a first setting language code into a first intermediate code file; When it is detected that the generation of the first intermediate code file is completed, acquiring and parsing the first intermediate code file to obtain all functions in the first intermediate code file; Based on all the functions and the preset injection rules, determine the function that needs to monitor performance from all the functions and inject a hook function into the function that needs to monitor performance, wherein the hook function is used to generate the running performance data of the function that needs to monitor performance during the process of the application executing the function that needs to monitor performance; Generate a second intermediate code file based on the function whose performance needs to be monitored after the hook function is injected; Based on the second intermediate code file, an application installation file of the application is constructed.

2. The method according to claim 1, wherein: The injection rules include: Make sure the judgment type function is an abstract class function or an interface function, and do not inject the hook function; or, Determine whether the method class function is a constructor, abstract method function, virtual function, get function, or set function, and do not inject a hook function; or, Make sure that the function containing the customized tag does not inject the hook function.

3. The method according to claim 1, injecting a hook function into the function whose performance needs to be monitored, comprises: Traversing the instructions of the function whose performance needs to be monitored, determining the starting position and the ending position of the function body of the function whose performance needs to be monitored, wherein the hook function includes a begin function and an end function; Based on the start position, inject the begin function; Based on the end position, the end function is injected.

4. The method according to claim 3, traversing the instructions of the function whose performance needs to be monitored, and determining the starting position and the ending position of the function body of the function whose performance needs to be monitored, comprises: Traverse the IL instructions of the functions that need to monitor performance, determine the position before the first line of IL instructions corresponding to the first function that needs to monitor performance in the IL instructions as the starting position, and determine the position before the end statement position in the IL instructions as the end position.

5. The method according to claim 1, further comprising: When the application is running, an execution instruction sent by the server is received, and when it is determined that the bool variable in the hook function is true, the hook function is executed to obtain the running performance data; The method further includes: sending the operating performance data to the server.

6. A method for monitoring function execution performance, comprising: Get the application installation file for the application; The application installation file is constructed based on the second intermediate code file; The second intermediate code file is an application installation file. When the device detects that Unity has finished compiling the first setting language code into the first intermediate code file using the first compiler, the device acquires and parses the first intermediate code file to obtain all functions in the first intermediate code file; Based on all the functions and the preset injection rules, determine the function whose performance needs to be monitored from all the functions and inject the hook function for the function whose performance needs to be monitored into the first intermediate code file; Installing the application based on the application installation file; During the running of the application program, the hook function is called to monitor the execution performance.

7. The method according to claim 6, wherein the hook function comprises a begin function and an end function, and the begin function and the end function are respectively injected into the beginning position and the end position of the function body of the function that needs to monitor performance; During the running of the application, calling the begin function to obtain a first timestamp and a first heap of memory when the application starts running; as well as, Call the end function to obtain a second timestamp and a second heap of memory when the application ends running; Based on the first timestamp, the first heap memory, the second timestamp, and the second heap memory, the execution performance of the function whose performance needs to be monitored is obtained.

8. The method according to claim 6, further comprising: During the performance monitoring process of the hook function, recording execution performance data; Based on the execution performance data, a corresponding record log is generated and the record log is sent to a server.

9. An electronic device comprising a memory, a processor and a computer program stored in the memory, wherein the processor implements the method according to any one of claims 1 to 8 when executing the computer program.

10. A computer-readable storage medium, wherein a computer program is stored in the computer-readable storage medium, and when the computer program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.

Citation Information

Patent Citations

  • Test analysis system and method based on embedded software

    CN102222041A

  • Code performance test method and device, computer equipment and storage medium

    CN110716854A

  • Layered and segmented monitoring and intervention method for large-scale parallel test tasks

    CN113326209A