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);
200 struct QQmlHandlingSignalProfiler {
201 QQmlHandlingSignalProfiler()
203 enabled = QQmlProfilerService::instance
204 ? QQmlProfilerService::instance->profilingEnabled() : false;
206 QQmlProfilerService::instance->startRange(
207 QQmlProfilerService::HandlingSignal);
211 void setSignalInfo(const QString &name, const QString &expression)
214 QQmlProfilerService::instance->rangeData(
215 QQmlProfilerService::HandlingSignal,
216 name % QLatin1String(": ") % expression);
219 void setLocation(const QString &file, int line, int column)
222 QQmlProfilerService::instance->rangeLocation(
223 QQmlProfilerService::HandlingSignal, file, line, column);
226 ~QQmlHandlingSignalProfiler()
229 QQmlProfilerService::instance->endRange(
230 QQmlProfilerService::HandlingSignal);
236 struct QQmlObjectCreatingProfiler {
237 QQmlObjectCreatingProfiler()
239 QQmlProfilerService *instance = QQmlProfilerService::instance;
241 instance->profilingEnabled() : false;
243 instance->startRange(QQmlProfilerService::Creating);
246 void setTypeName(const QString &typeName)
249 QQmlProfilerService::instance->rangeData(
250 QQmlProfilerService::Creating, typeName);
253 void setLocation(const QUrl &url, int line, int column)
256 QQmlProfilerService::instance->rangeLocation(
257 QQmlProfilerService::Creating, url, line, column);
260 ~QQmlObjectCreatingProfiler()
263 QQmlProfilerService::instance->endRange(QQmlProfilerService::Creating);
269 struct QQmlCompilingProfiler {
270 QQmlCompilingProfiler(const QString &name)
272 QQmlProfilerService *instance = QQmlProfilerService::instance;
274 instance->profilingEnabled() : false;
276 instance->startRange(QQmlProfilerService::Compiling);
277 instance->rangeLocation(QQmlProfilerService::Compiling, name, 1, 1);
278 instance->rangeData(QQmlProfilerService::Compiling, name);
282 ~QQmlCompilingProfiler()
285 QQmlProfilerService::instance->endRange(QQmlProfilerService::Compiling);
295 #endif // QQMLPROFILERSERVICE_P_H