Method, device and electronic device for locating leaked file descriptors
By monitoring and capturing the call stack of the target process, the problem of insufficient system resources caused by FD leakage is solved, and the efficiency of FD leakage positioning and the performance of electronic equipment are improved.
Patent Information
- Application Number
- CN202311442923.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-31
- Publication Date
- 2025-09-19
- Estimated Expiration
- 2043-10-31
AI Technical Summary
In the existing technology, file descriptor (FD) leakage leads to insufficient system resources, affecting the stability and performance of electronic devices. In addition, the method of locating FD leakage is inefficient and requires a lot of manual intervention and storage overhead.
By monitoring whether the target process has FD leakage and capturing its call stack, the location information of the FD leakage is obtained, and the call stack is used to locate the cause of the FD leakage, including capturing, managing and aggregating the call stack to improve positioning efficiency.
It can quickly and accurately locate the cause of FD leakage, reduce storage space usage, and improve the performance and stability of electronic equipment.
Smart Images

Figure CN118445137B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of electronic devices, and more specifically, to a method, device, and electronic device for locating leaked file descriptors. Background Art
[0002] In recent years, with the development of electronic devices, various applications (APPs) have continued to emerge. For example, top third-party apps like TikTok and Kuaishou have become an indispensable part of public entertainment, allowing users to conduct various tasks through these apps. File descriptors (FDs) are typically used to uniquely identify open files. Each time an application opens a file, the system assigns it a file descriptor, which the application can use to access the corresponding file.
[0003] When an application requests the kernel to open / create a file, the kernel will return an FD corresponding to the opened / created file. In fact, FD is an index value, a non-negative integer, pointing to the record table of files opened by the process maintained by the kernel for each process. However, since the number of FDs that each process can use is upper bounded, for example, the upper bound of FD is 1024 by default, which means that each process cannot create more than 1024 FDs, if the corresponding FD is not closed in time after the file call is completed, it will cause FD leakage, resulting in insufficient FDs, affecting the operation of the system and even causing the application of the electronic device to crash, freeze the screen, restart the virtual machine (VM), etc. In related technologies, most of them will export all FD information after determining that an FD leak has occurred, and use manual methods to read the FD information, which requires large storage overhead and high labor costs, and has low processing efficiency.
[0004] Therefore, how to accurately locate leaked file descriptors in electronic devices is a technical problem that needs to be solved urgently. Summary of the Invention
[0005] Embodiments of the present application provide a method, apparatus, and electronic device for locating a leaked file descriptor, for accurately locating the cause of the file descriptor leak in the electronic device.
[0006] In order to achieve the above objectives, the embodiments of the present application adopt the following technical solutions:
[0007] In a first aspect, embodiments of the present application provide a method for locating a leaked file descriptor, the method comprising: an electronic device monitoring a target process in the electronic device for a file descriptor leak. When the electronic device detects a file descriptor leak in the target process, the electronic device captures a call stack of the target process to obtain location information indicating that the target process has leaked the file descriptor, the location information including a call stack of one or more file descriptors of the target process, the call stack of the target process being used to locate the cause of the file descriptor leak in the target process.
[0008] In an embodiment of the present application, the electronic device monitors whether a file descriptor leak occurs in a target process in the electronic device; when a file descriptor leak occurs in the target process in the electronic device, the target process's call stack is captured to obtain a call stack of one or more file descriptors of the target process, and the call stack of the target process is used to locate the cause of the file descriptor leak in the target process (such as a specific code location). Therefore, a backend developer can use the call stack of the one or more file descriptors to locate the specific code location where the file descriptor leak occurs, which is conducive to timely locating the file descriptor leak problem of the electronic device and improving the performance of the electronic device.
[0009] In one possible implementation of the present application, when a file descriptor leak is detected in an electronic device, the call stack of the target process is captured to obtain location information of the file descriptor leak in the target process. The method provided in an embodiment of the present application further includes: the electronic device manages the captured call stack. For example, after the electronic device captures the call stack of the file descriptor that has been leaked in the electronic device, the electronic device may close or add a new file descriptor. At this time, the electronic device can manage the captured call stack based on the operation of closing or adding the new file descriptor, so that file descriptors of the same call stack can be shared.
[0010] In one possible implementation of the present application, when a file descriptor leak is detected in an electronic device, the call stack of a target process is captured to obtain location information of the file descriptor leak in the target process. The method provided in an embodiment of the present application further includes: upon detecting that a first file descriptor of the target process is closed, the electronic device clears the call stack of the first file descriptor. This allows the call stack of the closed file descriptor to be cleared in a timely manner, reducing the storage space occupied by the electronic device.
[0011] In one possible implementation of the present application, upon detecting a file descriptor leak in an electronic device, the method provided in an embodiment of the present application further comprises: upon detecting an operation to create a second file descriptor, the electronic device saves the call stack of the second file descriptor. This allows the call stack to be created in a timely manner, so that if a file descriptor leak is subsequently detected, the cause of the file descriptor leak can be located by capturing the call stack of the file descriptor.
[0012] In a possible implementation of the present application, when an electronic device detects that a file descriptor leak has occurred in the electronic device, after capturing the call stack of the file descriptor that has leaked in the electronic device, the method provided by the embodiment of the present application also includes: the electronic device aggregates one or more of the captured call stacks so that file descriptors using the same call stack share the call stack to obtain a call stack list, the call stack list including a call stack corresponding to each file descriptor type in one or more file descriptor types, and the number of file descriptors corresponding to each file descriptor type, wherein file descriptors with the same call stack belong to the same file descriptor type. By aggregating file descriptors using the same call stack, storage space usage can be reduced.
[0013] In one possible implementation of the present application, the method provided in an embodiment of the present application further includes: upon detecting that a first file descriptor of a target process is closed, the electronic device determines whether a call stack of the first file descriptor exists in a call stack list. If a call stack of the first file descriptor exists in the call stack list, the electronic device updates the call stack list based on the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs after the first file descriptor is closed.
[0014] In a possible implementation of the present application, the electronic device updates a call stack list based on the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs after closing the first file descriptor, including: if after closing the first file descriptor, the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs is 0, the electronic device clears the call stack of the first file descriptor from the call stack list. If after closing the first file descriptor, the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs is greater than 0, the electronic device reduces the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs in the call stack list.
[0015] In a possible implementation of the present application, the method provided in an embodiment of the present application further includes: upon detecting an operation of creating a second file descriptor, the electronic device determining whether a call stack identical to the call stack of the second file descriptor exists in a call stack list. If a call stack of the second file descriptor does not exist in the call stack list, the electronic device creates and saves a call stack of the second file descriptor. If a call stack of the second file descriptor exists in the call stack list, the electronic device increases the number of file descriptors corresponding to the file descriptor type to which the second file descriptor belongs in the call stack list.
[0016] In a second aspect, an embodiment of the present application provides a device for locating a leaked file descriptor, which can implement the method in the first aspect or any possible implementation of the first aspect, and thus can also achieve the beneficial effects in the first aspect or any possible implementation of the first aspect. The device for locating a leaked file descriptor can be an electronic device, or a device that supports an electronic device to implement the method in the first aspect or any possible implementation of the first aspect, such as a chip used in an electronic device. The device for locating a leaked file descriptor can implement the above method through software, hardware, or by executing corresponding software through hardware.
[0017] As an example, the apparatus for locating a leaked file descriptor may include a processing module and a storage module, wherein the storage module is configured to store a call stack of any file descriptor. The processing module is configured to execute the relevant steps of the processing performed by the electronic device in the first aspect or any possible implementation of the first aspect.
[0018] Optionally, the apparatus for locating a leaked file descriptor may further include a communication module, wherein the communication module is configured to support the electronic device in executing receiving / sending related steps.
[0019] Exemplarily, when the device for locating the leaked file descriptor is a chip or chip system in an electronic device, the processing module may be a processor, and the communication module may be a communication interface. For example, the communication interface may be an input / output interface, a pin, or a circuit, etc. The processing module executes the instructions stored in the storage unit to enable the electronic device to implement a method for locating a leaked file descriptor described in the first aspect or any possible implementation of the first aspect. The storage unit may be a storage unit in the chip (for example, a register, a cache, etc.), or it may be a storage unit in the electronic device located outside the chip (for example, a read-only memory, a random access memory, etc.).
[0020] In a third aspect, an embodiment of the present application further provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the method described in the first aspect or various possible implementations of the first aspect is implemented.
[0021] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, it implements the method described in the first aspect or various possible implementations of the first aspect.
[0022] In a fifth aspect, the present application provides a computer program product, comprising a computer program, which, when run on a computer, enables the computer to execute the method in the above-mentioned first aspect or any possible implementation of the first aspect.
[0023] In a sixth aspect, the present application provides a chip for use in an electronic device, the chip comprising at least one processor and a communication interface, the communication interface being coupled to at least one processor, the processor being used to run a computer program or instruction to execute the method of the first aspect or any possible implementation of the first aspect, and the communication interface being used to communicate with other modules outside the chip.
[0024] It can be understood that the beneficial effects that can be achieved by the technical solutions described in the second to sixth aspects provided above can refer to the beneficial effects of the method for locating leaked file descriptors in the first aspect and any possible design thereof, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] Figure 1 A schematic diagram of a scenario provided in an embodiment of the present application;
[0026] Figure 2 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;
[0027] Figure 3 A software structure block diagram of an electronic device provided in an embodiment of the present application;
[0028] Figure 4 A flowchart of a method for locating a leaked file descriptor provided in an embodiment of the present application;
[0029] Figure 5 A schematic diagram of the structure of an apparatus for locating leaked file descriptors provided in an embodiment of the present application;
[0030] Figure 6 A schematic diagram of the structure of a chip provided in an embodiment of the present application. DETAILED DESCRIPTION
[0031] In order to clearly describe the technical solutions of the embodiments of the present application, in the embodiments of the present application, words such as "first" and "second" are used to distinguish between identical or similar items with substantially the same functions and effects. For example, the first file descriptor and the second file descriptor are merely used to distinguish different file descriptors and do not limit their order. Those skilled in the art will understand that words such as "first" and "second" do not limit the quantity and execution order, and words such as "first" and "second" do not necessarily limit differences.
[0032] It should be noted that, in this application, words such as "exemplary" or "for example" are used to indicate examples, illustrations, or descriptions. Any embodiment or design described in this application as "exemplary" or "for example" should not be construed as being preferred or advantageous over other embodiments or designs. Rather, the use of words such as "exemplary" or "for example" is intended to present the relevant concepts in a concrete manner.
[0033] The business scenarios described in the embodiments of the present application are intended to more clearly illustrate the technical solutions of the embodiments of the present application, and do not constitute a limitation on the technical solutions provided in the embodiments of the present application. Ordinary technicians in this field will know that with the emergence of new business scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0034] In this application, "at least one" means one or more, and "more" means two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following items" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a, b, or c can mean: a, b, c, ab, ac, bc, or abc, where a, b, c can be single or multiple.
[0035] The steps involved in the method for locating a leaked file descriptor provided in an embodiment of the present application are merely examples. Not all steps must be performed, or not all information or content in a message is mandatory. They can be increased or decreased as needed during use.
[0036] In the embodiments of the present application, the same step or steps or messages with the same function in different embodiments can be referenced to each other.
[0037] The following are the definitions of the relevant terms involved in the embodiments of this application:
[0038] 1. File descriptor (FD)
[0039] Usually the kernel uses file descriptors to access files. File descriptors are non-negative integers. When opening an existing file or creating a new file, the kernel returns a file descriptor. Each file descriptor corresponds to an open file, and different file descriptors can point to the same file. The same file can be opened by different processes or multiple times in the same process. The system maintains a file descriptor table (FD table) for each process. The values of this table start at 0, so the same file descriptor can be seen in different processes. In this case, the same file descriptor may point to the same file or different files.
[0040] 2. File Descriptor Leak
[0041] The Android system has an upper limit on the number of file descriptors that can be opened, so each process is also limited in the number of file descriptors that can be opened. Each process has a maximum number of file descriptors. For example, the system allows a process to occupy a maximum of 1024 file descriptors, or even more than 1024 file descriptors. If a process opens a large number of files or fails to release the corresponding file descriptors promptly after closing a file, resulting in insufficient available file descriptors, the system will report an error and may even cause the application to which the process belongs to crash, forcing the application to exit during operation, affecting the stability of the application. This phenomenon of files not being closed promptly and continuing to occupy file descriptors is called a file descriptor leak. For example, when an electronic device runs an application, the kernel creates a file descriptor corresponding to that file, and the device then uses file operators to operate on the file. For example, when an electronic device uses an application to load images, load resources, read or write files, or when the upper layer calls some system resources or interfaces, these all involve loading file descriptors.
[0042] The following combination Figure 1 , taking the Android system as an example, the communication principle of FD leakage is introduced.
[0043] like Figure 1As shown, taking the collaborative scenario of electronic device 1 (such as a mobile phone) and electronic device 2 (such as a tablet) as an example, during the collaborative process of the mobile phone and the tablet, for example, when switching audio and video during the collaborative process of the mobile phone and the tablet, the use of the looper mechanism will cause the mobile phone and the tablet to involve loading file descriptors that are not released during the collaborative process, resulting in a continuous increase in the number of file descriptors. After the FD increases to a certain number, it may reach the upper limit of the electronic device, which will cause FD leakage and cause the collaborative connection between the mobile phone and the tablet to be disconnected, or cause the mobile phone or tablet to restart.
[0044] Currently, the cause of the disconnected collaborative connection between the phone and tablet is determined through the following methods: For example, by reproducing the issue and confirming the file leak type, analysis confirmed that the leak was caused by the eventfd and eventpoll files. After troubleshooting and debugging the business processes involving eventfd and eventpoll files, the cause was found to be the use of looper. This process analyzed the looper process, sorted out the eventfd and eventpoll application processes, and determined how to correctly release the eventfd and eventpoll files. Modifications and verification were completed. Although this solution can locate the leaked FD, the analysis process is complex, the location cycle is long, and low-probability issues are difficult to locate.
[0045] In view of this, in an embodiment of the present application, an electronic device monitors whether a file descriptor leak occurs in the electronic device; if a file descriptor leak is detected in the electronic device, the electronic device captures the call stack of the leaked file descriptor in the electronic device, and the call stack can be used to locate the cause of the file descriptor leak. Therefore, backend developers can locate the specific cause of the file descriptor leak through the call stack of the leaked file descriptor, which is conducive to timely locating the file descriptor leak problem of the electronic device and improving the performance of the electronic device.
[0046] The electronic devices involved in the embodiments of the present application may be electronic devices with an operating system, such as mobile phones, tablet computers, personal computers (PCs), wearable electronic devices such as smart watches, various teaching auxiliary tools (such as learning machines, early education machines), smart toys, portable robots, personal digital assistants (PDAs), augmented reality (AR) devices, virtual reality (VR) devices, etc.
[0047] In order to better understand the embodiments of the present application, the hardware structure of the electronic device of the embodiments of the present application is introduced below. Figure 2 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application.
[0048] The electronic device 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, 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, a sensor module 180, a button 190, an indicator 192, a camera 193, and a display screen 194, etc.
[0049] Optionally, the above-mentioned 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.
[0050] It is understood that the structures illustrated in the embodiments of the present application do not constitute specific limitations on the electronic device. In other embodiments of the present application, the electronic device may include more or fewer components than shown, or may combine or separate certain components, or arrange the components differently. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.
[0051] The processor 110 may include one or more processing units. The different processing units may be independent devices or integrated into one or more processors. The processor 110 may also be provided with a memory for storing instructions and data.
[0052] USB port 130 is an interface that complies with USB standards and may be a Mini USB port, a Micro USB port, a USB Type-C port, or the like. USB port 130 can be used to connect a charger to charge an electronic device, transfer data between the electronic device and peripherals, connect headphones to play audio, and connect other electronic devices, such as augmented reality devices.
[0053] The charging management module 140 is used to receive charging input from a charger. The charger can be a wireless charger or a wired charger. The power management module 141 is used to connect the charging management module 140 to the processor 110.
[0054] The wireless communication function of the electronic device can be implemented through antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, modem processor and baseband processor.
[0055] Antenna 1 and Antenna 2 are used to transmit and receive electromagnetic wave signals. Antennas in electronic devices can be used to cover single or multiple communication frequency bands. Different antennas can also be reused to improve antenna utilization.
[0056] Mobile communication module 150 can provide wireless communication solutions for electronic devices, including 2G / 3G / 4G / 5G. It can include at least one filter, switch, power amplifier, and low-noise amplifier (LNA). Mobile communication module 150 receives electromagnetic waves from antenna 1, filters and amplifies the received electromagnetic waves, and transmits them to a modem processor for demodulation.
[0057] The wireless communication module 160 can provide wireless communication solutions for electronic devices, including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks, Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM)), etc.
[0058] The electronic device implements display functionality through a GPU, display screen 194, and an application processor. The GPU is a microprocessor for image processing that connects the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering.
[0059] The display screen 194 is used to display images, videos, etc. The display screen 194 includes a display panel. In some embodiments, the electronic device may include one or N display screens 194, where N is a positive integer greater than one.
[0060] The electronic device can realize the shooting function through the ISP, camera 193, video codec, GPU, display 194 and application processor.
[0061] The camera 193 is used to capture still images or videos. In some embodiments, the electronic device may include 1 or N cameras 193, where N is a positive integer greater than 1.
[0062] 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. The external memory card communicates with the processor 110 through the external memory interface 120 to implement data storage.
[0063] For example, save music, videos and other files in an external storage card.
[0064] The internal memory 121 may be used to store computer executable program codes, where the executable program codes include instructions.
[0065] The internal memory 121 may include a program storage area and a data storage area.
[0066] The electronic device can implement audio functions such as music playback and recording through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, and the application processor.
[0067] The audio module 170 is used to convert digital audio information into analog audio signals for output, and also to convert analog audio input into digital audio signals. The speaker 170A, also called a "speaker", is used to convert audio electrical signals into sound signals.
[0068] The electronic device can listen to music or make hands-free calls through speaker 170A. Receiver 170B, also known as an "earpiece," converts audio signals into sound signals. When the electronic device receives a call or voice message, the user can hold receiver 170B close to their ear to hear the voice. Microphone 170C, also known as a "microphone" or "earpiece," converts sound signals into electrical signals.
[0069] The pressure sensor 180A is used to sense pressure signals and convert them into electrical signals. In some embodiments, the pressure sensor 180A can be provided on the display screen 194. The gyroscope sensor 180B can be used to determine the motion posture of the electronic device. The air pressure sensor 180C is used to measure air pressure. The magnetic sensor 180D includes a Hall sensor. The acceleration sensor 180E can detect the magnitude of the acceleration of the electronic device in various directions (generally three axes). The distance sensor 180F is used to measure distance. The proximity light sensor 180G can include, for example, a light emitting diode (LED) and a light detector, such as a photodiode.
[0070] Ambient light sensor 180L senses ambient light brightness. Fingerprint sensor 180H collects fingerprints. Temperature sensor 180J detects temperature. Touch sensor 180K, also known as a "touch control device," can be mounted on display screen 194. The touch sensor 180K and display screen 194 form a touch screen, also known as a "touch screen." Bone conduction sensor 180M captures vibration signals.
[0071] The buttons 190 include a power button, a volume button, etc. The buttons 190 can be mechanical buttons or touch buttons.
[0072] The electronic device can receive key inputs and generate key signal inputs related to user settings and function control of the electronic device. Indicator 192 can be an indicator light that can be used to indicate charging status, power changes, messages, missed calls, notifications, etc.
[0073] Figure 3 This is a software structure diagram of an electronic device applicable to embodiments of the present application. A layered architecture divides the software system of an electronic device into several layers, each with distinct roles and responsibilities. Layers communicate with each other via software interfaces.
[0074] In some embodiments, the Android system can be divided into four layers, from top to bottom: application layer, application framework layer, Android runtime and system library, and kernel layer.
[0075] The application layer can include a series of application packages, and the application layer runs applications by calling the application programming interface (API) provided by the application framework layer. Figure 3 As shown, the application package may include applications such as camera, gallery, calendar, call, map, navigation, Bluetooth, music, video, short message, etc.
[0076] The application framework layer provides API and programming framework for the applications in the application layer. The application framework layer includes some predefined functions. Figure 3 As shown, the application framework layer may include a window manager, a content provider, a view system, a phone manager, a resource manager, a notification manager, and the like.
[0077] The window manager manages windowed applications. It can determine the display size, determine whether a status bar is present, lock the screen, take screenshots, and more. Content providers store and retrieve data and make it accessible to applications. This data can include video, images, audio, incoming and outgoing calls, browsing history and bookmarks, and the phone book.
[0078] The view system includes visual controls, such as those for displaying text and images. The view system can be used to build applications. A display interface can consist of one or more views. For example, a display interface containing a text notification icon can include a view for displaying text and a view for displaying images. The phone manager provides communication functionality for electronic devices.
[0079] For example, the management of call status (including answering, hanging up, etc.). The resource manager provides various resources for applications, such as localized strings, icons, pictures, layout files, video files, etc. The notification manager enables applications to display notification information in the status bar, which can be used to convey notification-type messages and can automatically disappear after a short stay without user interaction. For example, the notification manager is used to notify downloads are complete, message reminders, etc. The notification manager can also be a notification that appears in the status bar at the top of the system in the form of a chart or scroll bar text, such as notifications of applications running in the background, or a notification that appears on the screen in the form of a dialog window. For example, prompting text information in the status bar, emitting a prompt sound, vibrating electronic equipment, flashing indicator lights, etc.
[0080] The Android system runtime consists of core libraries and a virtual machine. The Android system runtime is responsible for scheduling and management of the Android system. The core library consists of two parts: one for the Java language's callable functions and the other for the Android core library. The application layer and the application framework layer run in the virtual machine. The virtual machine executes Java files from the application layer and application framework layer as binary files. The virtual machine is responsible for performing functions such as object lifecycle management, stack management, thread management, security and exception management, and garbage collection. System libraries, also known as native services, can include libraries written in C or C++ and are used by various components in the Android system. System libraries also provide services to developers through the application framework layer. System libraries can include modules with multiple functions, such as a surface manager, a FD leak detection module, a FD stack capture module, a FD request stack return module, a call stack recording module, and a call stack storage module.
[0081] Optionally, the system library may further include an output module, also referred to as a dump module, for outputting call stack information of a file descriptor.
[0082] The surface manager is used to manage the display subsystem and provide the fusion of two-dimensional and three-dimensional layers for multiple applications.
[0083] The FD leakage detection module is used to detect whether the number of FDs in different processes is leaked.
[0084] The FD stack capture module is used to capture the leaked FD stack when the FD leakage detection module detects that there is an FD leakage, so as to obtain the leaked FD call stack.
[0085] The FD application back stack module is responsible for the FD application code back stack. For example, the FD application back stack module is used to obtain the call stack of the leaked FD from the call stack storage module based on the request of the FD stack capture module.
[0086] The call stack recording module is used to record the call stack for creating file descriptors.
[0087] The call stack saving module is responsible for saving the call stack of the FD. For example, the call stack of the FD can be saved in the cache and shared with the hiview module. Finally, the hiview module saves the call stack log.
[0088] The kernel layer is the layer between hardware and software. It drives the hardware, enabling it to operate. The kernel layer includes at least camera drivers and sensor drivers, though this is not a limitation in the present embodiment. The kernel layer also serves as an abstraction layer between the hardware and software stacks, and can include multiple library modules.
[0089] The above-mentioned FD leakage detection module and FD stack capture module can also be called hiview module.
[0090] It should be understood that the above Figure 3 The names of the modules are merely examples given for the convenience of introduction in the embodiments of the present application, and the embodiments of the present application do not limit the names of these modules.
[0091] like Figure 4 As shown, Figure 4 A flowchart of a method for locating a leaked file descriptor provided in an embodiment of the present application is provided. The method can be applied to an electronic device and includes:
[0092] Step 401: The electronic device monitors whether a file descriptor leak occurs in a target process in the electronic device.
[0093] For example, the target process can be any one of the many processes of the electronic device, or it can be all the processes in the electronic device, that is, the electronic device can perform file descriptor leakage monitoring operations on all processes in the electronic device. Of course, the target process can also be some processes in the electronic device, and the electronic device can perform file descriptor leakage monitoring operations on different processes in the electronic device. The embodiments of the present application do not limit this.
[0094] For example, the target process may be a process in an electronic device that is prone to file descriptor leakage, or a process that is frequently used in an electronic device or a relatively important process in an electronic device. This embodiment of the present application does not limit this.
[0095] As described above, each process in an electronic device corresponds to a file descriptor upper limit. Therefore, the electronic device can monitor the number of file descriptors of a target process and then compare the number of file descriptors of the target process with the file descriptor upper limit corresponding to the target process to determine whether a file descriptor leak has occurred in the target process. For example, if the number of file descriptors of the target process is lower than the file descriptor upper limit corresponding to the target process, it is determined that a file descriptor leak has not occurred in the target process. If the number of file descriptors of the target process is greater than or equal to the file descriptor upper limit corresponding to the target process, it is determined that a file descriptor leak has occurred in the target process.
[0096] Typically, the kernel records a user open file table corresponding to each process (e.g., the files_struct structure in the kernel). The user open file table corresponding to the target process records the file descriptors of files opened or created by the target process. By counting the number of file descriptors in the user open file table corresponding to the process, the electronic device can calculate the number of file descriptors used by the target process in the current cycle.
[0097] As an example, the electronic device may detect whether a certain process has FD leakage through the above-mentioned FD leakage detection module.
[0098] As an example, an electronic device can collect data at intervals according to a sampling time interval threshold (e.g., 3 seconds) to determine whether there is FD leakage. As an example, within a sampling time interval threshold, the electronic device samples FDs according to the maximum number of FDs collected (e.g., 4096). That is, within a sampling time interval threshold, the electronic device can collect up to 4096 FDs and determine whether the collected FDs have leaked. For example, if the electronic device determines that the number of FDs of a certain type exceeds a certain threshold, it can be considered that the FD of that type has leaked.
[0099] Optionally, the electronic device may classify and sort the collected at least one FD, that is, determine the type of the at least one FD and the number of FDs of each type, and then sort the number of FDs of each type according to the number of FDs of each type.
[0100] Optionally, the electronic device may disable a sampling function of the electronic device when FD leakage is detected. Monitoring at least one FD includes monitoring when an application adds an FD and when an application releases the FD.
[0101] Step 402: When the electronic device detects that a file descriptor leak occurs in a target process, the electronic device captures the call stack of the target process to obtain location information of the file descriptor leak in the target process. The location information includes at least one or more call stacks of file descriptors. The call stack of the target process is used to locate the cause of the file descriptor leak in the target process.
[0102] As an example, when a file descriptor leak occurs in an electronic device, a call stack list may be formed by capturing the call stack of the leaked file descriptor. The call stack list includes one or more call stacks, where each call stack may correspond to a file descriptor.
[0103] For example, the call stack of a file descriptor is used to identify the cause of a file descriptor leak, such as whether it was caused by a problem in the system-level code or by an upper-level application failing to release the file descriptor in a timely manner. The call stack includes information about how the file descriptor was requested and / or released.
[0104] It is understandable that the call stack of one or more file descriptors of the target process is the file descriptor that the electronic device detects the occurrence of FD leakage.For example, the call stack of one or more file descriptors of the target process may be an FD that has been used for a long time and has not been closed.
[0105] By capturing the call stack of one or more file descriptors of the target process, backend developers can locate the specific code location of one or more file descriptor leaks through the call stack of the application and / or release of the call stack of one or more file descriptors of the target process. This is conducive to timely locating the FD leakage problem of the electronic device and improving the performance of the electronic device.
[0106] As an example, when the electronic device detects whether a file descriptor leak occurs in the target process, the electronic device can immediately perform a stack capture operation. When capturing the stack, the electronic device can capture a call stack of up to 4096 file descriptors each time.
[0107] For example, the electronic device may capture the file descriptors of the target process through the FD stack capture module to obtain a call stack of one or more file descriptors of the target process.
[0108] As an example, if a target process leaks a file descriptor, and the electronic device is capturing the target process's stack, the electronic device may terminate the capture if certain conditions are met. For example, if the number of the target process's file descriptors exceeds a certain threshold and / or the target process's file descriptor timeout duration exceeds a certain duration, the electronic device may determine that the certain conditions have been met and terminate the capture of the target process's file descriptors.
[0109] As an example, optionally, capturing the file descriptor stack of the target process can also obtain the location information of the FD leak in the target process. For example, the location information of the FD leak in the target process includes not only the FD call stack, but also: the type of leaked FD and the number of leaked FDs.
[0110] For example, when an electronic device uses an application to load images or read and write files, or when the upper layer calls system layer resources, these operations involve loading file descriptors. By capturing the call stack, it can be determined whether the FD leak is caused by a problem in the system layer code or by the upper layer application failing to release the file descriptor in time.
[0111] As an example, after the method provided in the embodiment of the present application captures the call stack of the target process, the electronic device may further output the call stack of one or more file descriptors of the target process.
[0112] For example, the electronic device may call an output function to output and display a call stack of one or more file descriptors of a target process.
[0113] Exemplarily, the output function may be a dump function.
[0114] In an embodiment of the present application, the electronic device monitors whether a file descriptor leak occurs in a target process in the electronic device; when a file descriptor leak occurs in the target process in the electronic device, the call stack of the target process is captured to obtain a call stack of one or more file descriptors of the target process, and the call stack information of the target process is used to locate the cause of the file descriptor leak in the target process (such as a specific code location). Therefore, a backend developer can use the call stack of the one or more file descriptors to locate the specific code location where the file descriptor leak occurs, which is conducive to timely locating the file descriptor leak problem of the electronic device and improving the performance of the electronic device.
[0115] In this embodiment, during the startup or operation of an application, it is necessary to close open files and, therefore, some file descriptors. Typically, the application requests the system to allocate a file descriptor for it through a file descriptor allocation function. After accessing the corresponding file, the application releases the allocated file descriptor through a file descriptor release function. Thus, the allocation and release of file descriptors can be determined by monitoring the application's calls to the file descriptor allocation function and the file descriptor release function.
[0116] During specific implementation, the hook mechanism can be used to track the calls of the application to the file descriptor allocation function and the file descriptor release function. When the application is hooked to call the file descriptor allocation function, the file descriptor allocated by the system to the application is recorded. In the embodiment of the present invention, in order to analyze the file descriptor allocation situation and detect whether there is a file descriptor allocation leak in the application, while recording the file descriptors allocated by the system to the application, it is also necessary to record the allocation path corresponding to the file descriptor allocated by the system. Preferably, the allocation path here can be the call stack corresponding to the file descriptor. When the application is hooked to call the file descriptor release function, the released file descriptor and its corresponding allocation path are deleted from the recorded data.
[0117] In a possible implementation of the present application, after the electronic device captures the call stack of one or more file descriptors, the electronic device can store the call stack of one or more file descriptors, that is, if the electronic device captures the call stack of file descriptor 1 and file descriptor 2, the electronic device stores the call stack of file descriptor 1 and the call stack of file descriptor 2. Of course, the electronic device can also aggregate the call stacks of one or more file descriptors before storing them, such as the electronic device aggregates the call stacks of multiple file descriptors so that the FD using the same call stack is used to share the call stack. For example, after the electronic device captures the call stack of file descriptor 1 and the call stack of file descriptor 2, it is determined that the call stacks of file descriptor 1 and file descriptor 2 are the same, then the electronic device can aggregate the call stacks of file descriptor 1 and file descriptor 2 so that file descriptor 1 and file descriptor 2 share the same call stack, thereby obtaining the call stacks corresponding to different file descriptor types, and the number of file descriptors corresponding to each file descriptor type. For example, after the call stacks of file descriptor 1 and file descriptor 2 are aggregated, file descriptor 1 and file descriptor 2 can be attributed to file descriptor type 1. Then the call stack corresponding to file descriptor type 1 is the call stack of file descriptor 1 and file descriptor 2, and the number of file descriptors corresponding to file descriptor type 1 is 2.
[0118] For example, when monitoring an application calling a file descriptor release function, it is determined that the electronic device is to delete / release a certain file descriptor, and therefore the electronic device can clear the call stack created for the file descriptor. For example, the electronic device deletes / releases a certain file descriptor from the call stack of each of the one or more captured file descriptors, so that after the clearing, the call stacks of each file descriptor stored in the electronic device are call stacks of file descriptors allocated by the system for the application and not released.
[0119] For another example, after the electronic device captures the call stack of the FD, it can aggregate the call stacks of each FD so that the FDs of the same call stack can be shared, that is, different FDs corresponding to the same call stack can share the same call stack. Therefore, when it is monitored that the application calls the file descriptor release function, it is determined that the electronic device wants to delete / release a certain file descriptor (for example, the first file descriptor). Therefore, the electronic device can determine whether the call stack of the first file descriptor already exists in the electronic device. In the case that the call stack of the first file descriptor exists in the electronic device, the electronic device first determines the type of file descriptor to which the first file descriptor belongs, and the number of FDs corresponding to the FD type. If the number of FDs corresponding to the FD type is 0 after the electronic device releases the first file descriptor, the electronic device clears the call stack of the first file descriptor. If the number of FDs corresponding to the FD type is not 0 after the electronic device releases the first file descriptor, the electronic device updates the number of FDs corresponding to the FD type after the electronic device releases the first file descriptor, such as reducing the number of FDs corresponding to the FD type by one.
[0120] For example, when the electronic device monitors that an application calls a file descriptor allocation function, the electronic device determines that the system allocates / creates a new file descriptor for the application (hereinafter referred to as the second file descriptor). When the electronic device detects that the system allocates / creates a new second file descriptor for the application, the electronic device can save the call stack of the second file descriptor.
[0121] For example, when the electronic device monitors that an application calls a file descriptor allocation function, the electronic device determines that the system allocates / creates a file descriptor for the application (hereinafter referred to as the second file descriptor). When the electronic device detects that the system allocates / creates a second file descriptor for the application, the electronic device can first determine whether a call stack for the second file descriptor already exists in the electronic device. If a call stack for the second file descriptor does not exist in the electronic device, the electronic device creates a call stack for the second file descriptor. If a call stack for the second file descriptor already exists in the electronic device, the electronic device first determines the type of file descriptor to which the second file descriptor belongs, and then updates the FD number corresponding to the FD type, such as adding one to the FD number corresponding to the FD type.
[0122] For example, if an electronic device captures the call stacks of 100 file descriptors and the 100 file descriptors use the same call stack, the electronic device can aggregate the call stacks of the 100 file descriptors so that the 100 file descriptors share the call stack. For example, the 100 file descriptors share call stack 1, and the type of the 100 file descriptors is file descriptor type 1, that is, the number of file descriptors corresponding to file descriptor type 1 is 100. If the electronic device detects that a certain file descriptor 1 is released, and the call stack of file descriptor 1 is the same as the call stack of the 100 file descriptors, the electronic device can update the number of FDs corresponding to file descriptor type 1 to 99 after releasing file descriptor 1. If the electronic device detects that a certain file descriptor 2 is created, and the call stack of file descriptor 2 is the same as the call stack of the 100 file descriptors, the electronic device can update the number of FDs corresponding to file descriptor type 1 to 101 after adding file descriptor 2. In this way, the FDs with the same call stack in the electronic device can share the call stack.
[0123] The above describes in detail an example of a method for locating leaked file descriptors provided in an embodiment of the present application. It is understandable that, in order to implement the above functions, the electronic device includes hardware and / or software modules corresponding to the execution of each function. Those skilled in the art should easily appreciate that, in combination with the units and algorithm steps of each example described in the embodiments disclosed herein, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in a hardware or computer software driven hardware manner depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application in combination with the embodiments, but such implementation should not be considered to be beyond the scope of this application.
[0124] The embodiment of the present application can divide the functional modules of the electronic device according to the above method example. For example, each function can be divided into various functional modules, such as an FD leakage detection module, an FD stack capture module, etc., or two or more functions can be integrated into one module. The above-mentioned integrated module can be implemented in the form of hardware or in the form of a software functional module. It should be noted that the division of modules in the embodiment of the present application is schematic and is only a logical function division. There may be other division methods in actual implementation.
[0125] It should be noted that all relevant contents of each step involved in the above method embodiment can be referred to the functional description of the corresponding functional module and will not be repeated here.
[0126] like Figure 5 As shown, an embodiment of the present application provides a device for locating a leaked file descriptor. The device for locating a leaked file descriptor may be an electronic device or a chip used in an electronic device. The device includes:
[0127] The FD leak detection module 501 is used to monitor whether a target process in the electronic device has a file descriptor leak. The FD stack capture module 502 is used to capture the call stack of the target process when a file descriptor leak is detected in the target process to obtain the call stack of one or more file descriptors of the target process. The call stack of the target process is used to locate the cause of the file descriptor leak in the target process.
[0128] In a possible embodiment of the present application, the device for locating leaked file descriptors provided in the embodiment of the present application may further include: a deletion module, configured to clear the call stack of the first file descriptor upon detecting that the first file descriptor of the target process is closed.
[0129] In a possible embodiment of the present application, the device for locating a leaked file descriptor provided in the embodiment of the present application may further include: a storage module, configured to save a call stack of the second file descriptor upon detecting an operation of creating the second file descriptor.
[0130] In a possible embodiment of the present application, the device for locating leaked file descriptors provided by the embodiment of the present application may also include: an aggregation module, used to aggregate one or more captured call stacks so that file descriptors using the same call stack share the call stack to obtain a call stack list, wherein the call stack list includes a call stack corresponding to each file descriptor type in one or more file descriptor types, and the number of file descriptors corresponding to each file descriptor type, wherein file descriptors with the same call stack belong to the same file descriptor type.
[0131] In a possible embodiment of the present application, the device for locating leaked file descriptors provided in the embodiment of the present application may also include: a judgment module, used to determine whether there is a call stack of the first file descriptor in the call stack list when detecting that the first file descriptor of the target process is closed; an update module, used to update the call stack list according to the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs after closing the first file descriptor, if the call stack of the first file descriptor exists in the call stack list.
[0132] In a possible embodiment of the present application, the apparatus for locating a leaked file descriptor provided in the embodiment of the present application, the deletion module is specifically configured to clear the call stack of the first file descriptor from the call stack list if, after closing the first file descriptor, the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs is 0;
[0133] The update module is specifically used to reduce the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs in the call stack list if the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs is greater than 0 after the first file descriptor is closed.
[0134] In a possible embodiment of the present application, the apparatus for locating a leaked file descriptor provided in the embodiment of the present application may further include: a determination module configured to, upon detecting an operation of creating a second file descriptor, determine whether there is a call stack in the call stack list that is identical to the call stack of the second file descriptor;
[0135] If the call stack of the second file descriptor does not exist in the call stack list, the creation module is used to create the call stack of the second file descriptor, and the storage module is used to save the call stack of the second file descriptor;
[0136] If the call stack of the second file descriptor exists in the call stack list, the updating module is specifically configured to increase the number of file descriptors corresponding to the file descriptor type to which the second file descriptor belongs in the call stack list.
[0137] When integrated, the electronic device may also include a processing module, a storage module, and a communication module. The processing module may be used to control and manage the operation of the electronic device. The storage module may be used to support the execution of program code and data stored in the electronic device. The communication module may be used to support communication between the electronic device and other devices.
[0138] The processing module may be a processor or a controller. It may implement or execute the various exemplary logic blocks, modules, and circuits described in conjunction with the disclosure of this application. The processor may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of digital signal processing (DSP) and a microprocessor, and so on. The storage module may be a memory. The communication module may specifically be a device that interacts with other electronic devices, such as a radio frequency circuit, a Bluetooth chip, or a Wi-Fi chip.
[0139] In one embodiment, when the processing module is a processor and the storage module is a memory, the electronic device involved in this embodiment may be a Figure 2 Device with the structure shown.
[0140] Figure 6 FIG. 6 is a schematic diagram of the structure of a chip 60 provided in an embodiment of the present application. The chip 60 includes one or more (including two) processors 610 and a communication interface 630 .
[0141] Optionally, the chip 60 further includes a memory 640, which may include a read-only memory and a random access memory, and provides operation instructions and data to the processor 610. A portion of the memory 640 may also include a non-volatile random access memory (NVRAM).
[0142] In some embodiments, the memory 640 stores the following elements, execution modules or data structures, or a subset thereof, or an extended set thereof.
[0143] In the embodiment of the present application, the corresponding operation is performed by calling the operation instruction stored in the memory 640 (the operation instruction may be stored in the operating system).
[0144] One possible implementation is that the structures of electronic devices are similar, and different devices may use different chips to implement their respective functions.
[0145] The processor 610 controls processing operations of any electronic device and may also be referred to as a central processing unit (CPU).
[0146] The memory 640 may include a read-only memory and a random access memory, and provides instructions and data to the processor 610. A portion of the memory 640 may also include NVRAM. For example, in an application, the memory 640, the communication interface 630, and the memory 640 are coupled together via a bus system 620, wherein the bus system 620 may include a power bus, a control bus, and a status signal bus in addition to a data bus. However, for the sake of clarity, the following description is omitted: Figure 6 Various buses are labeled as bus system 620 .
[0147] The methods disclosed in the above embodiments of the present application can be applied to or implemented by processor 610. Processor 610 may be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method can be completed by hardware integrated logic circuits in processor 610 or by software instructions. The above processor 610 can be a general-purpose processor, a digital signal processor (DSP), an ASIC, a field-programmable gate array (FPGA), or other programmable logic device, discrete gate or transistor logic device, or discrete hardware component. The methods, steps, and logic block diagrams disclosed in the embodiments of the present application can be implemented or executed. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the methods disclosed in the embodiments of the present application can be directly implemented and executed by a hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. The software module can be located in a storage medium mature in the art, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, etc. The storage medium is located in the memory 640 , and the processor 610 reads the information in the memory 640 and completes the steps of the above method in combination with its hardware.
[0148] In a possible implementation, taking the chip 60 applied to an electronic device as an example, the communication interface 630 is used to execute Figure 4 The steps of receiving and sending of the electronic device in the embodiment shown. The processor 610 is used to execute Figure 4 The steps of the process of the electronic device in the embodiment shown are shown.
[0149] An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored. When the computer program is executed by a processor, the processor executes the method for locating a leaked file descriptor of any of the above embodiments.
[0150] An embodiment of the present application also provides a computer program product. When the computer program product is run on a computer, it enables the computer to execute the above-mentioned related steps to implement the method of locating the leaked file descriptor in the above-mentioned embodiment.
[0151] In addition, an embodiment of the present application also provides a device for locating leaked file descriptors. This device can specifically be a chip, component or module. The device may include a connected processor and memory; wherein the memory is used to store computer-executable instructions. When the device is running, the processor can execute the computer-executable instructions stored in the memory to enable the chip to execute the method of locating leaked file descriptors in the above-mentioned method embodiments.
[0152] Among them, the electronic device, computer-readable storage medium, computer program product or chip provided in this embodiment are all used to execute the corresponding methods provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding methods provided above, and will not be repeated here.
[0153] Through the description of the above implementation methods, technical personnel in the relevant field can understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be distributed and completed by different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.
[0154] In the several embodiments provided in this application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of modules or units is only a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0155] Units described as separate components may or may not be physically separate, and components shown as units may be one physical unit or multiple physical units, that is, they may be located in one place or distributed in multiple places. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0156] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0157] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling a device (which can be a single-chip microcomputer, chip, etc.) or a processor (processor) to execute all or part of the steps of the various embodiments 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.
[0158] The above content is merely a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of the present application. Therefore, the scope of protection of the present application should be based on the scope of protection of the claims.
Claims
1. A method for locating leaked file descriptors, characterized in that: include: Monitor the target process in the electronic device for file descriptor leaks; When a file descriptor leak is detected in the target process, capturing a call stack of one or more file descriptors in the target process, wherein the call stack is used to locate a cause of the file descriptor leak in the target process; aggregating the captured one or more call stacks so that file descriptors using the same call stack share the call stack, to obtain a call stack list, wherein the call stack list includes a call stack corresponding to each file descriptor type of the one or more file descriptor types and the number of file descriptors corresponding to each file descriptor type, wherein the file descriptors having the same call stack belong to the same file descriptor type; After aggregating the captured one or more call stacks, upon detecting that the first file descriptor of the target process is closed, determining whether a call stack of the first file descriptor exists in the call stack list; In a case where a call stack of the first file descriptor exists in the call stack list, updating the call stack list according to the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs after closing the first file descriptor; After aggregating the captured one or more call stacks, upon detecting an operation of creating a second file descriptor, determining whether there is a call stack in the call stack list that is identical to the call stack of the second file descriptor; If the call stack of the second file descriptor does not exist in the call stack list, creating and saving the call stack of the second file descriptor; If a call stack of the second file descriptor exists in the call stack list, increasing the number of file descriptors corresponding to the file descriptor type to which the second file descriptor belongs in the call stack list; Based on the call stack list, location information of a file descriptor leak occurring in the call stack of one or more file descriptors in the target process is obtained.
2. The method according to claim 1, characterized in that In the case where a file descriptor leak is detected in the electronic device, after capturing a call stack of one or more file descriptors in the target process, the method further includes: When it is detected that the first file descriptor of the target process is closed, the call stack of the first file descriptor is cleared.
3. The method according to claim 1 or 2, characterized in that In the case where a file descriptor leak is detected in the electronic device, after capturing a call stack of one or more file descriptors in the target process, the method further includes: When an operation of creating the second file descriptor is detected, a call stack of the second file descriptor is saved.
4. The method according to claim 1, wherein The updating of the call stack list according to the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs after closing the first file descriptor includes: If, after closing the first file descriptor, the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs is 0, clearing the call stack of the first file descriptor from the call stack list; If, after closing the first file descriptor, the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs is greater than 0, the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs in the call stack list is reduced.
5. A device for locating a leaked file descriptor, characterized in that: include: FD leak detection module, used to monitor whether the target process in the electronic device has a file descriptor leak; The FD stack capture module is used to capture the call stacks of one or more file descriptors in the target process when a file descriptor leak is detected in the target process, and the call stack is used to locate the cause of the file descriptor leak in the target process; aggregate the captured one or more call stacks so that file descriptors using the same call stack share the call stack to obtain a call stack list, wherein the call stack list includes a call stack corresponding to each file descriptor type in one or more file descriptor types, and the number of file descriptors corresponding to each file descriptor type, wherein file descriptors with the same call stack belong to the same file descriptor type; after aggregating the captured one or more call stacks, when it is detected that the first file descriptor of the target process is closed, it is determined whether there is a call stack of the first file descriptor in the call stack list; In the case of a call stack of a file descriptor, the call stack list is updated according to the number of file descriptors corresponding to the file descriptor type to which the first file descriptor belongs after closing the first file descriptor; after aggregating one or more captured call stacks, when an operation of creating a second file descriptor is detected, it is determined whether there is a call stack identical to the call stack of the second file descriptor in the call stack list; if the call stack of the second file descriptor does not exist in the call stack list, the call stack of the second file descriptor is created and saved; if the call stack of the second file descriptor exists in the call stack list, the number of file descriptors corresponding to the file descriptor type to which the second file descriptor belongs is increased in the call stack list; based on the call stack list, the location information of the file descriptor leak occurring in the call stack of one or more file descriptors in the target process is obtained.
6. An electronic device, characterized in that: include: A processor is coupled to a memory, the memory is used to store a computer program, and when the processor calls the computer program, the electronic device executes the method according to any one of claims 1 to 4.
7. A computer-readable storage medium, characterized in that Used to store a computer program, wherein the computer program includes instructions for implementing the method according to any one of claims 1 to 4.
Citation Information
Patent Citations
File descriptor leakage monitoring method and device, equipment and storage medium
CN115905137A