Measuring CPU usage and internet bandwidth



  • HI,

    Is there any way in QT we can measure CPU usage and Internet bandwidth?



  • No, it is too much platform related. You have to use operating system facilities to extract such information.



  • You didn't mention what platform you are on, but on Win32 detect the CPU usage is straight forward:

    Simply call GetSystemTimes() in a loop (with a Sleep() between the calls) and calculate the difference of the counters between the current call and the previous call. That will give you the time the CPU has spent in each of the three states (kernel/user/idle) within the last interval. Then you can calculate the fraction of the time that the CPU was "busy" (not idle) in the last interval. And that's the "CPU usage". The interval size is up to you.

    (Only take care that the "lpKernelTime" counter also includes the "idle" time!)



  • [quote author="MuldeR" date="1338225732"]
    Simply call GetSystemTimes() in a loop (with a Sleep() between the calls) and calculate the difference of the counters between the current call and the previous call. That will give you the time the CPU has spent in each of the three states (kernel/user/idle) within the last interval. Then you can calculate the fraction of the time that the CPU was "busy" (not idle) in the last interval. And that's the "CPU usage". The interval size is up to you.
    [/quote]

    Quite an hand-made solution, and it is going to work only on Windows.



  • Yeah, as said before, there probably isn't a platform independent solution for this. GetSystemTimes() is the way to do it on the Windows platform. You'd have to look for a different way to get the CPU time counters on Linux, e.g. by reading "/proc/stat". Anyway, the basic idea on how to calculate the "CPU usage" should be the same!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.