qtconcurrentiteratekernel.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/concurrent/qtconcurrentiteratekernel.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
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 QtCore 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#if defined(Q_OS_MAC)-
43#include <mach/mach.h>-
44#include <mach/mach_time.h>-
45#include <unistd.h>-
46#elif defined(Q_OS_UNIX)-
47#if defined(Q_OS_HURD)-
48#include <sys/time.h>-
49#endif-
50#include <time.h>-
51#include <unistd.h>-
52#elif defined(Q_OS_WIN)-
53#include <qt_windows.h>-
54#endif-
55-
56#include "private/qfunctions_p.h"-
57-
58-
59#ifndef QT_NO_CONCURRENT-
60-
61QT_BEGIN_NAMESPACE-
62-
63enum {-
64 TargetRatio = 100,-
65 MedianSize = 7-
66};-
67-
68#if defined(Q_OS_MAC)-
69-
70static qint64 getticks()-
71{-
72 return mach_absolute_time();-
73}-
74-
75#elif defined(Q_OS_UNIX)-
76-
77-
78static qint64 getticks()-
79{-
80#if (defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)) || defined(Q_OS_OPENBSD)-
81 clockid_t clockId;-
82-
83#ifndef _POSIX_THREAD_CPUTIME-
84 clockId = CLOCK_REALTIME;-
85#elif (_POSIX_THREAD_CPUTIME-0 <= 0)-
86 // if we don't have CLOCK_THREAD_CPUTIME_ID, we have to just use elapsed realtime instead-
87 clockId = CLOCK_REALTIME;-
88-
89# if (_POSIX_THREAD_CPUTIME-0 == 0)-
90 // detect availablility of CLOCK_THREAD_CPUTIME_ID-
91 static QBasicAtomicInt sUseThreadCpuTime = Q_BASIC_ATOMIC_INITIALIZER(-2);-
92 int useThreadCpuTime = sUseThreadCpuTime.load();-
93 if (useThreadCpuTime == -2) {
useThreadCpuTime == -2Description
TRUEevaluated 4 times by 4 tests
Evaluated by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
FALSEevaluated 37202 times by 4 tests
Evaluated by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
4-37202
94 // sysconf() will return either -1L or _POSIX_VERSION-
95 // (don't care about sysconf's exact return value)-
96 useThreadCpuTime = sysconf(_SC_THREAD_CPUTIME) == -1L ? -1 : 0 ;
sysconf(_SC_TH...PUTIME) == -1LDescription
TRUEnever evaluated
FALSEevaluated 4 times by 4 tests
Evaluated by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
0-4
97 sUseThreadCpuTime.store(useThreadCpuTime); // might happen multiple times, but doesn't matter-
98 }
executed 4 times by 4 tests: end of block
Executed by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
4
99 if (useThreadCpuTime != -1)
useThreadCpuTime != -1Description
TRUEevaluated 37206 times by 4 tests
Evaluated by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
FALSEnever evaluated
0-37206
100 clockId = CLOCK_THREAD_CPUTIME_ID;
executed 37206 times by 4 tests: clockId = 3;
Executed by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
37206
101# endif-
102#else-
103 clockId = CLOCK_THREAD_CPUTIME_ID;-
104#endif-
105-
106 struct timespec ts;-
107 if (clock_gettime(clockId, &ts) == -1)
clock_gettime(...Id, &ts) == -1Description
TRUEnever evaluated
FALSEevaluated 37206 times by 4 tests
Evaluated by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
0-37206
108 return 0;
never executed: return 0;
0
109 return (ts.tv_sec * 1000000000) + ts.tv_nsec;
executed 37206 times by 4 tests: return (ts.tv_sec * 1000000000) + ts.tv_nsec;
Executed by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
37206
110#else-
111-
112 // no clock_gettime(), fall back to wall time-
113 struct timeval tv;-
114 gettimeofday(&tv, 0);-
115 return (tv.tv_sec * 1000000) + tv.tv_usec;-
116#endif-
117}-
118-
119#elif defined(Q_OS_WIN)-
120-
121static qint64 getticks()-
122{-
123 LARGE_INTEGER x;-
124 if (!QueryPerformanceCounter(&x))-
125 return 0;-
126 return x.QuadPart;-
127}-
128-
129#endif-
130-
131static double elapsed(qint64 after, qint64 before)-
132{-
133 return double(after - before);
executed 37206 times by 4 tests: return double(after - before);
Executed by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
37206
134}-
135-
136namespace QtConcurrent {-
137-
138/*! \internal-
139-
140*/-
141BlockSizeManager::BlockSizeManager(int iterationCount)-
142: maxBlockSize(iterationCount / (QThreadPool::globalInstance()->maxThreadCount() * 2)),-
143 beforeUser(0), afterUser(0),-
144 controlPartElapsed(MedianSize), userPartElapsed(MedianSize),-
145 m_blockSize(1)-
146{
never executed: end of block
}
never executed: end of block
0
147-
148// Records the time before user code.-
149void BlockSizeManager::timeBeforeUser()-
150{-
151 if (blockSizeMaxed())
blockSizeMaxed()Description
TRUEnever evaluated
FALSEnever evaluated
0
152 return;
never executed: return;
0
153-
154 beforeUser = getticks();-
155 controlPartElapsed.addValue(elapsed(beforeUser, afterUser));-
156}
never executed: end of block
0
157-
158 // Records the time after user code and adjust the block size if we are spending-
159 // to much time in the for control code compared with the user code.-
160void BlockSizeManager::timeAfterUser()-
161{-
162 if (blockSizeMaxed())
blockSizeMaxed()Description
TRUEnever evaluated
FALSEnever evaluated
0
163 return;
never executed: return;
0
164-
165 afterUser = getticks();-
166 userPartElapsed.addValue(elapsed(afterUser, beforeUser));-
167-
168 if (controlPartElapsed.isMedianValid() == false)
controlPartEla...lid() == falseDescription
TRUEnever evaluated
FALSEnever evaluated
0
169 return;
never executed: return;
0
170-
171 if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median())
controlPartEla...apsed.median()Description
TRUEnever evaluated
FALSEnever evaluated
0
172 return;
never executed: return;
0
173-
174 m_blockSize = qMin(m_blockSize * 2, maxBlockSize);-
175-
176#ifdef QTCONCURRENT_FOR_DEBUG-
177 qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize;-
178#endif-
179-
180 // Reset the medians after adjusting the block size so we get-
181 // new measurements with the new block size.-
182 controlPartElapsed.reset();-
183 userPartElapsed.reset();-
184}
never executed: end of block
0
185-
186int BlockSizeManager::blockSize()-
187{-
188 return m_blockSize;
never executed: return m_blockSize;
0
189}-
190-
191/*! \internal-
192-
193*/-
194BlockSizeManagerV2::BlockSizeManagerV2(int iterationCount)-
195 : maxBlockSize(iterationCount / (QThreadPool::globalInstance()->maxThreadCount() * 2)),-
196 beforeUser(0), afterUser(0),-
197 m_blockSize(1)-
198{
executed 20972 times by 4 tests: end of block
Executed by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
}
executed 20972 times by 4 tests: end of block
Executed by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
20972
199-
200// Records the time before user code.-
201void BlockSizeManagerV2::timeBeforeUser()-
202{-
203 if (blockSizeMaxed())
blockSizeMaxed()Description
TRUEevaluated 560 times by 2 tests
Evaluated by:
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentmap - unknown status
FALSEevaluated 18603 times by 4 tests
Evaluated by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
560-18603
204 return;
executed 560 times by 2 tests: return;
Executed by:
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentmap - unknown status
560
205-
206 beforeUser = getticks();-
207 controlPartElapsed.addValue(elapsed(beforeUser, afterUser));-
208}
executed 18603 times by 4 tests: end of block
Executed by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
18603
209-
210 // Records the time after user code and adjust the block size if we are spending-
211 // to much time in the for control code compared with the user code.-
212void BlockSizeManagerV2::timeAfterUser()-
213{-
214 if (blockSizeMaxed())
blockSizeMaxed()Description
TRUEevaluated 559 times by 2 tests
Evaluated by:
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentmap - unknown status
FALSEevaluated 18603 times by 4 tests
Evaluated by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
559-18603
215 return;
executed 559 times by 2 tests: return;
Executed by:
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentmap - unknown status
559
216-
217 afterUser = getticks();-
218 userPartElapsed.addValue(elapsed(afterUser, beforeUser));-
219-
220 if (controlPartElapsed.isMedianValid() == false)
controlPartEla...lid() == falseDescription
TRUEevaluated 16072 times by 4 tests
Evaluated by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
FALSEevaluated 2531 times by 4 tests
Evaluated by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
2531-16072
221 return;
executed 16072 times by 4 tests: return;
Executed by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
16072
222-
223 if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median())
controlPartEla...apsed.median()Description
TRUEnever evaluated
FALSEevaluated 2531 times by 4 tests
Evaluated by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
0-2531
224 return;
never executed: return;
0
225-
226 m_blockSize = qMin(m_blockSize * 2, maxBlockSize);-
227-
228#ifdef QTCONCURRENT_FOR_DEBUG-
229 qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize;-
230#endif-
231-
232 // Reset the medians after adjusting the block size so we get-
233 // new measurements with the new block size.-
234 controlPartElapsed.reset();-
235 userPartElapsed.reset();-
236}
executed 2531 times by 4 tests: end of block
Executed by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
2531
237-
238int BlockSizeManagerV2::blockSize()-
239{-
240 return m_blockSize;
executed 40039 times by 4 tests: return m_blockSize;
Executed by:
  • tst_qfuturewatcher - unknown status
  • tst_qtconcurrentfilter - unknown status
  • tst_qtconcurrentiteratekernel - unknown status
  • tst_qtconcurrentmap - unknown status
40039
241}-
242-
243} // namespace QtConcurrent-
244-
245QT_END_NAMESPACE-
246-
247#endif // QT_NO_CONCURRENT-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9