Measuring CPU usage and internet bandwidth
-
wrote on 28 May 2012, 06:39 last edited by
HI,
Is there any way in QT we can measure CPU usage and Internet bandwidth?
-
wrote on 28 May 2012, 06:59 last edited by
No, it is too much platform related. You have to use operating system facilities to extract such information.
-
wrote on 28 May 2012, 17:22 last edited by
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!)
-
wrote on 29 May 2012, 06:02 last edited by
[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.
-
wrote on 29 May 2012, 08:29 last edited by
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!
1/5