blob: 4d117afb38c95c3c3a1baae26bacb5257f6b27a5 [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
Dan McCabe88938852012-06-01 13:40:04 -0700266void Retracer::addThumbnailToCapture(qlonglong num)
267{
268 if (!m_thumbnailsToCapture.contains(num)) {
269 m_thumbnailsToCapture.append(num);
270 }
271}
272
273void Retracer::resetThumbnailsToCapture()
274{
275 m_thumbnailsToCapture.clear();
276}
277
Dan McCabeb14bda22012-06-01 13:40:06 -0700278QString Retracer::thumbnailCallSet()
279{
280 QString callSet;
281
282 bool isFirst = true;
283
284 foreach (qlonglong callIndex, m_thumbnailsToCapture) {
285 // TODO: detect contiguous ranges
286 if (!isFirst) {
287 callSet.append(QLatin1String(","));
288 } else {
289 isFirst = false;
290 }
291
292 //emit "callIndex"
293 callSet.append(QString::number(callIndex));
294 }
295
296 //qDebug() << QLatin1String("debug: call set to capture: ") << callSet;
297 return callSet;
298}
Dan McCabe88938852012-06-01 13:40:04 -0700299
José Fonseca5bba4772012-03-25 12:46:04 +0100300/**
301 * Starting point for the retracing thread.
302 *
303 * Overrides QThread::run().
304 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400305void Retracer::run()
306{
José Fonseca126f64b2012-03-28 00:13:55 +0100307 QString msg = QLatin1String("Replay finished!");
Zack Rusinf389ae82011-04-10 19:27:28 -0400308
José Fonseca5bba4772012-03-25 12:46:04 +0100309 /*
310 * Construct command line
311 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400312
José Fonseca62997b42011-11-27 15:16:34 +0000313 QString prog;
Zack Rusinf389ae82011-04-10 19:27:28 -0400314 QStringList arguments;
Zack Rusin16ae0362011-04-11 21:30:04 -0400315
José Fonseca889d32c2012-04-23 10:18:28 +0100316 switch (m_api) {
317 case trace::API_GL:
José Fonseca62997b42011-11-27 15:16:34 +0000318 prog = QLatin1String("glretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100319 break;
320 case trace::API_EGL:
José Fonseca62997b42011-11-27 15:16:34 +0000321 prog = QLatin1String("eglretrace");
José Fonseca889d32c2012-04-23 10:18:28 +0100322 break;
323 case trace::API_DX:
324 case trace::API_D3D7:
325 case trace::API_D3D8:
326 case trace::API_D3D9:
José Fonsecae51e22f2012-12-07 07:48:10 +0000327 case trace::API_DXGI:
José Fonseca889d32c2012-04-23 10:18:28 +0100328#ifdef Q_OS_WIN
329 prog = QLatin1String("d3dretrace");
330#else
331 prog = QLatin1String("wine");
332 arguments << QLatin1String("d3dretrace.exe");
333#endif
334 break;
335 default:
José Fonseca67964382012-03-27 23:54:30 +0100336 emit finished(QLatin1String("Unsupported API"));
José Fonseca62997b42011-11-27 15:16:34 +0000337 return;
338 }
339
Peter Lohrmannb34c6752013-07-10 11:08:14 -0400340 if (m_singlethread) {
341 arguments << QLatin1String("--singlethread");
342 }
343
Corey Richardsonf3006462014-01-26 17:15:42 -0500344 if (m_useCoreProfile) {
345 arguments << QLatin1String("--core");
346 }
347
José Fonseca5bba4772012-03-25 12:46:04 +0100348 if (m_captureState) {
349 arguments << QLatin1String("-D");
350 arguments << QString::number(m_captureCall);
351 } else if (m_captureThumbnails) {
Dan McCabeb14bda22012-06-01 13:40:06 -0700352 if (!m_thumbnailsToCapture.isEmpty()) {
353 arguments << QLatin1String("-S");
354 arguments << thumbnailCallSet();
355 }
José Fonseca5bba4772012-03-25 12:46:04 +0100356 arguments << QLatin1String("-s"); // emit snapshots
357 arguments << QLatin1String("-"); // emit to stdout
James Bentonfc4f55a2012-08-08 17:09:07 +0100358 } else if (isProfiling()) {
359 if (m_profileGpu) {
José Fonsecabce31f62012-11-14 07:21:01 +0000360 arguments << QLatin1String("--pgpu");
James Bentonfc4f55a2012-08-08 17:09:07 +0100361 }
362
363 if (m_profileCpu) {
José Fonsecabce31f62012-11-14 07:21:01 +0000364 arguments << QLatin1String("--pcpu");
James Bentonfc4f55a2012-08-08 17:09:07 +0100365 }
366
367 if (m_profilePixels) {
José Fonsecabce31f62012-11-14 07:21:01 +0000368 arguments << QLatin1String("--ppd");
James Bentonfc4f55a2012-08-08 17:09:07 +0100369 }
370 } else {
Jose Fonsecac4f9daa2015-01-25 17:09:13 +0000371 if (!m_doubleBuffered) {
José Fonsecabce31f62012-11-14 07:21:01 +0000372 arguments << QLatin1String("--sb");
James Bentonfc4f55a2012-08-08 17:09:07 +0100373 }
374
375 if (m_benchmarking) {
376 arguments << QLatin1String("-b");
377 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400378 }
379
380 arguments << m_fileName;
381
José Fonseca5bba4772012-03-25 12:46:04 +0100382 /*
Carl Worth7257dfc2012-08-09 08:21:42 -0700383 * Support remote execution on a separate target.
384 */
385
386 if (m_remoteTarget.length() != 0) {
387 arguments.prepend(prog);
388 arguments.prepend(m_remoteTarget);
389 prog = QLatin1String("ssh");
390 }
391
392 /*
José Fonseca5bba4772012-03-25 12:46:04 +0100393 * Start the process.
394 */
Zack Rusinf389ae82011-04-10 19:27:28 -0400395
José Fonseca11ad0ea2014-11-15 09:58:44 +0000396 {
397 QDebug debug(QtDebugMsg);
398 debug << "Running:";
399 debug << prog;
400 foreach (const QString &argument, arguments) {
401 debug << argument;
402 }
403 }
404
José Fonseca5bba4772012-03-25 12:46:04 +0100405 QProcess process;
Zack Rusinf389ae82011-04-10 19:27:28 -0400406
José Fonseca6ea8dee2012-03-25 17:25:24 +0100407 process.start(prog, arguments, QIODevice::ReadOnly);
José Fonseca5bba4772012-03-25 12:46:04 +0100408 if (!process.waitForStarted(-1)) {
409 emit finished(QLatin1String("Could not start process"));
410 return;
411 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000412
José Fonseca5bba4772012-03-25 12:46:04 +0100413 /*
414 * Process standard output
415 */
416
Dan McCabec6f924e2012-06-01 13:40:05 -0700417 ImageHash thumbnails;
José Fonseca5bba4772012-03-25 12:46:04 +0100418 QVariantMap parsedJson;
James Bentonfc4f55a2012-08-08 17:09:07 +0100419 trace::Profile* profile = NULL;
José Fonseca5bba4772012-03-25 12:46:04 +0100420
421 process.setReadChannel(QProcess::StandardOutput);
422 if (process.waitForReadyRead(-1)) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100423 BlockingIODevice io(&process);
424
José Fonseca5bba4772012-03-25 12:46:04 +0100425 if (m_captureState) {
José Fonseca95b40562012-04-05 20:06:42 +0100426 process.waitForFinished(-1);
José Fonseca3f4cd302015-01-14 12:02:06 +0000427 QByteArray data = process.readAll();
428 QJsonParseError error;
429 QJsonDocument jsonDoc =
430 QJsonDocument::fromJson(data, &error);
431
432 if (error.error != QJsonParseError::NoError) {
433 //qDebug()<<"Error is "<<error.errorString();
434 msg = error.errorString();
José Fonseca5bba4772012-03-25 12:46:04 +0100435 }
José Fonseca3f4cd302015-01-14 12:02:06 +0000436 parsedJson = jsonDoc.toVariant().toMap();
José Fonseca5bba4772012-03-25 12:46:04 +0100437 } else if (m_captureThumbnails) {
438 /*
439 * Parse concatenated PNM images from output.
440 */
Dan McCabe66dfdda2012-03-05 17:20:39 -0800441
José Fonseca6ea8dee2012-03-25 17:25:24 +0100442 while (!io.atEnd()) {
José Fonsecabeda4442013-09-12 17:25:04 +0100443 image::PNMInfo info;
José Fonseca5bba4772012-03-25 12:46:04 +0100444
445 char header[512];
446 qint64 headerSize = 0;
447 int headerLines = 3; // assume no optional comment line
448
449 for (int headerLine = 0; headerLine < headerLines; ++headerLine) {
José Fonseca6ea8dee2012-03-25 17:25:24 +0100450 qint64 headerRead = io.readLine(&header[headerSize], sizeof(header) - headerSize);
José Fonseca5bba4772012-03-25 12:46:04 +0100451
452 // if header actually contains optional comment line, ...
453 if (headerLine == 1 && header[headerSize] == '#') {
454 ++headerLines;
455 }
456
457 headerSize += headerRead;
458 }
459
José Fonsecabeda4442013-09-12 17:25:04 +0100460 const char *headerEnd = image::readPNMHeader(header, headerSize, info);
José Fonseca5bba4772012-03-25 12:46:04 +0100461
462 // if invalid PNM header was encountered, ...
José Fonsecabeda4442013-09-12 17:25:04 +0100463 if (headerEnd == NULL ||
464 info.channelType != image::TYPE_UNORM8) {
José Fonseca5bba4772012-03-25 12:46:04 +0100465 qDebug() << "error: invalid snapshot stream encountered";
466 break;
467 }
468
José Fonsecabeda4442013-09-12 17:25:04 +0100469 unsigned channels = info.channels;
470 unsigned width = info.width;
471 unsigned height = info.height;
472
José Fonseca5bba4772012-03-25 12:46:04 +0100473 // qDebug() << "channels: " << channels << ", width: " << width << ", height: " << height";
474
475 QImage snapshot = QImage(width, height, channels == 1 ? QImage::Format_Mono : QImage::Format_RGB888);
476
477 int rowBytes = channels * width;
478 for (int y = 0; y < height; ++y) {
479 unsigned char *scanLine = snapshot.scanLine(y);
José Fonseca6ea8dee2012-03-25 17:25:24 +0100480 qint64 readBytes = io.read((char *) scanLine, rowBytes);
481 Q_ASSERT(readBytes == rowBytes);
José Fonsecaa2bf2872012-11-15 13:35:22 +0000482 (void)readBytes;
José Fonseca5bba4772012-03-25 12:46:04 +0100483 }
484
José Fonsecadc9e9c62012-03-26 10:29:32 +0100485 QImage thumb = thumbnail(snapshot);
Dan McCabec6f924e2012-06-01 13:40:05 -0700486 thumbnails.insert(info.commentNumber, thumb);
Dan McCabe66dfdda2012-03-05 17:20:39 -0800487 }
José Fonseca5bba4772012-03-25 12:46:04 +0100488
489 Q_ASSERT(process.state() != QProcess::Running);
James Bentonfc4f55a2012-08-08 17:09:07 +0100490 } else if (isProfiling()) {
491 profile = new trace::Profile();
José Fonseca5bba4772012-03-25 12:46:04 +0100492
James Bentonfc4f55a2012-08-08 17:09:07 +0100493 while (!io.atEnd()) {
494 char line[256];
495 qint64 lineLength;
496
497 lineLength = io.readLine(line, 256);
498
499 if (lineLength == -1)
500 break;
501
502 trace::Profiler::parseLine(line, profile);
503 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000504 } else {
José Fonseca676cd172012-03-24 09:46:24 +0000505 QByteArray output;
José Fonseca5bba4772012-03-25 12:46:04 +0100506 output = process.readAllStandardOutput();
José Fonseca126f64b2012-03-28 00:13:55 +0100507 if (output.length() < 80) {
508 msg = QString::fromUtf8(output);
509 }
José Fonseca56cd8ac2011-11-24 16:30:49 +0000510 }
Zack Rusinf389ae82011-04-10 19:27:28 -0400511 }
512
José Fonseca5bba4772012-03-25 12:46:04 +0100513 /*
514 * Wait for process termination
515 */
516
517 process.waitForFinished(-1);
518
519 if (process.exitStatus() != QProcess::NormalExit) {
520 msg = QLatin1String("Process crashed");
521 } else if (process.exitCode() != 0) {
522 msg = QLatin1String("Process exited with non zero exit code");
523 }
524
525 /*
526 * Parse errors.
527 */
528
Zack Rusin10fd4772011-09-14 01:45:12 -0400529 QList<ApiTraceError> errors;
José Fonseca5bba4772012-03-25 12:46:04 +0100530 process.setReadChannel(QProcess::StandardError);
José Fonseca8fdf56c2012-03-24 10:06:56 +0000531 QRegExp regexp("(^\\d+): +(\\b\\w+\\b): ([^\\r\\n]+)[\\r\\n]*$");
José Fonseca5bba4772012-03-25 12:46:04 +0100532 while (!process.atEnd()) {
533 QString line = process.readLine();
Zack Rusinb39e1c62011-04-19 23:09:26 -0400534 if (regexp.indexIn(line) != -1) {
Zack Rusin10fd4772011-09-14 01:45:12 -0400535 ApiTraceError error;
Zack Rusinb39e1c62011-04-19 23:09:26 -0400536 error.callIndex = regexp.cap(1).toInt();
537 error.type = regexp.cap(2);
538 error.message = regexp.cap(3);
539 errors.append(error);
gregoryf2329b62012-07-06 21:48:59 +0200540 } else if (!errors.isEmpty()) {
541 // Probably a multiligne message
542 ApiTraceError &previous = errors.last();
543 if (line.endsWith("\n")) {
544 line.chop(1);
545 }
546 previous.message.append('\n');
547 previous.message.append(line);
Zack Rusinb39e1c62011-04-19 23:09:26 -0400548 }
549 }
José Fonseca5bba4772012-03-25 12:46:04 +0100550
551 /*
552 * Emit signals
553 */
554
555 if (m_captureState) {
556 ApiTraceState *state = new ApiTraceState(parsedJson);
557 emit foundState(state);
José Fonseca5bba4772012-03-25 12:46:04 +0100558 }
559
560 if (m_captureThumbnails && !thumbnails.isEmpty()) {
561 emit foundThumbnails(thumbnails);
562 }
563
James Bentonfc4f55a2012-08-08 17:09:07 +0100564 if (isProfiling() && profile) {
565 emit foundProfile(profile);
566 }
567
Zack Rusinb39e1c62011-04-19 23:09:26 -0400568 if (!errors.isEmpty()) {
569 emit retraceErrors(errors);
570 }
José Fonseca5bba4772012-03-25 12:46:04 +0100571
Zack Rusinf389ae82011-04-10 19:27:28 -0400572 emit finished(msg);
Zack Rusin3acde362011-04-06 01:11:55 -0400573}
574
Zack Rusin3acde362011-04-06 01:11:55 -0400575#include "retracer.moc"