Chromium Code Reviews| OLD | NEW |
|---|---|
| 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() { | |
|
Primiano Tucci (use gerrit)
2016/05/17 19:38:43
can you make this a void GetThreadName(char* out_n
Dmitry Skiba
2016/05/17 20:05:18
Let's simply call function LeakThreadName(), and p
Primiano Tucci (use gerrit)
2016/05/17 20:46:12
You could pass a std::string* as arugment and leak
ssid
2016/05/18 03:38:05
Changed the name and added ANNOTATE_LEAKING_OBJECT
| |
| 45 char name[16]; | |
| 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); | |
|
Primiano Tucci (use gerrit)
2016/05/17 19:38:44
once you move this, make sure you include leak_ann
ssid
2016/05/18 03:38:05
Done.
| |
| 53 } | |
| 54 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | |
| 55 | |
| 56 // Use tid if we don't have a thread name. | |
| 57 snprintf(name, sizeof(name), "%lu", | |
| 58 static_cast<unsigned long>(PlatformThread::CurrentId())); | |
| 59 return strdup(name); | |
|
Primiano Tucci (use gerrit)
2016/05/17 19:38:43
instead of strduping twice what you want to do bel
ssid
2016/05/18 03:38:05
Keepin strdup twice.
| |
| 60 } | |
| 61 | |
| 36 } // namespace | 62 } // namespace |
| 37 | 63 |
| 38 // static | 64 // static |
| 39 AllocationContextTracker* | 65 AllocationContextTracker* |
| 40 AllocationContextTracker::GetInstanceForCurrentThread() { | 66 AllocationContextTracker::GetInstanceForCurrentThread() { |
| 41 AllocationContextTracker* tracker = | 67 AllocationContextTracker* tracker = |
| 42 static_cast<AllocationContextTracker*>(g_tls_alloc_ctx_tracker.Get()); | 68 static_cast<AllocationContextTracker*>(g_tls_alloc_ctx_tracker.Get()); |
| 43 if (tracker == kInitializingSentinel) | 69 if (tracker == kInitializingSentinel) |
| 44 return nullptr; // Re-entrancy case. | 70 return nullptr; // Re-entrancy case. |
| 45 | 71 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 ctx.backtrace.frame_count = 1; | 161 ctx.backtrace.frame_count = 1; |
| 136 return ctx; | 162 return ctx; |
| 137 } | 163 } |
| 138 | 164 |
| 139 CaptureMode mode = static_cast<CaptureMode>( | 165 CaptureMode mode = static_cast<CaptureMode>( |
| 140 subtle::NoBarrier_Load(&capture_mode_)); | 166 subtle::NoBarrier_Load(&capture_mode_)); |
| 141 | 167 |
| 142 auto backtrace = std::begin(ctx.backtrace.frames); | 168 auto backtrace = std::begin(ctx.backtrace.frames); |
| 143 auto backtrace_end = std::end(ctx.backtrace.frames); | 169 auto backtrace_end = std::end(ctx.backtrace.frames); |
| 144 | 170 |
| 145 // Add the thread name as the first entry | 171 if (!thread_name_) { |
| 172 // Ignore the string allocation made by GetThreadName to avoid reentrancy. | |
| 173 ignore_scope_depth_++; | |
| 174 thread_name_ = GetThreadName(); | |
| 175 DCHECK(thread_name_); | |
| 176 ignore_scope_depth_--; | |
| 177 } | |
| 178 | |
| 179 // Add the thread name as the first entry in pseudo stack. | |
| 146 if (thread_name_) { | 180 if (thread_name_) { |
| 147 *backtrace++ = StackFrame::FromThreadName(thread_name_); | 181 *backtrace++ = StackFrame::FromThreadName(thread_name_); |
| 148 } | 182 } |
| 149 | 183 |
| 150 switch (mode) { | 184 switch (mode) { |
| 151 case CaptureMode::DISABLED: | 185 case CaptureMode::DISABLED: |
| 152 { | 186 { |
| 153 break; | 187 break; |
| 154 } | 188 } |
| 155 case CaptureMode::PSEUDO_STACK: | 189 case CaptureMode::PSEUDO_STACK: |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 | 232 |
| 199 // TODO(ssid): Fix crbug.com/594803 to add file name as 3rd dimension | 233 // 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. | 234 // (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(); | 235 ctx.type_name = task_contexts_.empty() ? nullptr : task_contexts_.back(); |
| 202 | 236 |
| 203 return ctx; | 237 return ctx; |
| 204 } | 238 } |
| 205 | 239 |
| 206 } // namespace trace_event | 240 } // namespace trace_event |
| 207 } // namespace base | 241 } // namespace base |
| OLD | NEW |