summaryrefslogtreecommitdiff
path: root/include/comphelper/traceevent.hxx
diff options
context:
space:
mode:
Diffstat (limited to 'include/comphelper/traceevent.hxx')
-rw-r--r--include/comphelper/traceevent.hxx115
1 files changed, 114 insertions, 1 deletions
diff --git a/include/comphelper/traceevent.hxx b/include/comphelper/traceevent.hxx
index 7e988bd063f0..3d3c748ec278 100644
--- a/include/comphelper/traceevent.hxx
+++ b/include/comphelper/traceevent.hxx
@@ -14,9 +14,10 @@
#include <atomic>
#include <memory>
-#include <set>
+#include <vector>
#include <osl/process.h>
+#include <osl/thread.h>
#include <osl/time.h>
#include <com/sun/star/uno/Sequence.h>
#include <comphelper/comphelperdllapi.h>
@@ -33,12 +34,31 @@ protected:
static void addRecording(const OUString& sObject);
+ static long long getNow()
+ {
+ TimeValue systemTime;
+ osl_getSystemTime(&systemTime);
+ return static_cast<long long>(systemTime.Seconds) * 1000000 + systemTime.Nanosec / 1000;
+ }
+
+ static int getPid()
+ {
+ oslProcessInfo aProcessInfo;
+ aProcessInfo.Size = sizeof(oslProcessInfo);
+ if (osl_getProcessInfo(nullptr, osl_Process_IDENTIFIER, &aProcessInfo)
+ == osl_Process_E_None)
+ return aProcessInfo.Ident;
+ return -1;
+ }
+
public:
static void addInstantEvent(const char* sName);
static void startRecording();
static void stopRecording();
+ static std::vector<OUString> getEventVectorAndClear();
+
static css::uno::Sequence<OUString> getRecordingAndClear();
};
@@ -53,6 +73,99 @@ protected:
}
};
+// An AsyncEvent generates a 'b' (begin) event when constructed and an 'e' (end) event when destructed
+
+class COMPHELPER_DLLPUBLIC AsyncEvent : public NamedEvent,
+ public std::enable_shared_from_this<AsyncEvent>
+{
+ static int s_nIdCounter;
+ int m_nId;
+ int m_nPid;
+ std::vector<std::shared_ptr<AsyncEvent>> m_aChildren;
+ bool m_bBeginRecorded;
+
+ AsyncEvent(const char* sName, int nId)
+ : NamedEvent(sName)
+ , m_nId(nId)
+ , m_bBeginRecorded(false)
+ {
+ if (s_bRecording)
+ {
+ long long nNow = getNow();
+
+ m_nPid = getPid();
+
+ // Generate a "Begin " (type b) event
+ TraceEvent::addRecording("{"
+ "\"name\":\""
+ + OUString(m_sName, strlen(m_sName), RTL_TEXTENCODING_UTF8)
+ + "\","
+ "\"ph\":\"b\""
+ ","
+ "\"id\":"
+ + OUString::number(m_nId)
+ + "\","
+ "\"ts\":"
+ + OUString::number(nNow)
+ + ","
+ "\"pid\":"
+ + OUString::number(m_nPid)
+ + ","
+ "\"tid\":"
+ + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
+ m_bBeginRecorded = true;
+ }
+ }
+
+public:
+ AsyncEvent(const char* sName)
+ : AsyncEvent(sName, s_nIdCounter++)
+ {
+ }
+
+ ~AsyncEvent()
+ {
+ if (m_bBeginRecorded)
+ {
+ m_aChildren.clear();
+
+ long long nNow = getNow();
+ // Generate a "Env " (type e) event
+ TraceEvent::addRecording("{"
+ "\"name\":\""
+ + OUString(m_sName, strlen(m_sName), RTL_TEXTENCODING_UTF8)
+ + "\","
+ "\"ph\":\"e\""
+ ","
+ "\"id\":"
+ + OUString::number(m_nId)
+ + "\","
+ "\"ts\":"
+ + OUString::number(nNow)
+ + ","
+ "\"pid\":"
+ + OUString::number(m_nPid)
+ + ","
+ "\"tid\":"
+ + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
+ }
+ }
+
+ static std::weak_ptr<AsyncEvent> createWithParent(const char* sName,
+ std::shared_ptr<AsyncEvent> pParent)
+ {
+ std::shared_ptr<AsyncEvent> pResult;
+
+ if (s_bRecording && pParent->m_bBeginRecorded)
+ {
+ pResult.reset(new AsyncEvent(sName, pParent->m_nId));
+ pParent->m_aChildren.push_back(pResult);
+ }
+
+ return pResult;
+ }
+};
+
} // namespace comphelper
#endif // INCLUDED_COMPHELPER_TRACEEVENT_HXX