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.cpp
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
6#include <private/qv4mm_p.h>
7#include <private/qv4string_p.h>
8
9QT_BEGIN_NAMESPACE
10
11namespace QV4 {
12namespace Profiling {
13
14FunctionLocation FunctionCall::resolveLocation() const
15{
16 return FunctionLocation(m_function->name()->toQString(),
17 m_function->executableCompilationUnit()->fileName(),
18 m_function->compiledFunction->location.line(),
19 m_function->compiledFunction->location.column());
20}
21
22FunctionCallProperties FunctionCall::properties() const
23{
24 FunctionCallProperties props = {
25 m_start,
26 m_end,
27 reinterpret_cast<quintptr>(m_function)
28 };
29 return props;
30}
31
32Profiler::Profiler(QV4::ExecutionEngine *engine) : featuresEnabled(0), m_engine(engine)
33{
34 static const int metatypes[] = {
35 qRegisterMetaType<QVector<QV4::Profiling::FunctionCallProperties> >(),
36 qRegisterMetaType<QVector<QV4::Profiling::MemoryAllocationProperties> >(),
37 qRegisterMetaType<FunctionLocationHash>()
38 };
39 Q_UNUSED(metatypes);
40 m_timer.start();
41}
42
43void Profiler::stopProfiling()
44{
45 featuresEnabled = 0;
46 reportData();
47 m_sentLocations.clear();
48}
49
50bool operator<(const FunctionCall &call1, const FunctionCall &call2)
51{
52 return call1.m_start < call2.m_start ||
53 (call1.m_start == call2.m_start && (call1.m_end < call2.m_end ||
54 (call1.m_end == call2.m_end && call1.m_function < call2.m_function)));
55}
56
57void Profiler::reportData()
58{
59 std::sort(m_data.begin(), m_data.end());
60 QVector<FunctionCallProperties> properties;
61 FunctionLocationHash locations;
62 properties.reserve(m_data.size());
63
64 for (const FunctionCall &call : std::as_const(m_data)) {
65 properties.append(call.properties());
66 Function *function = call.function();
67 Q_ASSERT(function);
68 SentMarker &marker = m_sentLocations[reinterpret_cast<quintptr>(function)];
69 if (!marker.isValid()) {
70 FunctionLocation &location = locations[properties.constLast().id];
71 if (!location.isValid())
72 location = call.resolveLocation();
73 marker.setFunction(function);
74 }
75 }
76
77 emit dataReady(locations, properties, m_memory_data);
78 m_data.clear();
79 m_memory_data.clear();
80}
81
82void Profiler::startProfiling(quint64 features)
83{
84 if (featuresEnabled == 0) {
85 if (features & (1 << FeatureMemoryAllocation)) {
86 qint64 timestamp = m_timer.nsecsElapsed();
87 MemoryAllocationProperties heap = {timestamp,
88 (qint64)m_engine->memoryManager->getAllocatedMem() -
89 (qint64)m_engine->memoryManager->getLargeItemsMem(),
90 HeapPage};
91 m_memory_data.append(heap);
92 MemoryAllocationProperties smallP = {timestamp,
93 (qint64)m_engine->memoryManager->getUsedMem(),
94 SmallItem};
95 m_memory_data.append(smallP);
96 MemoryAllocationProperties large = {timestamp,
97 (qint64)m_engine->memoryManager->getLargeItemsMem(),
98 LargeItem};
99 m_memory_data.append(large);
100 }
101
102 featuresEnabled = features;
103 }
104}
105
106} // namespace Profiling
107} // namespace QV4
108
109QT_END_NAMESPACE
110
111#include "moc_qv4profiling_p.cpp"