Memory access monitoring method, electronic equipment and storage medium

By introducing the first preset exception instruction and a preset interceptor in the target application, the problem that the prior art cannot monitor the instant compilation and generation of memory access instructions is solved, and flexible monitoring and management of these instructions is realized.

CN120123174APending Publication Date: 2025-06-10PHYTIUM TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510185372.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-19
Publication Date
2025-06-10

AI Technical Summary

Technical Problem

The prior art cannot effectively monitor the memory access instructions generated through instant compilation technology, resulting in the inability to effectively manage these instructions.

Method used

By introducing the first preset exception instruction, the hot spot memory access instruction is replaced with the instruction, and the exception instruction is intercepted by the preset interceptor, and the monitoring code block is redirected to output the monitoring information.

Benefits of technology

It realizes monitoring of hot-spot memory access instructions generated through instant compilation technology, avoids virtual address space limitations, and improves monitoring flexibility and applicability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120123174A_ABST
    Figure CN120123174A_ABST
Patent Text Reader

Abstract

The invention provides a memory access monitoring method, electronic equipment and a storage medium, and relates to the technical field of computers. According to the method, through introduction of the first preset exception instruction, after the hotspot memory access instruction is obtained through sampling, the hotspot memory access instruction is replaced with the first preset exception instruction and runs, and exception detection can be performed in the running process of the first preset exception instruction; according to the method and the device, the hotspot access instruction generated by the instant compiling technology can be monitored, and the problem that a virtual address hole inserted into the monitoring code block cannot be found when a virtual address corresponding to an instant compiling code is occupied is avoided; in other words, the monitoring process does not need to be limited by the virtual address space corresponding to the instant compiling code, flexible monitoring can be achieved, and the applicability of the memory access monitoring method is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a memory access monitoring method, an electronic device and a storage medium. Background Art

[0002] Memory access instructions, also known as memory access instructions, are instructions used in computer programming to read data from memory (load instructions) or write data to memory (store instructions). Effective memory management can be achieved by monitoring memory access instructions during the design process.

[0003] In the prior art, when monitoring applications based on the ARM architecture, only memory access instructions generated by non-just-in-time compilation technology are supported for monitoring. However, due to the complexity of actual application scenarios, the monitoring of memory access instructions generated by just-in-time compilation technology is often also involved.

[0004] However, due to the particularity of just-in-time compilation technology, existing monitoring methods are not applicable to monitoring memory access instructions generated by just-in-time compilation technology. Summary of the invention

[0005] The purpose of the present application is to provide a memory access monitoring method, electronic device and storage medium to address the deficiencies in the above-mentioned prior art, which can monitor memory access instructions generated by just-in-time compilation technology.

[0006] To achieve the above purpose, the technical solution adopted in the embodiment of the present application is as follows:

[0007] In a first aspect, the present invention provides a memory access monitoring method, the method comprising:

[0008] Sampling and acquiring hot memory access instructions during the operation of the target application, wherein the hot memory access instructions are generated by using a just-in-time compilation technology;

[0009] The hotspot memory access instruction is replaced with a first preset exception instruction, and the first preset exception instruction is run, wherein the first preset exception instruction is capable of performing exception detection when running, and redirecting the execution of a monitoring code block to output monitoring information for the hotspot memory access instruction, and the monitoring code block includes the hotspot memory access instruction.

[0010] In an optional implementation, the executing the first preset exception instruction includes:

[0011] Running the first preset exception instruction, if the kernel detects the first preset exception instruction, querying whether the first preset exception instruction is registered in the user mode signal processing program;

[0012] If registered, the processing is performed according to the processing method recorded in the user mode signal processing program.

[0013] In an alternative embodiment, running the first preset exception instruction includes:

[0014] Running the first preset exception instruction and intercepting the first preset exception instruction through a preset interceptor.

[0015] In an alternative embodiment, the first preset exception instruction can indicate a first virtual address corresponding to the hot memory access instruction, and the preset interceptor is configured to register a preset address mapping relationship, where the preset address mapping relationship includes: a mapping relationship between the first virtual address of the hot memory access instruction corresponding to the first preset exception instruction, the call address of the monitoring code block, and the second virtual address indicated by the second preset exception instruction. Among them, the monitoring code block ends with the second preset exception instruction, and the second preset exception instruction is used to return to execute the next instruction of the first preset exception instruction after the monitoring code block is executed;

[0016] After running the first preset exception instruction and intercepting the first preset exception instruction through the preset interceptor, it further includes:

[0017] Running the first preset exception instruction and intercepting the first preset exception instruction through the preset interceptor;

[0018] Based on the call address of the monitoring code block corresponding to the first virtual address in the preset address mapping relationship, redirecting the execution of the monitoring code block and outputting monitoring information for the hot memory access instruction;

[0019] Running the second preset exception instruction and intercepting the second preset exception instruction through the preset interceptor, and based on the second virtual address corresponding to the call address of the monitoring code block in the preset address mapping relationship, returning to execute the next instruction of the first preset exception instruction.

[0020] In an alternative embodiment, redirecting the execution of the monitoring code block and outputting monitoring information for the hot memory access instruction includes:

[0021] According to the call address of the monitoring code block corresponding to the first preset exception instruction in the preset address mapping relationship;

[0022] According to the current thread identifier corresponding to the target application, obtaining the monitoring stack corresponding to the current thread, saving the stack pointer of the current thread, and making the stack pointer point to the monitoring stack;

[0023] According to the call address of the monitoring code block, executing the monitoring code block to output monitoring information for the hot memory access instruction, and marking the end of the exception handling of the first preset exception instruction.

[0024] In an alternative embodiment, executing the monitoring code block and outputting monitoring information for the hot memory access instruction includes:

[0025] Executing the monitoring code block, disassembling and parsing the hot memory access instruction to obtain a memory access code description data structure corresponding to the hot memory access instruction;

[0026] Calculating the memory access address and access mode of the hot memory access instruction according to the memory access code description data structure;

[0027] Using the memory access address and the access mode as parameters, calling a monitoring function in the executed monitoring code block, and running the hot memory access instruction, so that the hot memory access instruction is monitored through the monitoring function, and monitoring information for the hot memory access instruction is output.

[0028] In an alternative embodiment, the preset interceptor is further configured to include: a preset thread mapping relationship, which can be dynamically constructed when the target application is running. The preset thread mapping relationship includes: the thread identifier of the current thread corresponding to the target application, the monitoring stack address, and the stack pointer before the current thread executes the hot memory access instruction. Executing the second preset exception instruction and returning to execute the next instruction of the first preset exception instruction includes:

[0029] Executing the second preset exception instruction, and obtaining the address of the next instruction of the first virtual address corresponding to the second virtual address indicated by the second preset exception instruction according to the preset address mapping relationship;

[0030] Querying the preset thread mapping relationship according to the thread identifier of the current thread corresponding to the target application to obtain the stack pointer before executing the hot memory access instruction;

[0031] Modifying the exception scene according to the stack pointer before executing the hot instruction, so that the original stack corresponding to the current thread is used after the exception handling ends, and executing the next instruction of the hot memory access instruction according to the address of the next instruction of the first virtual address, and marking the end of the exception handling of the second preset exception instruction.

[0032] In an alternative embodiment, saving the stack pointer of the current thread and obtaining the monitoring stack corresponding to the current thread includes:

[0033] Determining whether the current thread has a monitoring stack;

[0034] If the current thread has a monitoring stack, returning the address of the monitoring stack;

[0035] If the monitoring stack does not exist for the current thread, an independent monitoring stack is allocated for the current thread in user mode or kernel mode.

[0036] In an alternative embodiment, the step of, if the monitoring stack does not exist for the current thread, allocating an independent monitoring stack for the current thread in user mode, includes:

[0037] If the monitoring stack does not exist for the current thread, a monitoring stack is allocated for the current thread from a preset stack virtual address segment, and the preset stack virtual address segment is pre-registered by the preset interceptor.

[0038] In an alternative embodiment, the step of sampling and obtaining hot memory access instructions during the running of a target application includes:

[0039] Sampling and obtaining initial hot instructions during the running of the target application;

[0040] If it is determined that the initial hot instruction is not a hot memory access instruction, the hot memory access instruction is searched for according to a preset rule.

[0041] In an alternative embodiment, the step of, if it is determined that the initial hot instruction is not a hot memory access instruction, searching for the hot memory access instruction according to a preset rule, includes:

[0042] If it is determined that the initial hot instruction is not a hot memory access instruction, it is searched whether there is a hot memory access instruction among the first N instructions before the initial hot instruction. If there is, the first hot memory access instruction closest to the initial hot instruction is used as the hot memory access instruction;

[0043] If there is no hot memory access instruction among the first N instructions before the initial hot instruction, it is searched whether there is a hot memory access instruction among the last M instructions after the initial hot instruction. If there is, the second hot memory access instruction closest to the initial hot instruction is used as the hot memory access instruction, where N and M are integers greater than 1.

[0044] In an alternative embodiment, the step of replacing the hot memory access instruction with a first preset exception instruction and running the first preset exception instruction further includes:

[0045] Restoring the first preset exception instruction to the hot memory access instruction and uninstalling the preset interceptor.

[0046] In an alternative embodiment, the monitoring information of the hot memory access instruction includes: the memory access address and / or access latency of the hot memory access instruction.

[0047] In a second aspect, the present invention provides a memory access monitoring device, including:

[0048] An acquisition module, configured to sample and acquire hot memory access instructions during the running of a target application, where the hot memory access instructions are generated through just-in-time compilation technology;

[0049] A running module, configured to replace the hot memory access instructions with first preset exception instructions and run the first preset exception instructions. When the first preset exception instructions are running, exception detection can be performed, and the execution of a monitoring code block can be redirected to output monitoring information for the hot memory access instructions, where the monitoring code block includes the hot memory access instructions.

[0050] In an optional implementation manner, running the first preset exception instructions includes:

[0051] Running the first preset exception instructions. If the kernel detects the first preset exception instructions, query whether the first preset exception instructions are registered in a user-mode signal handler;

[0052] If registered, perform processing according to the processing method recorded in the user-mode signal handler.

[0053] In an optional implementation manner, running the first preset exception instructions includes:

[0054] Running the first preset exception instructions and intercepting the first preset exception instructions through a preset interceptor.

[0055] In an optional implementation manner, the first preset exception instructions can indicate a first virtual address corresponding to the hot memory access instructions. The preset interceptor is configured to register a preset address mapping relationship, where the preset address mapping relationship includes: a mapping relationship between the first virtual address of the hot memory access instructions corresponding to the first preset exception instructions, the call address of the monitoring code block, and a second virtual address indicated by second preset exception instructions. The monitoring code block ends with the second preset exception instructions, and the second preset exception instructions are used to return and execute the next instruction of the first preset exception instructions after the monitoring code block is executed;

[0056] The running module is specifically configured to run the first preset exception instructions and intercept the first preset exception instructions through a preset interceptor;

[0057] Based on the call address of the monitoring code block corresponding to the first virtual address in the preset address mapping relationship, redirect the execution of the monitoring code block and output monitoring information for the hot memory access instructions;

[0058] Run the second preset exception instruction and intercept the second preset exception instruction through the preset interceptor. Based on the second virtual address corresponding to the call address of the monitored code block in the preset address mapping relationship, return and execute the next instruction of the first preset exception instruction.

[0059] In an alternative embodiment, the running module is specifically configured to obtain the call address corresponding to the monitored code block corresponding to the first preset exception instruction according to the preset address mapping relationship;

[0060] According to the current thread identifier corresponding to the target application, obtain the monitored stack corresponding to the current thread, save the stack pointer of the current thread, and make the stack pointer point to the monitored stack;

[0061] According to the call address corresponding to the monitored code block, execute the monitored code block to output monitoring information for the hot memory access instruction, and mark the end of the exception handling of the first preset exception instruction.

[0062] In an alternative embodiment, the running module is specifically configured to execute the monitored code block, perform disassembly and parsing on the hot memory access instruction, and obtain the memory access code description data structure corresponding to the hot memory access instruction;

[0063] According to the memory access code description data structure, calculate the memory access address and access method of the hot memory access instruction;

[0064] Take the memory access address and the access method as parameters, call and execute the monitoring function in the monitored code block, and run the hot memory access instruction, so that the hot memory access instruction is monitored through the monitoring function, and monitoring information for the hot memory access instruction is output.

[0065] In an alternative embodiment, the preset interceptor is further configured to include: a preset thread mapping relationship, which can be dynamically constructed when the target application is running. The preset thread mapping relationship includes: the thread identifier of the current thread corresponding to the target application, the monitored stack address, and the stack pointer of the current thread before executing the hot memory access instruction;

[0066] The running module is specifically configured to execute the second preset exception instruction, obtain the address of the next instruction of the first virtual address corresponding to the second virtual address indicated by the second preset exception instruction according to the preset address mapping relationship; query the preset thread mapping relationship according to the thread identifier of the current thread corresponding to the target application, and obtain the stack pointer before executing the hot memory access instruction; modify the exception scene according to the stack pointer before executing the hot instruction so that the original stack corresponding to the current thread is used after the exception handling ends, and execute the next instruction of the hot memory access instruction according to the address of the next instruction of the first virtual address, and mark the end of the exception handling of the second preset exception instruction.

[0067] In an alternative embodiment, the running module is specifically configured to determine whether the current thread has a monitored stack.

[0068] If the current thread has a monitored stack, return the address of the monitored stack.

[0069] If the current thread does not have the monitored stack, allocate an independent monitored stack for the current thread in user mode or kernel mode.

[0070] In an alternative embodiment, the running module is specifically configured to, if the current thread does not have the monitored stack, allocate a monitored stack for the current thread from a preset stack virtual address segment, and the preset stack virtual address segment is pre-registered by the preset interceptor.

[0071] In an alternative embodiment, the obtaining module is specifically configured to sample and obtain the initial hot instruction during the running of the target application.

[0072] If it is determined that the initial hot instruction is not a hot memory access instruction, find the hot memory access instruction according to a preset rule.

[0073] In an alternative embodiment, the obtaining module is specifically configured to, if it is determined that the initial hot instruction is not a hot memory access instruction, check whether there is a hot memory access instruction among the first N instructions before the initial hot instruction. If there is, use the first hot memory access instruction closest to the initial hot instruction as the hot memory access instruction.

[0074] If there is no hot memory access instruction among the first N instructions before the initial hot instruction, check whether there is a hot memory access instruction among the next M instructions after the initial hot instruction. If there is, use the second hot memory access instruction closest to the initial hot instruction as the hot memory access instruction, where N and M are integers greater than 1.

[0075] In an alternative embodiment, the running module is further configured to restore the first preset exception instruction to the hot memory access instruction and unload the preset interceptor.

[0076] In an alternative embodiment, the monitoring information of the hot memory access instruction includes: the memory access address and / or access latency of the hot memory access instruction.

[0077] In a third aspect, the present invention provides an electronic device, including: a processor, a storage medium, and a bus. The storage medium stores machine-readable instructions executable by the processor. When the electronic device runs, the processor communicates with the storage medium through the bus. The processor executes the machine-readable instructions to perform the steps of the memory access monitoring method according to any one of the foregoing embodiments.

[0078] In a fourth aspect, the present invention provides a computer-readable storage medium, on which a computer program is stored. When the computer program is run by a processor, it performs the steps of the memory access monitoring method according to any one of the foregoing embodiments.

[0079] The beneficial effects of this application are as follows:

[0080] The memory access monitoring method provided by the embodiments of this application, through the introduction of a first preset abnormal instruction, can, after sampling and obtaining a hot memory access instruction, replace the hot memory access instruction with the first preset abnormal instruction and run it. During the running process of the first preset abnormal instruction, it can be intercepted by a preset interceptor and enter the kernel, and be redirected to execute a monitoring code block to output the monitoring information for the hot memory access instruction, which can achieve the monitoring of hot memory access instructions generated by just-in-time compilation technology, avoid the problem that the virtual address hole for inserting the monitoring code block cannot be found when the virtual address corresponding to the just-in-time compilation code is occupied, that is, the monitoring process does not need to be limited by the virtual address space corresponding to the just-in-time compilation code, and can achieve flexible monitoring, improving the applicability of the memory access monitoring method. BRIEF DESCRIPTION OF THE DRAWINGS

[0081] In order to more clearly illustrate the technical solutions of the embodiments of this application, the following will briefly introduce the drawings required in the embodiments. It should be understood that the following drawings only show some embodiments of this application, and therefore should not be regarded as a limitation of the scope. For those of ordinary skill in the art, other related drawings can be obtained based on these drawings without creative efforts.

[0082] Figure 1 It is a schematic flowchart of a memory access monitoring method provided by an embodiment of this application;

[0083] Figure 2 It is a schematic flowchart of another memory access monitoring method provided by an embodiment of this application;

[0084] Figure 3 It is a schematic diagram of a memory access monitoring provided by an embodiment of this application;

[0085] Figure 4 It is a schematic flowchart of another memory access monitoring method provided by an embodiment of the present application;

[0086] Figure 5 It is a schematic flowchart of another memory access monitoring method provided by an embodiment of the present application;

[0087] Figure 6 It is a schematic flowchart of another memory access monitoring method provided by an embodiment of the present application;

[0088] Figure 7 It is a schematic flowchart of another memory access monitoring method provided by an embodiment of the present application;

[0089] Figure 8 It is a schematic flowchart of another memory access monitoring method provided by an embodiment of the present application;

[0090] Figure 9 It is a schematic flowchart of another memory access monitoring method provided by an embodiment of the present application;

[0091] Figure 10 It is a schematic flowchart of another memory access monitoring method provided by an embodiment of the present application;

[0092] Figure 11 It is a schematic diagram of functional modules of a memory access monitoring device provided by an embodiment of the present application;

[0093] Figure 12 It is a schematic diagram of the structure of an electronic device provided by an embodiment of the present application. Detailed implementation manners

[0094] To make the objectives, technical solutions, and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are some, but not all, of the embodiments of the present application. The components of the embodiments of the present application described and illustrated in the accompanying drawings here can be arranged and designed in various different configurations.

[0095] Therefore, the following detailed description of the embodiments of the present application provided in the accompanying drawings is not intended to limit the scope of the claimed present application, but merely represents selected embodiments of the present application. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present application without creative efforts fall within the scope of protection of the present application.

[0096] It should be noted that: like reference numerals and letters denote like items in the following drawings, and thus, once an item is defined in one drawing, it does not need to be further defined and explained in subsequent drawings.

[0097] In the prior art, when monitoring an application based on the ARM architecture, only memory access instructions generated by non-just-in-time compilation technology are supported. However, due to the complexity of the actual application scenario, it often involves monitoring memory access instructions generated by just-in-time compilation technology.

[0098] However, due to the particularity of just-in-time compilation technology, the existing monitoring methods cannot be applied to memory access instructions generated by just-in-time compilation technology.

[0099] Before introducing the present application, first, a brief introduction to just-in-time (JIT) compiler technology is given. JIT (Just-In-Time) compilation is a dynamic compilation technology that immediately compiles bytecode or intermediate code into native machine code during program execution, thereby improving the execution efficiency of the program. During the JIT compilation process, the generated native machine code needs to have a definite address in memory, and this address is called the virtual address corresponding to the JIT code (Virtual Address). One difference between JIT code and ordinary code is that the memory allocation method of JIT code generally is the same as that of dynamically allocating data buffers, so it is not easy to reserve a virtual address memory area for storing monitoring code near the virtual address segment of JIT code.

[0100] In view of this, the present application provides a memory access monitoring method, which can monitor hot memory access instructions generated by just-in-time compilation technology and has the characteristic of strong applicability.

[0101] Figure 1 It is a schematic flowchart of a memory access monitoring method provided by an embodiment of the present application. Among them, the execution subject of the memory access monitoring method can be a target device such as a computer, a server, a processor, etc. As Figure 1 shown, the method may include:

[0102] Step 101, sample and obtain hot memory access instructions during the running of the target application, and the hot memory access instructions are generated by just-in-time compilation technology.

[0103] The hot memory access instructions during the running of the target application, that is, among the native machine codes generated by just-in-time compilation technology during the running of the target application, the frequently called memory access instructions. Among them, the memory access instructions, that is, the instructions that interact with the main memory (RAM), such as Load (load) instructions, Store (store) instructions, etc., are not limited here.

[0104] Optionally, the sampling and positioning of the hot memory access instructions can be implemented by a preset performance analysis tool. For example, tools such as Perf, VTune, gprof, etc. can be borrowed for implementation, which is not limited here.

[0105] Step 102: Replace the hot memory access instruction with a first preset exception instruction and run the first preset exception instruction.

[0106] Among them, when the first preset exception instruction runs, it can perform exception detection and redirect the execution to the monitoring code block to output the monitoring information for the hot memory access instruction. The monitoring code block includes the hot memory access instruction.

[0107] Among them, the preset interceptor has the function of intercepting illegal instructions from entering the kernel and redirecting the execution to the specified code.

[0108] Optionally, for the target application, after the hot memory access instruction during the running of the target application is located and obtained, the hot memory access instruction can be replaced with a first preset exception instruction. The first preset exception instruction can be a preset first illegal instruction, which can perform exception detection when it runs. Among them, if this exception is detected, it can be redirected to the monitoring code block for execution, and the monitoring information of the hot memory access instruction can be output by executing the monitoring code block.

[0109] In some embodiments, the monitoring code block may include the hot memory access instruction and a monitoring function. Among them, the monitoring of the hot memory access instruction can be realized by calling the memory access monitoring function while the hot memory access instruction is running.

[0110] Applying the embodiments of the present application, it can be seen that through the introduction of the first preset exception instruction, after the hot memory access instruction is sampled and obtained, the hot memory access instruction can be replaced with the first preset exception instruction and run. When the first preset exception instruction runs, it can be recognized as an exception and redirected to the monitoring code block for execution to output the monitoring information for the hot memory access instruction, which can realize the monitoring of the hot memory access instruction generated by the just-in-time compilation technology, avoid the problem that the virtual address hole for inserting the monitoring code block cannot be found when the virtual address corresponding to the just-in-time compilation code is occupied, that is, the monitoring process does not need to be limited by the virtual address space corresponding to the just-in-time compilation code, and flexible monitoring can be realized, improving the applicability of the memory access monitoring method.

[0111] In addition, compared with the existing method of jumping to the monitoring code block through a jump instruction, the present application can also solve the problem that the accurate positioning of the monitoring code block cannot be realized due to the limited jump offset of the jump instruction in some architectures. For example, in ARM, the jump instruction on ARM can only jump to the virtual address at the current instruction address plus / minus 128M.

[0112] In some embodiments, the first preset exception instruction can be detected and recognized in various ways. Optionally, the above running of the first preset exception instruction includes:

[0113] Run a first preset exception instruction. If the kernel detects the first preset exception instruction, query whether the first preset exception instruction is registered in the user-mode signal handler; if it is registered, execute the processing according to the processing method recorded in the user-mode signal handler.

[0114] Optionally, the user-mode signal handler can run in the application-layer user mode. The user-mode signal handler can be pre-registered with the first preset exception instruction and the corresponding processing method when the first preset exception instruction is recognized (that is, redirect the execution to the monitoring code block to output the monitoring information for the hot memory access instruction). In some embodiments, the corresponding processing method when the first preset exception instruction is recognized can be implemented by registering a signal processing function.

[0115] In combination with the embodiments of the present application, during the running of the first preset exception instruction, it can be recognized by the kernel (which can correspond to different types of kernels according to the different operating systems of the target devices to which the memory access monitoring method is applied). Further, the kernel can query whether the first preset exception instruction is registered in the user-mode signal handler. If it is registered, it can redirect the execution to the monitoring code block to output the monitoring information for the hot memory access instruction according to the processing method recorded in the user-mode signal handler. In addition, it should be noted that if it is not registered, the corresponding default processing method can be executed, that is, just follow the normal exception handling process.

[0116] It should be noted that in specific implementation, in terms of hardware behavior, when the first preset exception instruction is executed, the processor will generate an exception and the kernel will receive this exception. At this time, the kernel can send an illegal instruction signal to the user-mode signal handler; if the user-mode signal handler has registered the registered signal processing function corresponding to this exception, this function will be called to redirect the execution to the monitoring code block and output the monitoring information for the hot memory access instruction.

[0117] Applying the embodiments of the present application can achieve the monitoring of hot memory access instructions and output the corresponding monitoring information by means of the application-layer user mode without changing the kernel behavior, with the characteristics of flexible detection and strong applicability.

[0118] Optionally, the above-mentioned running of the first preset exception instruction includes: running the first preset exception instruction and intercepting the first preset exception instruction through a preset interceptor.

[0119] In some embodiments, the preset interceptor can directly execute in the kernel mode. Optionally, the preset interceptor can be an eBPF (Extended Berkeley Packet Filter) interceptor, or other interceptors with the same or similar functions as the eBPF interceptor, which is not limited herein and can be flexibly selected according to the actual application scenario.

[0120] During the execution of the first preset exception instruction, it can also be intercepted by a preset interceptor and enter the kernel. After interception, it is redirected to the monitoring code block for execution. By executing the monitoring code block, monitoring information for the corresponding hot memory access instruction is output. This realizes the monitoring of hot memory access instructions through the preset interceptor executed in the kernel state and outputs the corresponding monitoring information. Thus, there is no signal sending process, which can meet the requirements of various scenarios and further improve the flexibility of the method of this application.

[0121] Figure 2 It is a schematic flowchart of another memory access monitoring method provided by an embodiment of this application. Figure 3 It is a schematic diagram of a memory access monitoring provided by an embodiment of this application. Optionally, the first preset exception instruction can indicate the first virtual address corresponding to the hot memory access instruction. The preset interceptor is configured to register a preset address mapping relationship, which includes: the mapping relationship between the first virtual address of the hot memory access instruction corresponding to the first preset exception instruction, the call address corresponding to the monitoring code block, and the second virtual address indicated by the second preset exception instruction. Among them, the monitoring code block ends with the second preset exception instruction, and the second preset exception instruction is used to return and execute the next instruction of the first preset exception instruction according to the address of the next instruction of the first virtual address indicated by the second virtual address after the execution of the monitoring code block, that is, return and execute the next instruction of the hot memory access instruction.

[0122] It can be understood that the exception reason corresponding to the first preset exception instruction can be an attempt to enter the monitoring code; the exception reason corresponding to the second preset exception instruction can be an attempt to return to the next instruction of the hot memory access instruction.

[0123] In an alternative implementation, as Figure 2 shown, the above execution of the first preset exception instruction includes:

[0124] Step 201, execute the first preset exception instruction and intercept the first preset exception instruction through the preset interceptor.

[0125] It can be understood that since the first preset exception instruction can indicate the first virtual address corresponding to the hot memory access instruction, therefore, in combination with the preset address mapping relationship, when the first preset exception instruction is executed, the preset interceptor can not only intercept the first preset exception instruction, but also determine the call address of the corresponding monitoring code block according to the first virtual address indicated by the first preset exception instruction.

[0126] Step 202, based on the call address of the monitoring code block corresponding to the first virtual address in the preset address mapping relationship, redirect the execution to the monitoring code block and output the monitoring information for the hot memory access instruction.

[0127] Among them, according to the first virtual address indicated by the first preset exception instruction and the preset address mapping relationship pre-registered by the preset interceptor, the call address of the monitoring code block corresponding to the first virtual address can be determined; according to the determined call address of the monitoring code block, the execution of the monitoring code block can be redirected, and by executing the monitoring code block, the monitoring information for the hot memory access instruction can be output.

[0128] In some embodiments, the monitoring information may include: the memory access address of the hot memory access instruction, the access latency of the hot memory access instruction, the usage frequency of the hot memory access instruction, etc., which are not limited herein, and the type of monitoring information can be flexibly configured according to the actual application scenario.

[0129] Of course, in some embodiments, the monitoring information corresponding to each hot memory access instruction during the running of the target application can also be comprehensively analyzed, and then through this monitoring information, it can be further characterized: the memory access density of the target application, the throughput capacity of the memory system in the target device where the target application is deployed, the impact of memory access on the performance of the target application, the memory access time of the target application, etc., which are not limited herein. Among them, it can be understood that the monitoring code block with one or more functions can be set according to the actual monitoring purpose.

[0130] Looking at the target application to which the hot memory access instruction belongs, after obtaining the monitoring information of the hot memory access instruction, the memory of the target device where the target application is deployed can be managed based on this, or alternatively, the memory access code logic of the target application can be optimized to improve the running performance of the target application.

[0131] Optionally, the ways to manage the memory of the target device where the target application is deployed may include but are not limited to the following: for example, if most of the memory accessed by a certain hot memory access instruction is cache miss, then it can be considered to set the memory page it accesses as non-cacheable to avoid polluting the cache with their access; or, if it is expected that the access of a certain hot memory access instruction will not affect the hardware data prefetch function, an instruction to intervene in hardware prefetch can be inserted here; or, according to the relationship between the thread to which the hot memory access instruction belongs and the accessed memory address, in order to better determine the node binding strategy to be adopted for the corresponding memory area. For example, if this always accesses the memory allocated through a certain context and this memory is always accessed by certain threads, then tags can be established for the allocation of such memory so that they are allocated to adjacent memory areas, so that these memories can be set to be bound to a certain node or interleaved among certain nodes while interfering with other memory allocations.

[0132] Of course, it should be noted that the memory management based on the monitoring information of the hot memory access instruction is not limited to the above examples, and the management method can be flexibly set according to the actual application scenario.

[0133] In some embodiments, the monitoring code block may include the hot memory access instruction and the monitoring function. Among them, when the hot memory access instruction is running, the monitoring of the hot memory access instruction can be realized by calling the memory access monitoring function.

[0134] In addition, it should be noted that different hot memory access instructions may correspond to different first virtual addresses, and different first virtual addresses may correspond to different call addresses of the monitoring code block, which can be flexibly set according to the actual application scenario.

[0135] Step 203: Run the second preset exception instruction and intercept the second preset exception instruction through the preset interceptor. Based on the second virtual address corresponding to the call address of the monitoring code block in the preset address mapping relationship, return to execute the next instruction of the first preset exception instruction.

[0136] After outputting the monitoring information for the hot memory access instruction, in order to accurately return to execute the next instruction of the first preset exception instruction. Among them, when the preset interceptor monitors the processing of the exception caused by the second preset exception instruction by the operating system, according to the next instruction address corresponding to the address of the second preset exception instruction in the preset address mapping relationship, make the current thread return to execute the next instruction of the first preset exception instruction, that is, return to execute the next instruction of this hot memory access instruction.

[0137] Combined Figure 3 As shown, when performing memory access monitoring on the hot memory access instruction X1, the preset interceptor can be configured to register the preset address mapping relationship. The preset address mapping relationship includes: the mapping relationship between the first virtual address A corresponding to the hot memory access instruction X1, the call address B corresponding to the monitoring code block, and the second virtual address C indicated by the second preset exception instruction. Then the specific monitoring process is as follows:

[0138] For the hot memory access instruction X in the JIT-compiled code corresponding to the target application, the hot memory access instruction X1 can be replaced with the first preset exception instruction, and the first preset exception instruction is run. When the preset interceptor recognizes the first preset exception instruction, it will redirect to execute the monitoring code block based on the call address B of the monitoring code block in the preset address mapping relationship and output the monitoring information for the hot memory access instruction; further, when the preset interceptor recognizes the second preset exception instruction, it will redirect to execute the next instruction of the hot memory access instruction X1 based on the second virtual address C corresponding to the call address B of the monitoring code block in the preset address mapping relationship, that is, execute the instruction X2.

[0139] Figure 4This is a flowchart showing another memory access monitoring method provided by an embodiment of this application. In an alternative implementation, as Figure 4 shown, redirecting the execution of the monitoring code block and outputting monitoring information for hot memory access instructions includes:

[0140] Step 301: Obtain the call address of the monitoring code block corresponding to the first preset exception instruction according to the preset address mapping relationship.

[0141] Step 302: Obtain the monitoring stack corresponding to the current thread according to the current thread identifier of the target application, save the stack pointer of the current thread, and make the stack pointer point to the monitoring stack.

[0142] Step 303: Execute the monitoring code block according to the call address corresponding to the monitoring code block, output the monitoring information for the hot memory access instruction, and mark the end of the exception handling of the first preset exception instruction.

[0143] Optionally, the current thread identifier can be the thread number of the current thread of the target application. Among them, if the current exception is caused by running the first preset exception instruction, during the redirection process, the call address corresponding to the monitoring code block can be obtained according to the preset address mapping relationship, the stack pointer of the current thread can be saved, and the stack address of the monitoring stack corresponding to the current thread recorded currently can be obtained. Among them, the monitoring stack can be a user-mode monitoring stack. Among them, if it is not recorded currently, a user-mode monitoring stack with a predefined size is allocated for this thread, and this stack address is recorded, and the PC value and the value of the stack top register registered in the exception scene are modified, so that the registered PC value becomes the call address corresponding to the monitoring code block, and the stack top register value becomes the address of the user-mode monitoring stack, so that after the exception handling ends, it starts to execute from the monitoring code; finally, mark the end of the exception handling of the first preset exception instruction, so that the kernel will not continue to process this first preset exception later, but directly return to the user mode, start to execute from the monitoring code block, execute the memory access instruction, and output the monitoring information for the hot memory access instruction.

[0144] It should be noted that, in addition to the PC value and the stack top register, the monitoring stack corresponding to the current thread is used to save the context of other general registers. In some embodiments, the context saving of other general registers can be responsible by the interceptor to save in the user-mode monitoring stack, or can be saved by the monitoring code itself after obtaining control.

[0145] Applying the embodiments of this application can flexibly call the monitoring code block during the process of running the first preset exception instruction, and then realize the monitoring of the hot memory access instruction, and the normal operation of the target application will not be affected during the monitoring process.

[0146] Figure 5This is a flowchart showing another memory access monitoring method provided by an embodiment of this application. In an alternative implementation, as Figure 5 shown, the above-mentioned execution of the monitoring code block and output of the monitoring information for the hot memory access instructions include:

[0147] Step 401: Execute the monitoring code block, disassemble and analyze the hot memory access instruction, and obtain the memory access code description data structure corresponding to the hot memory access instruction.

[0148] Step 402: Calculate the memory access address and access method of the hot memory access instruction according to the memory access code description data structure.

[0149] Step 403: Use the memory access address and access method as parameters, call the monitoring function in the execution monitoring code block, and run the hot memory access instruction, so that the hot memory access instruction is monitored through the monitoring function, and the monitoring information for the hot memory access instruction is output.

[0150] Optionally, the monitoring code block can have a disassembly function. During the process of executing the monitoring code block, the hot memory access instruction can be disassembled and analyzed to generate the corresponding memory access code description data structure.

[0151] Among them, by disassembling and analyzing the hot memory access instruction, a memory access code description data structure can be generated. The memory access code description data structure can include: the change parameters of the general register, and characterize the relationship between the memory access address and access method (such as atomic access) of the hot memory access instruction and the saved register context, that is, how to calculate the accessed memory cell address and access method according to the saved register context.

[0152] Based on the above description, when the hot memory access instruction is executed, the memory access address and access method of the hot memory access instruction can be used as the call parameters of the monitoring function, and the user-defined monitoring function can be called to access the memory cell indicated by the memory access address of the hot memory access instruction, and the access time can be timed to obtain the access latency, so as to output the monitoring information for the hot memory access instruction. It should be noted that if the hot memory access instruction is a read operation instruction, the accessed value will also be obtained, and the specific content of the monitoring information is not limited here.

[0153] It should be noted that after step 403 is executed, the values of the general registers in the monitoring stack should also be updated according to the change parameters of the general registers in the memory access code description data structure, that is, the values of the corresponding general registers saved in the monitoring stack are updated.

[0154] In addition, similar to the saving of the general register values, the restoration of the general register values can be executed by the monitoring code, or by the interceptor after it learns that the monitoring code has executed an illegal instruction.

[0155] Figure 6 It is a schematic flowchart of another memory access monitoring method provided by an embodiment of this application. Optionally, the preset interceptor is further configured to include: a preset thread mapping relationship, which can be dynamically constructed when the target application is running, and it can include: the thread identifier of the current thread corresponding to the target application, the monitoring stack address, and the stack pointer before the current thread executes the hot memory access instruction.

[0156] In an alternative embodiment, as Figure 6 shown, the above-mentioned execution of the second preset exception instruction and returning to execute the next instruction of the first preset exception instruction includes:

[0157] Step 501, execute the second preset exception instruction, and obtain the next instruction address of the first virtual address corresponding to the second virtual address indicated by the second preset exception instruction according to the preset address mapping relationship.

[0158] Step 502, query the preset thread mapping relationship according to the thread identifier of the current thread corresponding to the target application, and obtain the stack pointer before executing the hot memory access instruction.

[0159] Step 503, modify the exception scene according to the stack pointer before executing the hot instruction, so that the original stack corresponding to the current thread is used after the exception handling ends, and execute the next instruction of the hot memory access instruction according to the next instruction address of the first virtual address, and mark the end of the exception handling of the second preset exception instruction.

[0160] Among them, the original stack corresponding to the current thread can be used to store temporary data and function call information during the operation of the target application, manage the execution of functions, and track return addresses, local variables, function parameters, etc.

[0161] Optionally, during the process of executing the second preset exception instruction, the second virtual address indicated by the second preset exception instruction and the stack pointer of the current thread saved above can be obtained according to the preset address mapping relationship, and the kernel is not allowed to continue to process this second preset exception in the future; in addition, the values of the stack register and the PC register saved in the exception scene should also be modified according to the stack pointer before executing the hot instruction, so that the next instruction of the hot memory access instruction is executed after the exception handling ends. Specifically, when executing, the second preset exception instruction can be executed according to the second virtual address indicated by the second preset exception instruction, and by executing this second preset exception instruction, it is possible to return to execute the next instruction of the first preset exception instruction, and by marking the end of the exception handling of the first preset exception instruction, it is possible to prevent the kernel from further processing this exception, but directly return it to the user state.

[0162] Applying the embodiments of the present application, by running the second preset exception instruction, it is possible to return to execute the next instruction of the first preset exception instruction after completing the monitoring of the hot memory access instruction, ensuring the normal operation of the subsequent instructions of the hot memory access instruction, and realizing that the introduction of the monitoring function does not affect the normal operation of the target application.

[0163] Figure 7 It is a flowchart of another memory access monitoring method provided by the embodiments of the present application. In an alternative embodiment, as Figure 7 shown, the above-mentioned steps of saving the stack pointer of the current thread and obtaining the monitoring stack corresponding to the current thread include:

[0164] Step 601: Determine whether the current thread has a monitoring stack.

[0165] Step 602: If the current thread has a monitoring stack, return the address of the monitoring stack.

[0166] Step 603: If the current thread does not have a monitoring stack, allocate an independent monitoring stack for the current thread through the user mode or the kernel mode.

[0167] Among them, in order to improve the applicability of the method of the present application, during the operation of the preset interceptor, it can be determined whether the current thread has a monitoring stack. The monitoring stack can be used to store temporary data and function call information during the execution of the monitored code block, manage the execution of functions, and track return addresses, local variables, function parameters, etc.

[0168] Optionally, if the current thread has a monitoring stack, the address of the monitoring stack can be returned, so as to accurately locate the monitoring stack according to the address of the monitoring stack, and mark the end of the exception handling of the first preset exception instruction through the monitoring stack; if the current thread does not have a monitoring stack, at this time, an independent monitoring stack can be allocated for the current thread through the user mode or the kernel mode.

[0169] It can be understood that if an independent monitoring stack is allocated for the current thread through the user mode, the monitoring stack is located in the user space; if an independent monitoring stack is allocated for the current thread through the kernel mode, the monitoring stack is located in the kernel space.

[0170] Among them, when allocating a monitoring stack for the current thread through the user mode, optionally, the corresponding relationship between the current thread and the stack space of the monitoring stack can be registered in the preset interceptor, so that the allocation can be completed through the preset interceptor.

[0171] Of course, in some embodiments, the monitoring stack allocated for the current thread can be obtained from the current thread stack corresponding to the current thread. For example, in a scenario where there is sufficient available space in the current thread stack, the monitoring stack can be allocated through the current thread stack. It should be noted that the offset can be adjusted according to the actual application scenario so as not to affect the storage of other data in the thread stack. Among them, the thread stack is a memory area for storing data such as local variables, method call information, and return addresses during the execution of a thread. Each thread has its own thread stack for storing the execution context information of the thread.

[0172] In an alternative embodiment, if the current thread does not have a monitoring stack, an independent monitoring stack is allocated for the current thread through the user mode, including:

[0173] If the current thread does not have a monitoring stack, a monitoring stack is allocated for the current thread from a preset stack virtual address segment, and the preset stack virtual address segment is pre-registered by a preset interceptor.

[0174] In some embodiments, when an independent monitoring stack is allocated for the current thread through the user mode, optionally, a preset stack virtual address segment can be pre-registered in the preset interceptor for the allocation of the monitoring stack. Among them, if the current thread does not have a monitoring stack, a segment can be sequentially allocated from the preset stack virtual address segment as the monitoring stack, and the address of the monitoring stack is recorded and the address of the monitoring stack is returned so that the monitoring stack can be accurately located based on the address of the monitoring stack subsequently.

[0175] Among them, the process of allocating a monitoring stack for the current thread from the preset stack virtual address segment includes: generating the stack top of the monitoring stack and setting the program counter (PC) of the monitoring stack.

[0176] Figure 8 It is a schematic diagram of a memory access monitoring method provided by an embodiment of the present application. Among them, the monitoring stack can be obtained by opening up in the user mode, as Figure 8 shown, the memory access monitoring method may include:

[0177] Step 701, sampling to obtain the hot memory access instructions during the operation of the target application, and the hot memory access instructions are generated by just-in-time compilation technology.

[0178] Step 702, replacing the hot memory access instructions with first preset exception instructions, and running the first preset exception instructions, and obtaining the first virtual address indicated by the first preset exception instructions through a preset interceptor.

[0179] Step 703, based on the preset interceptor, obtaining the stack pointer of the current thread corresponding to the target application before executing the hot memory access instructions.

[0180] Step 704: Based on the call address of the monitoring code block corresponding to the first virtual address in the preset address mapping relationship, redirect the execution of the monitoring code block to output the monitoring information for the hot memory access instruction, and mark the end of the exception handling for the first preset exception instruction.

[0181] For the description of the preset address mapping relationship, please refer to the foregoing related content and will not be elaborated here.

[0182] Step 705: Run the second preset exception instruction and intercept the second preset exception instruction through the preset interceptor. Based on the second virtual address corresponding to the call address of the monitoring code block in the preset address mapping relationship, return to execute the next instruction of the first preset exception instruction.

[0183] Figure 9 It is a flowchart of another memory access monitoring method provided by the embodiments of the present application. In an alternative embodiment, as Figure 9 shown, the above-mentioned sampling to obtain the hot memory access instructions during the running of the target application includes:

[0184] Step 801: Sample and obtain the initial hot instructions during the running of the target application.

[0185] Step 802: If it is determined that the initial hot instruction is not a hot memory access instruction, search for the hot memory access instruction according to the preset rules.

[0186] Among them, the initial hot instruction can be the first hot instruction during the running of the target application, or any hot instruction, which is not limited here. It can be flexibly set according to the actual application scenario.

[0187] Optionally, during the sampling process, based on the binary code corresponding to the target application, sample and obtain the initial hot instructions during the running of the target application, and then filter the initial hot instructions according to the preset rules to find the hot memory access instructions. Optionally, the preset rules can indicate the search direction of the hot memory access instruction relative to the initial hot instruction.

[0188] In some embodiments, the sampling and positioning of the initial hot instruction can be implemented through a preset performance analysis tool. For example, tools such as Perf, VTune, gprof, etc. can be borrowed for implementation, which is not limited here.

[0189] Figure 10 It is a flowchart of another memory access monitoring method provided by the embodiments of the present application. In an alternative embodiment, as Figure 10 shown, the above-mentioned if it is determined that the initial hot instruction is not a hot memory access instruction, then search for the hot memory access instruction according to the preset rules includes:

[0190] Step 901: If it is determined that the initial hot instruction is not a hot memory access instruction, check whether there is a hot memory access instruction among the first N instructions before the initial hot instruction. If there is, use the first hot memory access instruction closest to the initial hot instruction as the hot memory access instruction.

[0191] Step 902: If there is no hot memory access instruction among the first N instructions before the initial hot instruction, check whether there is a hot memory access instruction among the next M instructions after the initial hot instruction. If there is, use the second hot memory access instruction closest to the initial hot instruction as the hot memory access instruction.

[0192] Wherein, N and M are integers greater than 1. Optionally, the values of N and M can be the same or different. For example, the value of N can be 10 and the value of M can be 20; or, the values of both N and M are 30, which is not limited herein.

[0193] In some embodiments, when the initial hot instruction is not a hot memory access instruction, based on the position of the initial hot instruction, check whether there is a hot memory access instruction among its first N instructions. If there is, optionally, according to the principle of proximity, use the first hot memory access instruction closest to the initial hot instruction as the hot memory access instruction; if not, continue to check whether there is a hot memory access instruction among its next M instructions. If there is, optionally, according to the principle of proximity, use the second hot memory access instruction closest to the initial hot instruction as the hot memory access instruction.

[0194] It should be noted that if it is determined that there is still no hot memory access instruction among the next M instructions after the initial hot instruction, optionally, at this time, the values of N and / or M can be adjusted to expand the search range. For example, the values of N and M can be increased to 50 and continue the search until a hot memory access instruction is found. Of course, the adjustment method of N and M is not limited herein.

[0195] Illustrate with an example. Taking the adjustment of N as an example, for example, a fixed value (such as 10, 20, etc.) can be added each time, or N can be adjusted to S×N each time, where S is an integer greater than 1.

[0196] In addition, it should be noted that the specific search method is not limited thereto. For example, the next M instructions can be searched first, and then the first N instructions can be searched, which can be flexibly set according to the actual application scenario.

[0197] Optionally, in some embodiments, the following method can also be referred to for searching for hot memory access instructions: Obtain the basic code block containing the hot instruction, and search for hot memory access instructions within this basic code block, where the execution times of each instruction in this basic code block are exactly the same and there is no jump instruction.

[0198] In an alternative embodiment, after replacing the hot memory access instruction with a first preset exception instruction and running the first preset exception instruction, the following steps are further included:

[0199] Restore the first preset exception instruction to the hot memory access instruction and uninstall the preset interceptor.

[0200] Based on the foregoing description, for each hot memory access instruction, after outputting the monitoring information for the hot memory access instruction, the first preset exception instruction can be restored to the hot memory access instruction, and the preset interceptor can be uninstalled, so as to avoid reducing the running performance of the target application due to the installation of the preset interceptor.

[0201] Applying the embodiments of the present application realizes that the preset interceptor can be uninstalled in time after the monitoring function is completed, avoiding reducing the running performance of the target application due to the installation of the preset interceptor, and improving the applicability of the method of the present application.

[0202] In an alternative embodiment, the monitoring information of the hot memory access instruction includes: the memory access address and / or access latency of the hot memory access instruction.

[0203] Among them, the memory access address of the hot memory access instruction can indicate the address of the memory location accessed when the hot memory access instruction is executed; the access latency of the hot memory access instruction can indicate the time required from the start of execution of the hot memory access instruction to obtaining the memory data.

[0204] Optionally, the type of monitoring information can be flexibly selected according to the actual application scenario.

[0205] In addition, it should be noted that in addition to the above two types, the monitoring information can also include other types of information, such as: cache hit rate, memory access pattern, memory access bandwidth, etc., which are not limited herein. Among them, the cache hit rate can indicate the proportion of request data found in the cache; the memory access pattern can include: sequential access and random access; the memory access bandwidth can indicate the amount of data that can be transmitted per unit time.

[0206] Figure 11 FIG. is a schematic diagram of functional modules of a memory access monitoring device provided by an embodiment of the present application. The basic principle and technical effects generated by this device are the same as those of the corresponding method embodiments described above. For the sake of brief description, for parts not mentioned in this embodiment, reference can be made to the corresponding content in the method embodiments. As Figure 11 shown, the memory access monitoring device 100 includes:

[0207] An acquisition module 110, configured to sample and acquire hot memory access instructions during the running of the target application, and the hot memory access instructions are generated by just-in-time compilation technology;

[0208] The running module 120 is used to replace the hot memory access instruction with a first preset exception instruction and run the first preset exception instruction. When the first preset exception instruction runs, it can perform exception detection and redirect the execution to monitor the code block to output the monitoring information for the hot memory access instruction. The monitoring code block includes the hot memory access instruction.

[0209] In an optional implementation manner, the running module 120 is specifically configured to run the first preset exception instruction. If the kernel detects the first preset exception instruction, it queries whether the first preset exception instruction is registered in the user-mode signal handler.

[0210] If it is registered, it performs processing according to the processing method recorded in the user-mode signal handler.

[0211] In an optional implementation manner, the running module 120 is specifically configured to run the first preset exception instruction and intercept the first preset exception instruction through a preset interceptor.

[0212] In an optional implementation manner, the first preset exception instruction can indicate the first virtual address corresponding to the hot memory access instruction. The preset interceptor is configured to register a preset address mapping relationship, and the preset address mapping relationship includes: the mapping relationship between the first virtual address of the hot memory access instruction corresponding to the first preset exception instruction, the call address corresponding to the monitoring code block, and the second virtual address indicated by the second preset exception instruction. The second preset exception instruction is used to return and execute the next instruction of the first preset exception instruction after the monitoring code block is executed.

[0213] The running module 120 is specifically configured to run the first preset exception instruction and intercept the first preset exception instruction through a preset interceptor.

[0214] Based on the call address of the monitoring code block corresponding to the first virtual address in the preset address mapping relationship, redirect the execution to monitor the code block and output the monitoring information for the hot memory access instruction.

[0215] Run the second preset exception instruction and intercept the second preset exception instruction through the preset interceptor. Based on the second virtual address corresponding to the call address of the monitoring code block in the preset address mapping relationship, return and execute the next instruction of the first preset exception instruction.

[0216] In an optional implementation manner, the running module 120 is specifically configured to obtain the call address corresponding to the monitoring code block corresponding to the first preset exception instruction according to the preset address mapping relationship.

[0217] According to the current thread identifier corresponding to the target application, obtain the monitoring stack corresponding to the current thread, save the stack pointer of the current thread, and make the stack pointer point to the monitoring stack.

[0218] Execute the monitoring code block according to the call address corresponding to the monitoring code block, output the monitoring information for the hot memory access instruction, and mark the end of the exception handling of the first preset exception instruction.

[0219] In an optional implementation manner, the running module 120 is specifically configured to execute the monitoring code block, disassemble and parse the hot memory access instruction, and obtain the memory access code description data structure corresponding to the hot memory access instruction;

[0220] Calculate the memory access address and access mode of the hot memory access instruction according to the memory access code description data structure;

[0221] Use the memory access address and the access mode as parameters to call and execute the monitoring function in the monitoring code block, and run the hot memory access instruction, so that the hot memory access instruction is monitored through the monitoring function, and the monitoring information for the hot memory access instruction is output.

[0222] In an optional implementation manner, the preset interceptor is further configured to include: a preset thread mapping relationship, which can be dynamically constructed when the target application is running, and the preset thread mapping relationship includes: the thread identifier of the current thread corresponding to the target application, the monitoring stack address, and the stack pointer before the current thread executes the hot memory access instruction;

[0223] The running module 120 is specifically configured to execute the second preset exception instruction, and obtain the address of the next instruction of the first virtual address corresponding to the second virtual address indicated by the second preset exception instruction according to the preset address mapping relationship;

[0224] Query the preset thread mapping relationship according to the thread identifier of the current thread corresponding to the target application, and obtain the stack pointer before executing the hot memory access instruction;

[0225] Modify the exception scene according to the stack pointer before executing the hot instruction, so that the original stack corresponding to the current thread is used after the exception handling ends, and execute the next instruction of the hot memory access instruction according to the address of the next instruction of the first virtual address, and mark the end of the exception handling of the second preset exception instruction.

[0226] In an optional implementation manner, the running module 120 is specifically configured to determine whether the current thread has a monitoring stack;

[0227] If the current thread has a monitoring stack, return the address of the monitoring stack;

[0228] If the current thread does not have a monitoring stack, allocate an independent monitoring stack for the current thread through the user mode or the kernel mode.

[0229] In an alternative embodiment, the running module 120 is specifically configured to allocate a monitoring stack for the current thread from a preset stack virtual address segment if the monitoring stack does not exist for the current thread, and the preset stack virtual address segment is pre-registered by a preset interceptor.

[0230] In an alternative embodiment, the obtaining module 110 is specifically configured to sample and obtain initial hot instructions during the running of the target application;

[0231] If it is determined that the initial hot instruction is not a hot memory access instruction, then search for a hot memory access instruction according to a preset rule.

[0232] In an alternative embodiment, the obtaining module 110 is specifically configured to, if it is determined that the initial hot instruction is not a hot memory access instruction, search whether there is a hot memory access instruction among the first N instructions of the initial hot instruction. If so, use the first hot memory access instruction closest to the initial hot instruction as the hot memory access instruction;

[0233] If there is no hot memory access instruction among the first N instructions of the initial hot instruction, then search whether there is a hot memory access instruction among the next M instructions of the initial hot instruction. If so, use the second hot memory access instruction closest to the initial hot instruction as the hot memory access instruction, where N and M are integers greater than 1.

[0234] In an alternative embodiment, the running module 120 is further configured to restore the first preset exception instruction to a hot memory access instruction and unload the preset interceptor.

[0235] In an alternative embodiment, the monitoring information of the hot memory access instruction includes: the memory access address and / or access latency of the hot memory access instruction.

[0236] The above device is used to execute the method provided in the foregoing embodiment, and its implementation principle and technical effects are similar, which will not be elaborated here.

[0237] The above modules may be one or more integrated circuits configured to implement the above methods, such as: one or more Application Specific Integrated Circuits (ASICs), or, one or more microprocessors, or, one or more Field Programmable Gate Arrays (FPGAs), etc. For another example, when a certain above module is implemented in the form of a processing element scheduler code, the processing element may be a general-purpose processor, such as a Central Processing Unit (CPU) or other processors that can call program code. For another example, these modules may be integrated together and implemented in the form of a system-on-a-chip (SOC).

[0238] Figure 12 A schematic structural diagram of an electronic device provided by an embodiment of the present application. The electronic device may be integrated into the above memory access monitoring device. As Figure 12 shown, the electronic device may include: a processor 210, a storage medium 220, and a bus 230. The storage medium 220 stores machine-readable instructions executable by the processor 210. When the electronic device runs, the processor 210 communicates with the storage medium 220 through the bus 230, and the processor 210 executes the machine-readable instructions to execute the steps of the above method embodiment. The specific implementation manners and technical effects are similar and will not be elaborated here.

[0239] Optionally, the present application further provides a storage medium, on which a computer program is stored. When the computer program is run by a processor, it executes the steps of the above method embodiment. The specific implementation manners and technical effects are similar and will not be elaborated here.

[0240] In several embodiments provided by the present application, it should be understood that the disclosed devices and methods may be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of units is only a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Another point is that the displayed or discussed coupling or direct coupling or communication connection between each other may be through some interfaces. The indirect coupling or communication connection of devices or units may be in an electrical, mechanical or other form.

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

[0242] In addition, the functional units in each embodiment of this application can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above integrated units can be implemented in the form of hardware or in the form of hardware plus software functional units.

[0243] The above integrated units implemented in the form of software functional units can be stored in a computer-readable storage medium. The above software functional units stored in a storage medium include several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to execute some steps of the methods in each embodiment of this application. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memories (abbreviated as ROM), random access memories (abbreviated as RAM), magnetic disks, or optical discs that can store program codes.

[0244] It should be noted that in this article, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements not only includes those elements, but also includes other elements not expressly listed, or also includes elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "including one..." does not exclude the existence of another identical element in the process, method, article or device including the element.

[0245] The above are only the preferred embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included within the protection scope of the present application. It should be noted that similar reference numerals and letters indicate similar items in the following drawings. Therefore, once an item is defined in one drawing, it does not need to be further defined and explained in subsequent drawings. The above are only the preferred embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included within the protection scope of the present application.

Claims

1. A memory access monitoring method, characterized in that: The method comprises: Sampling and acquiring hot memory access instructions during the operation of the target application, wherein the hot memory access instructions are generated by using a just-in-time compilation technology; The hotspot memory access instruction is replaced with a first preset exception instruction, and the first preset exception instruction is run, wherein the first preset exception instruction is capable of performing exception detection when running, and redirecting the execution of a monitoring code block to output monitoring information for the hotspot memory access instruction, and the monitoring code block includes the hotspot memory access instruction.

2. The method according to claim 1, characterized in that: The executing the first preset exception instruction includes: Running the first preset exception instruction, if the kernel detects the first preset exception instruction, querying whether the first preset exception instruction is registered in the user mode signal processing program; If registered, the processing is performed according to the processing method recorded in the user mode signal processing program.

3. The method according to claim 1, characterized in that: The executing the first preset exception instruction includes: The first preset abnormal instruction is executed and the first preset abnormal instruction is intercepted by a preset interceptor.

4. The method according to claim 3, characterized in that The first preset exception instruction can indicate a first virtual address corresponding to the hotspot memory access instruction, and the preset interceptor is configured to register a preset address mapping relationship, which includes: a mapping relationship between the first virtual address of the hotspot memory access instruction corresponding to the first preset exception instruction, the call address corresponding to the monitoring code block, and the second virtual address indicated by the second preset exception instruction, wherein the monitoring code block ends with the second preset exception instruction, and the second preset exception instruction is used to return to execute the next instruction of the first preset exception instruction after the monitoring code block is executed; After executing the first preset abnormal instruction and intercepting the first preset abnormal instruction by a preset interceptor, the method further includes: Run the first preset abnormal instruction and intercept the first preset abnormal instruction through a preset interceptor; Based on the calling address of the monitoring code block corresponding to the first virtual address in the preset address mapping relationship, redirect the execution of the monitoring code block and output monitoring information for the hotspot memory access instruction; Run the second preset exception instruction and intercept the second preset exception instruction through the preset interceptor, and return to execute the next instruction of the first preset exception instruction based on the second virtual address corresponding to the calling address of the monitoring code block in the preset address mapping relationship.

5. The method according to claim 4, characterized in that The redirecting execution of the monitoring code block and outputting monitoring information for the hotspot memory access instruction includes: Acquire a call address corresponding to the monitoring code block corresponding to the first preset abnormal instruction according to the preset address mapping relationship; According to the current thread identifier corresponding to the target application, obtain the monitoring stack corresponding to the current thread, save the stack pointer of the current thread, and make the stack pointer point to the monitoring stack; According to the calling address corresponding to the monitoring code block, the monitoring code block is executed to output monitoring information for the hotspot memory access instruction, and the first preset exception instruction exception handling is marked to be completed.

6. The method according to claim 5, characterized in that The executing the monitoring code block and outputting monitoring information for the hotspot memory access instruction includes: Execute the monitoring code block, disassemble and parse the hotspot memory access instruction, and obtain a memory access code description data structure corresponding to the hotspot memory access instruction; According to the memory access code description data structure, the memory access address and access mode of the hot memory access instruction are calculated; With the memory access address and the access mode as parameters, the monitoring function in the monitoring code block is called and executed, and the hot memory access instruction is run, so that the hot memory access instruction is monitored through the monitoring function, and monitoring information for the hot memory access instruction is output.

7. The method according to claim 5, characterized in that The preset interceptor is further configured to include: a preset thread mapping relationship, which can be dynamically constructed when the target application is running, and the preset thread mapping relationship includes: a thread identifier of the current thread corresponding to the target application, a monitoring stack address, and a stack pointer before the current thread executes the hotspot memory access instruction, and the execution of the second preset exception instruction returns to execute the next instruction of the first preset exception instruction, including: Execute the second preset exception instruction, and obtain, according to the preset address mapping relationship, the next instruction address of the first virtual address corresponding to the second virtual address indicated by the second preset exception instruction; According to the thread identifier of the current thread corresponding to the target application, query the preset thread mapping relationship to obtain the stack pointer before executing the hotspot memory access instruction; According to the stack pointer before executing the hot spot instruction, the exception scene is modified so that the original stack corresponding to the current thread is used after the exception handling is completed, and the next instruction of the hot spot memory access instruction is executed according to the next instruction address of the first virtual address, and the exception handling of the second preset exception instruction is marked to be completed.

8. The method according to claim 6, characterized in that The step of saving the stack pointer of the current thread and obtaining the monitoring stack corresponding to the current thread includes: Determine whether the current thread has a monitoring stack; If the current thread has a monitoring stack, the address of the monitoring stack is returned; If the monitoring stack does not exist for the current thread, an independent monitoring stack is allocated for the current thread through user mode or kernel mode.

9. The method according to claim 8, characterized in that If the monitoring stack does not exist for the current thread, allocating an independent monitoring stack for the current thread through the user state includes: If the monitoring stack does not exist for the current thread, a monitoring stack is allocated for the current thread from a preset stack virtual address segment, where the preset stack virtual address segment is pre-registered by the preset interceptor.

10. The method according to claim 1, characterized in that The sampling and obtaining of hot memory access instructions during the operation of the target application includes: Sampling and obtaining the initial hot spot instructions during the operation of the target application; If it is determined that the initial hot spot instruction is not a hot spot memory access instruction, the hot spot memory access instruction is searched according to a preset rule.

11. The method according to claim 10, characterized in that If it is determined that the initial hot spot instruction is not a hot spot memory access instruction, searching for the hot spot memory access instruction according to a preset rule includes: If it is determined that the initial hot spot instruction is not a hot spot memory access instruction, then find out whether there is a hot spot memory access instruction among the first N instructions of the initial hot spot instruction, and if there is, take the first hot spot memory access instruction closest to the initial hot spot instruction as the hot spot memory access instruction; If there is no hot memory access instruction among the first N instructions of the initial hot memory instruction, then check whether there is a hot memory access instruction among the last M instructions of the initial hot memory instruction. If so, the second hot memory access instruction closest to the initial hot memory instruction is used as the hot memory access instruction, wherein N and M are integers greater than 1.

12. The method according to claim 3, characterized in that The step of replacing the hotspot memory access instruction with a first preset exception instruction and executing the first preset exception instruction further includes: The first preset exception instruction is restored to the hotspot memory access instruction, and the preset interceptor is uninstalled.

13. The method according to any one of claims 1 to 12, characterized in that: The monitoring information of the hot memory access instruction includes: the memory access address and / or access delay of the hot memory access instruction.

14. An electronic device, characterized in that: include: A processor, a storage medium and a bus, wherein the storage medium stores machine-readable instructions executable by the processor. When the electronic device is running, the processor and the storage medium communicate via the bus, and the processor executes the machine-readable instructions to perform the steps of the memory access monitoring method as described in any one of claims 1-13.

15. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the memory access monitoring method according to any one of claims 1 to 13 are executed.