0

I'm trying to get a percentage of CPU usage using Delphi, I've tried this solutions:

How to retrieve cpu usage per process

Get the Percentage of Total CPU Usage

https://forum.lazarus.freepascal.org/index.php?topic=38839.0

https://www.mitec.cz/msics.html

However, in all of them it always returns an incorrect value if we compare it with what Resource Monitor shows me on Windows, especially when it exceeds 25% of the total CPU. When I even tried to get it from the specific process, it also showed a wrong value. With tests performed on some machines, the value shown is always higher, in others machines the value is always lower than the value shown in the Task Manager. Using the same setup, normally I get 7% difference with the real value, but in some cases the Resource Monitor shows 50% and the delphi rotine shows 5%. I've made a test application using C#, creating a PerformanceCounter and it shows me exactly what Resource Monitor shows on Windows(on the same machines). Is there any solution to get CPU usage correctly using Delphi? Thanks in advance

  • 1
    All the resources you listed already provide working solutions. Did you try all of them and they all produce wrong result? If you have built a working solution in C# - why don't you simply translate to Delphi ? – IVO GELOV May 10 '22 at 15:18
  • 1
    Have you also read accompanying comments to linked solutions. For instance in first linked solution one user also commented that you need to take into count the number of CPU cores in order to get the correct result. The commenter suggest using GetCpuCount function to get the number of cores which is actually wrong since GetCpuCount returns only hte number of physical cores but not the number of virtual cores. Therefore it will report wrong number of cores on any CPU that supports Hyperthreading. There are specific functions for retrieving the number of logical cores. – SilverWarior May 10 '22 at 15:47
  • I'm guessing that many of the linked solutions might not be reporting correct utilization due to not counting the logical cores correctly. – SilverWarior May 10 '22 at 15:49
  • If you actually read the top answer to the duplicate I linked, it explains how to get the individual CPU core percentage. The code in the answer is summing those individual percentages to get the total use, but if you don't sum then you have the individual core percentages. I don't think you read the answer very well. – Ken White May 10 '22 at 18:23

0 Answers0