1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtGui>
#include <QtDebug>
#include <private/qpaintengineex_p.h>
#include <private/qpaintbuffer_p.h>
class ReplayWidget : public QWidget
{
Q_OBJECT
public:
ReplayWidget(const QString &filename, int from, int to, bool single, int frame);
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *event);
public slots:
void updateRect();
public:
QList<QRegion> updates;
QPaintBuffer buffer;
int currentFrame;
int currentIteration;
QTime timer;
QList<uint> visibleUpdates;
QVector<uint> iterationTimes;
QString filename;
int from;
int to;
bool single;
int frame;
int currentCommand;
};
void ReplayWidget::updateRect()
{
if (frame >= 0 && !updates.isEmpty())
update(updates.at(frame));
else if (!visibleUpdates.isEmpty())
update(updates.at(visibleUpdates.at(currentFrame)));
}
const int singleFrameRepeatsPerCommand = 100;
const int singleFrameIterations = 4;
void ReplayWidget::paintEvent(QPaintEvent *)
{
QPainter p(this);
QTimer::singleShot(0, this, SLOT(updateRect()));
// p.setClipRegion(frames.at(currentFrame).updateRegion);
if (frame >= 0) {
int start = buffer.frameStartIndex(frame);
int end = buffer.frameEndIndex(frame);
iterationTimes.resize(end - start);
int saveRestoreStackDepth = buffer.processCommands(&p, start, start + currentCommand);
for (int i = 0; i < saveRestoreStackDepth; ++i)
p.restore();
const int repeats = currentIteration >= 3 ? singleFrameRepeatsPerCommand : 1;
++currentFrame;
if (currentFrame == repeats) {
currentFrame = 0;
if (currentIteration >= 3) {
iterationTimes[currentCommand - 1] = qMin(iterationTimes[currentCommand - 1], uint(timer.elapsed()));
timer.restart();
}
if (currentIteration >= singleFrameIterations + 3) {
printf(" # | ms | description\n");
printf("------+---------+------------------------------------------------------------\n");
qSort(iterationTimes);
int sum = 0;
for (int i = 0; i < iterationTimes.size(); ++i) {
int delta = iterationTimes.at(i);
if (i > 0)
delta -= iterationTimes.at(i-1);
sum += delta;
qreal deltaF = delta / qreal(repeats);
printf("%.5d | %.5f | %s\n", i, deltaF, qPrintable(buffer.commandDescription(start + i)));
}
printf("Total | %.5f | Total frame time\n", sum / qreal(repeats));
deleteLater();
return;
}
if (start + currentCommand >= end) {
currentCommand = 1;
++currentIteration;
if (currentIteration == 3) {
timer.start();
iterationTimes.fill(uint(-1));
}
if (currentIteration >= 3 && currentIteration < singleFrameIterations + 3)
printf("Profiling iteration %d of %d\n", currentIteration - 2, singleFrameIterations);
} else {
++currentCommand;
}
}
return;
}
buffer.draw(&p, visibleUpdates.at(currentFrame));
++currentFrame;
if (currentFrame >= visibleUpdates.size()) {
currentFrame = 0;
++currentIteration;
if (single) {
deleteLater();
return;
}
if (currentIteration == 3)
timer.start();
else if (currentIteration > 3) {
iterationTimes << timer.elapsed();
timer.restart();
if (iterationTimes.size() >= 3) {
qreal mean = 0;
qreal stddev = 0;
uint min = INT_MAX;
for (int i = 0; i < iterationTimes.size(); ++i) {
mean += iterationTimes.at(i);
min = qMin(min, iterationTimes.at(i));
}
mean /= qreal(iterationTimes.size());
for (int i = 0; i < iterationTimes.size(); ++i) {
qreal delta = iterationTimes.at(i) - mean;
stddev += delta * delta;
}
stddev = qSqrt(stddev / iterationTimes.size());
qSort(iterationTimes.begin(), iterationTimes.end());
uint median = iterationTimes.at(iterationTimes.size() / 2);
stddev = 100 * stddev / mean;
if (iterationTimes.size() >= 10 || stddev < 4) {
printf("%s, iterations: %d, frames: %d, min(ms): %d, median(ms): %d, stddev: %f %%, max(fps): %f\n", qPrintable(filename),
iterationTimes.size(), visibleUpdates.size(), min, median, stddev, 1000. * visibleUpdates.size() / min);
deleteLater();
return;
}
}
}
}
}
void ReplayWidget::resizeEvent(QResizeEvent *)
{
visibleUpdates.clear();
QRect bounds = rect();
int first = qMax(0, from);
int last = qMin(unsigned(to), unsigned(updates.size()));
for (int i = first; i < last; ++i) {
if (updates.at(i).intersects(bounds))
visibleUpdates << i;
}
int range = last - first;
if (visibleUpdates.size() != range)
printf("Warning: skipped %d frames due to limited resolution\n", range - visibleUpdates.size());
}
ReplayWidget::ReplayWidget(const QString &filename_, int from_, int to_, bool single_, int frame_)
: currentFrame(0)
, currentIteration(0)
, filename(filename_)
, from(from_)
, to(to_)
, single(single_)
, frame(frame_)
, currentCommand(1)
{
setWindowTitle(filename);
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
printf("Failed to load input file '%s'\n", qPrintable(filename_));
return;
}
QDataStream in(&file);
char *data;
uint size;
in.readBytes(data, size);
bool isTraceFile = size >= 7 && qstrncmp(data, "qttrace", 7) == 0;
uint version = 0;
if (size == 9 && qstrncmp(data, "qttraceV2", 9) == 0) {
in.setFloatingPointPrecision(QDataStream::SinglePrecision);
in >> version;
}
if (!isTraceFile) {
printf("File '%s' is not a trace file\n", qPrintable(filename_));
return;
}
in >> buffer >> updates;
printf("Read paint buffer version %d with %d frames\n", version, buffer.numFrames());
resize(buffer.boundingRect().size().toSize());
setAutoFillBackground(false);
setAttribute(Qt::WA_NoSystemBackground);
QTimer::singleShot(10, this, SLOT(updateRect()));
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
if (argc <= 1 || qstrcmp(argv[1], "-h") == 0 || qstrcmp(argv[1], "--help") == 0) {
printf("Replays a tracefile generated with '-graphicssystem trace'\n");
printf("Usage:\n > %s [OPTIONS] [traceFile]\n", argv[0]);
printf("OPTIONS\n"
" --range=from-to to specify a frame range.\n"
" --singlerun to do only one run (without statistics)\n"
" --instrumentframe=frame to instrument a single frame\n");
return 1;
}
QFile file(app.arguments().last());
if (!file.exists()) {
printf("%s does not exist\n", qPrintable(app.arguments().last()));
return 1;
}
bool single = false;
int frame = -1;
int from = 0;
int to = -1;
for (int i = 1; i < app.arguments().size() - 1; ++i) {
QString arg = app.arguments().at(i);
if (arg.startsWith(QLatin1String("--range="))) {
QString rest = arg.mid(8);
QStringList components = rest.split(QLatin1Char('-'));
bool ok1 = false;
bool ok2 = false;
int fromCandidate = 0;
int toCandidate = 0;
if (components.size() == 2) {
fromCandidate = components.first().toInt(&ok1);
toCandidate = components.last().toInt(&ok2);
}
if (ok1 && ok2) {
from = fromCandidate;
to = toCandidate;
} else {
printf("ERROR: malformed syntax in argument %s\n", qPrintable(arg));
}
} else if (arg == QLatin1String("--singlerun")) {
single = true;
} else if (arg.startsWith(QLatin1String("--instrumentframe="))) {
QString rest = arg.mid(18);
bool ok = false;
int frameCandidate = rest.toInt(&ok);
if (ok) {
frame = frameCandidate;
} else {
printf("ERROR: malformed syntax in argument %s\n", qPrintable(arg));
}
} else {
printf("Unrecognized argument: %s\n", qPrintable(arg));
return 1;
}
}
ReplayWidget *widget = new ReplayWidget(app.arguments().last(), from, to, single, frame);
if (!widget->updates.isEmpty()) {
widget->show();
return app.exec();
}
}
#include "main.moc"
|