blob: 77f778f51644298cf48ed3fbcd0bc105b4dcc97e [file] [log] [blame]
Zack Rusin3acde362011-04-06 01:11:55 -04001#include "retracer.h"
2
Zack Rusinf389ae82011-04-10 19:27:28 -04003#include "apitracecall.h"
José Fonseca3f456402012-03-25 20:59:24 +01004#include "thumbnail.h"
Zack Rusinf389ae82011-04-10 19:27:28 -04005
José Fonsecae7102bf2012-12-07 07:33:05 +00006#include "image/image.hpp"
Dan McCabe66dfdda2012-03-05 17:20:39 -08007
James Bentonfc4f55a2012-08-08 17:09:07 +01008#include "trace_profiler.hpp"
9
Zack Rusin3acde362011-04-06 01:11:55 -040010#include <QDebug>
Zack Rusinf389ae82011-04-10 19:27:28 -040011#include <QVariant>
Dan McCabe66dfdda2012-03-05 17:20:39 -080012#include <QList>
13#include <QImage>
José Fonseca3f4cd302015-01-14 12:02:06 +000014#include <QJsonDocument>
Zack Rusin3acde362011-04-06 01:11:55 -040015
José Fonseca6ea8dee2012-03-25 17:25:24 +010016/**
17 * Wrapper around a QProcess which enforces IO to block .
18 *
19 * Several QIODevice users (notably QJSON) expect blocking semantics, e.g.,
20 * they expect that QIODevice::read() will blocked until the requested ammount
21 * of bytes is read or end of file is reached. But by default QProcess, does
22 * not block. And passing QIODevice::Unbuffered mitigates but does not fully
23 * address the problem either.
24 *
25 * This class wraps around QProcess, providing QIODevice interface, while
26 * ensuring that all reads block.
27 *
28 * This class also works around a bug in QProcess::atEnd() implementation.
29 *
30 * See also:
31 * - http://qt-project.org/wiki/Simple_Crypt_IO_Device
32 * - http://qt-project.org/wiki/Custom_IO_Device
33 */
34class BlockingIODevice : public QIODevice
35{
36 /* We don't use the Q_OBJECT in this class given we don't declare any
37 * signals and slots or use any other services provided by Qt's meta-object
38 * system. */
39public:
40 BlockingIODevice(QProcess * io);
41 bool isSequential() const;
42 bool atEnd() const;
43 bool waitForReadyRead(int msecs = -1);
44
45protected:
46 qint64 readData(char * data, qint64 maxSize);
47 qint64 writeData(const char * data, qint64 maxSize);
48
49private:
50 QProcess *m_device;
51};
52
53BlockingIODevice::BlockingIODevice(QProcess * io) :
54 m_device(io)
55{
56 /*
57 * We pass QIODevice::Unbuffered to prevent the base QIODevice class to do
58 * its own buffering on top of the overridden readData() method.
59 *
60 * The only buffering used will be to satisfy QIODevice::peek() and
61 * QIODevice::ungetChar().
62 */
63 setOpenMode(ReadOnly | Unbuffered);
64}
65
66bool BlockingIODevice::isSequential() const
67{
68 return true;
69}
70
71bool BlockingIODevice::atEnd() const
72{
73 /*
74 * XXX: QProcess::atEnd() documentation is wrong -- it will return true
75 * even when the process is running --, so we try to workaround that here.
76 */
77 if (m_device->atEnd()) {
78 if (m_device->state() == QProcess::Running) {
79 if (!m_device->waitForReadyRead(-1)) {
80 return true;
81 }
82 }
83 }
84 return false;
85}
86
87bool BlockingIODevice::waitForReadyRead(int msecs)
88{
89 Q_UNUSED(msecs);
90 return true;
91}
92
93qint64 BlockingIODevice::readData(char * data, qint64 maxSize)
94{
95 qint64 bytesToRead = maxSize;
96 qint64 readSoFar = 0;
97 do {
98 qint64 chunkSize = m_device->read(data + readSoFar, bytesToRead);
99 if (chunkSize < 0) {
100 if (readSoFar) {
101 return readSoFar;
102 } else {
103 return chunkSize;
104 }
105 }
106 Q_ASSERT(chunkSize <= bytesToRead);
107 bytesToRead -= chunkSize;
108 readSoFar += chunkSize;
109 if (bytesToRead) {
110 if (!m_device->waitForReadyRead(-1)) {
111 qDebug() << "waitForReadyRead failed\n";
112 break;
113 }
114 }
115 } while(bytesToRead);
116
117 return readSoFar;
118}
119
120qint64 BlockingIODevice::writeData(const char * data, qint64 maxSize)
121{
122 Q_ASSERT(false);
123 return -1;
124}
125
José Fonseca5bba4772012-03-25 12:46:04 +0100126Q_DECLARE_METATYPE(QList<ApiTraceError>);
127
Zack Rusin3acde362011-04-06 01:11:55 -0400128Retracer::Retracer(QObject *parent)
Zack Rusinf389ae82011-04-10 19:27:28 -0400129 : QThread(parent),
Zack Rusin404a1ef2011-04-19 23:49:56 -0400130 m_benchmarking(false),
Zack Rusin3acde362011-04-06 01:11:55 -0400131 m_doubleBuffered(true),
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400132 m_singlethread(false),
José Fonsecac03e4b02014-05-30 17:24:42 +0100133 m_useCoreProfile(false),
Zack Rusin3acde362011-04-06 01:11:55 -0400134 m_captureState(false),
José Fonsecac03e4b02014-05-30 17:24:42 +0100135 m_captureThumbnails(false),
James Bentonfc4f55a2012-08-08 17:09:07 +0100136 m_captureCall(0),
137 m_profileGpu(false),
138 m_profileCpu(false),
139 m_profilePixels(false)
Zack Rusin3acde362011-04-06 01:11:55 -0400140{
José Fonseca5bba4772012-03-25 12:46:04 +0100141 qRegisterMetaType<QList<ApiTraceError> >();
Zack Rusin3acde362011-04-06 01:11:55 -0400142}
143
144QString Retracer::fileName() const
145{
146 return m_fileName;
147}
148
149void Retracer::setFileName(const QString &name)
150{
151 m_fileName = name;
152}
153
Carl Worth7257dfc2012-08-09 08:21:42 -0700154QString Retracer::remoteTarget() const
155{
156 return m_remoteTarget;
157}
158
159void Retracer::setRemoteTarget(const QString &host)
160{
161 m_remoteTarget = host;
162}
163
José Fonseca62997b42011-11-27 15:16:34 +0000164void Retracer::setAPI(trace::API api)
165{
166 m_api = api;
167}
168
Zack Rusin3acde362011-04-06 01:11:55 -0400169bool Retracer::isBenchmarking() const
170{
171 return m_benchmarking;
172}
173
174void Retracer::setBenchmarking(bool bench)
175{
176 m_benchmarking = bench;
177}
178
179bool Retracer::isDoubleBuffered() const
180{
181 return m_doubleBuffered;
182}
183
184void Retracer::setDoubleBuffered(bool db)
185{
186 m_doubleBuffered = db;
187}
188
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400189bool Retracer::isSinglethread() const
190{
191 return m_singlethread;
192}
193
194void Retracer::setSinglethread(bool singlethread)
195{
196 m_singlethread = singlethread;
197}
198
Corey Richardsonf3006462014-01-26 17:15:42 -0500199bool Retracer::isCoreProfile() const
200{
201 return m_useCoreProfile;
202}
203
204void Retracer::setCoreProfile(bool coreprofile)
205{
206 m_useCoreProfile = coreprofile;
207}
208
James Bentonfc4f55a2012-08-08 17:09:07 +0100209bool Retracer::isProfilingGpu() const
210{
211 return m_profileGpu;
212}
213
214bool Retracer::isProfilingCpu() const
215{
216 return m_profileCpu;
217}
218
219bool Retracer::isProfilingPixels() const
220{
221 return m_profilePixels;
222}
223
224bool Retracer::isProfiling() const
225{
226 return m_profileGpu || m_profileCpu || m_profilePixels;
227}
228
229void Retracer::setProfiling(bool gpu, bool cpu, bool pixels)
230{
231 m_profileGpu = gpu;
232 m_profileCpu = cpu;
233 m_profilePixels = pixels;
234}
235
Zack Rusin3acde362011-04-06 01:11:55 -0400236void Retracer::setCaptureAtCallNumber(qlonglong num)
237{
238 m_captureCall = num;
239}
240
241qlonglong Retracer::captureAtCallNumber() const
242{
243 return m_captureCall;
244}
245
246bool Retracer::captureState() const
247{
248 return m_captureState;
249}
250
251void Retracer::setCaptureState(bool enable)
252{
253 m_captureState = enable;
254}
255
Dan McCabe66dfdda2012-03-05 17:20:39 -0800256bool Retracer::captureThumbnails() const
257{
258 return m_captureThumbnails;
259}
260
261void Retracer::setCaptureThumbnails(bool enable)
262{
263 m_captureThumbnails = enable;
264}
265
José Fonseca5bba4772012-03-25 12:46:04 +0100266/**
267 * Starting point for the retracing thread.
268 *
269 * Overrides QThread::run().
270 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400271void Retracer::run()
272{
José Fonseca126f64b2012-03-28 00:13:55 +0100273 QString msg = QLatin1String("Replay finished!");
Zack Rusinf389ae82011-04-10 19:27:28 -0400274
José Fonseca5bba4772012-03-25 12:46:04 +0100275 /*
276 * Construct command line
277 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400278
José Fonseca62997b42011-11-27 15:16:34 +0000279 QString prog;
Zack Rusinf389ae82011-04-10 19:27:28 -0400280 QStringList arguments;
Zack Rusin16ae0362011-04-11 21:30:04 -0400281
José Fonseca889d32c2012-04-23 10:18:28 +0100282 switch (m_api) {
283 case trace::API_GL:
José Fonseca62997b42011-11-27 15:16:34 +0000284 prog = QLatin1String("glretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100285 break;
286 case trace::API_EGL:
José Fonseca62997b42011-11-27 15:16:34 +0000287 prog = QLatin1String("eglretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100288 break;
289 case trace::API_DX:
290 case trace::API_D3D7:
291 case trace::API_D3D8:
292 case trace::API_D3D9:
José Fonsecae51e22f2012-12-07 07:48:10 +0000293 case trace::API_DXGI:
José Fonseca889d32c2012-04-23 10:18:28 +0100294#ifdef Q_OS_WIN
295 prog = QLatin1String("d3dretrace");
296#else
297 prog = QLatin1String("wine");
298 arguments << QLatin1String("d3dretrace.exe");
299#endif
300 break;
301 default:
José Fonseca67964382012-03-27 23:54:30 +0100302 emit finished(QLatin1String("Unsupported API"));
José Fonseca62997b42011-11-27 15:16:34 +0000303 return;
304 }
305
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400306 if (m_singlethread) {
307 arguments << QLatin1String("--singlethread");
308 }
309
Corey Richardsonf3006462014-01-26 17:15:42 -0500310 if (m_useCoreProfile) {
311 arguments << QLatin1String("--core");
312 }
313
José Fonseca5bba4772012-03-25 12:46:04 +0100314 if (m_captureState) {
315 arguments << QLatin1String("-D");
316 arguments << QString::number(m_captureCall);
317 } else if (m_captureThumbnails) {
318 arguments << QLatin1String("-s"); // emit snapshots
319 arguments << QLatin1String("-"); // emit to stdout
James Bentonfc4f55a2012-08-08 17:09:07 +0100320 } else if (isProfiling()) {
321 if (m_profileGpu) {
José Fonsecabce31f62012-11-14 07:21:01 +0000322 arguments << QLatin1String("--pgpu");
James Bentonfc4f55a2012-08-08 17:09:07 +0100323 }
324
325 if (m_profileCpu) {
José Fonsecabce31f62012-11-14 07:21:01 +0000326 arguments << QLatin1String("--pcpu");
James Bentonfc4f55a2012-08-08 17:09:07 +0100327 }
328
329 if (m_profilePixels) {
José Fonsecabce31f62012-11-14 07:21:01 +0000330 arguments << QLatin1String("--ppd");
James Bentonfc4f55a2012-08-08 17:09:07 +0100331 }
332 } else {
Jose Fonsecac4f9daa2015-01-25 17:09:13 +0000333 if (!m_doubleBuffered) {
José Fonsecabce31f62012-11-14 07:21:01 +0000334 arguments << QLatin1String("--sb");
James Bentonfc4f55a2012-08-08 17:09:07 +0100335 }
336
337 if (m_benchmarking) {
338 arguments << QLatin1String("-b");
339 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400340 }
341
342 arguments << m_fileName;
343
José Fonseca5bba4772012-03-25 12:46:04 +0100344 /*
Carl Worth7257dfc2012-08-09 08:21:42 -0700345 * Support remote execution on a separate target.
346 */
347
348 if (m_remoteTarget.length() != 0) {
349 arguments.prepend(prog);
350 arguments.prepend(m_remoteTarget);
351 prog = QLatin1String("ssh");
352 }
353
354 /*
José Fonseca5bba4772012-03-25 12:46:04 +0100355 * Start the process.
356 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400357
José Fonseca11ad0ea2014-11-15 09:58:44 +0000358 {
359 QDebug debug(QtDebugMsg);
360 debug << "Running:";
361 debug << prog;
362 foreach (const QString &argument, arguments) {
363 debug << argument;
364 }
365 }
366
José Fonseca5bba4772012-03-25 12:46:04 +0100367 QProcess process;
Zack Rusinf389ae82011-04-10 19:27:28 -0400368
José Fonseca6ea8dee2012-03-25 17:25:24 +0100369 process.start(prog, arguments, QIODevice::ReadOnly);
José Fonseca5bba4772012-03-25 12:46:04 +0100370 if (!process.waitForStarted(-1)) {
371 emit finished(QLatin1String("Could not start process"));
372 return;
373 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000374
José Fonseca5bba4772012-03-25 12:46:04 +0100375 /*
376 * Process standard output
377 */
378
379 QList<QImage> thumbnails;
380 QVariantMap parsedJson;
James Bentonfc4f55a2012-08-08 17:09:07 +0100381 trace::Profile* profile = NULL;
José Fonseca5bba4772012-03-25 12:46:04 +0100382
383 process.setReadChannel(QProcess::StandardOutput);
384 if (process.waitForReadyRead(-1)) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100385 BlockingIODevice io(&process);
386
José Fonseca5bba4772012-03-25 12:46:04 +0100387 if (m_captureState) {
José Fonseca95b40562012-04-05 20:06:42 +0100388 process.waitForFinished(-1);
José Fonseca3f4cd302015-01-14 12:02:06 +0000389 QByteArray data = process.readAll();
390 QJsonParseError error;
391 QJsonDocument jsonDoc =
392 QJsonDocument::fromJson(data, &error);
393
394 if (error.error != QJsonParseError::NoError) {
395 //qDebug()<<"Error is "<<error.errorString();
396 msg = error.errorString();
José Fonseca5bba4772012-03-25 12:46:04 +0100397 }
José Fonseca3f4cd302015-01-14 12:02:06 +0000398 parsedJson = jsonDoc.toVariant().toMap();
José Fonseca5bba4772012-03-25 12:46:04 +0100399 } else if (m_captureThumbnails) {
400 /*
401 * Parse concatenated PNM images from output.
402 */
Dan McCabe66dfdda2012-03-05 17:20:39 -0800403
José Fonseca6ea8dee2012-03-25 17:25:24 +0100404 while (!io.atEnd()) {
José Fonsecabeda4442013-09-12 17:25:04 +0100405 image::PNMInfo info;
José Fonseca5bba4772012-03-25 12:46:04 +0100406
407 char header[512];
408 qint64 headerSize = 0;
409 int headerLines = 3; // assume no optional comment line
410
411 for (int headerLine = 0; headerLine < headerLines; ++headerLine) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100412 qint64 headerRead = io.readLine(&header[headerSize], sizeof(header) - headerSize);
José Fonseca5bba4772012-03-25 12:46:04 +0100413
414 // if header actually contains optional comment line, ...
415 if (headerLine == 1 && header[headerSize] == '#') {
416 ++headerLines;
417 }
418
419 headerSize += headerRead;
420 }
421
José Fonsecabeda4442013-09-12 17:25:04 +0100422 const char *headerEnd = image::readPNMHeader(header, headerSize, info);
José Fonseca5bba4772012-03-25 12:46:04 +0100423
424 // if invalid PNM header was encountered, ...
José Fonsecabeda4442013-09-12 17:25:04 +0100425 if (headerEnd == NULL ||
426 info.channelType != image::TYPE_UNORM8) {
José Fonseca5bba4772012-03-25 12:46:04 +0100427 qDebug() << "error: invalid snapshot stream encountered";
428 break;
429 }
430
José Fonsecabeda4442013-09-12 17:25:04 +0100431 unsigned channels = info.channels;
432 unsigned width = info.width;
433 unsigned height = info.height;
434
José Fonseca5bba4772012-03-25 12:46:04 +0100435 // qDebug() << "channels: " << channels << ", width: " << width << ", height: " << height";
436
437 QImage snapshot = QImage(width, height, channels == 1 ? QImage::Format_Mono : QImage::Format_RGB888);
438
439 int rowBytes = channels * width;
440 for (int y = 0; y < height; ++y) {
441 unsigned char *scanLine = snapshot.scanLine(y);
José Fonseca6ea8dee2012-03-25 17:25:24 +0100442 qint64 readBytes = io.read((char *) scanLine, rowBytes);
443 Q_ASSERT(readBytes == rowBytes);
José Fonsecaa2bf2872012-11-15 13:35:22 +0000444 (void)readBytes;
José Fonseca5bba4772012-03-25 12:46:04 +0100445 }
446
José Fonsecadc9e9c62012-03-26 10:29:32 +0100447 QImage thumb = thumbnail(snapshot);
448 thumbnails.append(thumb);
Dan McCabe66dfdda2012-03-05 17:20:39 -0800449 }
José Fonseca5bba4772012-03-25 12:46:04 +0100450
451 Q_ASSERT(process.state() != QProcess::Running);
James Bentonfc4f55a2012-08-08 17:09:07 +0100452 } else if (isProfiling()) {
453 profile = new trace::Profile();
José Fonseca5bba4772012-03-25 12:46:04 +0100454
James Bentonfc4f55a2012-08-08 17:09:07 +0100455 while (!io.atEnd()) {
456 char line[256];
457 qint64 lineLength;
458
459 lineLength = io.readLine(line, 256);
460
461 if (lineLength == -1)
462 break;
463
464 trace::Profiler::parseLine(line, profile);
465 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000466 } else {
José Fonseca676cd172012-03-24 09:46:24 +0000467 QByteArray output;
José Fonseca5bba4772012-03-25 12:46:04 +0100468 output = process.readAllStandardOutput();
José Fonseca126f64b2012-03-28 00:13:55 +0100469 if (output.length() < 80) {
470 msg = QString::fromUtf8(output);
471 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000472 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400473 }
474
José Fonseca5bba4772012-03-25 12:46:04 +0100475 /*
476 * Wait for process termination
477 */
478
479 process.waitForFinished(-1);
480
481 if (process.exitStatus() != QProcess::NormalExit) {
482 msg = QLatin1String("Process crashed");
483 } else if (process.exitCode() != 0) {
484 msg = QLatin1String("Process exited with non zero exit code");
485 }
486
487 /*
488 * Parse errors.
489 */
490
Zack Rusin10fd4772011-09-14 01:45:12 -0400491 QList<ApiTraceError> errors;
José Fonseca5bba4772012-03-25 12:46:04 +0100492 process.setReadChannel(QProcess::StandardError);
José Fonseca8fdf56c2012-03-24 10:06:56 +0000493 QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$");
José Fonseca5bba4772012-03-25 12:46:04 +0100494 while (!process.atEnd()) {
495 QString line = process.readLine();
Zack Rusinb39e1c62011-04-19 23:09:26 -0400496 if (regexp.indexIn(line) != -1) {
Zack Rusin10fd4772011-09-14 01:45:12 -0400497 ApiTraceError error;
Zack Rusinb39e1c62011-04-19 23:09:26 -0400498 error.callIndex = regexp.cap(1).toInt();
499 error.type = regexp.cap(2);
500 error.message = regexp.cap(3);
501 errors.append(error);
gregoryf2329b62012-07-06 21:48:59 +0200502 } else if (!errors.isEmpty()) {
503 // Probably a multiligne message
504 ApiTraceError &previous = errors.last();
505 if (line.endsWith("\n")) {
506 line.chop(1);
507 }
508 previous.message.append('\n');
509 previous.message.append(line);
Zack Rusinb39e1c62011-04-19 23:09:26 -0400510 }
511 }
José Fonseca5bba4772012-03-25 12:46:04 +0100512
513 /*
514 * Emit signals
515 */
516
517 if (m_captureState) {
518 ApiTraceState *state = new ApiTraceState(parsedJson);
519 emit foundState(state);
José Fonseca5bba4772012-03-25 12:46:04 +0100520 }
521
522 if (m_captureThumbnails && !thumbnails.isEmpty()) {
523 emit foundThumbnails(thumbnails);
524 }
525
James Bentonfc4f55a2012-08-08 17:09:07 +0100526 if (isProfiling() && profile) {
527 emit foundProfile(profile);
528 }
529
Zack Rusinb39e1c62011-04-19 23:09:26 -0400530 if (!errors.isEmpty()) {
531 emit retraceErrors(errors);
532 }
José Fonseca5bba4772012-03-25 12:46:04 +0100533
Zack Rusinf389ae82011-04-10 19:27:28 -0400534 emit finished(msg);
Zack Rusin3acde362011-04-06 01:11:55 -0400535}
536
Zack Rusin3acde362011-04-06 01:11:55 -0400537#include "retracer.moc"