qtconcurrentiteratekernel.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtCore module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
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#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#endif -
57 -
58#include "private/qfunctions_p.h" -
59 -
60 -
61#ifndef QT_NO_CONCURRENT -
62 -
63QT_BEGIN_NAMESPACE -
64 -
65enum { -
66 TargetRatio = 100, -
67 MedianSize = 7 -
68}; -
69 -
70#if defined(Q_OS_MAC) -
71 -
72static qint64 getticks() -
73{ -
74 return mach_absolute_time(); -
75} -
76 -
77#elif defined(Q_OS_UNIX) -
78 -
79 -
80static qint64 getticks() -
81{ -
82#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) -
83 clockid_t clockId;
executed (the execution status of this line is deduced): 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;
executed (the execution status of this line is deduced): clockId = 0;
-
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) {
evaluated: useThreadCpuTime == -2
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:152591
9-152591
95 // sysconf() will return either -1 or _POSIX_VERSION (don't care about thread races here) -
96 useThreadCpuTime = sysconf(_SC_THREAD_CPUTIME);
executed (the execution status of this line is deduced): useThreadCpuTime = sysconf(_SC_THREAD_CPUTIME);
-
97 }
executed: }
Execution Count:5
5
98 if (useThreadCpuTime != -1)
partially evaluated: useThreadCpuTime != -1
TRUEFALSE
yes
Evaluation Count:153076
no
Evaluation Count:0
0-153076
99 clockId = CLOCK_THREAD_CPUTIME_ID;
executed: clockId = 3;
Execution Count:153560
153560
100# endif -
101#else -
102 clockId = CLOCK_THREAD_CPUTIME_ID; -
103#endif -
104 -
105 struct timespec ts;
executed (the execution status of this line is deduced): struct timespec ts;
-
106 if (clock_gettime(clockId, &ts) == -1)
partially evaluated: clock_gettime(clockId, &ts) == -1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:155608
0-155608
107 return 0;
never executed: return 0;
0
108 return (ts.tv_sec * 1000000000) + ts.tv_nsec;
executed: return (ts.tv_sec * 1000000000) + ts.tv_nsec;
Execution Count:154291
154291
109#else -
110 -
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);
executed: return double(after - before);
Execution Count:155490
155490
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{ }
executed: }
Execution Count:25869
25869
146 -
147// Records the time before user code. -
148void BlockSizeManager::timeBeforeUser() -
149{ -
150 if (blockSizeMaxed())
evaluated: blockSizeMaxed()
TRUEFALSE
yes
Evaluation Count:1013
yes
Evaluation Count:78518
1013-78518
151 return;
executed: return;
Execution Count:1013
1013
152 -
153 beforeUser = getticks();
executed (the execution status of this line is deduced): beforeUser = getticks();
-
154 controlPartElapsed.addValue(elapsed(beforeUser, afterUser));
executed (the execution status of this line is deduced): controlPartElapsed.addValue(elapsed(beforeUser, afterUser));
-
155}
executed: }
Execution Count:79027
79027
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())
evaluated: blockSizeMaxed()
TRUEFALSE
yes
Evaluation Count:1013
yes
Evaluation Count:78767
1013-78767
162 return;
executed: return;
Execution Count:1013
1013
163 -
164 afterUser = getticks();
executed (the execution status of this line is deduced): afterUser = getticks();
-
165 userPartElapsed.addValue(elapsed(afterUser, beforeUser));
executed (the execution status of this line is deduced): userPartElapsed.addValue(elapsed(afterUser, beforeUser));
-
166 -
167 if (controlPartElapsed.isMedianValid() == false)
evaluated: controlPartElapsed.isMedianValid() == false
TRUEFALSE
yes
Evaluation Count:68580
yes
Evaluation Count:10218
10218-68580
168 return;
executed: return;
Execution Count:68474
68474
169 -
170 if (controlPartElapsed.median() * TargetRatio < userPartElapsed.median())
partially evaluated: controlPartElapsed.median() * TargetRatio < userPartElapsed.median()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:10188
0-10188
171 return;
never executed: return;
0
172 -
173 m_blockSize = qMin(m_blockSize * 2, maxBlockSize);
executed (the execution status of this line is deduced): 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();
executed (the execution status of this line is deduced): controlPartElapsed.reset();
-
182 userPartElapsed.reset();
executed (the execution status of this line is deduced): userPartElapsed.reset();
-
183}
executed: }
Execution Count:10200
10200
184 -
185int BlockSizeManager::blockSize() -
186{ -
187 return m_blockSize;
executed: return m_blockSize;
Execution Count:104102
104102
188} -
189 -
190} // namespace QtConcurrent -
191 -
192QT_END_NAMESPACE -
193 -
194#endif // QT_NO_CONCURRENT -
195 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial