Fix binary incompatibility between openssl versions
[profile/ivi/qtbase.git] / src / testlib / qbenchmarkvalgrind.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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 <QtTest/private/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     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::extractResult(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     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     if (!valSeen)
126         qFatal("Failed to extract result");
127     return val;
128 }
129
130 // Gets the newest file name (i.e. the one with the highest integer suffix).
131 QString QBenchmarkValgrindUtils::getNewestFileName()
132 {
133     QStringList nameFilters;
134     QString base = QBenchmarkGlobalData::current->callgrindOutFileBase;
135     Q_ASSERT(!base.isEmpty());
136
137     nameFilters << QString::fromLatin1("%1.*").arg(base);
138     QFileInfoList fiList = QDir().entryInfoList(nameFilters, QDir::Files | QDir::Readable);
139     Q_ASSERT(!fiList.empty());
140     int hiSuffix = -1;
141     QFileInfo lastFileInfo;
142     const QString pattern = QString::fromLatin1("%1.(\\d+)").arg(base);
143     QRegExp rx(pattern);
144     foreach (const QFileInfo &fileInfo, fiList) {
145         const int index = rx.indexIn(fileInfo.fileName());
146         Q_ASSERT(index == 0);
147         Q_UNUSED(index);
148         bool ok;
149         const int suffix = rx.cap(1).toInt(&ok);
150         Q_ASSERT(ok);
151         Q_ASSERT(suffix >= 0);
152         if (suffix > hiSuffix) {
153             lastFileInfo = fileInfo;
154             hiSuffix = suffix;
155         }
156     }
157
158     return lastFileInfo.fileName();
159 }
160
161 qint64 QBenchmarkValgrindUtils::extractLastResult()
162 {
163     return extractResult(getNewestFileName());
164 }
165
166 void QBenchmarkValgrindUtils::cleanup()
167 {
168     QStringList nameFilters;
169     QString base = QBenchmarkGlobalData::current->callgrindOutFileBase;
170     Q_ASSERT(!base.isEmpty());
171     nameFilters
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());
177         Q_ASSERT(removeOk);
178         Q_UNUSED(removeOk);
179     }
180 }
181
182 QString QBenchmarkValgrindUtils::outFileBase(qint64 pid)
183 {
184     return QString::fromLatin1("callgrind.out.%1").arg(
185         pid != -1 ? pid : QCoreApplication::applicationPid());
186 }
187
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)
192 {
193     const QString execFile(origAppArgs.at(0));
194     QStringList args;
195     args << QLatin1String("--tool=callgrind") << QLatin1String("--instr-atstart=yes")
196          << QLatin1String("--quiet")
197          << execFile << QLatin1String("-callgrindchild");
198
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"))
204             continue;
205         args << arg; // ok to pass on
206     }
207
208     QProcess process;
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();
215
216     dumpOutput(process.readAllStandardOutput(), stdout);
217     dumpOutput(process.readAllStandardError(), stderr);
218
219     return finishedOk;
220 }
221
222 void QBenchmarkCallgrindMeasurer::start()
223 {
224     CALLGRIND_ZERO_STATS;
225 }
226
227 qint64 QBenchmarkCallgrindMeasurer::checkpoint()
228 {
229     CALLGRIND_DUMP_STATS;
230     const qint64 result = QBenchmarkValgrindUtils::extractLastResult();
231     return result;
232 }
233
234 qint64 QBenchmarkCallgrindMeasurer::stop()
235 {
236     return checkpoint();
237 }
238
239 bool QBenchmarkCallgrindMeasurer::isMeasurementAccepted(qint64 measurement)
240 {
241     Q_UNUSED(measurement);
242     return true;
243 }
244
245 int QBenchmarkCallgrindMeasurer::adjustIterationCount(int)
246 {
247     return 1;
248 }
249
250 int QBenchmarkCallgrindMeasurer::adjustMedianCount(int)
251 {
252     return 1;
253 }
254
255 bool QBenchmarkCallgrindMeasurer::needsWarmupIteration()
256 {
257     return true;
258 }
259
260 QTest::QBenchmarkMetric QBenchmarkCallgrindMeasurer::metricType()
261 {
262     return QTest::InstructionReads;
263 }
264
265 QT_END_NAMESPACE
266
267 #endif // QTESTLIB_USE_VALGRIND