WO2011079585A1 - 获取进程阻塞信息的方法及装置 - Google Patents

获取进程阻塞信息的方法及装置 Download PDF

Info

Publication number
WO2011079585A1
WO2011079585A1 PCT/CN2010/074211 CN2010074211W WO2011079585A1 WO 2011079585 A1 WO2011079585 A1 WO 2011079585A1 CN 2010074211 W CN2010074211 W CN 2010074211W WO 2011079585 A1 WO2011079585 A1 WO 2011079585A1
Authority
WO
WIPO (PCT)
Prior art keywords
monitored process
time
monitored
blocking
message
Prior art date
Application number
PCT/CN2010/074211
Other languages
English (en)
French (fr)
Inventor
邓立波
Original Assignee
腾讯科技(深圳)有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 腾讯科技(深圳)有限公司 filed Critical 腾讯科技(深圳)有限公司
Publication of WO2011079585A1 publication Critical patent/WO2011079585A1/zh

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/30Monitoring
    • G06F11/3065Monitoring arrangements determined by the means or processing involved in reporting the monitored data

Definitions

  • the present invention relates to the field of computers, and in particular, to a method and apparatus for acquiring process blocking information. Background of the invention
  • a process is an execution activity of an application on a computer.
  • Running an application is to start a process.
  • Processes can be divided into three states: running, blocking, and ready, and they are transformed with each other according to certain conditions, including: Ready into operation, operation into congestion, and blocking into ready.
  • the blocking of the process refers to the process waiting for a certain resource (such as waiting for input and output ports)
  • the (I/O, input/output) device is temporarily inoperable, and the process cannot use the processor even if the processor is idle.
  • the programmer can block the information according to the process and analyze the reason why the process is blocked.
  • the present invention provides a method and apparatus for acquiring process blocking information.
  • the technical solution is as follows:
  • a method for obtaining process blocking information comprising:
  • Receiving the monitored process Determining whether the monitored process is blocked by monitoring the process of sending and receiving messages by the monitored process in real time;
  • the blocking information of the monitored process is obtained in real time.
  • An apparatus for acquiring process blocking information comprising:
  • a receiving module configured to receive a monitored process
  • a first real-time monitoring module configured to determine whether the monitored process is blocked by monitoring a process of sending and receiving a message by the monitored process in real time
  • the real-time obtaining module is configured to acquire blocking information of the monitored process in real time if blocking occurs.
  • the process of sending and receiving messages by the monitored process is monitored in real time, and whether the monitored process is blocked is obtained. If the blocking occurs, the blocked information of the monitored process is obtained in real time. Therefore, the prior art cannot overcome the defect that the process cannot be blocked and the process blocking information cannot be obtained; in addition, the obtained blocking information can be used to analyze the reason why the control process is blocked.
  • FIG. 1 is a flowchart of a method for acquiring process blocking information according to Embodiment 1 of the present invention
  • FIG. 2 is a flow chart of a method for acquiring process blocking information according to Embodiment 2 of the present invention
  • FIG. 3 is a flow chart of a method for acquiring process blocking information according to Embodiment 3 of the present invention.
  • FIG. 4 is a schematic diagram of an apparatus for acquiring process blocking information according to Embodiment 4 of the present invention. Mode for carrying out the invention
  • FIG. 1 is a flow chart of a method for acquiring process blocking information according to Embodiment 1 of the present invention. As shown in FIG. 1, the method includes:
  • Step 101 Receive a monitored process selected by a user.
  • Step 102 The process of sending and receiving messages by the monitored process is monitored in real time, and it is determined whether the monitored process is blocked. If the blocking occurs, step 103 is performed;
  • Step 103 Obtain blocking information of the monitored process in real time.
  • the programmer can analyze the blocked information according to the obtained blocking information.
  • the method shown in FIG. 1 can be implemented by a computer.
  • the process of sending and receiving messages by the monitored process is monitored in real time to determine whether the monitored process is blocked. If the blocking occurs, the blocked information of the monitored process is obtained in real time; thereby overcoming the prior art cannot determine whether the process occurs. Blocking and failure to obtain process blocking information; In addition, the obtained blocking information can be used to analyze the cause of blocking of the monitored process.
  • FIG. 2 is a flowchart of a method for acquiring process blocking information according to Embodiment 2. As shown in FIG. 2, the method includes:
  • Step 201 Receive a process selected by the user that needs to be monitored.
  • GUI Graphical User Interface
  • Step 202 Obtain a handle (Handle) of the monitored process.
  • Each process has its own Handle. Only the Handle of the process can perform various operations on the process. Therefore, in this embodiment, the Handle of the monitored process needs to be acquired first, and then the Handle of the monitored process can be used. The operation of obtaining blocking information is performed in the monitoring process. In addition, the Handle of each process is stored in the operating system, so the stored Handle of the control process can be obtained from the operating system.
  • the Handle of the monitored process is acquired by calling a function that acquires a process handle.
  • the application runs through the process, the process includes multiple threads, and the process runs the application by using the thread division itself.
  • the application is divided into multiple dynamic link library (DLL) files, and the DLL file is the basic unit of the thread running the application, so the thread runs by calling and running each DLL file. application.
  • DLL dynamic link library
  • Step 203 Inject the DLL file of the storage monitor into the monitored process by using the Handle of the monitored process.
  • the memory of the DLL file of the storage monitor is allocated in the monitored process, and the full path of the DLL file is written into the allocated memory, and the DLL file is obtained according to the full path of the DLL file. Inject into the allocated memory.
  • the memory allocation function can be used to allocate memory for the DLL file of the storage monitor program by calling the memory allocation function, and the full path of the DLL file of the storage monitor program can be written into the allocated by calling the string write function.
  • the DLL file can be injected into the allocated memory by calling the DLL file load function.
  • Step 204 In the monitored process, create a monitoring thread, where the monitoring thread is used to run the injected monitoring program.
  • the monitoring thread is used to monitor the monitored process in real time; the monitoring thread runs through the monitoring The program monitors the monitored process in real time.
  • a monitoring thread can be created by calling a create remote thread function.
  • the monitoring thread can be created only by injecting the monitoring program into the monitored process. After the monitoring thread is created, the monitoring thread automatically runs the monitoring program to realize real-time monitoring of the monitored process.
  • Step 205 Monitor the thread in real time to monitor whether the monitored process is blocked. If the monitored process is blocked, go to step 206.
  • Step 206 Obtain blocking information of the monitored process in real time during the blocking process of the monitored process
  • the process of obtaining the blocking information each time is: obtaining the current time from the operating system, and performing the difference between the current time and the blocked start time of the record to obtain the current blocking time; acquiring the thread of each thread included in the monitored process Basic information (THREAD - BASIC-INFORMATION ), from each thread basic information, get the stack called by the thread and the range of the area the thread accesses in the stack, traverse each acquired stack, and get the range of the area in each stack.
  • the stored function returns the address, and the obtained current blocking time and function return address are used as blocking information of the monitored process, and the blocking information of the monitored process is stored.
  • each thread included in the monitored process is running a program, when a thread runs a statement to the calling function in the application, the thread calls a stack, and the address of the location of the statement is used as a function of the call.
  • the return address is pushed into the stack for temporary storage, and the thread also records the called stack and the location of the return address of the stored function in the stack in the thread basic information; when the thread executes the called function and needs to return the application
  • the function returns the address from the record in the basic information of the thread and the address returned by the function stored in the stack
  • the return address is returned to the application according to the function and continues to run.
  • the stack that is called by each thread included in the monitored process is the stack that is called by the monitored process.
  • the range of the area that each thread included in the monitored process accesses in the stack is the area of the area that the monitored process accesses in the stack.
  • the time is referred to as the system time SystemTime
  • the system time SystemTime is acquired.
  • the monitoring process does not occupy the CPU time IdleTime, according to the system time SystemTime and the control process does not occupy the CPU time IdleTime according to the following formula (1) to calculate the CPU idle rate of the monitored process CurrentCpuIdle;
  • the CPU usage of the monitored process is calculated according to the following formula (2).
  • the CPU usage of the monitored process can also be used as the blocking information of the monitored process.
  • CPU usage 100 - (CurrentCpuIdle * 100) / NumberOfProcessors ( 2 )
  • the system time SystemTime is set in advance for a period of time.
  • the system time SystemTime does not exceed the interval between any two adjacent blocking information.
  • NumberOfProcessors is the number of cores included in the CPU, and NumberOfProcessors is a constant.
  • the programmer can analyze the reason why the monitored process is blocked according to the function return address in the blocking information. Since the blocking information at different times is periodically acquired during the blocking of the monitored process, the programmer can analyze the reason why the monitored process is blocked at different times according to the function returning address at different times in the blocking information.
  • Step 207 When monitoring the blocked process of the monitored process, monitor the thread to monitor whether the monitored process receives the information in real time, and if the monitored process receives the message, it is monitored. When the blocking of the control process ends, the blocking information of the monitored process is immediately stopped and the blocked start time of the record is eliminated, and then the process returns to step 205.
  • step 206 and step 207 when monitoring the blocked process of the monitored process, step 206 and step 207 occur synchronously, and during the blocking of the entire monitored process, step 206 and step 207 may occur independently or synchronously with each other.
  • each process that has been started in the operating system may be used as a monitored process, and each monitored process is monitored in real time. When a monitored process is blocked, the monitored process is obtained. Blocking information.
  • the created monitoring thread is uninstalled from the monitored process, and the memory allocated for the monitoring program is reclaimed from the monitored process.
  • the monitoring thread can be unloaded from the monitored process by calling the unload dynamic link library function, and the allocated memory can be reclaimed by calling the memory release function.
  • the function for obtaining the process handle may be an OpenProcess function
  • the memory allocation function may be a VirtualAllocEx function
  • the string writing function may be a WriteProcessMemory function
  • the DLL file loading function may be LoadLibrary function
  • create remote thread function can be CreateRemoteThread function
  • unload dynamic link library function can be RemoteThread function and FreeLibrary function
  • memory release function can be VirtualFreeEx.
  • the method shown in Fig. 2 can be implemented by a computer.
  • the monitoring process is blocked in real time, and if the blocking occurs, the blocking information of the monitored process is obtained in real time; thereby overcoming the defect that the prior art cannot determine whether the process is blocked or not, and the process blocking information cannot be obtained.
  • the obtained blocking information can be used to analyze the cause of the blocking of the monitored process.
  • FIG. 3 is a flowchart of a method for acquiring blocking information according to Embodiment 3 of the present invention, such as As shown in Figure 3, the method includes:
  • Step 301 Receive a process selected by the user that needs to be monitored.
  • Step 302 Obtain a Handle of the monitored process.
  • Step 303 Create a monitoring thread in the monitored process by using the Handle of the monitored process, where the monitoring thread is used to monitor the monitored process in real time.
  • the monitoring thread can be created in the monitored process by calling the create remote thread function.
  • Step 304 Monitor the thread to monitor whether the monitored process sends a message in real time. If the monitored process sends a message, go to step 305.
  • Step 305 Determine whether the monitored process is blocked. Specifically, during the period from zero to the preset threshold, the monitored process receives the message in real time. If the monitored process does not receive the message, the monitored process is blocked, and step 306 is performed; if the monitoring process receives the message, If the monitored process does not block, go to step 307.
  • step 306 is performed; if the time exceeds the preset threshold, that is, at zero to pre- During the period of the set threshold, if the monitored process receives the message, the monitored process does not block, and step 307 is performed.
  • Step 306 The time when the time counted from the operating system exceeds the preset threshold is Blocking the start time, and recording the blocking start time, performing step 308;
  • Step 307 Clear the timed time and return to 304 to re-monitor in real time whether the monitored process sends a message.
  • Step 308 Obtain blocking information of the monitored process in real time during the blocking process of the monitored process.
  • the blocked information of the monitored process is obtained, and then the blocking information is obtained periodically at regular intervals.
  • the process of acquiring the blocking information is the same as that in the step 206 of the embodiment 2, and details are not described herein again.
  • the program can analyze the reason why the monitored process is blocked according to the function return address in the blocking information. Since the blocking information at different times is periodically acquired during the blocking of the monitored process, the programmer can analyze the reason why the monitored process is blocked at different times according to the function returning address at different times in the blocking information.
  • Step 309 When monitoring the blocked process, the monitoring thread monitors whether the monitored process receives information in real time. If the monitored process receives the message, the blocked process occurs, and the monitoring process stops immediately. The message is blocked and the blocked start time of the record is eliminated, and then returns to step 304.
  • step 308 and step 309 occur synchronously, and during the blocking of the entire monitored process, step 308 and step 309 may occur independently or synchronously with each other.
  • the method shown in Fig. 3 can be implemented by a computer.
  • the monitoring process is blocked in real time, and if the blocking occurs, the blocking information of the monitored process is obtained in real time; thereby overcoming the defect that the prior art cannot determine whether the process is blocked or not, and the process blocking information cannot be obtained.
  • the obtained blocking information can be used to analyze the reason why the control process is blocked.
  • Example 4 4 is a schematic diagram of an apparatus for acquiring process blocking information according to Embodiment 4 of the present invention, where the apparatus includes:
  • a receiving module 401 configured to receive a monitored process
  • the first real-time monitoring module 402 is configured to monitor, in real time, whether the monitored process is blocked by monitoring whether the monitored process sends a message or receives a message;
  • the real-time obtaining module 403 is configured to acquire blocking information of the monitored process in real time if the blocking occurs.
  • the first real-time monitoring module 402 specifically includes:
  • the real-time monitoring unit is configured to monitor whether the monitored process sends a message in real time.
  • the timing unit is configured to: if the monitored process sends a message, start timing from zero at the time of sending the message, and monitor whether the monitored process receives in real time.
  • a recording unit configured to: if the monitored time is within zero to a preset threshold, and the monitored process does not monitor the received message, the monitored process is blocked, and the time when the timed time exceeds the preset threshold is recorded as the blocking start.
  • the clearing unit is configured to clear the time of the timer if the time of the timer is within zero to a preset threshold and the monitored process receives the message.
  • the real-time acquisition module 403 specifically includes:
  • a first acquiring unit configured to acquire a current blocking time according to a current time and a blocking start time
  • a second obtaining unit configured to acquire a stack called by the monitored process and a range of areas accessed by the monitored process in the stack;
  • the third obtaining unit is configured to obtain a function return address stored in the area of the stack.
  • the real-time acquisition module further includes:
  • the fourth obtaining unit is configured to obtain the CPU usage of the monitored process according to the time when the monitored process does not occupy the CPU, and use the CPU usage of the monitored process as the blocking information of the monitored process.
  • the device also includes
  • a module is created for injecting a monitoring program into the monitored process, and a monitoring thread is created in the monitored process, and the monitoring thread is used to monitor the monitored process in real time.
  • the device also includes:
  • the second real-time monitoring module is configured to monitor whether the monitored process receives the message in real time if the monitored process is blocked. If the monitored process receives the message, stop acquiring the blocked information of the monitored process, and clear the blocking start time.
  • the apparatus shown in Fig. 4 may be a computer.
  • the process by monitoring the process of sending and receiving messages by the monitored process, determining whether the monitored process is blocked, for example, blocking occurs, and acquiring blocking information of the monitored process in real time; thereby overcoming the prior art, the process cannot be judged to be blocked. And the defect that the process blocking information cannot be obtained; in addition, the obtained blocking information can be used to analyze the reason why the monitored process is blocked.
  • All or part of the technical solutions provided by the above embodiments may be implemented by software programming, and the software programs are stored in a readable storage medium such as a hard disk, an optical disk or a floppy disk in a computer.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Quality & Reliability (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Description

获取进程阻塞信息的方法及装置
技术领域
本发明涉及计算机领域, 特别涉及一种获取进程阻塞信息的方法及 装置。 发明背景
进程是应用程序在计算机上的一次执行活动,运行一个应用程序 即为启动一个进程。 进程可以划分为运行、 阻塞、 就绪三种状态, 并随一定条件而相互转化, 包括: 就绪转化为运行, 运行转化为阻 塞, 阻塞转化为就绪。
其中, 进程的阻塞是指进程因等待某一资源 (如等待输入输出端口
( I/O, input/output )设备)而暂时不能运行的状态, 而此时即使处理机 空闲, 进程也无法使用处理机。 而程序员可以根据进程阻塞信息, 分析 出进程发生阻塞的原因。
在实现本发明的过程中, 发明人发现现有技术至少存在以下问题: 目前, 现有技术无法判断进程是否发生阻塞, 另外, 当操作系统中的 进程发生阻塞时, 现有技术无法获取进程的阻塞信息。 发明内容
为了能够判断进程是否发生阻塞以及当判断出进程发生阻塞时获取 进程的阻塞信息, 本发明提供了一种获取进程阻塞信息的方法及装置。 所述技术方案如下:
一种获取进程阻塞信息的方法, 所述方法包括:
接收被监控进程; 通过实时监控所述被监控进程收发消息的过程, 判断所述被监控进 程是否发生阻塞;
如果发生阻塞, 则实时获取所述被监控进程的阻塞信息。
一种获取进程阻塞信息的装置, 所述装置包括:
接收模块, 用于接收被监控进程;
第一实时监控模块, 用于通过实时监控所述被监控进程收发消息的 过程, 判断所述被监控进程是否发生阻塞;
实时获取模块, 用于如果发生阻塞, 则实时获取所述被监控进程的 阻塞信息。
由此可见, 在本发明实施例中, 通过实时监控被监控进程收发消息 的过程, 判断被监控进程是否发生阻塞, 如果发生阻塞, 则实时获取被 监控进程的阻塞信息。 从而克服了现有技术不能判断进程是否发生阻塞 以及不能获取进程阻塞信息的缺陷; 另外, 获取的阻塞信息可以用于分 析出 «控进程发生阻塞的原因。 附图简要说明
图 1是本发明实施例 1提供的一种获取进程阻塞信息的方法流程图; 图 2 是本发明实施例 2提供的一种获取进程阻塞信息的方法流程 图;
图 3 是本发明实施例 3 提供的一种获取进程阻塞信息的方法流程 图;
图 4 是本发明实施例 4提供的一种获取进程阻塞信息的装置示意 图。 实施本发明的方式
为使本发明的目的、 技术方案和优点更加清楚, 下面将结合附图和 具体实施例对本发明作进一步地详细描述。
实施例 1
图 1 是本发明实施例 1 提供的一种获取进程阻塞信息的方法流程 图, 如图 1所示, 该方法包括:
步骤 101: 接收用户选择的被监控进程;
步骤 102: 通过实时监控被监控进程收发消息的过程, 判断被监控 进程是否发生阻塞, 如果发生阻塞, 则执行步骤 103;
步骤 103: 实时获取被监控进程的阻塞信息。
其中, 程序员可以根据获取的阻塞信息分析出被监控进程发生阻塞 的原因。
在本发明实施例中, 可以由计算机实现图 1所示的方法。
在本发明实施例中, 通过实时监控被监控进程收发消息的过程, 判 断被监控进程是否发生阻塞, 如果发生阻塞, 实时获取被监控进程的阻 塞信息; 从而克服了现有技术不能判断进程是否发生阻塞以及不能获取 进程阻塞信息的缺陷; 另外, 获取的阻塞信息可以用于分析出被监控进 程发生阻塞的原因。
实施例 2
图 2是本实施例 2提供的一种获取进程阻塞信息的方法流程图, 如 图 2所示, 该方法包括:
步骤 201 : 接收用户选择的需要被监控的进程。
其中, 在操作系统中可以同时启动多个进程, 操作系统将应用程序 放入到一个进程中, 由该进程运行该应用程序。 将操作系统中的所有已 经启动的进程都放置在图形用户接口 ( GUI , Graphical User Interface ) 界面中, 用户从 GUI界面选择需要被监控的进程。
步骤 202: 获取被监控进程的句柄 ( Handle )。
其中, 每个进程都有自身的 Handle, 只有通过进程的 Handle才能 对进程进行各种操作, 所以在本实施例中需要先获取被监控进程的 Handle, 然后才能通过被监控进程的 Handle, 在被监控进程中执行获取 阻塞信息的操作。 另外, 操作系统中存储每个进程的 Handle, 所以可以 从操作系统中获取存储的«控进程的 Handle。
其中, 在本实施例中通过调用获取进程句柄的函数来获取被监控进 程的 Handle。 另外, 在操作系统中, 通过进程运行应用程序, 进程包括 多个线程, 进程利用其自身包括的线程分工运行应用程序。 在利用进程 运行应用程序时,应用程序被分成多个动态链接库(DLL, Dynamic Link Library )文件, 而 DLL文件为线程运行应用程序的基本单位, 所以线程 通过调用并运行各个 DLL文件来分工运行应用程序。
步骤 203: 通过被监控进程的 Handle, 将存储监控程序的 DLL文件 注入被监控进程中。
具体地, 通过被监控进程的 Handle, 在被监控进程中为存储监控程 序的 DLL文件分配内存, 将该 DLL文件的全路径写到分配的内存中, 根据该 DLL文件的全路径将该 DLL文件注入到分配的内存中。
其中, 在本实施例中可以通过调用内存分配函数在被监控进程中为 存储监控程序的 DLL文件分配内存,可以通过调用字符串写入函数将存 储监控程序的 DLL文件的全路径写入分配的内存中,可以通过调用 DLL 文件加载函数将该 DLL文件注入到分配的内存中。
步骤 204: 在被监控进程中, 创建监控线程, 其中, 该监控线程用 于运行注入的监控程序。
其中, 监控线程用于实时监控被监控进程; 监控线程通过运行监控 程序来实时监控被监控进程。 可以通过调用创建远程线程函数创建监控 线程。 另外, 只有将监控程序注入到被监控进程中, 才能创建监控线程, 创建完监控线程后, 监控线程自动地运行监控程序来实现实时监控被监 控进程。
步驟 205: 通过监控线程实时监控被监控进程是否发生阻塞, 如果 被监控进程发生阻塞, 则执行步骤 206。
步骤 206: 在被监控进程阻塞期间, 实时获取被监控进程的阻塞信 息;
其中, 当监控出被监控进程发生阻塞之时, 获取被监控进程的阻塞 信息, 然后定时的每隔一段时间获取一次阻塞信息。 而每次获取阻塞信 息的过程, 具体为: 从操作系统中, 获取当前时刻, 并将当前时刻与记 录的阻塞开始时刻做差运算得到当前阻塞时间; 获取被监控进程包括的 每个线程的线程基本信息 (THREAD -BASIC-INFORMATION ), 从每 个线程基本信息中, 获取线程调用的堆栈以及线程在该堆栈中访问的区 域范围, 遍历获取的每个堆栈, 并获取每个堆栈中的区域范围内存储的 函数返回地址, 将该获取的当前阻塞时间和函数返回地址作为被监控进 程的阻塞信息, 并存储被监控进程的阻塞信息。
其中, 被监控进程包括的每个线程在运行应用程序时, 当某个线程 运行到应用程序中的调用函数的语句时, 该线程调用一个堆栈, 将该语 句所在位置的地址作为调用的函数的返回地址压入到该堆栈中暂时存 储, 同时该线程也将调用的堆栈以及在该堆栈中存储函数返回地址的位 置记录在线程基本信息中; 当该线程执行完调用的函数并需要返回应用 程序时, 从线程的基本信息中记录的堆栈以及在该堆栈中存储的函数返 回地址的位置处读取函数返回地址, 根据该函数返回地址返回到应用程 序中继续运行。 其中, 被监控进程包括的每个线程调用的堆栈即为被监控进程调用 的堆栈, 被监控进程包括的每个线程在堆栈中访问的区域范围即为被监 控进程在堆栈中访问的区域范围。
进一步地, 在本实施例中, 当被监控进程阻塞时, 从每次开始获取 阻塞信息起的一段时间内, 为了便于说明将该段时间称为系统时间 SystemTime, 在系统时间 SystemTime内, 获取被监控进程未占用 CPU 的时间 IdleTime , 根据系统时间 SystemTime和 控进程未占用 CPU 的时间 IdleTime按如下的公式 (1 )计算出被监控进程的 CPU 空闲率 CurrentCpuIdle;
CurrentCpuIdle = dbldleTime I dbSystemTime ( 1 )
再根据被监控进程的 CPU空闲率按如下的公式 (2 )计算出被监控 进程的 CPU占用率, 还可以将被监控进程的 CPU占用率作为被监控进 程的阻塞信息。
CPU占用率 =100 - (CurrentCpuIdle * 100) / NumberOfProcessors ( 2 )
其中, 系统时间 SystemTime 是事先设置的一段时间, 系统时间 SystemTime 不超过任意相邻两次获取阻塞信息的 间 隔 , NumberOfProcessors为 CPU包括的内核数目, NumberOfProcessors是常 数。
其中, 程序员可以根据阻塞信息中的函数返回地址分析出被监控进 程发生阻塞的原因。 由于在被监控进程阻塞期间, 定时地获取不同时刻 的阻塞信息, 从而程序员可以根据阻塞信息中的不同时刻的函数返回地 址分析出被监控进程在不同时刻发生阻塞的原因。
步骤 207: 在监控出被监控进程发生阻塞时, 通过监控线程实时监 控被监控进程是否接收信息, 如果监控出被监控进程接收消息, 则被监 控进程发生的阻塞结束, 立即停止获取被监控进程的阻塞信息并消除记 录的阻塞开始时刻, 然后返回步骤 205。
其中, 在监控出被监控进程发生阻塞时, 步骤 206和步骤 207同步 发生, 并在整个被监控进程阻塞期间, 步骤 206和步骤 207可以相互独 立地发生或同步地发生。
其中, 在本实施例中还可以将操作系统中已经启动的每个进程作为 被监控进程, 并对每个被监控进程进行实时监控, 当某个被监控进程发 生阻塞时, 获取该被监控进程的阻塞信息。
其中, 在本实施例中, 当被监控进程结束时, 则将创建的监控线程 从被监控进程中卸载掉, 同时从被监控进程中将为监控程序分配的内存 收回。 其中, 可以通过调用卸载动态链接库函数将监控线程从被监控进 程中卸载掉, 可以通过调用内存释放函数将分配的内存收回。
其中, 在本实施例中, 如果操作系统为 Windows系统, 则获取进程 句柄的函数可以为 OpenProcess 函数、 内存分配函数可以为 VirtualAllocEx函数、 字符串写入函数可以为 WriteProcessMemory函数、 DLL文件加载函数可以为 LoadLibrary函数、 创建远程线程函数可以为 CreateRemoteThread函数、卸载动态链接库函数可以为 RemoteThread函 数和 FreeLibrary函数、 内存释放函数可以为 VirtualFreeEx。
在本发明实施例中, 可以由计算机实现图 2所示的方法。
在本发明实施例中, 通过实时监控被监控进程是否发生阻塞, 如果 发生阻塞, 实时获取被监控进程的阻塞信息; 从而克服了现有技术不能 判断进程是否发生阻塞以及不能获取进程阻塞信息的缺陷; 另外, 获取 的阻塞信息可以用于分析出被监控进程发生阻塞的原因。
实施例 3
图 3是本发明实施例 3提供的一种获取阻塞信息的方法流程图, 如 图 3所示, 该方法包括:
步骤 301 : 接收用户选择的需要被监控的进程。
其中, 将操作系统中的所有已经启动的进程都放置在 GUI界面中, 用户从 GUI界面选择需要被监控的进程。
步驟 302: 获取被监控进程的 Handle。
其中, 可以通过调用获取进程句柄的函数来获取被监控进程的 Handle
步驟 303: 通过被监控进程的 Handle, 在被监控进程中创建监控线 程, 其中, 监控线程用于实时监控被监控进程。
其中, 可以通过调用创建远程线程函数在被监控进程中创建监控线 程。
步骤 304: 通过监控线程实时监控被监控进程是否发送消息, 如果 监控出被监控进程发送消息, 则执行步骤 305。
步骤 305: 判断被监控进程是否发生阻塞。 具体地, 在零到预设的 阈值的这段时间内, 实时监控被监控进程是否接收消息, 如果被监控进 程没有接收消息, 则被监控进程发生阻塞, 执行步骤 306; 如果监控进 程接收消息, 则被监控进程没有发生阻塞, 则执行步骤 307。
具体地, 从被监控进程发送消息的时刻起, 从零开始计时, 实时判 断计时的时间是否超过预设的阈值和实时监控被监控进程是否接收消 息; 如果计时的时间超过预设的阈值, 并在零到预设的阈值的这段时间 之内没有监控出被监控进程接收消息, 则被监控进程发生阻塞, 执行步 骤 306; 如果在计时的时间超过预设的阈值之前, 即在零到预设的阈值 的这段时间之内, 监控出被监控进程接收消息, 则被监控进程没有发生 阻塞, 执行步骤 307。
步骤 306: 从操作系统中获取计时的时间超过预设的阈值的时刻为 阻塞开始时刻, 并记录阻塞开始时刻, 执行步骤 308;
步骤 307: 将计时的时间清零, 并返回 304, 重新实时监控被监控 进程是否发送消息。
步骤 308: 在被监控进程阻塞期间, 实时获取被监控进程的阻塞信 息。
其中, 当监控出被监控进程发生阻塞之时, 获取被监控进程的阻塞 信息, 然后定时的每隔一段时间获取一次阻塞信息。 而每次获取阻塞信 息的过程与实施例 2的步骤 206中的内容相同, 在此不再赘述。
其中, 程序 可以根据阻塞信息中的函数返回地址分析出被监控进 程发生阻塞的原因。 由于在被监控进程阻塞期间, 定时地获取不同时刻 的阻塞信息, 从而程序员可以根据阻塞信息中的不同时刻的函数返回地 址分析出被监控进程在不同时刻发生阻塞的原因。
步骤 309: 在监控出被监控进程发生阻塞时, 通过监控线程实时监 控被监控进程是否接收信息, 如果监控出被监控进程接收消息, 则被监 控进程发生的阻塞结束, 立即停止获取被监控进程的阻塞信息并消除记 录的阻塞开始时刻, 然后返回步骤 304。
其中, 在监控出被监控进程发生阻塞时, 步骤 308和步骤 309同步 发生, 并在整个被监控进程阻塞期间, 步骤 308和步骤 309可以相互独 立地发生或同步地发生。
在本发明实施例中, 可以由计算机实现图 3所示的方法。
在本发明实施例中, 通过实时监控被监控进程是否发生阻塞, 如果 发生阻塞, 实时获取被监控进程的阻塞信息; 从而克服了现有技术不能 判断进程是否发生阻塞以及不能获取进程阻塞信息的缺陷; 另外, 获取 的阻塞信息可以用于分析出«控进程发生阻塞的原因。
实施例 4 图 4 是本发明实施例 4 提供的一种获取进程阻塞信息的装置示意 图, 该装置包括:
接收模块 401 , 用于接收被监控进程;
第一实时监控模块 402, 用于通过监控被监控进程是否发送消息或 接收消息来实时监控被监控进程是否发生阻塞;
实时获取模块 403 , 用于如果发生阻塞, 则实时获取被监控进程的 阻塞信息。
其中, 第一实时监控模块 402具体包括:
实时监控单元, 用于实时监控被监控进程是否发送消息; 计时单元, 用于如果监控出被监控进程发送消息, 则在发送消息的 时刻起, 从零开始计时, 并实时监控被监控进程是否接收消息;
记录单元, 用于如果计时的时间在零到预设的阈值之内, 未监控出 被监控进程接收消息, 则被监控进程发生阻塞, 将计时的时间超过预设 的阈值的时刻记录为阻塞开始时刻;
清零单元, 用于如果计时的时间在零到预设的阈值之内, 监控出被 监控进程接收消息, 则将计时的时间清零。
实时获取模块 403具体包括:
第一获取单元, 用于根据当前时刻和阻塞开始时刻, 获取当前阻塞 时间;
第二获取单元, 用于获取被监控进程调用的堆栈以及被监控进程在 该堆栈中访问的区域范围;
第三获取单元, 用于获取该堆栈中的该区域范围内存储的函数返回 地址。
其中, 将获取的当前阻塞时间和获取的函数返回地址作为被监控进 程的阻塞信息。 所述实时获取模块还包括:
第四获取单元,用于根据被监控进程未占用 CPU的时间获取被监控 进程的 CPU占用率, 将被监控进程的 CPU占用率作为被监控进程的阻 塞信息。
该装置还包括
创建模块, 用于将监控程序注入被监控进程中, 在被监控进程中创 建监控线程, 该监控线程用于实时监控被监控进程。
该装置还包括:
第二实时监控模块, 用于如果被监控进程发生阻塞, 实时监控被监 控进程是否接收消息, 如果监控出被监控进程接收消息时, 停止获取被 监控进程的阻塞信息, 清除阻塞开始时刻。
在本实施例中, 图 4所示的装置可以是计算机。
在本发明实施例中, 通过监控被监控进程收发消息的过程, 判断被 监控进程是否发生阻塞, 如杲发生阻塞, 实时获取被监控进程的阻塞信 息; 从而克服了现有技术不能判断进程发生阻塞以及不能获取进程阻塞 信息的缺陷; 另外, 获取的阻塞信息可以用于分析出被监控进程发生阻 塞的原因。
以上实施例提供的技术方案中的全部或部分内容可以通过软件编 程实现, 其软件程序存储在可读取的存储介质中, 存储介质例如: 计算 机中的硬盘、 光盘或软盘。
以上所述仅为本发明的较佳实施例, 并不用以限制本发明, 凡在本 发明的精神和原则之内, 所作的任何修改、 等同替换、 改进等, 均应包 含在本发明的保护范围之内。

Claims

权利要求书
1、 一种获取进程阻塞信息的方法, 其特征在于, 所述方法包括: 接收被监控进程;
通过实时监控所述被监控进程收发消息的过程, 判断所述被监控进 程是否发生阻塞;
如果发生阻塞, 则实时获取所述被监控进程的阻塞信息。
2、 如权利要求 1 所述的方法, 其特征在于, 所述通过实时监控所 述被监控进程收发消息的过程, 判断所述被监控进程是否发生阻塞, 具 体包括:
实时监控所述被监控进程是否发送消息;
如果监控出所述被监控进程发送消息, 则在发送消息的时刻起, 从 零开始计时, 并实时监控所述被监控进程是否接收消息;
如果所述计时的时间在零到预设的阈值之内, 未监控出所述被监控 进程接收消息, 则所述被监控进程发生阻塞, 并将所述计时的时间超过 所述预设的阈值的时刻记录为阻塞开始时刻;
如果所述计时的时间在零到所述预设的阈值之内, 监控出所述被监 控进程接收消息, 则将所述计时的时间清零。
3、 如权利要求 2 所述的方法, 其特征在于, 所述实时获取所述被 监控进程的阻塞信息, 具体包括:
根据当前时刻和所述阻塞开始时刻, 获取当前阻塞时间; 获取所述被监控进程调用的堆栈以及所述被监控进程在所述堆栈 中访问的区域范围;
获取所述堆栈中的所述区域范围内存储的函数返回地址; 将所述当前阻塞时间和所述函数返回地址作为所述被监控进程的 阻塞信息。
4、 如权利要求 1 所述的方法, 其特征在于, 所述实时获取所述被 监控进程的阻塞信息, 具体包括:
根据所述被监控进程未占用 CPU 的时间获取所述被监控进程的 CPU 占用率, 将所述被监控进程的 CPU 占用率作为所述被监控进程的 阻塞信息。
5、 如权利要求 1 所述的方法, 其特征在于, 所述实时获取所述被 监控进程的阻塞信息, 具体包括:
根据当前时刻和所述阻塞开始时刻, 获取当前阻塞时间; 获取所述被监控进程调用的堆栈以及所述被监控进程在所述堆栈 中访问的区域范围;
获取所述堆栈中的所述区域范围内存储的函数返回地址; 根据所述被监控进程未占用 CPU 的时间获取所述被监控进程的
CPU占用率
将所述当前阻塞时间、所述函数返回地址和被监控进程的 CPU占用 率作为所述被监控进程的阻塞信息。
6、 如权利要求 1 所述的方法, 其特征在于, 在通过实时监控所述 被监控进程收发消息的过程, 判断所述被监控进程是否发生阻塞之前, 还包括:
将监控程序注入所述被监控进程中, 在所述被监控进程中创建监控 线程, 所述监控线程用于实时监控所述被监控进程。
7、 如权利要求 2 所述的方法, 其特征在于, 在所述被监控进程发 生阻塞之后, 所述方法还包括:
实时监控所述被监控进程是否接收消息 , 如果监控出所述被监控进 程接收消息, 停止获取所述被监控进程的阻塞信息, 清除所述阻塞开始 时刻。
8、 一种获取进程阻塞信息的装置, 其特征在于, 所述装置包括: 接收模块, 用于接收被监控进程;
第一实时监控模块, 用于通过实时监控所述被监控进程收发消息的 过程, 判断所述被监控进程是否发生阻塞;
实时获取模块, 用于如果发生阻塞, 则实时获取所述被监控进程的 阻塞信息。
9、 如权利要求 8 所述的装置, 其特征在于, 所述第一实时监控模 块具体包括:
实时监控单元, 用于实时监控所述被监控进程是否发送消息; 计时单元, 用于如果监控出所述被监控进程发送消息, 则在发送消 息的时刻起,从零开始计时,并实时监控所述被监控进程是否接收消息; 记录单元, 用于如果所述计时的时间在零到预设的阈值之内, 未监 控出所述被监控进程接收消息, 则所述被监控进程发生阻塞, 并将所述 计时的时间超过所述预设的阈值的时刻记录为阻塞开始时刻;
清零单元, 用于如果所述计时的时间在零到所述预设的阈值之内, 监控出所述被监控进程接收消息, 则将所述计时的时间清零。
10、 如权利要求 9所述的装置, 其特征在于, 所述实时获取模块具 体包括:
第一获取单元, 用于根据当前时刻和所述阻塞开始时刻, 获取当前 阻塞时间;
第二获取单元, 用于获取所述被监控进程调用的堆栈以及所述被监 控进程在所述堆栈中访问的区域范围;
第三获取单元, 用于获取所述堆栈中的所述区域范围内存储的函数 返回地址; 其中, 将所述当前阻塞时间和所述函数返回地址作为所述被监控进 程的阻塞信息。
11、 如权利要求 8所述的装置, 其特征在于, 所述实时获取模块包 括:
第四获取单元,用于根据所述被监控进程未占用 CPU的时间获取所 述被监控进程的 CPU占用率, 将所述被监控进程的 CPU占用率作为所 述«控进程的阻塞信息。
12、 如权利要求 8所述的装置, 其特征在于, 所述实时获取模块具 体包括:
第一获取单元, 用于根据当前时刻和所述阻塞开始时刻, 获取当前 阻塞时间;
第二获取单元, 用于获取所述被监控进程调用的堆栈以及所述被监 控进程在所述堆栈中访问的区域范围;
第三获取单元, 用于获取所述堆栈中的所述区域范围内存储的函数 返回地址;
第四获取单元,用于根据所述被监控进程未占用 CPU的时间获取所 述被监控进程的 CPU占用率;
将所述当前阻塞时间、所述函数返回地址和被监控进程的 CPU占用 率作为所述被监控进程的阻塞信息。
13、 如权利要求 8所述的装置, 其特征在于, 所述装置还包括: 创建模块, 用于将监控程序注入所述被监控进程中, 在所述被监控 进程中创建监控线程, 所述监控线程用于实时监控所述被监控进程。
14、 如权利要求 9所述的装置, 其特征在于, 所述装置还包括: 第二实时监控模块, 用于在所述被监控进程发生阻塞后, 实时监控 所述被监控进程是否接收消息, 如果监控出所述被监控进程接收消息 时, 停止获取所述被监控进程的阻塞信息, 清除所述阻塞开始时刻。
PCT/CN2010/074211 2009-12-31 2010-06-22 获取进程阻塞信息的方法及装置 WO2011079585A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN200910265799.6 2009-12-31
CN200910265799.6A CN102117240B (zh) 2009-12-31 2009-12-31 一种获取进程阻塞信息的方法及装置

Publications (1)

Publication Number Publication Date
WO2011079585A1 true WO2011079585A1 (zh) 2011-07-07

Family

ID=44216018

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2010/074211 WO2011079585A1 (zh) 2009-12-31 2010-06-22 获取进程阻塞信息的方法及装置

Country Status (2)

Country Link
CN (1) CN102117240B (zh)
WO (1) WO2011079585A1 (zh)

Families Citing this family (17)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
KR101396781B1 (ko) * 2013-01-08 2014-05-20 주식회사 한올테크놀로지 응용프로그램 관리장치 및 관리방법
CN104050030A (zh) * 2014-05-30 2014-09-17 汉柏科技有限公司 一种操作系统进程调度方法
CN104503912B (zh) * 2014-12-24 2017-07-11 中科创达软件股份有限公司 一种服务响应过程的监测方法及装置
CN106293979B (zh) * 2015-06-25 2019-11-15 伊姆西公司 检测进程无响应的方法和装置
CN107196817B (zh) * 2016-03-15 2020-07-14 中国移动通信集团河南有限公司 Ping线程监测方法、网络服务状态监测方法及客户端
CN107659425A (zh) * 2016-07-26 2018-02-02 腾讯科技(深圳)有限公司 一种控制方法及服务器、终端
CN106371809B (zh) * 2016-08-31 2019-03-01 北京奇虎科技有限公司 线程处理器及线程处理方法
CN108345524B (zh) * 2017-01-22 2021-12-14 腾讯科技(深圳)有限公司 应用程序监控方法及应用程序监控装置
CN107590057B (zh) * 2017-09-28 2021-06-15 努比亚技术有限公司 冻屏监测与解决方法、移动终端及计算机可读存储介质
CN107783858A (zh) * 2017-10-31 2018-03-09 努比亚技术有限公司 终端冻屏的解决方法、终端和计算机可读存储介质
CN107908478B (zh) * 2017-11-30 2021-11-16 努比亚技术有限公司 内存清理方法、移动终端及计算机可读存储介质
CN107967177B (zh) * 2017-11-30 2022-02-22 努比亚技术有限公司 基于核心进程的内存优化方法、移动终端及可读存储介质
CN109992425B (zh) * 2017-12-29 2022-03-15 Oppo广东移动通信有限公司 信息处理方法、装置、计算机设备和计算机可读存储介质
CN108920298B (zh) * 2018-07-16 2021-07-13 北京中科网威信息技术有限公司 一种基于申威架构的监控信号量的处理方法及装置
CN109117271B (zh) * 2018-08-10 2021-03-23 普联技术有限公司 自动调节cpu负载的方法、存储介质及终端设备
CN112667388B (zh) * 2021-03-16 2021-06-01 荣耀终端有限公司 一种操作系统的保护方法及电子设备
CN115658242B (zh) * 2022-10-21 2024-04-26 芯华章科技股份有限公司 逻辑系统设计的任务处理方法及电子设备

Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1506826A (zh) * 2002-12-09 2004-06-23 联想(北京)有限公司 计算机应用软件自纠错自重起方法
CN1605987A (zh) * 2004-11-17 2005-04-13 中兴通讯股份有限公司 一种多线程系统中实现实时监控各线程状态的方法
CN1859229A (zh) * 2005-10-26 2006-11-08 华为技术有限公司 网络处理器以及监控网络处理器的方法
CN1904852A (zh) * 2006-08-01 2007-01-31 西安西电捷通无线网络通信有限公司 一种计算机应用程序的监控及异常处理方法
CN101114253A (zh) * 2006-07-26 2008-01-30 腾讯科技(深圳)有限公司 一种程序崩溃信息上报的方法及系统
CN101178688A (zh) * 2007-11-29 2008-05-14 中兴通讯股份有限公司 系统任务的cpu占用率检测方法及系统
CN101408861A (zh) * 2007-10-11 2009-04-15 鸿富锦精密工业(深圳)有限公司 应用程序实时监控系统及方法

Patent Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1506826A (zh) * 2002-12-09 2004-06-23 联想(北京)有限公司 计算机应用软件自纠错自重起方法
CN1605987A (zh) * 2004-11-17 2005-04-13 中兴通讯股份有限公司 一种多线程系统中实现实时监控各线程状态的方法
CN1859229A (zh) * 2005-10-26 2006-11-08 华为技术有限公司 网络处理器以及监控网络处理器的方法
CN101114253A (zh) * 2006-07-26 2008-01-30 腾讯科技(深圳)有限公司 一种程序崩溃信息上报的方法及系统
CN1904852A (zh) * 2006-08-01 2007-01-31 西安西电捷通无线网络通信有限公司 一种计算机应用程序的监控及异常处理方法
CN101408861A (zh) * 2007-10-11 2009-04-15 鸿富锦精密工业(深圳)有限公司 应用程序实时监控系统及方法
CN101178688A (zh) * 2007-11-29 2008-05-14 中兴通讯股份有限公司 系统任务的cpu占用率检测方法及系统

Also Published As

Publication number Publication date
CN102117240B (zh) 2016-01-20
CN102117240A (zh) 2011-07-06

Similar Documents

Publication Publication Date Title
WO2011079585A1 (zh) 获取进程阻塞信息的方法及装置
US6697972B1 (en) Method for monitoring fault of operating system and application program
CN102141947B (zh) 一种对采用嵌入式操作系统的计算机应用系统中异常任务的处理方法及系统
WO2014117653A1 (zh) 内存清理方法、装置和终端设备
JP5474982B2 (ja) 選択的に軽減を使用してエラーを低減するメモリー管理技術の有効性の評価
TW201737111A (zh) 分散式儲存系統硬碟掛住故障檢測、處理方法及裝置
US8601493B2 (en) Application controlling apparatus and storage medium which stores software for the apparatus
CN101887393B (zh) 基于半虚拟化技术的设备故障复现方法及系统
CN100524244C (zh) 网络存储系统的存储空间不足预警方法
CN109064327B (zh) 一种智能合约处理方法、装置及设备
US20090044198A1 (en) Method and Apparatus for Call Stack Sampling in a Data Processing System
CN103268277A (zh) 一种输出日志信息的方法及系统
WO2017156676A1 (zh) 一种针对应用的处理方法、装置及智能终端
KR20080104998A (ko) 저장 장치에 의한 내부 작동의 관리
US9176783B2 (en) Idle transitions sampling with execution context
US20230409206A1 (en) Systems and methods for ephemeral storage snapshotting
JP2004234557A (ja) データ管理方法、コントローラ、及びプログラム
CN102222036A (zh) 一种自动化测试方法和设备
CN102929761A (zh) 一种应对崩溃性错误的系统及方法
CN102426539A (zh) 一种定时器任务自动处理方法及系统
US20230385159A1 (en) Systems and methods for preventing data loss
JP4795388B2 (ja) 電子計算機装置及びソフトウェア処理遅延検出時のデータ保存方法
WO2009147738A1 (ja) 情報処理装置及びその制御方法並びにモニタプログラム
CN100489786C (zh) 状态机维护方法及装置
KR101507353B1 (ko) 모바일 디바이스의 메모리를 관리하는 방법, 서버 및 컴퓨터 판독 가능한 기록 매체

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 10840374

Country of ref document: EP

Kind code of ref document: A1

WWE Wipo information: entry into national phase

Ref document number: 4932/CHENP/2012

Country of ref document: IN

NENP Non-entry into the national phase

Ref country code: DE

32PN Ep: public notification in the ep bulletin as address of the adressee cannot be established

Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205 DATED 06.09.2012)

32PN Ep: public notification in the ep bulletin as address of the adressee cannot be established

Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205A DATED 291112)

122 Ep: pct application non-entry in european phase

Ref document number: 10840374

Country of ref document: EP

Kind code of ref document: A1