A multi-platform supported online binary optimization method, optimization system and application
By using a multi-platform online binary optimization method and system, the problems of performance impact, source code dependency, and instruction set compatibility of program optimization are solved, achieving efficient optimization without source code and cross-platform performance improvement.
Patent Information
- Application Number
- CN202311052652.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-21
- Publication Date
- 2025-12-05
- Estimated Expiration
- 2043-08-21
AI Technical Summary
Existing technologies have shortcomings such as impacting program performance during optimization, requiring source code, having low optimization matching, being unable to perform online optimization, and not supporting emerging instruction set architectures.
This paper provides a multi-platform online binary optimization method, which includes program dynamic behavior information collection, disassembly, optimization and online code replacement. It supports a multi-platform binary optimization system, uses hardware performance event sampling or dynamic binary instrumentation to obtain behavior information, performs optimizations such as function inlining, code layout, data layout and instruction folding, and realizes code replacement through process control.
It achieves precise optimization of binary executables without source code, improves performance, supports multi-platform deployment, avoids redeployment issues, and achieves performance improvements of more than 30% in some scenarios.
Smart Images

Figure CN119493573B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of compiler optimization technology, and relates to an online binary optimization method, optimization system and application guided by dynamic program execution information on multiple platforms. Background Technology
[0002] Dynamic runtime information-guided optimization (PGO) is an important compiler optimization technique. Depending on the stage at which the dynamic behavior information is applied, PGO can be divided into Profile-Guided Optimization (PGO) and Post-Link Optimization (PLO). PGO collects dynamic behavior information during program execution through program instrumentation, dynamic binary instrumentation, or performance event-based sampling techniques. During the recompilation of the source code to generate the executable file, this method utilizes the previously collected dynamic behavior information to perform more targeted compiler optimizations. However, PGO also has the following drawbacks:
[0003] 1.) After enabling PGO mode, the program's dynamic behavior information needs to be collected during the first run, which may affect the program's performance during the first run.
[0004] 2.) PGO requires recompiling the application under the guidance of dynamic behavior information, therefore PGO optimization can only be performed if the source code is available;
[0005] 3.) PGO optimizes the source code or intermediate representation of a program by using behavioral information during program execution. The dynamic behavior information of the program actually obtained by PGO and the code that PGO actually optimizes may not be a perfect match, thus affecting the final optimization effect.
[0006] Post-linker optimization (PGO) typically takes a binary executable as input and outputs a better-performing binary executable. The PGO collects dynamic behavior information specific to the binary executable, and since it operates on the binary executable, this high degree of matching between the runtime information and the optimized code allows PGO to perform more precise optimizations compared to PGO, resulting in more significant performance improvements for some large applications.
[0007] Post-link binary optimizers typically perform offline optimization. The complete optimization process using a post-link optimizer includes steps such as starting the application load, collecting dynamic behavior information, terminating the application load, generating the optimized application load, and restarting the optimized application load. Currently, there are two widely used and active post-link binary optimizers in the open-source community: Meta's BOLT. [1] Another one is Google's Propeller. [2] Both optimizers perform offline application load optimization following the aforementioned process. The collection of dynamic program behavior information and the execution of the optimized binary executable are two completely independent processes during optimization. Using an offline linker-based binary optimizer to optimize workloads introduces two potential problems: 1) the optimized application needs to be redeployed; 2) the dynamic program behavior information needs to be updated in real time. These two problems reduce the convenience of using a linker-based binary optimizer during the optimization process.
[0008] In addition, the compatibility of the linker binary optimizer with processors of different instruction set architectures is also a consideration. In recent years, many domestic manufacturers have been promoting processor R&D. Besides the existing Intel64 and AMD64 instruction set architectures that hold the majority market share, processor development based on ARM, RISC-V, MIPS, and Alpha instruction set architectures is also progressing. For example, there are now Kunpeng and Phytium processors based on the ARM architecture, XuanTie processors based on RISC-V, Loongson processors based on the MIPS architecture, and Shenwei processors based on the Alpha architecture, among others. The research and development of the linker binary optimizer in academia and industry has mainly focused on instruction set architectures with high market share, such as Intel64 and AMD64, resulting in a lack of this basic software on some emerging instruction set architecture platforms. Summary of the Invention
[0009] To address the shortcomings of existing technologies, such as impacting program performance during optimization, requiring source code, poor optimization matching, inability to perform online optimization, and lack of support for emerging instruction set architectures, the present invention aims to provide a scalable multi-platform online binary optimization method and system. The main technical problems to be solved include the collection of dynamic program behavior information across multiple platforms, offline binary executable file optimization across multiple platforms, and online executable code replacement across multiple platforms.
[0010] This invention provides a binary optimization method that supports multiple platforms, the method comprising the following steps:
[0011] Step 1: Start the target application load in the process, collect and analyze the dynamic behavior information of the program in the process;
[0012] Step 2: Using the program dynamic behavior information and target application load obtained in Step 1 as input, disassemble the binary code corresponding to the application load, optimize it, and output the optimized application load after linking.
[0013] Step 3: Replace the original application workload code with the optimized application workload code to achieve online code replacement and thus optimize the application workload.
[0014] In step one, the program dynamic behavior information refers to the information related to the behavioral characteristics of binary files collected by the application of the present invention during execution. This information can be used to guide the performance optimization of binary files, including the execution frequency of functions, the calling relationship between functions, the execution frequency and hit status of program branch instructions, and the access patterns of program memory access instructions, etc.
[0015] In step two, the input application load binary file is disassembled, and then the program dynamic behavior information collected in step one is analyzed and organized. Based on the differences in input information (including program dynamic behavior information, target application load, etc.), optimizations are performed for different categories of binary files. Binary executable files are broadly classified into two categories: compute-intensive and memory-intensive. Compute-intensive binary executable files mainly perform complex scientific computing, data analysis, and image rendering tasks. These tasks primarily focus on instruction execution and data manipulation, requiring significant CPU resources. Optimizations for these binary executable files mainly include function inlining, indirect call elimination, and instruction folding. Memory-intensive binary executable files are mainly involved in large dataset processing, file processing, and database operations. These applications require maintaining a large amount of information in memory for timely access. Optimizations for these binary executable files mainly include code layout and data layout. Specific optimizations can include inlining optimization guided by function execution frequency and inter-function call relationships; code layout optimization guided by program branch instructions and hit conditions; data layout optimization guided by memory access patterns of program memory access instructions; instruction folding optimization guided by program execution flow information; and indirect call elimination optimization guided by program indirect branch target address information, etc. Finally, the optimized disassembled file is relinked into an executable binary file.
[0016] The program inlining optimization guided by the function execution frequency and function call relationship can determine which functions should be inlined by analyzing the function execution frequency and function call relationship, thereby optimizing the program's execution efficiency and performance.
[0017] The code layout optimization guided by program branch instructions and hit conditions refers to reorganizing the program code to reduce the number of branch instruction executions and the probability of branch prediction failure, thereby improving program performance. This optimization technique requires the compiler to perform static or dynamic analysis of the program's structure and execution, and to make an appropriate trade-off between code size and performance.
[0018] The data layout optimization guided by the memory access patterns of program memory access instructions refers to improving program performance by optimizing the layout of data in the program to make the program's memory access patterns more efficient. This optimization technique requires the compiler to perform static or dynamic analysis of the program's data access patterns and make an appropriate trade-off between code size and performance.
[0019] The instruction folding optimization guided by the program execution flow information can obtain the dependencies between data under the guidance of data flow information, replace register variables whose calculated values are already known with constants, and reduce the computational overhead and code size during operation.
[0020] The indirect call elimination optimization guided by the indirect branch target address information can improve program execution efficiency by analyzing the use of function pointers and function references in the code. If the specific function pointed to by the function pointer or reference can be determined, indirect calls will be replaced with direct calls.
[0021] In step three, during the execution of the target program, the present invention can replace the original binary file with an optimized binary file by means of process control, process call stack analysis, machine code extraction and injection, etc., thereby improving the performance of the target program without affecting the normal execution of the program.
[0022] Process control refers to the effective management of the creation, execution, suspension, resumption, and termination of a process. By managing the target process, control over the application is achieved. The main purpose of this invention is to suspend the target program, perform code replacement, and then resume program execution. Process call stack analysis involves extracting functions currently on the call stack during process suspension. Using these functions as entry points, the target address pointed to by function call instructions within the current call stack functions is changed. The new target address is changed from the original function to an optimized new function, thus achieving code replacement. Machine code extraction refers to extracting the optimized function machine code from the BOLT-optimized binary file. Machine code injection involves allocating a new, specified address space for this new optimized machine code and making the function call instructions within the current call stack functions point to the new address space, achieving code replacement.
[0023] To implement the above-mentioned binary optimization method, the present invention also provides a binary optimization system supporting multiple platforms. The binary optimization system includes: a program dynamic behavior information acquisition module, an offline binary optimization module, and an online code replacement module; the program dynamic behavior information acquisition module, the offline binary optimization module, and the online code replacement module are connected and controlled by a control program.
[0024] The program dynamic behavior information acquisition module is used to collect and parse program dynamic behavior information in the process;
[0025] The program dynamic behavior information acquisition module further includes a hardware detector, an information acquisition module, and an information integration module;
[0026] The hardware detector is used to detect whether the processor used to execute the target application load has hardware registers associated with the dynamic behavior information of the program to be collected.
[0027] The information acquisition module selects the execution path for information acquisition based on the presence or absence of hardware registers.
[0028] The information integration module is used to integrate and process the collected program dynamic behavior information into program-specific dynamic behavior information in the input format required by the offline binary optimization module.
[0029] When the processor used to execute the target application load has hardware registers associated with the dynamic behavior information of the program to be collected, a hardware-based performance event sampling scheme is used to obtain the specific dynamic behavior information of the program; when the processor used to execute the target application load does not have hardware registers associated with the dynamic behavior information of the program to be collected, a dynamic binary instrumentation scheme is used to obtain the dynamic behavior information of the program to be collected.
[0030] The offline binary optimization module is used to optimize the target application load;
[0031] The offline binary optimization module further includes a binary file disassembler, a code optimizer, and a binary file relinker;
[0032] The binary file disassembler is used to disassemble binary executable files corresponding to the collected program dynamic behavior information.
[0033] The code optimizer is used to optimize platform-independent code; the code optimization methods include code layout optimization, instruction folding optimization, indirect call elimination optimization, data layout optimization, inlining optimization, etc.
[0034] The binary file relinker is used to relink the optimized disassembled binary code into a new binary executable file, which can run normally and has better performance than the original binary executable file.
[0035] The online code replacement module is used to replace the source code with the optimized code of the target application load online.
[0036] The online code replacement module further includes a process controller, a machine code acquirer, and a code refactoring unit;
[0037] The process controller is used to pause the process containing the target workload and extract the current function call stack information of the process before code replacement, and to restore the target process and continue to execute the optimized application workload after code replacement is completed;
[0038] The machine code acquirer is used to compare and acquire the modified virtual function table and function machine code in the optimized binary executable file, and write them to a specified directory. It also recodes the machine code of the function call instruction inside the call stack function of the current process in the original binary executable file, so that the target function pointed to by the machine code is the optimized function, and writes the modified optimized machine code to the specified directory.
[0039] The code refactoring tool is used to allocate additional virtual address space in the target process, and then write the optimized machine code information recorded by the machine code acquirer into the newly allocated address space.
[0040] The beneficial effects of this invention include:
[0041] This invention proposes a multi-platform online binary optimization method and system guided by program dynamic behavior information. The invention comprises three modules: a program dynamic behavior information acquisition module, an offline binary optimization module, and an online code replacement module. The program dynamic behavior information acquisition module can obtain program dynamic behavior information using hardware-level performance event sampling or software-level dynamic binary instrumentation; the offline binary optimization module uses the program dynamic behavior information to optimize the original workload and generate a better-performing binary executable file; the online code replacement module executes the optimized binary executable file's code during program execution without requiring a workload restart.
[0042] This invention optimizes binary executables based on post-linking binary optimization (PGO) technology, solving the problem of mismatch between the collected dynamic behavior information and the final optimization target in traditional PGO techniques. This invention enables more precise and effective code optimization at the binary executable level, further improving the performance of the optimized binary executable. Furthermore, this invention proposes an online code replacement scheme that can replace the original application workload code with the optimized application workload code during application load execution, avoiding the need for redeployment after optimization for large workloads. The challenge of online code replacement lies in executing the unoptimized and optimized code sequentially within the same process while ensuring a smooth transition between the two. Failure to address this challenge can lead to program crashes after code replacement. This invention utilizes the ptrace system call to control the target process. Before code replacement, the target program is paused. During the replacement process, function information on the current process's call stack is first obtained, and then optimized code is inserted using these functions as entry points. After code replacement, program execution resumes. In addition, the program dynamic behavior information acquisition module, offline binary optimization module, and online code replacement module of this invention all support multiple platforms. Therefore, this invention can be deployed and optimized on processors with various instruction set architectures, further optimizing the target workload without source code. This invention uses disassembly to decompose the binary executable file, then optimizes it, and finally generates the optimized binary executable file through a relinker. The above process of regenerating the optimized executable file does not require source code support, thus achieving source code-free optimization. This invention internally supports hardware detectors, which can select the acquisition path for program dynamic execution information based on the presence of hardware registers. In the absence of hardware registers that record the program dynamic behavior information to be collected, a platform-independent dynamic binary instrumentation method can be used to collect program dynamic behavior information, thus realizing a binary optimization module that supports multiple platforms. The optimization method in this invention can achieve performance improvements of more than 30% in certain database and big data application scenarios.
[0043] For cloud vendors, to ensure efficient business operations and improve customer experience, many are optimizing the performance of cloud applications such as MySQL and Redis. This invention can help cloud vendors optimize the performance of cloud applications like MySQL and Redis on processors with various instruction set architectures. For processor manufacturers, this invention can fill the gap in basic software for processors with lower market share instruction set architectures, enriching the basic software ecosystem. Only a few developers specifically develop basic software to support lower market share instruction set architectures, and these processors lack performance optimization tools such as binary optimization modules. Therefore, under the same conditions, programs running on these processors will perform worse than those running on mature instruction set architectures, causing users to be unwilling to run their workloads on these lower market share processors. A multi-platform online binary optimization module can generate optimized application workloads with the same instruction set architecture as the input application workload on multiple platforms, and replace the code of the running application workload with the code of the optimized application workload during program execution. Therefore, a multi-platform binary optimization module also helps promote the use of processors with lower market share instruction set architectures. For users, this invention optimizes application load entirely during program execution, eliminating the need for application redeployment. Furthermore, program runtime information collection, offline binary optimization, and online code replacement are all automated, eliminating the need for users to collect dynamic program runtime information or redeploy the optimized binary executable. This enhances user convenience and reduces operational complexity. Attached Figure Description
[0044] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0045] Figure 1 This is a schematic diagram illustrating the implementation principle of the online binary optimization system of this invention.
[0046] Figure 2 This is a schematic diagram illustrating the implementation principle of the dynamic behavior information acquisition module of this invention.
[0047] Figure 3 This is a schematic diagram illustrating the implementation principle of the offline binary optimization module of this invention.
[0048] Figure 4This is a schematic diagram illustrating the implementation principle of the online code replacement module of this invention.
[0049] Figure 5 This is a schematic diagram of the implementation scheme of Embodiment 1 of the present invention.
[0050] Figure 6 This is a schematic diagram of the implementation scheme of Embodiment 2 of the present invention.
[0051] Figure 7 This is a schematic diagram of the implementation scheme of Embodiment 3 of the present invention.
[0052] Figure 8 This is a schematic diagram of the implementation scheme of Embodiment 4 of the present invention.
[0053] Figure 9 This is a schematic diagram of the implementation scheme of Embodiment 5 of the present invention.
[0054] Figure 10 This is a schematic diagram of the implementation scheme of Embodiment 6 of the present invention. Detailed Implementation
[0055] The present invention will be further described in detail below with reference to the specific embodiments and accompanying drawings. Except for the contents specifically mentioned below, the processes, conditions, and experimental methods for implementing the present invention are all common knowledge and general knowledge in the art, and the present invention does not have any particular limitations.
[0056] The post-link binary optimization involved in this invention is a compiler optimization method guided by program dynamic behavior information. Some program-specific dynamic behavior information is obtained using dedicated hardware registers on the processor responsible for performance event sampling. However, some instruction set architectures lack such dedicated hardware and therefore cannot perform related post-link binary code optimization based on the specific dynamic runtime information collected by this dedicated hardware. Some post-link optimizers use hardware-independent dynamic binary instrumentation methods to collect program dynamic behavior information; however, dynamic binary instrumentation incurs significant performance overhead during information collection, thus offsetting the optimization effect of the code generated by the binary optimization module. This invention aims to design an efficient tool for collecting program dynamic behavior information that supports processors with multiple instruction set architectures.
[0057] After obtaining the program's dynamic behavior information, this invention also needs to further develop an offline binary optimization module that supports multiple platforms for different instruction set architectures. The front end of this binary optimization module can receive binary executable files from different instruction set architectures and perform disassembly operations on these binary executable files. The middle part needs to perform platform-independent code optimization, and the back end needs to generate an optimized binary executable file with the same instruction set architecture as the input executable file through relinking.
[0058] To address the issue of needing to redeploy application load during the optimization process of offline post-linking optimizers, this invention also proposes an online binary code replacement scheme applicable to multiple platforms. This online binary code replacer acquires dynamic behavior information of the running program during runtime and updates the program's memory image based on the new executable file generated after post-linking optimization. Without affecting program correctness, it replaces the original, insufficiently optimized code with the optimized code, thereby enabling the updated program to achieve higher performance. Furthermore, the performance improvement is influenced by complex factors, including the operating system version, instruction set architecture, the type of optimized binary executable file, and the specific execution scenario of the binary file. In certain database and big data application scenarios, performance improvements exceeding 30% can be achieved.
[0059] This invention utilizes post-linking optimization to improve the subsequent execution performance of an application through code replacement during program runtime. Furthermore, it supports multiple instruction set architectures, enabling performance optimization for multi-platform application workloads. The invention divides the multi-platform online binary optimization system into three modules: a program dynamic behavior information acquisition module, an offline binary optimization module, and an online code replacement module. These three modules are initially independent, but a control program can connect them, using the output of one module as the input of another. Figure 1 The overall workflow of this invention is shown. After the target application load starts in a specified process, the program dynamic behavior information acquisition module begins to collect and parse program dynamic behavior information during a period of normal operation of the process, converting the program dynamic behavior information into program dynamic behavior information in the target input format. The offline binary optimization module takes the converted program dynamic behavior information and the target application load as input and performs offline optimization, disassembling the input binary file to facilitate optimization. Then, the program dynamic behavior information guides the offline binary optimization module to optimize the disassembled binary file. Finally, the optimized disassembled file is relinked into an optimized application load, and the optimized application load after linking is output. Then, the online code replacement module replaces the code of the target application load with the code of the optimized application load, completing the online code replacement work. The program can then continue to run and execute the code of the optimized application load.
[0060] The program dynamic behavior information refers to the behavior information collected during the dynamic execution of the program. This information can be used to guide the performance optimization of binary files. It includes the function call frequency, the call relationship between functions, the execution frequency and hit status of program branch instructions, and the memory access mode of program memory access instructions, etc. The parsing refers to the aggregation of the raw format information of the program dynamic behavior information collected by the program dynamic behavior information acquisition module and the conversion into the format information required by the offline binary optimization module to actually guide the optimization of binary files.
[0061] The linking refers to reassembling the optimized disassembled binary code into a new binary executable file that can run normally and has better performance than the original binary executable file.
[0062] During the online code replacement process, the ptrace system call is used to control the target process in order to complete the tasks of pausing the target process, inserting optimized machine code, and resuming the target process. The online binary code replacement module extracts the virtual function table and function machine code of the optimized binary file and then puts the machine code into the allocated memory address space. The online binary optimization module also needs to obtain the current call stack functions of the target process, replace the machine code of these function call instructions, and use them as the entry point for executing the optimized code.
[0063] The specific principles and detailed workflows of the three modules are described below.
[0064] 5.1 Program Dynamic Behavior Information Acquisition Module
[0065] To enable the acquisition of program dynamic behavior information across different platforms, the program dynamic behavior information acquisition module of this invention incorporates two acquisition schemes: software-level dynamic binary instrumentation and hardware-level performance event sampling. To reduce acquisition overhead, if some processors support hardware registers for acquiring specific program dynamic behavior information, this invention prioritizes acquiring and parsing the program dynamic behavior information at the hardware level. For processors whose hardware does not support recording specific program dynamic behavior information, this invention can utilize software-level dynamic binary instrumentation to obtain the program dynamic behavior information.
[0066] like Figure 2 As described above, after the application load starts, the program dynamic behavior information acquisition module of this invention needs to allow a warm-up period before collecting dynamic behavior information, ensuring the program performance is stable. Before collection, this invention first uses a hardware detector to check if there are hardware registers on the processor associated with specific program dynamic behavior information. If so, to reduce collection overhead, this invention employs... Figure 2 In execution path 2, a hardware-based performance event sampling scheme is preferentially used to obtain specific dynamic behavior information of the program; otherwise, the present invention adopts... Figure 2 Execution path 1 uses a dynamic binary instrumentation scheme to obtain specific dynamic behavior information of the program. Since the two acquisition schemes have different acquisition principles, the format of the obtained specific dynamic program information also differs. Therefore, this invention further processes the acquired information into program-specific dynamic behavior information in the format required by the offline binary optimization module, so that the acquired program-specific dynamic behavior information can be directly used as input to the offline binary optimization module.
[0067] The performance event sampling method refers to collecting dynamic behavior information of the program during program execution by using registers associated with performance events. This scheme samples the dynamic behavior information during program execution at specified time intervals and is implemented at the hardware level, thus incurring less performance overhead. The dynamic binary instrumentation method refers to collecting dynamic behavior information of the program by using a dynamic binary instrumentation system. This scheme collects all dynamic execution information of the program during its operation and is implemented at the software level, resulting in greater performance overhead, but it can be applied to platforms with various instruction set architectures.
[0068] During the format conversion process, this invention associates the collected raw information with the function names inside the binary file, uses symbolic names plus offsets to represent the address where the collected information is located, and aggregates the collected information into the format required by the offline binary optimization module.
[0069] 5.2 Offline Binary Optimization Module
[0070] Because this invention can collect dynamic program information using performance event-based sampling or dynamic binary instrumentation, the input to the offline binary optimization module can be binary files from different instruction set architecture platforms. This invention adds a disassembler front-end and a relinker back-end for multiple instruction set architectures, and the code optimization part between the front-end and back-end is platform-independent. Therefore, the offline binary optimization module of this invention has multi-platform support characteristics and can optimize binary executable programs from different instruction set architectures. The offline binary optimization module further includes a binary file disassembler, a code optimizer, and a binary file relinker, such as... Figure 3As shown. After obtaining program dynamic behavior information in a unified format, the front end of the offline binary optimization module first receives the binary executable file corresponding to the collected program dynamic behavior information, so that the offline binary optimization module can perform disassembly operations on the binary executable file. Then, the middle part of the offline binary optimization module combines the program dynamic behavior information to perform platform-independent code optimization. The code optimization implemented by the middle part can be code layout optimization, instruction folding optimization, data layout optimization, inlining optimization, indirect call elimination optimization, etc. These optimizations are mature compilation optimization techniques in the process of compiling source code into executable files. This invention changes the target of these optimizations from source code or intermediate representation to binary executable files, and uses program dynamic runtime information to guide these optimizations. Therefore, the scope of optimization after linking is more precise and the effect is more obvious. The code layout optimization refers to arranging frequently executed basic blocks / functions together, guided by the execution frequency and hit rate of branch instructions, to optimize the code layout and improve the locality of the program. The instruction folding optimization refers to obtaining the dependencies between data under the guidance of data flow information, replacing register variables whose calculated values are already known with constants, reducing computational overhead and code size. The data layout optimization refers to optimizing the layout of data in the program to make the program's memory access pattern more efficient, thereby improving program performance. The inlining optimization refers to optimization guided by information on function call frequency and call relationships, reducing the overhead of function calls and function returns, and improving the performance of the optimized program. Finally, the backend of the offline binary optimization module relinks the mid-stage optimized code blocks to generate an optimized binary executable file with the same instruction set architecture as the input executable file. The disassembler at the front end and the relinker at the back end of this binary optimizer are instruction set architecture related parts, while the mid-stage code optimizer is instruction set architecture independent. Figure 3 As shown, the offline binary optimizer takes program dynamic behavior information and the application load to be optimized as input, and outputs the optimized application load. This offline binary optimizer can take binary executable files with application loads from various instruction set architectures such as x86-64, ARM, and RISC-V as input, and outputs optimized binary executable files with application loads corresponding to the corresponding instruction set architecture after processing by the platform-independent code optimizer. Because the code optimizer in this module is platform-independent, the optimizations implemented by the offline binary optimization module are scalable, supporting various program runtime information-guided compilation optimizations, including code layout optimization, data layout optimization, instruction folding optimization, inlining optimization, indirect call elimination optimization, and so on. Users can also easily add custom optimization methods.
[0071] Inlining, a compiler optimization technique based on the relationship between function execution frequency and function call relationships, is primarily used to improve program execution efficiency. In programming languages, a function is a block of code that performs a specific task. Functions in a program can call each other, forming function call relationships. Function inlining involves copying the code of a called function and inserting it into the place where it is called, thereby avoiding the overhead of function calls and improving program performance. The key to this optimization technique is determining which functions should be inlined and which should remain as independent function calls based on their execution frequency and call relationships. Generally, functions with high execution frequency and relatively small execution time are preferentially inlined because this reduces the overhead of function calls and unnecessary jumps during execution. When performing this optimization, the compiler needs to perform static or dynamic analysis based on the code structure and execution to determine which function inlining can bring actual performance improvements, while also considering the side effects of code bloat. Because inlining increases code length, it may lead to more cache misses and instruction cache misses; therefore, a trade-off must be made between code size and performance.
[0072] The code layout optimization guided by program branch instructions and hit conditions is also a compiler optimization technique. Its goal is to improve program performance by rationally arranging the code layout in the program to minimize the number of branch instruction executions and branch prediction failures. In computer programs, branch instructions are instructions used to select different code paths to execute based on conditions. Branch instructions may cause prediction errors during processor execution because the processor predicts the direction of the branch when it encounters a branch instruction. If the prediction is incorrect, the processor will flush the pipeline and re-execute the instruction after the prediction error; this process is called "branch prediction failure." Code layout refers to the way program instructions are organized in memory. Optimizing code layout can attempt to arrange the order of instructions in memory to minimize the number of branch instruction executions and prediction failures, thereby improving program performance. Specifically, this optimization technique involves the following aspects: 1) Code alignment: Aligning code according to specific boundaries can align branch target addresses, thereby reducing the performance loss caused by misaligned branch target addresses. 2) Branch target location selection: Place the target locations of frequently executed branches within a relatively short distance to reduce the distance of branch jumps and lower the probability of branch prediction failure. 3) Code reordering: Reorder the code to group frequently executed code together, reducing unnecessary branches. 4) Loop unrolling: Copy the code within a loop multiple times to reduce the number of branch instructions within the loop. 5) Function inlining: Insert the code within a function directly into the place where the function is called, avoiding the branch instructions and overhead caused by function calls.
[0073] The data layout optimization guided by the memory access patterns of program memory access instructions is also a compiler optimization technique. Its goal is to improve program performance by rationally arranging data layout within the program to optimize memory access patterns. Memory access is a crucial operation in computer programs, but different memory access patterns can lead to varying efficiencies. For example, sequential memory access is generally more efficient than random, scattered memory access because modern computers typically use caches to cache contiguous blocks of memory data, reducing memory access latency. This optimization technique mainly involves the following aspects: 1) Locality of reference optimization: Based on the program's memory access patterns, frequently accessed data is placed on contiguous memory addresses to utilize the principle of locality of reference in caching. This reduces the number of main memory accesses, thereby speeding up program execution. 2) Data alignment: Aligning data according to specific boundaries makes data access more efficient, especially for SIMD (Single Instruction, Multiple Data) instruction sets and vectorized operations. Aligned data layouts can improve the performance of parallel computing. 3) Loop rearrangement: By rearranging the order of calculations within a loop, memory accesses become more sequential, thus reducing memory access latency. 4) Data structure optimization: For complex data structures, the layout of the data can be reorganized, making memory instructions for accessing that data more efficient. 5) Prefetch optimization: By predicting the program's memory access patterns, data is pre-loaded into the cache to reduce memory access latency.
[0074] Instruction folding optimization is a compiler optimization technique that aims to improve program efficiency and performance by merging adjacent instructions to reduce the number of instructions in a program. In a computer program, instructions are the basic operations executed by the computer processor. Each instruction represents a specific calculation or operation. Optimizing the number of instructions can effectively reduce program execution time because fewer instructions mean fewer instruction cache accesses, fewer pipeline pauses, and less execution overhead. Instruction folding optimization mainly involves the following aspects: 1) Instruction merging: Merging multiple adjacent instructions into a single, more complex instruction, thereby reducing the total number of instructions. This optimization typically occurs when the processor instruction set contains instructions that support compound operations. 2) Constant folding: Merging constant operations with computation instructions to avoid repeatedly loading constant data. 3) Copy propagation: Copying the results of operations with the same value in an instruction to other locations, thereby reducing redundant calculations. 4) Loop unrolling: Unrolling instructions within a loop multiple times to reduce the number of times loop control instructions are executed, thereby reducing loop overhead. Instruction folding optimization is a part of compiler optimization. During code generation, it rewrites and rearranges the generated instruction sequence to make the final machine code more compact and efficient. This optimization technique requires the compiler to perform static analysis of the program's instruction sequence and perform corresponding optimizations based on the characteristics of the instruction set to balance code size and performance.
[0075] Indirect call elimination optimization is a compiler optimization technique that aims to convert indirect function calls in a program into direct function calls, thereby improving program execution efficiency. In programming languages, function calls are implemented using function names. Direct function calls refer to calling a function directly using its name in the program; the compiler can determine the function to be called at compile time. Indirect function calls, however, are implemented using function pointers or function references. The compiler cannot determine the specific function to be called at compile time and needs to determine it at runtime based on the value of the pointer or reference. Indirect function calls involve additional runtime overhead and cannot be optimized through static analysis, which can lead to a decrease in program execution efficiency. "Indirect call elimination optimization" attempts to convert indirect function calls into direct function calls to avoid these performance losses. The optimizer analyzes the usage of function pointers and function references in the code. If it can determine the specific function that the function pointer or reference points to, it will replace the indirect call with a direct call. This optimization may require reverse engineering or runtime-specific information in some cases, because the target function of the function pointer or reference cannot be completely determined in all situations. By eliminating indirection, the compiler can directly encode function calls as instructions when appropriate, reducing function call overhead and the number of jumps, thereby improving program performance and execution speed. However, it's important to note that this optimization can also increase code size, as direct calls may occur multiple times, while indirect calls only require one representation. Therefore, compilers typically weigh code size against performance to choose the most suitable optimization strategy.
[0076] 5.3 Online Code Replacement Module
[0077] The online code replacement module is responsible for replacing the original application workload's code with the optimized application workload's code during program execution. The online code replacement module mainly includes a process controller, a machine code acquirer, and a code refactoring unit. The process controller is primarily responsible for pausing the process containing the target workload and extracting the process's current function call stack information before code replacement. The process controller is also responsible for resuming the target process and continuing the execution of the optimized application workload after code replacement. The machine code acquirer is responsible for two main tasks: firstly, comparing the newly generated binary executable file with the original binary executable file to obtain the modified virtual function tables and function machine code from the optimized binary executable file, and writing them to a user-defined directory; secondly, obtaining the machine code of function call instructions inside the current process's call stack from the original binary executable file, recoding this machine code so that it points to the optimized function, and then writing this machine code to the user-defined directory as well. The code refactoring tool is mainly used to allocate additional virtual address space in the target process, and then write the optimized machine code information recorded by the machine code acquirer into the allocated virtual address space. Since the present invention has changed the target function address pointed to by the function call instruction in the machine code acquirer, the executed machine code will be replaced with the optimized machine code after the process is restored, so the present invention can successfully complete the online code replacement work.
[0078] The execution flow of the online code replacement module is as follows: Figure 4 As shown, to replace the code of the running application workload with the optimized application workload's code, the online code replacement module first uses the process controller to pause the process containing the target application workload and obtain the function call stack information of the current process. Then, the machine code retrieval unit in the online code replacement module combines the application workload information before and after optimization with the process's function call stack information to obtain the optimized functions, virtual function tables, and the machine code information corresponding to the function call instructions. The code refactoring unit then writes the aforementioned machine code information into the virtual address space. Finally, after the process control module resumes the target process, the optimized code can be run.
[0079] The specific implementation of the present invention is described through the following embodiments, in which A, B, C, D, and E represent different types of processor models, wherein... Figure 5 and Figure 7 In this context, A and C specifically refer to processors equipped with hardware registers that record specific dynamic behavior information of a program; Figure 6 and Figure 8 In this context, B and D specifically refer to processors that lack hardware registers to record specific dynamic behavior information of a program. X, Y, Z1, Z2, Z3, W1, W2, W3, and H represent computing platforms based on the aforementioned processor types. Figure 7 and Figure 8 Representing a multi-machine deployment scenario, Z1, Z2, and Z3 (W1, W2, and W3) refer to computing platforms with the same processor architecture and the same hardware and software environment configuration. Furthermore, the Z1, Z2, and Z3 (W1, W2, and W3) computing platforms all perform the same type of computing or memory access tasks for the same workload. Figure 9 This represents an example of binary code optimization using code layout optimization methods. Figure 10 This represents an optimized example of MySQL application workload on an Intel64 platform using code layout optimization methods.
[0080] Example 1:
[0081] Single-machine sampling, optimization, and deployment: The target processor has hardware registers that collect specific dynamic behavior information of the program. A computing platform X with processor model A has a workload to be optimized, and the computing platform containing processor model A has hardware registers that record specific dynamic behavior information of the program. Example 1 aims to complete the collection of program dynamic behavior information, binary optimization based on the post-link optimizer, and online deployment of the optimized workload on a single computing platform.
[0082] The execution flow of Example 1 is as follows: Figure 5 As shown, the target application load is first started in a specified process. Then, the program dynamic behavior information acquisition module begins collecting and parsing program dynamic behavior information within a specified interval of that process. During the collection process, the hardware detector of the program dynamic behavior information acquisition module discovers hardware registers on computing platform X that record specific program dynamic behavior information. Therefore, hardware-level performance event sampling technology is used on computing platform X to collect specific program dynamic behavior information. Next, the offline binary optimization module takes the program dynamic behavior information and the target application load as input and outputs the optimized application load after linking. Finally, the online code replacement module replaces the code of the target application load with the code of the optimized application load, completing the online code replacement. The program can then continue running to execute the optimized application load code, achieving the goal of improving the performance of a specific application load on computing platform X.
[0083] Example 2:
[0084] Single-machine sampling, optimization, and deployment are performed on the target processor, which lacks hardware registers for collecting program-specific dynamic behavior information. The computing platform Y, with processor model B, has a workload to be optimized, and this platform does not have hardware registers for recording program-specific dynamic behavior information. Example 2 aims to complete the collection of program dynamic behavior information, binary optimization based on the post-link optimizer, and online deployment of the optimized workload on a single computing platform.
[0085] The execution flow of Example 2 is as follows: Figure 6 As shown, the target application load is first started in a specified process. Then, the program dynamic behavior information acquisition module begins collecting and parsing program dynamic behavior information within a specified interval of that process. During the collection process, the hardware detector of the program dynamic behavior information acquisition module discovers that the computing platform Y does not have hardware registers that record program-specific dynamic behavior information. Therefore, software-level dynamic binary instrumentation technology is used on computing platform Y to collect program-specific dynamic behavior information. Afterwards, the offline binary optimization module takes the program dynamic behavior information and the target application load as input and outputs the optimized application load after linking. Finally, the online code replacement module replaces the code of the target application load with the code of the optimized application load, completing the online code replacement. The program can then continue running to execute the optimized application load code, achieving the goal of improving the performance of a specific application load on computing platform Y.
[0086] Example 3:
[0087] Single-machine sampling and optimization, multi-machine deployment, and hardware registers on the target processor for collecting program-specific dynamic behavior information. Computing platforms Z1, Z2, and Z3, all running processor C, contain workloads to be optimized. These platforms are used to execute specific types of computation / memory access tasks with the same application workload, and the computing platform containing processor C has hardware registers for recording program-specific dynamic behavior information. Example 3 aims to collect program dynamic behavior information on a single computing platform and optimize the target program through post-linking optimization, then deploy the optimized workload online across multiple computing platforms. Thus, while only one computing platform bears the additional performance overhead of the optimization process, Example 3 can improve the performance of application workloads running on multiple computing platforms.
[0088] The execution flow of Example 3 is as follows: Figure 7 As shown, firstly, identical workloads are started on computing platforms Z1, Z2, and Z3, and the same type of computation / memory access tasks are executed. Then, computing platform Z1, following the process in Example 1, completes the collection of dynamic program behavior information for a single computing platform through performance event sampling, and optimizes the target application workload based on the offline linking optimizer. Afterwards, the optimized application workload of computing platform Z1 is sent to computing platforms Z2 and Z3. Finally, the online code replacement module can be started simultaneously on computing platforms Z1, Z2, and Z3. Example 3 completes the work of simultaneously performing online code replacement on multiple computing platforms.
[0089] Example 4:
[0090] Single-machine sampling and optimization, multi-machine deployment: The target processor lacks hardware registers for collecting program-specific dynamic behavior information. Computing platforms W1, W2, and W3, all running processor D, contain workloads to be optimized. These platforms are used to execute specific types of computation / memory access tasks with the same application workload. Furthermore, the computing platform running processor D lacks hardware registers for recording program-specific dynamic behavior information. Example 4 aims to collect program dynamic behavior information on a single computing platform and optimize the target program through post-linking optimization, then deploy the optimized workload online across multiple computing platforms. Thus, by bearing the additional performance overhead of the optimization process on only one computing platform, Example 4 can improve the performance of application workloads running on multiple computing platforms.
[0091] The execution flow of Example 4 is as follows: Figure 8 As shown, firstly, the same workload is started on computing platforms W1, W2, and W3, and the same type of computing tasks are executed. Then, computing platform W1, following the process in Example 2, completes the collection of dynamic program behavior information for a single computing platform and optimizes the target application workload based on the offline linking optimizer through dynamic binary instrumentation. Afterwards, the optimized application workload of computing platform W1 is sent to computing platforms W2 and W3. Finally, the online code replacement module is started simultaneously on computing platforms W1, W2, and W3. Example 4 completes the work of simultaneously performing online code replacement on multiple computing platforms.
[0092] Example 5:
[0093] Single-machine sampling, optimization, and deployment: The offline binary optimizer primarily performs code layout optimization. The computing platform H with processor model E has a workload to be optimized. Example 5 aims to complete the collection of dynamic program behavior information, binary optimization based on the post-link optimizer, and online deployment of the optimized workload on a single computing platform. Specifically, Example 5 aims to optimize the code layout of the binary executable file using dynamic program branch information. Compared to the original application workload, the optimized workload can improve the CPU's instruction cache hit rate and reduce the pressure on the processor pipeline front end during execution, resulting in a significant performance improvement for the target application workload.
[0094] The execution flow of Example 5 is as follows: Figure 9As shown, the target application workload is first started in a specified process. Then, the program dynamic behavior information acquisition module begins collecting and parsing dynamic branch information within a specified interval of this process. This information mainly includes the starting address of branch jumps in basic blocks / functions, the ending address of branch jumps, and the number of branch jumps that occur during program execution. Next, the offline binary optimization module takes the program dynamic branch information and the target application workload as input, performs code layout optimizations such as basic block rearrangement and function rearrangement, and outputs a new, higher-performing application workload. Finally, the online code replacement module replaces the code of the target application workload with the code of the optimized application workload, completing the online code replacement. The program can then continue running to execute the optimized application workload code, achieving the goal of improving the performance of a specific application workload on the computing platform H through linking optimization.
[0095] This embodiment takes code layout optimization as an example. Other code optimizations are implemented in a similar way, except that the information collected by the information collection module and the optimizations performed by the offline binary optimization module are different.
[0096] Example 6:
[0097] This embodiment provides an optimization example in a specific implementation scenario, as follows:
[0098] Single-machine sampling and optimized deployment; the offline binary optimizer primarily performs code layout optimization. The processor model is... A Gold 5218R processor is used on a Linux computing platform (Ubuntu 20.04.5LTS) with an unoptimized MySQL workload (version 8.0.11). This platform primarily uses MySQL in a read-only scenario where only data is read from the database. This embodiment aims to complete the collection of dynamic program behavior information, binary optimization based on the post-link optimizer, and online deployment of the optimized MySQL on a single computing platform. Specifically, this embodiment aims to optimize the code layout of the MySQL application in a read-only scenario using dynamic branching information. The Gold 5218R processor uses the Intel 64 instruction set architecture and can utilize the Intel LBR hardware to collect dynamic branch information of programs and use this information to optimize code layout.
[0099] The execution flow of this embodiment is as follows: Figure 10As shown, MySQL is first started in a specified process. Then, the program dynamic behavior information acquisition module begins collecting and parsing dynamic branch information within a specified interval of this process. This information mainly includes the starting address of branch jumps in basic blocks / functions, the ending address of branch jumps, and the number of branch jumps that occur during program execution. Afterwards, the offline binary optimization module takes the program dynamic branch information and the MySQL application as input, performs code layout optimizations such as basic block rearrangement and function rearrangement, and outputs the optimized MySQL application. Finally, the online code replacement module replaces the original MySQL code with the optimized MySQL code, completing the online code replacement. The program can then continue running to execute the optimized MySQL program, achieving the goal of improving the load performance of the MySQL application through linking optimization. Sysbench tests were conducted before and after optimization to measure the transaction volume per second of MySQL in a read-only scenario. Compared to before optimization, the optimized MySQL achieved a 32% improvement in transaction volume per second in a read-only scenario. The performance improvement data in this embodiment is based on code layout optimization for read-only MySQL applications on the Intel64 platform. Performance improvement data obtained by performing different types of optimizations for different workloads on different platforms will vary.
[0100] References
[0101] [1]Panchenko M,Auler R,Nell B,et al.Bolt: a practical binary optimizer for data centers and beyond[C] / / 2019IEEE / ACM International Symposium on CodeGeneration and Optimization(CGO).IEEE,2019:2-14.
[0102] [2] Shen H, Pszeniczny K, Lavaee R, et al. Propeller: A Profile Guided, Relinking Optimizer for Warehouse-Scale Applications [C] / / Proceedings of the 28th ACM International Conference on Architectural Support for Programming Languages and Operating Systems, Volume 2. 2023: 617-631. The scope of protection of this invention is not limited to the above embodiments. Variations and advantages that can be conceived by those skilled in the art without departing from the spirit and scope of this invention are included in this invention and are protected by the appended claims.
Claims
1. A multi-platform supported binary optimization system, characterized in that, The binary optimization system comprises a program dynamic behavior information acquisition module, an offline binary optimization module and an online code replacement module; the program dynamic behavior information acquisition module, the offline binary optimization module and the online code replacement module are connected through a control program; The program dynamic behavior information acquisition module is configured to acquire and analyze program dynamic behavior information in a process; when there is a hardware register associated with the program dynamic behavior information to be acquired in a processor for executing a target application load, a hardware-based performance event sampling scheme is adopted to acquire specific dynamic behavior information of the program; when there is no hardware register associated with the program dynamic behavior information to be acquired in the processor for executing the target application load, a dynamic binary instrumentation scheme is adopted to acquire the program dynamic behavior information to be acquired; The offline binary optimization module is configured to optimize the target application load; the offline binary optimization module further comprises a binary file disassembler, a code optimizer and a binary file re-linker; The binary file disassembler is configured to perform a disassembly operation on a binary executable file corresponding to the collected program dynamic behavior information; The code optimizer is configured to optimize platform-independent code; the code optimization method comprises code layout optimization, instruction folding optimization, indirect call elimination optimization, data layout optimization and inline optimization; The binary file re-linker is configured to link the optimized code blocks again to generate an optimized binary executable file of the same instruction set architecture as the input executable file; The online code replacement module is configured to replace source code with the code of the target application load obtained through optimization; the online code replacement module further comprises a process controller, a machine code obtainer and a code refresher; The process controller is configured to pause a process in which the target work load is located before code replacement and extract current function call stack information of the process, and resume the target process and continue to execute the optimized application load after the code replacement is completed; The machine code obtainer is configured to compare and obtain machine code of a changed virtual function table and function in the optimized binary executable file, and write the machine code to a specified directory; and re-encode machine code of a function call instruction inside a function in the call stack of the current process in the original binary executable file, so that the target function pointed to by the machine code is the optimized function, and the modified optimized machine code is also written to the specified directory; The code refresher is configured to allocate additional virtual address space in the target process, and then write the optimized machine code information recorded by the machine code obtainer to the newly allocated address space.
2. The binary optimization system of claim 1, wherein, The program dynamic behavior information acquisition module further comprises a hardware detector, an information acquisition module and an information integration module; The hardware detector is configured to detect whether there is a hardware register associated with the program dynamic behavior information to be acquired in a processor for executing a target application load; The information acquisition module selects an information acquisition path according to whether there is a hardware register. The information integration module is used for integrating the collected program dynamic behavior information into program specific dynamic behavior information in the input format required by the offline binary optimization module.
3. A method for binary optimization supporting multiple platforms, comprising: The method comprises the following steps: Step one, starting the target application load in the process, collecting and analyzing the program dynamic behavior information in the process; Step two, taking the program dynamic behavior information obtained in step one and the target application load as input, disassembling the binary code corresponding to the application load, optimizing it, and outputting the optimized application load after linking; Step three, replacing the code of the original application load with the optimized application load, realizing online code replacement, and further realizing the optimization of the application load.
4. The binary optimization method of claim 3, wherein, In step one, the program dynamic behavior information refers to the behavior information collected during the dynamic execution of the program, which is used to guide the performance optimization of the binary file; the program dynamic behavior information includes the call frequency of functions, the call relationship between functions, the execution frequency and hit of program branch instructions, and the memory access mode of program memory access instructions.
5. The binary optimization method of claim 3, wherein, In step two, the input binary file of the application load is disassembled, and then the program dynamic behavior information collected in step one is analyzed and sorted; different types of binary files are optimized according to the differences of the input information; finally, the optimized disassembled file is linked into an executable binary file; the optimization includes program inlining optimization guided by function execution frequency and function call relationship, code layout optimization guided by program branch instructions and hit, instruction folding optimization guided by program execution flow information, indirect call elimination optimization guided by program indirect branch target address information, and data layout optimization guided by memory access mode of program memory access instructions.
6. The binary optimization method of claim 3, wherein, In step three, during the execution of the target program, the original binary file with performance to be improved is replaced with the optimized binary file using methods including process control, process call stack analysis, machine code extraction and injection, without affecting the normal execution of the program, the performance of the target program is improved; before the replacement operation, the target program is paused, the function information on the current process call stack is obtained first during the code replacement process, and then the optimized code is inserted with the function on the current process call stack as the entry point, and the execution of the program is restored after the code replacement.
Citation Information
Patent Citations
Binary translation method combining static optimization
CN101452396A
Method for injecting test codes for Linux system
CN101984409A