LibreOffice Module comphelper (master) 1
traceevent.cxx
Go to the documentation of this file.
1/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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#include <sal/config.h>
11
12#include <atomic>
13#include <mutex>
14#include <iostream>
15
19
20namespace comphelper
21{
22#ifdef DBG_UTIL
23std::atomic<bool> TraceEvent::s_bRecording = (getenv("TRACE_EVENT_RECORDING") != nullptr);
24#else
25std::atomic<bool> TraceEvent::s_bRecording = false;
26#endif
27
28std::size_t TraceEvent::s_nBufferSize = 0;
29void (*TraceEvent::s_pBufferFullCallback)() = nullptr;
30
32
33static thread_local int nProfileZoneNesting = 0; // Level of Nested Profile Zones
34
35namespace
36{
37std::vector<OUString> g_aRecording; // recorded data
38std::mutex g_aMutex;
39}
40
41void TraceEvent::addRecording(const OUString& sObject)
42{
43 std::lock_guard aGuard(g_aMutex);
44
45 g_aRecording.emplace_back(sObject);
46
47 if (s_nBufferSize > 0 && g_aRecording.size() >= s_nBufferSize)
48 {
49 if (s_pBufferFullCallback != nullptr)
50 (*s_pBufferFullCallback)();
51 }
52}
53
54void TraceEvent::addInstantEvent(const char* sName, const std::map<OUString, OUString>& args)
55{
56 long long nNow = getNow();
57
58 int nPid = 0;
59 oslProcessInfo aProcessInfo;
60 aProcessInfo.Size = sizeof(oslProcessInfo);
61 if (osl_getProcessInfo(nullptr, osl_Process_IDENTIFIER, &aProcessInfo) == osl_Process_E_None)
62 nPid = aProcessInfo.Ident;
63
64 addRecording("{"
65 "\"name:\""
66 + OUString(sName, strlen(sName), RTL_TEXTENCODING_UTF8)
67 + "\","
68 "\"ph\":\"i\""
69 + createArgsString(args) + ",\"ts\":" + OUString::number(nNow)
70 + ","
71 "\"pid\":"
72 + OUString::number(nPid)
73 + ","
74 "\"tid\":"
75 + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
76}
77
79{
80 std::lock_guard aGuard(g_aMutex);
81 s_bRecording = true;
82}
83
85
86void TraceEvent::setBufferSizeAndCallback(std::size_t bufferSize, void (*bufferFullCallback)())
87{
88 s_nBufferSize = bufferSize;
89 s_pBufferFullCallback = bufferFullCallback;
90}
91
93{
94 bool bRecording;
95 std::vector<OUString> aRecording;
96 {
97 std::lock_guard aGuard(g_aMutex);
98 bRecording = s_bRecording;
100 aRecording.swap(g_aRecording);
101 }
102 // reset start time and nesting level
103 if (bRecording)
105 return aRecording;
106}
107
108css::uno::Sequence<OUString> TraceEvent::getRecordingAndClear()
109{
111}
112
113void ProfileZone::addRecording()
114{
115 assert(s_bRecording);
116
117 long long nNow = getNow();
118
119 // Generate a single "Complete Event" (type X)
121 "\"name\":\""
122 + OUString(m_sName, strlen(m_sName), RTL_TEXTENCODING_UTF8)
123 + "\","
124 "\"ph\":\"X\","
125 "\"ts\":"
126 + OUString::number(m_nCreateTime)
127 + ","
128 "\"dur\":"
129 + OUString::number(nNow - m_nCreateTime) + m_sArgs
130 + ","
131 "\"pid\":"
132 + OUString::number(m_nPid)
133 + ","
134 "\"tid\":"
135 + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
136}
137
138int ProfileZone::getNestingLevel() { return nProfileZoneNesting; }
139
140void ProfileZone::setNestingLevel(int nNestingLevel) { nProfileZoneNesting = nNestingLevel; }
141
142} // namespace comphelper
143
144/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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 void setBufferSizeAndCallback(std::size_t bufferSize, void(*bufferFullCallback)())
Definition: traceevent.cxx:86
static void(* s_pBufferFullCallback)()
Definition: traceevent.hxx:47
static long long getNow()
Definition: traceevent.hxx:54
static void stopRecording()
Definition: traceevent.cxx:84
static void addInstantEvent(const char *sName, const std::map< OUString, OUString > &args=std::map< OUString, OUString >())
Definition: traceevent.cxx:54
static std::vector< OUString > getEventVectorAndClear()
Definition: traceevent.cxx:92
static OUString createArgsString(const std::map< OUString, OUString > &args)
Definition: traceevent.hxx:61
static css::uno::Sequence< OUString > getRecordingAndClear()
Definition: traceevent.cxx:108
static void startRecording()
Definition: traceevent.cxx:78
OUString m_sName
OUString sName
static thread_local int nProfileZoneNesting
Definition: traceevent.cxx:33
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Copy from a container into a Sequence.
Definition: sequence.hxx:190
args
std::mutex mutex
Definition: random.cxx:41