LibreOffice Module comphelper (master) 1
traceevent.hxx
Go to the documentation of this file.
1/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8*/
9
10#ifndef INCLUDED_COMPHELPER_TRACEEVENT_HXX
11#define INCLUDED_COMPHELPER_TRACEEVENT_HXX
12
13#include <sal/config.h>
14
15#include <atomic>
16#include <map>
17#include <memory>
18#include <utility>
19#include <vector>
20
21#include <osl/process.h>
22#include <osl/thread.h>
23#include <osl/time.h>
24#include <com/sun/star/uno/Sequence.h>
26#include <rtl/ustrbuf.hxx>
27#include <rtl/ustring.hxx>
28
29// implementation of XToolkitExperimental profiling API
30
31namespace comphelper
32{
34{
35private:
36 static int getPid()
37 {
38 oslProcessInfo aProcessInfo;
39 aProcessInfo.Size = sizeof(oslProcessInfo);
40 if (osl_getProcessInfo(nullptr, osl_Process_IDENTIFIER, &aProcessInfo)
41 == osl_Process_E_None)
42 return aProcessInfo.Ident;
43 return -1;
44 }
45
46 static std::size_t s_nBufferSize;
47 static void (*s_pBufferFullCallback)();
48
49protected:
50 static std::atomic<bool> s_bRecording; // true during recording
51
52 static void addRecording(const OUString& sObject);
53
54 static long long getNow()
55 {
56 TimeValue systemTime;
57 osl_getSystemTime(&systemTime);
58 return static_cast<long long>(systemTime.Seconds) * 1000000 + systemTime.Nanosec / 1000;
59 }
60
61 static OUString createArgsString(const std::map<OUString, OUString>& args)
62 {
63 if (args.size() == 0)
64 return "";
65
66 OUStringBuffer sResult;
67 sResult.append(",\"args\":{");
68 bool first = true;
69 for (auto& i : args)
70 {
71 if (!first)
72 sResult.append(',');
73 sResult.append('"');
74 sResult.append(i.first);
75 sResult.append("\":\"");
76 sResult.append(i.second);
77 sResult.append('"');
78 first = false;
79 }
80 sResult.append('}');
81
82 return sResult.makeStringAndClear();
83 }
84
85 const int m_nPid;
86 const OUString m_sArgs;
87
88 TraceEvent(OUString sArgs)
89 : m_nPid(s_bRecording ? getPid() : 1)
90 , m_sArgs(std::move(sArgs))
91 {
92 }
93
94 TraceEvent(const std::map<OUString, OUString>& aArgs)
95 : TraceEvent(createArgsString(aArgs))
96 {
97 }
98
99public:
100 static void addInstantEvent(const char* sName, const std::map<OUString, OUString>& args
101 = std::map<OUString, OUString>());
102
103 static void startRecording();
104 static void stopRecording();
105 static void setBufferSizeAndCallback(std::size_t bufferSize, void (*bufferFullCallback)());
106
107 static std::vector<OUString> getEventVectorAndClear();
108
109 static css::uno::Sequence<OUString> getRecordingAndClear();
110};
111
113{
114protected:
115 const char* m_sName;
116
117 NamedEvent(const char* sName, const OUString& sArgs)
118 : TraceEvent(sArgs)
119 , m_sName(sName ? sName : "(null)")
120 {
121 }
122
123 NamedEvent(const char* sName, const std::map<OUString, OUString>& aArgs)
124 : TraceEvent(aArgs)
125 , m_sName(sName ? sName : "(null)")
126 {
127 }
128};
129
130// An AsyncEvent generates an 'S' (start) event when constructed and a 'F' (finish) event when it
131// is destructed.
132
133// The Trace Event specification claims that these event types are deprecated and replaces by
134// nestable 'b' (begin) and 'e' (end) events, but Chrome does not seem to support those.
135
136// To generate a pair of 'S' and 'F' events, create an AsyncEvent object using the AsyncEvent(const
137// char* sName) constructor when you want the 'S' event to be generated, and destroy it when you
138// want the corresponding 'F' event to be generated.
139
141 public std::enable_shared_from_this<AsyncEvent>
142{
143 static int s_nIdCounter;
144 int m_nId;
146
147 AsyncEvent(const char* sName, int nId, const std::map<OUString, OUString>& args)
149 , m_nId(nId)
150 , m_bBeginRecorded(false)
151 {
152 if (!s_bRecording)
153 return;
154
155 long long nNow = getNow();
156
157 // Generate a "Start" (type S) event
159 "\"name\":\""
160 + OUString(m_sName, strlen(m_sName), RTL_TEXTENCODING_UTF8)
161 + "\","
162 "\"ph\":\"S\""
163 ","
164 "\"id\":"
165 + OUString::number(m_nId) + m_sArgs
166 + ","
167 "\"ts\":"
168 + OUString::number(nNow)
169 + ","
170 "\"pid\":"
171 + OUString::number(m_nPid)
172 + ","
173 "\"tid\":"
174 + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
175 m_bBeginRecorded = true;
176 }
177
179 {
180 if (!m_bBeginRecorded)
181 return;
182
183 m_bBeginRecorded = false;
184
185 long long nNow = getNow();
186 // Generate a "Finish" (type F) event
188 "\"name\":\""
189 + OUString(m_sName, strlen(m_sName), RTL_TEXTENCODING_UTF8)
190 + "\","
191 "\"ph\":\"F\""
192 ","
193 "\"id\":"
194 + OUString::number(m_nId) + m_sArgs
195 + ","
196 "\"ts\":"
197 + OUString::number(nNow)
198 + ","
199 "\"pid\":"
200 + OUString::number(m_nPid)
201 + ","
202 "\"tid\":"
203 + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
204 }
205
206public:
207 AsyncEvent(const char* sName,
208 const std::map<OUString, OUString>& args = std::map<OUString, OUString>())
209 : AsyncEvent(sName, s_nIdCounter++, args)
210 {
211 }
212
213 ~AsyncEvent() { generateEnd(); }
214
215 void finish() { generateEnd(); }
216};
217
218} // namespace comphelper
219
220#endif // INCLUDED_COMPHELPER_TRACEEVENT_HXX
221
222/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AsyncEvent(const char *sName, const std::map< OUString, OUString > &args=std::map< OUString, OUString >())
Definition: traceevent.hxx:207
AsyncEvent(const char *sName, int nId, const std::map< OUString, OUString > &args)
Definition: traceevent.hxx:147
NamedEvent(const char *sName, const std::map< OUString, OUString > &aArgs)
Definition: traceevent.hxx:123
NamedEvent(const char *sName, const OUString &sArgs)
Definition: traceevent.hxx:117
TraceEvent(OUString sArgs)
Definition: traceevent.hxx:88
const OUString m_sArgs
Definition: traceevent.hxx:86
static std::size_t s_nBufferSize
Definition: traceevent.hxx:46
static void addRecording(const OUString &sObject)
Definition: traceevent.cxx:41
static std::atomic< bool > s_bRecording
Definition: traceevent.hxx:50
static long long getNow()
Definition: traceevent.hxx:54
TraceEvent(const std::map< OUString, OUString > &aArgs)
Definition: traceevent.hxx:94
static OUString createArgsString(const std::map< OUString, OUString > &args)
Definition: traceevent.hxx:61
#define COMPHELPER_DLLPUBLIC
OUString m_sName
const char * sName
int i
constexpr OUStringLiteral first
args
sal_Int16 nId