summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qtimerinfo_unix.cpp
blob: cce55171f2becda68c5580775b6ef0f9a2f04d13 (plain)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
// Copyright (C) 2021 The Qt Company Ltd.// Copyright (C) 2016 Intel Corporation.// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only#include <qelapsedtimer.h>#include <qcoreapplication.h>#include"private/qcore_unix_p.h"#include"private/qtimerinfo_unix_p.h"#include"private/qobject_p.h"#include"private/qabstracteventdispatcher_p.h"#include <sys/times.h>using namespacestd::chrono;// Implied by "using namespace std::chrono", but be explicit about it, for grep-abilityusing namespacestd::chrono_literals; QT_BEGIN_NAMESPACE Q_CORE_EXPORT bool qt_disable_lowpriority_timers=false;/* * Internal functions for manipulating timer data structures. The * timerBitVec array is used for keeping track of timer identifiers. */QTimerInfoList::QTimerInfoList() =default;steady_clock::time_point QTimerInfoList::updateCurrentTime()const{ currentTime =steady_clock::now();return currentTime;}/*! \internal Updates the currentTime member to the current time, and returns \c true if the first timer's timeout is in the future (after currentTime). The list is sorted by timeout, thus it's enough to check the first timer only.*/boolQTimerInfoList::hasPendingTimers(){if(timers.isEmpty())return false;returnupdateCurrentTime() < timers.at(0)->timeout;}static boolbyTimeout(const QTimerInfo *a,const QTimerInfo *b){return a->timeout < b->timeout; };/* insert timer info into list*/voidQTimerInfoList::timerInsert(QTimerInfo *ti){ timers.insert(std::upper_bound(timers.cbegin(), timers.cend(), ti, byTimeout), ti);}staticconstexpr milliseconds roundToMillisecond(nanoseconds val){// always round up// worst case scenario is that the first trigger of a 1-ms timer is 0.999 ms latereturn ceil<milliseconds>(val);}static_assert(roundToMillisecond(0ns) ==0ms);static_assert(roundToMillisecond(1ns) ==1ms);static_assert(roundToMillisecond(999'999ns) ==1ms);static_assert(roundToMillisecond(1'000'000ns) ==1ms);static_assert(roundToMillisecond(999'000'000ns) ==999ms);static_assert(roundToMillisecond(999'000'001ns) ==1000ms);static_assert(roundToMillisecond(999'999'999ns) ==1000ms);static_assert(roundToMillisecond(1s) ==1s);staticconstexpr seconds roundToSecs(nanoseconds interval){// The very coarse timer is based on full second precision, so we want to// round the interval to the closest second, rounding 500ms up to 1s.//// std::chrono::round() wouldn't work with all multiples of 500 because for the// middle point it would round to even:// value round() wanted// 500 0 1// 1500 2 2// 2500 2 3auto secs = duration_cast<seconds>(interval);const nanoseconds frac = interval - secs;if(frac >=500ms)++secs;return secs;}static voidcalculateCoarseTimerTimeout(QTimerInfo *t,steady_clock::time_point now){// The coarse timer works like this:// - interval under 40 ms: round to even// - between 40 and 99 ms: round to multiple of 4// - otherwise: try to wake up at a multiple of 25 ms, with a maximum error of 5%//// We try to wake up at the following second-fraction, in order of preference:// 0 ms// 500 ms// 250 ms or 750 ms// 200, 400, 600, 800 ms// other multiples of 100// other multiples of 50// other multiples of 25//// The objective is to make most timers wake up at the same time, thereby reducing CPU wakeups.Q_ASSERT(t->interval >=20ms);constauto timeoutInSecs = time_point_cast<seconds>(t->timeout);auto recalculate = [&](const milliseconds frac) { t->timeout = timeoutInSecs + frac;if(t->timeout < now) t->timeout += t->interval;};// Calculate how much we can round and still keep within 5% error milliseconds interval =roundToMillisecond(t->interval);const milliseconds absMaxRounding = interval /20;auto fracMsec = duration_cast<milliseconds>(t->timeout - timeoutInSecs);if(interval <100ms && interval !=25ms && interval !=50ms && interval !=75ms) {auto fracCount = fracMsec.count();// special mode for timers of less than 100 msif(interval <50ms) {// round to even// round towards multiples of 50 msbool roundUp = (fracCount %50) >=25; fracCount >>=1; fracCount |= roundUp; fracCount <<=1;}else{// round to multiple of 4// round towards multiples of 100 msbool roundUp = (fracCount %100) >=50; fracCount >>=2; fracCount |= roundUp; fracCount <<=2;} fracMsec = milliseconds{fracCount};recalculate(fracMsec);return;} milliseconds min =std::max(0ms, fracMsec - absMaxRounding); milliseconds max =std::min(1000ms, fracMsec + absMaxRounding);// find the boundary that we want, according to the rules above// extra rules:// 1) whatever the interval, we'll take any round-to-the-second timeoutif(min ==0ms) { fracMsec =0ms;recalculate(fracMsec);return;}else if(max ==1000ms) { fracMsec =1000ms;recalculate(fracMsec);return;} milliseconds wantedBoundaryMultiple{25};// 2) if the interval is a multiple of 500 ms and > 5000 ms, we'll always round// towards a round-to-the-second// 3) if the interval is a multiple of 500 ms, we'll round towards the nearest// multiple of 500 msif((interval %500) ==0ms) {if(interval >=5s) { fracMsec = fracMsec >=500ms ? max : min;recalculate(fracMsec);return;}else{ wantedBoundaryMultiple =500ms;}}else if((interval %50) ==0ms) {// 4) same for multiples of 250, 200, 100, 50 milliseconds mult50 = interval /50;if((mult50 %4) ==0ms) {// multiple of 200 wantedBoundaryMultiple =200ms;}else if((mult50 %2) ==0ms) {// multiple of 100 wantedBoundaryMultiple =100ms;}else if((mult50 %5) ==0ms) {// multiple of 250 wantedBoundaryMultiple =250ms;}else{// multiple of 50 wantedBoundaryMultiple =50ms;}} milliseconds base = (fracMsec / wantedBoundaryMultiple) * wantedBoundaryMultiple; milliseconds middlepoint = base + wantedBoundaryMultiple /2;if(fracMsec < middlepoint) fracMsec =qMax(base, min);else fracMsec =qMin(base + wantedBoundaryMultiple, max);recalculate(fracMsec);}static voidcalculateNextTimeout(QTimerInfo *t,steady_clock::time_point now){switch(t->timerType) {caseQt::PreciseTimer:caseQt::CoarseTimer: t->timeout += t->interval;if(t->timeout < now) { t->timeout = now; t->timeout += t->interval;}if(t->timerType ==Qt::CoarseTimer)calculateCoarseTimerTimeout(t, now);return;caseQt::VeryCoarseTimer:// t->interval already rounded to full seconds in registerTimer() t->timeout += t->interval;if(t->timeout <= now) t->timeout = time_point_cast<seconds>(now + t->interval);break;}}/* Returns the time to wait for the first timer that has not been activated yet, otherwise returns std::nullopt. */std::optional<QTimerInfoList::Duration>QTimerInfoList::timerWait(){steady_clock::time_point now =updateCurrentTime();auto isWaiting = [](QTimerInfo *tinfo) {return!tinfo->activateRef; };// Find first waiting timer not already activeauto it =std::find_if(timers.cbegin(), timers.cend(), isWaiting);if(it == timers.cend())returnstd::nullopt; Duration timeToWait = (*it)->timeout - now;if(timeToWait >0ns)returnroundToMillisecond(timeToWait);return0ms;}/* Returns the timer's remaining time in milliseconds with the given timerId. If the timer id is not found in the list, the returned value will be \c{Duration::min()}. If the timer is overdue, the returned value will be 0.*/QTimerInfoList::Duration QTimerInfoList::remainingDuration(Qt::TimerId timerId)const{conststeady_clock::time_point now =updateCurrentTime();auto it =findTimerById(timerId);if(it == timers.cend()) {#ifndef QT_NO_DEBUGqWarning("QTimerInfoList::timerRemainingTime: timer id %i not found",int(timerId));#endifreturnDuration::min();}const QTimerInfo *t = *it;if(now < t->timeout)// time to waitreturn t->timeout - now;return0ms;}voidQTimerInfoList::registerTimer(Qt::TimerId timerId,QTimerInfoList::Duration interval,Qt::TimerType timerType, QObject *object){// correct the timer type firstif(timerType ==Qt::CoarseTimer) {// this timer has up to 5% coarseness// so our boundaries are 20 ms and 20 s// below 20 ms, 5% inaccuracy is below 1 ms, so we convert to high precision// above 20 s, 5% inaccuracy is above 1 s, so we convert to VeryCoarseTimerif(interval >=20s) timerType =Qt::VeryCoarseTimer;else if(interval <=20ms) timerType =Qt::PreciseTimer;} QTimerInfo *t =newQTimerInfo(timerId, interval, timerType, object);QTimerInfo::TimePoint expected =updateCurrentTime() + interval;switch(timerType) {caseQt::PreciseTimer:// high precision timer is based on millisecond precision// so no adjustment is necessary t->timeout = expected;break;caseQt::CoarseTimer: t->timeout = expected; t->interval =roundToMillisecond(interval);calculateCoarseTimerTimeout(t, currentTime);break;caseQt::VeryCoarseTimer: t->interval =roundToSecs(t->interval);constauto currentTimeInSecs = floor<seconds>(currentTime); t->timeout = currentTimeInSecs + t->interval;// If we're past the half-second mark, increase the timeout againif(currentTime - currentTimeInSecs >500ms) t->timeout +=1s;}timerInsert(t);}boolQTimerInfoList::unregisterTimer(Qt::TimerId timerId){auto it =findTimerById(timerId);if(it == timers.cend())return false;// id not found// set timer inactive QTimerInfo *t = *it;if(t == firstTimerInfo) firstTimerInfo =nullptr;if(t->activateRef)*(t->activateRef) =nullptr;delete t; timers.erase(it);return true;}boolQTimerInfoList::unregisterTimers(QObject *object){if(timers.isEmpty())return false;auto associatedWith = [this](QObject *o) {return[this, o](auto&t) {if(t->obj == o) {if(t == firstTimerInfo) firstTimerInfo =nullptr;if(t->activateRef)*(t->activateRef) =nullptr;delete t;return true;}return false;};}; qsizetype count = timers.removeIf(associatedWith(object));return count >0;}auto QTimerInfoList::registeredTimers(QObject *object)const-> QList<TimerInfo>{ QList<TimerInfo> list;for(constauto&t : timers) {if(t->obj == object) list.emplaceBack(TimerInfo{t->interval, t->id, t->timerType});}return list;}/* Activate pending timers, returning how many where activated.*/intQTimerInfoList::activateTimers(){if(qt_disable_lowpriority_timers || timers.isEmpty())return0;// nothing to do firstTimerInfo =nullptr;conststeady_clock::time_point now =updateCurrentTime();// qDebug() << "Thread" << QThread::currentThreadId() << "woken up at" << now;// Find out how many timer have expiredauto stillActive = [&now](const QTimerInfo *t) {return now < t->timeout; };// Find first one still active (list is sorted by timeout)auto it =std::find_if(timers.cbegin(), timers.cend(), stillActive);auto maxCount = it - timers.cbegin();int n_act =0;//fire the timers.while(maxCount--) {if(timers.isEmpty())break; QTimerInfo *currentTimerInfo = timers.constFirst();if(now < currentTimerInfo->timeout)break;// no timer has expiredif(!firstTimerInfo) { firstTimerInfo = currentTimerInfo;}else if(firstTimerInfo == currentTimerInfo) {// avoid sending the same timer multiple timesbreak;}else if(currentTimerInfo->interval < firstTimerInfo->interval || currentTimerInfo->interval == firstTimerInfo->interval) { firstTimerInfo = currentTimerInfo;}// determine next timeout timecalculateNextTimeout(currentTimerInfo, now);if(timers.size() >1) {// Find where "currentTimerInfo" should be in the list so as// to keep the list ordered by timeoutauto afterCurrentIt = timers.begin() +1;auto iter =std::upper_bound(afterCurrentIt, timers.end(), currentTimerInfo, byTimeout); currentTimerInfo = *std::rotate(timers.begin(), afterCurrentIt, iter);}if(currentTimerInfo->interval >0ms) n_act++;// Send event, but don't allow it to recurse:if(!currentTimerInfo->activateRef) { currentTimerInfo->activateRef = &currentTimerInfo; QTimerEvent e(currentTimerInfo->id);QCoreApplication::sendEvent(currentTimerInfo->obj, &e);// Storing currentTimerInfo's address in its activateRef allows the// handling of that event to clear this local variable on deletion// of the object it points to - if it didn't, clear activateRef:if(currentTimerInfo) currentTimerInfo->activateRef =nullptr;}} firstTimerInfo =nullptr;// qDebug() << "Thread" << QThread::currentThreadId() << "activated" << n_act << "timers";return n_act;} QT_END_NAMESPACE 
close