Tracing: use only lower 32bits for thread, to make capture compatible with perfetto UI

This commit is contained in:
emeric
2024-03-31 13:07:02 +02:00
parent 9ca4ac073a
commit 4ba8330194
2 changed files with 25 additions and 2 deletions
+23 -2
View File
@@ -141,7 +141,7 @@ namespace lms::core::tracing
os << "\t\t{ ";
os << "\"name\" : \"thread_name\", ";
os << "\"pid\" : 1, ";
os << "\"tid\" : " << threadId << ", ";
os << "\"tid\" : " << toTraceThreadId(threadId) << ", ";
os << "\"ph\" : \"M\", ";
os << "\"args\" : { \"name\" : \"" + threadName + "\" }";
os << " }";
@@ -171,7 +171,7 @@ namespace lms::core::tracing
os << "\"name\" : \"" << event.name.c_str() << "\", ";
os << "\"cat\" : \"" << event.category.c_str() << "\", ";
os << "\"pid\": 1, ";
os << "\"tid\" : " << event.threadId << ", ";
os << "\"tid\" : " << toTraceThreadId(event.threadId) << ", ";
os << "\"ts\" : " << std::fixed << std::setprecision(3) << std::chrono::duration_cast<clockMicro>(event.start - _start).count() << ", ";
os << "\"dur\" : " << std::fixed << std::setprecision(3) << std::chrono::duration_cast<clockMicro>(event.duration).count() << ", ";
os << "\"ph\" : \"X\"";
@@ -198,4 +198,25 @@ namespace lms::core::tracing
std::scoped_lock lock{ _threadNameMutex };
_threadNames.emplace(id, threadName);
}
std::uint32_t TraceLogger::toTraceThreadId(std::thread::id threadId) const
{
{
auto it{ _cachedTraceThreadIds.find(threadId) };
if (it != std::cend(_cachedTraceThreadIds))
return it->second;
}
// Pefetto UI does not accept 64bits thread ids
std::ostringstream oss;
oss << threadId;
std::istringstream iss{ oss.str() };
std::uint64_t id;
iss >> id;
const std::uint32_t res{ static_cast<std::uint32_t>(id) };
_cachedTraceThreadIds.emplace(threadId, res);
return res;
}
}
+2
View File
@@ -42,6 +42,7 @@ namespace lms::core::tracing
void write(const CompleteEvent& event) override;
void dumpCurrentBuffer(std::ostream& os) override;
void setThreadName(std::thread::id id, std::string_view threadName) override;
std::uint32_t toTraceThreadId(std::thread::id threadId) const;
static constexpr std::size_t BufferSize{ 32 * 1024 };
@@ -64,6 +65,7 @@ namespace lms::core::tracing
std::mutex _threadNameMutex;
std::unordered_map<std::thread::id, std::string> _threadNames;
mutable std::unordered_map<std::thread::id, std::uint32_t> _cachedTraceThreadIds;
std::mutex _mutex;
std::deque<Buffer*> _freeBuffers;