Process lock holding detection method, electronic device and readable medium thereof

By creating a daemon process khungtaskd in the kernel, selecting non-equal lock processes in the same thread group, and saving process information, the problem of process waiting caused by long-term lock holding is solved, and the efficiency and accuracy of lock-holding process location are improved.

CN114625545BActive Publication Date: 2025-11-25HUAWEI TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202011457088.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-12-10
Publication Date
2025-11-25
Estimated Expiration
2040-12-10

AI Technical Summary

Technical Problem

In the Linux kernel, when a process holding a lock in a thread group holds the mmap_sem lock for an extended period, it causes other processes waiting for the lock to wait for a long time. The system log has limited capacity, making it difficult to accurately locate the lock-holding process and increasing the workload for developers in troubleshooting.

Method used

By creating a daemon process khungtaskd in the kernel, it traverses the processes in the system that are in an uninterruptible sleep state, selects non-equal lock processes in the same thread group, saves their process information, reduces the number of process stacks printed, and accurately locates the lock-holding process.

Benefits of technology

This significantly reduces the number of process stacks printed by the kernel, lowers the workload for developers, and improves the accuracy of locating lock-holding processes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114625545B_ABST
    Figure CN114625545B_ABST
Patent Text Reader

Abstract

The application relates to the computer field and discloses a process lock holding detection method, an electronic device and a readable medium. The process lock holding detection method comprises the following steps: determining that a first process of the electronic device is in a process state satisfying a system protection triggering condition, and the first process is waiting for using a read-write lock of shared memory of the electronic device; selecting a plurality of second processes from a thread group in which the first process is located, wherein the second processes are not waiting for the read-write lock and are in a lock holding process state; and saving process information of the plurality of second processes. Therefore, by using the process lock holding detection method, the number of process stacks printed by the final kernel is greatly reduced, the workload of the developer for troubleshooting is greatly reduced, and the lock holding process can be accurately positioned.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the field of computers, and in particular to a process lock holding detection method, an electronic device thereof, and a readable medium. BACKGROUND

[0002] The kernel is used to manage the processes, memory, device drivers, files, and network systems of a system, and determines the performance and stability of the system. For example, multiple processes share memory, and there is a competition problem. The kernel provides a read-write lock mechanism to protect the shared memory, that is, only a process holding the read-write lock can access the shared memory for read-write operation. In the Linux kernel, all processes in a thread group share a memory space, and the read-write lock for protecting the memory space of the thread group is called mmap_sem lock (memory map semaphore).

[0003] As shown in Figure 1 , multiple processes of a thread group are waiting for the mmap_sem lock (hereinafter referred to as lock waiting processes), and the lock waiting processes are in an uninterruptible sleep state (i.e., D state). A process holding the mmap_sem lock (hereinafter referred to as a lock holding process) is in an executable state (i.e., R state), and the lock holding process in the R state occupies the shared memory to perform read-write operation.

[0004] However, when the lock holding process in the thread group holds the mmap_sem lock for a long time, it can cause other lock waiting processes in the thread group to wait for the mmap_sem lock for a long time, that is, to be in the D state for a long time. If the lock waiting process is in the D state for more than a time threshold (for example, 90 seconds), the kernel prints the process stack of all processes in the system in the D state or R state, and then triggers the protection mechanism of the system (for example, shutdown and restart).

[0005] The developer needs to analyze the process stack of all processes in the D state or R state printed by the kernel in the system log, find the process that uniquely holds the mmap_sem lock, and then optimize the program code of the lock holding process to reduce the probability of downtime. However, there are hundreds of processes in the D state or R state in the system, and the capacity of the system log is limited, which brings great difficulty to the troubleshooting work of the developer. SUMMARY

[0006] The embodiments of the present application provide a process lock holding detection method, an electronic device thereof, and a readable medium. By using the process lock holding detection method of the present application, the number of process stacks finally printed by the kernel is greatly reduced, which greatly reduces the troubleshooting workload of the developer and is also beneficial to accurately positioning the lock holding process.

[0007] In a first aspect, a process lock holding detection method of an electronic device includes:

[0008] determining that a process state of a first process of the electronic device meets a system protection trigger condition, and that the first process is waiting for a read-write lock for using shared memory of the electronic device;

[0009] selecting a plurality of second processes from a thread group in which the first process is located, wherein the second processes are not waiting for the read-write lock and are in a lock-holding process state;

[0010] saving process information of the plurality of second processes.

[0011] In some embodiments of the present application, the kernel creates a daemon process khungtaskd for traversing the system to determine whether there is a first process in an uninterruptible sleep state.

[0012] In an implementation of the first aspect, a kernel used by the system of the electronic device is a Linux kernel.

[0013] In some embodiments of the present application, the Linux kernel at least includes a process management module, a memory management module, a virtual file system, and a network subsystem.

[0014] In an implementation of the first aspect, the system protection trigger condition includes:

[0015] a duration of the process being in the uninterruptible sleep state exceeds a predetermined time threshold.

[0016] In some embodiments of the present application, the predetermined time threshold can be set by a user or the system, for example, the predetermined time threshold is set to 90 seconds.

[0017] In an implementation of the first aspect, the and the first process is waiting for the read-write lock for using the shared memory of the electronic device is derived by:

[0018] traversing whether there is a structure body pointer of the first process in a wait lock chain table;

[0019] in a case where the structure body pointer of the first process is in the wait lock chain table, the first process is waiting for the read-write lock for using the shared memory of the electronic device.

[0020] In some embodiments of the present application, the kernel creates a wait lock chain table for saving related information of all processes waiting for the read-write lock, wherein the related information includes a structure body pointer of a process, and the structure body pointer is a first address of a structure body of the process.

[0021] Taking an mmap_sem lock as an example, when a process wants to access shared memory, it must first apply for an mmap_sem lock. When a wait lock process is in a wait lock queue (for example, a wait lock queue of the mmap_sem lock), the wait lock process is waiting for the read-write lock for using the shared memory of the electronic device. Figure 1The kernel adds the information of the lock-waiting process into the lock-waiting list.

[0022] In an implementation of the first aspect, selecting the second processes from the thread group in which the first process is located comprises:

[0023] traversing all processes of the first thread group based on the thread group identifier of the first process to select the second processes from the thread group of the first process.

[0024] In an implementation of the first aspect, the lock-holding process state comprises an uninterruptible sleep state and an executable state.

[0025] In some embodiments of the present application, the lock-holding process state comprises an uninterruptible sleep state (i.e., D state) and an executable state (i.e., R state), because when a process holds a lock, it can be in the R state and always occupies the shared memory space without releasing the read-write lock, or the process holds a lock and waits for another read-write lock in the D state. Therefore, the process in the R state or the D state can hold a lock.

[0026] In an implementation of the first aspect, the process information comprises a process stack, and the process stack comprises at least one of parameters, local variables and a return address of a function.

[0027] In an implementation of the first aspect, the read-write lock is a memory-mapped semaphore lock, which is used to protect the shared memory of the electronic device.

[0028] The memory-mapped semaphore lock divides the processes accessing the shared memory space into readers and writers, the readers only perform read operations on the shared memory space, and the writers only perform write operations on the shared memory space. The memory-mapped semaphore lock allows multiple readers to access the shared memory space at the same time, but only one writer can access the shared memory space, that is, the memory-mapped semaphore lock can only have one writer or multiple readers at the same time, but cannot have both readers and writers at the same time.

[0029] The second aspect relates to an electronic device, comprising:

[0030] a memory storing instructions;

[0031] a processor coupled with the memory, when the program instructions stored in the memory are executed by the processor, the electronic device performs the process lock detection method of the first aspect.

[0032] The third aspect relates to a readable medium, the readable medium stores instructions, when the instructions run on the readable medium, the readable medium performs the process lock detection method of the first aspect.

[0033] In a fourth aspect, a computer program product includes computer programs / instructions which, when executed by a processor, implement the process lock detection method of the first aspect.

[0034] In a fifth aspect, an apparatus is included in an electronic device, and the apparatus has functions to implement the behaviors of the electronic device in the above aspects and possible implementation manners of the above aspects. The functions can be implemented by hardware, or by hardware executing corresponding software. The hardware or software includes one or more modules or units corresponding to the above functions. For example, a communication connection unit or module (which can be a Bluetooth or WIFI module), a display parameter acquisition unit or module, a display interface generation unit or module, and a multi-screen cooperation unit or module (such as a processor having the functions of the module or unit), etc. For example, the communication connection unit or module is configured to support a first electronic device and a second electronic device in a plurality of electronic devices to establish a multi-screen cooperation connection; the display parameter acquisition unit or module is configured to support the first electronic device to receive second display parameters from the second electronic device; the display interface generation unit or module is configured to support the first electronic device to generate a third interface according to the second display parameters received by the display parameter acquisition unit or module, and the third interface includes first content of a first interface of the first electronic device, and the first content has the second display parameters. The multi-screen cooperation unit or module is configured to support the first electronic device to send the third interface to the second electronic device; and the second electronic device displays a fourth interface in response to the received third interface, and the fourth interface includes second content and a first window, the first window includes the first content, and the first content in the first window has the second display parameters. BRIEF DESCRIPTION OF DRAWINGS

[0035] Figure 1 According to some embodiments of the present application, a process lock diagram is provided.

[0036] Figure 2 According to some embodiments of the present application, an application scenario diagram of a process lock detection technical solution is provided.

[0037] Figure 3 According to some embodiments of the present application, a hardware structure block diagram of a mobile phone 100 is provided.

[0038] Figure 4 According to some embodiments of the present application, a software structure block diagram of a mobile phone 100 is provided.

[0039] Figure 5 According to some embodiments of the present application, a process structure diagram is provided.

[0040] Figure 6 According to some embodiments of the present application, a shared memory diagram is provided.

[0041] Figure 7 According to some embodiments of the present application, a flowchart of a process lock holding detection method is provided.

[0042] Figure 8 According to some embodiments of the present application, a one-way linked list waiting for a mmap_sem lock is provided.

[0043] Figure 9 According to some embodiments of the present application, a schematic diagram of a thread group 900 is provided.

[0044] Figure 10 According to some embodiments of the present application, a flowchart of another process lock holding detection method is provided. DETAILED DESCRIPTION

[0045] The technical solutions in the embodiments of the present application will be described in detail below with reference to the accompanying drawings.

[0046] Figure 2 An application scenario diagram of the process lock holding detection technical solution provided by the embodiments of the present application is shown.

[0047] As shown in Figure 2 The screen interface 101 of the electronic device 100 has multiple application programs (application, APP), and the user clicks the APP, and the electronic device 100 creates a corresponding application process, which can implement various functions of the corresponding APP. For example, the user clicks the instant messaging APP, and the electronic device creates a process of the instant messaging APP, and the user can realize the chat functions such as voice sending and phone calling through the process of the instant messaging APP. However, when the process of the instant messaging APP needs to wait for a lock because of accessing a shared memory, if the waiting time for the lock exceeds a system set threshold value, the instant messaging APP will be stuck, as shown in FIG. 102. At this time, the electronic device 100 solves the problem of the stuck instant messaging APP through an internal system protection mechanism, for example, as shown in FIG. 103, the electronic device 103 is shut down and restarted.

[0048] In some embodiments of the present application, before the electronic device 100 is restarted, the kernel of the electronic device 100 does not print the process stacks of all processes in the D state or the R state in the system, and save them in the system log of the electronic device 100, and then provide the developer with the holding process whose holding time exceeds the time threshold, but prints the log including the process stack of the holding process in the following way:

[0049] First, the kernel of the electronic device 100 judges whether the deadlocked process is waiting for a lock. If it is determined that the deadlocked process is waiting for a lock, taking the mmap_sem lock as an example, if it is determined that the deadlocked process is waiting for the mmap_sem lock, it indicates that the lock-holding process and the deadlocked process waiting for a lock belong to the same thread group.

[0050] Then, a process that is not waiting for a lock and is in the D state or the R state is selected from other processes in the thread group in which the deadlocked process waiting for a lock is located, and the process stack log including the lock-holding process is printed, wherein printing the process stack is to export the relevant data of the process. Because only the processes in the D state and the R state can hold a lock, if the process waiting for a lock does not hold a lock, the processes in the deadlocked thread group that are not waiting for a lock and are in the D state and the R state can all be lock-holding processes. This is because when a process holds the mmap_sem lock, it can be in the R state and continuously occupy the shared memory space without releasing the mmap_sem lock, or the process holding the mmap_sem lock is waiting for another read-write lock that is not the mmap_sem lock and is in the D state. As can be seen, the processes in the R state or the D state can all hold the mmap_sem lock.

[0051] As can be seen, compared with the prior art in which all processes in the D state or the R state in the system are printed into a log, the technical solution of the present application only prints the processes in the D state or the R state that are not waiting for a lock in the thread group in which the deadlocked process is located, and the number of processes printed by the kernel is greatly reduced, which greatly reduces the workload of the developer in troubleshooting and is also conducive to accurately locating the lock-holding process.

[0052] It should be noted that if the process waiting for a lock holds a lock, the system is in a deadlock state, and the prior art can solve the deadlock problem, and the embodiments of the present application do not repeat the description.

[0053] It should be noted that in the present application, a thread is regarded as a light-weight process, all threads are implemented as processes, and the processes and threads are uniformly described as processes in the present application.

[0054] It can be understood that the electronic device 100 includes but is not limited to a mobile phone, a tablet computer, a notebook computer, a desktop computer, a vehicle-mounted electronic device, a palm computer, a personal digital assistant (PDA), a smart television, and any electronic device installed with a system. For ease of description, the technical solution of the present application is described below with reference to a mobile phone 100.

[0055] Figure 3 According to the embodiments of the present application, a method for implementing Figure 2 A structural block diagram of the mobile phone 100 capable of implementing the functions of the electronic device 100 is shown. Specifically, as shown in Figure 3As shown, the mobile phone 100 can include a processor 110, a mobile communication module 120, a wireless communication module 121, an internal storage 130, an external storage interface 140, a display screen 150, an audio module 160, a sensor module 170, an input unit 180, and a power supply 190.

[0056] It can be understood that the structure shown by the embodiments of the present application does not constitute a specific limitation on the mobile phone 100. In other embodiments of the present application, the mobile phone 100 can include more or fewer components than shown, or combine certain components, or split certain components, or different component arrangements. The components shown can be implemented in hardware, software, or a combination of software and hardware.

[0057] The processor 110 can include one or more processing units, for example: the processor 110 can include an application processor (AP), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, a video codec, a digital signal processor (DSP), a DPU (data processing unit), a baseband processor, and / or a neural-network processing unit (NPU), etc. Among them, different processing units can be independent devices, or can be integrated in one or more processors.

[0058] The processor 110 can 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 can save instructions or data that the processor 110 has just used or repeatedly uses. If the processor 110 needs to use the instructions or data again, it can be directly called from the memory. Avoiding repeated access reduces the waiting time of the processor 110, thus improving the efficiency of the system. In addition, in some embodiments of the present application, the processor 110 can be used to execute the process lock detection technical solutions of the present application.

[0059] In some embodiments of the present application, the processor 110 can include one or more interfaces. The interfaces can 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.

[0060] It can be understood that the interface connection relationship between the modules shown in the embodiments of the present application is only illustrative and does not constitute a structural limitation of the mobile phone 100. In some other embodiments of the present application, the mobile phone 100 can also use different interface connection modes or a combination of multiple interface connection modes in the above embodiments.

[0061] The mobile communication module 120 can be used for receiving and sending signals in the process of information or call transmission. In particular, after receiving the downlink information from the base station, the mobile communication module 120 delivers the information to the one or more processors 110 for processing. In addition, the mobile communication module 120 sends the data related to the uplink to the base station. Generally, the mobile communication module 120 includes, but is not limited to, an antenna, at least one amplifier, a tuner, one or more oscillators, a Subscriber Identity Module (SIM) card, a transceiver, a coupler, an LNA (Low Noise Amplifier), a duplexer, etc. In addition, the mobile communication module 120 can communicate with a network and other devices through wireless communication. The wireless communication can use any communication standards or protocols, including but not limited to GSM (Global System for Mobile communication), GPRS (General Packet Radio Service), CDMA (Code Division Multiple Access), CDMA2000 (Wideband Code Division Multiple Access), WCDMA (Wideband Code Division Multiple Access), LTE (Long Term Evolution), email, SMS (Short Messaging Service), etc.

[0062] The wireless communication module 121 can provide solutions for wireless communication, 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) technology, and the like, which are applied to the mobile phone 100. The wireless communication module 121 can be one or more devices that integrate at least one communication processing module. The wireless communication module 121 receives electromagnetic waves via an antenna, frequency-modulates and filters the electromagnetic wave signals, and sends the processed signals to the processor 110. The wireless communication module 121 can also receive signals to be sent from the processor 110, frequency-modulate them, amplify them, and radiate them as electromagnetic waves via an antenna. In some embodiments, the wireless communication module 121 can implement the multicarrier technology of the Wi-Fi network-based communication protocol described above, thereby supporting the mobile phone 100 to establish a communication connection with the mail server 300 through an existing Wi-Fi.

[0063] The internal memory 130 can be used to store computer executable program codes, including instructions. The internal memory 130 can include a program storage area and a data storage area. The program storage area can store systems, application programs required by at least one function (such as a sound playing function, an image playing function, etc.), and the like. The data storage area can store data created during use of the mobile phone 100 (such as audio data, a phone book, etc.), and the like. In addition, the internal memory 130 can include a random access memory, and can also include a non-volatile memory, such as at least one magnetic disk storage device, a flash memory device, a universal flash storage (UFS), and the like. In some embodiments of the present application, the processor 110 processes various functions and data by running instructions stored in the internal memory 130 and / or instructions stored in a memory disposed in the processor.

[0064] In some embodiments of the present application, the internal memory 130 can be a random access memory (RAM) used to store the structure of a process and a linked list, for example, task_struct of a process of each application installed on the mobile phone 100, and a one-way linked list of all memap_sem locks.

[0065] In some embodiments of the present application, the internal memory 130 can be a non-volatile memory for storing applications and the like, after the user clicks on the application, the application is loaded into the RAM to become a process, and the processor 110 can run the process in the RAM.

[0066] The external memory interface 240 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the mobile phone 100. The external memory card communicates with the processor 110 through the external memory interface 240 to realize the data storage function.

[0067] The display screen 150 can be a liquid crystal display (LCD), an organic light-emitting diode (OLED) display, an active-matrix organic light emitting diode (AMOLED) display, a flexible light-emitting diode (FLED) display, a quantum dot light-emitting diode (QLED) display, etc.

[0068] The mobile phone 100 also includes an audio module 160, which can include a speaker, a receiver, a microphone, a headset interface, and an application processor to realize audio functions such as music playback, recording, etc.

[0069] The audio module 160 is used to convert digital audio information into analog audio signals for output, and is also used to convert analog audio input into digital audio signals. The audio module 160 can also be used to encode and decode audio signals. In some embodiments, the audio module 160 can be disposed in the processor 110, or some of the functional modules of the audio module 160 can be disposed in the processor 110.

[0070] The mobile phone 100 also includes a sensor module 170, which can include a pressure sensor, a gyroscope sensor, a barometric pressure sensor, a magnetic sensor, an acceleration sensor, a distance sensor, a proximity light sensor, a fingerprint sensor, a temperature sensor, a touch sensor, an ambient light sensor, etc.

[0071] The input unit 180 can be used to receive input digital or character information, and to generate keyboard, mouse, joystick, optical or trackball signal inputs related to user settings and function control.

[0072] The mobile phone 100 also includes a power supply 190, such as a battery, for powering the various components of the mobile phone 100. In preferred embodiments, power supply 190 is logically connected to processor 110 through a power management system, which enables functions such as managing charging, discharging, and power consumption management. Power supply 190 can also include one or more DC or AC power sources, recharging system, power failure detection circuitry, power converter or inverter, power status indicator, and the like.

[0073] Referring now to the drawings Figure 4 , the software system of the mobile phone 100 can employ a layered architecture, an event-driven architecture, a microkernel architecture, a microservices architecture, or a cloud architecture. Embodiments of the present application will be described with respect to a layered architecture, such as an Android system, to illustrate the software structure of the terminal device. Figure 4 is a block diagram of the software structure of the mobile phone 100 of embodiments of the present application.

[0074] A layered architecture divides software into a number of layers, each with a clear role and division of labor. Layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into four layers, from top to bottom, an application layer, an application framework layer, an Android runtime and system library, and a kernel layer.

[0075] The application layer can include a series of application packages.

[0076] As shown in Figure 4 , the application packages can include applications such as phone, camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, short message, and the like.

[0077] The application framework layer provides application programming interfaces (APIs) and programming frameworks for the applications of the application layer. The application framework layer includes a number of pre-defined functions.

[0078] As shown in Figure 4 , the application framework layer can include a window manager, a content provider, a view system, a phone manager, a resource manager, a notification manager, and the like.

[0079] The window manager is used to manage window programs. The window manager can obtain the size of the display screen, determine whether there is a status bar, lock the screen, and take screenshots, and the like.

[0080] The content provider is used to store and obtain data, and to make the data accessible to applications. The data can include videos, images, audio, dialed and received calls, browsing history and bookmarks, phonebook, and the like.

[0081] The view system includes visual controls, such as controls that display text, controls that display pictures, and the like. The view system can be used to build an application. A display interface can be composed of one or more views. For example, a display interface that includes a short message notification icon can include a view that displays text and a view that displays a picture.

[0082] The phone manager is used to provide the communication function of the terminal device. For example, the management of the call state (including call connection, call hang-up, and the like).

[0083] The resource manager provides various resources for the application, such as localized strings, icons, pictures, layout files, video files, and the like.

[0084] The notification manager enables the application to display notification information in the status bar, which can be used to convey a notification type of message that can automatically disappear after a short stay without user interaction. For example, the notification manager is used to notify the completion of a download, a message reminder, and the like. The notification manager can also be a notification that appears in the top status bar of the system in the form of a chart or a scrolling text, such as a notification of an application running in the background, and can also be a notification that appears on the screen in the form of a dialog window. For example, the text information is prompted in the status bar, a prompt sound is emitted, the terminal device vibrates, the indicator light flashes, and the like.

[0085] The Android runtime includes the core library and the microkernel. The Android runtime is responsible for the scheduling and management of the Android system.

[0086] The core library includes two parts: one part is the function function that the java language needs to call, and the other part is the core library of Android.

[0087] The application layer and the application framework layer run in the instant messaging APP. The instant messaging APP executes the java file of the application layer and the application framework layer into a binary file. The instant messaging APP is used to execute the management of the object life cycle, the stack management, the thread management, the security and exception management, and the garbage collection, and the like.

[0088] The system library can include multiple functional modules. For example: the surface manager, the media library, the three-dimensional graphics processing library (for example: OpenGL ES), the 2D graphics engine (for example: SGL), and the like.

[0089] The surface manager is used to manage the display subsystem and provides the fusion of 2D and 3D layers for multiple applications.

[0090] The media library supports a variety of commonly used audio, video format playback and recording, and static image files, etc. The media library can support a variety of audio and video coding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc.

[0091] The three-dimensional graphics processing library is used to realize three-dimensional graphics drawing, image rendering, synthesis, and layer processing, etc.

[0092] The 2D graphics engine is a drawing engine for 2D drawing.

[0093] The kernel layer is a layer between hardware and software. The kernel layer at least includes a process management module, a memory management module, a virtual file system, and a network subsystem. The following introduces each module of the kernel layer, specifically including:

[0094] (1) Process scheduler (PS): responsible for managing central processing unit (CPU) resources so that each process can access the CPU in the most fair manner.

[0095] After the kernel detects that the user clicks the application, the application is loaded into the memory and becomes a process. The process can be regarded as an activity of the application. After the application becomes a process, the process management module creates a processing control block (PCB) for the process. In the Linux system, the processing control block is also called a task_struct structure. The process management module manages the process through the task_struct structure.

[0096] Each process has a task_struct structure, and the task_struct structure contains all the attribute information of the process.

[0097] As shown in Figure 5 , the task_struct structure includes a process identifier, a process state, a priority, a program counter, context data, I / O status information, accounting information, etc.

[0098] Process identifier (PID): also known as process control symbol, represents the identity of the process. As long as the kernel creates a process, it will automatically assign an identifier to the process. However, the PID is only temporarily unique. After the process is terminated, the PID is recycled by the system and may be assigned to another new process.

[0099] Process state: reflects the changes of the process execution process. These states are converted with the execution of the process and the changes of external conditions.

[0100] For example, in Linux system, there are 6 process states, which are: executable state (TASK_RUNNING), interruptible sleep state (TASK_INTERRUPTIBLE), uninterruptible sleep state (TASK_UNINTERRUPTIBLE), pause or trace state (TASK_STOPPED or TASK_TRACED), exit state (TASK_DEAD-EXIT_ZOMBLE), exit state (TASK_DEAD-EXIT_DEAD).

[0101] Executable state (TASK_RUNNING), simply R state, only the process in this state can be executed on the CPU.

[0102] Interruptible sleep state (TASK_INTERRUPTIBLE), simply S state, the process in this state will be woken up regularly.

[0103] Uninterruptible sleep state (TASK_UNINTERRUPTIBLE): simply D state, the process in this state is in sleep state and does not respond to asynchronous signals.

[0104] Pause or trace state (TASK_STOPPED or TASK_TRACED): simply T / t state, send a SIGSTOP signal to the process, it will enter the TASK_STOPPED state in response to the signal, when the process is being traced, it is in the special state of TASK_TRACED, "being traced" means that the process is paused and waits for the process that traces it to operate on it.

[0105] Exit state (TASK_DEAD-EXIT_ZOMBLE), simply Z state, the process becomes a zombie process.

[0106] Exit state (TASK_DEAD-EXIT_DEAD), simply X state, the process is about to be destroyed.

[0107] Priority: if multiple processes are being executed, the order in which the processes are executed is related to the priority of the processes.

[0108] Program counter: the address of the next instruction to be executed in the program.

[0109] Memory pointer: the pointer of the program code and the process related data.

[0110] Context data: the data in the registers of the processor when the process is executed.

[0111] I / O status information: including the displayed I / O request, the I / O device allocated to the process and the file list used by the process, etc.

[0112] Accounting information: including the total time of the processor, the accounting number, etc.

[0113] (2) Memory management module (MM): responsible for managing the memory (Memory) space so that each process can safely share the memory space of the electronic device 100. In addition, the memory management module will provide a virtual memory mechanism that allows a process to use more memory than the system can provide, and the unused memory will be saved in the external non-volatile memory through the virtual file system, and will be taken back to the system memory when needed.

[0114] For example, Figure 6 A schematic diagram of shared memory is shown, as Figure 6 Process A and process B map the address space (virtual address) to the physical address through the page table, and there is a common memory area in the physical address, that is, the shared memory, which is simultaneously available to both processes. Among them, the page table is responsible for mapping the virtual address of the process to the physical address.

[0115] For example, there are 100 processes in a thread group, and all processes in the thread group share the memory space. In order to protect the memory space of the thread group, the memory management module protects the shared memory space through mmap_sem lock, which divides the processes accessing the shared memory space into readers and writers. Readers only read the shared memory space, and writers only write the shared memory space. mmap_sem lock allows multiple readers to access the shared memory space at the same time, but only one writer can access the shared memory space, that is, mmap_sem lock can only have one writer or multiple readers at the same time, but cannot have both readers and writers at the same time.

[0116] (3) Virtual file system (VFS): the kernel abstracts different functional external devices, such as hard disk, magnetic disk, NAND Flash, Nor Flash, etc., input / output devices, display devices, etc., into a unified file operation interface (open, close, read, write, etc.) to access.

[0117] (4) Network subsystem (network): responsible for managing various network devices and implementing various network protocol stacks to ultimately implement the function of connecting other systems through the network.

[0118] The following is based on the above Figure 3 and Figure 4The structure, according to Figure 7 The technical solutions of the application are described in detail in combination with specific scenarios. As shown in Figure 7 The technical solutions of the application include the following steps:

[0119] 701: The process management module of the kernel detects whether there is a process in an un-interruptible sleep state (D state) in the system.

[0120] In some embodiments of the application, the kernel creates a daemon process khungtaskd in the system, which is used to traverse whether there is a process in the D state in the system.

[0121] If there is a process in the D state, step 702 is performed; otherwise, step 701 is returned.

[0122] 702: The process management module of the kernel judges whether the duration of the process in the un-interruptible sleep state exceeds a time threshold.

[0123] In some embodiments of the application, the time threshold can be set by a user or the system, for example, the time threshold is set to 90 seconds.

[0124] If the time threshold is exceeded, step 703 is performed; otherwise, step 701 is returned.

[0125] 703: The process management module of the kernel judges whether the process in the un-interruptible sleep state exceeding the time threshold is waiting for a read-write lock, i.e., whether it is a lock-waiting process.

[0126] It can be understood that if the process in the un-interruptible sleep state exceeding the time threshold is waiting for a lock, it means that the lock-holding process and the process in the un-interruptible sleep state exceeding the time threshold belong to the same thread group, then step 704 is performed, the kernel selects a non-lock-waiting process from the thread group in which the process exceeding the time threshold is located; otherwise, it means that the process in the un-interruptible sleep state exceeding the time threshold is not waiting for a lock, step 706 is performed, the kernel triggers a system protection mechanism, and the system is restarted.

[0127] In some embodiments of the application, the kernel first obtains the identifier of the process in the un-interruptible sleep state exceeding the time threshold, then obtains the structure pointer of the process according to the process identifier, and finally judges whether the process is waiting for a lock based on the structure pointer.

[0128] Specifically, taking WeChat TM as an example, when the kernel creates a WeChat process, the kernel assigns a unique PID to the WeChat process, wherein the value range of the PID is 0 to 32767. For example, the PID of the WeChat process is 31001.

[0129] Then, the kernel obtains the structure pointer of the WeChat process based on the WeChat process's PID. The structure pointer is the starting address of the process's structure stored in system memory. For example, if the starting address of the WeChat process's `task_struct` structure in system memory is 0x00000038, then the WeChat process's structure pointer is 0x00000038.

[0130] Finally, after the kernel obtains the structure pointer of the WeChat process, it determines whether the WeChat process is waiting for a lock based on the structure pointer.

[0131] Taking the mmap_sem lock as an example, when a process wants to access shared memory, it must first acquire the mmap_sem lock. This occurs while processes waiting for the lock are in the lock-waiting queue (e.g., ...). Figure 1 As shown in the diagram, the kernel will add the relevant information of the lock-equaling process to the lock-equaling list.

[0132] Figure 8 This illustrates a singly linked list waiting for the mmap_sem lock, such as... Figure 8 As shown, linked list 800 includes a head pointer 801, nodes 802, 803, 804, and 805. The head pointer 801 contains only pointer elements and no data elements, indicating the beginning of linked list 800. Each node corresponds to a process and includes data elements and pointer elements. The data elements include a pointer to a process structure, and the pointer elements indicate the next node to be accessed. For example, node 802 includes a data element a1 and a pointer element next. Data element a1 includes a pointer to a process structure waiting for the mmap_sem lock, and the pointer element next indicates that the next node to be accessed is node 803.

[0133] If a node in linked list 800 contains a structure pointer identical to the structure pointer of the WeChat process, it indicates that the WeChat process is waiting for the mmap_sem lock. For example, if the structure pointer of WeChat is 0x00000038, and data element a1 in node 802 of the linked list contains a structure pointer of 0x00000038, then node 802 corresponds to the WeChat process, meaning the WeChat process is waiting for the mmap_sem lock.

[0134] Understandable, Figure 8 The linked list shown contains 4 nodes, representing the storage of information for 4 processes. In fact... Figure 8 The number of nodes in the linked list shown can also be other numbers; this application embodiment does not limit the number of nodes in the equal-locked linked list. Furthermore, Figure 8 The linked list shown is a singly linked list. In other embodiments of this application, the linked list can also be a doubly linked list, a circular linked list, or a non-linear linked list, etc. The embodiments of this application do not limit the form of the linked list.

[0135] 704: The kernel's process management module selects non-locking processes from the thread group of lock-equaling processes that have exceeded the time threshold.

[0136] If any process in the thread group is waiting for the lock, it means that the process cannot hold the lock, and a 704 error is returned; otherwise, it means that the process may hold the lock, and a 705 error is returned.

[0137] like Figure 9 As shown, thread group 900 contains multiple threads: main thread t0, thread t1, thread t2, ..., thread tn. Each thread corresponds to a lightweight process; for example, main thread t0 corresponds to lightweight process P0, thread t1 corresponds to lightweight process P1, thread t2 corresponds to lightweight process P2, and thread tn corresponds to lightweight process Pn. These threads all share a common thread group identifier (TGID), which is the PID of the lightweight process corresponding to the main thread. For example, in thread group 900, if the PID of the lightweight process P0 corresponding to the main thread t0 is 'a', then the TGID of thread group 900 is 'a'.

[0138] Therefore, the kernel's process management module uses the WeChat process's TGID (e.g., TGID 31000) to find other processes in the same thread group as the WeChat process. It then checks each process in the thread group to see if it is waiting for a lock, thus selecting the non-lock-waiting process. The method for determining whether other processes in the same thread group are waiting for a lock is described in section 703 above and will not be repeated here.

[0139] 705: The kernel prints the process stacks of the non-equal-locked processes that are in an uninterruptible sleep state and an executable state, and saves them in the system log.

[0140] As is understandable, the process stack is a way of storing process data. It stores function parameters, local variables, register values ​​(used for register restoration), function return addresses, and data for structured exception handling. This data is organized in a specific order. The kernel prints the process stack, allowing developers to analyze why a process holding a lock is holding it for too long, and then optimize the code of that process to reduce the chance of crashes.

[0141] In some embodiments of this application, the kernel first traverses the process states of all processes in the same thread group that are not in the lock-equaling state. If the process state of the process is not in the D state or R state, it means that the process cannot hold the lock; otherwise, if the process state of the process is in the D state or R state, it means that the process may hold the lock. Then, the kernel prints the process stacks of all non-lock-equaling processes in the same process that are in the D state or R state and saves them in the system log of the mobile phone 100.

[0142] Reference Figure 8 and Figure 9 In the thread group 900, it is assumed that there are 100 processes, of which 90 processes are in the D state waiting for the mmap_sem lock, and the WeChat process is one of the processes waiting for the lock. The kernel detects that the time of the WeChat process in the D state exceeds the time threshold (90s), and the kernel 200 determines that the WeChat process is waiting for the lock. First, the other 99 processes in the same thread group as the WeChat process are screened out, and the processes not in the same thread group as the WeChat process are excluded. Then, it is checked whether the 99 processes are waiting for the lock. For example, 10 processes not waiting for the lock are screened out, and 89 processes waiting for the lock (excluding the WeChat process) are excluded. Finally, 5 processes in the D state or the R state not waiting for the lock are screened out, and 5 processes not in the D state or the R state not waiting for the lock are excluded.

[0143] As can be seen, the kernel determines that there are 5 processes that may hold the mmap_sem lock after three screenings, and then prints the process stacks of the 5 processes that may hold the mmap_sem lock. As a comparison, if the kernel determines that the processes in the D state exceed the time threshold (90s), the process stacks of the processes in the D state or the R state are printed, and the number of printed process stacks will be much more than the process lock detection method provided in the embodiments of the present application. For example, there are 1000 processes in the system in the D state or the R state, and the kernel will print the process stacks of the 1000 processes in the D state or the R state. Assuming that the system log can only save the process stacks of 200 processes, the system log will only save the process stacks of the last 200 processes among the process stacks of the 1000 processes, and the process stack of the process holding the lock may be located in the process stacks of the first 800 processes, so that the developer cannot find the process holding the lock.

[0144] In some embodiments of the present application, the kernel obtains 5 processes that may hold the mmap_sem lock after three screenings, and the kernel can only print the process stacks of the 5 processes in the system log. In addition, in order to fully utilize the log space of the system, the kernel can first print the process stacks of all 1000 processes in the system in the D state or the R state, and finally print the process stacks of the 5 processes screened out.

[0145] In addition, if only one process stack is finally screened out, the process must hold the mmap_sem lock, which causes the WeChat process to be stuck for more than the time threshold, so the process stack of the process holding the lock that is finally screened out can be reported to a commercial big data website. In this way, not only can the fact that the WeChat process is stuck be known, but also the reason why the WeChat process is stuck, i.e., the WeChat process waits for the mmap_sem lock for too long and is stuck, can be detected. The developer can optimize the code of the process holding the lock to reduce the problem of too long holding time of the lock leading to system restart.

[0146] 706: system restart.

[0147] In some embodiments of the present application, after the kernel prints the process stack, the kernel actively triggers the system protection mechanism, and the mobile phone 100 is shut down and restarted. In some other embodiments of the present application, when the kernel determines that the process in the non-interruptible sleep state exceeding the time threshold is not waiting for a lock, the kernel actively triggers the system protection mechanism, and the mobile phone 100 is shut down and restarted.

[0148] Another process lock holding detection technical solution provided by the embodiments of the present application is introduced as follows. Figure 10 As shown in FIG. 10, the process lock holding detection technical solution of the present application includes:

[0149] 1001: The daemon detects the process identifier of the process that needs to trigger the system protection mechanism.

[0150] Referring to the above embodiments, the condition for triggering the system protection mechanism is that the duration of the process in the D state exceeds the time threshold, for example, the time threshold is 90 seconds. The daemon detects the process in the D state exceeding the time threshold, and obtains the process identifier of the process.

[0151] 1002: The kernel obtains the process structure pointer through the process identifier. For details, refer to the description of 703 in FIG. 7, which will not be repeated here. Figure 7

[0152] 1003: The kernel determines whether the process is in the lock waiting list. For details, refer to the description of 703 in FIG. 7, which will not be repeated here. Figure 7

[0153] If the process is in the lock waiting list, it means that the lock holding process and the process belong to the same thread group, and then 1004 is executed; otherwise, 1009 is executed, and the system is restarted.

[0154] 1004: The kernel traverses all processes in the thread group to which the process belongs.

[0155] 1005: The kernel determines whether the process in the thread group is in the lock waiting list. For details, refer to the description of 703 in FIG. 7, which will not be repeated here. Figure 7

[0156] If the process in the thread group is in the lock waiting list, return to 1004 to continue traversing the next process in the thread group; otherwise, if the process is not in the lock waiting list, 1006 is executed.

[0157] 1006: The kernel determines whether the process is in the non-interruptible sleep state or the executable state. For details, refer to the description of 704 in FIG. 7, which will not be repeated here. Figure 7 ​​​​

[0158] If the process is not in D state or R state, return to execute 1004 to continue traversing the next process; otherwise, execute 1007.

[0159] 1007: print the process stack in D state or R state.

[0160] 1008: judge whether the processes of the thread group are traversed.

[0161] If not, return to execute 1004 until the processes of the thread group are traversed; otherwise, execute 1009.

[0162] 1009: system restart.

[0163] It can be understood that the numbers listed in the above embodiments are only exemplary, for example, the thread group has 100 processes, 90 processes are waiting for the mmap_sem lock, and the system has 1000 processes in D state or R state, and the numbers in these examples can also be other numbers, and the embodiments of the present application do not limit this.

[0164] It can be understood that the process lock holding detection method provided by the embodiments of the present application can be used to detect the process holding the mmap_sem lock, and in other embodiments of the present application, the process lock holding detection method can also be used for other read-write locks other than the mmap_sem lock, and the embodiments of the present application do not limit the type of read-write lock.

[0165] Reference in the specification to "some embodiments" or "an embodiment" means that a particular feature, structure, or characteristic described in connection with the embodiment is included in at least one example embodiment according to the present application. The appearance of the phrase "in one embodiment" in various places in the specification does not necessarily refer to the same embodiment.

[0166] The present application also relates to a device for performing the operations in the text. The device can be specially constructed for the required purpose or it can comprise a general purpose computer selectively activated or reconfigured by a computer program stored in the computer. Such a computer program can be stored in a computer readable medium, such as, but not limited to any type of disk including floppy disks, optical disks, CD-ROMs, magnetic-optical disks, read-only memories (ROMs), random access memories (RAMs), EPROMs, EEPROMs, magnetic or optical cards, application specific integrated circuits (ASICs) or any type of media suitable for storing electronic instructions, and each can be coupled to a computer system bus. Furthermore, the computers described in the specification can include a single processor or can be architectures involving multiple processors related by a bus.

[0167] The processes and displays presented herein are not inherently related to any particular computer or other apparatus. Various general purpose systems can be used with programs in accordance with the teachings herein, or it can prove convenient to construct more specialized apparatus to perform one or more method steps. The structure for a variety of these systems is discussed in the description below. In addition, any particular programming language can be used to implement the teachings of the present application as discussed herein. Various programming languages can be used, and each user can select and use the languages or languages deemed most suitable for the intended purposes.

[0168] Also, the language used in the specification has been principally selected for readability and instructional purposes and can not have been selected to delineate or circumscribe the disclosed subject matter. Accordingly, the present application is intended to be illustrative, and not limiting, of the scope of the concepts discussed herein.

Claims

1. A method for detecting process holding lock in an electronic device, characterized in that, include: It is determined that the process state of the first process of the electronic device meets the system protection triggering condition, and the first process is waiting to use the read-write lock of the shared memory of the electronic device; The system protection triggering conditions include: the duration of the process being in an uninterruptible sleep state exceeds a predetermined time threshold; Multiple second processes are selected from the thread group to which the first process is located, wherein the second processes are not waiting for the read-write lock and are in the lock-holding process state, the lock-holding process state including an uninterruptible sleep state and an executable state; Save the process information of the multiple second processes.

2. The method according to claim 1, characterized in that, The electronic device uses the Linux kernel.

3. The method according to claim 2, characterized in that, The first process obtains the information that it is waiting for a read / write lock to use the shared memory of the electronic device in the following way: Check if the structure pointer of the first process exists in the chain list; If a structure pointer of the first process exists in the lock list, the first process is waiting for a read-write lock to use the shared memory of the electronic device.

4. The method according to claim 2, characterized in that, The step of selecting multiple second processes from the thread group to which the first process belongs includes: Based on the thread group identifier of the first process, traverse all processes in the thread group and select multiple second processes from the thread group of the first process.

5. The method according to claim 2, characterized in that, The process information includes a process stack, which includes at least one of the following: function parameters, local variables, and function return addresses.

6. The method according to claim 2, characterized in that, The read-write lock is a memory-mapped semaphore lock, used to protect the shared memory of the electronic device.

7. An electronic device, characterized in that, include: Memory, which stores instructions; A processor coupled to a memory, wherein when program instructions stored in the memory are executed by the processor, the electronic device performs the process holding detection method according to any one of claims 1 to 6.

8. A readable medium storing instructions, characterized in that, When the instruction is executed on the readable medium, the readable medium causes the readable medium to perform the process holding lock detection method according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Method and device for deadlock detection

    CN103761182A

  • Method and system for processing deadlock cycle

    CN104239147A