blob: 9603e28e53691e2bb0f905da8c430bd5b4aa3508 [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>
Zack Rusinf389ae82011-04-10 19:27:28 -040014
15#include <qjson/parser.h>
Zack Rusin3acde362011-04-06 01:11:55 -040016
José Fonseca6ea8dee2012-03-25 17:25:24 +010017/**
18 * Wrapper around a QProcess which enforces IO to block .
19 *
20 * Several QIODevice users (notably QJSON) expect blocking semantics, e.g.,
21 * they expect that QIODevice::read() will blocked until the requested ammount
22 * of bytes is read or end of file is reached. But by default QProcess, does
23 * not block. And passing QIODevice::Unbuffered mitigates but does not fully
24 * address the problem either.
25 *
26 * This class wraps around QProcess, providing QIODevice interface, while
27 * ensuring that all reads block.
28 *
29 * This class also works around a bug in QProcess::atEnd() implementation.
30 *
31 * See also:
32 * - http://qt-project.org/wiki/Simple_Crypt_IO_Device
33 * - http://qt-project.org/wiki/Custom_IO_Device
34 */
35class BlockingIODevice : public QIODevice
36{
37 /* We don't use the Q_OBJECT in this class given we don't declare any
38 * signals and slots or use any other services provided by Qt's meta-object
39 * system. */
40public:
41 BlockingIODevice(QProcess * io);
42 bool isSequential() const;
43 bool atEnd() const;
44 bool waitForReadyRead(int msecs = -1);
45
46protected:
47 qint64 readData(char * data, qint64 maxSize);
48 qint64 writeData(const char * data, qint64 maxSize);
49
50private:
51 QProcess *m_device;
52};
53
54BlockingIODevice::BlockingIODevice(QProcess * io) :
55 m_device(io)
56{
57 /*
58 * We pass QIODevice::Unbuffered to prevent the base QIODevice class to do
59 * its own buffering on top of the overridden readData() method.
60 *
61 * The only buffering used will be to satisfy QIODevice::peek() and
62 * QIODevice::ungetChar().
63 */
64 setOpenMode(ReadOnly | Unbuffered);
65}
66
67bool BlockingIODevice::isSequential() const
68{
69 return true;
70}
71
72bool BlockingIODevice::atEnd() const
73{
74 /*
75 * XXX: QProcess::atEnd() documentation is wrong -- it will return true
76 * even when the process is running --, so we try to workaround that here.
77 */
78 if (m_device->atEnd()) {
79 if (m_device->state() == QProcess::Running) {
80 if (!m_device->waitForReadyRead(-1)) {
81 return true;
82 }
83 }
84 }
85 return false;
86}
87
88bool BlockingIODevice::waitForReadyRead(int msecs)
89{
90 Q_UNUSED(msecs);
91 return true;
92}
93
94qint64 BlockingIODevice::readData(char * data, qint64 maxSize)
95{
96 qint64 bytesToRead = maxSize;
97 qint64 readSoFar = 0;
98 do {
99 qint64 chunkSize = m_device->read(data + readSoFar, bytesToRead);
100 if (chunkSize < 0) {
101 if (readSoFar) {
102 return readSoFar;
103 } else {
104 return chunkSize;
105 }
106 }
107 Q_ASSERT(chunkSize <= bytesToRead);
108 bytesToRead -= chunkSize;
109 readSoFar += chunkSize;
110 if (bytesToRead) {
111 if (!m_device->waitForReadyRead(-1)) {
112 qDebug() << "waitForReadyRead failed\n";
113 break;
114 }
115 }
116 } while(bytesToRead);
117
118 return readSoFar;
119}
120
121qint64 BlockingIODevice::writeData(const char * data, qint64 maxSize)
122{
123 Q_ASSERT(false);
124 return -1;
125}
126
José Fonseca5bba4772012-03-25 12:46:04 +0100127Q_DECLARE_METATYPE(QList<ApiTraceError>);
128
Zack Rusin3acde362011-04-06 01:11:55 -0400129Retracer::Retracer(QObject *parent)
Zack Rusinf389ae82011-04-10 19:27:28 -0400130 : QThread(parent),
Zack Rusin404a1ef2011-04-19 23:49:56 -0400131 m_benchmarking(false),
Zack Rusin3acde362011-04-06 01:11:55 -0400132 m_doubleBuffered(true),
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400133 m_singlethread(false),
Zack Rusin3acde362011-04-06 01:11:55 -0400134 m_captureState(false),
James Bentonfc4f55a2012-08-08 17:09:07 +0100135 m_captureCall(0),
136 m_profileGpu(false),
137 m_profileCpu(false),
138 m_profilePixels(false)
Zack Rusin3acde362011-04-06 01:11:55 -0400139{
José Fonseca5bba4772012-03-25 12:46:04 +0100140 qRegisterMetaType<QList<ApiTraceError> >();
Zack Rusin3acde362011-04-06 01:11:55 -0400141}
142
143QString Retracer::fileName() const
144{
145 return m_fileName;
146}
147
148void Retracer::setFileName(const QString &name)
149{
150 m_fileName = name;
151}
152
Carl Worth7257dfc2012-08-09 08:21:42 -0700153QString Retracer::remoteTarget() const
154{
155 return m_remoteTarget;
156}
157
158void Retracer::setRemoteTarget(const QString &host)
159{
160 m_remoteTarget = host;
161}
162
José Fonseca62997b42011-11-27 15:16:34 +0000163void Retracer::setAPI(trace::API api)
164{
165 m_api = api;
166}
167
Zack Rusin3acde362011-04-06 01:11:55 -0400168bool Retracer::isBenchmarking() const
169{
170 return m_benchmarking;
171}
172
173void Retracer::setBenchmarking(bool bench)
174{
175 m_benchmarking = bench;
176}
177
178bool Retracer::isDoubleBuffered() const
179{
180 return m_doubleBuffered;
181}
182
183void Retracer::setDoubleBuffered(bool db)
184{
185 m_doubleBuffered = db;
186}
187
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400188bool Retracer::isSinglethread() const
189{
190 return m_singlethread;
191}
192
193void Retracer::setSinglethread(bool singlethread)
194{
195 m_singlethread = singlethread;
196}
197
Corey Richardsonf3006462014-01-26 17:15:42 -0500198bool Retracer::isCoreProfile() const
199{
200 return m_useCoreProfile;
201}
202
203void Retracer::setCoreProfile(bool coreprofile)
204{
205 m_useCoreProfile = coreprofile;
206}
207
James Bentonfc4f55a2012-08-08 17:09:07 +0100208bool Retracer::isProfilingGpu() const
209{
210 return m_profileGpu;
211}
212
213bool Retracer::isProfilingCpu() const
214{
215 return m_profileCpu;
216}
217
218bool Retracer::isProfilingPixels() const
219{
220 return m_profilePixels;
221}
222
223bool Retracer::isProfiling() const
224{
225 return m_profileGpu || m_profileCpu || m_profilePixels;
226}
227
228void Retracer::setProfiling(bool gpu, bool cpu, bool pixels)
229{
230 m_profileGpu = gpu;
231 m_profileCpu = cpu;
232 m_profilePixels = pixels;
233}
234
Zack Rusin3acde362011-04-06 01:11:55 -0400235void Retracer::setCaptureAtCallNumber(qlonglong num)
236{
237 m_captureCall = num;
238}
239
240qlonglong Retracer::captureAtCallNumber() const
241{
242 return m_captureCall;
243}
244
245bool Retracer::captureState() const
246{
247 return m_captureState;
248}
249
250void Retracer::setCaptureState(bool enable)
251{
252 m_captureState = enable;
253}
254
Dan McCabe66dfdda2012-03-05 17:20:39 -0800255bool Retracer::captureThumbnails() const
256{
257 return m_captureThumbnails;
258}
259
260void Retracer::setCaptureThumbnails(bool enable)
261{
262 m_captureThumbnails = enable;
263}
264
José Fonseca5bba4772012-03-25 12:46:04 +0100265/**
266 * Starting point for the retracing thread.
267 *
268 * Overrides QThread::run().
269 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400270void Retracer::run()
271{
José Fonseca126f64b2012-03-28 00:13:55 +0100272 QString msg = QLatin1String("Replay finished!");
Zack Rusinf389ae82011-04-10 19:27:28 -0400273
José Fonseca5bba4772012-03-25 12:46:04 +0100274 /*
275 * Construct command line
276 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400277
José Fonseca62997b42011-11-27 15:16:34 +0000278 QString prog;
Zack Rusinf389ae82011-04-10 19:27:28 -0400279 QStringList arguments;
Zack Rusin16ae0362011-04-11 21:30:04 -0400280
José Fonseca889d32c2012-04-23 10:18:28 +0100281 switch (m_api) {
282 case trace::API_GL:
José Fonseca62997b42011-11-27 15:16:34 +0000283 prog = QLatin1String("glretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100284 break;
285 case trace::API_EGL:
José Fonseca62997b42011-11-27 15:16:34 +0000286 prog = QLatin1String("eglretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100287 break;
288 case trace::API_DX:
289 case trace::API_D3D7:
290 case trace::API_D3D8:
291 case trace::API_D3D9:
José Fonsecae51e22f2012-12-07 07:48:10 +0000292 case trace::API_DXGI:
José Fonseca889d32c2012-04-23 10:18:28 +0100293#ifdef Q_OS_WIN
294 prog = QLatin1String("d3dretrace");
295#else
296 prog = QLatin1String("wine");
297 arguments << QLatin1String("d3dretrace.exe");
298#endif
299 break;
300 default:
José Fonseca67964382012-03-27 23:54:30 +0100301 emit finished(QLatin1String("Unsupported API"));
José Fonseca62997b42011-11-27 15:16:34 +0000302 return;
303 }
304
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400305 if (m_singlethread) {
306 arguments << QLatin1String("--singlethread");
307 }
308
Corey Richardsonf3006462014-01-26 17:15:42 -0500309 if (m_useCoreProfile) {
310 arguments << QLatin1String("--core");
311 }
312
José Fonseca5bba4772012-03-25 12:46:04 +0100313 if (m_captureState) {
314 arguments << QLatin1String("-D");
315 arguments << QString::number(m_captureCall);
316 } else if (m_captureThumbnails) {
317 arguments << QLatin1String("-s"); // emit snapshots
318 arguments << QLatin1String("-"); // emit to stdout
James Bentonfc4f55a2012-08-08 17:09:07 +0100319 } else if (isProfiling()) {
320 if (m_profileGpu) {
José Fonsecabce31f62012-11-14 07:21:01 +0000321 arguments << QLatin1String("--pgpu");
James Bentonfc4f55a2012-08-08 17:09:07 +0100322 }
323
324 if (m_profileCpu) {
José Fonsecabce31f62012-11-14 07:21:01 +0000325 arguments << QLatin1String("--pcpu");
James Bentonfc4f55a2012-08-08 17:09:07 +0100326 }
327
328 if (m_profilePixels) {
José Fonsecabce31f62012-11-14 07:21:01 +0000329 arguments << QLatin1String("--ppd");
James Bentonfc4f55a2012-08-08 17:09:07 +0100330 }
331 } else {
332 if (m_doubleBuffered) {
José Fonsecabce31f62012-11-14 07:21:01 +0000333 arguments << QLatin1String("--db");
James Bentonfc4f55a2012-08-08 17:09:07 +0100334 } else {
José Fonsecabce31f62012-11-14 07:21:01 +0000335 arguments << QLatin1String("--sb");
James Bentonfc4f55a2012-08-08 17:09:07 +0100336 }
337
338 if (m_benchmarking) {
339 arguments << QLatin1String("-b");
340 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400341 }
342
343 arguments << m_fileName;
344
José Fonseca5bba4772012-03-25 12:46:04 +0100345 /*
Carl Worth7257dfc2012-08-09 08:21:42 -0700346 * Support remote execution on a separate target.
347 */
348
349 if (m_remoteTarget.length() != 0) {
350 arguments.prepend(prog);
351 arguments.prepend(m_remoteTarget);
352 prog = QLatin1String("ssh");
353 }
354
355 /*
José Fonseca5bba4772012-03-25 12:46:04 +0100356 * Start the process.
357 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400358
José Fonseca5bba4772012-03-25 12:46:04 +0100359 QProcess process;
Zack Rusinf389ae82011-04-10 19:27:28 -0400360
José Fonseca6ea8dee2012-03-25 17:25:24 +0100361 process.start(prog, arguments, QIODevice::ReadOnly);
José Fonseca5bba4772012-03-25 12:46:04 +0100362 if (!process.waitForStarted(-1)) {
363 emit finished(QLatin1String("Could not start process"));
364 return;
365 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000366
José Fonseca5bba4772012-03-25 12:46:04 +0100367 /*
368 * Process standard output
369 */
370
371 QList<QImage> thumbnails;
372 QVariantMap parsedJson;
James Bentonfc4f55a2012-08-08 17:09:07 +0100373 trace::Profile* profile = NULL;
José Fonseca5bba4772012-03-25 12:46:04 +0100374
375 process.setReadChannel(QProcess::StandardOutput);
376 if (process.waitForReadyRead(-1)) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100377 BlockingIODevice io(&process);
378
José Fonseca5bba4772012-03-25 12:46:04 +0100379 if (m_captureState) {
380 /*
381 * Parse JSON from the output.
382 *
José Fonseca5bba4772012-03-25 12:46:04 +0100383 * XXX: QJSON's scanner is inneficient as it abuses single
384 * character QIODevice::peek (not cheap), instead of maintaining a
385 * lookahead character on its own.
386 */
387
José Fonseca5bba4772012-03-25 12:46:04 +0100388 bool ok = false;
389 QJson::Parser jsonParser;
José Fonseca74936bb2012-10-27 10:13:58 +0100390
391 // Allow Nan/Infinity
392 jsonParser.allowSpecialNumbers(true);
José Fonseca95b40562012-04-05 20:06:42 +0100393#if 0
José Fonseca6ea8dee2012-03-25 17:25:24 +0100394 parsedJson = jsonParser.parse(&io, &ok).toMap();
José Fonseca95b40562012-04-05 20:06:42 +0100395#else
396 /*
397 * XXX: QJSON expects blocking IO, and it looks like
398 * BlockingIODevice does not work reliably in all cases.
399 */
400 process.waitForFinished(-1);
401 parsedJson = jsonParser.parse(&process, &ok).toMap();
402#endif
José Fonseca5bba4772012-03-25 12:46:04 +0100403 if (!ok) {
404 msg = QLatin1String("failed to parse JSON");
405 }
406 } else if (m_captureThumbnails) {
407 /*
408 * Parse concatenated PNM images from output.
409 */
Dan McCabe66dfdda2012-03-05 17:20:39 -0800410
José Fonseca6ea8dee2012-03-25 17:25:24 +0100411 while (!io.atEnd()) {
José Fonsecabeda4442013-09-12 17:25:04 +0100412 image::PNMInfo info;
José Fonseca5bba4772012-03-25 12:46:04 +0100413
414 char header[512];
415 qint64 headerSize = 0;
416 int headerLines = 3; // assume no optional comment line
417
418 for (int headerLine = 0; headerLine < headerLines; ++headerLine) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100419 qint64 headerRead = io.readLine(&header[headerSize], sizeof(header) - headerSize);
José Fonseca5bba4772012-03-25 12:46:04 +0100420
421 // if header actually contains optional comment line, ...
422 if (headerLine == 1 && header[headerSize] == '#') {
423 ++headerLines;
424 }
425
426 headerSize += headerRead;
427 }
428
José Fonsecabeda4442013-09-12 17:25:04 +0100429 const char *headerEnd = image::readPNMHeader(header, headerSize, info);
José Fonseca5bba4772012-03-25 12:46:04 +0100430
431 // if invalid PNM header was encountered, ...
José Fonsecabeda4442013-09-12 17:25:04 +0100432 if (headerEnd == NULL ||
433 info.channelType != image::TYPE_UNORM8) {
José Fonseca5bba4772012-03-25 12:46:04 +0100434 qDebug() << "error: invalid snapshot stream encountered";
435 break;
436 }
437
José Fonsecabeda4442013-09-12 17:25:04 +0100438 unsigned channels = info.channels;
439 unsigned width = info.width;
440 unsigned height = info.height;
441
José Fonseca5bba4772012-03-25 12:46:04 +0100442 // qDebug() << "channels: " << channels << ", width: " << width << ", height: " << height";
443
444 QImage snapshot = QImage(width, height, channels == 1 ? QImage::Format_Mono : QImage::Format_RGB888);
445
446 int rowBytes = channels * width;
447 for (int y = 0; y < height; ++y) {
448 unsigned char *scanLine = snapshot.scanLine(y);
José Fonseca6ea8dee2012-03-25 17:25:24 +0100449 qint64 readBytes = io.read((char *) scanLine, rowBytes);
450 Q_ASSERT(readBytes == rowBytes);
José Fonsecaa2bf2872012-11-15 13:35:22 +0000451 (void)readBytes;
José Fonseca5bba4772012-03-25 12:46:04 +0100452 }
453
José Fonsecadc9e9c62012-03-26 10:29:32 +0100454 QImage thumb = thumbnail(snapshot);
455 thumbnails.append(thumb);
Dan McCabe66dfdda2012-03-05 17:20:39 -0800456 }
José Fonseca5bba4772012-03-25 12:46:04 +0100457
458 Q_ASSERT(process.state() != QProcess::Running);
James Bentonfc4f55a2012-08-08 17:09:07 +0100459 } else if (isProfiling()) {
460 profile = new trace::Profile();
José Fonseca5bba4772012-03-25 12:46:04 +0100461
James Bentonfc4f55a2012-08-08 17:09:07 +0100462 while (!io.atEnd()) {
463 char line[256];
464 qint64 lineLength;
465
466 lineLength = io.readLine(line, 256);
467
468 if (lineLength == -1)
469 break;
470
471 trace::Profiler::parseLine(line, profile);
472 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000473 } else {
José Fonseca676cd172012-03-24 09:46:24 +0000474 QByteArray output;
José Fonseca5bba4772012-03-25 12:46:04 +0100475 output = process.readAllStandardOutput();
José Fonseca126f64b2012-03-28 00:13:55 +0100476 if (output.length() < 80) {
477 msg = QString::fromUtf8(output);
478 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000479 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400480 }
481
José Fonseca5bba4772012-03-25 12:46:04 +0100482 /*
483 * Wait for process termination
484 */
485
486 process.waitForFinished(-1);
487
488 if (process.exitStatus() != QProcess::NormalExit) {
489 msg = QLatin1String("Process crashed");
490 } else if (process.exitCode() != 0) {
491 msg = QLatin1String("Process exited with non zero exit code");
492 }
493
494 /*
495 * Parse errors.
496 */
497
Zack Rusin10fd4772011-09-14 01:45:12 -0400498 QList<ApiTraceError> errors;
José Fonseca5bba4772012-03-25 12:46:04 +0100499 process.setReadChannel(QProcess::StandardError);
José Fonseca8fdf56c2012-03-24 10:06:56 +0000500 QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$");
José Fonseca5bba4772012-03-25 12:46:04 +0100501 while (!process.atEnd()) {
502 QString line = process.readLine();
Zack Rusinb39e1c62011-04-19 23:09:26 -0400503 if (regexp.indexIn(line) != -1) {
Zack Rusin10fd4772011-09-14 01:45:12 -0400504 ApiTraceError error;
Zack Rusinb39e1c62011-04-19 23:09:26 -0400505 error.callIndex = regexp.cap(1).toInt();
506 error.type = regexp.cap(2);
507 error.message = regexp.cap(3);
508 errors.append(error);
gregoryf2329b62012-07-06 21:48:59 +0200509 } else if (!errors.isEmpty()) {
510 // Probably a multiligne message
511 ApiTraceError &previous = errors.last();
512 if (line.endsWith("\n")) {
513 line.chop(1);
514 }
515 previous.message.append('\n');
516 previous.message.append(line);
Zack Rusinb39e1c62011-04-19 23:09:26 -0400517 }
518 }
José Fonseca5bba4772012-03-25 12:46:04 +0100519
520 /*
521 * Emit signals
522 */
523
524 if (m_captureState) {
525 ApiTraceState *state = new ApiTraceState(parsedJson);
526 emit foundState(state);
José Fonseca5bba4772012-03-25 12:46:04 +0100527 }
528
529 if (m_captureThumbnails && !thumbnails.isEmpty()) {
530 emit foundThumbnails(thumbnails);
531 }
532
James Bentonfc4f55a2012-08-08 17:09:07 +0100533 if (isProfiling() && profile) {
534 emit foundProfile(profile);
535 }
536
Zack Rusinb39e1c62011-04-19 23:09:26 -0400537 if (!errors.isEmpty()) {
538 emit retraceErrors(errors);
539 }
José Fonseca5bba4772012-03-25 12:46:04 +0100540
Zack Rusinf389ae82011-04-10 19:27:28 -0400541 emit finished(msg);
Zack Rusin3acde362011-04-06 01:11:55 -0400542}
543
Zack Rusin3acde362011-04-06 01:11:55 -0400544#include "retracer.moc"