1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QQMLPROFILERSERVICE_P_H
43 #define QQMLPROFILERSERVICE_P_H
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include <private/qqmldebugservice_p.h>
57 #include <QtQml/qtqmlglobal.h>
58 #include <QtCore/qelapsedtimer.h>
59 #include <QtCore/qmutex.h>
60 #include <QtCore/qvector.h>
61 #include <QtCore/qstringbuilder.h>
67 struct Q_AUTOTEST_EXPORT QQmlProfilerData
74 QString detailData; //used by RangeData and RangeLocation
75 int line; //used by RangeLocation
76 int column; //used by RangeLocation
77 int framerate; //used by animation events
78 int animationcount; //used by animation events
80 QByteArray toByteArray() const;
83 Q_DECLARE_TYPEINFO(QQmlProfilerData, Q_MOVABLE_TYPE);
89 class Q_QML_EXPORT QQmlProfilerService : public QQmlDebugService
98 Complete, // end of transmission
118 Binding, //running a binding
119 HandlingSignal, //running a signal handler
124 static void initialize();
126 static bool startProfiling();
127 static bool stopProfiling();
128 static void sendStartedProfilingMessage();
129 static void addEvent(EventType);
130 static void animationFrame(qint64);
132 static void sendProfilingData();
134 QQmlProfilerService();
135 ~QQmlProfilerService();
138 virtual void stateAboutToBeChanged(State state);
139 virtual void messageReceived(const QByteArray &);
142 bool startProfilingImpl();
143 bool stopProfilingImpl();
144 void sendStartedProfilingMessageImpl();
145 void addEventImpl(EventType);
146 void animationFrameImpl(qint64);
148 void startRange(RangeType);
149 void rangeData(RangeType, const QString &);
150 void rangeData(RangeType, const QUrl &);
151 void rangeLocation(RangeType, const QString &, int, int);
152 void rangeLocation(RangeType, const QUrl &, int, int);
153 void endRange(RangeType);
156 bool profilingEnabled();
157 void setProfilingEnabled(bool enable);
159 void processMessage(const QQmlProfilerData &);
162 QElapsedTimer m_timer;
164 bool m_messageReceived;
165 QVector<QQmlProfilerData> m_data;
168 static QQmlProfilerService *instance;
170 friend struct QQmlBindingProfiler;
171 friend struct QQmlHandlingSignalProfiler;
172 friend struct QQmlObjectCreatingProfiler;
173 friend struct QQmlCompilingProfiler;
177 // RAII helper structs
180 struct QQmlBindingProfiler {
181 QQmlBindingProfiler(const QString &url, int line, int column)
183 QQmlProfilerService *instance = QQmlProfilerService::instance;
184 enabled = instance ? instance->profilingEnabled() : false;
186 instance->startRange(QQmlProfilerService::Binding);
187 instance->rangeLocation(QQmlProfilerService::Binding, url, line, column);
191 ~QQmlBindingProfiler()
194 QQmlProfilerService::instance->endRange(QQmlProfilerService::Binding);
197 void addDetail(const QString &details)
200 QQmlProfilerService::instance->rangeData(QQmlProfilerService::Binding,
207 struct QQmlHandlingSignalProfiler {
208 QQmlHandlingSignalProfiler()
210 enabled = QQmlProfilerService::instance
211 ? QQmlProfilerService::instance->profilingEnabled() : false;
213 QQmlProfilerService::instance->startRange(
214 QQmlProfilerService::HandlingSignal);
218 void setSignalInfo(const QString &name, const QString &expression)
221 QQmlProfilerService::instance->rangeData(
222 QQmlProfilerService::HandlingSignal,
223 name % QLatin1String(": ") % expression);
226 void setLocation(const QString &file, int line, int column)
229 QQmlProfilerService::instance->rangeLocation(
230 QQmlProfilerService::HandlingSignal, file, line, column);
233 ~QQmlHandlingSignalProfiler()
236 QQmlProfilerService::instance->endRange(
237 QQmlProfilerService::HandlingSignal);
243 struct QQmlObjectCreatingProfiler {
244 QQmlObjectCreatingProfiler()
246 QQmlProfilerService *instance = QQmlProfilerService::instance;
248 instance->profilingEnabled() : false;
250 instance->startRange(QQmlProfilerService::Creating);
253 void setTypeName(const QString &typeName)
256 QQmlProfilerService::instance->rangeData(
257 QQmlProfilerService::Creating, typeName);
260 void setLocation(const QUrl &url, int line, int column)
263 QQmlProfilerService::instance->rangeLocation(
264 QQmlProfilerService::Creating, url, line, column);
267 ~QQmlObjectCreatingProfiler()
270 QQmlProfilerService::instance->endRange(QQmlProfilerService::Creating);
276 struct QQmlCompilingProfiler {
277 QQmlCompilingProfiler(const QString &name)
279 QQmlProfilerService *instance = QQmlProfilerService::instance;
281 instance->profilingEnabled() : false;
283 instance->startRange(QQmlProfilerService::Compiling);
284 instance->rangeLocation(QQmlProfilerService::Compiling, name, 1, 1);
285 instance->rangeData(QQmlProfilerService::Compiling, name);
289 ~QQmlCompilingProfiler()
292 QQmlProfilerService::instance->endRange(QQmlProfilerService::Compiling);
302 #endif // QQMLPROFILERSERVICE_P_H