Qt 4.8
Static Public Functions | List of all members
QBenchmarkValgrindUtils Class Reference

#include <qbenchmarkvalgrind_p.h>

Static Public Functions

static void cleanup ()
 
static qint64 extractLastResult ()
 
static qint64 extractResult (const QString &fileName)
 
static QString getNewestFileName ()
 
static bool haveValgrind ()
 
static QString outFileBase (qint64 pid=-1)
 
static bool rerunThroughCallgrind (const QStringList &origAppArgs, int &exitCode)
 
static bool runCallgrindSubProcess (const QStringList &origAppArgs, int &exitCode)
 

Detailed Description

Definition at line 65 of file qbenchmarkvalgrind_p.h.

Functions

◆ cleanup()

void QBenchmarkValgrindUtils::cleanup ( )
static

Definition at line 165 of file qbenchmarkvalgrind.cpp.

Referenced by QTest::qExec().

166 {
167  QStringList nameFilters;
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 }
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
static QBenchmarkGlobalData * current
Definition: qbenchmark_p.h:136
QString fileName() const
Returns the name of the file, excluding the path.
Definition: qfileinfo.cpp:726
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static const uint base
Definition: qurl.cpp:268
QString callgrindOutFileBase
Definition: qbenchmark_p.h:153
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QFileInfoList entryInfoList(Filters filters=NoFilter, SortFlags sort=NoSort) const
Returns a list of QFileInfo objects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting().
Definition: qdir.cpp:1316
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
bool remove()
Removes the file specified by fileName().
Definition: qfile.cpp:715
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ extractLastResult()

qint64 QBenchmarkValgrindUtils::extractLastResult ( )
static

Definition at line 160 of file qbenchmarkvalgrind.cpp.

Referenced by QBenchmarkCallgrindMeasurer::checkpoint().

161 {
163 }
static QString getNewestFileName()
static qint64 extractResult(const QString &fileName)

◆ extractResult()

qint64 QBenchmarkValgrindUtils::extractResult ( const QString fileName)
static

Definition at line 104 of file qbenchmarkvalgrind.cpp.

Referenced by extractLastResult().

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 }
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
__int64 qint64
Definition: qglobal.h:942
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ getNewestFileName()

QString QBenchmarkValgrindUtils::getNewestFileName ( )
static

Definition at line 130 of file qbenchmarkvalgrind.cpp.

Referenced by extractLastResult().

131 {
132  QStringList nameFilters;
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 }
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
static QBenchmarkGlobalData * current
Definition: qbenchmark_p.h:136
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
bool empty() const
This function is provided for STL compatibility.
Definition: qlist.h:304
QString fileName() const
Returns the name of the file, excluding the path.
Definition: qfileinfo.cpp:726
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static const uint base
Definition: qurl.cpp:268
QString callgrindOutFileBase
Definition: qbenchmark_p.h:153
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QFileInfoList entryInfoList(Filters filters=NoFilter, SortFlags sort=NoSort) const
Returns a list of QFileInfo objects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting().
Definition: qdir.cpp:1316
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
int toInt(bool *ok=0, int base=10) const
Returns the byte array converted to an int using base base, which is 10 by default and must be betwee...
QByteArray suffix
quint16 index
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ haveValgrind()

bool QBenchmarkValgrindUtils::haveValgrind ( )
static

Definition at line 57 of file qbenchmarkvalgrind.cpp.

Referenced by QTest::qtest_qParseArgs().

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 }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool waitForFinished(int msecs=30000)
Blocks until the process has finished and the finished() signal has been emitted, or until msecs mill...
Definition: qprocess.cpp:1863
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
void start(const QString &program, const QStringList &arguments, OpenMode mode=ReadWrite)
Starts the given program in a new process, if none is already running, passing the command line argum...
Definition: qprocess.cpp:2060
QByteArray readAllStandardOutput()
Regardless of the current read channel, this function returns all data available from the standard ou...
Definition: qprocess.cpp:2013
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102

◆ outFileBase()

QString QBenchmarkValgrindUtils::outFileBase ( qint64  pid = -1)
static

Definition at line 181 of file qbenchmarkvalgrind.cpp.

Referenced by QTest::qtest_qParseArgs(), and runCallgrindSubProcess().

182 {
183  return QString::fromLatin1("callgrind.out.%1").arg(
184  pid != -1 ? pid : QCoreApplication::applicationPid());
185 }
static qint64 applicationPid()
Returns the current process ID for the application.
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188

◆ rerunThroughCallgrind()

bool QBenchmarkValgrindUtils::rerunThroughCallgrind ( const QStringList origAppArgs,
int &  exitCode 
)
static

Definition at line 88 of file qbenchmarkvalgrind.cpp.

Referenced by QTest::qExec().

89 {
90  if (!QBenchmarkValgrindUtils::runCallgrindSubProcess(origAppArgs, exitCode)) {
91  qWarning("failed to run callgrind subprocess");
92  return false;
93  }
94  return true;
95 }
static bool runCallgrindSubProcess(const QStringList &origAppArgs, int &exitCode)
Q_CORE_EXPORT void qWarning(const char *,...)

◆ runCallgrindSubProcess()

bool QBenchmarkValgrindUtils::runCallgrindSubProcess ( const QStringList origAppArgs,
int &  exitCode 
)
static

Definition at line 190 of file qbenchmarkvalgrind.cpp.

Referenced by rerunThroughCallgrind().

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);
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 }
int exitCode() const
Returns the exit code of the last process that finished.
Definition: qprocess.cpp:2240
static QString outFileBase(qint64 pid=-1)
static QBenchmarkGlobalData * current
Definition: qbenchmark_p.h:136
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_PID pid() const
Returns the native process identifier for the running process, if available.
Definition: qprocess.cpp:1581
QString callgrindOutFileBase
Definition: qbenchmark_p.h:153
bool waitForFinished(int msecs=30000)
Blocks until the process has finished and the finished() signal has been emitted, or until msecs mill...
Definition: qprocess.cpp:1863
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static void dumpOutput(const QByteArray &data, FILE *fh)
QByteArray readAllStandardError()
Regardless of the current read channel, this function returns all data available from the standard er...
Definition: qprocess.cpp:2029
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
void start(const QString &program, const QStringList &arguments, OpenMode mode=ReadWrite)
Starts the given program in a new process, if none is already running, passing the command line argum...
Definition: qprocess.cpp:2060
QByteArray readAllStandardOutput()
Regardless of the current read channel, this function returns all data available from the standard ou...
Definition: qprocess.cpp:2013
bool waitForStarted(int msecs=30000)
Blocks until the process has started and the started() signal has been emitted, or until msecs millis...
Definition: qprocess.cpp:1800
The QProcess class is used to start external programs and to communicate with them.
Definition: qprocess.h:102

The documentation for this class was generated from the following files: