1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <sal/config.h>
16 #include <comphelper/profilezone.hxx>
17 #include <comphelper/sequence.hxx>
18 #include <comphelper/traceevent.hxx>
23 std::atomic
<bool> TraceEvent::s_bRecording
= (getenv("TRACE_EVENT_RECORDING") != nullptr);
25 std::atomic
<bool> TraceEvent::s_bRecording
= false;
28 std::size_t TraceEvent::s_nBufferSize
= 0;
29 void (*TraceEvent::s_pBufferFullCallback
)() = nullptr;
31 int AsyncEvent::s_nIdCounter
= 0;
33 static thread_local
int nProfileZoneNesting
= 0; // Level of Nested Profile Zones
37 std::vector
<OUString
> g_aRecording
; // recorded data
41 void TraceEvent::addRecording(const OUString
& sObject
)
43 std::lock_guard
aGuard(g_aMutex
);
45 g_aRecording
.emplace_back(sObject
);
47 if (s_nBufferSize
> 0 && g_aRecording
.size() >= s_nBufferSize
)
49 if (s_pBufferFullCallback
!= nullptr)
50 (*s_pBufferFullCallback
)();
54 void TraceEvent::addInstantEvent(const char* sName
, const std::map
<OUString
, OUString
>& args
)
56 long long nNow
= getNow();
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
;
66 + OUString(sName
, strlen(sName
), RTL_TEXTENCODING_UTF8
)
69 + createArgsString(args
) + ",\"ts\":" + OUString::number(nNow
)
72 + OUString::number(nPid
)
75 + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
78 void TraceEvent::startRecording()
80 std::lock_guard
aGuard(g_aMutex
);
84 void TraceEvent::stopRecording() { s_bRecording
= false; }
86 void TraceEvent::setBufferSizeAndCallback(std::size_t bufferSize
, void (*bufferFullCallback
)())
88 s_nBufferSize
= bufferSize
;
89 s_pBufferFullCallback
= bufferFullCallback
;
92 std::vector
<OUString
> TraceEvent::getEventVectorAndClear()
95 std::vector
<OUString
> aRecording
;
97 std::lock_guard
aGuard(g_aMutex
);
98 bRecording
= s_bRecording
;
100 aRecording
.swap(g_aRecording
);
102 // reset start time and nesting level
108 css::uno::Sequence
<OUString
> TraceEvent::getRecordingAndClear()
110 return comphelper::containerToSequence(getEventVectorAndClear());
113 void ProfileZone::addRecording()
115 assert(s_bRecording
);
117 long long nNow
= getNow();
119 // Generate a single "Complete Event" (type X)
120 TraceEvent::addRecording("{"
122 + OUString(m_sName
, strlen(m_sName
), RTL_TEXTENCODING_UTF8
)
126 + OUString::number(m_nCreateTime
)
129 + OUString::number(nNow
- m_nCreateTime
) + m_sArgs
132 + OUString::number(m_nPid
)
135 + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
138 int ProfileZone::getNestingLevel() { return nProfileZoneNesting
; }
140 void ProfileZone::setNestingLevel(int nNestingLevel
) { nProfileZoneNesting
= nNestingLevel
; }
142 } // namespace comphelper
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */