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