Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2297)

Unified Diff: base/trace_event/heap_profiler_allocation_context_tracker.cc

Issue 1958703002: [tracing] Fix mising thread names in heap profiler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use thread id. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/trace_event/heap_profiler_allocation_context_tracker_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/trace_event/heap_profiler_allocation_context_tracker.cc
diff --git a/base/trace_event/heap_profiler_allocation_context_tracker.cc b/base/trace_event/heap_profiler_allocation_context_tracker.cc
index fd4e21abe5d62d6594e6fd5bf56544228cce92f1..0cce94c0c039e05efba0223ad27205855b7be708 100644
--- a/base/trace_event/heap_profiler_allocation_context_tracker.cc
+++ b/base/trace_event/heap_profiler_allocation_context_tracker.cc
@@ -8,9 +8,14 @@
#include <iterator>
#include "base/atomicops.h"
+#include "base/threading/platform_thread.h"
#include "base/threading/thread_local_storage.h"
#include "base/trace_event/heap_profiler_allocation_context.h"
+#if defined(OS_LINUX) || defined(OS_ANDROID)
+#include <sys/prctl.h>
+#endif
+
namespace base {
namespace trace_event {
@@ -33,6 +38,26 @@ void DestructAllocationContextTracker(void* alloc_ctx_tracker) {
delete static_cast<AllocationContextTracker*>(alloc_ctx_tracker);
}
+// Gets the thread name from kernel if available or returns a string with id.
+// This function intenionally leaks the allocated strings since they are used to
+// tag allocations even after the thread dies.
+const char* GetThreadName() {
+ char name[50];
Dmitry Skiba 2016/05/16 21:11:10 Why 50? PRCTL needs 16 and longest thing %d (%lld)
ssid 2016/05/16 23:22:41 Used unsigned long - maximum is 12 chars.
+#if defined(OS_LINUX) || defined(OS_ANDROID)
+ // If the thread name is not set, try to get it from prctl. Thread name might
+ // not be set in cases where the thread started before heap profiling was
+ // enabled.
+ int err = prctl(PR_GET_NAME, name);
+ if (!err) {
+ return strdup(name);
+ }
+#endif // defined(OS_LINUX) || defined(OS_ANDROID)
+
+ // Use tid if we don't have a thread name.
+ snprintf(name, 50, "%x", PlatformThread::CurrentId());
Dmitry Skiba 2016/05/16 21:11:10 Better use sizeof(name) instead of 50. Also, I thi
ssid 2016/05/16 23:22:41 I was thinking dword was int32 so %x should still
+ return strdup(name);
+}
+
} // namespace
// static
@@ -142,7 +167,15 @@ AllocationContext AllocationContextTracker::GetContextSnapshot() {
auto backtrace = std::begin(ctx.backtrace.frames);
auto backtrace_end = std::end(ctx.backtrace.frames);
- // Add the thread name as the first entry
+ if (!thread_name_) {
+ // Ignore the string allocation made by GetThreadName to avoid reentrancy.
+ ignore_scope_depth_++;
+ thread_name_ = GetThreadName();
+ DCHECK(thread_name_);
+ ignore_scope_depth_--;
+ }
+
+ // Add the thread name as the first entry in pseudo stack.
if (thread_name_) {
*backtrace++ = StackFrame::FromThreadName(thread_name_);
}
« no previous file with comments | « no previous file | base/trace_event/heap_profiler_allocation_context_tracker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698