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// Qt-Security score:significant reason:default
4
5#include "qtconcurrentiteratekernel.h"
6
7#include <qdeadlinetimer.h>
8#include "private/qfunctions_p.h"
9
10
11#if !defined(QT_NO_CONCURRENT) || defined(Q_QDOC)
12
13QT_BEGIN_NAMESPACE
14
15enum {
16 TargetRatio = 100
17};
18
19static qint64 getticks()
20{
21 return QDeadlineTimer::current(timerType: Qt::PreciseTimer).deadlineNSecs();
22}
23
24static double elapsed(qint64 after, qint64 before)
25{
26 return double(after - before);
27}
28
29namespace QtConcurrent {
30
31/*!
32 \class QtConcurrent::Median
33 \inmodule QtConcurrent
34 \internal
35 */
36
37/*!
38 \class QtConcurrent::BlockSizeManager
39 \inmodule QtConcurrent
40 \internal
41 */
42
43/*!
44 \class QtConcurrent::ResultReporter
45 \inmodule QtConcurrent
46 \internal
47 */
48
49/*! \fn bool QtConcurrent::selectIteration(std::bidirectional_iterator_tag)
50 \internal
51 */
52
53/*! \fn bool QtConcurrent::selectIteration(std::forward_iterator_tag)
54 \internal
55 */
56
57/*! \fn bool QtConcurrent::selectIteration(std::random_access_iterator_tag)
58 \internal
59 */
60
61/*!
62 \class QtConcurrent::IterateKernel
63 \inmodule QtConcurrent
64 \internal
65 */
66
67/*! \internal
68
69*/
70BlockSizeManager::BlockSizeManager(QThreadPool *pool, int iterationCount)
71 : maxBlockSize(iterationCount / (std::max(a: pool->maxThreadCount(), b: 1) * 2)),
72 beforeUser(0), afterUser(0),
73 m_blockSize(1)
74{ }
75
76// Records the time before user code.
77void BlockSizeManager::timeBeforeUser()
78{
79 if (blockSizeMaxed())
80 return;
81
82 beforeUser = getticks();
83 controlPartElapsed.addValue(value: elapsed(after: beforeUser, before: afterUser));
84}
85
86 // Records the time after user code and adjust the block size if we are spending
87 // to much time in the for control code compared with the user code.
88void BlockSizeManager::timeAfterUser()
89{
90 if (blockSizeMaxed())
91 return;
92
93 afterUser = getticks();
94 userPartElapsed.addValue(value: elapsed(after: afterUser, before: beforeUser));
95
96 if (controlPartElapsed.isMedianValid() == false)
97 return;
98
99 if (controlPartElapsed.median() * int(TargetRatio) < userPartElapsed.median())
100 return;
101
102 m_blockSize = qMin(a: m_blockSize * 2, b: maxBlockSize);
103
104#ifdef QTCONCURRENT_FOR_DEBUG
105 qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize;
106#endif
107
108 // Reset the medians after adjusting the block size so we get
109 // new measurements with the new block size.
110 controlPartElapsed.reset();
111 userPartElapsed.reset();
112}
113
114int BlockSizeManager::blockSize()
115{
116 return m_blockSize;
117}
118
119} // namespace QtConcurrent
120
121QT_END_NAMESPACE
122
123#endif // QT_NO_CONCURRENT
124

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