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

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: 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 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/threading/platform_thread.h"
11 #include "base/threading/thread_local_storage.h" 12 #include "base/threading/thread_local_storage.h"
12 #include "base/trace_event/heap_profiler_allocation_context.h" 13 #include "base/trace_event/heap_profiler_allocation_context.h"
13 14
15 #if defined(OS_LINUX) || defined(OS_ANDROID)
16 #include <sys/prctl.h>
17 #endif
18
14 namespace base { 19 namespace base {
15 namespace trace_event { 20 namespace trace_event {
16 21
17 subtle::Atomic32 AllocationContextTracker::capture_mode_ = 22 subtle::Atomic32 AllocationContextTracker::capture_mode_ =
18 static_cast<int32_t>(AllocationContextTracker::CaptureMode::DISABLED); 23 static_cast<int32_t>(AllocationContextTracker::CaptureMode::DISABLED);
19 24
20 namespace { 25 namespace {
21 26
22 const size_t kMaxStackDepth = 128u; 27 const size_t kMaxStackDepth = 128u;
23 const size_t kMaxTaskDepth = 16u; 28 const size_t kMaxTaskDepth = 16u;
24 AllocationContextTracker* const kInitializingSentinel = 29 AllocationContextTracker* const kInitializingSentinel =
25 reinterpret_cast<AllocationContextTracker*>(-1); 30 reinterpret_cast<AllocationContextTracker*>(-1);
26 const char kTracingOverhead[] = "tracing_overhead"; 31 const char kTracingOverhead[] = "tracing_overhead";
27 32
28 ThreadLocalStorage::StaticSlot g_tls_alloc_ctx_tracker = TLS_INITIALIZER; 33 ThreadLocalStorage::StaticSlot g_tls_alloc_ctx_tracker = TLS_INITIALIZER;
29 34
30 // This function is added to the TLS slot to clean up the instance when the 35 // This function is added to the TLS slot to clean up the instance when the
31 // thread exits. 36 // thread exits.
32 void DestructAllocationContextTracker(void* alloc_ctx_tracker) { 37 void DestructAllocationContextTracker(void* alloc_ctx_tracker) {
33 delete static_cast<AllocationContextTracker*>(alloc_ctx_tracker); 38 delete static_cast<AllocationContextTracker*>(alloc_ctx_tracker);
34 } 39 }
35 40
41 // Gets the thread name from kernel if available or returns a string with id.
42 // This function intenionally leaks the allocated strings since they are used to
43 // tag allocations even after the thread dies.
44 const char* GetThreadName() {
45 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.
46 #if defined(OS_LINUX) || defined(OS_ANDROID)
47 // If the thread name is not set, try to get it from prctl. Thread name might
48 // not be set in cases where the thread started before heap profiling was
49 // enabled.
50 int err = prctl(PR_GET_NAME, name);
51 if (!err) {
52 return strdup(name);
53 }
54 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
55
56 // Use tid if we don't have a thread name.
57 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
58 return strdup(name);
59 }
60
36 } // namespace 61 } // namespace
37 62
38 // static 63 // static
39 AllocationContextTracker* 64 AllocationContextTracker*
40 AllocationContextTracker::GetInstanceForCurrentThread() { 65 AllocationContextTracker::GetInstanceForCurrentThread() {
41 AllocationContextTracker* tracker = 66 AllocationContextTracker* tracker =
42 static_cast<AllocationContextTracker*>(g_tls_alloc_ctx_tracker.Get()); 67 static_cast<AllocationContextTracker*>(g_tls_alloc_ctx_tracker.Get());
43 if (tracker == kInitializingSentinel) 68 if (tracker == kInitializingSentinel)
44 return nullptr; // Re-entrancy case. 69 return nullptr; // Re-entrancy case.
45 70
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 ctx.backtrace.frame_count = 1; 160 ctx.backtrace.frame_count = 1;
136 return ctx; 161 return ctx;
137 } 162 }
138 163
139 CaptureMode mode = static_cast<CaptureMode>( 164 CaptureMode mode = static_cast<CaptureMode>(
140 subtle::NoBarrier_Load(&capture_mode_)); 165 subtle::NoBarrier_Load(&capture_mode_));
141 166
142 auto backtrace = std::begin(ctx.backtrace.frames); 167 auto backtrace = std::begin(ctx.backtrace.frames);
143 auto backtrace_end = std::end(ctx.backtrace.frames); 168 auto backtrace_end = std::end(ctx.backtrace.frames);
144 169
145 // Add the thread name as the first entry 170 if (!thread_name_) {
171 // Ignore the string allocation made by GetThreadName to avoid reentrancy.
172 ignore_scope_depth_++;
173 thread_name_ = GetThreadName();
174 DCHECK(thread_name_);
175 ignore_scope_depth_--;
176 }
177
178 // Add the thread name as the first entry in pseudo stack.
146 if (thread_name_) { 179 if (thread_name_) {
147 *backtrace++ = StackFrame::FromThreadName(thread_name_); 180 *backtrace++ = StackFrame::FromThreadName(thread_name_);
148 } 181 }
149 182
150 switch (mode) { 183 switch (mode) {
151 case CaptureMode::DISABLED: 184 case CaptureMode::DISABLED:
152 { 185 {
153 break; 186 break;
154 } 187 }
155 case CaptureMode::PSEUDO_STACK: 188 case CaptureMode::PSEUDO_STACK:
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 231
199 // TODO(ssid): Fix crbug.com/594803 to add file name as 3rd dimension 232 // 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. 233 // (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(); 234 ctx.type_name = task_contexts_.empty() ? nullptr : task_contexts_.back();
202 235
203 return ctx; 236 return ctx;
204 } 237 }
205 238
206 } // namespace trace_event 239 } // namespace trace_event
207 } // namespace base 240 } // 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