source: trunk/src/testlib/qbenchmarkvalgrind.cpp@ 349

Last change on this file since 349 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.6 KB
Line 
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.
56bool 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.
87bool 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
96static void dumpOutput(const QByteArray &data, FILE *fh)
97{
98 QFile file;
99 file.open(fh, QIODevice::WriteOnly);
100 file.write(data);
101}
102
103qint64 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).
129QString 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);