diff options
author | Tor Lillqvist <tml@collabora.com> | 2021-06-02 09:15:54 +0300 |
---|---|---|
committer | Tor Lillqvist <tml@iki.fi> | 2021-06-02 10:18:24 +0300 |
commit | 0f156aefecba489f7e14c51ca6881b5404c0ee3b (patch) | |
tree | 47e6b1a6c576ac51226d00de47c0802ffc816bba | |
parent | Add the thread name also to Trace Events from the client without args (diff) | |
download | online-0f156aefecba489f7e14c51ca6881b5404c0ee3b.tar.gz online-0f156aefecba489f7e14c51ca6881b5404c0ee3b.zip |
Include the thread name in the Trace Events emitted from the C++ code, too
Make sure to avoid most of the code path through createArgsString() if
Trace Event generation is not turned on.
Signed-off-by: Tor Lillqvist <tml@collabora.com>
Change-Id: Id86b2c9b4ded9d402647b7a12b9290d0122ba2aa
-rw-r--r-- | common/TraceEvent.cpp | 42 | ||||
-rw-r--r-- | common/TraceEvent.hpp | 38 |
2 files changed, 49 insertions, 31 deletions
diff --git a/common/TraceEvent.cpp b/common/TraceEvent.cpp index cde80aabe7..1aba7720a1 100644 --- a/common/TraceEvent.cpp +++ b/common/TraceEvent.cpp @@ -20,27 +20,22 @@ thread_local int TraceEvent::threadLocalNesting = 0; // level of overlapped zone static std::mutex mutex; -void TraceEvent::emitInstantEvent(const std::string& name, const std::string& args) +void TraceEvent::emitInstantEvent(const std::string& name, const std::string& argsOrEmpty) { if (!recordingOn) return; -#ifdef TEST_TRACEEVENT_EXE - static thread_local int threadId = 0; - static int threadCounter = 1; - - if (!threadId) - threadId = threadCounter++; -#else - auto threadId = Util::getThreadId(); -#endif + std::string args; + if (argsOrEmpty.length() == 0) + args = createDefaultArgsString(); + else + args = argsOrEmpty; emitOneRecording("{" "\"name\":\"" + name + "\"," "\"ph\":\"i\"" - + args + ",\"ts\":" + std::to_string(std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::system_clock::now().time_since_epoch()) @@ -50,7 +45,9 @@ void TraceEvent::emitInstantEvent(const std::string& name, const std::string& ar + std::to_string(getpid()) + "," "\"tid\":" - + std::to_string(threadId) + + std::to_string(getThreadId()) + + ",\"args\":" + + args + "}" // We add a trailing comma and newline, it is up to the code that handles these "recordings" // (outputs them into a JSON array) to remove the final comma before adding the terminating @@ -70,19 +67,17 @@ void ProfileZone::emitRecording() { assert(recordingOn); -#ifdef TEST_TRACEEVENT_EXE - static thread_local int threadId = 0; - static int threadCounter = 1; - - if (!threadId) - threadId = threadCounter++; -#else - auto threadId = Util::getThreadId(); -#endif auto now = std::chrono::system_clock::now(); // Generate a single "Complete Event" (type X) auto duration = now - _createTime; + + std::string args; + if (_args.length() == 0) + args = createDefaultArgsString(); + else + args = _args; + std::string recordingData( "{" "\"name\":\"" @@ -101,8 +96,9 @@ void ProfileZone::emitRecording() + std::to_string(_pid) + "," "\"tid\":" - + std::to_string(threadId) - + _args + + std::to_string(getThreadId()) + + ",\"args\":" + + args + "}" // We emit a trailing comma and newline, it is up to the code that handles these "recordings" // (outputs them into a JSON array) to remove the final comma before emiting the terminating diff --git a/common/TraceEvent.hpp b/common/TraceEvent.hpp index 6316fbaa41..7e2fc3ac49 100644 --- a/common/TraceEvent.hpp +++ b/common/TraceEvent.hpp @@ -40,19 +40,40 @@ protected: std::string _args; thread_local static int threadLocalNesting; // For use only by the ProfileZone derived class - // Returns a string that can be inserted into the string representation of a JSON object between - // two other properties. The string is either completely empty, or starts with a comma and - // contains the property name "args" and as its value an object. + static long getThreadId() + { +#ifdef TEST_TRACEEVENT_EXE + static thread_local int threadId = 0; + static std::atomic<int> threadCounter(1); + + if (!threadId) + threadId = threadCounter++; + return threadId; +#else + return Util::getThreadId(); +#endif + } + + static std::string getThreadName() + { +#ifdef TEST_TRACEEVENT_EXE + return "thread-" + std::to_string(getThreadId()); +#else + return Util::getThreadName(); +#endif + } + + static std::string createDefaultArgsString() + { + return "{\"thread\":\"" + std::string(getThreadName()) + "\"}"; + } static std::string createArgsString(const std::map<std::string, std::string>& args) { if (!recordingOn) - return ""; - - if (args.size() == 0) - return ""; + return "0"; - std::string result = ",\"args\":{"; + std::string result = "{"; bool first = true; for (auto i : args) { @@ -65,6 +86,7 @@ protected: result += '"'; first = false; } + result += ",\"thread\":\"" + std::string(getThreadName()) + "\""; result += '}'; return result; |