Method and device for determining utilization rate of thread central processing unit

The thread operation information is obtained through the operating system interface, the CPU usage rate is calculated and the thread name is output, which solves the problem of difficulty in determining thread CPU usage in the existing technology, and realizes resource saving and efficient thread monitoring.

CN119201584APending Publication Date: 2024-12-27BEIJING ZITIAO NETWORK TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202310760944.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-06-26
Publication Date
2024-12-27

AI Technical Summary

Technical Problem

The prior art is difficult to effectively determine the central processor usage of threads, affecting thread performance analysis, CPU resource management, multi-thread scheduling and fault diagnosis.

Method used

The operating system provides interfaces to obtain the running information of the target thread at different points in time, calculate its usage time, and determine the thread name and CPU usage based on the thread identification, and output the thread name and usage rate.

Benefits of technology

With fewer resources, real-time monitoring and output of thread CPU usage is realized, improving system performance and resource management efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119201584A_ABST
    Figure CN119201584A_ABST
Patent Text Reader

Abstract

The invention discloses a method for determining the utilization rate of a thread CPU, and the method comprises the steps: obtaining the first operation information of a target thread through a first interface provided by an operation system at a first moment, and the first operation information comprises the identification of the target thread and the first use time of a target limited practical CPU; and obtaining second operation information of the target thread through the first interface again at a second moment later than the first moment, wherein the second operation information comprises the identifier of the target thread and second use time of the CPU used by the target thread. And determining the CPU usage rate of the target thread according to the first usage time and the second usage time, and determining the thread name of the target thread according to the identifier of the target thread. And then, outputting the thread name of the target thread and the CPU utilization rate of the target thread. By means of the scheme, the CPU utilization rate of the target thread can be determined on the premise that few resources are used.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a method and device for determining a central processing unit usage rate of a thread. Background Art

[0002] At least one thread can run on a device, and each thread will occupy the device's central processing unit (CPU) during operation. It is particularly important to determine the CPU usage of a thread. For example, determining the CPU usage of a thread can analyze the performance of the thread; for another example, determining the CPU usage of a thread can help manage CPU resources; for another example, in a multi-threaded scenario, determining the CPU usage of a thread can help schedule the execution order of multiple threads; for another example, determining the CPU usage of a thread can help with fault diagnosis. Among them, "CPU usage of a thread" can also be referred to as "CPU usage of a thread", and the two can be used interchangeably.

[0003] How to determine the CPU usage of a thread is a problem that has yet to be solved. Summary of the invention

[0004] In order to solve or at least partially solve the above technical problems, an embodiment of the present application provides a method and device for determining the CPU usage of a thread.

[0005] In a first aspect, an embodiment of the present application provides a method for determining a CPU usage rate of a thread, the method comprising:

[0006] Acquire, at a first moment, first running information of a target thread through a first interface provided by an operating system, the first running information including an identifier of the target thread and a first usage time of a central processing unit used by the target thread;

[0007] acquiring, at a second time, second running information of the target thread through the first interface, the second running information including the identifier and a second usage time of the central processor used by the target thread, the second time being later than the first time;

[0008] Determine a CPU usage rate of a target thread according to the first usage time and the second usage time, and determine a thread name of the target thread according to an identifier of the target thread;

[0009] The thread name of the target thread and the CPU usage rate of the target thread are output.

[0010] Optionally, the first interface is an interface for obtaining thread status in an interface provided by a class used for source code debugging of the Android system.

[0011] Optionally, determining the thread name of the target thread according to the identifier of the target thread includes:

[0012] Using the first function to traverse the thread list, obtain the thread pointer corresponding to the target thread, the thread list including the thread pointer of the target thread;

[0013] The thread pointer of the target thread is used as an input parameter of the second function to obtain the thread name of the target thread.

[0014] Optionally, the first function is a loop function, and the second function is a thread name acquisition function.

[0015] Optional,

[0016] The acquiring first running information of the target thread through a first interface provided by the operating system at the first moment includes: acquiring first thread running information through the first interface at the first moment, the first thread running information including first running information corresponding to each thread in a plurality of threads, the plurality of threads including the target thread;

[0017] The acquiring the second running information of the target thread through the first interface at the second moment includes: acquiring the second thread running information through the first interface at the second moment, the second thread running information including the second running information corresponding to each thread in the multiple threads, the multiple threads including the target thread;

[0018] The method further comprises:

[0019] Determine, according to the first thread running information and the second thread running information, an increment of a central processing unit usage time of each thread in the plurality of threads;

[0020] The target thread is determined from threads whose increments meet a preset condition.

[0021] Optionally, determining the target thread from threads whose increments meet a preset condition includes:

[0022] Determine the target thread from the threads whose increments are greater than a preset threshold;

[0023] or,

[0024] The multiple threads are sorted in descending order of the increments, and the target thread is determined from the threads in the first N positions of the sort, where N is an integer greater than or equal to 1.

[0025] Optionally, the method further includes:

[0026] Output the identifier of the target thread and / or the CPU usage time of the target thread within a preset time period, wherein the preset time period is a time period between the first moment and the second moment.

[0027] In a second aspect, an embodiment of the present application provides a device for determining a CPU usage rate of a thread, the device comprising:

[0028] A first acquisition unit, configured to acquire, at a first moment, first running information of a target thread through a first interface provided by an operating system, wherein the first running information includes an identifier of the target thread and a first usage time of a central processing unit used by the target thread;

[0029] A second acquiring unit, configured to acquire second running information of the target thread through the first interface at a second time, the second running information including the identifier and a second usage time of the central processor used by the target thread, the second time being later than the first time;

[0030] a first determining unit, configured to determine a CPU usage rate of a target thread according to the first usage time and the second usage time, and to determine a thread name of the target thread according to an identifier of the target thread;

[0031] The first output unit is used to output the thread name of the target thread and the CPU usage rate of the target thread.

[0032] Optionally, the first interface is an interface for obtaining thread status in an interface provided by a class used for source code debugging of the Android system.

[0033] Optionally, the first determining unit is configured to:

[0034] Using the first function to traverse the thread list, obtain the thread pointer corresponding to the target thread, the thread list including the thread pointer of the target thread;

[0035] The thread pointer of the target thread is used as an input parameter of the second function to obtain the thread name of the target thread.

[0036] Optionally, the first function is a loop function, and the second function is a thread name acquisition function.

[0037] Optional,

[0038] The first acquisition unit is configured to: acquire first thread running information through the first interface at the first moment, the first thread running information including first running information corresponding to each thread in a plurality of threads, the plurality of threads including the target thread;

[0039] The second acquisition unit is used to: acquire second thread running information through the first interface at the second moment, the second thread running information including second running information corresponding to each thread in the multiple threads, the multiple threads including the target thread;

[0040] The device also includes:

[0041] A second determining unit, configured to determine an increment of a CPU usage time of each of the plurality of threads according to the first thread running information and the second thread running information;

[0042] The third determining unit is used to determine the target thread from the threads whose increments meet the preset conditions.

[0043] Optionally, the third determining unit is used to:

[0044] Determine the target thread from the threads whose increments are greater than a preset threshold;

[0045] or,

[0046] The multiple threads are sorted in descending order of the increments, and the target thread is determined from the threads in the first N positions of the sort, where N is an integer greater than or equal to 1.

[0047] Optionally, the device further comprises:

[0048] The second output unit is used to output the identifier of the target thread and / or the CPU usage time of the target thread within a preset time period, wherein the preset time period is a time period between the first moment and the second moment.

[0049] In a third aspect, an embodiment of the present application provides a device, the device comprising a processor and a memory;

[0050] The processor is used to execute instructions stored in the memory so that the device executes the method described in any one of the first aspects above.

[0051] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, comprising instructions, wherein the instructions instruct a device to execute any of the methods described in the first aspect above.

[0052] In a fifth aspect, an embodiment of the present application provides a computer program product, which, when executed on a computer, enables the computer to execute any of the methods described in the first aspect above.

[0053] Compared with the prior art, the embodiments of the present application have the following advantages:

[0054] The embodiment of the present application provides a method for determining the CPU usage of a thread, the method comprising: obtaining the first operation information of the target thread through the first interface provided by the operating system at a first moment, the first operation information including the identifier of the target thread and the first usage time of the target limited practical CPU. At a second moment later than the first moment, the second operation information of the target thread is obtained again through the first interface, the second operation information including the identifier of the target thread and the second usage time of the target thread using the CPU. After determining the first usage time and the second usage time, the CPU usage of the target thread can be determined according to the first usage time and the second usage time. In addition, the thread name of the target thread can also be determined according to the identifier of the target thread. After determining the thread name of the target thread and the CPU usage of the target thread, the thread name of the target thread and the CPU usage of the target thread can be output. In this way, the user can view the thread name of the target thread and the CPU usage of the target thread. Using this solution, the operation information of the target thread can be obtained through the first interface, so as to determine the CPU usage of the target thread. The method of calling the interface occupies less resources. Therefore, using this solution, the CPU usage of the target thread can be determined under the premise of using less resources. BRIEF DESCRIPTION OF THE DRAWINGS

[0055] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0056] Figure 1 A flowchart of a method for determining a CPU usage rate of a thread provided in an embodiment of the present application;

[0057] Figure 2 A flowchart of a method for determining a thread name of a target thread provided in an embodiment of the present application;

[0058] Figure 3 A flowchart of a method for determining a target thread provided in an embodiment of the present application;

[0059] Figure 4 A schematic diagram of the structure of a device for determining the CPU usage of a thread provided in an embodiment of the present application. DETAILED DESCRIPTION

[0060] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0061] The inventor of the present application has found through research that, at present, when determining the CPU usage rate of a thread in the Android operating system, the CPU usage time and the overall CPU usage time of the thread can be obtained by reading the proc file, thereby determining the CPU usage rate of the thread. Specifically, the proc file can be read at the actual time and the end time of the detection cycle respectively, to obtain the CPU usage time corresponding to the aforementioned start time and end time of the thread, and the overall CPU usage time corresponding to the start time and the end time, respectively, so as to obtain the increment of the CPU usage time of the thread in the detection cycle and the increment of the overall CPU usage time in the detection cycle, and then, the ratio between the increment of the CPU usage time of the thread in the detection cycle and the increment of the overall CPU usage time in the detection cycle is calculated to obtain the CPU usage rate of the thread.

[0062] Considering that in actual applications, it is often desired to obtain the real-time value of the thread CPU usage, the above detection period will be set very small. Therefore, using the above solution to determine the CPU usage of the thread requires frequent reading of the proc file, which will occupy more resources and thus affect system performance.

[0063] In order to solve the above problems, an embodiment of the present application provides a method for determining the CPU usage of a thread, which can determine the CPU usage of a thread while using fewer resources.

[0064] Various non-limiting implementations of the present application are described in detail below in conjunction with the accompanying drawings.

[0065] Exemplary Methods

[0066] See also Figure 1 , which is a flow chart of a method for determining the CPU usage of a thread provided by an embodiment of the present application. In one example, the method can be executed by a client or a server, and the embodiment of the present application does not make specific limitations. In this embodiment, the method can, for example, include the following steps: S101-S104.

[0067] S101: Acquire first running information of a target thread through a first interface provided by an operating system at a first moment, where the first running information includes an identifier of the target thread and a first usage time of the target thread using a CPU.

[0068] In one example, during specific implementation of S101, the identifier of the target thread may be used as an index to obtain the first running information of the target thread through the first interface.

[0069] In another example, first thread running information may be obtained through the first interface, where the first thread running information includes first running information corresponding to each thread in a plurality of threads, and the target thread may be one of the plurality of threads.

[0070] The first operation information of the target thread may include the identifier of the target thread and the first usage time of the target thread using the CPU. The first usage time mentioned here may be the time obtained by timing from the time when the target thread starts to use the CPU. For example, it may be the difference between the first moment and the moment when the target thread starts to use the CPU.

[0071] In one example, the first interface may be called by a reflection call, so as to obtain the first running information of the target thread through the first interface.

[0072] The operating system in the embodiment of the present application may be, for example, an Android system.

[0073] The first interface in the embodiment of the present application may be an interface that can obtain the running information of a thread. The embodiment of the present application does not specifically limit the first interface, and the first interface may be any interface that can obtain the running information of a thread. In one example, considering that the Android system has a built-in class for source code debugging, the class for source code debugging may provide some interfaces. In an embodiment of the present application, the first interface may be a class interface for source code debugging of the Android system. For example, the class for source code debugging of the Android system may be the org.apache.harmony.dalvik.ddmc.DdmVmInternal class. In one example, the class interface for source code debugging of the Android system includes an interface for obtaining thread status (getThreadStats), and the getThreadStats interface may obtain the running information of a thread. Therefore, the first interface may be the aforementioned getThreadStats interface.

[0074] Regarding the org.apache.harmony.dalvik.ddmc.DdmVmInternal class of the Android system, it should be noted that in Android system development, the org.apache.harmony.dalvik.ddmc.DdmVmInternal class is part of the Dalvik Debug Monitor Server (DDMS), which is used to interact with and debug the Dalvik virtual machine. Dalvik is a virtual machine on the Android platform that is used to execute the bytecode of Android applications. The DdmVmInternal class provides a set of static methods that allow developers to obtain information about the internal state of the Dalvik virtual machine at runtime, as well as perform certain debugging operations. By using the DdmVmInternal class, developers can monitor and analyze the internal state of the Dalvik virtual machine while the application is running, helping to debug and optimize the application.

[0075] Regarding the getThreadStats interface, it should be noted that it can be a custom interface that can obtain the running information of the thread, where the running information of the thread includes the thread identifier and the time the thread uses the CPU. The time the thread uses the CPU mentioned here can include the running time of the thread in user state (utime) and the running time of the thread in kernel state (stime), or in other words, the time the thread uses the CPU can be the sum of the utime and stime of the thread.

[0076] In the embodiment of the present application, the identifier of a thread (eg, a target thread) may be used to uniquely identify the thread. The identifier of the thread may be, for example, the number of the thread.

[0077] S102: Acquire second running information of the target thread through the first interface at a second moment, where the second running information includes the identifier and a second usage time of the CPU used by the target thread, and the second moment is later than the first moment.

[0078] In one example, during specific implementation of S102, the identifier of the target thread may be used as an index to obtain the second running information of the target thread through the first interface.

[0079] In another example, the second thread running information may be obtained through the first interface, where the second thread running information includes second running information corresponding to each thread in a plurality of threads, and the target thread may be one of the plurality of threads.

[0080] The second operation information of the target thread may include the identifier of the target thread and the second usage time of the target thread using the CPU. The second usage time mentioned here may be the time obtained by timing from when the target thread starts to use the CPU. For example, it may be the difference between the second moment and the moment when the target thread starts to use the CPU.

[0081] In one example, the first interface may be called by a reflection call, so as to obtain the second running information of the target thread through the first interface.

[0082] S103: Determine a CPU usage rate of a target thread according to the first usage time and the second usage time, and determine a thread name of the target thread according to an identifier of the target thread.

[0083] After determining the first usage time and the second usage time, the increment of the CPU usage time of the target thread can be further determined. Specifically, the increment of the CPU usage time of the target thread can be obtained by subtracting the first usage time from the second usage time. And further, the CPU usage rate of the target thread is determined according to the increment of the CPU usage time of the target thread. For example, the ratio of the CPU usage rate of the target thread and the increment of the overall CPU usage time can be determined as the CPU usage rate of the target thread. In one example, the increment of the overall CPU usage time can be the time difference between the second moment and the first moment.

[0084] In the embodiment of the present application, considering that the thread identifier generally does not have special semantics, it is difficult for the user to determine the specific thread through the thread identifier, while the thread name of the thread carries strong semantics, which enables the user to determine the specific thread. Therefore, in one example, the thread name of the target thread can also be determined according to the target thread identifier, so that the user can accurately locate the specific thread based on the thread name.

[0085] In one example, considering that a process may include multiple threads, and the process-related file may include thread-related information, such as thread identifiers and thread names. Therefore, in one example, the thread identifier may be used as an index to obtain the thread name of the target thread from the process-related file.

[0086] In another example, Figure 2 The method shown determines the thread name of the target thread. Figure 2 A flowchart of a method for determining a thread name of a target thread provided in an embodiment of the present application. Figure 2 The method shown may include the following S201-S202.

[0087] S201: traverse a thread list using a first function to obtain a thread pointer corresponding to the target thread, wherein the thread list includes the thread pointer of the target thread.

[0088] In an embodiment of the present application, the first function may be used to traverse a thread list, the thread list may include multiple thread identifiers and corresponding thread pointers, and the target thread may be one of the multiple threads. In an embodiment of the present application, the target thread identifier may be used as an index to obtain the thread pointer of the target thread from the thread list. The thread pointer of the target thread may be used to indicate the storage address of the thread name of the target thread in the memory.

[0089] The embodiment of the present application does not specifically limit the first function, and the first function can be any function that can traverse the thread list. In one example, the first function can be a loop (ForEach) function. The ForEach function can perform a specified operation or callback function on each element in a collection, array, or iterator. It can avoid using traditional loop structures (such as for or while loops), simplify code writing and reading, and thus improve processing efficiency. In an embodiment of the present application, considering that in actual applications, the CPU usage of multiple threads is often counted, the ForEach function is used to perform the same operation (obtaining thread pointers) on each thread, which can improve the efficiency of determining the CPU usage of multiple threads.

[0090] S202: Using the thread pointer of the target thread as an input parameter of a second function to obtain the thread name of the target thread.

[0091] After obtaining the thread pointer of the target thread, the thread pointer of the target thread can be used as an input parameter of the second function to obtain the thread name of the target thread. The second function in the embodiment of the present application is used to determine the thread name, and its input parameter is the thread pointer. Therefore, the thread pointer of the target thread is used as the input parameter of the second function, and the second function can return the thread name of the target thread based on the thread pointer of the target thread. In a specific example, the second function can read the thread name of the target thread from the memory address corresponding to the thread pointer based on the thread pointer.

[0092] The embodiment of the present application does not specifically limit the second function, and the second function can be any function that can take a thread pointer as an input parameter and is used to determine the thread name. In one example, the second function can be a GetThreadName function. By using the first function and the second function to determine the thread name of the target thread, the thread name of the target thread can be determined while using fewer resources.

[0093] S104: Output the thread name of the target thread and the CPU usage rate of the target thread.

[0094] After determining the CPU usage of the target thread and the thread name of the target thread, the thread name of the target thread and the CPU usage of the target thread may be output, so that the user can view the output thread name of the target thread and the CPU usage of the target thread.

[0095] In one example, when S104 is implemented, the thread name of the target thread and the CPU usage of the target thread can be output in the form of a file. For example, the file can include the thread names of multiple threads and the CPU usages corresponding to the multiple threads respectively. The target thread can be one of the multiple threads.

[0096] In yet another example, during specific implementation of S104, the thread name of the target thread and the CPU usage rate of the target thread may be displayed on a display device (eg, a display screen).

[0097] In one example, in order to enable the user to obtain more information related to the target thread, the target thread identifier and / or the CPU usage time of the target thread within a preset time period can also be output, where the preset time period can be, for example, the time period between the aforementioned first moment and the second moment.

[0098] In an example, outputting the CPU usage time of the target thread within a preset time period may be outputting utime, stime, and (utime+stime) of the target thread within the preset time period.

[0099] In one example, when outputting the identifier of the target thread and / or the CPU usage time of the target thread within a preset time period, the identifier of the target thread and / or the CPU usage time of the target thread within a preset time period can be output in the form of a file.

[0100] In another example, when outputting the identifier of the target thread and / or the CPU usage time of the target thread within a preset time period, the identifier of the target thread and / or the CPU usage time of the target thread within a preset time period can be displayed on a display device.

[0101] From the above description, it can be seen that by using this solution, the running information of the target thread can be obtained through the first interface, so as to determine the CPU usage of the target thread. The method of calling the interface occupies less resources. Therefore, by using this solution, the CPU usage of the target thread can be determined while using fewer resources.

[0102] In the embodiment of the present application, the target thread may be a thread to be analyzed. Before determining the CPU usage of the target thread, it is necessary to determine the thread to be analyzed from multiple threads, so as to further determine the CPU usage of the thread to be analyzed. Next, the method for determining the thread to be analyzed is described.

[0103] In one example, the thread to be analyzed may be specified by a user. For example, the user may input an identifier of the target thread through a human-computer interaction interface.

[0104] In another example, the thread to be analyzed may be a thread that uses more CPU. In this case, the target thread may be determined by combining the aforementioned first thread running information and the second thread running information. Figure 3 In S301 - S302 shown, a target thread is determined. Figure 3 A flowchart of a method for determining a target thread provided in an embodiment of the present application.

[0105] S301: Determine an increment of CPU usage time of each thread in the plurality of threads according to the first thread running information and the second thread running information.

[0106] For any one of the multiple threads, the increment of the CPU usage time of the thread may be obtained by subtracting the first usage time corresponding to the thread from the second usage time corresponding to the thread.

[0107] S302: Determine the target thread from the threads whose increments meet preset conditions.

[0108] In the embodiment of the present application, the thread whose increment meets the preset condition may refer to a thread that uses more CPU in a preset time period. The preset time period mentioned here may be the time period between the first moment and the second moment.

[0109] In one example, considering that for a certain thread, if its corresponding increment is greater than a preset threshold, it means that the thread uses more CPU in a preset time period. In this case, the thread can be determined as a target thread. In other words, in one example, the thread whose increment meets the preset conditions can be a thread whose increment is greater than the preset threshold. In this case, when S302 is specifically implemented, one thread can be selected from the threads whose increments are greater than the preset threshold as the target thread, or multiple threads whose increments are greater than the preset threshold can be determined as target threads to obtain multiple target threads.

[0110] For example, if there are 100 threads in total, and the increments corresponding to 20 threads among the 100 threads are greater than or equal to the preset threshold, the target thread can be determined from the 20 threads. For example, one thread can be selected from the 20 threads as the target thread. For another example, each of the 20 threads can be determined as a target thread, and 20 target threads can be obtained.

[0111] The embodiment of the present application does not specifically limit the preset threshold, and the preset threshold can be set according to actual conditions.

[0112] In another example, considering that more increments indicate that the thread uses more CPU in a preset time period, the multiple threads may be sorted in descending order of the increments, and the target thread may be determined from the threads in the top N positions of the sort, where N is an integer greater than or equal to 1. In other words, in one example, the thread whose increment meets the preset condition may be the thread in the top N positions of the sort. In this case, when S302 is specifically implemented, one thread may be selected from the threads in the top N positions of the sort as the target thread, or all the N threads in the top N positions of the sort may be determined as the target threads to obtain N target threads.

[0113] The embodiment of the present application does not specifically limit the value of N. The value of N can be set according to actual conditions. For example, the value of N can be 10.

[0114] Exemplary Devices

[0115] Based on the method provided in the above embodiment, the embodiment of the present application further provides a device, which is described below in conjunction with the accompanying drawings.

[0116] See also Figure 4 , which is a structural schematic diagram of a device for determining thread CPU usage provided in an embodiment of the present application. Figure 4 The apparatus 400 for determining the CPU usage of a thread shown can be used to execute the method for determining the CPU usage of a thread provided in the above method embodiment.

[0117] In an example, the device 400 includes: a first acquisition unit 401 , a second acquisition unit 402 , a first determination unit 403 , and a first output unit 404 .

[0118] A first acquisition unit 401 is configured to acquire first running information of a target thread through a first interface provided by an operating system at a first moment, wherein the first running information includes an identifier of the target thread and a first usage time of a central processing unit used by the target thread;

[0119] A second acquiring unit 402 is configured to acquire second running information of the target thread through the first interface at a second time, wherein the second running information includes the identifier and a second usage time of the target thread using the central processor, and the second time is later than the first time;

[0120] A first determining unit 403, configured to determine a CPU usage rate of a target thread according to the first usage time and the second usage time, and to determine a thread name of the target thread according to an identifier of the target thread;

[0121] The first output unit 404 is used to output the thread name of the target thread and the CPU usage rate of the target thread.

[0122] Optionally, the first interface is an interface for obtaining thread status in an interface provided by a class used for source code debugging of the Android system.

[0123] Optionally, the first determining unit 403 is configured to:

[0124] Using the first function to traverse the thread list, obtain the thread pointer corresponding to the target thread, the thread list including the thread pointer of the target thread;

[0125] The thread pointer of the target thread is used as an input parameter of the second function to obtain the thread name of the target thread.

[0126] Optionally, the first function is a loop function, and the second function is a thread name acquisition function.

[0127] Optional,

[0128] The first acquisition unit 401 is configured to: acquire first thread running information through the first interface at the first moment, the first thread running information including first running information corresponding to each thread in a plurality of threads, the plurality of threads including the target thread;

[0129] The second acquisition unit 402 is configured to: acquire second thread running information through the first interface at the second moment, the second thread running information including second running information corresponding to each thread in the multiple threads, the multiple threads including the target thread;

[0130] The device also includes:

[0131] A second determining unit, configured to determine an increment of a CPU usage time of each of the plurality of threads according to the first thread running information and the second thread running information;

[0132] The third determining unit is used to determine the target thread from the threads whose increments meet the preset conditions.

[0133] Optionally, the third determining unit is used to:

[0134] Determine the target thread from the threads whose increments are greater than a preset threshold;

[0135] or,

[0136] The multiple threads are sorted in descending order of the increments, and the target thread is determined from the threads in the first N positions of the sort, where N is an integer greater than or equal to 1.

[0137] Optionally, the device further comprises:

[0138] The second output unit is used to output the identifier of the target thread and / or the CPU usage time of the target thread within a preset time period, wherein the preset time period is a time period between the first moment and the second moment.

[0139] Since the device 400 is a device corresponding to the method provided in the above method embodiment, the specific implementation of each unit of the device 400 is based on the same concept as the above method embodiment. Therefore, for the specific implementation of each unit of the device 400, reference can be made to the description part of the above method embodiment, which will not be repeated here.

[0140] The embodiment of the present application also provides a device, the device comprising a processor and a memory;

[0141] The processor is used to execute the instructions stored in the memory, so that the device executes the method for determining the CPU usage of a thread provided in the above method embodiment.

[0142] An embodiment of the present application provides a computer-readable storage medium, including instructions, wherein the instructions instruct a device to execute the method for determining the CPU usage of a thread provided in the above method embodiment.

[0143] An embodiment of the present application provides a computer program product. When the computer program product is run on a computer, the computer is enabled to execute the method for determining the CPU usage rate of a thread provided in the above method embodiment.

[0144] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the invention disclosed herein. The present application is intended to cover any modification, use or adaptation of the present application, which follows the general principles of the present application and includes common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present application are indicated by the following claims.

[0145] It should be understood that the present application is not limited to the precise structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.

[0146] The above description is only a preferred embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application should be included in the protection scope of the present application.

Claims

1. A method for determining the CPU usage of a thread, characterized in that: The method comprises: Acquire, at a first moment, first running information of a target thread through a first interface provided by an operating system, the first running information including an identifier of the target thread and a first usage time of a central processing unit used by the target thread; acquiring, at a second time, second running information of the target thread through the first interface, the second running information including the identifier and a second usage time of the central processor used by the target thread, the second time being later than the first time; Determine a CPU usage rate of a target thread according to the first usage time and the second usage time, and determine a thread name of the target thread according to an identifier of the target thread; The thread name of the target thread and the CPU usage rate of the target thread are output.

2. The method according to claim 1, characterized in that The first interface is an interface for obtaining thread status in the interface provided by the class used for source code debugging of the Android system.

3. The method according to claim 1, characterized in that Determining the thread name of the target thread according to the identifier of the target thread includes: Using the first function to traverse the thread list, obtain the thread pointer corresponding to the target thread, the thread list including the thread pointer of the target thread; The thread pointer of the target thread is used as an input parameter of the second function to obtain the thread name of the target thread.

4. The method according to claim 3, characterized in that The first function is a loop function, and the second function is a thread name acquisition function.

5. The method according to claim 1, characterized in that: The acquiring first running information of the target thread through a first interface provided by the operating system at the first moment includes: acquiring first thread running information through the first interface at the first moment, the first thread running information including first running information corresponding to each thread in a plurality of threads, the plurality of threads including the target thread; The acquiring the second running information of the target thread through the first interface at the second moment includes: acquiring the second thread running information through the first interface at the second moment, the second thread running information including the second running information corresponding to each thread in the multiple threads, the multiple threads including the target thread; The method further comprises: Determine, according to the first thread running information and the second thread running information, an increment of a central processing unit usage time of each thread in the plurality of threads; The target thread is determined from threads whose increments meet a preset condition.

6. The method according to claim 5, characterized in that Determining the target thread from threads whose increments meet preset conditions includes: Determine the target thread from the threads whose increments are greater than a preset threshold; or, The multiple threads are sorted in descending order of the increments, and the target thread is determined from the threads in the first N positions of the sort, where N is an integer greater than or equal to 1.

7. The method according to claim 1, characterized in that The method further comprises: Output the identifier of the target thread and / or the CPU usage time of the target thread within a preset time period, wherein the preset time period is a time period between the first moment and the second moment.

8. A device for determining the CPU usage of a thread, characterized in that: The device comprises: A first acquisition unit, configured to acquire, at a first moment, first running information of a target thread through a first interface provided by an operating system, wherein the first running information includes an identifier of the target thread and a first usage time of a central processing unit used by the target thread; A second acquiring unit, configured to acquire second running information of the target thread through the first interface at a second time, the second running information including the identifier and a second usage time of the central processor used by the target thread, the second time being later than the first time; a first determining unit, configured to determine a CPU usage rate of a target thread according to the first usage time and the second usage time, and to determine a thread name of the target thread according to an identifier of the target thread; The first output unit is used to output the thread name of the target thread and the CPU usage rate of the target thread.

9. A device, characterized in that: The device comprises a processor and a memory; The processor is configured to execute instructions stored in the memory, so that the device performs the method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: The method comprises instructions for instructing a device to execute the method according to any one of claims 1 to 7.