[Libreoffice-commits] core.git: comphelper/qa comphelper/source include/comphelper

Tor Lillqvist (via logerrit) logerrit at kemper.freedesktop.org
Wed Jun 9 11:50:15 UTC 2021


 comphelper/qa/unit/test_traceevent.cxx |   15 ++++--
 comphelper/source/misc/traceevent.cxx  |    9 +--
 include/comphelper/profilezone.hxx     |    9 +--
 include/comphelper/traceevent.hxx      |   82 ++++++++++++++++++++++++---------
 4 files changed, 77 insertions(+), 38 deletions(-)

New commits:
commit 6038c9125bcb9f6d5dc2b998f2bef476ba1b1f98
Author:     Tor Lillqvist <tml at collabora.com>
AuthorDate: Tue May 11 13:58:38 2021 +0300
Commit:     Tor Lillqvist <tml at collabora.com>
CommitDate: Wed Jun 9 13:49:31 2021 +0200

    Add the possibility to include a set of arguments in Trace Events
    
    Change-Id: I55720baf64bd9b719026c94e4373b6368a1a7106
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116847
    Tested-by: Tor Lillqvist <tml at collabora.com>
    Reviewed-by: Tor Lillqvist <tml at collabora.com>

diff --git a/comphelper/qa/unit/test_traceevent.cxx b/comphelper/qa/unit/test_traceevent.cxx
index 29094b14f01b..37855b82d5d4 100644
--- a/comphelper/qa/unit/test_traceevent.cxx
+++ b/comphelper/qa/unit/test_traceevent.cxx
@@ -74,7 +74,8 @@ void trace_event_test()
         CPPUNIT_ASSERT(pAsync2.expired());
 
         // This will generate a 'b' event for async3
-        auto pAsync3(std::make_shared<comphelper::AsyncEvent>("async3"));
+        std::map<OUString, OUString> aArgsAsync3({ { "foo", "bar" }, { "tem", "42" } });
+        auto pAsync3(std::make_shared<comphelper::AsyncEvent>("async3", aArgsAsync3));
 
         std::weak_ptr<comphelper::AsyncEvent> pAsync4;
 
@@ -92,7 +93,8 @@ void trace_event_test()
         comphelper::ProfileZone aZone4("test2");
 
         // This will generate an 'i' event for instant2"
-        comphelper::TraceEvent::addInstantEvent("instant2");
+        std::map<OUString, OUString> aArgsInstant2({ { "foo2", "bar2" }, { "tem2", "42" } });
+        comphelper::TraceEvent::addInstantEvent("instant2", aArgsInstant2);
 
         std::weak_ptr<comphelper::AsyncEvent> pAsync5;
         {
@@ -148,11 +150,13 @@ void TestTraceEvent::test()
     CPPUNIT_ASSERT(aEvents[0].startsWith("{\"name:\"instant1\",\"ph\":\"i\","));
     CPPUNIT_ASSERT(aEvents[1].startsWith("{\"name\":\"async2.5\",\"ph\":\"b\",\"id\":1,"));
     CPPUNIT_ASSERT(aEvents[2].startsWith("{\"name\":\"block2\",\"ph\":\"X\","));
-    CPPUNIT_ASSERT(aEvents[3].startsWith("{\"name\":\"async3\",\"ph\":\"b\",\"id\":2,"));
+    CPPUNIT_ASSERT(aEvents[3].startsWith(
+        "{\"name\":\"async3\",\"ph\":\"b\",\"id\":2,\"args\":{\"foo\",\"bar\",\"tem\",\"42\"},"));
     CPPUNIT_ASSERT(aEvents[4].startsWith("{\"name\":\"async4in3\",\"ph\":\"b\",\"id\":2,"));
     CPPUNIT_ASSERT(aEvents[5].startsWith("{\"name\":\"block3\",\"ph\":\"X\","));
     CPPUNIT_ASSERT(aEvents[6].startsWith("{\"name\":\"async2.5\",\"ph\":\"e\",\"id\":1,"));
-    CPPUNIT_ASSERT(aEvents[7].startsWith("{\"name:\"instant2\",\"ph\":\"i\","));
+    CPPUNIT_ASSERT(aEvents[7].startsWith(
+        "{\"name:\"instant2\",\"ph\":\"i\",\"args\":{\"foo2\",\"bar2\",\"tem2\",\"42\"},"));
     CPPUNIT_ASSERT(aEvents[8].startsWith("{\"name\":\"async5in4\",\"ph\":\"b\",\"id\":2,"));
     CPPUNIT_ASSERT(aEvents[9].startsWith("{\"name\":\"async6in5\",\"ph\":\"b\",\"id\":2,"));
     CPPUNIT_ASSERT(aEvents[10].startsWith("{\"name\":\"async6in5\",\"ph\":\"e\",\"id\":2,"));
@@ -161,7 +165,8 @@ void TestTraceEvent::test()
     CPPUNIT_ASSERT(aEvents[13].startsWith("{\"name\":\"test2\",\"ph\":\"X\""));
     CPPUNIT_ASSERT(aEvents[14].startsWith("{\"name\":\"async4in3\",\"ph\":\"e\",\"id\":2,"));
     CPPUNIT_ASSERT(aEvents[15].startsWith("{\"name\":\"async7in3\",\"ph\":\"e\",\"id\":2,"));
-    CPPUNIT_ASSERT(aEvents[16].startsWith("{\"name\":\"async3\",\"ph\":\"e\",\"id\":2,"));
+    CPPUNIT_ASSERT(aEvents[16].startsWith(
+        "{\"name\":\"async3\",\"ph\":\"e\",\"id\":2,\"args\":{\"foo\",\"bar\",\"tem\",\"42\"},"));
 }
 
 CPPUNIT_TEST_SUITE_REGISTRATION(TestTraceEvent);
diff --git a/comphelper/source/misc/traceevent.cxx b/comphelper/source/misc/traceevent.cxx
index 3a2e1b012c4c..cd6d3929e27c 100644
--- a/comphelper/source/misc/traceevent.cxx
+++ b/comphelper/source/misc/traceevent.cxx
@@ -40,7 +40,7 @@ void TraceEvent::addRecording(const OUString& sObject)
     g_aRecording.emplace_back(sObject);
 }
 
-void TraceEvent::addInstantEvent(const char* sName)
+void TraceEvent::addInstantEvent(const char* sName, const std::map<OUString, OUString>& args)
 {
     long long nNow = getNow();
 
@@ -54,9 +54,8 @@ void TraceEvent::addInstantEvent(const char* sName)
                  "\"name:\""
                  + OUString(sName, strlen(sName), RTL_TEXTENCODING_UTF8)
                  + "\","
-                   "\"ph\":\"i\","
-                   "\"ts\":"
-                 + OUString::number(nNow)
+                   "\"ph\":\"i\""
+                 + createArgsString(args) + ",\"ts\":" + OUString::number(nNow)
                  + ","
                    "\"pid\":"
                  + OUString::number(nPid)
@@ -110,7 +109,7 @@ void ProfileZone::addRecording()
                              + OUString::number(m_nCreateTime)
                              + ","
                                "\"dur\":"
-                             + OUString::number(nNow - m_nCreateTime)
+                             + OUString::number(nNow - m_nCreateTime) + m_sArgs
                              + ","
                                "\"pid\":"
                              + OUString::number(m_nPid)
diff --git a/include/comphelper/profilezone.hxx b/include/comphelper/profilezone.hxx
index 3431c20efcea..babd11f5b93b 100644
--- a/include/comphelper/profilezone.hxx
+++ b/include/comphelper/profilezone.hxx
@@ -27,7 +27,6 @@ class COMPHELPER_DLLPUBLIC ProfileZone : public NamedEvent
     long long m_nCreateTime;
     bool m_bConsole;
     void stopConsole();
-    int m_nPid;
     int m_nNesting;
 
     void addRecording();
@@ -48,10 +47,10 @@ public:
      * Similar to the DEBUG macro in sal/log.hxx, don't forget to remove these lines before
      * committing.
      */
-    ProfileZone(const char* sName, bool bConsole = false)
-        : NamedEvent(sName)
+    ProfileZone(const char* sName, bool bConsole = false,
+                const std::map<OUString, OUString>& args = std::map<OUString, OUString>())
+        : NamedEvent(sName, args)
         , m_bConsole(bConsole)
-        , m_nPid(-1)
         , m_nNesting(-1)
     {
         if (s_bRecording || m_bConsole)
@@ -61,8 +60,6 @@ public:
             m_nCreateTime
                 = static_cast<long long>(systemTime.Seconds) * 1000000 + systemTime.Nanosec / 1000;
 
-            m_nPid = getPid();
-
             m_nNesting = s_nNesting++;
         }
         else
diff --git a/include/comphelper/traceevent.hxx b/include/comphelper/traceevent.hxx
index 44805d21a6df..73c87e3767ac 100644
--- a/include/comphelper/traceevent.hxx
+++ b/include/comphelper/traceevent.hxx
@@ -14,6 +14,7 @@
 
 #include <algorithm>
 #include <atomic>
+#include <map>
 #include <memory>
 #include <vector>
 
@@ -22,6 +23,7 @@
 #include <osl/time.h>
 #include <com/sun/star/uno/Sequence.h>
 #include <comphelper/comphelperdllapi.h>
+#include <rtl/ustrbuf.hxx>
 #include <rtl/ustring.hxx>
 
 // implementation of XToolkitExperimental profiling API
@@ -30,6 +32,17 @@ namespace comphelper
 {
 class COMPHELPER_DLLPUBLIC TraceEvent
 {
+private:
+    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;
+    }
+
 protected:
     static std::atomic<bool> s_bRecording; // true during recording
 
@@ -42,18 +55,42 @@ protected:
         return static_cast<long long>(systemTime.Seconds) * 1000000 + systemTime.Nanosec / 1000;
     }
 
-    static int getPid()
+    static OUString createArgsString(const std::map<OUString, OUString>& args)
+    {
+        if (args.size() == 0)
+            return "";
+
+        OUStringBuffer sResult;
+        sResult.append(",\"args\":{");
+        bool first = true;
+        for (auto& i : args)
+        {
+            if (!first)
+                sResult.append(',');
+            sResult.append('"');
+            sResult.append(i.first);
+            sResult.append("\",\"");
+            sResult.append(i.second);
+            sResult.append('"');
+            first = false;
+        }
+        sResult.append('}');
+
+        return sResult.makeStringAndClear();
+    }
+
+    const int m_nPid;
+    const OUString m_sArgs;
+
+    TraceEvent(std::map<OUString, OUString> args)
+        : m_nPid(getPid())
+        , m_sArgs(createArgsString(args))
     {
-        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 addInstantEvent(const char* sName, const std::map<OUString, OUString>& args
+                                                   = std::map<OUString, OUString>());
 
     static void startRecording();
     static void stopRecording();
@@ -68,8 +105,10 @@ class COMPHELPER_DLLPUBLIC NamedEvent : public TraceEvent
 protected:
     const char* m_sName;
 
-    NamedEvent(const char* sName)
-        : m_sName(sName ? sName : "(null)")
+    NamedEvent(const char* sName,
+               const std::map<OUString, OUString>& args = std::map<OUString, OUString>())
+        : TraceEvent(args)
+        , m_sName(sName ? sName : "(null)")
     {
     }
 };
@@ -99,13 +138,12 @@ class COMPHELPER_DLLPUBLIC AsyncEvent : public NamedEvent,
 {
     static int s_nIdCounter;
     int m_nId;
-    int m_nPid;
     std::vector<std::shared_ptr<AsyncEvent>> m_aChildren;
     std::weak_ptr<AsyncEvent> m_pParent;
     bool m_bBeginRecorded;
 
-    AsyncEvent(const char* sName, int nId)
-        : NamedEvent(sName)
+    AsyncEvent(const char* sName, int nId, const std::map<OUString, OUString>& args)
+        : NamedEvent(sName, args)
         , m_nId(nId)
         , m_bBeginRecorded(false)
     {
@@ -113,8 +151,6 @@ class COMPHELPER_DLLPUBLIC AsyncEvent : public NamedEvent,
         {
             long long nNow = getNow();
 
-            m_nPid = getPid();
-
             // Generate a "Begin " (type b) event
             TraceEvent::addRecording("{"
                                      "\"name\":\""
@@ -123,7 +159,7 @@ class COMPHELPER_DLLPUBLIC AsyncEvent : public NamedEvent,
                                        "\"ph\":\"b\""
                                        ","
                                        "\"id\":"
-                                     + OUString::number(m_nId)
+                                     + OUString::number(m_nId) + m_sArgs
                                      + ","
                                        "\"ts\":"
                                      + OUString::number(nNow)
@@ -160,7 +196,7 @@ class COMPHELPER_DLLPUBLIC AsyncEvent : public NamedEvent,
                                        "\"ph\":\"e\""
                                        ","
                                        "\"id\":"
-                                     + OUString::number(m_nId)
+                                     + OUString::number(m_nId) + m_sArgs
                                      + ","
                                        "\"ts\":"
                                      + OUString::number(nNow)
@@ -174,21 +210,23 @@ class COMPHELPER_DLLPUBLIC AsyncEvent : public NamedEvent,
     }
 
 public:
-    AsyncEvent(const char* sName)
-        : AsyncEvent(sName, s_nIdCounter++)
+    AsyncEvent(const char* sName,
+               const std::map<OUString, OUString>& args = std::map<OUString, OUString>())
+        : AsyncEvent(sName, s_nIdCounter++, args)
     {
     }
 
     ~AsyncEvent() { generateEnd(); }
 
-    static std::weak_ptr<AsyncEvent> createWithParent(const char* sName,
-                                                      std::shared_ptr<AsyncEvent> pParent)
+    static std::weak_ptr<AsyncEvent>
+    createWithParent(const char* sName, std::shared_ptr<AsyncEvent> pParent,
+                     const std::map<OUString, OUString>& args = std::map<OUString, OUString>())
     {
         std::shared_ptr<AsyncEvent> pResult;
 
         if (s_bRecording && pParent->m_bBeginRecorded)
         {
-            pResult.reset(new AsyncEvent(sName, pParent->m_nId));
+            pResult.reset(new AsyncEvent(sName, pParent->m_nId, args));
             pParent->m_aChildren.push_back(pResult);
             pResult->m_pParent = pParent;
         }


More information about the Libreoffice-commits mailing list