author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
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) |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
49 |
#if defined(Q_OS_HURD) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
50 |
#include <sys/time.h> |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
51 |
#endif |
0 | 52 |
#include <time.h> |
53 |
#include <unistd.h> |
|
54 |
#elif defined(Q_OS_WIN) |
|
55 |
#include <qt_windows.h> |
|
56 |
#endif |
|
57 |
||
58 |
#include "private/qfunctions_p.h" |
|
59 |
||
60 |
||
61 |
#ifndef QT_NO_CONCURRENT |
|
62 |
||
63 |
QT_BEGIN_NAMESPACE |
|
64 |
||
65 |
enum { |
|
66 |
TargetRatio = 100, |
|
67 |
MedianSize = 7 |
|
68 |
}; |
|
69 |
||
70 |
#if defined(Q_OS_MAC) |
|
71 |
||
72 |
static qint64 getticks() |
|
73 |
{ |
|
74 |
return mach_absolute_time(); |
|
75 |
} |
|
76 |
||
77 |
#elif defined(Q_OS_UNIX) |
|
78 |
||
79 |
||
80 |
static qint64 getticks() |
|
81 |
{ |
|
82 |
#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) |
|
83 |
clockid_t clockId; |
|
84 |
||
85 |
#ifndef _POSIX_THREAD_CPUTIME |
|
86 |
clockId = CLOCK_REALTIME; |
|
87 |
#elif (_POSIX_THREAD_CPUTIME-0 <= 0) |
|
88 |
// if we don't have CLOCK_THREAD_CPUTIME_ID, we have to just use elapsed realtime instead |
|
89 |
clockId = CLOCK_REALTIME; |
|
90 |
||
91 |
# if (_POSIX_THREAD_CPUTIME-0 == 0) |
|
92 |
// detect availablility of CLOCK_THREAD_CPUTIME_ID |
|
93 |
static long useThreadCpuTime = -2; |
|
94 |
if (useThreadCpuTime == -2) { |
|
95 |
// sysconf() will return either -1 or _POSIX_VERSION (don't care about thread races here) |
|
96 |
useThreadCpuTime = sysconf(_SC_THREAD_CPUTIME); |
|
97 |
} |
|
98 |
if (useThreadCpuTime != -1) |
|
99 |
clockId = CLOCK_THREAD_CPUTIME_ID; |
|
100 |
# endif |
|
101 |
#else |
|
102 |
clockId = CLOCK_THREAD_CPUTIME_ID; |
|
103 |
#endif |
|
104 |
||
105 |
struct timespec ts; |
|
106 |
if (clock_gettime(clockId, &ts) == -1) |
|
107 |
return 0; |
|
108 |
return (ts.tv_sec * 1000000000) + ts.tv_nsec; |
|
109 |
#else |
|
110 |
||
111 |
#ifdef Q_OS_SYMBIAN |
|
112 |
return clock(); |
|
113 |
#else |
|
114 |
// no clock_gettime(), fall back to wall time |
|
115 |
struct timeval tv; |
|
116 |
gettimeofday(&tv, 0); |
|
117 |
return (tv.tv_sec * 1000000) + tv.tv_usec; |
|
118 |
#endif |
|
119 |
||
120 |
#endif |
|
121 |
} |
|
122 |
||
123 |
#elif defined(Q_OS_WIN) |
|
124 |
||
125 |
static qint64 getticks() |
|
126 |
{ |
|
127 |
LARGE_INTEGER x; |
|
128 |
if (!QueryPerformanceCounter(&x)) |
|
129 |
return 0; |
|
130 |
return x.QuadPart; |
|
131 |
} |
|
132 |
||
133 |
#endif |
|
134 |
||
135 |
static double elapsed(qint64 after, qint64 before) |
|
136 |
{ |
|
137 |
return double(after - before); |
|
138 |
} |
|
139 |
||
140 |
namespace QtConcurrent { |
|
141 |
||
142 |
/*! \internal |
|
143 |
||
144 |
*/ |
|
145 |
BlockSizeManager::BlockSizeManager(int iterationCount) |
|
146 |
: maxBlockSize(iterationCount / (QThreadPool::globalInstance()->maxThreadCount() * 2)), |
|
147 |
beforeUser(0), afterUser(0), |
|
148 |
controlPartElapsed(MedianSize), userPartElapsed(MedianSize), |
|
149 |
m_blockSize(1) |
|
150 |
{ } |
|
151 |
||
152 |
// Records the time before user code. |
|
153 |
void BlockSizeManager::timeBeforeUser() |
|
154 |
{ |
|
155 |
if (blockSizeMaxed()) |
|
156 |
return; |
|
157 |
||
158 |
beforeUser = getticks(); |
|
159 |
controlPartElapsed.addValue(elapsed(beforeUser, afterUser)); |
|
160 |
} |
|
161 |
||
162 |
// Records the time after user code and adjust the block size if we are spending |
|
163 |
// to much time in the for control code compared with the user code. |
|
164 |
void BlockSizeManager::timeAfterUser() |
|
165 |
{ |
|
166 |
if (blockSizeMaxed()) |
|
167 |
return; |
|
168 |
||
169 |
afterUser = getticks(); |
|
170 |
userPartElapsed.addValue(elapsed(afterUser, beforeUser)); |
|
171 |
||
172 |
if (controlPartElapsed.isMedianValid() == false) |
|
173 |
return; |
|
174 |
||
175 |
if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median()) |
|
176 |
return; |
|
177 |
||
178 |
m_blockSize = qMin(m_blockSize * 2, maxBlockSize); |
|
179 |
||
180 |
#ifdef QTCONCURRENT_FOR_DEBUG |
|
181 |
qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize; |
|
182 |
#endif |
|
183 |
||
184 |
// Reset the medians after adjusting the block size so we get |
|
185 |
// new measurements with the new block size. |
|
186 |
controlPartElapsed.reset(); |
|
187 |
userPartElapsed.reset(); |
|
188 |
} |
|
189 |
||
190 |
int BlockSizeManager::blockSize() |
|
191 |
{ |
|
192 |
return m_blockSize; |
|
193 |
} |
|
194 |
||
195 |
} // namespace QtConcurrent |
|
196 |
||
197 |
QT_END_NAMESPACE |
|
198 |
||
199 |
#endif // QT_NO_CONCURRENT |