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