| 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 "qv4profiling_p.h" |
| 41 | #include <private/qv4mm_p.h> |
| 42 | #include <private/qv4string_p.h> |
| 43 | |
| 44 | QT_BEGIN_NAMESPACE |
| 45 | |
| 46 | namespace QV4 { |
| 47 | namespace Profiling { |
| 48 | |
| 49 | FunctionLocation FunctionCall::resolveLocation() const |
| 50 | { |
| 51 | return FunctionLocation(m_function->name()->toQString(), |
| 52 | m_function->executableCompilationUnit()->fileName(), |
| 53 | m_function->compiledFunction->location.line, |
| 54 | m_function->compiledFunction->location.column); |
| 55 | } |
| 56 | |
| 57 | FunctionCallProperties FunctionCall::properties() const |
| 58 | { |
| 59 | FunctionCallProperties props = { |
| 60 | .start: m_start, |
| 61 | .end: m_end, |
| 62 | .id: reinterpret_cast<quintptr>(m_function) |
| 63 | }; |
| 64 | return props; |
| 65 | } |
| 66 | |
| 67 | Profiler::Profiler(QV4::ExecutionEngine *engine) : featuresEnabled(0), m_engine(engine) |
| 68 | { |
| 69 | static const int metatypes[] = { |
| 70 | qRegisterMetaType<QVector<QV4::Profiling::FunctionCallProperties> >(), |
| 71 | qRegisterMetaType<QVector<QV4::Profiling::MemoryAllocationProperties> >(), |
| 72 | qRegisterMetaType<FunctionLocationHash>() |
| 73 | }; |
| 74 | Q_UNUSED(metatypes); |
| 75 | m_timer.start(); |
| 76 | } |
| 77 | |
| 78 | void Profiler::stopProfiling() |
| 79 | { |
| 80 | featuresEnabled = 0; |
| 81 | reportData(); |
| 82 | m_sentLocations.clear(); |
| 83 | } |
| 84 | |
| 85 | bool operator<(const FunctionCall &call1, const FunctionCall &call2) |
| 86 | { |
| 87 | return call1.m_start < call2.m_start || |
| 88 | (call1.m_start == call2.m_start && (call1.m_end < call2.m_end || |
| 89 | (call1.m_end == call2.m_end && call1.m_function < call2.m_function))); |
| 90 | } |
| 91 | |
| 92 | void Profiler::reportData() |
| 93 | { |
| 94 | std::sort(first: m_data.begin(), last: m_data.end()); |
| 95 | QVector<FunctionCallProperties> properties; |
| 96 | FunctionLocationHash locations; |
| 97 | properties.reserve(size: m_data.size()); |
| 98 | |
| 99 | for (const FunctionCall &call : qAsConst(t&: m_data)) { |
| 100 | properties.append(t: call.properties()); |
| 101 | Function *function = call.function(); |
| 102 | SentMarker &marker = m_sentLocations[reinterpret_cast<quintptr>(function)]; |
| 103 | if (!marker.isValid()) { |
| 104 | FunctionLocation &location = locations[properties.constLast().id]; |
| 105 | if (!location.isValid()) |
| 106 | location = call.resolveLocation(); |
| 107 | marker.setFunction(function); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | emit dataReady(locations, properties, m_memory_data); |
| 112 | m_data.clear(); |
| 113 | m_memory_data.clear(); |
| 114 | } |
| 115 | |
| 116 | void Profiler::startProfiling(quint64 features) |
| 117 | { |
| 118 | if (featuresEnabled == 0) { |
| 119 | if (features & (1 << FeatureMemoryAllocation)) { |
| 120 | qint64 timestamp = m_timer.nsecsElapsed(); |
| 121 | MemoryAllocationProperties heap = {.timestamp: timestamp, |
| 122 | .size: (qint64)m_engine->memoryManager->getAllocatedMem() - |
| 123 | (qint64)m_engine->memoryManager->getLargeItemsMem(), |
| 124 | .type: HeapPage}; |
| 125 | m_memory_data.append(t: heap); |
| 126 | MemoryAllocationProperties small = {.timestamp: timestamp, |
| 127 | .size: (qint64)m_engine->memoryManager->getUsedMem(), |
| 128 | .type: SmallItem}; |
| 129 | m_memory_data.append(t: small); |
| 130 | MemoryAllocationProperties large = {.timestamp: timestamp, |
| 131 | .size: (qint64)m_engine->memoryManager->getLargeItemsMem(), |
| 132 | .type: LargeItem}; |
| 133 | m_memory_data.append(t: large); |
| 134 | } |
| 135 | |
| 136 | featuresEnabled = features; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | } // namespace Profiling |
| 141 | } // namespace QV4 |
| 142 | |
| 143 | QT_END_NAMESPACE |
| 144 | |
| 145 | #include "moc_qv4profiling_p.cpp" |
| 146 | |