1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtTest module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include <QtTest/private/qbenchmark_p.h>
44 #ifdef QTESTLIB_USE_VALGRIND
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 <QtTest/private/callgrind_p.h>
56 // Returns true iff a sufficiently recent valgrind is available.
57 bool QBenchmarkValgrindUtils::haveValgrind()
64 args << QLatin1String("--version");
65 process.start(QLatin1String("valgrind"), args);
66 if (!process.waitForFinished(-1))
68 const QByteArray out = process.readAllStandardOutput();
69 QRegExp rx(QLatin1String("^valgrind-([0-9]).([0-9]).[0-9]"));
70 if (rx.indexIn(QLatin1String(out.data())) == -1)
73 const int major = rx.cap(1).toInt(&ok);
76 const int minor = rx.cap(2).toInt(&ok);
79 // return (major > 3 || (major == 3 && minor >= 3)); // v >= 3.3 for --callgrind-out-file option
82 return true; // skip version restriction for now
86 // Reruns this program through callgrind.
87 // Returns true upon success, otherwise false.
88 bool QBenchmarkValgrindUtils::rerunThroughCallgrind(const QStringList &origAppArgs, int &exitCode)
90 if (!QBenchmarkValgrindUtils::runCallgrindSubProcess(origAppArgs, exitCode)) {
91 qWarning("failed to run callgrind subprocess");
97 static void dumpOutput(const QByteArray &data, FILE *fh)
100 file.open(fh, QIODevice::WriteOnly);
104 qint64 QBenchmarkValgrindUtils::extractResult(const QString &fileName)
106 QFile file(fileName);
107 const bool openOk = file.open(QIODevice::ReadOnly | QIODevice::Text);
112 bool valSeen = false;
113 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.captureCount() == 1);
119 val = rxValue.cap(1).toLongLong(&ok);
126 qFatal("Failed to extract result");
130 // Gets the newest file name (i.e. the one with the highest integer suffix).
131 QString QBenchmarkValgrindUtils::getNewestFileName()
133 QStringList nameFilters;
134 QString base = QBenchmarkGlobalData::current->callgrindOutFileBase;
135 Q_ASSERT(!base.isEmpty());
137 nameFilters << QString::fromLatin1("%1.*").arg(base);
138 QFileInfoList fiList = QDir().entryInfoList(nameFilters, QDir::Files | QDir::Readable);
139 Q_ASSERT(!fiList.empty());
141 QFileInfo lastFileInfo;
142 const QString pattern = QString::fromLatin1("%1.(\\d+)").arg(base);
144 foreach (const QFileInfo &fileInfo, fiList) {
145 const int index = rx.indexIn(fileInfo.fileName());
146 Q_ASSERT(index == 0);
149 const int suffix = rx.cap(1).toInt(&ok);
151 Q_ASSERT(suffix >= 0);
152 if (suffix > hiSuffix) {
153 lastFileInfo = fileInfo;
158 return lastFileInfo.fileName();
161 qint64 QBenchmarkValgrindUtils::extractLastResult()
163 return extractResult(getNewestFileName());
166 void QBenchmarkValgrindUtils::cleanup()
168 QStringList nameFilters;
169 QString base = QBenchmarkGlobalData::current->callgrindOutFileBase;
170 Q_ASSERT(!base.isEmpty());
172 << base // overall summary
173 << QString::fromLatin1("%1.*").arg(base); // individual dumps
174 QFileInfoList fiList = QDir().entryInfoList(nameFilters, QDir::Files | QDir::Readable);
175 foreach (const QFileInfo &fileInfo, fiList) {
176 const bool removeOk = QFile::remove(fileInfo.fileName());
182 QString QBenchmarkValgrindUtils::outFileBase(qint64 pid)
184 return QString::fromLatin1("callgrind.out.%1").arg(
185 pid != -1 ? pid : QCoreApplication::applicationPid());
188 // Reruns this program through callgrind, storing callgrind result files in the
189 // current directory.
190 // Returns true upon success, otherwise false.
191 bool QBenchmarkValgrindUtils::runCallgrindSubProcess(const QStringList &origAppArgs, int &exitCode)
193 const QString execFile(origAppArgs.at(0));
195 args << QLatin1String("--tool=callgrind") << QLatin1String("--instr-atstart=yes")
196 << QLatin1String("--quiet")
197 << execFile << QLatin1String("-callgrindchild");
199 // pass on original arguments that make sense (e.g. avoid wasting time producing output
200 // that will be ignored anyway) ...
201 for (int i = 1; i < origAppArgs.size(); ++i) {
202 const QString arg(origAppArgs.at(i));
203 if (arg == QLatin1String("-callgrind"))
205 args << arg; // ok to pass on
209 process.start(QLatin1String("valgrind"), args);
210 process.waitForStarted(-1);
211 QBenchmarkGlobalData::current->callgrindOutFileBase =
212 QBenchmarkValgrindUtils::outFileBase(process.pid());
213 const bool finishedOk = process.waitForFinished(-1);
214 exitCode = process.exitCode();
216 dumpOutput(process.readAllStandardOutput(), stdout);
217 dumpOutput(process.readAllStandardError(), stderr);
222 void QBenchmarkCallgrindMeasurer::start()
224 CALLGRIND_ZERO_STATS;
227 qint64 QBenchmarkCallgrindMeasurer::checkpoint()
229 CALLGRIND_DUMP_STATS;
230 const qint64 result = QBenchmarkValgrindUtils::extractLastResult();
234 qint64 QBenchmarkCallgrindMeasurer::stop()
239 bool QBenchmarkCallgrindMeasurer::isMeasurementAccepted(qint64 measurement)
241 Q_UNUSED(measurement);
245 int QBenchmarkCallgrindMeasurer::adjustIterationCount(int)
250 int QBenchmarkCallgrindMeasurer::adjustMedianCount(int)
255 bool QBenchmarkCallgrindMeasurer::needsWarmupIteration()
260 QTest::QBenchmarkMetric QBenchmarkCallgrindMeasurer::metricType()
262 return QTest::InstructionReads;
267 #endif // QTESTLIB_USE_VALGRIND