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 #ifndef BASE_DEBUG_TRACE_EVENT_MEMORY_H_ |
| 6 #define BASE_DEBUG_TRACE_EVENT_MEMORY_H_ |
| 7 |
| 8 #include "base/base_export.h" |
| 9 #include "base/debug/trace_event_impl.h" |
| 10 #include "base/gtest_prod_util.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/timer/timer.h" |
| 14 |
| 15 namespace base { |
| 16 |
| 17 class MessageLoopProxy; |
| 18 |
| 19 namespace debug { |
| 20 |
| 21 // Watches for chrome://tracing to be enabled or disabled. When tracing is |
| 22 // enabled, also enables tcmalloc heap profiling. This class is the preferred |
| 23 // way to turn trace-base heap memory profiling on and off. |
| 24 class BASE_EXPORT TraceMemoryController |
| 25 : public TraceLog::EnabledStateObserver { |
| 26 public: |
| 27 // |message_loop_proxy| must be a proxy to the primary thread for the client |
| 28 // process, e.g. the UI thread in a browser. |
| 29 explicit TraceMemoryController( |
| 30 scoped_refptr<MessageLoopProxy> message_loop_proxy); |
| 31 virtual ~TraceMemoryController(); |
| 32 |
| 33 // base::debug::TraceLog::EnabledStateChangedObserver overrides: |
| 34 virtual void OnTraceLogEnabled() OVERRIDE; |
| 35 virtual void OnTraceLogDisabled() OVERRIDE; |
| 36 |
| 37 // Starts heap memory profiling. |
| 38 void StartProfiling(); |
| 39 |
| 40 // Ends heap memory profiling. |
| 41 void StopProfiling(); |
| 42 |
| 43 private: |
| 44 FRIEND_TEST_ALL_PREFIXES(TraceMemoryTest, TraceMemoryController); |
| 45 |
| 46 bool IsTimerRunningForTest() const; |
| 47 |
| 48 // Ensures the observer starts and stops tracing on the primary thread. |
| 49 scoped_refptr<MessageLoopProxy> message_loop_proxy_; |
| 50 |
| 51 // Timer to schedule memory profile dumps. |
| 52 RepeatingTimer<TraceMemoryController> dump_timer_; |
| 53 |
| 54 WeakPtrFactory<TraceMemoryController> weak_factory_; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(TraceMemoryController); |
| 57 }; |
| 58 |
| 59 ////////////////////////////////////////////////////////////////////////////// |
| 60 |
| 61 // A scoped context for memory tracing. Pushes the name onto a stack for |
| 62 // recording by tcmalloc heap profiling. |
| 63 class BASE_EXPORT ScopedTraceMemory { |
| 64 public: |
| 65 // Memory for |name| must be static, for example, a literal string in |
| 66 // a TRACE_EVENT macro. |
| 67 explicit ScopedTraceMemory(const char* name); |
| 68 ~ScopedTraceMemory(); |
| 69 |
| 70 // Testing interface: |
| 71 static void InitForTest(); |
| 72 static void CleanupForTest(); |
| 73 static int GetStackIndexForTest(); |
| 74 static const char* GetItemForTest(int index); |
| 75 |
| 76 private: |
| 77 DISALLOW_COPY_AND_ASSIGN(ScopedTraceMemory); |
| 78 }; |
| 79 |
| 80 ////////////////////////////////////////////////////////////////////////////// |
| 81 |
| 82 // Converts tcmalloc's heap profiler data with psuedo-stacks in |input| to |
| 83 // trace event compatible JSON and appends to |output|. Visible for testing. |
| 84 BASE_EXPORT void AppendHeapProfileAsTraceFormat(const char* input, |
| 85 std::string* output); |
| 86 |
| 87 // Converts the first |line| of heap profiler data, which contains totals for |
| 88 // all allocations in a special format, into trace event compatible JSON and |
| 89 // appends to |output|. Visible for testing. |
| 90 BASE_EXPORT void AppendHeapProfileTotalsAsTraceFormat(const std::string& line, |
| 91 std::string* output); |
| 92 |
| 93 // Converts a single |line| of heap profiler data into trace event compatible |
| 94 // JSON and appends to |output|. Returns true if the line was valid and has a |
| 95 // non-zero number of current allocations. Visible for testing. |
| 96 BASE_EXPORT bool AppendHeapProfileLineAsTraceFormat(const std::string& line, |
| 97 std::string* output); |
| 98 |
| 99 } // namespace debug |
| 100 } // namespace base |
| 101 |
| 102 // Make local variables with unique names based on the line number. Note that |
| 103 // the extra level of redirection is needed. |
| 104 #define INTERNAL_TRACE_MEMORY_ID3(line) trace_memory_unique_##line |
| 105 #define INTERNAL_TRACE_MEMORY_ID2(line) INTERNAL_TRACE_MEMORY_ID3(line) |
| 106 #define INTERNAL_TRACE_MEMORY_ID INTERNAL_TRACE_MEMORY_ID2(__LINE__) |
| 107 |
| 108 // Generates a unique local variable name. |
| 109 // TODO(jamescook): Make it record both category and name. |
| 110 #define INTERNAL_TRACE_MEMORY(category, name) \ |
| 111 base::debug::ScopedTraceMemory INTERNAL_TRACE_MEMORY_ID(name); |
| 112 |
| 113 // A special trace name that allows us to ignore memory allocations inside |
| 114 // the memory dump system itself. The allocations are recorded, but the |
| 115 // visualizer skips them. Must match the value in heap.js. |
| 116 #define TRACE_MEMORY_IGNORE "trace-memory-ignore" |
| 117 |
| 118 #endif // BASE_DEBUG_TRACE_EVENT_MEMORY_H_ |
OLD | NEW |