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 QtTest 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 "QtTest/private/qbenchmark_p.h"
|
|
43 |
|
|
44 |
#ifdef QTESTLIB_USE_VALGRIND
|
|
45 |
|
|
46 |
#include "QtTest/private/qbenchmarkvalgrind_p.h"
|
|
47 |
#include <QtCore/qstringlist.h>
|
|
48 |
#include <QtCore/qcoreapplication.h>
|
|
49 |
#include <QtCore/qprocess.h>
|
|
50 |
#include <QtCore/qdir.h>
|
|
51 |
#include <QtCore/qset.h>
|
|
52 |
#include "3rdparty/callgrind_p.h"
|
|
53 |
|
|
54 |
QT_BEGIN_NAMESPACE
|
|
55 |
|
|
56 |
// Returns true iff a sufficiently recent valgrind is available.
|
|
57 |
bool QBenchmarkValgrindUtils::haveValgrind()
|
|
58 |
{
|
|
59 |
#ifdef NVALGRIND
|
|
60 |
return false;
|
|
61 |
#else
|
|
62 |
QProcess process;
|
|
63 |
QStringList args;
|
|
64 |
args << QLatin1String("--version");
|
|
65 |
process.start(QLatin1String("valgrind"), args);
|
|
66 |
if (!process.waitForFinished(-1))
|
|
67 |
return false;
|
|
68 |
const QByteArray out = process.readAllStandardOutput();
|
|
69 |
const QRegExp rx(QLatin1String("^valgrind-([0-9]).([0-9]).[0-9]"));
|
|
70 |
if (rx.indexIn(QLatin1String(out.data())) == -1)
|
|
71 |
return false;
|
|
72 |
bool ok;
|
|
73 |
const int major = rx.cap(1).toInt(&ok);
|
|
74 |
if (!ok)
|
|
75 |
return false;
|
|
76 |
const int minor = rx.cap(2).toInt(&ok);
|
|
77 |
if (!ok)
|
|
78 |
return false;
|
|
79 |
// return (major > 3 || (major == 3 && minor >= 3)); // v >= 3.3 for --callgrind-out-file option
|
|
80 |
Q_UNUSED(major);
|
|
81 |
Q_UNUSED(minor);
|
|
82 |
return true; // skip version restriction for now
|
|
83 |
#endif
|
|
84 |
}
|
|
85 |
|
|
86 |
// Reruns this program through callgrind.
|
|
87 |
// Returns true upon success, otherwise false.
|
|
88 |
bool QBenchmarkValgrindUtils::rerunThroughCallgrind(const QStringList &origAppArgs, int &exitCode)
|
|
89 |
{
|
|
90 |
if (!QBenchmarkValgrindUtils::runCallgrindSubProcess(origAppArgs, exitCode)) {
|
|
91 |
qWarning("failed to run callgrind subprocess");
|
|
92 |
return false;
|
|
93 |
}
|
|
94 |
return true;
|
|
95 |
}
|
|
96 |
|
|
97 |
static void dumpOutput(const QByteArray &data, FILE *fh)
|
|
98 |
{
|
|
99 |
QFile file;
|
|
100 |
file.open(fh, QIODevice::WriteOnly);
|
|
101 |
file.write(data);
|
|
102 |
}
|
|
103 |
|
|
104 |
qint64 QBenchmarkValgrindUtils::extractResult(const QString &fileName)
|
|
105 |
{
|
|
106 |
QFile file(fileName);
|
|
107 |
const bool openOk = file.open(QIODevice::ReadOnly | QIODevice::Text);
|
|
108 |
Q_ASSERT(openOk);
|
|
109 |
Q_UNUSED(openOk);
|
|
110 |
|
|
111 |
qint64 val = -1;
|
|
112 |
bool valSeen = false;
|
|
113 |
const QRegExp rxValue(QLatin1String("^summary: (\\d+)"));
|
|
114 |
while (!file.atEnd()) {
|
|
115 |
const QString line(QLatin1String(file.readLine()));
|
|
116 |
if (rxValue.indexIn(line) != -1) {
|
|
117 |
Q_ASSERT(rxValue.numCaptures() == 1);
|
|
118 |
bool ok;
|
|
119 |
val = rxValue.cap(1).toLongLong(&ok);
|
|
120 |
Q_ASSERT(ok);
|
|
121 |
valSeen = true;
|
|
122 |
break;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
Q_ASSERT(valSeen);
|
|
126 |
return val;
|
|
127 |
}
|
|
128 |
|
|
129 |
// Gets the newest file name (i.e. the one with the highest integer suffix).
|
|
130 |
QString QBenchmarkValgrindUtils::getNewestFileName()
|
|
131 |
{
|
|
132 |
QStringList nameFilters;
|
|
133 |
QString base = QBenchmarkGlobalData::current->callgrindOutFileBase;
|
|
134 |
Q_ASSERT(!base.isEmpty());
|
|
135 |
|
|
136 |
nameFilters << QString::fromLatin1("%1.*").arg(base);
|
|
137 |
QFileInfoList fiList = QDir().entryInfoList(nameFilters, QDir::Files | QDir::Readable);
|
|
138 |
Q_ASSERT(!fiList.empty());
|
|
139 |
int hiSuffix = -1;
|
|
140 |
QFileInfo lastFileInfo;
|
|
141 |
const QString pattern = QString::fromLatin1("%1.(\\d+)").arg(base);
|
|
142 |
const QRegExp rx(pattern);
|
|
143 |
foreach (QFileInfo fileInfo, fiList) {
|
|
144 |
const int index = rx.indexIn(fileInfo.fileName());
|
|
145 |
Q_ASSERT(index == 0);
|
|
146 |
Q_UNUSED(index);
|
|
147 |
bool ok;
|
|
148 |
const int suffix = rx.cap(1).toInt(&ok);
|
|
149 |
Q_ASSERT(ok);
|
|
150 |
Q_ASSERT(suffix >= 0);
|
|
151 |
if (suffix > hiSuffix) {
|
|
152 |
lastFileInfo = fileInfo;
|
|
153 |
hiSuffix = suffix;
|
|
154 |
}
|
|
155 |
}
|
|
156 |
|
|
157 |
return lastFileInfo.fileName();
|
|
158 |
}
|
|
159 |
|
|
160 |
qint64 QBenchmarkValgrindUtils::extractLastResult()
|
|
161 |
{
|
|
162 |
return extractResult(getNewestFileName());
|
|
163 |
}
|
|
164 |
|
|
165 |
void QBenchmarkValgrindUtils::cleanup()
|
|
166 |
{
|
|
167 |
QStringList nameFilters;
|
|
168 |
QString base = QBenchmarkGlobalData::current->callgrindOutFileBase;
|
|
169 |
Q_ASSERT(!base.isEmpty());
|
|
170 |
nameFilters
|
|
171 |
<< base // overall summary
|
|
172 |
<< QString::fromLatin1("%1.*").arg(base); // individual dumps
|
|
173 |
QFileInfoList fiList = QDir().entryInfoList(nameFilters, QDir::Files | QDir::Readable);
|
|
174 |
foreach (QFileInfo fileInfo, fiList) {
|
|
175 |
const bool removeOk = QFile::remove(fileInfo.fileName());
|
|
176 |
Q_ASSERT(removeOk);
|
|
177 |
Q_UNUSED(removeOk);
|
|
178 |
}
|
|
179 |
}
|
|
180 |
|
|
181 |
QString QBenchmarkValgrindUtils::outFileBase(qint64 pid)
|
|
182 |
{
|
|
183 |
return QString::fromLatin1("callgrind.out.%1").arg(
|
|
184 |
pid != -1 ? pid : QCoreApplication::applicationPid());
|
|
185 |
}
|
|
186 |
|
|
187 |
// Reruns this program through callgrind, storing callgrind result files in the
|
|
188 |
// current directory.
|
|
189 |
// Returns true upon success, otherwise false.
|
|
190 |
bool QBenchmarkValgrindUtils::runCallgrindSubProcess(const QStringList &origAppArgs, int &exitCode)
|
|
191 |
{
|
|
192 |
const QString execFile(origAppArgs.at(0));
|
|
193 |
QStringList args;
|
|
194 |
args << QLatin1String("--tool=callgrind") << QLatin1String("--instr-atstart=yes")
|
|
195 |
<< QLatin1String("--quiet")
|
|
196 |
<< execFile << QLatin1String("-callgrindchild");
|
|
197 |
|
|
198 |
#if (defined Q_WS_QWS)
|
|
199 |
// While running the child process, we aren't processing events, and hence aren't
|
|
200 |
// acting as the QWS server. Therefore it's necessary to tell the child to act
|
|
201 |
// as its own server instead of connecting to us.
|
|
202 |
args << QLatin1String("-qws");
|
|
203 |
#endif
|
|
204 |
|
|
205 |
// pass on original arguments that make sense (e.g. avoid wasting time producing output
|
|
206 |
// that will be ignored anyway) ...
|
|
207 |
for (int i = 1; i < origAppArgs.size(); ++i) {
|
|
208 |
const QString arg(origAppArgs.at(i));
|
|
209 |
if (arg == QLatin1String("-callgrind"))
|
|
210 |
continue;
|
|
211 |
args << arg; // ok to pass on
|
|
212 |
}
|
|
213 |
|
|
214 |
QProcess process;
|
|
215 |
process.start(QLatin1String("valgrind"), args);
|
|
216 |
process.waitForStarted(-1);
|
|
217 |
QBenchmarkGlobalData::current->callgrindOutFileBase =
|
|
218 |
QBenchmarkValgrindUtils::outFileBase(process.pid());
|
|
219 |
const bool finishedOk = process.waitForFinished(-1);
|
|
220 |
exitCode = process.exitCode();
|
|
221 |
|
|
222 |
dumpOutput(process.readAllStandardOutput(), stdout);
|
|
223 |
dumpOutput(process.readAllStandardError(), stderr);
|
|
224 |
|
|
225 |
return finishedOk;
|
|
226 |
}
|
|
227 |
|
|
228 |
void QBenchmarkCallgrindMeasurer::start()
|
|
229 |
{
|
|
230 |
CALLGRIND_ZERO_STATS;
|
|
231 |
}
|
|
232 |
|
|
233 |
qint64 QBenchmarkCallgrindMeasurer::checkpoint()
|
|
234 |
{
|
|
235 |
CALLGRIND_DUMP_STATS;
|
|
236 |
const qint64 result = QBenchmarkValgrindUtils::extractLastResult();
|
|
237 |
return result;
|
|
238 |
}
|
|
239 |
|
|
240 |
qint64 QBenchmarkCallgrindMeasurer::stop()
|
|
241 |
{
|
|
242 |
return checkpoint();
|
|
243 |
}
|
|
244 |
|
|
245 |
bool QBenchmarkCallgrindMeasurer::isMeasurementAccepted(qint64 measurement)
|
|
246 |
{
|
|
247 |
Q_UNUSED(measurement);
|
|
248 |
return true;
|
|
249 |
}
|
|
250 |
|
|
251 |
int QBenchmarkCallgrindMeasurer::adjustIterationCount(int)
|
|
252 |
{
|
|
253 |
return 1;
|
|
254 |
}
|
|
255 |
|
|
256 |
int QBenchmarkCallgrindMeasurer::adjustMedianCount(int)
|
|
257 |
{
|
|
258 |
return 1;
|
|
259 |
}
|
|
260 |
|
|
261 |
bool QBenchmarkCallgrindMeasurer::needsWarmupIteration()
|
|
262 |
{
|
|
263 |
return true;
|
|
264 |
}
|
|
265 |
|
|
266 |
QString QBenchmarkCallgrindMeasurer::unitText()
|
|
267 |
{
|
|
268 |
return QLatin1String("instr. loads");
|
|
269 |
}
|
|
270 |
|
|
271 |
QString QBenchmarkCallgrindMeasurer::metricText()
|
|
272 |
{
|
|
273 |
return QLatin1String("callgrind");
|
|
274 |
}
|
|
275 |
|
|
276 |
QT_END_NAMESPACE
|
|
277 |
|
|
278 |
#endif // QTESTLIB_USE_VALGRIND
|