blob: 73b7a50f3d6c4ee06e237c1f8ca493f5a3c8a27d [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
Jose Fonsecabceafec2016-05-05 11:09:52 +01006#include "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>
Jose Fonsecac296a3e2015-05-01 17:43:54 +010014
15#include "qubjson.h"
16
Zack Rusin3acde362011-04-06 01:11:55 -040017
José Fonseca6ea8dee2012-03-25 17:25:24 +010018/**
19 * Wrapper around a QProcess which enforces IO to block .
20 *
21 * Several QIODevice users (notably QJSON) expect blocking semantics, e.g.,
22 * they expect that QIODevice::read() will blocked until the requested ammount
23 * of bytes is read or end of file is reached. But by default QProcess, does
24 * not block. And passing QIODevice::Unbuffered mitigates but does not fully
25 * address the problem either.
26 *
27 * This class wraps around QProcess, providing QIODevice interface, while
28 * ensuring that all reads block.
29 *
30 * This class also works around a bug in QProcess::atEnd() implementation.
31 *
32 * See also:
33 * - http://qt-project.org/wiki/Simple_Crypt_IO_Device
34 * - http://qt-project.org/wiki/Custom_IO_Device
35 */
36class BlockingIODevice : public QIODevice
37{
38 /* We don't use the Q_OBJECT in this class given we don't declare any
39 * signals and slots or use any other services provided by Qt's meta-object
40 * system. */
41public:
42 BlockingIODevice(QProcess * io);
Jose Fonseca010f9962016-03-05 14:45:41 +000043 bool isSequential() const override;
44 bool atEnd() const override;
45 bool waitForReadyRead(int msecs = -1) override;
José Fonseca6ea8dee2012-03-25 17:25:24 +010046
47protected:
Jose Fonseca010f9962016-03-05 14:45:41 +000048 qint64 readData(char * data, qint64 maxSize) override;
49 qint64 writeData(const char * data, qint64 maxSize) override;
José Fonseca6ea8dee2012-03-25 17:25:24 +010050
51private:
52 QProcess *m_device;
53};
54
55BlockingIODevice::BlockingIODevice(QProcess * io) :
56 m_device(io)
57{
58 /*
59 * We pass QIODevice::Unbuffered to prevent the base QIODevice class to do
60 * its own buffering on top of the overridden readData() method.
61 *
62 * The only buffering used will be to satisfy QIODevice::peek() and
63 * QIODevice::ungetChar().
64 */
65 setOpenMode(ReadOnly | Unbuffered);
66}
67
68bool BlockingIODevice::isSequential() const
69{
70 return true;
71}
72
73bool BlockingIODevice::atEnd() const
74{
75 /*
76 * XXX: QProcess::atEnd() documentation is wrong -- it will return true
77 * even when the process is running --, so we try to workaround that here.
78 */
79 if (m_device->atEnd()) {
80 if (m_device->state() == QProcess::Running) {
81 if (!m_device->waitForReadyRead(-1)) {
82 return true;
83 }
84 }
85 }
86 return false;
87}
88
89bool BlockingIODevice::waitForReadyRead(int msecs)
90{
91 Q_UNUSED(msecs);
92 return true;
93}
94
95qint64 BlockingIODevice::readData(char * data, qint64 maxSize)
96{
97 qint64 bytesToRead = maxSize;
98 qint64 readSoFar = 0;
99 do {
100 qint64 chunkSize = m_device->read(data + readSoFar, bytesToRead);
101 if (chunkSize < 0) {
102 if (readSoFar) {
103 return readSoFar;
104 } else {
105 return chunkSize;
106 }
107 }
108 Q_ASSERT(chunkSize <= bytesToRead);
109 bytesToRead -= chunkSize;
110 readSoFar += chunkSize;
111 if (bytesToRead) {
112 if (!m_device->waitForReadyRead(-1)) {
113 qDebug() << "waitForReadyRead failed\n";
114 break;
115 }
116 }
117 } while(bytesToRead);
118
119 return readSoFar;
120}
121
122qint64 BlockingIODevice::writeData(const char * data, qint64 maxSize)
123{
124 Q_ASSERT(false);
125 return -1;
126}
127
José Fonseca5bba4772012-03-25 12:46:04 +0100128Q_DECLARE_METATYPE(QList<ApiTraceError>);
129
Zack Rusin3acde362011-04-06 01:11:55 -0400130Retracer::Retracer(QObject *parent)
Zack Rusinf389ae82011-04-10 19:27:28 -0400131 : QThread(parent),
Zack Rusin404a1ef2011-04-19 23:49:56 -0400132 m_benchmarking(false),
Zack Rusin3acde362011-04-06 01:11:55 -0400133 m_doubleBuffered(true),
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400134 m_singlethread(false),
José Fonsecac03e4b02014-05-30 17:24:42 +0100135 m_useCoreProfile(false),
Zack Rusin3acde362011-04-06 01:11:55 -0400136 m_captureState(false),
José Fonsecac03e4b02014-05-30 17:24:42 +0100137 m_captureThumbnails(false),
James Bentonfc4f55a2012-08-08 17:09:07 +0100138 m_captureCall(0),
139 m_profileGpu(false),
140 m_profileCpu(false),
Jose Fonseca65d26052017-07-30 13:48:40 +0100141 m_profilePixels(false)
Zack Rusin3acde362011-04-06 01:11:55 -0400142{
José Fonseca5bba4772012-03-25 12:46:04 +0100143 qRegisterMetaType<QList<ApiTraceError> >();
Zack Rusin3acde362011-04-06 01:11:55 -0400144}
145
146QString Retracer::fileName() const
147{
148 return m_fileName;
149}
150
151void Retracer::setFileName(const QString &name)
152{
153 m_fileName = name;
154}
155
Carl Worth7257dfc2012-08-09 08:21:42 -0700156QString Retracer::remoteTarget() const
157{
158 return m_remoteTarget;
159}
160
161void Retracer::setRemoteTarget(const QString &host)
162{
163 m_remoteTarget = host;
164}
165
José Fonseca62997b42011-11-27 15:16:34 +0000166void Retracer::setAPI(trace::API api)
167{
168 m_api = api;
169}
170
Zack Rusin3acde362011-04-06 01:11:55 -0400171bool Retracer::isBenchmarking() const
172{
173 return m_benchmarking;
174}
175
176void Retracer::setBenchmarking(bool bench)
177{
178 m_benchmarking = bench;
179}
180
181bool Retracer::isDoubleBuffered() const
182{
183 return m_doubleBuffered;
184}
185
186void Retracer::setDoubleBuffered(bool db)
187{
188 m_doubleBuffered = db;
189}
190
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400191bool Retracer::isSinglethread() const
192{
193 return m_singlethread;
194}
195
196void Retracer::setSinglethread(bool singlethread)
197{
198 m_singlethread = singlethread;
199}
200
Corey Richardsonf3006462014-01-26 17:15:42 -0500201bool Retracer::isCoreProfile() const
202{
203 return m_useCoreProfile;
204}
205
206void Retracer::setCoreProfile(bool coreprofile)
207{
208 m_useCoreProfile = coreprofile;
209}
210
James Bentonfc4f55a2012-08-08 17:09:07 +0100211bool Retracer::isProfilingGpu() const
212{
213 return m_profileGpu;
214}
215
216bool Retracer::isProfilingCpu() const
217{
218 return m_profileCpu;
219}
220
221bool Retracer::isProfilingPixels() const
222{
223 return m_profilePixels;
224}
225
BogDan Vatraa9f9e642015-02-10 14:31:17 +0200226bool Retracer::isProfiling() const
227{
Jose Fonseca65d26052017-07-30 13:48:40 +0100228 return m_profileGpu || m_profileCpu || m_profilePixels;
BogDan Vatraa9f9e642015-02-10 14:31:17 +0200229}
230
Jose Fonseca65d26052017-07-30 13:48:40 +0100231void Retracer::setProfiling(bool gpu, bool cpu, bool pixels)
James Bentonfc4f55a2012-08-08 17:09:07 +0100232{
233 m_profileGpu = gpu;
234 m_profileCpu = cpu;
235 m_profilePixels = pixels;
236}
237
Zack Rusin3acde362011-04-06 01:11:55 -0400238void Retracer::setCaptureAtCallNumber(qlonglong num)
239{
240 m_captureCall = num;
241}
242
243qlonglong Retracer::captureAtCallNumber() const
244{
245 return m_captureCall;
246}
247
248bool Retracer::captureState() const
249{
250 return m_captureState;
251}
252
253void Retracer::setCaptureState(bool enable)
254{
255 m_captureState = enable;
256}
257
Dan McCabe66dfdda2012-03-05 17:20:39 -0800258bool Retracer::captureThumbnails() const
259{
260 return m_captureThumbnails;
261}
262
263void Retracer::setCaptureThumbnails(bool enable)
264{
265 m_captureThumbnails = enable;
266}
267
Dan McCabe88938852012-06-01 13:40:04 -0700268void Retracer::addThumbnailToCapture(qlonglong num)
269{
270 if (!m_thumbnailsToCapture.contains(num)) {
271 m_thumbnailsToCapture.append(num);
272 }
273}
274
275void Retracer::resetThumbnailsToCapture()
276{
277 m_thumbnailsToCapture.clear();
278}
279
Jose Fonseca65d26052017-07-30 13:48:40 +0100280QString Retracer::thumbnailCallSet()
Dan McCabeb14bda22012-06-01 13:40:06 -0700281{
282 QString callSet;
283
284 bool isFirst = true;
285
286 foreach (qlonglong callIndex, m_thumbnailsToCapture) {
287 // TODO: detect contiguous ranges
288 if (!isFirst) {
289 callSet.append(QLatin1String(","));
290 } else {
291 isFirst = false;
292 }
293
294 //emit "callIndex"
295 callSet.append(QString::number(callIndex));
296 }
297
298 //qDebug() << QLatin1String("debug: call set to capture: ") << callSet;
299 return callSet;
300}
Dan McCabe88938852012-06-01 13:40:04 -0700301
José Fonseca5bba4772012-03-25 12:46:04 +0100302/**
303 * Starting point for the retracing thread.
304 *
305 * Overrides QThread::run().
306 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400307void Retracer::run()
308{
José Fonseca126f64b2012-03-28 00:13:55 +0100309 QString msg = QLatin1String("Replay finished!");
Zack Rusinf389ae82011-04-10 19:27:28 -0400310
José Fonseca5bba4772012-03-25 12:46:04 +0100311 /*
312 * Construct command line
313 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400314
José Fonseca62997b42011-11-27 15:16:34 +0000315 QString prog;
Zack Rusinf389ae82011-04-10 19:27:28 -0400316 QStringList arguments;
Zack Rusin16ae0362011-04-11 21:30:04 -0400317
José Fonseca889d32c2012-04-23 10:18:28 +0100318 switch (m_api) {
319 case trace::API_GL:
José Fonseca62997b42011-11-27 15:16:34 +0000320 prog = QLatin1String("glretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100321 break;
322 case trace::API_EGL:
José Fonseca62997b42011-11-27 15:16:34 +0000323 prog = QLatin1String("eglretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100324 break;
325 case trace::API_DX:
326 case trace::API_D3D7:
327 case trace::API_D3D8:
328 case trace::API_D3D9:
José Fonsecae51e22f2012-12-07 07:48:10 +0000329 case trace::API_DXGI:
José Fonseca889d32c2012-04-23 10:18:28 +0100330#ifdef Q_OS_WIN
331 prog = QLatin1String("d3dretrace");
332#else
333 prog = QLatin1String("wine");
334 arguments << QLatin1String("d3dretrace.exe");
335#endif
336 break;
337 default:
José Fonseca67964382012-03-27 23:54:30 +0100338 emit finished(QLatin1String("Unsupported API"));
José Fonseca62997b42011-11-27 15:16:34 +0000339 return;
340 }
341
Jose Fonseca65d26052017-07-30 13:48:40 +0100342 if (m_singlethread) {
343 arguments << QLatin1String("--singlethread");
344 }
345
346 if (m_useCoreProfile) {
347 arguments << QLatin1String("--core");
348 }
349
350 if (m_captureState) {
351 arguments << QLatin1String("-D");
352 arguments << QString::number(m_captureCall);
353 arguments << QLatin1String("--dump-format");
354 arguments << QLatin1String("ubjson");
355 } else if (m_captureThumbnails) {
356 if (!m_thumbnailsToCapture.isEmpty()) {
357 arguments << QLatin1String("-S");
358 arguments << thumbnailCallSet();
359 }
360 arguments << QLatin1String("-s"); // emit snapshots
361 arguments << QLatin1String("-"); // emit to stdout
362 } else if (isProfiling()) {
363 if (m_profileGpu) {
364 arguments << QLatin1String("--pgpu");
365 }
366
367 if (m_profileCpu) {
368 arguments << QLatin1String("--pcpu");
369 }
370
371 if (m_profilePixels) {
372 arguments << QLatin1String("--ppd");
373 }
374 } else {
375 if (!m_doubleBuffered) {
376 arguments << QLatin1String("--sb");
377 }
378
379 if (m_benchmarking) {
380 arguments << QLatin1String("-b");
381 }
382 }
383
384 arguments << m_fileName;
Zack Rusinf389ae82011-04-10 19:27:28 -0400385
José Fonseca5bba4772012-03-25 12:46:04 +0100386 /*
Carl Worth7257dfc2012-08-09 08:21:42 -0700387 * Support remote execution on a separate target.
388 */
389
390 if (m_remoteTarget.length() != 0) {
391 arguments.prepend(prog);
392 arguments.prepend(m_remoteTarget);
393 prog = QLatin1String("ssh");
394 }
395
396 /*
José Fonseca5bba4772012-03-25 12:46:04 +0100397 * Start the process.
398 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400399
José Fonseca11ad0ea2014-11-15 09:58:44 +0000400 {
401 QDebug debug(QtDebugMsg);
402 debug << "Running:";
403 debug << prog;
404 foreach (const QString &argument, arguments) {
405 debug << argument;
406 }
407 }
408
José Fonseca5bba4772012-03-25 12:46:04 +0100409 QProcess process;
Zack Rusinf389ae82011-04-10 19:27:28 -0400410
José Fonseca6ea8dee2012-03-25 17:25:24 +0100411 process.start(prog, arguments, QIODevice::ReadOnly);
José Fonseca5bba4772012-03-25 12:46:04 +0100412 if (!process.waitForStarted(-1)) {
413 emit finished(QLatin1String("Could not start process"));
414 return;
415 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000416
José Fonseca5bba4772012-03-25 12:46:04 +0100417 /*
418 * Process standard output
419 */
420
Dan McCabec6f924e2012-06-01 13:40:05 -0700421 ImageHash thumbnails;
José Fonseca5bba4772012-03-25 12:46:04 +0100422 QVariantMap parsedJson;
James Bentonfc4f55a2012-08-08 17:09:07 +0100423 trace::Profile* profile = NULL;
José Fonseca5bba4772012-03-25 12:46:04 +0100424
425 process.setReadChannel(QProcess::StandardOutput);
426 if (process.waitForReadyRead(-1)) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100427 BlockingIODevice io(&process);
428
José Fonseca5bba4772012-03-25 12:46:04 +0100429 if (m_captureState) {
Jose Fonseca08fdb7b2015-05-21 21:41:50 +0100430 parsedJson = decodeUBJSONObject(&io).toMap();
José Fonseca95b40562012-04-05 20:06:42 +0100431 process.waitForFinished(-1);
José Fonseca5bba4772012-03-25 12:46:04 +0100432 } else if (m_captureThumbnails) {
433 /*
434 * Parse concatenated PNM images from output.
435 */
Dan McCabe66dfdda2012-03-05 17:20:39 -0800436
José Fonseca6ea8dee2012-03-25 17:25:24 +0100437 while (!io.atEnd()) {
José Fonsecabeda4442013-09-12 17:25:04 +0100438 image::PNMInfo info;
José Fonseca5bba4772012-03-25 12:46:04 +0100439
440 char header[512];
441 qint64 headerSize = 0;
442 int headerLines = 3; // assume no optional comment line
443
444 for (int headerLine = 0; headerLine < headerLines; ++headerLine) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100445 qint64 headerRead = io.readLine(&header[headerSize], sizeof(header) - headerSize);
José Fonseca5bba4772012-03-25 12:46:04 +0100446
447 // if header actually contains optional comment line, ...
448 if (headerLine == 1 && header[headerSize] == '#') {
449 ++headerLines;
450 }
451
452 headerSize += headerRead;
453 }
454
José Fonsecabeda4442013-09-12 17:25:04 +0100455 const char *headerEnd = image::readPNMHeader(header, headerSize, info);
José Fonseca5bba4772012-03-25 12:46:04 +0100456
457 // if invalid PNM header was encountered, ...
José Fonsecabeda4442013-09-12 17:25:04 +0100458 if (headerEnd == NULL ||
459 info.channelType != image::TYPE_UNORM8) {
José Fonseca5bba4772012-03-25 12:46:04 +0100460 qDebug() << "error: invalid snapshot stream encountered";
461 break;
462 }
463
José Fonsecabeda4442013-09-12 17:25:04 +0100464 unsigned channels = info.channels;
465 unsigned width = info.width;
466 unsigned height = info.height;
467
José Fonseca5bba4772012-03-25 12:46:04 +0100468 // qDebug() << "channels: " << channels << ", width: " << width << ", height: " << height";
469
470 QImage snapshot = QImage(width, height, channels == 1 ? QImage::Format_Mono : QImage::Format_RGB888);
471
472 int rowBytes = channels * width;
473 for (int y = 0; y < height; ++y) {
474 unsigned char *scanLine = snapshot.scanLine(y);
José Fonseca6ea8dee2012-03-25 17:25:24 +0100475 qint64 readBytes = io.read((char *) scanLine, rowBytes);
476 Q_ASSERT(readBytes == rowBytes);
José Fonsecaa2bf2872012-11-15 13:35:22 +0000477 (void)readBytes;
José Fonseca5bba4772012-03-25 12:46:04 +0100478 }
479
José Fonsecadc9e9c62012-03-26 10:29:32 +0100480 QImage thumb = thumbnail(snapshot);
Dan McCabec6f924e2012-06-01 13:40:05 -0700481 thumbnails.insert(info.commentNumber, thumb);
Dan McCabe66dfdda2012-03-05 17:20:39 -0800482 }
José Fonseca5bba4772012-03-25 12:46:04 +0100483
484 Q_ASSERT(process.state() != QProcess::Running);
James Bentonfc4f55a2012-08-08 17:09:07 +0100485 } else if (isProfiling()) {
486 profile = new trace::Profile();
José Fonseca5bba4772012-03-25 12:46:04 +0100487
James Bentonfc4f55a2012-08-08 17:09:07 +0100488 while (!io.atEnd()) {
489 char line[256];
490 qint64 lineLength;
491
492 lineLength = io.readLine(line, 256);
493
494 if (lineLength == -1)
495 break;
496
497 trace::Profiler::parseLine(line, profile);
498 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000499 } else {
José Fonseca676cd172012-03-24 09:46:24 +0000500 QByteArray output;
José Fonseca5bba4772012-03-25 12:46:04 +0100501 output = process.readAllStandardOutput();
José Fonseca126f64b2012-03-28 00:13:55 +0100502 if (output.length() < 80) {
503 msg = QString::fromUtf8(output);
504 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000505 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400506 }
507
José Fonseca5bba4772012-03-25 12:46:04 +0100508 /*
509 * Wait for process termination
510 */
511
512 process.waitForFinished(-1);
513
514 if (process.exitStatus() != QProcess::NormalExit) {
515 msg = QLatin1String("Process crashed");
516 } else if (process.exitCode() != 0) {
517 msg = QLatin1String("Process exited with non zero exit code");
518 }
519
520 /*
521 * Parse errors.
522 */
523
Zack Rusin10fd4772011-09-14 01:45:12 -0400524 QList<ApiTraceError> errors;
José Fonseca5bba4772012-03-25 12:46:04 +0100525 process.setReadChannel(QProcess::StandardError);
José Fonseca8fdf56c2012-03-24 10:06:56 +0000526 QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$");
José Fonseca5bba4772012-03-25 12:46:04 +0100527 while (!process.atEnd()) {
528 QString line = process.readLine();
Zack Rusinb39e1c62011-04-19 23:09:26 -0400529 if (regexp.indexIn(line) != -1) {
Zack Rusin10fd4772011-09-14 01:45:12 -0400530 ApiTraceError error;
Zack Rusinb39e1c62011-04-19 23:09:26 -0400531 error.callIndex = regexp.cap(1).toInt();
532 error.type = regexp.cap(2);
533 error.message = regexp.cap(3);
534 errors.append(error);
gregoryf2329b62012-07-06 21:48:59 +0200535 } else if (!errors.isEmpty()) {
536 // Probably a multiligne message
537 ApiTraceError &previous = errors.last();
538 if (line.endsWith("\n")) {
539 line.chop(1);
540 }
541 previous.message.append('\n');
542 previous.message.append(line);
Zack Rusinb39e1c62011-04-19 23:09:26 -0400543 }
544 }
José Fonseca5bba4772012-03-25 12:46:04 +0100545
546 /*
547 * Emit signals
548 */
549
550 if (m_captureState) {
551 ApiTraceState *state = new ApiTraceState(parsedJson);
552 emit foundState(state);
José Fonseca5bba4772012-03-25 12:46:04 +0100553 }
554
555 if (m_captureThumbnails && !thumbnails.isEmpty()) {
556 emit foundThumbnails(thumbnails);
557 }
558
James Bentonfc4f55a2012-08-08 17:09:07 +0100559 if (isProfiling() && profile) {
560 emit foundProfile(profile);
561 }
562
Zack Rusinb39e1c62011-04-19 23:09:26 -0400563 if (!errors.isEmpty()) {
564 emit retraceErrors(errors);
565 }
José Fonseca5bba4772012-03-25 12:46:04 +0100566
Zack Rusinf389ae82011-04-10 19:27:28 -0400567 emit finished(msg);
Zack Rusin3acde362011-04-06 01:11:55 -0400568}
569
Zack Rusin3acde362011-04-06 01:11:55 -0400570#include "retracer.moc"