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