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 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | // Returns \c true if valgrind is available. |
54 | bool QBenchmarkValgrindUtils::haveValgrind() |
55 | { |
56 | #ifdef NVALGRIND |
57 | return false; |
58 | #else |
59 | QProcess process; |
60 | process.start(QLatin1String("valgrind" ), QStringList(QLatin1String("--version" ))); |
61 | return process.waitForStarted() && process.waitForFinished(-1); |
62 | #endif |
63 | } |
64 | |
65 | // Reruns this program through callgrind. |
66 | // Returns \c true upon success, otherwise false. |
67 | bool QBenchmarkValgrindUtils::rerunThroughCallgrind(const QStringList &origAppArgs, int &exitCode) |
68 | { |
69 | if (!QBenchmarkValgrindUtils::runCallgrindSubProcess(origAppArgs, exitCode)) { |
70 | qWarning("failed to run callgrind subprocess" ); |
71 | return false; |
72 | } |
73 | return true; |
74 | } |
75 | |
76 | static void dumpOutput(const QByteArray &data, FILE *fh) |
77 | { |
78 | QFile file; |
79 | file.open(fh, QIODevice::WriteOnly); |
80 | file.write(data); |
81 | } |
82 | |
83 | qint64 QBenchmarkValgrindUtils::(const QString &fileName) |
84 | { |
85 | QFile file(fileName); |
86 | const bool openOk = file.open(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(line); |
96 | if (match.hasMatch()) { |
97 | bool ok; |
98 | val = match.captured(1).toLongLong(&ok); |
99 | Q_ASSERT(ok); |
100 | valSeen = true; |
101 | break; |
102 | } |
103 | } |
104 | if (Q_UNLIKELY(!valSeen)) |
105 | qFatal("Failed to extract result" ); |
106 | return val; |
107 | } |
108 | |
109 | // Gets the newest file name (i.e. the one with the highest integer suffix). |
110 | QString QBenchmarkValgrindUtils::getNewestFileName() |
111 | { |
112 | QStringList nameFilters; |
113 | QString base = QBenchmarkGlobalData::current->callgrindOutFileBase; |
114 | Q_ASSERT(!base.isEmpty()); |
115 | |
116 | nameFilters << QString::fromLatin1("%1.*" ).arg(base); |
117 | const QFileInfoList fiList = QDir().entryInfoList(nameFilters, QDir::Files | QDir::Readable); |
118 | Q_ASSERT(!fiList.empty()); |
119 | int hiSuffix = -1; |
120 | QFileInfo lastFileInfo; |
121 | const QString pattern = QString::fromLatin1("%1.(\\d+)" ).arg(base); |
122 | QRegularExpression rx(pattern); |
123 | for (const QFileInfo &fileInfo : fiList) { |
124 | QRegularExpressionMatch match = rx.match(fileInfo.fileName()); |
125 | Q_ASSERT(match.hasMatch()); |
126 | bool ok; |
127 | const int suffix = match.captured(1).toInt(&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 | |
139 | qint64 QBenchmarkValgrindUtils::() |
140 | { |
141 | return extractResult(getNewestFileName()); |
142 | } |
143 | |
144 | void 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("%1.*" ).arg(base); // individual dumps |
152 | const QFileInfoList fiList = QDir().entryInfoList(nameFilters, QDir::Files | QDir::Readable); |
153 | for (const QFileInfo &fileInfo : fiList) { |
154 | const bool removeOk = QFile::remove(fileInfo.fileName()); |
155 | Q_ASSERT(removeOk); |
156 | Q_UNUSED(removeOk); |
157 | } |
158 | } |
159 | |
160 | QString QBenchmarkValgrindUtils::outFileBase(qint64 pid) |
161 | { |
162 | return QString::fromLatin1("callgrind.out.%1" ).arg( |
163 | 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. |
169 | bool QBenchmarkValgrindUtils::runCallgrindSubProcess(const QStringList &origAppArgs, int &exitCode) |
170 | { |
171 | const QString &execFile = origAppArgs.at(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(QLatin1String("valgrind" ), args); |
188 | process.waitForStarted(-1); |
189 | QBenchmarkGlobalData::current->callgrindOutFileBase = |
190 | QBenchmarkValgrindUtils::outFileBase(process.processId()); |
191 | const bool finishedOk = process.waitForFinished(-1); |
192 | exitCode = process.exitCode(); |
193 | |
194 | dumpOutput(process.readAllStandardOutput(), stdout); |
195 | dumpOutput(process.readAllStandardError(), stderr); |
196 | |
197 | return finishedOk; |
198 | } |
199 | |
200 | void QBenchmarkCallgrindMeasurer::start() |
201 | { |
202 | CALLGRIND_ZERO_STATS; |
203 | } |
204 | |
205 | qint64 QBenchmarkCallgrindMeasurer::checkpoint() |
206 | { |
207 | CALLGRIND_DUMP_STATS; |
208 | const qint64 result = QBenchmarkValgrindUtils::extractLastResult(); |
209 | return result; |
210 | } |
211 | |
212 | qint64 QBenchmarkCallgrindMeasurer::stop() |
213 | { |
214 | return checkpoint(); |
215 | } |
216 | |
217 | bool QBenchmarkCallgrindMeasurer::isMeasurementAccepted(qint64 measurement) |
218 | { |
219 | Q_UNUSED(measurement); |
220 | return true; |
221 | } |
222 | |
223 | int QBenchmarkCallgrindMeasurer::adjustIterationCount(int) |
224 | { |
225 | return 1; |
226 | } |
227 | |
228 | int QBenchmarkCallgrindMeasurer::adjustMedianCount(int) |
229 | { |
230 | return 1; |
231 | } |
232 | |
233 | bool QBenchmarkCallgrindMeasurer::needsWarmupIteration() |
234 | { |
235 | return true; |
236 | } |
237 | |
238 | QTest::QBenchmarkMetric QBenchmarkCallgrindMeasurer::metricType() |
239 | { |
240 | return QTest::InstructionReads; |
241 | } |
242 | |
243 | QT_END_NAMESPACE |
244 | |