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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "timelinenotesmodel.h"
#include "timelinetracemanager.h"
#include "timelinetracefile.h"
#include <utils/qtcassert.h>
#include <utils/temporaryfile.h>
#include <utils/runextensions.h>
#include <QFile>
#include <QDataStream>
#include <memory>
namespace Timeline {
TraceEventTypeStorage::~TraceEventTypeStorage()
{
}
TraceEventStorage::~TraceEventStorage()
{
}
class TimelineTraceManager::TimelineTraceManagerPrivate
{
public:
std::unique_ptr<TraceEventTypeStorage> typeStorage;
std::unique_ptr<TraceEventStorage> eventStorage;
TimelineNotesModel *notesModel = nullptr;
int numEvents = 0;
quint64 availableFeatures = 0;
quint64 visibleFeatures = 0;
quint64 recordedFeatures = 0;
bool aggregateTraces = false;
QHash<quint8, QVector<TraceEventLoader>> eventLoaders;
QVector<Initializer> initializers;
QVector<Finalizer> finalizers;
QVector<Clearer> clearers;
qint64 traceStart = -1;
qint64 traceEnd = -1;
void dispatch(const TraceEvent &event, const TraceEventType &type);
void reset();
void updateTraceTime(qint64 time);
};
TimelineTraceManager::TimelineTraceManager(std::unique_ptr<TraceEventStorage> &&eventStorage,
std::unique_ptr<TraceEventTypeStorage> &&typeStorage,
QObject *parent) :
QObject(parent), d(new TimelineTraceManagerPrivate)
{
d->eventStorage = std::move(eventStorage);
d->typeStorage = std::move(typeStorage);
}
TimelineTraceManager::~TimelineTraceManager()
{
delete d;
}
TimelineNotesModel *TimelineTraceManager::notesModel() const
{
return d->notesModel;
}
bool TimelineTraceManager::isEmpty() const
{
return d->eventStorage->size() == 0;
}
int TimelineTraceManager::numEvents() const
{
return d->numEvents;
}
int TimelineTraceManager::numEventTypes() const
{
return d->typeStorage->size();
}
void TimelineTraceManager::swapEventStorage(std::unique_ptr<TraceEventStorage> &other)
{
d->eventStorage.swap(other);
}
const TraceEventStorage *TimelineTraceManager::eventStorage() const
{
return d->eventStorage.get();
}
const TraceEventTypeStorage *TimelineTraceManager::typeStorage() const
{
return d->typeStorage.get();
}
void TimelineTraceManager::appendEvent(TraceEvent &&event)
{
d->dispatch(event, d->typeStorage->get(event.typeIndex()));
d->eventStorage->append(std::move(event));
}
const TraceEventType &TimelineTraceManager::eventType(int typeId) const
{
return d->typeStorage->get(typeId);
}
void TimelineTraceManager::setEventType(int typeId, TraceEventType &&type)
{
d->recordedFeatures |= (1ull << type.feature());
d->typeStorage->set(typeId, std::move(type));
}
int TimelineTraceManager::appendEventType(TraceEventType &&type)
{
d->recordedFeatures |= (1ull << type.feature());
return d->typeStorage->append(std::move(type));
}
void TimelineTraceManager::registerFeatures(quint64 features, TraceEventLoader eventLoader,
Initializer initializer, Finalizer finalizer,
Clearer clearer)
{
if ((features & d->availableFeatures) != features) {
d->availableFeatures |= features;
emit availableFeaturesChanged(d->availableFeatures);
}
if ((features & d->visibleFeatures) != features) {
d->visibleFeatures |= features;
emit visibleFeaturesChanged(d->visibleFeatures);
}
for (quint8 feature = 0; feature != sizeof(features) * 8; ++feature) {
if (features & (1ULL << feature)) {
if (eventLoader)
d->eventLoaders[feature].append(eventLoader);
}
}
if (initializer)
d->initializers.append(initializer);
if (finalizer)
d->finalizers.append(finalizer);
if (clearer)
d->clearers.append(clearer);
}
quint64 TimelineTraceManager::availableFeatures() const
{
return d->availableFeatures;
}
quint64 TimelineTraceManager::visibleFeatures() const
{
return d->visibleFeatures;
}
void TimelineTraceManager::setVisibleFeatures(quint64 features)
{
if (d->visibleFeatures != features) {
d->visibleFeatures = features;
emit visibleFeaturesChanged(d->visibleFeatures);
}
}
quint64 TimelineTraceManager::recordedFeatures() const
{
return d->recordedFeatures;
}
void TimelineTraceManager::setRecordedFeatures(quint64 features)
{
if (d->recordedFeatures != features) {
d->recordedFeatures = features;
emit recordedFeaturesChanged(d->recordedFeatures);
}
}
bool TimelineTraceManager::aggregateTraces() const
{
return d->aggregateTraces;
}
void TimelineTraceManager::setAggregateTraces(bool aggregateTraces)
{
d->aggregateTraces = aggregateTraces;
}
void TimelineTraceManager::initialize()
{
for (const Initializer &initializer : qAsConst(d->initializers))
initializer();
}
void TimelineTraceManager::finalize()
{
d->eventStorage->finalize();
// Load notes after the timeline models have been initialized ...
// which happens on stateChanged(Done).
for (const Finalizer &finalizer : qAsConst(d->finalizers))
finalizer();
}
QFuture<void> TimelineTraceManager::save(const QString &filename)
{
TimelineTraceFile *writer = createTraceFile();
writer->setTraceTime(traceStart(), traceEnd(), traceDuration());
writer->setTraceManager(this);
writer->setNotes(d->notesModel);
connect(writer, &QObject::destroyed, this, &TimelineTraceManager::saveFinished);
connect(writer, &TimelineTraceFile::error, this, &TimelineTraceManager::error);
return Utils::runAsync([filename, writer] (QFutureInterface<void> &future) {
writer->setFuture(future);
QFile file(filename);
if (file.open(QIODevice::WriteOnly))
writer->save(&file);
else
writer->fail(tr("Could not open %1 for writing.").arg(filename));
if (future.isCanceled())
file.remove();
writer->deleteLater();
});
}
QFuture<void> TimelineTraceManager::load(const QString &filename)
{
clearAll();
initialize();
TimelineTraceFile *reader = createTraceFile();
reader->setTraceManager(this);
reader->setNotes(d->notesModel);
connect(reader, &QObject::destroyed, this, &TimelineTraceManager::loadFinished);
connect(reader, &TimelineTraceFile::error, this, &TimelineTraceManager::error);
QFuture<void> future = Utils::runAsync([filename, reader] (QFutureInterface<void> &future) {
reader->setFuture(future);
QFile file(filename);
if (file.open(QIODevice::ReadOnly))
reader->load(&file);
else
reader->fail(tr("Could not open %1 for reading.").arg(filename));
reader->deleteLater();
});
QFutureWatcher<void> *watcher = new QFutureWatcher<void>(reader);
connect(watcher, &QFutureWatcherBase::canceled, this, &TimelineTraceManager::clearAll);
connect(watcher, &QFutureWatcherBase::finished, this, [this, reader]() {
if (!reader->isCanceled()) {
if (reader->traceStart() >= 0)
decreaseTraceStart(reader->traceStart());
if (reader->traceEnd() >= 0)
increaseTraceEnd(reader->traceEnd());
finalize();
}
});
watcher->setFuture(future);
return future;
}
qint64 TimelineTraceManager::traceStart() const
{
return d->traceStart;
}
qint64 TimelineTraceManager::traceEnd() const
{
return d->traceEnd;
}
qint64 TimelineTraceManager::traceDuration() const
{
return traceEnd() - traceStart();
}
void TimelineTraceManager::decreaseTraceStart(qint64 start)
{
QTC_ASSERT(start >= 0, return);
if (d->traceStart > start || d->traceStart == -1) {
d->traceStart = start;
if (d->traceEnd == -1)
d->traceEnd = d->traceStart;
else
QTC_ASSERT(d->traceEnd >= d->traceStart, d->traceEnd = d->traceStart);
}
}
void TimelineTraceManager::increaseTraceEnd(qint64 end)
{
QTC_ASSERT(end >= 0, return);
if (d->traceEnd < end || d->traceEnd == -1) {
d->traceEnd = end;
if (d->traceStart == -1)
d->traceStart = d->traceEnd;
else
QTC_ASSERT(d->traceEnd >= d->traceStart, d->traceStart = d->traceEnd);
}
}
void TimelineTraceManager::TimelineTraceManagerPrivate::updateTraceTime(qint64 time)
{
QTC_ASSERT(time >= 0, return);
if (traceStart > time || traceStart == -1)
traceStart = time;
if (traceEnd < time || traceEnd == -1)
traceEnd = time;
QTC_ASSERT(traceEnd >= traceStart, traceStart = traceEnd);
}
void TimelineTraceManager::setNotesModel(TimelineNotesModel *notesModel)
{
d->notesModel = notesModel;
}
void TimelineTraceManager::clearEventStorage()
{
d->reset();
if (d->notesModel)
d->notesModel->clear();
setRecordedFeatures(0);
d->eventStorage->clear();
}
void TimelineTraceManager::clearTypeStorage()
{
d->typeStorage->clear();
d->recordedFeatures = 0;
}
void TimelineTraceManager::clear()
{
clearEventStorage();
}
void TimelineTraceManager::clearAll()
{
clearEventStorage();
clearTypeStorage();
}
void TimelineTraceManager::restrictByFilter(TraceEventFilter filter)
{
if (d->notesModel)
d->notesModel->stash();
d->reset();
QFutureInterface<void> future;
replayEvents(filter(std::bind(&TimelineTraceManagerPrivate::dispatch, d,
std::placeholders::_1, std::placeholders::_2)),
[this]() {
initialize();
}, [this]() {
if (d->notesModel)
d->notesModel->restore();
finalize();
}, [this](const QString &message) {
if (!message.isEmpty()) {
emit error(tr("Could not re-read events from temporary trace file: %1\n"
"The trace data is lost.").arg(message));
}
clearAll();
}, future);
}
void TimelineTraceManager::TimelineTraceManagerPrivate::dispatch(const TraceEvent &event,
const TraceEventType &type)
{
for (const TraceEventLoader &loader : eventLoaders[type.feature()])
loader(event, type);
if (event.timestamp() >= 0)
updateTraceTime(event.timestamp());
++numEvents;
}
void TimelineTraceManager::TimelineTraceManagerPrivate::reset()
{
traceStart = -1;
traceEnd = -1;
for (const Clearer &clearer : qAsConst(clearers))
clearer();
numEvents = 0;
}
} // namespace Timeline
|