Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / base / trace_event / trace_event_memory.h
blob2e8fb5d4810f2c19b516de08084b041c789df799
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.
5 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_H_
6 #define BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_H_
8 #include "base/base_export.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/timer/timer.h"
13 #include "base/trace_event/trace_log.h"
15 // TODO(jamescook): Windows support for memory tracing.
16 #if !defined(NO_TCMALLOC) && !defined(OS_NACL) && \
17 (defined(OS_LINUX) || defined(OS_ANDROID))
18 #define TCMALLOC_TRACE_MEMORY_SUPPORTED 1
19 #endif
21 namespace base {
23 class SingleThreadTaskRunner;
25 namespace trace_event {
27 // Watches for chrome://tracing to be enabled or disabled. When tracing is
28 // enabled, also enables tcmalloc heap profiling. This class is the preferred
29 // way to turn trace-base heap memory profiling on and off.
30 class BASE_EXPORT TraceMemoryController
31 : public TraceLog::EnabledStateObserver {
32 public:
33 typedef int (*StackGeneratorFunction)(int skip_count, void** stack);
34 typedef void (*HeapProfilerStartFunction)(StackGeneratorFunction callback);
35 typedef void (*HeapProfilerStopFunction)();
36 typedef char* (*GetHeapProfileFunction)();
38 // |task_runner| must be a task runner for the primary thread for the client
39 // process, e.g. the UI thread in a browser. The function pointers must be
40 // pointers to tcmalloc heap profiling functions; by avoiding direct calls to
41 // these functions we avoid a dependency on third_party/tcmalloc from base.
42 TraceMemoryController(scoped_refptr<SingleThreadTaskRunner> task_runner,
43 HeapProfilerStartFunction heap_profiler_start_function,
44 HeapProfilerStopFunction heap_profiler_stop_function,
45 GetHeapProfileFunction get_heap_profile_function);
46 ~TraceMemoryController() override;
48 // base::trace_event::TraceLog::EnabledStateChangedObserver overrides:
49 void OnTraceLogEnabled() override;
50 void OnTraceLogDisabled() override;
52 // Starts heap memory profiling.
53 void StartProfiling();
55 // Captures a heap profile.
56 void DumpMemoryProfile();
58 // If memory tracing is enabled, dumps a memory profile to the tracing system.
59 void StopProfiling();
61 private:
62 FRIEND_TEST_ALL_PREFIXES(TraceMemoryTest, TraceMemoryController);
64 bool IsTimerRunningForTest() const;
66 // Ensures the observer starts and stops tracing on the primary thread.
67 scoped_refptr<SingleThreadTaskRunner> task_runner_;
69 // Pointers to tcmalloc heap profiling functions. Allows this class to use
70 // tcmalloc functions without introducing a dependency from base to tcmalloc.
71 HeapProfilerStartFunction heap_profiler_start_function_;
72 HeapProfilerStopFunction heap_profiler_stop_function_;
73 GetHeapProfileFunction get_heap_profile_function_;
75 // Timer to schedule memory profile dumps.
76 RepeatingTimer<TraceMemoryController> dump_timer_;
78 WeakPtrFactory<TraceMemoryController> weak_factory_;
80 DISALLOW_COPY_AND_ASSIGN(TraceMemoryController);
83 //////////////////////////////////////////////////////////////////////////////
85 // A scoped context for memory tracing. Pushes the name onto a stack for
86 // recording by tcmalloc heap profiling.
87 class BASE_EXPORT ScopedTraceMemory {
88 public:
89 struct ScopeData {
90 const char* category;
91 const char* name;
94 // Memory for |category| and |name| must be static, for example, literal
95 // strings in a TRACE_EVENT macro.
96 ScopedTraceMemory(const char* category, const char* name) {
97 if (!enabled_)
98 return;
99 Initialize(category, name);
101 ~ScopedTraceMemory() {
102 if (!enabled_)
103 return;
104 Destroy();
107 // Enables the storing of trace names on a per-thread stack.
108 static void set_enabled(bool enabled) { enabled_ = enabled; }
110 // Testing interface:
111 static void InitForTest();
112 static void CleanupForTest();
113 static int GetStackDepthForTest();
114 static ScopeData GetScopeDataForTest(int stack_index);
116 private:
117 void Initialize(const char* category, const char* name);
118 void Destroy();
120 static bool enabled_;
121 DISALLOW_COPY_AND_ASSIGN(ScopedTraceMemory);
124 //////////////////////////////////////////////////////////////////////////////
126 // Converts tcmalloc's heap profiler data with pseudo-stacks in |input| to
127 // trace event compatible JSON and appends to |output|. Visible for testing.
128 BASE_EXPORT void AppendHeapProfileAsTraceFormat(const char* input,
129 std::string* output);
131 // Converts the first |line| of heap profiler data, which contains totals for
132 // all allocations in a special format, into trace event compatible JSON and
133 // appends to |output|. Visible for testing.
134 BASE_EXPORT void AppendHeapProfileTotalsAsTraceFormat(const std::string& line,
135 std::string* output);
137 // Converts a single |line| of heap profiler data into trace event compatible
138 // JSON and appends to |output|. Returns true if the line was valid and has a
139 // non-zero number of current allocations. Visible for testing.
140 BASE_EXPORT bool AppendHeapProfileLineAsTraceFormat(const std::string& line,
141 std::string* output);
143 // Returns a pointer to a string given its hexadecimal address in |hex_address|.
144 // Handles both 32-bit and 64-bit addresses. Returns "null" for null pointers
145 // and "error" if |address| could not be parsed. Visible for testing.
146 BASE_EXPORT const char* StringFromHexAddress(const std::string& hex_address);
148 } // namespace trace_event
149 } // namespace base
151 // Make local variables with unique names based on the line number. Note that
152 // the extra level of redirection is needed.
153 #define INTERNAL_TRACE_MEMORY_ID3(line) trace_memory_unique_##line
154 #define INTERNAL_TRACE_MEMORY_ID2(line) INTERNAL_TRACE_MEMORY_ID3(line)
155 #define INTERNAL_TRACE_MEMORY_ID INTERNAL_TRACE_MEMORY_ID2(__LINE__)
157 // This is the core macro that adds a scope to each TRACE_EVENT location.
158 // It generates a unique local variable name using the macros above.
159 #if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED)
160 #define INTERNAL_TRACE_MEMORY(category, name) \
161 base::trace_event::ScopedTraceMemory INTERNAL_TRACE_MEMORY_ID(category, name);
162 #else
163 #define INTERNAL_TRACE_MEMORY(category, name)
164 #endif // defined(TRACE_MEMORY_SUPPORTED)
166 // A special trace name that allows us to ignore memory allocations inside
167 // the memory dump system itself. The allocations are recorded, but the
168 // visualizer skips them. Must match the value in heap.js.
169 #define TRACE_MEMORY_IGNORE "trace-memory-ignore"
171 #endif // BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_H_