1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
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 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 "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::(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.captureCount() == 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::() |
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 | #if defined(Q_CC_GNU) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406) |
229 | // the callgrind macros below generate warnings |
230 | # pragma GCC diagnostic push |
231 | # pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
232 | #endif |
233 | |
234 | void QBenchmarkCallgrindMeasurer::start() |
235 | { |
236 | CALLGRIND_ZERO_STATS; |
237 | } |
238 | |
239 | qint64 QBenchmarkCallgrindMeasurer::checkpoint() |
240 | { |
241 | CALLGRIND_DUMP_STATS; |
242 | const qint64 result = QBenchmarkValgrindUtils::extractLastResult(); |
243 | return result; |
244 | } |
245 | |
246 | #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406) |
247 | // the callgrind macros above generate warnings |
248 | # pragma GCC diagnostic pop |
249 | #endif |
250 | |
251 | qint64 QBenchmarkCallgrindMeasurer::stop() |
252 | { |
253 | return checkpoint(); |
254 | } |
255 | |
256 | bool QBenchmarkCallgrindMeasurer::isMeasurementAccepted(qint64 measurement) |
257 | { |
258 | Q_UNUSED(measurement); |
259 | return true; |
260 | } |
261 | |
262 | int QBenchmarkCallgrindMeasurer::adjustIterationCount(int) |
263 | { |
264 | return 1; |
265 | } |
266 | |
267 | int QBenchmarkCallgrindMeasurer::adjustMedianCount(int) |
268 | { |
269 | return 1; |
270 | } |
271 | |
272 | bool QBenchmarkCallgrindMeasurer::needsWarmupIteration() |
273 | { |
274 | return true; |
275 | } |
276 | |
277 | QTest::QBenchmarkMetric QBenchmarkCallgrindMeasurer::metricType() |
278 | { |
279 | return QTest::InstructionReads; |
280 | } |
281 | |
282 | QT_END_NAMESPACE |
283 | |
284 | #endif // QTESTLIB_USE_VALGRIND |
285 | |