Android application ANR monitoring method, device, equipment and medium
By listening to main thread messages in the Android system, building a cache queue and sampling the stack, the problem of stack errors in the ANR mechanism is solved, and the efficiency and accuracy of ANR events are improved.
Patent Information
- Application Number
- CN202411116974.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-15
- Publication Date
- 2024-11-05
- Estimated Expiration
- 2044-08-15
AI Technical Summary
When the existing Android ANR mechanism is troubleshooting, the crawled stack may be invalid or incorrect, causing troubleshooting and misleading developers.
By listening to the messages of the main thread, using the child thread to detect execution time, build a cache queue, sample the stack when the timeout, collect data when the ANR event occurs, package and upload it to the analysis end, and perform analysis of the key stack.
Accurate key stack acquisition is achieved, improving the efficiency and accuracy of ANR events, and reducing the error rate.
Smart Images

Figure CN118626344B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of ANR monitoring, and specifically relates to an Android application ANR monitoring method, device, equipment and medium. Background Art
[0002] ANR: Application Not Responding, which means that the key actions in the main thread cannot be responded to for a long time. It is a fallback measure designed by the Android system. For example, if a broadcast is sent and not processed for more than 10 seconds, the system ANR mechanism will be triggered.
[0003] Currently, solving Android ANR problems is too dependent on system environment and special issues, making it difficult to solve most problems.
[0004] The root cause lies in the design of the ANR mechanism of the Android system and the stack drift caused by the lag of the dump mechanism, which makes it difficult to obtain key time-consuming messages or even erroneous messages.
[0005] Since the main thread can only process one message at a time, and the system only monitors the start execution time and completion time of key messages, if the execution is not completed within 10 seconds, the system considers that an ANR has occurred and captures the stack information for backtracking. However, when the ANR mechanism is troubleshooting, the captured stack may be invalid or wrong, which makes it difficult for the ANR mechanism to troubleshoot problems, and the wrong stack may even mislead developers. Summary of the invention
[0006] The purpose of the present invention is to provide an Android application ANR monitoring method, device, equipment and storage medium to solve the problem that when the existing ANR mechanism is troubleshooting, the captured stack may be invalid or erroneous, resulting in great difficulty in troubleshooting the ANR mechanism, and the erroneous stack may even mislead the developer.
[0007] In order to achieve the above object, the present invention adopts the following technical solutions:
[0008] In a first aspect, the present invention provides an Android application ANR monitoring method, the method comprising:
[0009] Monitor the messages of the main thread, use the child thread to detect the execution time of each message executed by the main thread, and build the cache queue of the main thread based on the messages executed by the main thread and the corresponding execution time;
[0010] The child thread determines whether the execution time taken by the main thread to execute any message exceeds a preset time. If so, the main thread triggers an execution timeout, and the child thread samples the stack of the main thread to obtain at least one time-consuming stack;
[0011] Listen to ANR events. When ANR occurs, the child thread collects the messages to be distributed and ANR messages of the main thread, samples the stack of the main thread, obtains the ANR stack, and adds the messages to be distributed and ANR messages to the cache queue;
[0012] The cache queue, ANR events, ANR stack and at least one time-consuming stack are packaged to obtain a data upload package, and the data upload package is uploaded to the analysis end so that the analysis end analyzes the data upload package to obtain a key stack.
[0013] Preferably, the method further comprises:
[0014] Classify the messages to obtain message categories, where the message categories include at least: ANR messages, freeze messages, important messages, frame drop messages, general messages, and messages to be distributed;
[0015] Determine the priority of each message based on the message category, the importance of each message, or the execution time of each message.
[0016] Preferably, the method also includes: based on a preset critical time limit value, aggregating and splitting the messages in the cache queue to obtain time-consuming messages and ordinary messages, wherein the time-consuming messages are used to characterize that the execution time required to execute the time-consuming messages exceeds the preset critical time limit value, and the ordinary messages are used to characterize that the execution time required to execute the ordinary messages does not exceed the preset critical time limit value.
[0017] Preferably, the method further comprises: based on a preset cleaning rule, cleaning up the messages in the cache queue;
[0018] The preset cleaning rules include at least:
[0019] When the capacity of the messages cached in the cache queue reaches a preset capacity limit, clear the lowest priority message; or
[0020] When the number of messages cached in the cache queue reaches a preset limit, the excess messages are cleared; or
[0021] When the cache time of any message in the cache queue reaches a first preset cache time limit and the priority of the message is not the highest level or the highest level, clear the message; or
[0022] When the cache time of any message in the cache queue reaches a second preset cache time limit and the priority of the message is not the highest level, clear the message; or
[0023] When the cache time of any message in the cache queue reaches a third preset cache time limit, the message is cleared;
[0024] The third preset cache time limit is greater than the second preset cache time limit, and the second preset cache time limit is greater than the first preset cache time limit.
[0025] Preferably, the method further comprises:
[0026] Build a message buffer pool, recycle the cleared messages in the cache queue into the message buffer pool, and use the recycled messages as reused messages.
[0027] Preferably, the main thread triggers execution timeout, and the child thread samples the stack of the main thread to obtain at least one time-consuming stack, including:
[0028] Create a delay detection task in the child thread, the delay detection task is used to detect whether the main thread has completed the execution of the message when the execution time taken by the main thread to execute any message reaches a preset time length;
[0029] When the main thread fails to complete the execution of the message, a dump task is generated;
[0030] The child thread executes a dump task to capture the time-consuming stack;
[0031] The preset duration of the delay detection task is reduced by the preset duration decrement value. When the execution time of the main thread continuing to execute the message reaches the reduced preset duration, it is detected again whether the main thread has completed the execution of the message. If the main thread has not completed the execution of the message, the dump task is generated again to enable the child thread to capture the time-consuming stack. When the main thread completes the execution of the message, the preset duration is initialized and waits for the next message to be executed by the main thread.
[0032] Preferably, the analysis end analyzes the data upload package to obtain a key stack, including:
[0033] Aggregate the ANR stack and the time-consuming stack corresponding to each ANR event in the data upload package according to a preset aggregation strategy to obtain at least one key stack corresponding to each ANR event;
[0034] The number of key stacks corresponding to each ANR event is counted, and the ANR events are sorted according to the number of key stacks to obtain the resolution priority of the ANR event.
[0035] In a second aspect, the present invention provides an Android application ANR monitoring device, which is used to implement the above-mentioned Android application ANR monitoring method, and the device includes:
[0036] A first monitoring module is used to monitor the messages of the main thread and obtain a cache queue of the main thread, wherein the cache queue caches a number of messages executed by the main thread;
[0037] The stack sampling module is used by the child thread to determine whether the execution time taken by the main thread to execute any message exceeds the preset time. If so, the main thread triggers the execution timeout, and the child thread samples the stack of the main thread to obtain at least one time-consuming stack;
[0038] The second monitoring module is used to monitor ANR events. When ANR occurs, the child thread detects the execution time of each message in the main thread execution cache queue;
[0039] The data upload module is used to package the cache queue, ANR events, ANR stack and at least one time-consuming stack to obtain a data upload package, and upload the data upload package to the analysis end so that the analysis end analyzes the data upload package to obtain a key stack.
[0040] In a third aspect, the present invention provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above-mentioned Android application ANR monitoring method when executing the computer program.
[0041] In a fourth aspect, the present invention provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the above-mentioned Android application ANR monitoring method.
[0042] Beneficial effects:
[0043] The present invention uses a cache queue to cache in real time a number of messages executed by the main thread and the execution time of each message, and at the same time performs a timeout judgment on the execution time of each message. When the execution time of any message exceeds a preset time, stack sampling is performed to obtain at least one time-consuming stack. When ANR occurs, the ANR stack at this time is collected, and the cache queue, ANR event, ANR stack and at least one time-consuming stack are packaged into a data upload package and uploaded to the analysis end. Finally, the analysis end is used to analyze the data upload package to obtain an accurate key stack. The key stack is conducive to troubleshooting ANR events and improves the efficiency and accuracy of troubleshooting. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] The accompanying drawings are used to provide a further understanding of the embodiments of the present invention and constitute a part of the specification. Together with the following specific implementations, they are used to explain the embodiments of the present invention, but do not constitute a limitation on the embodiments of the present invention. In the accompanying drawings:
[0045] Figure 1It is a flow chart of an Android application ANR monitoring method provided by an embodiment of the present invention;
[0046] Figure 2 It is a schematic diagram of the architecture of a cache queue and a message buffer pool provided in one embodiment of the present invention;
[0047] Figure 3 It is a block diagram of an Android application ANR monitoring device provided by one embodiment of the present invention. DETAILED DESCRIPTION
[0048] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the present invention will be briefly introduced below in combination with the drawings and the description of the embodiments or the prior art. Obviously, the following description of the structure of the drawings is only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work. It should be noted that the description of these embodiments is used to help understand the present invention, but does not constitute a limitation of the present invention.
[0049] Embodiment 1
[0050] Figure 1 is a flow chart of an Android application ANR monitoring method provided by an embodiment of the present invention, such as Figure 1 As shown, this embodiment provides an Android application ANR monitoring method, the method comprising:
[0051] Step S10: monitor the messages of the main thread, use the child thread to detect the execution time of each message executed by the main thread, and build a cache queue of the main thread based on the messages executed by the main thread and the corresponding execution time;
[0052] Step S20: The child thread determines whether the execution time taken by the main thread to execute any message exceeds a preset time. If so, the main thread triggers an execution timeout, and the child thread samples the stack of the main thread to obtain at least one time-consuming stack.
[0053] Step S30: monitoring ANR events. When ANR occurs, the child thread collects the to-be-dispatched messages and ANR messages of the main thread, samples the stack of the main thread, obtains the ANR stack, and adds the to-be-dispatched messages and ANR messages to the cache queue;
[0054] Step S40: Packing the cache queue, ANR events, ANR stack and at least one time-consuming stack to obtain a data upload package, and uploading the data upload package to the analysis end so that the analysis end analyzes the data upload package to obtain a key stack.
[0055] The present invention uses a cache queue to cache in real time a number of messages executed by the main thread and the execution time of each message, and at the same time performs a timeout judgment on the execution time of each message. When the execution time of any message exceeds a preset time, stack sampling is performed to obtain at least one time-consuming stack. When ANR occurs, the ANR stack at this time is collected, and the cache queue, ANR event, ANR stack and at least one time-consuming stack are packaged into a data upload package and uploaded to the analysis end. Finally, the analysis end is used to analyze the data upload package to obtain an accurate key stack. The key stack is conducive to troubleshooting ANR events and improves the efficiency and accuracy of troubleshooting.
[0056] In this embodiment, when an App (Application) has an ANR, the system will send a SIGQUIT signal to the App to notify the dump process information. The present invention packages and reports the collected messages and undistributed messages, stacks, device information, process-related data, etc. by monitoring the SIGQUIT signal.
[0057] As a further optimization of this embodiment, after monitoring the message of the main thread, the message of the main thread needs to be classified; specifically, the method further includes:
[0058] Classify the messages to obtain message categories, which include at least: ANR messages, stuck messages, important messages, frame drop messages, general messages and messages to be distributed; determine the priority of each message according to the message category, the importance of each message or the execution time of each message.
[0059] In this embodiment, each message is divided into five priority levels according to its importance, including: highest priority, high priority, medium priority, low priority and lowest priority.
[0060] For example: 1. Messages such as ANR messages, pending messages, and stuck messages are classified as the highest priority. For other messages, if the execution time of such messages exceeds 5s, such messages are also classified as the highest priority;
[0061] 2. For messages other than ANR messages, pending messages, and stuck messages, if the execution time of such messages is between 500ms and 5s, such messages are classified as high priority;
[0062] 3. For messages other than ANR messages, pending messages, and stuck messages, if the execution time of such messages is between 300ms and 500ms, such messages are classified as medium priority; at the same time, frame drop messages (drawing messages exceeding 16ms) and important messages other than the above cases are classified as medium priority;
[0063] 4. General messages other than those in 1-3 above are classified as low priority;
[0064] 5. All messages other than those classified in 1-4 above are classified as the lowest priority.
[0065] In this embodiment, the execution time of the main thread when executing most messages is not long, and may not even exceed 1ms. Such messages are non-critical messages, and there is no analytical value in collecting such messages, so the messages need to be aggregated and split.
[0066] Specifically, the method also includes: based on a preset critical time limit value, aggregating and splitting the messages in the cache queue to obtain time-consuming messages and ordinary messages, wherein the time-consuming messages are used to characterize that the execution time required to execute the time-consuming messages exceeds the preset critical time limit value, and the ordinary messages are used to characterize that the execution time required to execute the ordinary messages does not exceed the preset critical time limit value.
[0067] In this embodiment, for example, 300ms is used as the preset critical time limit. When the execution time of a message exceeds 300ms, the message is a time-consuming message. If the execution time of a message is less than 300ms, the message is regarded as a normal message. Messages less than 300ms need to be aggregated.
[0068] For example, there are three messages in the cache queue, and the execution time of the three messages is 100ms, 20ms, and 80ms. After aggregation, one message is obtained (as a normal message), and the execution time of the aggregated message is 200ms.
[0069] In this embodiment, when multiple messages are aggregated, the total execution time is less than 300ms. When the latest message is added, the total execution time is greater than 300ms. At this time, the aggregated messages need to be split, and the multiple messages less than 300ms are treated as a single event, and the latest message is also treated as a single event.
[0070] For example, if the execution times of the four messages in the cache queue are 100ms, 20ms, 80ms, and 500ms respectively, then one common message and one time-consuming message are obtained after aggregation, and the execution times of the two aggregated messages are 200ms and 500ms respectively.
[0071] In extreme cases, there are 10ms, 400ms, 10ms, 400ms, 10ms, 400ms… In order to avoid fluctuations, some buffer is left and the accumulated messages below 16ms (one frame) are still regarded as an aggregation.
[0072] For example, the execution times of the four messages in the cache queue are 10ms, 300ms, 16ms, and 500ms, respectively. After aggregation, two common messages and one time-consuming message are obtained. The execution times of the three messages after aggregation are 310ms, 16ms, and 500ms, respectively. Since the execution time of the first message before aggregation is 10ms, which is less than the time of one frame, it is still aggregated.
[0073] As a further optimization of this embodiment, both the monitored main thread messages and the dumped stack data are continuously collected by the main thread. When the collected data is too long, the analysis value of these data is limited, which will lead to a waste of memory resources of the cache queue; therefore, the cache queue needs to be cleaned up in real time.
[0074] Specifically, the method further includes: based on a preset cleaning rule, cleaning the messages in the cache queue;
[0075] The preset cleaning rules include at least:
[0076] When the capacity of the messages cached in the cache queue reaches a preset capacity limit, clear the lowest priority message; or
[0077] When the number of messages cached in the cache queue reaches a preset limit, the excess messages are cleared; or
[0078] When the cache time of any message in the cache queue reaches a first preset cache time limit and the priority of the message is not the highest level or the highest level, clear the message; or
[0079] When the cache time of any message in the cache queue reaches a second preset cache time limit and the priority of the message is not the highest level, clear the message; or
[0080] When the cache time of any message in the cache queue reaches a third preset cache time limit, the message is cleared;
[0081] The third preset cache time limit is greater than the second preset cache time limit, and the second preset cache time limit is greater than the first preset cache time limit.
[0082] In this embodiment, at the current moment, detailed messages of the last 30 seconds are collected. If a message exceeds 30 seconds, all messages are collected regardless of the length. Among them, the preset capacity limit is preferably 90% of the maximum cache capacity in the cache queue, the preset quantity limit is preferably 6000 messages, the first preset cache time limit is preferably 30 seconds, the second preset cache time limit is preferably 3 minutes, and the third preset cache time limit is preferably 5 minutes.
[0083] During the cleaning process, since data sources are continuously generated and expired data is continuously discarded, the generation and discarding process will cause memory jitter. In order to avoid memory jitter, as a further optimization of this embodiment, the method further includes:
[0084] Build a message buffer pool, recycle the cleared messages in the cache queue into the message buffer pool, and use the recycled messages as reused messages.
[0085] In this embodiment, if Figure 2 As shown in the figure, when the expired data in the cache queue is discarded, it is not destroyed but stored in the message buffer pool. The maximum storage capacity of the message buffer pool is 50 pieces of data. When there is a message that needs to be cached, it is taken from the message buffer pool first and is not created again. This can greatly avoid the creation and destruction of messages in a short period of time, and at the same time has a certain buffering effect, and will not affect the number of collected messages. This method not only retains the ability to dynamically expand the capacity of messages, but also avoids the problem of memory jitter in a short period of time.
[0086] In this embodiment, the execution time of each message is obtained according to step S10, and the business logic is still like a black box. Without keywords, it is difficult to troubleshoot the problem. Therefore, stack sampling is also required.
[0087] Specifically, the main thread triggers execution timeout, and the child thread samples the stack of the main thread to obtain at least one time-consuming stack, including:
[0088] Step a01: creating a delay detection task in the child thread, wherein the delay detection task is used to detect whether the main thread has completed the execution of any message when the execution time taken by the main thread to execute the message reaches a preset time length;
[0089] Step a02: When the main thread fails to complete the message, a dump task is generated;
[0090] Step a03: the child thread executes a dump task to capture the time-consuming stack;
[0091] Step a04: Reduce the preset duration of the delay detection task by a preset duration decrement value. When the execution time of the main thread continuing to execute the message reaches the reduced preset duration, check again whether the main thread has completed the execution of the message. If the main thread has not completed the execution of the message, generate a dump task again to enable the child thread to capture the time-consuming stack. When the main thread has completed the execution of the message, initialize the preset duration and wait for the next message to be executed by the main thread.
[0092] In this embodiment, when the stack of the main thread is captured (dumped), there is a certain amount of overhead, so it cannot be dumped all the time. It will only be dumped when the execution of a message takes more than 500ms and is still not completed. When a message takes longer and longer to execute, the possibility of causing ANR will also increase, so the dump time (preset duration) will also decrease gradually.
[0093] For example, when it is detected that the main thread message has not been completed after 500ms, the preset duration needs to be reduced by -25 to 50ms (500, 475, 450, 425, ... 100, 75, 50, 50, 50 ...), that is, it will be stable at 50ms after 5.175s, because the minimum ANR duration is 5s, which can be at a lower frequency when ANR occurs. At the same time, the delay rate of each call is calculated. When it exceeds 50%, the detection cycle cannot be less than 200ms.
[0094] In this embodiment, when a message starts to be executed, the execution time is recorded, and a 500ms delay detection task is started in the child thread. When the delay task is executed, it is checked whether the message has been processed. If not, a dump is executed to deny the cancellation of the task. However, frequent opening and cancellation of delay tasks will also bring certain overhead. Therefore, when the main thread message starts to be scheduled, a delay task is submitted, and the target time (+500ms) for the message to be executed is set. When the message is executed, the delay task will not be canceled, and the target time for the next message to be executed will be updated. When the target time delay task is executed, it is determined whether the message is completed. If it is not completed, a dump is triggered. This method can avoid performance loss caused by frequent addition and removal.
[0095] In this embodiment, when an ANR event occurs in the APP, all messages in the cache queue, as well as the ANR event, the ANR stack and at least one time-consuming stack are uploaded to the analysis end. The analysis end is used to analyze the time-consuming stacks of all time-consuming messages in a period of time before the ANR event occurs and the ANR stack at the current moment, so as to obtain the key stack related to the ANR event, and use the key stack to resolve the ANR event.
[0096] As a further optimization of this embodiment, the analysis end analyzes the data upload package to obtain a key stack, including:
[0097] Step b01: Aggregate the ANR stack and time-consuming stack corresponding to each ANR event in the data upload package according to a preset aggregation strategy to obtain at least one key stack corresponding to each ANR event;
[0098] Step b02: Count the number of key stacks corresponding to each ANR event, sort the ANR events according to the number of key stacks, and obtain the resolution priority of the ANR event.
[0099] In this embodiment, the analysis end may be a cloud, and the Android application runs on a mobile terminal, such as a mobile phone. The mobile phone uploads the monitored data upload package to the cloud, and the cloud analyzes the data upload package.
[0100] After the data upload package reported by ANR monitoring is obtained in the cloud, although a single ANR problem can be analyzed, it will be difficult to solve the ANR problem with a larger impact if the stack is not aggregated. In fact, the ANR problem is ultimately caused by the main thread message blocking the execution of the key message. As long as the stack of the time-consuming message in each ANR is aggregated according to a certain strategy (preset aggregation strategy), the key stack is obtained, and then the number of key stacks is counted, and then sorted according to the number of items, the corresponding ANR problem can be solved first.
[0101] In this embodiment, the preset aggregation strategy includes:
[0102] 1. Generally speaking, the message where the ANR occurs is considered an ANR message, but in fact, the dump time may also be offset. If we need to know more accurately, we need to consider the cause of the ANR. Search for the most recent main thread message based on the ANR occurrence time, which should not exceed 5s. Of course, if there is no reason for the ANR to occur, the message where the ANR occurs is considered an ANR message.
[0103] 2. First, remove all messages that take more than 10 seconds to execute and have a time stack >= 1. Such messages are severely stuck and may cause ANR. If the number is less than 5, expand the scope and filter the messages that take more than 5 seconds to execute and have a time stack >= 2. If it is still insufficient, expand to 3s, 2s, and 1s in sequence. The maximum number of messages is 10. If it exceeds, remove the oldest part of the newly added part. Regardless of whether there is data or not, if there is a time stack on the ANR message, it needs to be added.
[0104] 3. Get the key calling method according to a specific time-consuming stack, traverse from the bottom of the method stack upwards, and the priority is as follows from high to low (the principle is to take internal code first, then take code in third-party packages, and finally take by keyword):
[0105] A. The class name starts with "com.yupao.", and all data is obtained;
[0106] B. All data are not started with "android.", "java.", "com.android.", "androidx.", "sun.";
[0107] C. The line contains "android.app.SharedPreferencesImpl$EditorImpl";
[0108] D. The first line of methods contains "native" and "wait", and the methods in the line contain "sleep" and "handleSleeping"
[0109] E. The line contains one of "android.view.ThreadedRenderer", "android.os.BinderProxy.transactNative", "android.os.MessageQueue.nativePollOnce", and "java.io.FileDescriptor.sync";
[0110] F. First row;
[0111] 4. Aggregation of multiple stack data. In theory, the higher the stack is, the closer it is to the actual time-consuming method. However, this may be more random, resulting in a worse aggregation (except for blocking classes). Therefore, it is necessary to find a compromise point and find a method with a higher repetition rate from top to bottom.
[0112] ① First check whether there is data in 3-A. If yes, check from top to bottom (the higher the accuracy, the higher the accuracy) to see whether the repetition rate exceeds 50% (if there are no more than 2, it is necessary to check 100%). If yes, use the line to the bottom of the method stack as the key stack of the message. If there is no data, take the one with the highest repetition rate;
[0113] ② If 3-A does not exist, check 3-B. If data exists, check whether all rows from the current row to the bottom are consistent. If the repetition rate exceeds 50% (if there are no more than 2 records, check 100%), the current row to the bottom of the method stack will be used as the key stack of the message. If there is no data with a repetition rate exceeding 50%, take the maximum value exceeding 30% (at least 2 records). If it still does not exist, judge according to the subsequent conditions;
[0114] ③If the above conditions are not met, check 3-C, 3-D, 3-E, and the rules are the same as above;
[0115] ④ If none of the above conditions are met, the error summary will be processed as “failed to obtain error summary”.
[0116] 5. According to the above strategy, each ANR can find multiple time-consuming messages, each time-consuming message can have multiple time-consuming stacks, each time-consuming stack can obtain key lines, and multiple time-consuming stacks can be aggregated into a key stack through key lines, and can be classified according to this key stack. Of course, in most cases, a certain ANR is likely to be classified into multiple key stacks.
[0117] 6. There is also a special type of cyclic message, with the same callback and handler. As long as the message is sent more than 20 times in a row, it can be retrieved by the message name. This type of message may not take time to process individually, but it takes time as a whole.
[0118] The present invention can improve the accuracy of key stack judgment and reduce the ANR error rate by aggregating time-consuming stacks, and the ANR error rate drops from 0.15% to 0.06%.
[0119] Embodiment 2
[0120] Figure 3 is a block diagram of an Android application ANR monitoring device provided by an embodiment of the present invention, such as Figure 2 As shown, this embodiment provides an Android application ANR monitoring device, which is used to implement the Android application ANR monitoring method of embodiment 1, and the device includes:
[0121] A first monitoring module is used to monitor the messages of the main thread and obtain a cache queue of the main thread, wherein the cache queue caches a number of messages executed by the main thread;
[0122] The stack sampling module is used by the child thread to determine whether the execution time taken by the main thread to execute any message exceeds the preset time. If so, the main thread triggers the execution timeout, and the child thread samples the stack of the main thread to obtain at least one time-consuming stack;
[0123] The second monitoring module is used to monitor ANR events. When ANR occurs, the child thread detects the execution time of each message in the main thread execution cache queue;
[0124] The data upload module is used to package the cache queue, ANR events, ANR stack and at least one time-consuming stack to obtain a data upload package, and upload the data upload package to the analysis end so that the analysis end analyzes the data upload package to obtain a key stack.
[0125] This embodiment also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above-mentioned Android application ANR monitoring method when executing the computer program.
[0126] This embodiment also provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the above-mentioned Android application ANR monitoring method is implemented.
[0127] The present invention uses a cache queue to cache in real time a number of messages executed by the main thread and the execution time of each message, and at the same time performs a timeout judgment on the execution time of each message. When the execution time of any message exceeds a preset time, stack sampling is performed to obtain at least one time-consuming stack. When ANR occurs, the ANR stack at this time is collected, and the cache queue, ANR event, ANR stack and at least one time-consuming stack are packaged into a data upload package and uploaded to the analysis end. Finally, the analysis end is used to analyze the data upload package to obtain an accurate key stack. The key stack is conducive to troubleshooting ANR events and improves the efficiency and accuracy of troubleshooting.
[0128] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0129] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0130] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included within the scope of the claims of the present application.
Claims
1. A method for monitoring ANR of an Android application, characterized in that: The method comprises: Monitor the messages of the main thread, use the child thread to detect the execution time of each message executed by the main thread, and build the cache queue of the main thread based on the messages executed by the main thread and the corresponding execution time; The child thread determines whether the execution time taken by the main thread to execute any message exceeds a preset time. If so, the main thread triggers an execution timeout, and the child thread samples the stack of the main thread to obtain at least one time-consuming stack; Listen to ANR events. When ANR occurs, the child thread collects the messages to be distributed and ANR messages of the main thread, samples the stack of the main thread, obtains the ANR stack, and adds the messages to be distributed and ANR messages to the cache queue; Packing the cache queue, the ANR event, the ANR stack and at least one time-consuming stack to obtain a data upload package, and uploading the data upload package to the analysis end so that the analysis end analyzes the data upload package to obtain a key stack; The main thread triggers execution timeout, and the child thread samples the stack of the main thread to obtain at least one time-consuming stack, including: Create a delay detection task in the child thread, the delay detection task is used to detect whether the main thread has completed the execution of the message when the execution time taken by the main thread to execute any message reaches a preset time length; When the main thread fails to complete the execution of the message, a dump task is generated; The child thread executes a dump task to capture the time-consuming stack; The preset duration of the delay detection task is reduced by the preset duration decrement value. When the execution time of the main thread continuing to execute the message reaches the reduced preset duration, it is detected again whether the main thread has completed the execution of the message. If the main thread has not completed the execution of the message, the dump task is generated again to enable the child thread to capture the time-consuming stack. When the main thread completes the execution of the message, the preset duration is initialized and waits for the next message to be executed by the main thread.
2. The method for monitoring ANR of Android applications according to claim 1, characterized in that: The method further comprises: Classify the messages to obtain message categories, where the message categories include at least: ANR messages, freeze messages, important messages, frame drop messages, general messages, and messages to be distributed; Determine the priority of each message based on the message category, the importance of each message, or the execution time of each message.
3. The method for monitoring ANR of Android applications according to claim 2, characterized in that: The method also includes: based on a preset critical time limit value, aggregating and splitting the messages in the cache queue to obtain time-consuming messages and ordinary messages, wherein the time-consuming messages are used to characterize that the execution time required to execute the time-consuming messages exceeds the preset critical time limit value, and the ordinary messages are used to characterize that the execution time required to execute the ordinary messages does not exceed the preset critical time limit value.
4. The method for monitoring ANR of Android applications according to claim 3, characterized in that: The method further includes: based on a preset cleaning rule, cleaning the messages in the cache queue; The preset cleaning rules include at least: When the capacity of the messages cached in the cache queue reaches a preset capacity limit, clear the lowest priority message; or When the number of messages cached in the cache queue reaches a preset limit, the excess messages are cleared; or When the cache time of any message in the cache queue reaches a first preset cache time limit and the priority of the message is not the highest level or the highest level, clear the message; or When the cache time of any message in the cache queue reaches a second preset cache time limit and the priority of the message is not the highest level, clear the message; or When the cache time of any message in the cache queue reaches a third preset cache time limit, the message is cleared; The third preset cache time limit is greater than the second preset cache time limit, and the second preset cache time limit is greater than the first preset cache time limit.
5. The method for monitoring ANR of Android applications according to claim 4, characterized in that: The method further comprises: Build a message buffer pool, recycle the cleared messages in the cache queue into the message buffer pool, and use the recycled messages as reused messages.
6. The method for monitoring ANR of Android applications according to claim 1, characterized in that: The analysis end analyzes the data upload package and obtains the key stack, including: Aggregate the ANR stack and the time-consuming stack corresponding to each ANR event in the data upload package according to a preset aggregation strategy to obtain at least one key stack corresponding to each ANR event; The number of key stacks corresponding to each ANR event is counted, and the ANR events are sorted according to the number of key stacks to obtain the resolution priority of the ANR event.
7. An Android application ANR monitoring device, used to implement the Android application ANR monitoring method according to any one of claims 1 to 6, characterized in that: The device comprises: A first monitoring module is used to monitor the messages of the main thread and obtain a cache queue of the main thread, wherein the cache queue caches a number of messages executed by the main thread; The stack sampling module is used by the child thread to determine whether the execution time taken by the main thread to execute any message exceeds the preset time. If so, the main thread triggers the execution timeout, and the child thread samples the stack of the main thread to obtain at least one time-consuming stack; The second monitoring module is used to monitor ANR events. When ANR occurs, the child thread detects the execution time of each message in the main thread execution cache queue; The data upload module is used to package the cache queue, ANR events, ANR stack and at least one time-consuming stack to obtain a data upload package, and upload the data upload package to the analysis end so that the analysis end analyzes the data upload package to obtain a key stack.
8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the Android application ANR monitoring method described in any one of claims 1-6 is implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the Android application ANR monitoring method described in any one of claims 1-6 is implemented.
