giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.07k stars 1.37k forks source link

[Windows]Why are the indicators collected by psuntil too large? #2384

Open RLYRLY opened 3 months ago

RLYRLY commented 3 months ago

Summary

When collecting the CPU utilization of the same process within the same time range, it is found that the value collected by psutil will be larger, and the task manager that comes with Windows will be much less.

RLYRLY commented 3 months ago

After reading the latest psutil source code, the CPU utilization uses the GetSystemTimes API, but the calculation of the CPU utilization in the task manager after win10 has changed. The GetSystemTimes API has been a long time ago, and now it is replaced by a "performance counter", so there are inconsistencies. The performance counter module under WMI obtains the process CPU utilization, which should be the same as what is displayed in the task manager. https://learn.microsoft.com/en-us/windows/win32/perfctrs/performance-counters-portal https://learn.microsoft.com/en-us/windows/win32/wmisdk/monitoring-performance-data

RLYRLY commented 3 months ago

After doing a benchmark test, the CPU utilization output by psutil was 55%, and the result using a PowerShell script combined with Windows Performance Counters was 6%. The difference is nearly 10 times.

RLYRLY commented 2 months ago

@giampaolo Hello, can you help me take a look?

giampaolo commented 2 months ago

Do you have any idea in what Windows version GetSystemTimes() was "obsoleted" by performance counters? Or put it in another way, any idea when Windows task manager started using PC? I'm fine with using PC on newer Windows versions and GetSystemTimes() for older ones. Feel free to make a PR if you know how to do this.

RLYRLY commented 2 months ago

First of all, thank you for your answer. After reading the psutil documentation, I found that there is no problem in calculating the CPU utilization of a process in Windows. It was my misunderstanding. Thanks again for your answer. ps: I am currently testing on macOS and found that there is a problem with the calculation of CPU utilization, and I did not find a relevant explanation in the documentation. For details, see: https://github.com/giampaolo/psutil/issues/2411 @giampaolo