summaryrefslogtreecommitdiff
path: root/comphelper/source/misc/traceevent.cxx
diff options
context:
space:
mode:
authorTor Lillqvist <tml@collabora.com>2021-04-27 09:20:32 +0300
committerTor Lillqvist <tml@collabora.com>2021-04-29 14:10:53 +0200
commitef865bba15aa351dd32b909eedac55d1e2a1771b (patch)
treebd3f8a3711e10b713b21c73acf3230aeeb567f23 /comphelper/source/misc/traceevent.cxx
parent27cf65fb14e23f9afe1a45d7a3f1c8a997f07660 (diff)
Extend the trace event API with instant events
As a preparetion for further work, refactor ProfileZone into a base class TraceEvent and a derived class ProfileZone. Change-Id: I3a93c79f46ffc5768ddaf338789fe2daa225ef4d Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114776 Tested-by: Tor Lillqvist <tml@collabora.com> Reviewed-by: Tor Lillqvist <tml@collabora.com>
Diffstat (limited to 'comphelper/source/misc/traceevent.cxx')
-rw-r--r--comphelper/source/misc/traceevent.cxx125
1 files changed, 125 insertions, 0 deletions
diff --git a/comphelper/source/misc/traceevent.cxx b/comphelper/source/misc/traceevent.cxx
new file mode 100644
index 000000000000..b1313271f89b
--- /dev/null
+++ b/comphelper/source/misc/traceevent.cxx
@@ -0,0 +1,125 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <sal/config.h>
+
+#include <atomic>
+#include <iostream>
+#include <string_view>
+
+#include <comphelper/profilezone.hxx>
+#include <comphelper/sequence.hxx>
+#include <comphelper/traceevent.hxx>
+
+#include <osl/time.h>
+#include <osl/thread.h>
+
+namespace comphelper
+{
+std::atomic<bool> TraceEvent::s_bRecording = false;
+
+int TraceEvent::s_nNesting = 0; // level of overlapped zones
+
+namespace
+{
+std::vector<OUString> g_aRecording; // recorded data
+::osl::Mutex g_aMutex;
+}
+
+void TraceEvent::addRecording(const OUString& sObject)
+{
+ ::osl::MutexGuard aGuard(g_aMutex);
+
+ g_aRecording.emplace_back(sObject);
+}
+
+void TraceEvent::addInstantEvent(const char* sProfileId)
+{
+ TimeValue aSystemTime;
+ osl_getSystemTime(&aSystemTime);
+ long long nNow
+ = static_cast<long long>(aSystemTime.Seconds) * 1000000 + aSystemTime.Nanosec / 1000;
+
+ int nPid = 0;
+ oslProcessInfo aProcessInfo;
+ aProcessInfo.Size = sizeof(oslProcessInfo);
+ if (osl_getProcessInfo(nullptr, osl_Process_IDENTIFIER, &aProcessInfo) == osl_Process_E_None)
+ nPid = aProcessInfo.Ident;
+
+ addRecording("{"
+ "\"name:\""
+ + OUString(sProfileId, strlen(sProfileId), RTL_TEXTENCODING_UTF8)
+ + "\","
+ "\"ph\":\"i\","
+ "\"ts\":"
+ + OUString::number(nNow)
+ + ","
+ "\"pid\":"
+ + OUString::number(nPid)
+ + ","
+ "\"tid\":"
+ + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
+}
+
+void ProfileZone::startRecording()
+{
+ ::osl::MutexGuard aGuard(g_aMutex);
+ s_nNesting = 0;
+ s_bRecording = true;
+}
+
+void ProfileZone::stopRecording() { s_bRecording = false; }
+
+void ProfileZone::addRecording()
+{
+ assert(s_bRecording);
+
+ TimeValue aSystemTime;
+ osl_getSystemTime(&aSystemTime);
+ long long nNow
+ = static_cast<long long>(aSystemTime.Seconds) * 1000000 + aSystemTime.Nanosec / 1000;
+
+ // Generate a single "Complete Event" (type X)
+ TraceEvent::addRecording("{"
+ "\"name\":\""
+ + OUString(m_sProfileId, strlen(m_sProfileId), RTL_TEXTENCODING_UTF8)
+ + "\","
+ "\"ph\":\"X\","
+ "\"ts\":"
+ + OUString::number(m_nCreateTime)
+ + ","
+ "\"dur\":"
+ + OUString::number(nNow - m_nCreateTime)
+ + ","
+ "\"pid\":"
+ + OUString::number(m_nPid)
+ + ","
+ "\"tid\":"
+ + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
+}
+
+css::uno::Sequence<OUString> ProfileZone::getRecordingAndClear()
+{
+ bool bRecording;
+ std::vector<OUString> aRecording;
+ {
+ ::osl::MutexGuard aGuard(g_aMutex);
+ bRecording = s_bRecording;
+ stopRecording();
+ aRecording.swap(g_aRecording);
+ }
+ // reset start time and nesting level
+ if (bRecording)
+ startRecording();
+ return ::comphelper::containerToSequence(aRecording);
+}
+
+} // namespace comphelper
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */