OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/debug/trace_memory.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/logging.h" //TODO remove |
| 9 #include "base/threading/thread_local.h" |
| 10 |
| 11 #if !defined(NO_TCMALLOC) && !defined(OS_NACL) && defined(OS_LINUX) |
| 12 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" |
| 13 #endif |
| 14 |
| 15 namespace base { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Maximum number of nested TRACE_MEMORY macros. |
| 20 const int kMaxStackSize = 32; |
| 21 |
| 22 // Records a stack of TRACE_MEMORY events. One per thread is required. |
| 23 struct TraceMemoryStack { |
| 24 TraceMemoryStack() : index_(0) { |
| 25 memset(category_stack_, 0, kMaxStackSize * sizeof(const char*)); |
| 26 } |
| 27 |
| 28 // Points to the next free entry. |
| 29 int index_; |
| 30 const char* category_stack_[kMaxStackSize]; |
| 31 }; |
| 32 |
| 33 // A stack of TRACE_MEMORY events, per thread. |
| 34 LazyInstance<ThreadLocalPointer<TraceMemoryStack> >::Leaky trace_memory_stack = |
| 35 LAZY_INSTANCE_INITIALIZER; |
| 36 |
| 37 // Returns a "pseudo-stack" of pointers to trace events. |
| 38 // TODO(jamescook): Record both category and name, perhaps in a pair for speed. |
| 39 int GetPseudoStack(void** stack_out) { |
| 40 // Is this a fast enough lookup? Should we avoid LazyInstance somehow? |
| 41 TraceMemoryStack* stack = trace_memory_stack.Get().Get(); |
| 42 // TODO - figure out how to initialize this on every thread so we don't need |
| 43 // this if(). |
| 44 if (!stack) |
| 45 return 0; // could also init the stack here |
| 46 const int count = stack->index_; |
| 47 // memcpy works for zero bytes. |
| 48 memcpy(stack_out, stack->category_stack_, count * sizeof(void*)); |
| 49 return count; |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
| 54 ScopedTraceMemory::ScopedTraceMemory(const char* category) { |
| 55 // Get our thread's copy of the stack. |
| 56 TraceMemoryStack* stack = trace_memory_stack.Get().Get(); |
| 57 if (!stack) { |
| 58 trace_memory_stack.Get().Set(new TraceMemoryStack); |
| 59 stack = trace_memory_stack.Get().Get(); |
| 60 } |
| 61 // Bounds checking is for the weak. |
| 62 const int index = stack->index_; |
| 63 stack->category_stack_[index] = category; |
| 64 stack->index_++; |
| 65 } |
| 66 |
| 67 ScopedTraceMemory::~ScopedTraceMemory() { |
| 68 // Get our thread's copy of the stack. |
| 69 TraceMemoryStack* stack = trace_memory_stack.Get().Get(); |
| 70 stack->index_--; |
| 71 } |
| 72 |
| 73 // static |
| 74 int ScopedTraceMemory::GetStackIndexForTest() { |
| 75 return trace_memory_stack.Get().Get()->index_; |
| 76 } |
| 77 |
| 78 void TraceMemoryStart() { |
| 79 LOG(ERROR) << "JAMESDEBUG starting trace memory"; |
| 80 // Ensure thread-local-storage is initialized by creating a dummy event. |
| 81 ScopedTraceMemory initialize(NULL); |
| 82 #if !defined(NO_TCMALLOC) && !defined(OS_NACL) && defined(OS_LINUX) |
| 83 ::SetPseudoStackGenerator(&GetPseudoStack); |
| 84 ::HeapProfilerStart("/tmp/trace-memory"); |
| 85 #endif |
| 86 } |
| 87 |
| 88 void TraceMemoryDump() { |
| 89 LOG(ERROR) << "JAMESDEBUG dumping trace memory"; |
| 90 #if !defined(NO_TCMALLOC) && !defined(OS_NACL) && defined(OS_LINUX) |
| 91 ::HeapProfilerDump("dump-for-trace-memory"); |
| 92 #endif |
| 93 } |
| 94 |
| 95 char* TraceMemoryDumpAsString() { |
| 96 LOG(ERROR) << "JAMESDEBUG getting memory dump as string"; |
| 97 // Ensure thread-local-storage is initialized by creating a dummy event. |
| 98 #if !defined(NO_TCMALLOC) && !defined(OS_NACL) && defined(OS_LINUX) |
| 99 return ::GetHeapProfile(); |
| 100 #endif |
| 101 return NULL; |
| 102 } |
| 103 |
| 104 void TraceMemoryStop() { |
| 105 LOG(ERROR) << "JAMESDEBUG stopping trace memory"; |
| 106 #if !defined(NO_TCMALLOC) && !defined(OS_NACL) && defined(OS_LINUX) |
| 107 ::HeapProfilerStop(); |
| 108 #endif |
| 109 } |
| 110 |
| 111 } // namespace base |
OLD | NEW |