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

Side by Side 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: Rename and annotate. 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" 5 #include "base/trace_event/heap_profiler_allocation_context_tracker.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 9
10 #include "base/atomicops.h" 10 #include "base/atomicops.h"
11 #include "base/debug/leak_annotations.h"
12 #include "base/threading/platform_thread.h"
11 #include "base/threading/thread_local_storage.h" 13 #include "base/threading/thread_local_storage.h"
12 #include "base/trace_event/heap_profiler_allocation_context.h" 14 #include "base/trace_event/heap_profiler_allocation_context.h"
13 15
16 #if defined(OS_LINUX) || defined(OS_ANDROID)
17 #include <sys/prctl.h>
18 #endif
19
14 namespace base { 20 namespace base {
15 namespace trace_event { 21 namespace trace_event {
16 22
17 subtle::Atomic32 AllocationContextTracker::capture_mode_ = 23 subtle::Atomic32 AllocationContextTracker::capture_mode_ =
18 static_cast<int32_t>(AllocationContextTracker::CaptureMode::DISABLED); 24 static_cast<int32_t>(AllocationContextTracker::CaptureMode::DISABLED);
19 25
20 namespace { 26 namespace {
21 27
22 const size_t kMaxStackDepth = 128u; 28 const size_t kMaxStackDepth = 128u;
23 const size_t kMaxTaskDepth = 16u; 29 const size_t kMaxTaskDepth = 16u;
24 AllocationContextTracker* const kInitializingSentinel = 30 AllocationContextTracker* const kInitializingSentinel =
25 reinterpret_cast<AllocationContextTracker*>(-1); 31 reinterpret_cast<AllocationContextTracker*>(-1);
26 const char kTracingOverhead[] = "tracing_overhead"; 32 const char kTracingOverhead[] = "tracing_overhead";
27 33
28 ThreadLocalStorage::StaticSlot g_tls_alloc_ctx_tracker = TLS_INITIALIZER; 34 ThreadLocalStorage::StaticSlot g_tls_alloc_ctx_tracker = TLS_INITIALIZER;
29 35
30 // This function is added to the TLS slot to clean up the instance when the 36 // This function is added to the TLS slot to clean up the instance when the
31 // thread exits. 37 // thread exits.
32 void DestructAllocationContextTracker(void* alloc_ctx_tracker) { 38 void DestructAllocationContextTracker(void* alloc_ctx_tracker) {
33 delete static_cast<AllocationContextTracker*>(alloc_ctx_tracker); 39 delete static_cast<AllocationContextTracker*>(alloc_ctx_tracker);
34 } 40 }
35 41
42 // Cannot call ThreadIdNameManager::GetName because it holds a lock and causes
43 // deadlock when lock is already held by ThreadIdNameManager before the current
44 // allocation. Gets the thread name from kernel if available or returns a string
45 // with id. This function intenionally leaks the allocated strings since they
46 // are used to tag allocations even after the thread dies.
47 const char* GetAndLeakThreadName() {
48 char name[16];
49 #if defined(OS_LINUX) || defined(OS_ANDROID)
50 // If the thread name is not set, try to get it from prctl. Thread name might
51 // not be set in cases where the thread started before heap profiling was
52 // enabled.
53 int err = prctl(PR_GET_NAME, name);
54 if (!err) {
55 return strdup(name);
56 }
57 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
58
59 // Use tid if we don't have a thread name.
60 snprintf(name, sizeof(name), "%lu",
61 static_cast<unsigned long>(PlatformThread::CurrentId()));
62 return strdup(name);
63 }
64
36 } // namespace 65 } // namespace
37 66
38 // static 67 // static
39 AllocationContextTracker* 68 AllocationContextTracker*
40 AllocationContextTracker::GetInstanceForCurrentThread() { 69 AllocationContextTracker::GetInstanceForCurrentThread() {
41 AllocationContextTracker* tracker = 70 AllocationContextTracker* tracker =
42 static_cast<AllocationContextTracker*>(g_tls_alloc_ctx_tracker.Get()); 71 static_cast<AllocationContextTracker*>(g_tls_alloc_ctx_tracker.Get());
43 if (tracker == kInitializingSentinel) 72 if (tracker == kInitializingSentinel)
44 return nullptr; // Re-entrancy case. 73 return nullptr; // Re-entrancy case.
45 74
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 ctx.backtrace.frame_count = 1; 164 ctx.backtrace.frame_count = 1;
136 return ctx; 165 return ctx;
137 } 166 }
138 167
139 CaptureMode mode = static_cast<CaptureMode>( 168 CaptureMode mode = static_cast<CaptureMode>(
140 subtle::NoBarrier_Load(&capture_mode_)); 169 subtle::NoBarrier_Load(&capture_mode_));
141 170
142 auto backtrace = std::begin(ctx.backtrace.frames); 171 auto backtrace = std::begin(ctx.backtrace.frames);
143 auto backtrace_end = std::end(ctx.backtrace.frames); 172 auto backtrace_end = std::end(ctx.backtrace.frames);
144 173
145 // Add the thread name as the first entry 174 if (!thread_name_) {
175 // Ignore the string allocation made by GetAndLeakThreadName to avoid
176 // reentrancy.
177 ignore_scope_depth_++;
178 thread_name_ = GetAndLeakThreadName();
179 ANNOTATE_LEAKING_OBJECT_PTR(thread_name_);
180 DCHECK(thread_name_);
181 ignore_scope_depth_--;
182 }
183
184 // Add the thread name as the first entry in pseudo stack.
146 if (thread_name_) { 185 if (thread_name_) {
147 *backtrace++ = StackFrame::FromThreadName(thread_name_); 186 *backtrace++ = StackFrame::FromThreadName(thread_name_);
148 } 187 }
149 188
150 switch (mode) { 189 switch (mode) {
151 case CaptureMode::DISABLED: 190 case CaptureMode::DISABLED:
152 { 191 {
153 break; 192 break;
154 } 193 }
155 case CaptureMode::PSEUDO_STACK: 194 case CaptureMode::PSEUDO_STACK:
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 237
199 // TODO(ssid): Fix crbug.com/594803 to add file name as 3rd dimension 238 // TODO(ssid): Fix crbug.com/594803 to add file name as 3rd dimension
200 // (component name) in the heap profiler and not piggy back on the type name. 239 // (component name) in the heap profiler and not piggy back on the type name.
201 ctx.type_name = task_contexts_.empty() ? nullptr : task_contexts_.back(); 240 ctx.type_name = task_contexts_.empty() ? nullptr : task_contexts_.back();
202 241
203 return ctx; 242 return ctx;
204 } 243 }
205 244
206 } // namespace trace_event 245 } // namespace trace_event
207 } // namespace base 246 } // namespace base
OLDNEW
« 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