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