QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgseventtracing.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgseventtracing.cpp
3 --------------------------------------
4 Date : October 2019
5 Copyright : (C) 2019 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgseventtracing.h"
17
18#include <QCoreApplication>
19#include <QFile>
20#include <QString>
21#include <QThread>
22
23using namespace Qt::StringLiterals;
24
26
27struct TraceItem
28{
29 QgsEventTracing::EventType type;
30 uint threadId;
31 qint64 timestamp;
32 QString category;
33 QString name;
34 QString id;
35};
36
38static bool sIsTracing = false;
40Q_GLOBAL_STATIC( QElapsedTimer, sTracingTimer )
42Q_GLOBAL_STATIC( QVector<TraceItem>, sTraceEvents )
44Q_GLOBAL_STATIC( QMutex, sTraceEventsMutex )
45
46
47bool QgsEventTracing::startTracing()
48{
49 if ( sIsTracing )
50 return false;
51
52 sIsTracing = true;
53 sTraceEventsMutex()->lock();
54 sTracingTimer()->start();
55 sTraceEvents()->clear();
56 sTraceEvents()->reserve( 1000 );
57 sTraceEventsMutex()->unlock();
58 return true;
59}
60
61bool QgsEventTracing::stopTracing()
62{
63 if ( !sIsTracing )
64 return false;
65
66 sIsTracing = false;
67 sTracingTimer()->invalidate();
68 return false;
69}
70
71bool QgsEventTracing::isTracingEnabled()
72{
73 return sIsTracing;
74}
75
76static char _eventTypeToChar( QgsEventTracing::EventType type )
77{
78 switch ( type )
79 {
80 case QgsEventTracing::Begin: return 'B';
81 case QgsEventTracing::End: return 'E';
82 case QgsEventTracing::Instant: return 'i';
83 case QgsEventTracing::AsyncBegin: return 'b';
84 case QgsEventTracing::AsyncEnd: return 'e';
85 }
86 return '?';
87}
88
89bool QgsEventTracing::writeTrace( const QString &fileName )
90{
91 if ( sIsTracing )
92 return false;
93
94 QFile f( fileName );
95 if ( !f.open( QIODevice::WriteOnly ) )
96 return false;
97
98 f.write( "{\n\"traceEvents\": [\n" );
99
100 bool first = true;
101 for ( const auto &item : *sTraceEvents() )
102 {
103 if ( !first )
104 f.write( ",\n" );
105 else
106 first = false;
107 const char t = _eventTypeToChar( item.type );
108 QString msg = u" {\"cat\": \"%1\", \"pid\": 1, \"tid\": %2, \"ts\": %3, \"ph\": \"%4\", \"name\": \"%5\""_s
109 .arg( item.category ).arg( item.threadId ).arg( item.timestamp ).arg( t ).arg( item.name );
110
111 // for instant events we always set them as global (currently not supporting instant events at thread scope)
112 if ( item.type == Instant )
113 msg += ", \"s\": \"g\""_L1;
114
115 // async events also need to have ID associated
116 if ( item.type == AsyncBegin || item.type == AsyncEnd )
117 msg += u", \"id\": \"%1\""_s.arg( item.id );
118
119 msg += " }";
120
121 f.write( msg.toUtf8() );
122 }
123
124 f.write( "\n]\n}\n" );
125 f.close();
126 return true;
127}
128
129void QgsEventTracing::addEvent( QgsEventTracing::EventType type, const QString &category, const QString &name, const QString &id )
130{
131 if ( !sIsTracing )
132 return;
133
134 sTraceEventsMutex()->lock();
135 TraceItem item;
136 item.type = type;
137 item.timestamp = sTracingTimer()->nsecsElapsed() / 1000;
138 if ( QThread::currentThread() == QCoreApplication::instance()->thread() )
139 item.threadId = 0; // to make it show up first
140 else
141 item.threadId = static_cast<uint>( reinterpret_cast<quint64>( QThread::currentThreadId() ) );
142 item.category = category;
143 item.name = name;
144 item.id = id;
145 sTraceEvents()->append( item );
146 sTraceEventsMutex()->unlock();
147}
148
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)