1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtTest 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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <QtTest/private/qbenchmark_p.h>
41
42#include <QtTest/private/qbenchmarkvalgrind_p.h>
43#include <QtCore/qstringlist.h>
44#include <QtCore/qcoreapplication.h>
45#include <QtCore/qprocess.h>
46#include <QtCore/qdir.h>
47#include <QtCore/qregularexpression.h>
48#include <QtCore/qset.h>
49#include <QtTest/private/callgrind_p.h>
50
51QT_BEGIN_NAMESPACE
52
53// Returns \c true if valgrind is available.
54bool QBenchmarkValgrindUtils::haveValgrind()
55{
56#ifdef NVALGRIND
57 return false;
58#else
59 QProcess process;
60 process.start(program: QLatin1String("valgrind"), arguments: QStringList(QLatin1String("--version")));
61 return process.waitForStarted() && process.waitForFinished(msecs: -1);
62#endif
63}
64
65// Reruns this program through callgrind.
66// Returns \c true upon success, otherwise false.
67bool QBenchmarkValgrindUtils::rerunThroughCallgrind(const QStringList &origAppArgs, int &exitCode)
68{
69 if (!QBenchmarkValgrindUtils::runCallgrindSubProcess(origAppArgs, exitCode)) {
70 qWarning(msg: "failed to run callgrind subprocess");
71 return false;
72 }
73 return true;
74}
75
76static void dumpOutput(const QByteArray &data, FILE *fh)
77{
78 QFile file;
79 file.open(f: fh, ioFlags: QIODevice::WriteOnly);
80 file.write(data);
81}
82
83qint64 QBenchmarkValgrindUtils::extractResult(const QString &fileName)
84{
85 QFile file(fileName);
86 const bool openOk = file.open(flags: QIODevice::ReadOnly | QIODevice::Text);
87 Q_ASSERT(openOk);
88 Q_UNUSED(openOk);
89
90 qint64 val = -1;
91 bool valSeen = false;
92 QRegularExpression rxValue(QLatin1String("^summary: (\\d+)"));
93 while (!file.atEnd()) {
94 const QString line(QLatin1String(file.readLine()));
95 QRegularExpressionMatch match = rxValue.match(subject: line);
96 if (match.hasMatch()) {
97 bool ok;
98 val = match.captured(nth: 1).toLongLong(ok: &ok);
99 Q_ASSERT(ok);
100 valSeen = true;
101 break;
102 }
103 }
104 if (Q_UNLIKELY(!valSeen))
105 qFatal(msg: "Failed to extract result");
106 return val;
107}
108
109// Gets the newest file name (i.e. the one with the highest integer suffix).
110QString QBenchmarkValgrindUtils::getNewestFileName()
111{
112 QStringList nameFilters;
113 QString base = QBenchmarkGlobalData::current->callgrindOutFileBase;
114 Q_ASSERT(!base.isEmpty());
115
116 nameFilters << QString::fromLatin1(str: "%1.*").arg(a: base);
117 const QFileInfoList fiList = QDir().entryInfoList(nameFilters, filters: QDir::Files | QDir::Readable);
118 Q_ASSERT(!fiList.empty());
119 int hiSuffix = -1;
120 QFileInfo lastFileInfo;
121 const QString pattern = QString::fromLatin1(str: "%1.(\\d+)").arg(a: base);
122 QRegularExpression rx(pattern);
123 for (const QFileInfo &fileInfo : fiList) {
124 QRegularExpressionMatch match = rx.match(subject: fileInfo.fileName());
125 Q_ASSERT(match.hasMatch());
126 bool ok;
127 const int suffix = match.captured(nth: 1).toInt(ok: &ok);
128 Q_ASSERT(ok);
129 Q_ASSERT(suffix >= 0);
130 if (suffix > hiSuffix) {
131 lastFileInfo = fileInfo;
132 hiSuffix = suffix;
133 }
134 }
135
136 return lastFileInfo.fileName();
137}
138
139qint64 QBenchmarkValgrindUtils::extractLastResult()
140{
141 return extractResult(fileName: getNewestFileName());
142}
143
144void QBenchmarkValgrindUtils::cleanup()
145{
146 QStringList nameFilters;
147 QString base = QBenchmarkGlobalData::current->callgrindOutFileBase;
148 Q_ASSERT(!base.isEmpty());
149 nameFilters
150 << base // overall summary
151 << QString::fromLatin1(str: "%1.*").arg(a: base); // individual dumps
152 const QFileInfoList fiList = QDir().entryInfoList(nameFilters, filters: QDir::Files | QDir::Readable);
153 for (const QFileInfo &fileInfo : fiList) {
154 const bool removeOk = QFile::remove(fileName: fileInfo.fileName());
155 Q_ASSERT(removeOk);
156 Q_UNUSED(removeOk);
157 }
158}
159
160QString QBenchmarkValgrindUtils::outFileBase(qint64 pid)
161{
162 return QString::fromLatin1(str: "callgrind.out.%1").arg(
163 a: pid != -1 ? pid : QCoreApplication::applicationPid());
164}
165
166// Reruns this program through callgrind, storing callgrind result files in the
167// current directory.
168// Returns \c true upon success, otherwise false.
169bool QBenchmarkValgrindUtils::runCallgrindSubProcess(const QStringList &origAppArgs, int &exitCode)
170{
171 const QString &execFile = origAppArgs.at(i: 0);
172 QStringList args;
173 args << QLatin1String("--tool=callgrind") << QLatin1String("--instr-atstart=yes")
174 << QLatin1String("--quiet")
175 << execFile << QLatin1String("-callgrindchild");
176
177 // pass on original arguments that make sense (e.g. avoid wasting time producing output
178 // that will be ignored anyway) ...
179 for (int i = 1; i < origAppArgs.size(); ++i) {
180 const QString &arg = origAppArgs.at(i);
181 if (arg == QLatin1String("-callgrind"))
182 continue;
183 args << arg; // ok to pass on
184 }
185
186 QProcess process;
187 process.start(program: QLatin1String("valgrind"), arguments: args);
188 process.waitForStarted(msecs: -1);
189 QBenchmarkGlobalData::current->callgrindOutFileBase =
190 QBenchmarkValgrindUtils::outFileBase(pid: process.processId());
191 const bool finishedOk = process.waitForFinished(msecs: -1);
192 exitCode = process.exitCode();
193
194 dumpOutput(data: process.readAllStandardOutput(), stdout);
195 dumpOutput(data: process.readAllStandardError(), stderr);
196
197 return finishedOk;
198}
199
200void QBenchmarkCallgrindMeasurer::start()
201{
202 CALLGRIND_ZERO_STATS;
203}
204
205qint64 QBenchmarkCallgrindMeasurer::checkpoint()
206{
207 CALLGRIND_DUMP_STATS;
208 const qint64 result = QBenchmarkValgrindUtils::extractLastResult();
209 return result;
210}
211
212qint64 QBenchmarkCallgrindMeasurer::stop()
213{
214 return checkpoint();
215}
216
217bool QBenchmarkCallgrindMeasurer::isMeasurementAccepted(qint64 measurement)
218{
219 Q_UNUSED(measurement);
220 return true;
221}
222
223int QBenchmarkCallgrindMeasurer::adjustIterationCount(int)
224{
225 return 1;
226}
227
228int QBenchmarkCallgrindMeasurer::adjustMedianCount(int)
229{
230 return 1;
231}
232
233bool QBenchmarkCallgrindMeasurer::needsWarmupIteration()
234{
235 return true;
236}
237
238QTest::QBenchmarkMetric QBenchmarkCallgrindMeasurer::metricType()
239{
240 return QTest::InstructionReads;
241}
242
243QT_END_NAMESPACE
244

source code of qtbase/src/testlib/qbenchmarkvalgrind.cpp