From a5e61184bf4055f403a3b9064c24fe7bd068373f Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Thu, 29 Dec 2016 16:34:49 -0800 Subject: [PATCH] Add support for adding thread names to a trace --- libraries/shared/src/Profile.cpp | 1 + libraries/shared/src/Profile.h | 6 ++++ libraries/shared/src/Trace.cpp | 59 ++++++++++++++++++++++---------- libraries/shared/src/Trace.h | 1 + 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/libraries/shared/src/Profile.cpp b/libraries/shared/src/Profile.cpp index f23c17c6be..b4f7ed8509 100644 --- a/libraries/shared/src/Profile.cpp +++ b/libraries/shared/src/Profile.cpp @@ -10,6 +10,7 @@ Q_LOGGING_CATEGORY(trace_app, "trace.app") Q_LOGGING_CATEGORY(trace_app_detail, "trace.app.detail") +Q_LOGGING_CATEGORY(trace_metadata, "trace.metadata") Q_LOGGING_CATEGORY(trace_network, "trace.network") Q_LOGGING_CATEGORY(trace_parse, "trace.parse") Q_LOGGING_CATEGORY(trace_render, "trace.render") diff --git a/libraries/shared/src/Profile.h b/libraries/shared/src/Profile.h index da786526a7..bbba240840 100644 --- a/libraries/shared/src/Profile.h +++ b/libraries/shared/src/Profile.h @@ -16,6 +16,7 @@ // When profiling something that may happen many times per frame, use a xxx_detail category so that they may easily be filtered out of trace results Q_DECLARE_LOGGING_CATEGORY(trace_app) Q_DECLARE_LOGGING_CATEGORY(trace_app_detail) +Q_DECLARE_LOGGING_CATEGORY(trace_metadata) Q_DECLARE_LOGGING_CATEGORY(trace_network) Q_DECLARE_LOGGING_CATEGORY(trace_render) Q_DECLARE_LOGGING_CATEGORY(trace_render_detail) @@ -69,6 +70,10 @@ inline void counter(const QLoggingCategory& category, const QString& name, const } } +inline void metadata(const QString& metadataType, const QVariantMap& args) { + tracing::traceEvent(trace_metadata(), metadataType, tracing::Metadata, "", args); +} + #define PROFILE_RANGE(category, name) Duration profileRangeThis(trace_##category(), name); #define PROFILE_RANGE_EX(category, name, argbColor, payload, ...) Duration profileRangeThis(trace_##category(), name, argbColor, (uint64_t)payload, ##__VA_ARGS__); #define PROFILE_RANGE_BEGIN(category, rangeId, name, argbColor) rangeId = Duration::beginRange(trace_##category(), name, argbColor) @@ -78,6 +83,7 @@ inline void counter(const QLoggingCategory& category, const QString& name, const #define PROFILE_COUNTER_IF_CHANGED(category, name, type, value) { static type lastValue = 0; type newValue = value; if (newValue != lastValue) { counter(trace_##category(), name, { { name, newValue }}); lastValue = newValue; } } #define PROFILE_COUNTER(category, name, ...) counter(trace_##category(), name, ##__VA_ARGS__); #define PROFILE_INSTANT(category, name, ...) instant(trace_##category(), name, ##__VA_ARGS__); +#define PROFILE_SET_THREAD_NAME(threadName) metadata("thread_name", { { "name", threadName } }); #define SAMPLE_PROFILE_RANGE(chance, category, name, ...) if (randFloat() <= chance) { PROFILE_RANGE(category, name); } #define SAMPLE_PROFILE_RANGE_EX(chance, category, name, ...) if (randFloat() <= chance) { PROFILE_RANGE_EX(category, name, argbColor, payload, ##__VA_ARGS__); } diff --git a/libraries/shared/src/Trace.cpp b/libraries/shared/src/Trace.cpp index e4ad70d912..1e3d490a9c 100644 --- a/libraries/shared/src/Trace.cpp +++ b/libraries/shared/src/Trace.cpp @@ -132,6 +132,9 @@ void Tracer::serialize(const QString& originalPath) { { std::lock_guard guard(_eventsMutex); currentEvents.swap(_events); + for (auto& event : _metadataEvents) { + currentEvents.push_back(event); + } } // If the file exists and we can't remove it, fail early @@ -194,33 +197,53 @@ void Tracer::serialize(const QString& originalPath) { #endif } -void Tracer::traceEvent(const QLoggingCategory& category, - const QString& name, EventType type, - qint64 timestamp, qint64 processID, qint64 threadID, - const QString& id, - const QVariantMap& args, const QVariantMap& extra) { +void Tracer::traceEvent(const QLoggingCategory& category, + const QString& name, EventType type, + qint64 timestamp, qint64 processID, qint64 threadID, + const QString& id, + const QVariantMap& args, const QVariantMap& extra) { std::lock_guard guard(_eventsMutex); - if (!_enabled) { + + // We always want to store metadata events even if tracing is not enabled so that when + // tracing is enabled we will be able to associate that metadata with that trace. + // Metadata events should be used sparingly - as of 12/30/16 the Chrome Tracing + // spec only supports thread+process metadata, so we should only expect to see metadata + // events created when a new thread or process is created. + if (!_enabled && type != Metadata) { return; } - _events.push_back({ - id, - name, - type, - timestamp, - processID, - threadID, - category, - args, - extra - }); + if (type == Metadata) { + _metadataEvents.push_back({ + id, + name, + type, + timestamp, + processID, + threadID, + category, + args, + extra + }); + } else { + _events.push_back({ + id, + name, + type, + timestamp, + processID, + threadID, + category, + args, + extra + }); + } } void Tracer::traceEvent(const QLoggingCategory& category, const QString& name, EventType type, const QString& id, const QVariantMap& args, const QVariantMap& extra) { - if (!_enabled) { + if (!_enabled && type != Metadata) { return; } diff --git a/libraries/shared/src/Trace.h b/libraries/shared/src/Trace.h index ee4f28f0ce..93e2c6c4c2 100644 --- a/libraries/shared/src/Trace.h +++ b/libraries/shared/src/Trace.h @@ -97,6 +97,7 @@ private: bool _enabled { false }; std::list _events; + std::list _metadataEvents; std::mutex _eventsMutex; };