0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
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)
|
|
49 |
#include <time.h>
|
|
50 |
#include <unistd.h>
|
|
51 |
#elif defined(Q_OS_WIN)
|
|
52 |
#include <qt_windows.h>
|
|
53 |
#endif
|
|
54 |
|
|
55 |
#include "private/qfunctions_p.h"
|
|
56 |
|
|
57 |
|
|
58 |
#ifndef QT_NO_CONCURRENT
|
|
59 |
|
|
60 |
QT_BEGIN_NAMESPACE
|
|
61 |
|
|
62 |
enum {
|
|
63 |
TargetRatio = 100,
|
|
64 |
MedianSize = 7
|
|
65 |
};
|
|
66 |
|
|
67 |
#if defined(Q_OS_MAC)
|
|
68 |
|
|
69 |
static qint64 getticks()
|
|
70 |
{
|
|
71 |
return mach_absolute_time();
|
|
72 |
}
|
|
73 |
|
|
74 |
#elif defined(Q_OS_UNIX)
|
|
75 |
|
|
76 |
|
|
77 |
static qint64 getticks()
|
|
78 |
{
|
|
79 |
#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
|
|
80 |
clockid_t clockId;
|
|
81 |
|
|
82 |
#ifndef _POSIX_THREAD_CPUTIME
|
|
83 |
clockId = CLOCK_REALTIME;
|
|
84 |
#elif (_POSIX_THREAD_CPUTIME-0 <= 0)
|
|
85 |
// if we don't have CLOCK_THREAD_CPUTIME_ID, we have to just use elapsed realtime instead
|
|
86 |
clockId = CLOCK_REALTIME;
|
|
87 |
|
|
88 |
# if (_POSIX_THREAD_CPUTIME-0 == 0)
|
|
89 |
// detect availablility of CLOCK_THREAD_CPUTIME_ID
|
|
90 |
static long useThreadCpuTime = -2;
|
|
91 |
if (useThreadCpuTime == -2) {
|
|
92 |
// sysconf() will return either -1 or _POSIX_VERSION (don't care about thread races here)
|
|
93 |
useThreadCpuTime = sysconf(_SC_THREAD_CPUTIME);
|
|
94 |
}
|
|
95 |
if (useThreadCpuTime != -1)
|
|
96 |
clockId = CLOCK_THREAD_CPUTIME_ID;
|
|
97 |
# endif
|
|
98 |
#else
|
|
99 |
clockId = CLOCK_THREAD_CPUTIME_ID;
|
|
100 |
#endif
|
|
101 |
|
|
102 |
struct timespec ts;
|
|
103 |
if (clock_gettime(clockId, &ts) == -1)
|
|
104 |
return 0;
|
|
105 |
return (ts.tv_sec * 1000000000) + ts.tv_nsec;
|
|
106 |
#else
|
|
107 |
|
|
108 |
#ifdef Q_OS_SYMBIAN
|
|
109 |
return clock();
|
|
110 |
#else
|
|
111 |
// no clock_gettime(), fall back to wall time
|
|
112 |
struct timeval tv;
|
|
113 |
gettimeofday(&tv, 0);
|
|
114 |
return (tv.tv_sec * 1000000) + tv.tv_usec;
|
|
115 |
#endif
|
|
116 |
|
|
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 |
#endif
|
|
131 |
|
|
132 |
static double elapsed(qint64 after, qint64 before)
|
|
133 |
{
|
|
134 |
return double(after - before);
|
|
135 |
}
|
|
136 |
|
|
137 |
namespace QtConcurrent {
|
|
138 |
|
|
139 |
/*! \internal
|
|
140 |
|
|
141 |
*/
|
|
142 |
BlockSizeManager::BlockSizeManager(int iterationCount)
|
|
143 |
: maxBlockSize(iterationCount / (QThreadPool::globalInstance()->maxThreadCount() * 2)),
|
|
144 |
beforeUser(0), afterUser(0),
|
|
145 |
controlPartElapsed(MedianSize), userPartElapsed(MedianSize),
|
|
146 |
m_blockSize(1)
|
|
147 |
{ }
|
|
148 |
|
|
149 |
// Records the time before user code.
|
|
150 |
void BlockSizeManager::timeBeforeUser()
|
|
151 |
{
|
|
152 |
if (blockSizeMaxed())
|
|
153 |
return;
|
|
154 |
|
|
155 |
beforeUser = getticks();
|
|
156 |
controlPartElapsed.addValue(elapsed(beforeUser, afterUser));
|
|
157 |
}
|
|
158 |
|
|
159 |
// Records the time after user code and adjust the block size if we are spending
|
|
160 |
// to much time in the for control code compared with the user code.
|
|
161 |
void BlockSizeManager::timeAfterUser()
|
|
162 |
{
|
|
163 |
if (blockSizeMaxed())
|
|
164 |
return;
|
|
165 |
|
|
166 |
afterUser = getticks();
|
|
167 |
userPartElapsed.addValue(elapsed(afterUser, beforeUser));
|
|
168 |
|
|
169 |
if (controlPartElapsed.isMedianValid() == false)
|
|
170 |
return;
|
|
171 |
|
|
172 |
if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median())
|
|
173 |
return;
|
|
174 |
|
|
175 |
m_blockSize = qMin(m_blockSize * 2, maxBlockSize);
|
|
176 |
|
|
177 |
#ifdef QTCONCURRENT_FOR_DEBUG
|
|
178 |
qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize;
|
|
179 |
#endif
|
|
180 |
|
|
181 |
// Reset the medians after adjusting the block size so we get
|
|
182 |
// new measurements with the new block size.
|
|
183 |
controlPartElapsed.reset();
|
|
184 |
userPartElapsed.reset();
|
|
185 |
}
|
|
186 |
|
|
187 |
int BlockSizeManager::blockSize()
|
|
188 |
{
|
|
189 |
return m_blockSize;
|
|
190 |
}
|
|
191 |
|
|
192 |
} // namespace QtConcurrent
|
|
193 |
|
|
194 |
QT_END_NAMESPACE
|
|
195 |
|
|
196 |
#endif // QT_NO_CONCURRENT
|