Added a way to set thread name in profiling captures
This commit is contained in:
@@ -63,6 +63,8 @@ namespace profiling
|
|||||||
if (bufferSizeinMBytes < MinBufferSizeInMBytes)
|
if (bufferSizeinMBytes < MinBufferSizeInMBytes)
|
||||||
throw LmsException{ "Profiler must be configured with at least " + std::to_string(MinBufferSizeInMBytes) + " MBytes" };
|
throw LmsException{ "Profiler must be configured with at least " + std::to_string(MinBufferSizeInMBytes) + " MBytes" };
|
||||||
|
|
||||||
|
setThreadName(_creatorThreadId, "MainThread");
|
||||||
|
|
||||||
for (Buffer& buffer : _buffers)
|
for (Buffer& buffer : _buffers)
|
||||||
_freeBuffers.push_back(&buffer);
|
_freeBuffers.push_back(&buffer);
|
||||||
|
|
||||||
@@ -124,16 +126,30 @@ namespace profiling
|
|||||||
os << "{" << std::endl;
|
os << "{" << std::endl;
|
||||||
os << "\t\"traceEvents\": [" << std::endl;
|
os << "\t\"traceEvents\": [" << std::endl;
|
||||||
|
|
||||||
// we allow threads to fill in their current block while dumping
|
bool first{ true };
|
||||||
|
|
||||||
{
|
{
|
||||||
|
std::scoped_lock lock{ _threadNameMutex };
|
||||||
|
|
||||||
|
for (const auto& [threadId, threadName] : _threadNames)
|
||||||
|
{
|
||||||
|
if (first)
|
||||||
|
first = false;
|
||||||
|
else
|
||||||
|
os << ", " << std::endl;
|
||||||
|
|
||||||
os << "\t\t{ ";
|
os << "\t\t{ ";
|
||||||
os << "\"name\" : \"thread_name\", ";
|
os << "\"name\" : \"thread_name\", ";
|
||||||
os << "\"pid\" : 1, ";
|
os << "\"pid\" : 1, ";
|
||||||
os << "\"tid\" : " << _creatorThreadId << ", ";
|
os << "\"tid\" : " << threadId << ", ";
|
||||||
os << "\"ph\" : \"M\", ";
|
os << "\"ph\" : \"M\", ";
|
||||||
os << "\"args\" : { \"name\" : \"MainThread\" }";
|
os << "\"args\" : { \"name\" : \"" + threadName + "\" }";
|
||||||
os << " }";
|
os << " }";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// we allow threads to fill in their current block while dumping
|
||||||
|
{
|
||||||
std::scoped_lock lock{ _mutex };
|
std::scoped_lock lock{ _mutex };
|
||||||
|
|
||||||
for (Buffer& buffer : _buffers)
|
for (Buffer& buffer : _buffers)
|
||||||
@@ -144,7 +160,11 @@ namespace profiling
|
|||||||
using clockMicro = std::chrono::duration<double, std::micro>;
|
using clockMicro = std::chrono::duration<double, std::micro>;
|
||||||
const CompleteEvent& event{ buffer.durationEvents[i] };
|
const CompleteEvent& event{ buffer.durationEvents[i] };
|
||||||
|
|
||||||
os << "," << std::endl;
|
if (first)
|
||||||
|
first = false;
|
||||||
|
else
|
||||||
|
os << ", " << std::endl;;
|
||||||
|
|
||||||
os << "\t\t{ ";
|
os << "\t\t{ ";
|
||||||
os << "\"name\" : \"" << event.name.c_str() << "\", ";
|
os << "\"name\" : \"" << event.name.c_str() << "\", ";
|
||||||
os << "\"cat\" : \"" << event.category.c_str() << "\", ";
|
os << "\"cat\" : \"" << event.category.c_str() << "\", ";
|
||||||
@@ -163,4 +183,10 @@ namespace profiling
|
|||||||
os << "\t\"meta_cpu_count\" : " << std::thread::hardware_concurrency() << std::endl;
|
os << "\t\"meta_cpu_count\" : " << std::thread::hardware_concurrency() << std::endl;
|
||||||
os << "}" << std::endl;
|
os << "}" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Profiler::setThreadName(std::thread::id id, std::string_view threadName)
|
||||||
|
{
|
||||||
|
std::scoped_lock lock{ _threadNameMutex };
|
||||||
|
_threadNames.emplace(id, threadName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -24,6 +24,7 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "utils/IProfiler.hpp"
|
#include "utils/IProfiler.hpp"
|
||||||
|
|
||||||
@@ -40,6 +41,7 @@ namespace profiling
|
|||||||
bool isLevelActive(Level level) const override;
|
bool isLevelActive(Level level) const override;
|
||||||
void write(const CompleteEvent& event) override;
|
void write(const CompleteEvent& event) override;
|
||||||
void dumpCurrentBuffer(std::ostream& os) override;
|
void dumpCurrentBuffer(std::ostream& os) override;
|
||||||
|
void setThreadName(std::thread::id id, std::string_view threadName) override;
|
||||||
|
|
||||||
static constexpr std::size_t BufferSize{ 32 * 1024 };
|
static constexpr std::size_t BufferSize{ 32 * 1024 };
|
||||||
|
|
||||||
@@ -60,6 +62,9 @@ namespace profiling
|
|||||||
|
|
||||||
std::vector<Buffer> _buffers; // allocated once during construction
|
std::vector<Buffer> _buffers; // allocated once during construction
|
||||||
|
|
||||||
|
std::mutex _threadNameMutex;
|
||||||
|
std::unordered_map<std::thread::id, std::string> _threadNames;
|
||||||
|
|
||||||
std::mutex _mutex;
|
std::mutex _mutex;
|
||||||
std::deque<Buffer*> _freeBuffers;
|
std::deque<Buffer*> _freeBuffers;
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
#include <string_view>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include "LiteralString.hpp"
|
#include "LiteralString.hpp"
|
||||||
@@ -69,6 +70,7 @@ namespace profiling
|
|||||||
virtual bool isLevelActive(Level level) const = 0;
|
virtual bool isLevelActive(Level level) const = 0;
|
||||||
virtual void write(const CompleteEvent& entry) = 0;
|
virtual void write(const CompleteEvent& entry) = 0;
|
||||||
virtual void dumpCurrentBuffer(std::ostream& os) = 0;
|
virtual void dumpCurrentBuffer(std::ostream& os) = 0;
|
||||||
|
virtual void setThreadName(std::thread::id id, std::string_view threadName) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr std::size_t MinBufferSizeInMBytes = 16;
|
static constexpr std::size_t MinBufferSizeInMBytes = 16;
|
||||||
|
|||||||
Reference in New Issue
Block a user