Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/concurrent/qtconcurrentiteratekernel.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
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 | - | |||||||
61 | QT_BEGIN_NAMESPACE | - | ||||||
62 | - | |||||||
63 | enum { | - | ||||||
64 | TargetRatio = 100, | - | ||||||
65 | MedianSize = 7 | - | ||||||
66 | }; | - | ||||||
67 | - | |||||||
68 | #if defined(Q_OS_MAC) | - | ||||||
69 | - | |||||||
70 | static qint64 getticks() | - | ||||||
71 | { | - | ||||||
72 | return mach_absolute_time(); | - | ||||||
73 | } | - | ||||||
74 | - | |||||||
75 | #elif defined(Q_OS_UNIX) | - | ||||||
76 | - | |||||||
77 | - | |||||||
78 | static 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) {
| 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 ;
| 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:
| 4 | ||||||
99 | if (useThreadCpuTime != -1)
| 0-37206 | ||||||
100 | clockId = CLOCK_THREAD_CPUTIME_ID; executed 37206 times by 4 tests: clockId = 3; Executed by:
| 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)
| 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:
| 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 | - | |||||||
121 | static qint64 getticks() | - | ||||||
122 | { | - | ||||||
123 | LARGE_INTEGER x; | - | ||||||
124 | if (!QueryPerformanceCounter(&x)) | - | ||||||
125 | return 0; | - | ||||||
126 | return x.QuadPart; | - | ||||||
127 | } | - | ||||||
128 | - | |||||||
129 | #endif | - | ||||||
130 | - | |||||||
131 | static double elapsed(qint64 after, qint64 before) | - | ||||||
132 | { | - | ||||||
133 | return double(after - before); executed 37206 times by 4 tests: return double(after - before); Executed by:
| 37206 | ||||||
134 | } | - | ||||||
135 | - | |||||||
136 | namespace QtConcurrent { | - | ||||||
137 | - | |||||||
138 | /*! \internal | - | ||||||
139 | - | |||||||
140 | */ | - | ||||||
141 | BlockSizeManager::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. | - | ||||||
149 | void BlockSizeManager::timeBeforeUser() | - | ||||||
150 | { | - | ||||||
151 | if (blockSizeMaxed())
| 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. | - | ||||||
160 | void BlockSizeManager::timeAfterUser() | - | ||||||
161 | { | - | ||||||
162 | if (blockSizeMaxed())
| 0 | ||||||
163 | return; never executed: return; | 0 | ||||||
164 | - | |||||||
165 | afterUser = getticks(); | - | ||||||
166 | userPartElapsed.addValue(elapsed(afterUser, beforeUser)); | - | ||||||
167 | - | |||||||
168 | if (controlPartElapsed.isMedianValid() == false)
| 0 | ||||||
169 | return; never executed: return; | 0 | ||||||
170 | - | |||||||
171 | if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median())
| 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 | - | |||||||
186 | int BlockSizeManager::blockSize() | - | ||||||
187 | { | - | ||||||
188 | return m_blockSize; never executed: return m_blockSize; | 0 | ||||||
189 | } | - | ||||||
190 | - | |||||||
191 | /*! \internal | - | ||||||
192 | - | |||||||
193 | */ | - | ||||||
194 | BlockSizeManagerV2::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:
executed 20972 times by 4 tests: end of block Executed by:
| 20972 | ||||||
199 | - | |||||||
200 | // Records the time before user code. | - | ||||||
201 | void BlockSizeManagerV2::timeBeforeUser() | - | ||||||
202 | { | - | ||||||
203 | if (blockSizeMaxed())
| 560-18603 | ||||||
204 | return; executed 560 times by 2 tests: return; Executed by:
| 560 | ||||||
205 | - | |||||||
206 | beforeUser = getticks(); | - | ||||||
207 | controlPartElapsed.addValue(elapsed(beforeUser, afterUser)); | - | ||||||
208 | } executed 18603 times by 4 tests: end of block Executed by:
| 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. | - | ||||||
212 | void BlockSizeManagerV2::timeAfterUser() | - | ||||||
213 | { | - | ||||||
214 | if (blockSizeMaxed())
| 559-18603 | ||||||
215 | return; executed 559 times by 2 tests: return; Executed by:
| 559 | ||||||
216 | - | |||||||
217 | afterUser = getticks(); | - | ||||||
218 | userPartElapsed.addValue(elapsed(afterUser, beforeUser)); | - | ||||||
219 | - | |||||||
220 | if (controlPartElapsed.isMedianValid() == false)
| 2531-16072 | ||||||
221 | return; executed 16072 times by 4 tests: return; Executed by:
| 16072 | ||||||
222 | - | |||||||
223 | if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median())
| 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:
| 2531 | ||||||
237 | - | |||||||
238 | int BlockSizeManagerV2::blockSize() | - | ||||||
239 | { | - | ||||||
240 | return m_blockSize; executed 40039 times by 4 tests: return m_blockSize; Executed by:
| 40039 | ||||||
241 | } | - | ||||||
242 | - | |||||||
243 | } // namespace QtConcurrent | - | ||||||
244 | - | |||||||
245 | QT_END_NAMESPACE | - | ||||||
246 | - | |||||||
247 | #endif // QT_NO_CONCURRENT | - | ||||||
Source code | Switch to Preprocessed file |