Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qtconcurrentiteratekernel.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <qdeadlinetimer.h>
7#include "private/qfunctions_p.h"
8
9
10#if !defined(QT_NO_CONCURRENT) || defined(Q_QDOC)
11
13
14enum {
15 TargetRatio = 100
16};
17
19{
20 return QDeadlineTimer::current(Qt::PreciseTimer).deadlineNSecs();
21}
22
23static double elapsed(qint64 after, qint64 before)
24{
25 return double(after - before);
26}
27
28namespace QtConcurrent {
29
70 : maxBlockSize(iterationCount / (std::max(pool->maxThreadCount(), 1) * 2)),
71 beforeUser(0), afterUser(0),
72 m_blockSize(1)
73{ }
74
75// Records the time before user code.
77{
78 if (blockSizeMaxed())
79 return;
80
81 beforeUser = getticks();
82 controlPartElapsed.addValue(elapsed(beforeUser, afterUser));
83}
84
85 // Records the time after user code and adjust the block size if we are spending
86 // to much time in the for control code compared with the user code.
88{
89 if (blockSizeMaxed())
90 return;
91
92 afterUser = getticks();
93 userPartElapsed.addValue(elapsed(afterUser, beforeUser));
94
95 if (controlPartElapsed.isMedianValid() == false)
96 return;
97
98 if (controlPartElapsed.median() * int(TargetRatio) < userPartElapsed.median())
99 return;
100
101 m_blockSize = qMin(m_blockSize * 2, maxBlockSize);
102
103#ifdef QTCONCURRENT_FOR_DEBUG
104 qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize;
105#endif
106
107 // Reset the medians after adjusting the block size so we get
108 // new measurements with the new block size.
109 controlPartElapsed.reset();
110 userPartElapsed.reset();
111}
112
114{
115 return m_blockSize;
116}
117
118} // namespace QtConcurrent
119
121
122#endif // QT_NO_CONCURRENT
static QDeadlineTimer current(Qt::TimerType timerType=Qt::CoarseTimer) noexcept
Returns a QDeadlineTimer that is expired but is guaranteed to contain the current time.
\inmodule QtCore
Definition qthreadpool.h:22
static QThread * currentThread()
Definition qthread.cpp:1039
BlockSizeManager(QThreadPool *pool, int iterationCount)
void addValue(double value)
Combined button and popup list for selecting options.
\inmodule QtConcurrent
@ PreciseTimer
#define qDebug
[1]
Definition qlogging.h:164
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
static qint64 getticks()
static double elapsed(qint64 after, qint64 before)
long long qint64
Definition qtypes.h:60