1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qtconcurrentiteratekernel.h"
|
---|
43 |
|
---|
44 | #if defined(Q_OS_MAC)
|
---|
45 | #include <mach/mach.h>
|
---|
46 | #include <mach/mach_time.h>
|
---|
47 | #include <unistd.h>
|
---|
48 | #elif defined(Q_OS_UNIX)
|
---|
49 | #if defined(Q_OS_HURD)
|
---|
50 | #include <sys/time.h>
|
---|
51 | #endif
|
---|
52 | #include <time.h>
|
---|
53 | #include <unistd.h>
|
---|
54 | #elif defined(Q_OS_WIN)
|
---|
55 | #include <qt_windows.h>
|
---|
56 | #elif defined(Q_OS_OS2) && defined(Q_CC_GNU)
|
---|
57 | #include <InnoTekLIBC/FastInfoBlocks.h>
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | #include "private/qfunctions_p.h"
|
---|
61 |
|
---|
62 |
|
---|
63 | #ifndef QT_NO_CONCURRENT
|
---|
64 |
|
---|
65 | QT_BEGIN_NAMESPACE
|
---|
66 |
|
---|
67 | enum {
|
---|
68 | TargetRatio = 100,
|
---|
69 | MedianSize = 7
|
---|
70 | };
|
---|
71 |
|
---|
72 | #if defined(Q_OS_MAC)
|
---|
73 |
|
---|
74 | static qint64 getticks()
|
---|
75 | {
|
---|
76 | return mach_absolute_time();
|
---|
77 | }
|
---|
78 |
|
---|
79 | #elif defined(Q_OS_UNIX) || (defined(Q_OS_OS2) && !defined(Q_CC_GNU))
|
---|
80 |
|
---|
81 | static qint64 getticks()
|
---|
82 | {
|
---|
83 | #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
|
---|
84 | clockid_t clockId;
|
---|
85 |
|
---|
86 | #ifndef _POSIX_THREAD_CPUTIME
|
---|
87 | clockId = CLOCK_REALTIME;
|
---|
88 | #elif (_POSIX_THREAD_CPUTIME-0 <= 0)
|
---|
89 | // if we don't have CLOCK_THREAD_CPUTIME_ID, we have to just use elapsed realtime instead
|
---|
90 | clockId = CLOCK_REALTIME;
|
---|
91 |
|
---|
92 | # if (_POSIX_THREAD_CPUTIME-0 == 0)
|
---|
93 | // detect availablility of CLOCK_THREAD_CPUTIME_ID
|
---|
94 | static long useThreadCpuTime = -2;
|
---|
95 | if (useThreadCpuTime == -2) {
|
---|
96 | // sysconf() will return either -1 or _POSIX_VERSION (don't care about thread races here)
|
---|
97 | useThreadCpuTime = sysconf(_SC_THREAD_CPUTIME);
|
---|
98 | }
|
---|
99 | if (useThreadCpuTime != -1)
|
---|
100 | clockId = CLOCK_THREAD_CPUTIME_ID;
|
---|
101 | # endif
|
---|
102 | #else
|
---|
103 | clockId = CLOCK_THREAD_CPUTIME_ID;
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | struct timespec ts;
|
---|
107 | if (clock_gettime(clockId, &ts) == -1)
|
---|
108 | return 0;
|
---|
109 | return (ts.tv_sec * 1000000000) + ts.tv_nsec;
|
---|
110 | #else
|
---|
111 |
|
---|
112 | #ifdef Q_OS_SYMBIAN
|
---|
113 | return clock();
|
---|
114 | #else
|
---|
115 | // no clock_gettime(), fall back to wall time
|
---|
116 | struct timeval tv;
|
---|
117 | gettimeofday(&tv, 0);
|
---|
118 | return (tv.tv_sec * 1000000) + tv.tv_usec;
|
---|
119 | #endif
|
---|
120 |
|
---|
121 | #endif
|
---|
122 | }
|
---|
123 |
|
---|
124 | #elif defined(Q_OS_WIN)
|
---|
125 |
|
---|
126 | static qint64 getticks()
|
---|
127 | {
|
---|
128 | LARGE_INTEGER x;
|
---|
129 | if (!QueryPerformanceCounter(&x))
|
---|
130 | return 0;
|
---|
131 | return x.QuadPart;
|
---|
132 | }
|
---|
133 |
|
---|
134 | #elif defined(Q_OS_OS2) && defined(Q_CC_GNU)
|
---|
135 |
|
---|
136 | static qint64 getticks()
|
---|
137 | {
|
---|
138 | return fibGetMsCount() * (quint64) 1000000;
|
---|
139 | }
|
---|
140 |
|
---|
141 | #endif
|
---|
142 |
|
---|
143 | static double elapsed(qint64 after, qint64 before)
|
---|
144 | {
|
---|
145 | return double(after - before);
|
---|
146 | }
|
---|
147 |
|
---|
148 | namespace QtConcurrent {
|
---|
149 |
|
---|
150 | /*! \internal
|
---|
151 |
|
---|
152 | */
|
---|
153 | BlockSizeManager::BlockSizeManager(int iterationCount)
|
---|
154 | : maxBlockSize(iterationCount / (QThreadPool::globalInstance()->maxThreadCount() * 2)),
|
---|
155 | beforeUser(0), afterUser(0),
|
---|
156 | controlPartElapsed(MedianSize), userPartElapsed(MedianSize),
|
---|
157 | m_blockSize(1)
|
---|
158 | { }
|
---|
159 |
|
---|
160 | // Records the time before user code.
|
---|
161 | void BlockSizeManager::timeBeforeUser()
|
---|
162 | {
|
---|
163 | if (blockSizeMaxed())
|
---|
164 | return;
|
---|
165 |
|
---|
166 | beforeUser = getticks();
|
---|
167 | controlPartElapsed.addValue(elapsed(beforeUser, afterUser));
|
---|
168 | }
|
---|
169 |
|
---|
170 | // Records the time after user code and adjust the block size if we are spending
|
---|
171 | // to much time in the for control code compared with the user code.
|
---|
172 | void BlockSizeManager::timeAfterUser()
|
---|
173 | {
|
---|
174 | if (blockSizeMaxed())
|
---|
175 | return;
|
---|
176 |
|
---|
177 | afterUser = getticks();
|
---|
178 | userPartElapsed.addValue(elapsed(afterUser, beforeUser));
|
---|
179 |
|
---|
180 | if (controlPartElapsed.isMedianValid() == false)
|
---|
181 | return;
|
---|
182 |
|
---|
183 | if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median())
|
---|
184 | return;
|
---|
185 |
|
---|
186 | m_blockSize = qMin(m_blockSize * 2, maxBlockSize);
|
---|
187 |
|
---|
188 | #ifdef QTCONCURRENT_FOR_DEBUG
|
---|
189 | qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize;
|
---|
190 | #endif
|
---|
191 |
|
---|
192 | // Reset the medians after adjusting the block size so we get
|
---|
193 | // new measurements with the new block size.
|
---|
194 | controlPartElapsed.reset();
|
---|
195 | userPartElapsed.reset();
|
---|
196 | }
|
---|
197 |
|
---|
198 | int BlockSizeManager::blockSize()
|
---|
199 | {
|
---|
200 | return m_blockSize;
|
---|
201 | }
|
---|
202 |
|
---|
203 | } // namespace QtConcurrent
|
---|
204 |
|
---|
205 | QT_END_NAMESPACE
|
---|
206 |
|
---|
207 | #endif // QT_NO_CONCURRENT
|
---|