Index: clang/test/Driver/check-time-trace.cpp =================================================================== --- clang/test/Driver/check-time-trace.cpp +++ 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 struct Struct { Index: lld/test/ELF/time-trace.s =================================================================== --- lld/test/ELF/time-trace.s +++ 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: Index: llvm/lib/Support/TimeProfiler.cpp =================================================================== --- llvm/lib/Support/TimeProfiler.cpp +++ llvm/lib/Support/TimeProfiler.cpp @@ -15,6 +15,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 #include @@ -73,10 +74,18 @@ } }; +static std::string getThreadName() { + SmallString<64> Name; + llvm::get_thread_name(Name); + return std::string(std::move(Name)); +} + struct TimeTraceProfiler { TimeTraceProfiler(unsigned TimeTraceGranularity = 0, StringRef ProcName = "") : StartTime(steady_clock::now()), ProcName(ProcName), - Tid(llvm::get_threadid()), TimeTraceGranularity(TimeTraceGranularity) {} + Pid(sys::Process::getProcessId()), ThreadName(getThreadName()), + Tid(llvm::get_threadid()), + TimeTraceGranularity(TimeTraceGranularity) {} void begin(std::string Name, llvm::function_ref Detail) { Stack.emplace_back(steady_clock::now(), TimePointType(), std::move(Name), @@ -141,7 +150,7 @@ auto DurUs = E.getFlameGraphDurUs(); J.object([&]{ - J.attribute("pid", 1); + J.attribute("pid", Pid); J.attribute("tid", int64_t(Tid)); J.attribute("ph", "X"); J.attribute("ts", StartUs); @@ -205,7 +214,7 @@ auto Count = AllCountAndTotalPerName[Total.first].first; J.object([&]{ - J.attribute("pid", 1); + J.attribute("pid", Pid); J.attribute("tid", int64_t(TotalTid)); J.attribute("ph", "X"); J.attribute("ts", 0); @@ -220,16 +229,25 @@ ++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, auto 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 auto &TTP : ThreadTimeTraceProfilerInstances) { + writeMetadataEvent("thread_name", TTP->Tid, TTP->ThreadName); + } J.arrayEnd(); J.attributeEnd(); @@ -241,6 +259,8 @@ StringMap CountAndTotalPerName; const TimePointType StartTime; const std::string ProcName; + const sys::Process::Pid Pid; + const std::string ThreadName; const uint64_t Tid; // Minimum time granularity (in microseconds)