QTimer is very inaccurate
-
@Pavankumar-S-V How busy is your main thread where the timer is running? Maybe the load is very high, so the slot is called with long delay?
-
@Pavankumar-S-V
As @jsulm says: in particular,timerEvnt()
comments indicate potentially quite a lot of processing, how long does that take? You won't get another timeout till it concludes. If you comment out (some of) its actions how accurate is the timer then? -
@jsulm , @JonB : Thanks for the response.
I'm only doing some initializations inside the main thread and finally starting the event loop by calling "return app->exec".
As per my understanding, main thread should not have consumed too much cpu time. But suprisingly, when checked in htop, it was consuming lot of cpu time(more than 100%). So i tried to limit the cpu usage of main thread using linux utility "cpulimit" and limited the cpu usage of the main thread to 70%,80%....But, there was no improvement in the accuracy of Qtimer.I also calculated the time taken to execute everything inside the timeout handler(timerEvnt()) of the QTimer using "QElapsedTimer" and saw that it was taking very less time(around 1/2ms) to complete the execution.
-
@Pavankumar-S-V said in QTimer is very inaccurate:
As per my understanding, main thread should not have consumed too much cpu time.
I don't understand. You have a timer and its slot running in the main thread. The main thread does not only do what you put in its constructor....
You will not improve the accuracy of the timer by throttling the CPU if it is already using anywhere near 100%! That indicates it is very "busy" doing something, hence it won't have time to process your timer events quickly! You should find out why this is....
-
@Pavankumar-S-V said in QTimer is very inaccurate:
So i tried to limit the cpu usage of main thread using linux utility "cpulimit" and limited the cpu usage of the main thread to 70%,80%..
That's not what you should do and I don't know why you think it could improve anything.
What I and @JonB asked is: is your main thread too busy? Did you check that? As @JonB pointed out if you're doing too much stuff in the slot then the next timeout will not cause a new slot call untill the current one finishes. You should really check what you are doing in main thread (why is the CPU usage so high?). It's your application you should know what you are doing. You can also use a profiler to see which parts of your code cause most CPU load. -
@JonB, @jsulm :
Yes understood. But as I mentioned earlier, I'm not doing any time consuming tasks inside the main thread or timerEvnt() from application point of view. So, I could not understand why the cpu load was too high. Qt might be doing something internally that might be causing high cpu load. I wanted to know what could it be.The Reason why I tried limiting the cpu usage of main thread:
Note: One of the main task of timerEvnt() is to exchange data with UI objects of the currently displayed screen.
Because of the inaccuracy of the QTimer, I am seeing sluggishness when interacting with the UI(This is the actual issue that I'm facing). So, I'm trying to improve the accuracy of the Qtimer and see if it can improve the UI responsiveness.I read that "QSGRenderThread" thread is responsible for managing and controlling rendering operations for Qt Quick applications and also helps prevent the user interface from freezing or becoming unresponsive during resource-intensive graphical tasks.
As my main thread was using too much cpu , I thought "QSGRenderThread" is starving and reducing the cpu usage of main thread may help to get more cpu time for "QSGRenderThread" thereby increasing the performance of the graphics. But this did not happen.@jsulm Also you mentioned that profiling might help me get a good picture of what is happening in the application. Can you please share any link with good description of how to profile Embedded QT applications?
-
@Pavankumar-S-V You should really find out why you have such a high CPU load.
Either use a profiler or disable some code (comment out) until CPU load gets lower then you know which part of your code causes the CPU load."Because of the inaccuracy of the QTimer, I am seeing sluggishness when interacting with the UI" - I doubt it is "inaccuracy" of QTimer - as already explained if you have such high CPU load then it can delay slot calls. So, before blaming Qt check why you have so high CPU load...
-
@jsulm Ok, I will check which part of my application could be causing high cpu load.
If possible, can you please share any link with good description of how to profile Embedded QT applications?
Thanks a lot for the suggestions@jsulm , @JonB can creating a separate thread for timerEvnt() with a msleep(20) will help me to get good accuracy?
-
@Pavankumar-S-V said in QTimer is very inaccurate:
@jsulm , @JonB can creating a separate thread for timerEvnt() with a msleep(20) will help me to get good accuracy?
No.
-
@Pavankumar-S-V said in QTimer is very inaccurate:
can creating a separate thread for timerEvnt() with a msleep(20) will help me to get good accuracy?
I would say it depends whether the slot will be called in main thread or in the same thread where the timer is running. If in the same thread as timer then this could improve the situation (at least if the CPU has more than one core). But even then you should investigate the CPU load.
Profiling with GCC: https://www.thegeekstuff.com/2012/08/gprof-tutorial/
-
@jsulm
If you want a timer to tick at 20ms and do some processing I cannot see howmsleep(20)
can do any good anywhere. At best it would occupy the whole desired 20ms interval, leaving no time to do any further processing...? Though you know better than I. -
@Pavankumar-S-V
You might like to look at the topic Small example showing QTimer slowing to a halt. The important take away is the OP's last post there, https://forum.qt.io/topic/94657/small-example-showing-qtimer-slowing-to-a-halt/16, where the gist isCurrently I have moved the logging functionality on a separate thread and this seems to work fine.
Something in your application might benefit from being moved to a thread if the main thread is too busy to keep up.
But to me this still does not resolve your CPU usage issue. If anything takes near 100% CPU time you have a potential problem keeping up....
-
@jsulm : I tried to profile the application using "gprof" as per your suggestion.
I followed the same procedure that is mentioned in the link shared by you. But the file that holds the profiled data(gmon.out) is not getting generated after running and exiting from the application.
Just to confirm, I followed the following steps:
1) Enabled -pg, -O0 in the compiler, linker and built the qt project.
2) I made the following modification to the main() so that the application returns from main() gracefully(This is one of the condition for gprof):int main(int argc, char *argv[]) { QApplication *app; app = new QApplication(argc, argv); MainClass maincls; maincls.view = new QQuickView; maincls.view->setSource(QUrl::fromLocalFile("Path of Loader.qml")); maincls.view->show(); //starting all the user threads maincls.view->installEventFilter(&maincls); exitTimer = new QTimer(); exitTimer->setSingleShot(true); QObject::connect(exitTimer, &QTimer::timeout, app, &QApplication::quit, Qt::QueuedConnection); exitTimer->start(85000); //main event loop should run for 85 sec and return to the main function after that. app->exec(); qDebug()<<"Exiting qt;"; //This message is printed successfully return 0; }
The above procedure should have generated "gmon.out" file but it has not. I have used gprof before for C based application and successfully profiled it. But I'm facing this issue when profiling qt application using grpof.
- Please let me know if I'm missing something during profiling.
- Please let me know if gprof is a right profiling tool to profile the qt application. As per my experience with gprof before, it is not very suitable for multi threaded applications and also provides info related to only user space.
- Please guide me to the right way for profiling embedded qt applications as there is very limited information available online related to qt profiling. As I'm working on improving the performance of the qt based device, it would be very useful for me to profile the app.
Thank you
-
@Pavankumar-S-V said in QTimer is very inaccurate:
Enabled -pg, -O0 in the compiler, linker and built the qt project.
Can you show how?
-
@Pavankumar-S-V Did you do a complete rebuild?
- Delete build folder
- Run qmake
- Build
-
@jsulm : Yes I did a complete re-build.
We have a Makefile that calls qmake to generate a .mk for the build.
I have checked in that generated .mk file and made sure that these flags are reflected in CFLAGS, CXXFLAGS. So, these flags are indeed considered for the build as per my understanding.