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
4#include "qtconcurrentiteratekernel.h"
5
6#include <qdeadlinetimer.h>
7#include "private/qfunctions_p.h"
8
9
10#if !defined(QT_NO_CONCURRENT) || defined(Q_QDOC)
11
12QT_BEGIN_NAMESPACE
13
14enum {
15 TargetRatio = 100
16};
17
18static qint64 getticks()
19{
20 return QDeadlineTimer::current(timerType: Qt::PreciseTimer).deadlineNSecs();
21}
22
23static double elapsed(qint64 after, qint64 before)
24{
25 return double(after - before);
26}
27
28namespace QtConcurrent {
29
30/*!
31 \class QtConcurrent::Median
32 \inmodule QtConcurrent
33 \internal
34 */
35
36/*!
37 \class QtConcurrent::BlockSizeManager
38 \inmodule QtConcurrent
39 \internal
40 */
41
42/*!
43 \class QtConcurrent::ResultReporter
44 \inmodule QtConcurrent
45 \internal
46 */
47
48/*! \fn bool QtConcurrent::selectIteration(std::bidirectional_iterator_tag)
49 \internal
50 */
51
52/*! \fn bool QtConcurrent::selectIteration(std::forward_iterator_tag)
53 \internal
54 */
55
56/*! \fn bool QtConcurrent::selectIteration(std::random_access_iterator_tag)
57 \internal
58 */
59
60/*!
61 \class QtConcurrent::IterateKernel
62 \inmodule QtConcurrent
63 \internal
64 */
65
66/*! \internal
67
68*/
69BlockSizeManager::BlockSizeManager(QThreadPool *pool, int iterationCount)
70 : maxBlockSize(iterationCount / (pool->maxThreadCount() * 2)),
71 beforeUser(0), afterUser(0),
72 m_blockSize(1)
73{ }
74
75// Records the time before user code.
76void BlockSizeManager::timeBeforeUser()
77{
78 if (blockSizeMaxed())
79 return;
80
81 beforeUser = getticks();
82 controlPartElapsed.addValue(value: elapsed(after: beforeUser, before: 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.
87void BlockSizeManager::timeAfterUser()
88{
89 if (blockSizeMaxed())
90 return;
91
92 afterUser = getticks();
93 userPartElapsed.addValue(value: elapsed(after: afterUser, before: 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(a: m_blockSize * 2, b: 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
113int BlockSizeManager::blockSize()
114{
115 return m_blockSize;
116}
117
118} // namespace QtConcurrent
119
120QT_END_NAMESPACE
121
122#endif // QT_NO_CONCURRENT
123

source code of qtbase/src/concurrent/qtconcurrentiteratekernel.cpp