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 <iostream>
14 #include <string_view>
15 
17 #include <comphelper/sequence.hxx>
19 
20 namespace comphelper
21 {
22 #ifdef DBG_UTIL
23 std::atomic<bool> TraceEvent::s_bRecording = (getenv("TRACE_EVENT_RECORDING") != nullptr);
24 #else
25 std::atomic<bool> TraceEvent::s_bRecording = false;
26 #endif
27 
28 std::size_t TraceEvent::s_nBufferSize = 0;
30 
32 
33 static thread_local int nProfileZoneNesting = 0; // Level of Nested Profile Zones
34 
35 namespace
36 {
37 std::vector<OUString> g_aRecording; // recorded data
38 osl::Mutex g_aMutex;
39 }
40 
41 void TraceEvent::addRecording(const OUString& sObject)
42 {
43  osl::MutexGuard 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 
54 void 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  osl::MutexGuard aGuard(g_aMutex);
81  s_bRecording = true;
82 }
83 
85 
86 void TraceEvent::setBufferSizeAndCallback(std::size_t bufferSize, void (*bufferFullCallback)())
87 {
88  s_nBufferSize = bufferSize;
89  s_pBufferFullCallback = bufferFullCallback;
90 }
91 
92 std::vector<OUString> TraceEvent::getEventVectorAndClear()
93 {
94  bool bRecording;
95  std::vector<OUString> aRecording;
96  {
97  osl::MutexGuard aGuard(g_aMutex);
98  bRecording = s_bRecording;
99  stopRecording();
100  aRecording.swap(g_aRecording);
101  }
102  // reset start time and nesting level
103  if (bRecording)
104  startRecording();
105  return aRecording;
106 }
107 
108 css::uno::Sequence<OUString> TraceEvent::getRecordingAndClear()
109 {
111 }
112 
113 void 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 
138 int ProfileZone::getNestingLevel() { return nProfileZoneNesting; }
139 
140 void ProfileZone::setNestingLevel(int nNestingLevel) { nProfileZoneNesting = nNestingLevel; }
141 
142 } // namespace comphelper
143 
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void(* s_pBufferFullCallback)()
Definition: traceevent.hxx:47
static std::atomic< bool > s_bRecording
Definition: traceevent.hxx:50
static void setBufferSizeAndCallback(std::size_t bufferSize, void(*bufferFullCallback)())
Definition: traceevent.cxx:86
static css::uno::Sequence< OUString > getRecordingAndClear()
Definition: traceevent.cxx:108
static void addInstantEvent(const char *sName, const std::map< OUString, OUString > &args=std::map< OUString, OUString >())
Definition: traceevent.cxx:54
OUString m_sName
static std::vector< OUString > getEventVectorAndClear()
Definition: traceevent.cxx:92
static long long getNow()
Definition: traceevent.hxx:54
static OUString createArgsString(const std::map< OUString, OUString > &args)
Definition: traceevent.hxx:61
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:182
static void stopRecording()
Definition: traceevent.cxx:84
static std::size_t s_nBufferSize
Definition: traceevent.hxx:46
static void addRecording(const OUString &sObject)
Definition: traceevent.cxx:41
static void startRecording()
Definition: traceevent.cxx:78
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo