source: trunk/src/corelib/concurrent/qtconcurrentiteratekernel.cpp@ 45

Last change on this file since 45 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.2 KB
Line 
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#endif
60
61
62#ifndef QT_NO_CONCURRENT
63
64QT_BEGIN_NAMESPACE
65
66enum {
67 TargetRatio = 100,
68 MedianSize = 7
69};
70
71#if defined(Q_OS_MAC)
72
73static qint64 getticks()
74{
75 return mach_absolute_time();
76}
77
78#elif defined(Q_OS_UNIX)
79
80
81static 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 // 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
118#elif defined(Q_OS_WIN)
119
120static qint64 getticks()
121{
122 LARGE_INTEGER x;
123 if (!QueryPerformanceCounter(&x))
124 return 0;
125 return x.QuadPart;
126}
127
128#endif
129
130static double elapsed(qint64 after, qint64 before)
131{
132 return double(after - before);
133}
134
135namespace QtConcurrent {
136
137/*! \internal
138
139*/
140BlockSizeManager::BlockSizeManager(int iterationCount)
141: maxBlockSize(iterationCount / (QThreadPool::globalInstance()->maxThreadCount() * 2)),
142 beforeUser(0), afterUser(0),
143 controlPartElapsed(MedianSize), userPartElapsed(MedianSize),
144 m_blockSize(1)
145{ }
146
147// Records the time before user code.
148void BlockSizeManager::timeBeforeUser()
149{
150 if (blockSizeMaxed())
151 return;
152
153 beforeUser = getticks();
154 controlPartElapsed.addValue(elapsed(beforeUser, afterUser));
155}
156
157 // Records the time after user code and adjust the block size if we are spending
158 // to much time in the for control code compared with the user code.
159void BlockSizeManager::timeAfterUser()
160{
161 if (blockSizeMaxed())
162 return;
163
164 afterUser = getticks();
165 userPartElapsed.addValue(elapsed(afterUser, beforeUser));
166
167 if (controlPartElapsed.isMedianValid() == false)
168 return;
169
170 if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median())
171 return;
172
173 m_blockSize = qMin(m_blockSize * 2, maxBlockSize);
174
175#ifdef QTCONCURRENT_FOR_DEBUG
176 qDebug() << QThread::currentThread() << "adjusting block size" << controlPartElapsed.median() << userPartElapsed.median() << m_blockSize;
177#endif
178
179 // Reset the medians after adjusting the block size so we get
180 // new measurements with the new block size.
181 controlPartElapsed.reset();
182 userPartElapsed.reset();
183}
184
185int BlockSizeManager::blockSize()
186{
187 return m_blockSize;
188}
189
190} // namespace QtConcurrent
191
192QT_END_NAMESPACE
193
194#endif // QT_NO_CONCURRENT
Note: See TracBrowser for help on using the repository browser.