Thread leakage detection method, electronic equipment and computer readable storage medium
By aggregating thread leakage logs from the Native layer and Java layer, the problem of inability to effectively detect and locate thread leakage in the existing technology is solved, the detection capability of thread leakage is improved, and the performance and user experience of the application are improved.
Patent Information
- Application Number
- CN202410112375.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-26
- Publication Date
- 2025-01-03
- Estimated Expiration
- 2044-01-26
AI Technical Summary
The prior art is difficult to effectively detect and locate thread leakage in problems such as application lag and interface unresponsiveness. Especially in Android systems, it is impossible to associate thread leakage logs from the Native layer and the Java layer for analysis.
When a thread leak occurs in the target process, the first thread leakage log and the second thread leakage log are generated and aggregated into the third thread leakage log to record thread information and call stack, and improve the detection ability of thread leakage.
After the target process has thread leakage, the thread information and call stack log are aggregated into one log, which improves the detection and location capabilities of thread leakage, can effectively identify and solve thread leakage problems, and improves the performance and user experience of the application.
Smart Images

Figure CN119248541A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of smart terminal technology, and in particular to a thread leakage detection method, an electronic device, and a computer-readable storage medium. Background Art
[0002] In application development, taking Android system application development as an example, thread leakage usually refers to the main thread in a process being blocked or occupied by a non-main thread (for example, a background thread or a child thread), causing the main thread to be unable to respond to user operations in a timely manner, thereby affecting the application's performance and user experience. Among them, the main thread refers to the first thread created after the application's corresponding process is started, and the non-main thread refers to the thread created after the first thread.
[0003] Thread leakage may cause problems such as application freezing or unresponsive interface, which seriously affects the performance of the application and user experience. Therefore, when developing applications, special attention should be paid to avoid thread leakage. Summary of the invention
[0004] The embodiments of the present application provide a thread leakage detection method, an electronic device, and a computer-readable storage medium, so as to realize that after a thread leakage occurs in a target process, a thread leakage log recording thread information and a log recording the call stack of the thread are aggregated into one log, thereby improving the thread leakage detection capability.
[0005] In a first aspect, an embodiment of the present application provides a method for detecting a thread leakage, comprising: after determining that a thread leakage occurs in a target process, generating a first thread leakage log; wherein the first thread leakage log stores information about threads that have been opened in the target process; obtaining a second thread leakage log stored in a bridge node; wherein the second thread leakage log records a call stack of the leaked thread in the target process; the leaked threads include newly opened threads in the target process after a first number reaches a preset first threshold, the first number being the number of opened threads in the target process; and aggregating the first thread leakage log and the second thread leakage log into a third thread leakage log.
[0006] In the above-mentioned thread leakage detection method, after the electronic device determines that a thread leakage occurs in the target process, it generates a first thread leakage log, then obtains the second thread leakage log stored in the bridge node, and aggregates the first thread leakage log and the second thread leakage log into a third thread leakage log. In this way, after a thread leakage occurs in the target process, the first thread leakage log recording thread information and the second thread leakage log recording the call stack of the leaked thread can be aggregated into one log, thereby improving the thread leakage detection capability.
[0007] In one possible implementation, before obtaining the second thread leakage log stored in the bridge node, it also includes: obtaining a first number of opened threads in the target process; after the first number reaches a preset first threshold, obtaining a call stack of the leaked thread in the target process; generating the second thread leakage log according to the call stack of the leaked thread; and storing the second thread leakage log in the bridge node.
[0008] In one possible implementation, generating the second thread leakage log according to the call stack of the leaking thread includes: obtaining the number of calls of each call stack according to the call stack of the leaking thread; and sorting the call stack according to the number of calls to generate the second thread leakage log.
[0009] In one possible implementation manner, after obtaining the second thread leakage log stored in the bridge node, the method further includes: deleting the second thread leakage log stored in the bridge node.
[0010] In one possible implementation, determining whether a thread leak occurs in the target process includes: obtaining a first number of threads opened by the target process; if the first number is greater than or equal to a preset second threshold, determining that a thread leak occurs in the target process; wherein the second threshold is greater than or equal to the first threshold.
[0011] In one possible implementation manner, generating the first thread leakage log includes: acquiring information of threads started by the target process; and generating the first thread leakage log according to the thread information.
[0012] In one possible implementation manner, generating a first thread leakage log according to the thread information includes: sorting information of threads started by the target process to generate a first thread leakage log.
[0013] In one possible implementation, the thread information includes: the thread name of the thread; the sorting of the information of the threads that have been opened by the target process includes: obtaining a second number of threads corresponding to each thread name according to the thread name of the thread; and sorting the thread information corresponding to each thread name according to the second number.
[0014] In one possible implementation, after sorting the thread information corresponding to each thread name according to the second number, it also includes: if there are at least two thread names among the threads opened by the target process and the number of threads corresponding to the at least two thread names is the same, then sorting the thread information corresponding to the at least two thread names according to the at least two thread names.
[0015] In one possible implementation, the thread information also includes: the running time of the thread; after sorting the thread information corresponding to each thread name according to the second number, it also includes: sorting the thread information corresponding to the first thread name according to the running time; wherein the first thread name includes the thread name whose corresponding number of threads is greater than 1 among the thread names of the threads opened by the target process.
[0016] In one possible implementation, the thread information also includes: the creation time of the thread; after sorting the information of the thread corresponding to the first thread name according to the running time, it also includes: if there are at least two threads with the same running time in the information of the thread corresponding to the first thread name, sorting the information of the at least two threads according to the creation time.
[0017] In one possible implementation, after aggregating the first thread leakage log and the second thread leakage log into a third thread leakage log, the method further includes: if the application corresponding to the target process registers for log sharing, sharing the third thread leakage log with the application.
[0018] In one possible implementation manner, after aggregating the first thread leakage log and the second thread leakage log into a third thread leakage log, the method further includes: locating a thread having a problem in the target process according to the third thread leakage log.
[0019] In a second aspect, an embodiment of the present application provides an electronic device, comprising: one or more processors; a memory; multiple applications; and one or more computer programs, wherein the one or more computer programs are stored in the memory, and the one or more computer programs include instructions, which, when executed by the electronic device, enable the electronic device to execute the method provided in the first aspect.
[0020] It should be understood that the second aspect of the embodiment of the present application is consistent with the technical solution of the first aspect of the embodiment of the present application, and the beneficial effects achieved by each aspect and the corresponding feasible implementation methods are similar and will not be repeated here.
[0021] In a third aspect, an embodiment of the present application provides a computer-readable storage medium, in which a computer program is stored. When the computer-readable storage medium is run on a computer, the computer executes the method provided in the first aspect.
[0022] In a fourth aspect, an embodiment of the present application provides a computer program, which, when executed by a computer, is used to execute the method provided in the first aspect.
[0023] In one possible design, the program in the fourth aspect may be stored in whole or in part on a storage medium packaged together with the processor, or may be stored in whole or in part on a memory not packaged together with the processor. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 A schematic diagram of a thread leak detection solution provided by the existing related technology;
[0025] Figure 2 A schematic diagram of the structure of an electronic device provided by an embodiment of the present application;
[0026] Figure 3 A software structure block diagram of an electronic device 100 provided in one embodiment of the present application;
[0027] Figure 4 A flowchart of a thread leakage detection method provided by an embodiment of the present application;
[0028] Figure 5 A flowchart of a thread leakage detection method provided by another embodiment of the present application;
[0029] Figure 6 A flowchart of a thread leakage detection method provided in yet another embodiment of the present application;
[0030] Figure 7 A flowchart of a thread leakage detection method provided in yet another embodiment of the present application. DETAILED DESCRIPTION
[0031] The terms used in the implementation section of this application are only used to explain the specific embodiments of this application and are not intended to limit this application.
[0032] After the application installed in the electronic device is started, a process is started, which is referred to as the target process in the following description. Generally speaking, one application corresponds to one target process, and multiple threads can be opened in a target process to perform tasks. After the task is completed, the thread executing the task will be released, but if the thread executing the task is not released after the task is completed, it will continue to occupy memory. In this way, if the number of threads that have not been released after completing the task gradually increases, it will cause thread leakage in the target process, the memory occupied by the target process will increase, and the application corresponding to the target process will run stuck, and it may even cause the application to crash. The "thread with problems" mentioned in the following description of this article may include threads that cause thread leakage in the target process, for example: threads that have not been released after completing the task.
[0033] Therefore, when developing applications, special attention should be paid to avoiding thread leakage issues. Figure 1A schematic diagram of a solution for thread leak detection provided by the existing related technology, such as Figure 1 As shown, after performing thread leakage detection on the target process, a thread leakage threshold (ThresholdValue) is obtained. If the number of threads opened by the target process is greater than or equal to the thread leakage threshold, it can be determined that a thread leakage fault occurs in the target process, and then the thread leakage log can be obtained.
[0034] Specifically, thread leakage detection is performed at the native layer, so after detecting that a thread leakage fault occurs in the target process, the Native layer can obtain the information of the threads that have been opened by the target process and generate a thread leakage log of the Native layer. In addition, after the number of threads started by the target process reaches a certain number, for the newly opened threads of the target process, the Java layer (i.e., the application layer) obtains the call stack of the newly opened thread and generates a thread leakage log of the Java layer. However, the thread leakage log of the Native layer and the thread leakage log of the Java layer are separate. As shown in Table 1, the thread leakage log of the Native layer records the information of the threads that have been opened by the target process, but does not record the information of the corresponding call stack. The thread leakage log of the Java layer only has the thread name and call stack, and no other information of the thread. In the existing related technology, it is impossible to associate the logs of the two layers to locate and analyze the threads with problems. In addition, in the existing related technology, it is also impossible to share the thread leakage logs of the Native layer and the Java layer with third-party applications installed in the electronic device.
[0035] Table 1
[0036] Thread leak log defect The Native layer obtains information about threads that have been opened by the target process There is no corresponding call stack information The Java layer obtains the call stack of the newly opened thread Only the thread name and call stack, no other information of the thread
[0037] Based on the above problems, an embodiment of the present application provides a thread leakage detection method, which can aggregate the thread leakage log recording thread information (such as the thread leakage log of the Native layer mentioned above) and the log recording the call stack of the thread (such as the thread leakage log of the Java layer mentioned above) into one log after a thread leakage occurs in the target process, thereby improving the thread leakage detection capability.
[0038] The thread leakage detection method provided in the embodiment of the present application can be applied to electronic devices, wherein the above-mentioned electronic devices can be smart phones, tablet computers, wearable devices, vehicle-mounted devices, augmented reality (AR) / virtual reality (VR) devices, laptop computers, ultra-mobile personal computers (UMPC), netbooks or personal digital assistants (PDA) and other devices; the embodiment of the present application does not impose any restrictions on the specific type of electronic devices.
[0039] For example, Figure 2 A schematic diagram of the structure of an electronic device provided by an embodiment of the present application is shown as follows: Figure 2 As shown, the electronic device 100 may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, an air pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.
[0040] It is to be understood that the structure illustrated in the embodiment of the present application does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0041] The processor 110 may include one or more processing units, for example, the processor 110 may include an application processor (AP), a modem processor, a graphics processor (GPU), an image signal processor (ISP), a controller, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Different processing units may be independent devices or integrated into one or more processors.
[0042] The controller can generate operation control signals according to the instruction operation code and timing signal to complete the control of instruction fetching and execution.
[0043] The processor 110 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. The memory may store instructions or data that the processor 110 has just used or cyclically used. If the processor 110 needs to use the instruction or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.
[0044] In some embodiments, the processor 110 may include one or more interfaces. The interface may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.
[0045] The USB interface 130 is an interface that complies with the USB standard specification, and specifically can be a Mini USB interface, a Micro USB interface, a USB Type C interface, etc. The USB interface 130 can be used to connect a charger to charge the electronic device 100, and can also be used to transfer data between the electronic device 100 and a peripheral device. It can also be used to connect headphones to play audio through the headphones. The interface can also be used to connect other electronic devices, such as AR devices, etc.
[0046] It is understandable that the interface connection relationship between the modules illustrated in the embodiment of the present application is only a schematic illustration and does not constitute a structural limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may also adopt different interface connection methods in the above embodiments, or a combination of multiple interface connection methods.
[0047] The charging management module 140 is used to receive charging input from a charger. The charger may be a wireless charger or a wired charger. In some wired charging embodiments, the charging management module 140 may receive charging input from a wired charger through the USB interface 130. In some wireless charging embodiments, the charging management module 140 may receive wireless charging input through a wireless charging coil of the electronic device 100. While the charging management module 140 is charging the battery 142, it may also power the electronic device 100 through the power management module 141.
[0048] The power management module 141 is used to connect the battery 142, the charging management module 140 and the processor 110. The power management module 141 receives input from the battery 142 and / or the charging management module 140, and supplies power to the processor 110, the internal memory 121, the display screen 194, the camera 193, and the wireless communication module 160. The power management module 141 can also be used to monitor parameters such as battery capacity, battery cycle number, battery health status (leakage, impedance), etc. In some other embodiments, the power management module 141 can also be set in the processor 110. In other embodiments, the power management module 141 and the charging management module 140 can also be set in the same device.
[0049] The wireless communication function of the electronic device 100 can be implemented through the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem processor and the baseband processor.
[0050] Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 100 can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve the utilization of antennas. For example, antenna 1 can be reused as a diversity antenna for a wireless local area network. In some other embodiments, the antenna can be used in combination with a tuning switch.
[0051] The mobile communication module 150 can provide solutions for wireless communications including 2G / 3G / 4G / 5G, etc., applied to the electronic device 100. The mobile communication module 150 may include at least one filter, a switch, a power amplifier, a low noise amplifier (LNA), etc. The mobile communication module 150 can receive electromagnetic waves from the antenna 1, and filter, amplify, and process the received electromagnetic waves, and transmit them to the modulation and demodulation processor for demodulation. The mobile communication module 150 can also amplify the signal modulated by the modulation and demodulation processor, and convert it into electromagnetic waves for radiation through the antenna 1. In some embodiments, at least some of the functional modules of the mobile communication module 150 can be set in the processor 110. In some embodiments, at least some of the functional modules of the mobile communication module 150 can be set in the same device as at least some of the modules of the processor 110.
[0052] The modem processor may include a modulator and a demodulator. Among them, the modulator is used to modulate the low-frequency baseband signal to be sent into a medium-high frequency signal. The demodulator is used to demodulate the received electromagnetic wave signal into a low-frequency baseband signal. The demodulator then transmits the demodulated low-frequency baseband signal to the baseband processor for processing. After the low-frequency baseband signal is processed by the baseband processor, it is passed to the application processor. The application processor outputs a sound signal through an audio device (not limited to a speaker 170A, a receiver 170B, etc.), or displays an image or video through a display screen 194. In some embodiments, the modem processor may be an independent device. In other embodiments, the modem processor may be independent of the processor 110 and be set in the same device as the mobile communication module 150 or other functional modules.
[0053] The wireless communication module 160 can provide wireless communication solutions including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared (IR), etc., which are applied to the electronic device 100. The wireless communication module 160 can be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via the antenna 2, modulates the frequency of the electromagnetic wave signal and performs filtering, and sends the processed signal to the processor 110. The wireless communication module 160 can also receive the signal to be sent from the processor 110, modulate the frequency of it, amplify it, and convert it into electromagnetic waves for radiation through the antenna 2.
[0054] In some embodiments, the antenna 1 of the electronic device 100 is coupled to the mobile communication module 150, and the antenna 2 is coupled to the wireless communication module 160, so that the electronic device 100 can communicate with the network and other devices through wireless communication technology. The wireless communication technology may include global system for mobile communications (GSM), general packet radio service (GPRS), code division multiple access (CDMA), wideband code division multiple access (WCDMA), time-division code division multiple access (TD-SCDMA), long term evolution (LTE), BT, GNSS, WLAN, NFC, FM, and / or IR technology. The GNSS may include a global positioning system (GPS), a global navigation satellite system (GLONASS), a Beidou navigation satellite system (BDS), a quasi-zenith satellite system (QZSS) and / or a satellite based augmentation system (SBAS).
[0055] The electronic device 100 implements the display function through a GPU, a display screen 194, and an application processor. The GPU is a microprocessor for image processing, which connects the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 may include one or more GPUs that execute program instructions to generate or change display information.
[0056] The display screen 194 is used to display images, videos, etc. The display screen 194 includes a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode or an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), Miniled, MicroLed, Micro-oLed, a quantum dot light-emitting diode (QLED), etc. In some embodiments, the electronic device 100 may include 1 or N display screens 194, where N is a positive integer greater than 1.
[0057] The electronic device 100 can realize the shooting function through ISP, camera 193, video codec, GPU, display screen 194 and application processor.
[0058] ISP is used to process the data fed back by camera 193. For example, when taking a photo, the shutter is opened, and the light is transmitted to the camera photosensitive element through the lens. The light signal is converted into an electrical signal, and the camera photosensitive element transmits the electrical signal to ISP for processing and converts it into an image visible to the naked eye. ISP can also perform algorithm optimization on the noise, brightness, and skin color of the image. ISP can also optimize the exposure, color temperature and other parameters of the shooting scene. In some embodiments, ISP can be set in camera 193.
[0059] The camera 193 is used to capture still images or videos. The object generates an optical image through the lens and projects it onto the photosensitive element. The photosensitive element can be a charge coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the optical signal into an electrical signal, and then passes the electrical signal to the ISP to be converted into a digital image signal. The ISP outputs the digital image signal to the DSP for processing. The DSP converts the digital image signal into an image signal in a standard RGB, YUV or other format. In some embodiments, the electronic device 100 may include 1 or N cameras 193, where N is a positive integer greater than 1.
[0060] The digital signal processor is used to process digital signals, and can process not only digital image signals but also other digital signals. For example, when the electronic device 100 is selecting a frequency point, the digital signal processor is used to perform Fourier transform on the frequency point energy.
[0061] Video codecs are used to compress or decompress digital videos. The electronic device 100 may support one or more video codecs. Thus, the electronic device 100 may play or record videos in a variety of coding formats, such as Moving Picture Experts Group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.
[0062] NPU is a neural network (NN) computing processor. By drawing on the structure of biological neural networks, such as the transmission mode between neurons in the human brain, it can quickly process input information and can also continuously self-learn. Through NPU, applications such as intelligent cognition of electronic device 100 can be realized, such as image recognition, face recognition, voice recognition, text understanding, etc.
[0063] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 through the external memory interface 120 to implement a data storage function, such as storing music, video and other files in the external memory card.
[0064] The internal memory 121 can be used to store computer executable program codes, which include instructions. The internal memory 121 may include a program storage area and a data storage area. Among them, the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc. The data storage area may store data created during the use of the electronic device 100 (such as audio data, a phone book, etc.), etc. In addition, the internal memory 121 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc. The processor 110 executes various functional applications and data processing of the electronic device 100 by running instructions stored in the internal memory 121, and / or instructions stored in a memory provided in the processor.
[0065] The electronic device 100 can implement audio functions such as music playing and recording through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone jack 170D, and the application processor.
[0066] The audio module 170 is used to convert digital audio information into analog audio signal output, and is also used to convert analog audio input into digital audio signals. The audio module 170 can also be used to encode and decode audio signals. In some embodiments, the audio module 170 can be arranged in the processor 110, or some functional modules of the audio module 170 can be arranged in the processor 110.
[0067] The speaker 170A, also called a "speaker", is used to convert an audio electrical signal into a sound signal. The electronic device 100 can listen to music or listen to a hands-free call through the speaker 170A.
[0068] The receiver 170B, also called a "earpiece", is used to convert audio electrical signals into sound signals. When the electronic device 100 receives a call or voice message, the voice can be received by placing the receiver 170B close to the human ear.
[0069] Microphone 170C, also called "microphone" or "microphone", is used to convert sound signals into electrical signals. When making a call or sending a voice message, the user can speak by putting their mouth close to microphone 170C to input the sound signal into microphone 170C. The electronic device 100 can be provided with at least one microphone 170C. In other embodiments, the electronic device 100 can be provided with two microphones 170C, which can not only collect sound signals but also realize noise reduction function. In other embodiments, the electronic device 100 can also be provided with three, four or more microphones 170C to collect sound signals, reduce noise, identify the sound source, realize directional recording function, etc.
[0070] The earphone interface 170D is used to connect a wired earphone and can be a USB interface 130 or a 3.5 mm open mobile terminal platform (OMTP) standard interface or a cellular telecommunications industry association of the USA (CTIA) standard interface.
[0071] The key 190 includes a power key, a volume key, etc. The key 190 may be a mechanical key or a touch key. The electronic device 100 may receive key input and generate key signal input related to user settings and function control of the electronic device 100.
[0072] Motor 191 can generate vibration prompts. Motor 191 can be used for incoming call vibration prompts, and can also be used for touch vibration feedback. For example, touch operations acting on different applications (such as taking pictures, audio playback, etc.) can correspond to different vibration feedback effects. For touch operations acting on different areas of the display screen 194, motor 191 can also correspond to different vibration feedback effects. Different application scenarios (for example: time reminders, receiving messages, alarm clocks, games, etc.) can also correspond to different vibration feedback effects. The touch vibration feedback effect can also support customization.
[0073] The indicator 192 may be an indicator light, which may be used to indicate the charging status, power changes, messages, missed calls, notifications, etc.
[0074] The SIM card interface 195 is used to connect a SIM card. The SIM card can be connected to and separated from the electronic device 100 by inserting it into the SIM card interface 195 or pulling it out from the SIM card interface 195. The electronic device 100 can support 1 or N SIM card interfaces, where N is a positive integer greater than 1. The SIM card interface 195 can support Nano SIM cards, Micro SIM cards, SIM cards, and the like. Multiple cards can be inserted into the same SIM card interface 195 at the same time. The types of the multiple cards can be the same or different. The SIM card interface 195 can also be compatible with different types of SIM cards. The SIM card interface 195 can also be compatible with external memory cards. The electronic device 100 interacts with the network through the SIM card to implement functions such as calls and data communications. In some embodiments, the electronic device 100 uses an eSIM, i.e., an embedded SIM card. The eSIM card can be embedded in the electronic device 100 and cannot be separated from the electronic device 100.
[0075] The software system of the electronic device 100 may adopt a layered architecture, an event-driven architecture, a micro-core architecture, a micro-service architecture, or a cloud architecture. The embodiment of the present application takes the Android system of the layered architecture as an example to exemplify the software structure of the electronic device 100.
[0076] Figure 3 A software structure block diagram of an electronic device 100 provided in accordance with an embodiment of the present application.
[0077] The layered architecture divides the software into several layers, each with clear roles and division of labor. The layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into four layers, from top to bottom: the Java layer (i.e., the application layer), the application framework layer, the native layer, and the kernel layer.
[0078] After the first number of threads opened by the target process reaches a preset first threshold, the newly opened thread of the target process can be called a leaked thread. The size of the first threshold can be set according to system performance and / or implementation requirements during specific implementation, and the present embodiment does not limit the size of the first threshold. For example, assuming that the first threshold can be 10, then after the first number of threads opened by the target process reaches 10, the newly opened thread of the target process, such as the eleventh thread, the twelfth thread, ..., can all be called a leaked thread.
[0079] like Figure 3 As shown, in an embodiment of the present application, the Java layer obtains the call stack of the leaked thread, and stores the second thread leakage log that saves the call stack of the leaked thread in the bridge node of the kernel layer. The specific storage location may be "proc / self / thread_stat". Among them, the call stack of the thread records the order in which the thread calls the function, that is, which function the thread jumps from to which function in the process of executing the task. Generally speaking, each thread has its own corresponding call stack, and the call stacks of different threads may be the same or different.
[0080] The Native layer performs thread leakage detection. After determining that a thread leakage occurs in the target process, the Native layer generates a first thread leakage log. The first thread leakage log stores information about threads that have been opened by the target process. Then the Native layer obtains a second thread leakage log from the bridge node, and aggregates the first thread leakage log and the second thread leakage log into a third thread leakage log, so that the thread leakage logs obtained by the electronic device 100 are no longer separated, thereby improving the thread leakage detection capability.
[0081] For ease of understanding, the following embodiments of the present application will be described in detail with Figure 2 and Figure 3 Taking the electronic device with the structure shown as an example, the thread leakage detection method provided in the embodiment of the present application is specifically described in combination with the accompanying drawings and application scenarios.
[0082] Figure 4 A flowchart of a thread leakage detection method provided by an embodiment of the present application is shown in FIG. Figure 4 As shown, the above thread leakage detection method may include:
[0083] Step 401: After the electronic device 100 determines that a thread leak occurs in a target process, it generates a first thread leak log.
[0084] The first thread leakage log stores information about threads that have been opened by the target process. Figure 3 , the operation of the electronic device 100 to generate the first thread leakage log is performed at the Native layer.
[0085] Step 402: The electronic device 100 obtains the second thread leakage log stored in the bridge node.
[0086] Among them, the call stack of the leaked thread in the target process is recorded in the second thread leakage log. As mentioned above, the leaked thread includes the newly opened thread in the target process after the first number reaches a preset first threshold, and the first number is the number of opened threads in the target process. Among them, the size of the above-mentioned first threshold can be set according to system performance and / or implementation requirements during specific implementation, and this embodiment does not limit the size of the first threshold. For example, assuming that the first threshold is 10, then when the first number of opened threads of the target process reaches 10, the newly opened thread of the target process, such as the eleventh thread, the twelfth thread, ..., can be called a leaked thread.
[0087] See also Figure 3 , the above-mentioned second thread leakage log is generated by the Java layer and stored in the bridge node by the Java layer, and the operation of the electronic device 100 to obtain the second thread leakage log is performed at the Native layer. In this way, after determining that a thread leakage occurs in the target process, the electronic device 100 can not only obtain the first thread leakage log generated by the Native layer, but also obtain the second thread leakage log generated by the Java layer from the bridge node, so that the first thread leakage log recording thread information and the second thread leakage log recording the call stack of the leaking thread can be aggregated into one log, thereby improving the ability to detect thread leakage and the ability to locate the thread with problems.
[0088] In step 403, the electronic device 100 aggregates the first thread leakage log and the second thread leakage log into a third thread leakage log.
[0089] In this embodiment, after aggregating the first thread leakage log and the second thread leakage log into a third thread leakage log, the electronic device 100 may temporarily store the third thread leakage log in the location “ / data / log / diagnosis_temp / ”.
[0090] In some examples, after step 403, if the application corresponding to the target process has registered log sharing, the electronic device 100 can share the third thread leakage log with the application, so that the third thread leakage log can be shared with a third-party application, and the third thread leakage log includes not only the information of the opened thread in the target process, but also the call stack of the leaked thread in the target process, so that the third-party application can easily locate the thread with the problem. In specific implementation, the path for sharing the third thread leakage log with the third-party application can be configured as "data / user / 0 / application package name / files".
[0091] In some examples, after step 403, the electronic device 100 can locate the thread with the problem in the target process based on the third thread leakage log. Since the third thread leakage log includes not only the information of the opened threads in the target process, but also the call stack of the leaked thread in the target process, the ability to detect thread leakage and the ability to locate the thread with the problem can be improved. That is, in this embodiment, after obtaining the third thread leakage log, regardless of whether the third thread leakage log is shared with a third-party application, the third thread leakage log can be analyzed to locate the thread with the problem.
[0092] In the above-mentioned thread leakage detection method, after the electronic device 100 determines that a thread leakage occurs in the target process, it generates a first thread leakage log, then obtains the second thread leakage log stored in the bridge node, and aggregates the first thread leakage log and the second thread leakage log into a third thread leakage log. In this way, after a thread leakage occurs in the target process, the first thread leakage log recording thread information and the second thread leakage log recording the call stack of the leaked thread can be aggregated into one log, thereby improving the ability to detect thread leakage and the ability to locate the thread where the problem occurs.
[0093] Figure 5 A flowchart of a thread leakage detection method provided by another embodiment of the present application is shown in FIG. Figure 5 As shown, this application Figure 4 In the illustrated embodiment, before step 402, the following may also be included:
[0094] Step 501: The electronic device 100 obtains a first number of opened threads in a target process.
[0095] Step 502: After the first number reaches a preset first threshold, the electronic device 100 obtains a call stack of a leaked thread in a target process;
[0096] Step 502: The electronic device 100 generates a second thread leakage log according to the call stack of the leakage thread.
[0097] Step 503: The electronic device 100 stores the second thread leakage log in the bridge node.
[0098] See also Figure 3 The operations of step 501 to step 503 are performed by the electronic device 100 at the Java layer, so that the call stack of the leaked thread obtained by the Java layer can be stored in the bridge node, so that the Native layer can obtain the above-mentioned second thread leakage log from the bridge node.
[0099] In a specific implementation, step 502 may be: the electronic device 100 obtains the number of calls of each call stack according to the call stack of the leaked thread; and sorts the call stack according to the number of calls to generate a second thread leakage log.
[0100] In some examples, after the first number of threads opened by the target process reaches a first threshold, for the first leaked thread newly opened by the target process, the Java layer can record the thread name, call stack, and call count of the call stack of the first leaked thread, as shown in Table 2.
[0101] Table 2
[0102] Thread Name Call Stack The number of calls in the call stack a1 Call Stack 1 1
[0103] Next, for the second leaked thread opened by the target process, the Java layer can also record the thread name, call stack and call count of the call stack of the second leaked thread. Here, it is assumed that the call stack of the second leaked thread is the same as the call stack of the first leaked thread. In this case, there is no need to repeatedly record the call stack of the second leaked thread. Instead, the call count of the call stack of the first leaked thread needs to be increased by 1, as shown in Table 3.
[0104] Table 3
[0105]
[0106] Next, for the third leaked thread opened by the target process, the Java layer continues to record the thread name, call stack, and call count of the call stack of the third leaked thread. The call stack of the third leaked thread is different from the call stacks of the first two leaked threads. Therefore, the records are shown in Table 4.
[0107] Table 4
[0108]
[0109] Similarly, for the leaked thread opened by the target process, the Java layer will record the thread name, call stack and call count of the leaked thread, forming a data structure similar to Table 4.
[0110] After recording the thread name, call stack, and call count of the call stack of the leaking thread, the Java layer may also sort the call stacks in descending order according to the call count of each call stack.
[0111] Referring to Table 4, in Table 4, the number of calls of call stack 1 is 2, and the number of calls of call stack 2 is 1, so call stack 1 is sorted before call stack 2. In addition, if at least two call stacks have the same number of calls, they can be sorted according to the strings corresponding to the call stacks. For example, the strings corresponding to the call stacks can be hash mapped, and then the call stacks can be sorted according to the obtained hash mapping values.
[0112] After sorting, the Java layer can also compress the sorted call stack to generate a second thread leakage log, and then the Java layer stores the second thread leakage log in the bridge node of the kernel layer. Compressing the sorted call stack can reduce the size of the second thread leakage log and save storage space of the bridge node of the kernel layer.
[0113] Since the call stack stored in the second thread leakage log is compressed, in step 403 of this embodiment, the electronic device 100 aggregates the first thread leakage log and the second thread leakage log into a third thread leakage log as follows: the electronic device 100 first decompresses the call stack stored in the second thread leakage log, and then stores the decompressed call stack and the thread information recorded in the first thread leakage log into a log file, and then compresses the log file to generate a third thread leakage log, thereby saving the memory space occupied by the third thread leakage log.
[0114] In addition, in this embodiment, after step 402, the electronic device 100 can also delete the second thread leakage log stored in the bridge node, thereby saving the storage space of the bridge node. That is, the bridge node can set the second thread leakage log of the Java layer to be stored only once, and delete it after being obtained from the bridge node by the Native layer.
[0115] Figure 6 A flowchart of a thread leakage detection method provided in yet another embodiment of the present application is shown in FIG. Figure 6 As shown, this application Figure 4 In the illustrated embodiment, step 401 may include:
[0116] Step 601: The electronic device 100 obtains a first number of threads started by a target process.
[0117] Step 602: If the first number is greater than or equal to a preset second threshold, the electronic device 100 determines that a thread leak occurs in the target process.
[0118] Among them, the second threshold is greater than or equal to the first threshold. The size of the second threshold can be set according to system performance and / or implementation requirements during specific implementation. This embodiment does not limit the size of the second threshold, as long as the second threshold is greater than or equal to the first threshold.
[0119] Step 603: the electronic device 100 obtains information about threads that have been opened by the target process.
[0120] Step 604: The electronic device 100 generates a first thread leakage log according to the information of the above thread.
[0121] Specifically, the electronic device 100 generating the first thread leakage log according to the information of the above-mentioned thread may include: the electronic device 100 sorts the information of the thread that has been opened by the target process to generate the first thread leakage log.
[0122] In some examples, the information of the thread may include: the thread name of the thread; thus, the information of the threads opened by the target process may be sorted as follows: the electronic device 100 obtains the second number of threads corresponding to each thread name according to the thread name of the thread, and sorts the information of the threads corresponding to each thread name according to the second number. Specifically, the electronic device 100 may sort the information of the threads corresponding to each thread name in descending order according to the second number.
[0123] After sorting the information of the threads corresponding to each thread name according to the second number, if there are at least two threads corresponding to the same number of threads among the threads opened by the target process, the information of the threads corresponding to the at least two thread names can be sorted according to the at least two thread names. Specifically, the electronic device 100 can sort the information of the threads corresponding to the at least two thread names in ascending order according to the letters contained in the character strings corresponding to the at least two thread names.
[0124] In some examples, the information of the above-mentioned thread may also include: the running time of the above-mentioned thread; in this way, after sorting the information of the thread corresponding to each thread name according to the second number, the electronic device 100 can also sort the information of the thread corresponding to the first thread name according to the running time; wherein, the first thread name can be a thread name whose corresponding number of threads is greater than 1 among the thread names of the threads that have been opened by the target process.
[0125] The running time of the above thread is the CPU occupancy time of the thread. The smaller the running time of the above thread is, the greater the possibility that the thread causes thread leakage in the target process, thereby improving the ability to locate the thread with the problem.
[0126] In the specific implementation, the kernel document "kernel_platform / msm-kernel / Documentation / filesystems / proc.rst" is consulted to know that in the Android system, " / proc / <pid> / task / <tid>" / stat" records all running information of the thread, where "utime" is the running time of the thread. In this embodiment, the running time of the thread is added to the information of the thread recorded by the first thread leakage log, which can improve the ability to locate the thread with the problem.
[0127] In some examples, the information of the above-mentioned thread may also include: the creation time of the above-mentioned thread; in this way, after sorting the information of the thread corresponding to the first thread name according to the running time, if there are at least two threads with the same running time in the information of the thread corresponding to the first thread name, the electronic device 100 sorts the information of the above-mentioned at least two threads according to the above-mentioned creation time.
[0128] It can be known from the above description that the information of the thread recorded in the first thread leakage log may include the thread name, running time and creation time of the thread that has been opened in the target process. Of course, the thread information recorded in the first thread leakage log may also include more information, such as: thread identification, etc., which is not limited in this embodiment. Since multiple threads can be opened in the target process, each thread has its own thread name, but there may be different threads with the same thread name, therefore, the electronic device 100 can first count the second number of threads corresponding to each thread name according to the thread name, and sort the thread information according to the second number; then, if there are at least two thread names corresponding to the same number of threads, then the thread information can be sorted according to the above at least two thread names. Finally, for the thread corresponding to the first thread name, it can be further sorted according to the running time and creation time of the thread.
[0129] For example, assuming that the number of threads corresponding to the thread name "d1" is 5, the number of threads corresponding to the thread name "e2" is 5, and the number of threads corresponding to the thread name "f1" is 3, then in the first thread leakage log, the information of the thread corresponding to "d1" is placed first, followed by the information of the thread corresponding to "e2", and finally the information of the thread corresponding to "f1". Furthermore, for the 5 threads corresponding to "d1", the information of the 5 threads can be sorted according to the running time of the 5 threads. If the running time of 2 or more threads in the information of the 5 threads is the same, the electronic device 100 can sort the information of the above 2 or more threads according to the creation time. Similarly, the 5 threads corresponding to "e2" and the 3 threads corresponding to "f1" can be sorted in the same way, which will not be repeated here.
[0130] Figure 7 A flowchart of a thread leakage detection method provided in yet another embodiment of the present application is shown in FIG. Figure 7 As shown, the above thread leakage detection method may include:
[0131] Step 701: The electronic device 100 obtains a first number of opened threads in a target process.
[0132] Step 702: After the first number reaches a preset first threshold, the electronic device 100 obtains the call stack of the leaked thread in the target process.
[0133] Step 703: The electronic device 100 generates a second thread leakage log according to the call stack of the leakage thread.
[0134] Step 704: The electronic device 100 stores the second thread leakage log in the bridge node.
[0135] The operations of step 701 to step 704 are executed by the electronic device 100 at the Java layer.
[0136] Step 705: The electronic device 100 obtains the first number of threads that have been opened by the target process.
[0137] Step 706: If the first number is greater than or equal to a preset second threshold, the electronic device 100 determines that a thread leak occurs in the target process.
[0138] Step 707: The electronic device 100 obtains information about threads that have been opened by the target process.
[0139] Step 708: The electronic device 100 generates a first thread leakage log according to the information of the above thread.
[0140] Step 709: The electronic device 100 obtains the second thread leakage log stored in the bridge node.
[0141] In step 710, the electronic device 100 aggregates the first thread leakage log and the second thread leakage log into a third thread leakage log. Then, step 711 or step 712 is executed.
[0142] Step 711: If the application corresponding to the target process has registered for log sharing, the electronic device 100 shares the third thread leakage log with the application.
[0143] Step 712: The electronic device 100 locates the thread where the problem occurs in the target process according to the third thread leakage log.
[0144] Among them, the operations of step 705 to step 712 are performed by the electronic device 100 at the Native layer, and steps 705 to step 708 and steps 701 to step 704 can be performed in parallel or sequentially. This embodiment does not limit the execution order of steps 705 to step 708 and steps 701 to step 704.
[0145] In addition, after step 709, the following steps may also be included:
[0146] Step 713: The electronic device 100 deletes the second thread leakage log stored in the bridge node.
[0147] The operation of step 713 is performed by the electronic device 100 in the bridge node of the kernel layer.
[0148] It is to be understood that some or all of the steps or operations in the above embodiments are merely examples, and the present application embodiments may also perform other operations or variations of various operations. In addition, the various steps may be performed in different orders presented in the above embodiments, and it is possible that not all of the operations in the above embodiments need to be performed.
[0149] The present application also provides a computer-readable storage medium, in which a computer program is stored. When the computer-readable storage medium is run on a computer, the computer executes the present application. Figures 4 to 7 The method provided by the illustrated embodiment.
[0150] The present application also provides a computer program product, which includes a computer program, which, when executed on a computer, enables the computer to execute the present application. Figures 4 to 7 The method provided by the illustrated embodiment.
[0151] In the embodiments of the present application, "at least one" refers to one or more, and "more than one" refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B may represent the existence of A alone, the existence of A and B at the same time, and the existence of B alone. A and B may be singular or plural. The character " / " generally indicates that the previous and subsequent associated objects are in an "or" relationship. "At least one of the following" and similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one of a, b and c may represent: a, b, c, a and b, a and c, b and c, or a and b and c, where a, b, c may be single or multiple.
[0152] Those of ordinary skill in the art will appreciate that the various units and algorithm steps described in the embodiments disclosed herein can be implemented in a combination of electronic hardware, computer software, and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0153] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0154] In several embodiments provided in the present application, any function can be stored in a computer-readable storage medium if it is implemented in the form of a software functional unit and sold or used as an independent product. Based on this understanding, the technical solution of the present application can be embodied in the form of a software product in essence or in other words, the part that contributes to the prior art or the part of the technical solution. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0155] The above is only a specific implementation of the present application. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. The protection scope of the present application should be based on the protection scope of the claims.< / tid> < / pid>
Claims
1. A thread leakage detection method, characterized in that: include: After determining that a thread leak occurs in the target process, generating a first thread leak log; wherein the first thread leak log stores information about threads that have been opened by the target process; Obtain a second thread leakage log stored in the bridge node; wherein the second thread leakage log records the call stack of the leaked thread in the target process; the leaked thread includes a thread newly opened in the target process after the first number reaches a preset first threshold, and the first number is the number of opened threads in the target process; The first thread leakage log and the second thread leakage log are aggregated into a third thread leakage log.
2. The method according to claim 1, characterized in that Before obtaining the second thread leakage log stored in the bridge node, the method further includes: Get the first number of threads started in the target process; After the first number reaches a preset first threshold, obtaining a call stack of a leaked thread in the target process; Generate the second thread leakage log according to the call stack of the leakage thread; The second thread leakage log is stored in the bridge node.
3. The method according to claim 2, characterized in that The generating the second thread leakage log according to the call stack of the leakage thread includes: According to the call stack of the leaked thread, the number of calls of each call stack is obtained; The call stack is sorted according to the number of calls to generate the second thread leakage log.
4. The method according to any one of claims 1 to 3, characterized in that: After obtaining the second thread leakage log stored in the bridge node, the method further includes: The second thread leakage log stored in the bridge node is deleted.
5. The method according to claim 1, characterized in that: Determining that a thread leak occurs in the target process includes: Get the first number of threads started by the target process; If the first number is greater than or equal to a preset second threshold, it is determined that a thread leak occurs in the target process; wherein the second threshold is greater than or equal to the first threshold.
6. The method according to claim 1 or 5, characterized in that: Generating the first thread leakage log includes: Obtain information about threads that have been opened by the target process; A first thread leakage log is generated according to the information of the thread.
7. The method according to claim 6, characterized in that The generating a first thread leakage log according to the thread information includes: Information of threads started by the target process is sorted to generate a first thread leakage log.
8. The method according to claim 7, characterized in that The thread information includes: the thread name of the thread; The sorting of information of threads started by the target process includes: According to the thread name of the thread, obtaining a second number of threads corresponding to each thread name; According to the second number, the thread information corresponding to each thread name is sorted.
9. The method according to claim 8, characterized in that After sorting the thread information corresponding to each thread name according to the second number, the method further includes: If there are at least two thread names in the opened threads of the target process with the same number of threads corresponding to them, information of the threads corresponding to the at least two thread names is sorted according to the at least two thread names.
10. The method according to claim 9, characterized in that The thread information also includes: the running time of the thread; After sorting the thread information corresponding to each thread name according to the second number, the method further includes: According to the running time, the information of the threads corresponding to the first thread name is sorted; wherein the first thread name includes the thread names of the thread opened by the target process, the number of corresponding threads of which is greater than 1.
11. The method according to claim 10, characterized in that The thread information also includes: the creation time of the thread; After sorting the information of the threads corresponding to the first thread name according to the running time, the method further includes: If at least two threads in the thread information corresponding to the first thread name have the same running time, the information of the at least two threads is sorted according to the creation time.
12. The method according to claim 1, characterized in that After the first thread leakage log and the second thread leakage log are aggregated into a third thread leakage log, the method further includes: If the application corresponding to the target process has registered for log sharing, the third thread leakage log is shared with the application.
13. The method according to claim 1, characterized in that After the first thread leakage log and the second thread leakage log are aggregated into a third thread leakage log, the method further includes: According to the third thread leakage log, the thread where the problem occurs in the target process is located.
14. An electronic device, characterized in that: include: one or more processors; Memory; Multiple applications; and one or more computer programs, wherein the one or more computer programs are stored in the memory, and the one or more computer programs include instructions, which, when executed by the processor, enable the electronic device to perform the 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, which, when executed on a computer, enables the computer to execute the method according to any one of claims 1 to 13.
Citation Information
Patent Citations
Method and device for determining abnormal thread, computer equipment and storage medium
CN114003390A
