1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qqmlprofileradapter.h" |
41 | #include "qqmlprofilerservice.h" |
42 | |
43 | #include <private/qqmldebugserviceinterfaces_p.h> |
44 | |
45 | QT_BEGIN_NAMESPACE |
46 | |
47 | QQmlProfilerAdapter::QQmlProfilerAdapter(QQmlProfilerService *service, QQmlEnginePrivate *engine) |
48 | { |
49 | engine->profiler = new QQmlProfiler; |
50 | init(service, profiler: engine->profiler); |
51 | } |
52 | |
53 | QQmlProfilerAdapter::QQmlProfilerAdapter(QQmlProfilerService *service, QQmlTypeLoader *loader) |
54 | { |
55 | QQmlProfiler *profiler = new QQmlProfiler; |
56 | loader->setProfiler(profiler); |
57 | init(service, profiler); |
58 | } |
59 | |
60 | void QQmlProfilerAdapter::init(QQmlProfilerService *service, QQmlProfiler *profiler) |
61 | { |
62 | next = 0; |
63 | setService(service); |
64 | connect(sender: this, signal: &QQmlProfilerAdapter::profilingEnabled, |
65 | receiver: profiler, slot: &QQmlProfiler::startProfiling); |
66 | connect(sender: this, signal: &QQmlAbstractProfilerAdapter::profilingEnabledWhileWaiting, |
67 | receiver: profiler, slot: &QQmlProfiler::startProfiling, type: Qt::DirectConnection); |
68 | connect(sender: this, signal: &QQmlAbstractProfilerAdapter::profilingDisabled, |
69 | receiver: profiler, slot: &QQmlProfiler::stopProfiling); |
70 | connect(sender: this, signal: &QQmlAbstractProfilerAdapter::profilingDisabledWhileWaiting, |
71 | receiver: profiler, slot: &QQmlProfiler::stopProfiling, type: Qt::DirectConnection); |
72 | connect(sender: this, signal: &QQmlAbstractProfilerAdapter::dataRequested, |
73 | receiver: profiler, slot: &QQmlProfiler::reportData); |
74 | connect(sender: this, signal: &QQmlAbstractProfilerAdapter::referenceTimeKnown, |
75 | receiver: profiler, slot: &QQmlProfiler::setTimer); |
76 | connect(sender: profiler, signal: &QQmlProfiler::dataReady, |
77 | receiver: this, slot: &QQmlProfilerAdapter::receiveData); |
78 | } |
79 | |
80 | // convert to QByteArrays that can be sent to the debug client |
81 | static void qQmlProfilerDataToByteArrays(const QQmlProfilerData &d, |
82 | QQmlProfiler::LocationHash &locations, |
83 | QList<QByteArray> &messages) |
84 | { |
85 | QQmlDebugPacket ds; |
86 | Q_ASSERT_X((d.messageType & (1 << 31)) == 0, Q_FUNC_INFO, |
87 | "You can use at most 31 message types." ); |
88 | for (quint32 decodedMessageType = 0; (d.messageType >> decodedMessageType) != 0; |
89 | ++decodedMessageType) { |
90 | if (decodedMessageType == QQmlProfilerDefinitions::RangeData |
91 | || (d.messageType & (1 << decodedMessageType)) == 0) { |
92 | continue; // RangeData is sent together with RangeLocation |
93 | } |
94 | |
95 | if (decodedMessageType == QQmlProfilerDefinitions::RangeEnd |
96 | || decodedMessageType == QQmlProfilerDefinitions::RangeStart) { |
97 | ds << d.time << decodedMessageType << static_cast<quint32>(d.detailType); |
98 | if (d.locationId != 0) |
99 | ds << static_cast<qint64>(d.locationId); |
100 | } else { |
101 | auto i = locations.find(akey: d.locationId); |
102 | if (i != locations.end()) { |
103 | ds << d.time << decodedMessageType << static_cast<quint32>(d.detailType); |
104 | ds << (i->url.isEmpty() ? i->location.sourceFile : i->url.toString()) |
105 | << static_cast<qint32>(i->location.line) |
106 | << static_cast<qint32>(i->location.column); |
107 | if (d.messageType & (1 << QQmlProfilerDefinitions::RangeData)) { |
108 | // Send both, location and data ... |
109 | ds << static_cast<qint64>(d.locationId); |
110 | messages.append(t: ds.squeezedData()); |
111 | ds.clear(); |
112 | ds << d.time << int(QQmlProfilerDefinitions::RangeData) |
113 | << static_cast<quint32>(d.detailType) |
114 | << (i->location.sourceFile.isEmpty() ? i->url.toString() : |
115 | i->location.sourceFile); |
116 | } |
117 | ds << static_cast<qint64>(d.locationId); |
118 | locations.erase(it: i); // ... so that we can erase here without missing anything. |
119 | } else { |
120 | // Skip RangeData and RangeLocation: We've already sent them |
121 | continue; |
122 | } |
123 | } |
124 | messages.append(t: ds.squeezedData()); |
125 | ds.clear(); |
126 | } |
127 | } |
128 | |
129 | qint64 QQmlProfilerAdapter::sendMessages(qint64 until, QList<QByteArray> &messages) |
130 | { |
131 | while (next != data.length()) { |
132 | const QQmlProfilerData &nextData = data.at(i: next); |
133 | if (nextData.time > until || messages.length() > s_numMessagesPerBatch) |
134 | return nextData.time; |
135 | qQmlProfilerDataToByteArrays(d: nextData, locations, messages); |
136 | ++next; |
137 | } |
138 | |
139 | next = 0; |
140 | data.clear(); |
141 | locations.clear(); |
142 | return -1; |
143 | } |
144 | |
145 | void QQmlProfilerAdapter::receiveData(const QVector<QQmlProfilerData> &new_data, |
146 | const QQmlProfiler::LocationHash &new_locations) |
147 | { |
148 | if (data.isEmpty()) |
149 | data = new_data; |
150 | else |
151 | data.append(l: new_data); |
152 | |
153 | if (locations.isEmpty()) |
154 | locations = new_locations; |
155 | else |
156 | locations.insert(hash: new_locations); |
157 | |
158 | service->dataReady(profiler: this); |
159 | } |
160 | |
161 | QT_END_NAMESPACE |
162 | |