A method and electronic device for determining when a task is executed by multiple threads and makes requests.
Patent Information
- Application Number
- CN202110676774.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-06-18
- Publication Date
- 2026-09-01
- Estimated Expiration
- 2041-06-18
AI Technical Summary
[0040]本发明提供了一种任务被多线程执行并发出请求的确定方法,基于hook的方式,进行阶段性的采样,获取每个任务阶段的执行时间和线程ID;
Smart Images

Figure CN115495253B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of thread execution research technology, and in particular to a method and electronic device for determining when a task is executed by multiple threads and issues a request. Background Technology
[0002] On the Android platform, if a network request application is developed using native C++ code, its network request tasks, which involve I / O operations and are time-consuming, will be designed for multi-threaded execution. Since I / O is a time-consuming operation, multi-threading is necessary to ensure program performance, and a thread queue is essential for scheduling and temporarily storing tasks. With a task queue, tasks will wait in line for multiple threads to consume and execute them. Furthermore, on the Android platform, network tasks are typically submitted by the Java layer and executed by the native layer. Therefore, it can execute multiple request tasks across different language environments.
[0003] Therefore, how to confirm that a task can be executed concurrently in multiple threads, rather than being ordered into a single task by a task queue, is an urgent problem to be solved. Summary of the Invention
[0004] This invention provides a method for determining when a task is executed by multiple threads and issues a request. Based on a hook approach, it performs phased sampling to obtain the execution time and thread ID of each task phase.
[0005] It can create multiple threads for each thread, perform concurrent task request statistics, and perform phased time-consuming function statistics, which is beneficial for determining and optimizing the thread execution phase.
[0006] A method for determining when a task is executed by multiple threads and issues a request includes:
[0007] Establish enumeration variables to distinguish and determine the execution stage of the task;
[0008] And establish a data structure to store the task's phase information;
[0009] Determine the task execution phase;
[0010] Based on the hook approach, phased sampling is performed during the execution of each phase layer to obtain call stack information, execution time of each task phase, and thread ID;
[0011] For each task request, multiple threads are created to perform concurrent task request statistics and stage-based time consumption function statistics.
[0012] Preferably, the enumerated variables correspond to the execution stages of the task, including: the task execution stage in the Java layer, the task transfer from the Java layer to the native layer for execution, the task execution stage in the native layer, and the task transfer from the native layer to the Java layer for execution.
[0013] Preferably, the data structure corresponds to the task's stage information, which includes: task stage information, the thread ID of the task in this stage, the execution time of the task, and function call stack information.
[0014] Preferably, based on a hook approach, phased sampling is performed during the execution of each phase layer to obtain call stack information, the execution time of each task phase, and the thread ID. Specifically, this includes:
[0015] Define a structure variable info data;
[0016] Assign the type of the structure variable to the task execution phase in the Java layer;
[0017] Assign the ID of the structure variable to the thread ID currently being executed in the Java layer;
[0018] When the task starts, retrieve the system's current time once;
[0019] By using a hook approach, the current function call stack information is obtained to determine the task execution phase and the corresponding execution time of the task phase.
[0020] Preferably, multiple threads are created for each task request to perform concurrent task request statistics and stage-specific time consumption function statistics, specifically including:
[0021] For each task request, multiple threads are created to execute the corresponding task request;
[0022] Before each task request is executed, a statistics thread is created to collect call stack information and thread ID;
[0023] Sort by the execution time of each stage of the task;
[0024] For task requests, perform concurrent task request statistics and stage time consumption function statistics.
[0025] Preferably, the number of threads created is no greater than the number of CPU cores, and the sorting based on the execution time of each stage of the task is in descending order.
[0026] Preferably, before each task request is executed, a statistics thread is created to collect call stack information and thread IDs, specifically including:
[0027] Sampling is performed based on the execution time of each stage;
[0028] The statistics thread will iterate through all threads in the system and pause the execution of all threads;
[0029] Obtain the function call stacks of all threads at the same time and the execution thread ID of each task, and store them in the stack information.
[0030] Preferably, for task requests, concurrent task request statistics and stage-specific time consumption function statistics are performed, which specifically include:
[0031] First, iterate through each task execution stage;
[0032] If a thread change occurs during task execution, it is determined that there are non-concurrent task requests.
[0033] If no thread changes occur during task execution, then the entire execution of the current task is considered to be a single, independent thread.
[0034] Then, observe the changes in the thread ID;
[0035] Based on the function call stack, determine whether functions are executed sequentially;
[0036] The parallel execution time and serial execution time of a request task are determined based on statistical frequency.
[0037] In a second aspect, embodiments of the present invention provide an electronic device, including an application processor and a memory, wherein the processor is configured to implement a method for determining whether a task is executed by multiple threads and issuing a request when a computer management program stored in the memory is available.
[0038] Thirdly, embodiments of the present invention provide a computer-readable storage medium storing a computer management program thereon, characterized in that, when the computer management program is executed by a processor, it implements a method for determining whether a task is executed by multiple threads and a request is issued.
[0039] Beneficial effects
[0040] This invention provides a method for determining when a task is executed by multiple threads and issues a request. Based on a hook approach, it performs phased sampling to obtain the execution time and thread ID of each task phase.
[0041] It can create multiple threads for each thread, perform concurrent task request statistics, and perform phased time-consuming function statistics, which is beneficial for determining and optimizing the thread execution phase. Attached Figure Description
[0042] Figure 1 This is a flowchart illustrating a method for determining when a task is executed by multiple threads and a request is issued, as described in this invention.
[0043] Figure 2 This is a flowchart illustrating the staged sampling method based on hooks as described in this invention.
[0044] Figure 3 A schematic diagram of the hardware structure of a device for determining a task that is executed by multiple threads and issues a request, provided in an embodiment of the present invention.
[0045] Figure 4 This is a schematic diagram of a possible embodiment of an electronic device provided by an embodiment of the present invention.
[0046] Figure 5 This is a schematic diagram illustrating one possible embodiment of a computer-readable storage medium provided in this invention. Detailed Implementation
[0047] The following specific embodiments illustrate the implementation of the present invention. Those skilled in the art can easily understand other advantages and effects of the present invention from the content disclosed in this specification. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0048] It should be noted that in the description of this invention, terms such as "center," "upper," "lower," "horizontal," and "inner," indicating directional or positional relationships, are based on the directional or positional relationships shown in the accompanying drawings. These are merely for ease of description and do not indicate or imply that the device or element must have a specific orientation, or be constructed and operated in a specific orientation; therefore, they should not be construed as limitations on this invention. Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance.
[0049] Furthermore, it should be noted that, in the description of this invention, unless otherwise explicitly specified and limited, the terms "set," "install," "connect," and "link" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; and they can refer to the internal communication between two components. Those skilled in the art can understand the specific meaning of the above terms in this invention according to the specific circumstances.
[0050] like Figure 1 As shown, based on the technical problem raised in the background art, the present invention provides a method for determining when a task is executed by multiple threads and issues a request, including:
[0051] Step S110: Establish enumeration variables to distinguish and determine the execution stage of the task; specifically, this can be implemented by the following code:
[0052] Define an enumeration variable EnumType;
[0053] The enumerated variables correspond to the execution stages of the task, including: Java_type = 0, Java_to_native_type, Native_type, and Native_to_java_type;
[0054] Among them, Java_type=0 indicates that the task is executed in the Java layer; Java_to_native_type indicates that the task is transferred from the Java layer to the native layer for execution; Native_type indicates that the task is executed in the native layer; and Native_to_java_type indicates that the task is transferred from the native layer to the Java layer for execution.
[0055] It is important to note that, for the sake of subsequent statistics and analysis, it is necessary to distinguish between the various stages of task execution. This allows us to easily obtain the execution status of each stage of the task, thereby enabling us to understand the thread execution status of the task through the acquired data. At the same time, based on the time, it is also convenient to statistically analyze whether there are multiple threads running concurrently in subsequent tasks.
[0056] Step S120: Establish a data structure to store the task's stage information. Specifically, this can be implemented using the following code:
[0057] Define the data structure Structinfo;
[0058] The data structure includes stage information for the corresponding task, specifically: Type etype, ThreadID id, Timetime, and List. <stackfunction>stack;
[0059] Among them, Type (etype) stores the stage information of the task, ThreadID (id) stores the thread ID of the thread executing in this stage of the task, Time (time) stores the execution time of the task, and List... <stackfunction>The stack stores function call stack information;
[0060] It is worth noting that a data structure was designed to facilitate the storage of information at each stage, which enabled the comparison and analysis that would be beneficial after the threads were created.
[0061] Step S130: Determine the task execution phase;
[0062] It is important to note that a task involves multiple execution phases. Requests submitted from the Java layer are executed in native C++. A request task may have some functionality executed in the Java layer and some in the native C++. After the native execution is complete, the result is sent back to the Java layer. Intermediate results of the task may also be sent to the Java layer via callbacks. Therefore, a task may span multiple threads. By determining the execution phases of a task, the beneficial effect of distinguishing time consumption is achieved.
[0063] like Figure 2 As shown, step S140 involves sampling at each stage of execution using a hook-based approach to obtain call stack information, the execution time of each task stage, and the thread ID. Specifically, this includes the following steps:
[0064] Step S141: Define a structure variable info data;
[0065] Step S142: Assign the value of data.etype to the Java_type stage;
[0066] Assign the value of data.id to the thread ID currently executing in the Java layer;
[0067] Step S143: When the task starts, obtain the current system time once;
[0068] Step S144: Based on a hook approach, obtain the current function call stack information to determine the task execution stage and the corresponding execution time of the task stage; for each task stage, the specific determination process is as follows:
[0069] 1) Java_type stage.
[0070] Since all request tasks are initiated from the Java layer, it is relatively easy to determine the stage at the Java layer, which is the task initiation stage.
[0071] 2) Java_to_native_type stage definition.
[0072] First, to deliver code to the native layer for execution, some functional interfaces are written in the native layer and provided to the Java layer for execution. Therefore, if a native method is defined in the Java layer, it is called from the native layer. All functions registered in the Java layer that correspond to native functions can be hooked.
[0073] By using hooks, the call stack of the Java layer can be obtained, and it can also be determined which native function this function corresponds to. Furthermore, the start time of this function call is the end time of the Java layer call phase. The execution time of the Java_type phase can be obtained by subtracting the start time of the Java layer task from the data.times, which is the time in the Java_type phase.
[0074] The call stack information for this function can be obtained through the Java layer.
[0075] 3) Native_type stage.
[0076] After the Java_to_native_type phase is executed, execution switches from the Java layer to native code. The functions executed in the Java layer will then be executed in the native code. Therefore, it is possible to hook the native functions in C++ to obtain the start time of the native function's execution. This time is also the end time of the Java_to_native_type phase. Thus, the time of the Java_to_native_type phase can be calculated by subtracting the hook time of the Java_to_native_type phase from the current time.
[0077] During the Native_type phase, record the start time of the Native_type phase.
[0078] When executing in native code, the Java layer interface is eventually called to push data to the Java layer. Therefore, a hook can be made when the native code calls the Java layer, which can modify the timing of the native code call to the Java layer. By obtaining this timing, the total execution time in the native layer can be known.
[0079] 4) Native_to_java_type stage.
[0080] During native execution, after the current task is completed, the result is pushed back to the Java layer. Therefore, reflection is used to call the Java layer's interface to inform the Java layer of the result. By hooking the interface provided by the Java layer to the native layer, the timing of the native layer pushing the result data can be obtained. Furthermore, the execution time of the native layer transitioning to the Java layer can be obtained when the native call ends and when the native layer finally calls the Java layer. At this stage, the native layer delivers the request result to the Java layer, thus completing the entire request process.
[0081] It should be noted that this step uses a hook-based approach to obtain the time spent at each stage, which is more accurate than obtaining the time through function calls without hooks. This achieves the beneficial effect of more accurately calculating the complete time spent at each stage.
[0082] Step S150: Based on each task request, create multiple threads to perform concurrent task request statistics and stage-specific time consumption function statistics. Specifically:
[0083] There will be a UI rendering thread, which is the main thread, in the entire program. This thread is mainly used for interface rendering, so it cannot perform any time-consuming operations. Instead, a new thread will be created for subsequent task statistics and data acquisition functions.
[0084] For each task request, a new thread is created to execute it. If the design prioritizes task concurrency, the number of concurrent threads cannot exceed the number of CPU cores. Therefore, if the number of cores is 10, the number of concurrent tasks cannot exceed 8. That is, if the design uses 4 threads, the concurrency of 4 request tasks can be tracked, specifically including:
[0085] 1. Design a sampling method for each stage layer.
[0086] Once the various stages of the task request have been designed, the total time spent in each stage can be determined by the execution time of each stage. However, for this paper, it is necessary to know whether there is concurrent execution at each stage level. Therefore, it is necessary to perform periodic sampling during the execution of each stage level to obtain its call stack information and thread ID.
[0087] Specifically, for any given task, one thread is designed to collect statistics, while the other four threads are used to execute the requested task.
[0088] During sampling, the method is as follows: When the task is executed to each stage, the statistics thread will sample based on the execution time of each stage. The statistics thread will traverse all threads in the system and pause the execution of all threads. Only when the execution of all threads is paused can the execution function call stack of all threads at the same time be accurately obtained. After obtaining the stack, it is stored in the stack information. At the same time, the execution thread ID of each task needs to be obtained and stored as well.
[0089] The tasks are sorted by execution time for each stage, with the most time-consuming stages appearing first. In a preferred embodiment, the frequency of use is also based on time, with stages that take longer being used more frequently.
[0090] In another embodiment, the following comparison method is used during the statistical phase:
[0091] Horizontal Comparison: First, a horizontal comparison is performed on the task requests, that is, each task is examined at each stage from start to finish to observe the changes in its threads. If a task is executed by a single thread from start to finish, it means that this task is executed entirely by a single thread, and other tasks are also executed by different threads, indicating that the entire execution of the current task is an independent thread, and the task achieves maximum parallel execution. However, if the thread changes during task execution, it means that there is thread switching, which may result in tasks being executed sequentially, rather than concurrently.
[0092] Vertical comparison: For multiple tasks, we can examine the concurrency situation at each stage, checking whether the thread IDs are different during task execution. If the task is a multi-threaded parallel task, we can see that each task executes on a different thread. If multiple tasks share the same thread ID, it indicates that tasks are being queued and executed within a single thread at this stage. We can determine how much of a request task is executed in parallel and how much in serial execution based on statistical frequency. Furthermore, by analyzing the function call stack, we can identify which functions are executed serially. Therefore, using the stack makes it easy to find these functions and optimize for parallel execution.
[0093] By separately calculating the time for each stage, we can identify which stage the total time of the task is spent in, since some stages need to be executed sequentially during task design, such as the task's thread queue or the task delivery stage. Furthermore, by calculating the time for each stage, we can determine which stage the total time of the task is spent in, and we can conveniently perform time-consuming function statistics for each stage based on the call stack of each stage.
[0094] It should be noted that, due to the different languages used at each stage, it is inconvenient to statistically analyze the native and Java stack traces. Therefore, the statistics are performed separately for each stage, which achieves the beneficial effect of facilitating statistical analysis in stages.
[0095] In another embodiment, specific data effect examples are provided:
[0096] For example, if four threads are executing four request tasks, we would like to check the data to see if there is a large amount of concurrent time involved.
[0097] ThreadID id; Stores the thread ID of the task during this phase.
[0098] Time; the time taken to execute a storage task.
[0099] List <stackfunction>stack;
[0100] The above are multiple data samples that are stored at each stage.
[0101] For example, the task is currently being executed at the native_type stage.
[0102] Sampling records the thread ID of each request at each sampled data point. Ideally, for four concurrent requests, each task should execute on a different thread ID, and this should be maintained across all sampled data. However, if any of the four tasks share the same thread ID, the time consumed by that thread ID needs to be examined. This indicates a possible situation where the four tasks weren't executing concurrently but were in the same queue or waiting together. In this case, the function call stack can be examined to determine which section of code the task was queued for execution, indicating sequential rather than concurrent execution.
[0103] By adjusting the code and continuously conducting the aforementioned experiments to collect and analyze data, the goal was to achieve concurrent execution of all four request tasks in four different threads, thus maximizing performance.
[0104] This invention provides a method for determining when a task is executed by multiple threads and issues a request. Based on a hook approach, it performs phased sampling to obtain the execution time and thread ID of each task phase; and creates multiple threads accordingly. This enables the statistics of concurrent task requests and the statistics of time consumption functions in different phases, which is beneficial for determining the thread execution phase and optimizing performance to the maximum extent.
[0105] The following describes in detail, from a hardware processing perspective, the device for determining the task that is executed by multiple threads and issues a request in the embodiments of this application. Figure 3 A schematic diagram of the hardware structure of the device for determining whether a task is executed by multiple threads and issuing a request, provided in an embodiment of the present invention; please refer to... Figure 3 A block diagram of an embodiment of the device for determining whether a task is executed by multiple threads and issuing a request, as described in this invention.
[0106] As one embodiment, it includes:
[0107] Input device 301, output device 302, processor 303, and memory 304 (where the number of processors 303 can be one or more). Figure 3 (Taking a processor 303 as an example). In some embodiments of the present invention, the input device 301, the output device 302, the processor 303, and the memory 304 may be connected via a bus or other means, wherein... Figure 3 Taking the example of a connection between China and Israel via a bus.
[0108] Specifically, by calling the operation instructions stored in memory 304, processor 303 executes the following steps:
[0109] Establish enumeration variables to distinguish and determine the execution stage of the task;
[0110] And establish a data structure to store the task's phase information;
[0111] Determine the task execution phase;
[0112] Based on the hook approach, phased sampling is performed during the execution of each phase layer to obtain call stack information, execution time of each task phase, and thread ID;
[0113] For each task request, multiple threads are created to perform concurrent task request statistics and stage-based time consumption function statistics.
[0114] By calling the operation instructions stored in memory 304, processor 303 is also used to execute... Figure 1 Any of the methods in the corresponding embodiments.
[0115] Figure 4 A schematic diagram of a possible embodiment of an electronic device provided by an embodiment of the present invention is shown in Figure 4.
[0116] In another embodiment, the present invention also provides an electronic device including a memory 410, a processor 420, and a computer program 411 stored in the memory 420 and executable on the processor 420, wherein the processor 420 executes the computer program 411 to perform the following steps.
[0117] Establish enumeration variables to distinguish and determine the execution stage of the task;
[0118] And establish a data structure to store the task's phase information;
[0119] Determine the task execution phase;
[0120] Based on the hook approach, phased sampling is performed during the execution of each phase layer to obtain call stack information, execution time of each task phase, and thread ID;
[0121] For each task request, multiple threads are created to perform concurrent task request statistics and stage-based time consumption function statistics.
[0122] Since the electronic device described in this embodiment is for implementing the present invention, those skilled in the art can understand the specific implementation method and various variations of the electronic device based on the method described in the present invention. Therefore, how the electronic device implements the method in the present invention will not be described in detail here. Any device used by those skilled in the art to implement the method in the present invention is within the scope of protection of this application.
[0123] Figure 5 Please refer to the schematic diagram of a possible embodiment of a computer-readable storage medium provided in this invention. Figure 5 .
[0124] like Figure 5 As shown, this embodiment provides a computer-readable storage medium 500 on which a computer program 511 is stored. When the computer program 511 is executed by a processor, it performs the following steps:
[0125] Establish enumeration variables to distinguish and determine the execution stage of the task;
[0126] And establish a data structure to store the task's phase information;
[0127] Determine the task execution phase;
[0128] Based on the hook approach, phased sampling is performed during the execution of each phase layer to obtain call stack information, execution time of each task phase, and thread ID;
[0129] For each task request, multiple threads are created to perform concurrent task request statistics and stage-based time consumption function statistics.
[0130] In practical implementation, when the computer program 511 is executed by the processor, it can achieve the following: Figure 1 Any of the corresponding implementation methods in the embodiments.
[0131] It should be noted that the descriptions of each embodiment in the above embodiments have different focuses. For parts that are not described in detail in a certain embodiment, please refer to the relevant descriptions in other embodiments.
[0132] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0133] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded computer, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create a machine for implementing the flowchart illustrations. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0134] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0135] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0136] This invention also provides a computer program product comprising computer software instructions that, when executed on a processing device, cause the processing device to perform actions such as... Figure 1 The flow of the method for preventing system function calls in the corresponding embodiment.
[0137] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions may be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a server or data center that integrates one or more available media. The available medium may be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid-state disk (SSD)).
[0138] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0139] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and 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. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection between apparatuses or units through some interfaces, and may be electrical, mechanical, or other forms.
[0140] The units described as separate components may or may not be physically separate. 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 to achieve the purpose of this embodiment according to actual needs.
[0141] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0142] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0143] The technical solution of the present invention has been described above with reference to the preferred embodiments shown in the accompanying drawings. However, it will be readily understood by those skilled in the art that the scope of protection of the present invention is obviously not limited to these specific embodiments. Without departing from the principles of the present invention, those skilled in the art can make equivalent changes or substitutions to the relevant technical features, and the technical solutions after these changes or substitutions will all fall within the scope of protection of the present invention.< / stackfunction> < / stackfunction> < / stackfunction>
Claims
1. A method for determining when a task is executed by multiple threads and issues a request, characterized in that, include: Establish enumeration variables to distinguish and determine the execution stage of the task; And establish a data structure to store the task's phase information; Determine the task execution phase; Based on the hook approach, phased sampling is performed during the execution of each phase layer to obtain call stack information, execution time of each task phase, and thread ID; For each task request, multiple threads are created to perform concurrent task request statistics and stage time consumption function statistics. The enumerated variables correspond to the execution phases of the task, including: the task execution phase in the Java layer, the task transfer from the Java layer to the native layer for execution, the task execution phase in the native layer, and the task transfer from the native layer to the Java layer for execution. The data structure corresponds to the task's stage information, which includes: the task's stage information, the thread ID executing the task in this stage, the execution time of the task, and the function call stack information. Based on a hook-based approach, phased sampling is performed during the execution of each phase layer to obtain call stack information, the execution time of each task phase, and the thread ID. Specifically, this includes: Define a structure variable info data; Assign the type of the structure variable to the task execution phase in the Java layer; Assign the ID of the structure variable to the thread ID currently being executed in the Java layer; When the task starts, retrieve the system's current time once; By using a hook approach, the current function call stack information is obtained to determine the task execution phase and the corresponding execution time of the task phase.
2. The method for determining whether a task is executed by multiple threads and issues a request, as described in claim 1, is characterized in that... For each task request, multiple threads are created to perform concurrent task request statistics and stage-specific time consumption function statistics, which specifically include: For each task request, multiple threads are created to execute the corresponding task request; Before each task request is executed, a statistics thread is created to collect call stack information and thread ID; Sort by the execution time of each stage of the task; For task requests, perform concurrent task request statistics and stage time consumption function statistics.
3. The method for determining whether a task is executed by multiple threads and issues a request, as described in claim 2, is characterized in that... The number of threads created shall not exceed the number of CPU cores, and the sorting based on the execution time of each stage of the task shall be in descending order.
4. The method for determining whether a task is executed by multiple threads and issues a request, as described in claim 3, is characterized in that... Before each task request is executed, a statistics thread is created to collect call stack information and thread IDs, which specifically include: Sampling is performed based on the execution time of each stage; The statistics thread will iterate through all threads in the system and pause the execution of all threads; Obtain the function call stacks of all threads at the same time and the execution thread ID of each task, and store them in the stack information.
5. The method for determining whether a task is executed by multiple threads and issues a request, as described in claim 4, is characterized in that... For task requests, concurrent task request statistics and stage-specific time consumption function statistics are performed, specifically including: First, iterate through each task execution stage; If a thread change occurs during task execution, it is determined that there are non-concurrent task requests. If no thread changes occur during task execution, then the entire execution of the current task is considered to be a single, independent thread. Then, observe the changes in the thread ID; Based on the function call stack, determine whether functions are executed sequentially; The parallel execution time and serial execution time of a request task are determined based on statistical frequency.
6. An electronic device comprising an application processor and a memory, characterized in that, The processor is used to implement the method for determining whether a task as described in any one of claims 1-5 is executed by multiple threads and a request is issued when the computer management program stored in the memory is implemented.
7. A computer-readable storage medium storing computer management programs thereon, characterized in that, When the computer management program is executed by the processor, it implements the method for determining whether a task is executed by multiple threads and a request is issued, as described in any one of claims 1-5.
Citation Information
Patent Citations
Application program analysis method and device
CN107291586A