CPU usage per thread

I need to get CPU% for each process thread.

So, I am creating a simple script:

import psutil from psutil import Process p = psutil.Process(4499) treads_list = p.get_threads() for i in treads_list: o = i[0] th = psutil.Process(o) cpu_perc = th.get_cpu_percent(interval=1) print('PID %s use %% CPU = %s' % (o, cpu_perc)) 

Here's what TOP looks like for this process:

  4942 teamcity 20 0 3288m 831m 3124 R 33.3 10.6 10303:37 java 32700 teamcity 20 0 3288m 831m 3124 S 5.9 10.6 18:49.99 java 5824 teamcity 20 0 3288m 831m 3124 S 5.9 10.6 1:57.90 java 4621 teamcity 20 0 3288m 831m 3124 S 3.0 10.6 1834:09 java 4622 teamcity 20 0 3288m 831m 3124 S 2.6 10.6 1844:15 java 

The threads use 2.6-5.9% of the CPU, and the parent PID - use 33.3.

But - here is the result of the script:

 # ./psutil_threads.py PID 10231 use % CPU = 60.9 PID 10681 use % CPU = 75.3 PID 11371 use % CPU = 69.9 PID 11860 use % CPU = 85.9 PID 12977 use % CPU = 56.0 PID 14114 use % CPU = 88.8 

It seems that each thread "feeds" 56-88% of the CPU ...

What am I missing here?

+5
source share
3 answers

get_cpu_percent (interval = 0.1)

Returns a float, which represents the percentage of CPU utilization of the process.

When an interval> 0.0 compares the process time with the processor system time elapsed before and after the interval (blocking).

If the interval is 0.0, or None compares the process time with the processor system time elapsed since the last call, returns immediately. In this case, it is recommended, for accuracy, that this function be called for at least 0.1 seconds between calls.

This is very similar to the fact that it will give you how much time for the CPU time spent on a non-idle will be returned (i.e.: the amount of CPU time of the process to the CPU time of the system), and the top one shows the amount of CPU time of the process in relation to the real time. This seems realistic given your numbers.

To get the top values, you will show you, simply multiplying the CPU utilization of each thread by the CPU utilization of the kernel that the thread is running on should work. psutil.cpu_percent should help with this. Note that you need to divide percentages by 100.0 (to get a β€œpercent” between 0 and 1) before multiplying them.

+4
source

This should give you what you need and match the top (adapt to your use case):

 import psutil def get_threads_cpu_percent(p, interval=0.1): total_percent = p.get_cpu_percent(interval) total_time = sum(p.cpu_times()) return [total_percent * ((t.system_time + t.user_time)/total_time) for t in p.get_threads()] # Example usage for process with process id 8008: proc = psutil.Process(8008) print(get_threads_cpu_percent(proc)) 
+3
source

While Gabe's answer is wonderful, note that the following updated syntax is required for the new psutil version:

 import psutil def get_threads_cpu_percent(p, interval=0.1): total_percent = p.cpu_percent(interval) total_time = sum(p.cpu_times()) return [total_percent * ((t.system_time + t.user_time)/total_time) for t in p.threads()] # Example usage for process with process id 8008: proc = psutil.Process(8008) print(get_threads_cpu_percent(proc)) 
+1
source

Source: https://habr.com/ru/post/1204186/


All Articles