Chromium Code Reviews| 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 TraceMemoryController( | |
|
jar (doing other things)
2013/07/04 02:18:30
nit: explicit?
James Cook
2013/07/08 23:21:34
Done.
| |
| 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 ScopedTraceMemory(const char* name); | |
|
jar (doing other things)
2013/07/04 02:18:30
nit: explicit
James Cook
2013/07/08 23:21:34
Done.
| |
| 68 ~ScopedTraceMemory(); | |
| 69 | |
| 70 // Testing interface: | |
| 71 static int GetStackIndexForTest(); | |
| 72 static const char* GetItemForTest(int index); | |
| 73 }; | |
|
jar (doing other things)
2013/07/04 02:18:30
nit: DISALLOW_COPY_AND_ASSIGN (despite the fact t
James Cook
2013/07/08 23:21:34
Done.
| |
| 74 | |
| 75 ////////////////////////////////////////////////////////////////////////////// | |
| 76 | |
| 77 // Converts tcmalloc's heap profiler data with psuedo-stacks in |input| to | |
| 78 // trace event compatible JSON and appends to |output|. Visible for testing. | |
| 79 BASE_EXPORT void AppendHeapProfileAsTraceFormat(const char* input, | |
| 80 std::string* output); | |
| 81 | |
| 82 // Converts the first |line| of heap profiler data, which contains totals for | |
| 83 // all allocations in a special format, into trace event compatible JSON and | |
| 84 // appends to |output|. Visible for testing. | |
| 85 BASE_EXPORT void AppendHeapProfileTotalsAsTraceFormat(const std::string& line, | |
| 86 std::string* output); | |
| 87 | |
| 88 // Converts a single |line| of heap profiler data into trace event compatible | |
| 89 // JSON and appends to |output|. Returns true if the line was valid and has a | |
| 90 // non-zero number of current allocations. Visible for testing. | |
| 91 BASE_EXPORT bool AppendHeapProfileLineAsTraceFormat(const std::string& line, | |
| 92 std::string* output); | |
| 93 | |
| 94 } // namespace debug | |
| 95 } // namespace base | |
| 96 | |
| 97 // Make local variables with unique names based on the line number. Note that | |
| 98 // the extra level of redirection is needed. | |
| 99 #define INTERNAL_TRACE_MEMORY_ID3(line) trace_memory_unique_##line | |
| 100 #define INTERNAL_TRACE_MEMORY_ID2(line) INTERNAL_TRACE_MEMORY_ID3(line) | |
| 101 #define INTERNAL_TRACE_MEMORY_ID INTERNAL_TRACE_MEMORY_ID2(__LINE__) | |
| 102 | |
| 103 // Generates a unique local variable name. | |
| 104 // TODO(jamescook): Make it record both category and name. | |
| 105 #define INTERNAL_TRACE_MEMORY(category, name) \ | |
| 106 base::debug::ScopedTraceMemory INTERNAL_TRACE_MEMORY_ID(name); | |
| 107 | |
| 108 // A special trace name that allows us to ignore memory allocations inside | |
| 109 // the memory dump system itself. The allocations are recorded, but the | |
| 110 // visualizer skips them. Must match the value in heap.js. | |
| 111 #define TRACE_MEMORY_IGNORE "trace-memory-ignore" | |
| 112 | |
| 113 #endif // BASE_DEBUG_TRACE_EVENT_MEMORY_H_ | |
| OLD | NEW |