1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtConcurrent module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qtconcurrentiteratekernel.h"
41
42#include <qdeadlinetimer.h>
43#include "private/qfunctions_p.h"
44
45
46#if !defined(QT_NO_CONCURRENT) || defined(Q_CLANG_QDOC)
47
48QT_BEGIN_NAMESPACE
49
50enum {
51 TargetRatio = 100
52};
53
54static qint64 getticks()
55{
56 return QDeadlineTimer::current(Qt::PreciseTimer).deadlineNSecs();
57}
58
59static double elapsed(qint64 after, qint64 before)
60{
61 return double(after - before);
62}
63
64namespace QtConcurrent {
65
66/*!
67 \class QtConcurrent::Median
68 \inmodule QtConcurrent
69 \internal
70 */
71
72/*!
73 \class QtConcurrent::BlockSizeManager
74 \inmodule QtConcurrent
75 \internal
76 */
77
78/*!
79 \class QtConcurrent::ResultReporter
80 \inmodule QtConcurrent
81 \internal
82 */
83
84/*! \fn bool QtConcurrent::selectIteration(std::bidirectional_iterator_tag)
85 \internal
86 */
87
88/*! \fn bool QtConcurrent::selectIteration(std::forward_iterator_tag)
89 \internal
90 */
91
92/*! \fn bool QtConcurrent::selectIteration(std::random_access_iterator_tag)
93 \internal
94 */
95
96/*!
97 \class QtConcurrent::IterateKernel
98 \inmodule QtConcurrent
99 \internal
100 */
101
102/*! \internal
103
104*/
105BlockSizeManager::BlockSizeManager(QThreadPool *pool, int iterationCount)
106 : maxBlockSize(iterationCount / (pool->maxThreadCount() * 2)),
107 beforeUser(0), afterUser(0),
108 m_blockSize(1)
109{ }
110
111// Records the time before user code.
112void BlockSizeManager::timeBeforeUser()
113{
114 if (blockSizeMaxed())
115 return;
116
117 beforeUser = getticks();
118 controlPartElapsed.addValue(elapsed(beforeUser, afterUser));
119}
120
121 // Records the time after user code and adjust the block size if we are spending
122 // to much time in the for control code compared with the user code.
123void BlockSizeManager::timeAfterUser()
124{
125 if (blockSizeMaxed())
126 return;
127
128 afterUser = getticks();
129 userPartElapsed.addValue(elapsed(afterUser, beforeUser));
130
131 if (controlPartElapsed.isMedianValid() == false)
132 return;
133
134 if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median())
135 return;
136
137 m_blockSize = qMin(m_blockSize * 2, maxBlockSize);
138
139#ifdef QTCONCURRENT_FOR_DEBUG
140 qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize;
141#endif
142
143 // Reset the medians after adjusting the block size so we get
144 // new measurements with the new block size.
145 controlPartElapsed.reset();
146 userPartElapsed.reset();
147}
148
149int BlockSizeManager::blockSize()
150{
151 return m_blockSize;
152}
153
154} // namespace QtConcurrent
155
156QT_END_NAMESPACE
157
158#endif // QT_NO_CONCURRENT
159