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) 2015 The Qt Company Ltd. | - | ||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||
25 | ** | - | ||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||
29 | ** | - | ||||||
30 | ** $QT_END_LICENSE$ | - | ||||||
31 | ** | - | ||||||
32 | ****************************************************************************/ | - | ||||||
33 | - | |||||||
34 | #include "qtconcurrentiteratekernel.h" | - | ||||||
35 | - | |||||||
36 | #if defined(Q_OS_MAC) | - | ||||||
37 | #include <mach/mach.h> | - | ||||||
38 | #include <mach/mach_time.h> | - | ||||||
39 | #include <unistd.h> | - | ||||||
40 | #elif defined(Q_OS_UNIX) | - | ||||||
41 | #if defined(Q_OS_HURD) | - | ||||||
42 | #include <sys/time.h> | - | ||||||
43 | #endif | - | ||||||
44 | #include <time.h> | - | ||||||
45 | #include <unistd.h> | - | ||||||
46 | #elif defined(Q_OS_WIN) | - | ||||||
47 | #include <qt_windows.h> | - | ||||||
48 | #endif | - | ||||||
49 | - | |||||||
50 | #include "private/qfunctions_p.h" | - | ||||||
51 | - | |||||||
52 | - | |||||||
53 | #ifndef QT_NO_CONCURRENT | - | ||||||
54 | - | |||||||
55 | QT_BEGIN_NAMESPACE | - | ||||||
56 | - | |||||||
57 | enum { | - | ||||||
58 | TargetRatio = 100, | - | ||||||
59 | MedianSize = 7 | - | ||||||
60 | }; | - | ||||||
61 | - | |||||||
62 | #if defined(Q_OS_MAC) | - | ||||||
63 | - | |||||||
64 | static qint64 getticks() | - | ||||||
65 | { | - | ||||||
66 | return mach_absolute_time(); | - | ||||||
67 | } | - | ||||||
68 | - | |||||||
69 | #elif defined(Q_OS_UNIX) | - | ||||||
70 | - | |||||||
71 | - | |||||||
72 | static qint64 getticks() | - | ||||||
73 | { | - | ||||||
74 | #if (defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)) || defined(Q_OS_OPENBSD) | - | ||||||
75 | clockid_t clockId; | - | ||||||
76 | - | |||||||
77 | #ifndef _POSIX_THREAD_CPUTIME | - | ||||||
78 | clockId = CLOCK_REALTIME; | - | ||||||
79 | #elif (_POSIX_THREAD_CPUTIME-0 <= 0) | - | ||||||
80 | // if we don't have CLOCK_THREAD_CPUTIME_ID, we have to just use elapsed realtime instead | - | ||||||
81 | clockId = CLOCK_REALTIME; | - | ||||||
82 | - | |||||||
83 | # if (_POSIX_THREAD_CPUTIME-0 == 0) | - | ||||||
84 | // detect availablility of CLOCK_THREAD_CPUTIME_ID | - | ||||||
85 | static QBasicAtomicInt sUseThreadCpuTime = Q_BASIC_ATOMIC_INITIALIZER(-2); | - | ||||||
86 | int useThreadCpuTime = sUseThreadCpuTime.load(); | - | ||||||
87 | if (useThreadCpuTime == -2) {
| 4-37244 | ||||||
88 | // sysconf() will return either -1L or _POSIX_VERSION | - | ||||||
89 | // (don't care about sysconf's exact return value) | - | ||||||
90 | useThreadCpuTime = sysconf(_SC_THREAD_CPUTIME) == -1L ? -1 : 0 ;
| 0-4 | ||||||
91 | sUseThreadCpuTime.store(useThreadCpuTime); // might happen multiple times, but doesn't matter | - | ||||||
92 | } executed 4 times by 4 tests: end of block Executed by:
| 4 | ||||||
93 | if (useThreadCpuTime != -1)
| 0-37248 | ||||||
94 | clockId = CLOCK_THREAD_CPUTIME_ID; executed 37248 times by 4 tests: clockId = 3; Executed by:
| 37248 | ||||||
95 | # endif | - | ||||||
96 | #else | - | ||||||
97 | clockId = CLOCK_THREAD_CPUTIME_ID; | - | ||||||
98 | #endif | - | ||||||
99 | - | |||||||
100 | struct timespec ts; | - | ||||||
101 | if (clock_gettime(clockId, &ts) == -1)
| 0-37248 | ||||||
102 | return 0; never executed: return 0; | 0 | ||||||
103 | return (ts.tv_sec * 1000000000) + ts.tv_nsec; executed 37248 times by 4 tests: return (ts.tv_sec * 1000000000) + ts.tv_nsec; Executed by:
| 37248 | ||||||
104 | #else | - | ||||||
105 | - | |||||||
106 | // no clock_gettime(), fall back to wall time | - | ||||||
107 | struct timeval tv; | - | ||||||
108 | gettimeofday(&tv, 0); | - | ||||||
109 | return (tv.tv_sec * 1000000) + tv.tv_usec; | - | ||||||
110 | #endif | - | ||||||
111 | } | - | ||||||
112 | - | |||||||
113 | #elif defined(Q_OS_WIN) | - | ||||||
114 | - | |||||||
115 | static qint64 getticks() | - | ||||||
116 | { | - | ||||||
117 | LARGE_INTEGER x; | - | ||||||
118 | if (!QueryPerformanceCounter(&x)) | - | ||||||
119 | return 0; | - | ||||||
120 | return x.QuadPart; | - | ||||||
121 | } | - | ||||||
122 | - | |||||||
123 | #endif | - | ||||||
124 | - | |||||||
125 | static double elapsed(qint64 after, qint64 before) | - | ||||||
126 | { | - | ||||||
127 | return double(after - before); executed 37248 times by 4 tests: return double(after - before); Executed by:
| 37248 | ||||||
128 | } | - | ||||||
129 | - | |||||||
130 | namespace QtConcurrent { | - | ||||||
131 | - | |||||||
132 | /*! \internal | - | ||||||
133 | - | |||||||
134 | */ | - | ||||||
135 | BlockSizeManager::BlockSizeManager(int iterationCount) | - | ||||||
136 | : maxBlockSize(iterationCount / (QThreadPool::globalInstance()->maxThreadCount() * 2)), | - | ||||||
137 | beforeUser(0), afterUser(0), | - | ||||||
138 | controlPartElapsed(MedianSize), userPartElapsed(MedianSize), | - | ||||||
139 | m_blockSize(1) | - | ||||||
140 | { never executed: }end of block never executed: end of block | 0 | ||||||
141 | - | |||||||
142 | // Records the time before user code. | - | ||||||
143 | void BlockSizeManager::timeBeforeUser() | - | ||||||
144 | { | - | ||||||
145 | if (blockSizeMaxed())
| 0 | ||||||
146 | return; never executed: return; | 0 | ||||||
147 | - | |||||||
148 | beforeUser = getticks(); | - | ||||||
149 | controlPartElapsed.addValue(elapsed(beforeUser, afterUser)); | - | ||||||
150 | } never executed: end of block | 0 | ||||||
151 | - | |||||||
152 | // Records the time after user code and adjust the block size if we are spending | - | ||||||
153 | // to much time in the for control code compared with the user code. | - | ||||||
154 | void BlockSizeManager::timeAfterUser() | - | ||||||
155 | { | - | ||||||
156 | if (blockSizeMaxed())
| 0 | ||||||
157 | return; never executed: return; | 0 | ||||||
158 | - | |||||||
159 | afterUser = getticks(); | - | ||||||
160 | userPartElapsed.addValue(elapsed(afterUser, beforeUser)); | - | ||||||
161 | - | |||||||
162 | if (controlPartElapsed.isMedianValid() == false)
| 0 | ||||||
163 | return; never executed: return; | 0 | ||||||
164 | - | |||||||
165 | if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median())
| 0 | ||||||
166 | return; never executed: return; | 0 | ||||||
167 | - | |||||||
168 | m_blockSize = qMin(m_blockSize * 2, maxBlockSize); | - | ||||||
169 | - | |||||||
170 | #ifdef QTCONCURRENT_FOR_DEBUG | - | ||||||
171 | qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize; | - | ||||||
172 | #endif | - | ||||||
173 | - | |||||||
174 | // Reset the medians after adjusting the block size so we get | - | ||||||
175 | // new measurements with the new block size. | - | ||||||
176 | controlPartElapsed.reset(); | - | ||||||
177 | userPartElapsed.reset(); | - | ||||||
178 | } never executed: end of block | 0 | ||||||
179 | - | |||||||
180 | int BlockSizeManager::blockSize() | - | ||||||
181 | { | - | ||||||
182 | return m_blockSize; never executed: return m_blockSize; | 0 | ||||||
183 | } | - | ||||||
184 | - | |||||||
185 | /*! \internal | - | ||||||
186 | - | |||||||
187 | */ | - | ||||||
188 | BlockSizeManagerV2::BlockSizeManagerV2(int iterationCount) | - | ||||||
189 | : maxBlockSize(iterationCount / (QThreadPool::globalInstance()->maxThreadCount() * 2)), | - | ||||||
190 | beforeUser(0), afterUser(0), | - | ||||||
191 | m_blockSize(1) | - | ||||||
192 | { executed 20974 times by 4 tests: }end of block Executed by:
executed 20974 times by 4 tests: end of block Executed by:
| 20974 | ||||||
193 | - | |||||||
194 | // Records the time before user code. | - | ||||||
195 | void BlockSizeManagerV2::timeBeforeUser() | - | ||||||
196 | { | - | ||||||
197 | if (blockSizeMaxed())
| 560-18624 | ||||||
198 | return; executed 560 times by 2 tests: return; Executed by:
| 560 | ||||||
199 | - | |||||||
200 | beforeUser = getticks(); | - | ||||||
201 | controlPartElapsed.addValue(elapsed(beforeUser, afterUser)); | - | ||||||
202 | } executed 18624 times by 4 tests: end of block Executed by:
| 18624 | ||||||
203 | - | |||||||
204 | // Records the time after user code and adjust the block size if we are spending | - | ||||||
205 | // to much time in the for control code compared with the user code. | - | ||||||
206 | void BlockSizeManagerV2::timeAfterUser() | - | ||||||
207 | { | - | ||||||
208 | if (blockSizeMaxed())
| 559-18624 | ||||||
209 | return; executed 559 times by 2 tests: return; Executed by:
| 559 | ||||||
210 | - | |||||||
211 | afterUser = getticks(); | - | ||||||
212 | userPartElapsed.addValue(elapsed(afterUser, beforeUser)); | - | ||||||
213 | - | |||||||
214 | if (controlPartElapsed.isMedianValid() == false)
| 2534-16090 | ||||||
215 | return; executed 16090 times by 4 tests: return; Executed by:
| 16090 | ||||||
216 | - | |||||||
217 | if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median())
| 0-2534 | ||||||
218 | return; never executed: return; | 0 | ||||||
219 | - | |||||||
220 | m_blockSize = qMin(m_blockSize * 2, maxBlockSize); | - | ||||||
221 | - | |||||||
222 | #ifdef QTCONCURRENT_FOR_DEBUG | - | ||||||
223 | qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize; | - | ||||||
224 | #endif | - | ||||||
225 | - | |||||||
226 | // Reset the medians after adjusting the block size so we get | - | ||||||
227 | // new measurements with the new block size. | - | ||||||
228 | controlPartElapsed.reset(); | - | ||||||
229 | userPartElapsed.reset(); | - | ||||||
230 | } executed 2534 times by 4 tests: end of block Executed by:
| 2534 | ||||||
231 | - | |||||||
232 | int BlockSizeManagerV2::blockSize() | - | ||||||
233 | { | - | ||||||
234 | return m_blockSize; executed 40062 times by 4 tests: return m_blockSize; Executed by:
| 40062 | ||||||
235 | } | - | ||||||
236 | - | |||||||
237 | } // namespace QtConcurrent | - | ||||||
238 | - | |||||||
239 | QT_END_NAMESPACE | - | ||||||
240 | - | |||||||
241 | #endif // QT_NO_CONCURRENT | - | ||||||
Source code | Switch to Preprocessed file |