Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-100438

Incorrect rounding to ms in QTimer when clock too coarse



    • Bug
    • Resolution: Done
    • P2: Important
    • 6.3.0 Beta2, 6.4.0 Beta1
    • 6.2.3, 6.3.0 Alpha
    • Core: Other
    • None
    • All
    • 47cf674477 (qt/qtbase/dev) 47cf674477 (qt/tqtc-qtbase/dev) f6e6aca01e (qt/qtbase/6.2) 693f3073ab (qt/qtbase/6.3) f6e6aca01e (qt/tqtc-qtbase/6.2) 693f3073ab (qt/tqtc-qtbase/6.3)


      Happens (at least on QNX) where default tick (clock) is 1ms and it's is precisely that when roundToMillisecond is called.


      static timespec roundToMillisecond(timespec val)

      { // always round up // worst case scenario is that the first trigger of a 1-ms timer is 0.999 ms late int ns = val.tv_nsec % (1000 * 1000); val.tv_nsec += 1000 * 1000 - ns; return normalizedTimespec(val); }

      called from QTimerInfoList::timerRemainingTime():

      tm = roundToMillisecond(t->timeout - currentTime); return tm.tv_sec*1000 + tm.tv_nsec/1000/1000;

      The only way for that math to return a value greater than 10 and lesser than 1000 is if tm.tv_nsec >= 11000000 after roundToMillisecond. So under what entry conditions could it cause an exit with tv_nsec = 11 million? That function causes the output to be:

      tv_nsec - (tv_nsec % 1000000) + 1000000

      If tv_nsec is not a multiple of a million, then the modulus produces a value that rounds it down to a multiple of a million. Then it adds a million, causing tv_nsec to become rounded to a millisecond. However, if tv_nsec is a multiple of a million, then the modulus is zero and we simply add a full millisecond. So 10 ms becomes 11. This can happen if the current system time has not changed since we installed the timer.

      Both QTimerInfoList::registerTimer() and QTimerInfoList::timerRemainingTime() call updateCurrentTime() but there's no requirement that the clock has moved forwards. The monotonic clock can't move backwards, but it can stay the same. This is probably what's happening on QNX: the clock is too coarse.


        No reviews matched the request. Check your Options in the drop-down menu of this sections header.



            thiago Thiago Macieira
            papetaja Pasi Petäjäjärvi
            0 Vote for this issue
            1 Start watching this issue



              Gerrit Reviews

                There are no open Gerrit changes