Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qv4profiling_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#ifndef QV4PROFILING_H
6#define QV4PROFILING_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtQml/private/qv4global_p.h>
20#include "qv4engine_p.h"
21#include "qv4function_p.h"
22
23#include <QElapsedTimer>
24
25#if !QT_CONFIG(qml_debug)
26
27#define Q_V4_PROFILE_ALLOC(engine, size, type) Q_UNUSED(engine)
28#define Q_V4_PROFILE_DEALLOC(engine, size, type) Q_UNUSED(engine)
29
31
32namespace QV4 {
33namespace Profiling {
34class Profiler {};
39}
40}
41
43
44#else
45
46#define Q_V4_PROFILE_ALLOC(engine, size, type)
47 (engine->profiler() &&
48 (engine->profiler()->featuresEnabled & (1 << Profiling::FeatureMemoryAllocation)) ?
49 engine->profiler()->trackAlloc(size, type) : false)
50
51#define Q_V4_PROFILE_DEALLOC(engine, size, type)
52 (engine->profiler() &&
53 (engine->profiler()->featuresEnabled & (1 << Profiling::FeatureMemoryAllocation)) ?
54 engine->profiler()->trackDealloc(size, type) : false)
55
57
58namespace QV4 {
59
60namespace Profiling {
61
62enum Features {
63 FeatureFunctionCall,
64 FeatureMemoryAllocation
65};
66
67enum MemoryType {
68 HeapPage,
69 LargeItem,
70 SmallItem
71};
72
73struct FunctionCallProperties {
74 qint64 start;
75 qint64 end;
76 quintptr id;
77};
78
79struct FunctionLocation {
80 FunctionLocation(const QString &name = QString(), const QString &file = QString(),
81 int line = -1, int column = -1) :
82 name(name), file(file), line(line), column(column)
83 {}
84
85 bool isValid()
86 {
87 return !name.isEmpty();
88 }
89
90 QString name;
91 QString file;
92 int line;
93 int column;
94};
95
96typedef QHash<quintptr, QV4::Profiling::FunctionLocation> FunctionLocationHash;
97
98struct MemoryAllocationProperties {
99 qint64 timestamp;
100 qint64 size;
101 MemoryType type;
102};
103
104class FunctionCall {
105public:
106 FunctionCall() : m_function(nullptr), m_start(0), m_end(0) {}
107
108 FunctionCall(Function *function, qint64 start, qint64 end) :
109 m_function(function), m_start(start), m_end(end)
110 { m_function->executableCompilationUnit()->addref(); }
111
112 FunctionCall(const FunctionCall &other) :
113 m_function(other.m_function), m_start(other.m_start), m_end(other.m_end)
114 { m_function->executableCompilationUnit()->addref(); }
115
116 FunctionCall(FunctionCall &&other) noexcept
117 : m_function(std::exchange(other.m_function, nullptr))
118 , m_start(std::exchange(other.m_start, 0))
119 , m_end(std::exchange(other.m_end, 0))
120 {}
121
122 ~FunctionCall()
123 {
124 if (m_function)
125 m_function->executableCompilationUnit()->release();
126 }
127
128 FunctionCall &operator=(const FunctionCall &other) {
129 if (&other != this) {
130 if (other.m_function)
131 other.m_function->executableCompilationUnit()->addref();
132 if (m_function)
133 m_function->executableCompilationUnit()->release();
134 m_function = other.m_function;
135 m_start = other.m_start;
136 m_end = other.m_end;
137 }
138 return *this;
139 }
140
141 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(FunctionCall)
142
143 void swap(FunctionCall &other) noexcept
144 {
145 qt_ptr_swap(m_function, other.m_function);
146 std::swap(m_start, other.m_start);
147 std::swap(m_end, other.m_end);
148 }
149
150 Function *function() const
151 {
152 return m_function;
153 }
154
155 FunctionLocation resolveLocation() const;
156 FunctionCallProperties properties() const;
157
158private:
159 friend bool operator<(const FunctionCall &call1, const FunctionCall &call2);
160
161 Function *m_function;
162 qint64 m_start;
163 qint64 m_end;
164};
165
166class Q_QML_EXPORT Profiler : public QObject {
168public:
169 struct SentMarker {
170 SentMarker() : m_function(nullptr) {}
171
173 {
174 if (m_function)
176 }
177
178 ~SentMarker()
179 {
180 if (m_function)
182 }
183
185 {
186 if (&other != this) {
187 if (m_function)
191 }
192 return *this;
193 }
194
196 {
197 Q_ASSERT(m_function == nullptr);
200 }
201
202 bool isValid() const
203 { return m_function != nullptr; }
204
205 private:
207 };
208
210
212 {
213 if (size) {
216 return true;
217 } else {
218 return false;
219 }
220 }
221
223 {
224 if (size) {
227 return true;
228 } else {
229 return false;
230 }
231 }
232
234
235 void stopProfiling();
237 void reportData();
238 void setTimer(const QElapsedTimer &timer) { m_timer = timer; }
239
244
245private:
251
252 friend class FunctionCallProfiler;
253};
254
255class FunctionCallProfiler {
256 Q_DISABLE_COPY(FunctionCallProfiler)
257public:
258
259 // It's enough to ref() the function in the destructor as it will probably not disappear while
260 // it's executing ...
261 FunctionCallProfiler(ExecutionEngine *engine, Function *f)
262 {
263 Profiler *p = engine->profiler();
264 if (Q_UNLIKELY(p) && (p->featuresEnabled & (1 << Profiling::FeatureFunctionCall))) {
265 profiler = p;
266 function = f;
267 startTime = profiler->m_timer.nsecsElapsed();
268 }
269 }
270
271 ~FunctionCallProfiler()
272 {
273 if (profiler)
274 profiler->m_data.append(FunctionCall(function, startTime, profiler->m_timer.nsecsElapsed()));
275 }
276
277 Profiler *profiler = nullptr;
278 Function *function = nullptr;
279 qint64 startTime = 0;
280};
281
282
283} // namespace Profiling
284} // namespace QV4
285
286Q_DECLARE_TYPEINFO(QV4::Profiling::MemoryAllocationProperties, Q_RELOCATABLE_TYPE);
287Q_DECLARE_TYPEINFO(QV4::Profiling::FunctionCallProperties, Q_RELOCATABLE_TYPE);
288Q_DECLARE_TYPEINFO(QV4::Profiling::FunctionCall, Q_RELOCATABLE_TYPE);
289Q_DECLARE_TYPEINFO(QV4::Profiling::FunctionLocation, Q_RELOCATABLE_TYPE);
291
292QT_END_NAMESPACE
293Q_DECLARE_METATYPE(QV4::Profiling::FunctionLocationHash)
294Q_DECLARE_METATYPE(QVector<QV4::Profiling::FunctionCallProperties>)
295Q_DECLARE_METATYPE(QVector<QV4::Profiling::MemoryAllocationProperties>)
296
297#endif // QT_CONFIG(qml_debug)
298
299#endif // QV4PROFILING_H
Definition qjsvalue.h:23
Q_DECLARE_TYPEINFO(QObjectPrivate::ConnectionList, Q_RELOCATABLE_TYPE)