[clang] a5bf028 - [TimeProfiler] Emit real process ID and thread names

Sergej Jaskiewicz via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 22 14:13:22 PDT 2020


Author: Sergej Jaskiewicz
Date: 2020-04-23T00:12:51+03:00
New Revision: a5bf02815d8b8d1a76682a63281561f076968dae

URL: https://github.com/llvm/llvm-project/commit/a5bf02815d8b8d1a76682a63281561f076968dae
DIFF: https://github.com/llvm/llvm-project/commit/a5bf02815d8b8d1a76682a63281561f076968dae.diff

LOG: [TimeProfiler] Emit real process ID and thread names

Differential Revision: https://reviews.llvm.org/D78027

Added: 
    

Modified: 
    clang/test/Driver/check-time-trace.cpp
    lld/test/ELF/time-trace.s
    llvm/lib/Support/TimeProfiler.cpp

Removed: 
    


################################################################################
diff  --git a/clang/test/Driver/check-time-trace.cpp b/clang/test/Driver/check-time-trace.cpp
index bff2c1984daa..1484462b72bb 100644
--- a/clang/test/Driver/check-time-trace.cpp
+++ b/clang/test/Driver/check-time-trace.cpp
@@ -14,6 +14,7 @@
 // CHECK-NEXT: "ts":
 // CHECK: "name": "clang{{.*}}"
 // CHECK: "name": "process_name"
+// CHECK: "name": "thread_name"
 
 template <typename T>
 struct Struct {

diff  --git a/lld/test/ELF/time-trace.s b/lld/test/ELF/time-trace.s
index 74311a984110..8085a256649e 100644
--- a/lld/test/ELF/time-trace.s
+++ b/lld/test/ELF/time-trace.s
@@ -34,6 +34,7 @@
 # Check process_name entry field
 # CHECK: "name": "ld.lld{{(.exe)?}}"
 # CHECK: "name": "process_name"
+# CHECK: "name": "thread_name"
 
 .globl _start
 _start:

diff  --git a/llvm/lib/Support/TimeProfiler.cpp b/llvm/lib/Support/TimeProfiler.cpp
index 510f763b8fe3..c907c1aaf983 100644
--- a/llvm/lib/Support/TimeProfiler.cpp
+++ b/llvm/lib/Support/TimeProfiler.cpp
@@ -16,6 +16,7 @@
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/JSON.h"
 #include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
 #include "llvm/Support/Threading.h"
 #include <algorithm>
 #include <cassert>
@@ -75,7 +76,10 @@ struct Entry {
 struct llvm::TimeTraceProfiler {
   TimeTraceProfiler(unsigned TimeTraceGranularity = 0, StringRef ProcName = "")
       : StartTime(steady_clock::now()), ProcName(ProcName),
-        Tid(llvm::get_threadid()), TimeTraceGranularity(TimeTraceGranularity) {}
+        Pid(sys::Process::getProcessId()), Tid(llvm::get_threadid()),
+        TimeTraceGranularity(TimeTraceGranularity) {
+    llvm::get_thread_name(ThreadName);
+  }
 
   void begin(std::string Name, llvm::function_ref<std::string()> Detail) {
     Stack.emplace_back(steady_clock::now(), TimePointType(), std::move(Name),
@@ -138,8 +142,8 @@ struct llvm::TimeTraceProfiler {
       auto StartUs = E.getFlameGraphStartUs(StartTime);
       auto DurUs = E.getFlameGraphDurUs();
 
-      J.object([&]{
-        J.attribute("pid", 1);
+      J.object([&] {
+        J.attribute("pid", Pid);
         J.attribute("tid", int64_t(Tid));
         J.attribute("ph", "X");
         J.attribute("ts", StartUs);
@@ -194,8 +198,8 @@ struct llvm::TimeTraceProfiler {
       auto DurUs = duration_cast<microseconds>(Total.second.second).count();
       auto Count = AllCountAndTotalPerName[Total.first].first;
 
-      J.object([&]{
-        J.attribute("pid", 1);
+      J.object([&] {
+        J.attribute("pid", Pid);
         J.attribute("tid", int64_t(TotalTid));
         J.attribute("ph", "X");
         J.attribute("ts", 0);
@@ -210,16 +214,23 @@ struct llvm::TimeTraceProfiler {
       ++TotalTid;
     }
 
-    // Emit metadata event with process name.
-    J.object([&] {
-      J.attribute("cat", "");
-      J.attribute("pid", 1);
-      J.attribute("tid", 0);
-      J.attribute("ts", 0);
-      J.attribute("ph", "M");
-      J.attribute("name", "process_name");
-      J.attributeObject("args", [&] { J.attribute("name", ProcName); });
-    });
+    auto writeMetadataEvent = [&](const char *Name, uint64_t Tid,
+                                  StringRef arg) {
+      J.object([&] {
+        J.attribute("cat", "");
+        J.attribute("pid", Pid);
+        J.attribute("tid", int64_t(Tid));
+        J.attribute("ts", 0);
+        J.attribute("ph", "M");
+        J.attribute("name", Name);
+        J.attributeObject("args", [&] { J.attribute("name", arg); });
+      });
+    };
+
+    writeMetadataEvent("process_name", Tid, ProcName);
+    writeMetadataEvent("thread_name", Tid, ThreadName);
+    for (const TimeTraceProfiler *TTP : ThreadTimeTraceProfilerInstances)
+      writeMetadataEvent("thread_name", TTP->Tid, TTP->ThreadName);
 
     J.arrayEnd();
     J.attributeEnd();
@@ -231,6 +242,8 @@ struct llvm::TimeTraceProfiler {
   StringMap<CountAndDurationType> CountAndTotalPerName;
   const TimePointType StartTime;
   const std::string ProcName;
+  const sys::Process::Pid Pid;
+  SmallString<0> ThreadName;
   const uint64_t Tid;
 
   // Minimum time granularity (in microseconds)


        


More information about the cfe-commits mailing list