OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_H_ | 5 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_H_ |
6 #define BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_H_ | 6 #define BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/gtest_prod_util.h" | 9 #include "base/gtest_prod_util.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
13 #include "base/trace_event/trace_event_impl.h" | 13 #include "base/trace_event/trace_event_impl.h" |
14 | 14 |
15 // TODO(jamescook): Windows support for memory tracing. | 15 // TODO(jamescook): Windows support for memory tracing. |
16 #if !defined(NO_TCMALLOC) && !defined(OS_NACL) && \ | 16 #if !defined(NO_TCMALLOC) && !defined(OS_NACL) && \ |
17 (defined(OS_LINUX) || defined(OS_ANDROID)) | 17 (defined(OS_LINUX) || defined(OS_ANDROID)) |
18 #define TCMALLOC_TRACE_MEMORY_SUPPORTED 1 | 18 #define TCMALLOC_TRACE_MEMORY_SUPPORTED 1 |
19 #endif | 19 #endif |
20 | 20 |
21 namespace base { | 21 namespace base { |
22 | 22 |
23 class MessageLoopProxy; | 23 class MessageLoopProxy; |
24 | 24 |
25 namespace debug { | 25 namespace trace_event { |
26 | 26 |
27 // Watches for chrome://tracing to be enabled or disabled. When tracing is | 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 | 28 // enabled, also enables tcmalloc heap profiling. This class is the preferred |
29 // way to turn trace-base heap memory profiling on and off. | 29 // way to turn trace-base heap memory profiling on and off. |
30 class BASE_EXPORT TraceMemoryController | 30 class BASE_EXPORT TraceMemoryController |
31 : public TraceLog::EnabledStateObserver { | 31 : public TraceLog::EnabledStateObserver { |
32 public: | 32 public: |
33 typedef int (*StackGeneratorFunction)(int skip_count, void** stack); | 33 typedef int (*StackGeneratorFunction)(int skip_count, void** stack); |
34 typedef void (*HeapProfilerStartFunction)(StackGeneratorFunction callback); | 34 typedef void (*HeapProfilerStartFunction)(StackGeneratorFunction callback); |
35 typedef void (*HeapProfilerStopFunction)(); | 35 typedef void (*HeapProfilerStopFunction)(); |
36 typedef char* (*GetHeapProfileFunction)(); | 36 typedef char* (*GetHeapProfileFunction)(); |
37 | 37 |
38 // |message_loop_proxy| must be a proxy to the primary thread for the client | 38 // |message_loop_proxy| must be a proxy to the primary thread for the client |
39 // process, e.g. the UI thread in a browser. The function pointers must be | 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 | 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. | 41 // these functions we avoid a dependency on third_party/tcmalloc from base. |
42 TraceMemoryController( | 42 TraceMemoryController( |
43 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 43 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
44 HeapProfilerStartFunction heap_profiler_start_function, | 44 HeapProfilerStartFunction heap_profiler_start_function, |
45 HeapProfilerStopFunction heap_profiler_stop_function, | 45 HeapProfilerStopFunction heap_profiler_stop_function, |
46 GetHeapProfileFunction get_heap_profile_function); | 46 GetHeapProfileFunction get_heap_profile_function); |
47 virtual ~TraceMemoryController(); | 47 virtual ~TraceMemoryController(); |
48 | 48 |
49 // base::debug::TraceLog::EnabledStateChangedObserver overrides: | 49 // base::trace_event::TraceLog::EnabledStateChangedObserver overrides: |
50 void OnTraceLogEnabled() override; | 50 void OnTraceLogEnabled() override; |
51 void OnTraceLogDisabled() override; | 51 void OnTraceLogDisabled() override; |
52 | 52 |
53 // Starts heap memory profiling. | 53 // Starts heap memory profiling. |
54 void StartProfiling(); | 54 void StartProfiling(); |
55 | 55 |
56 // Captures a heap profile. | 56 // Captures a heap profile. |
57 void DumpMemoryProfile(); | 57 void DumpMemoryProfile(); |
58 | 58 |
59 // If memory tracing is enabled, dumps a memory profile to the tracing system. | 59 // If memory tracing is enabled, dumps a memory profile to the tracing system. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 // JSON and appends to |output|. Returns true if the line was valid and has a | 139 // JSON and appends to |output|. Returns true if the line was valid and has a |
140 // non-zero number of current allocations. Visible for testing. | 140 // non-zero number of current allocations. Visible for testing. |
141 BASE_EXPORT bool AppendHeapProfileLineAsTraceFormat(const std::string& line, | 141 BASE_EXPORT bool AppendHeapProfileLineAsTraceFormat(const std::string& line, |
142 std::string* output); | 142 std::string* output); |
143 | 143 |
144 // Returns a pointer to a string given its hexadecimal address in |hex_address|. | 144 // Returns a pointer to a string given its hexadecimal address in |hex_address|. |
145 // Handles both 32-bit and 64-bit addresses. Returns "null" for null pointers | 145 // Handles both 32-bit and 64-bit addresses. Returns "null" for null pointers |
146 // and "error" if |address| could not be parsed. Visible for testing. | 146 // and "error" if |address| could not be parsed. Visible for testing. |
147 BASE_EXPORT const char* StringFromHexAddress(const std::string& hex_address); | 147 BASE_EXPORT const char* StringFromHexAddress(const std::string& hex_address); |
148 | 148 |
149 } // namespace debug | 149 } // namespace trace_event |
150 } // namespace base | 150 } // namespace base |
151 | 151 |
152 // Make local variables with unique names based on the line number. Note that | 152 // Make local variables with unique names based on the line number. Note that |
153 // the extra level of redirection is needed. | 153 // the extra level of redirection is needed. |
154 #define INTERNAL_TRACE_MEMORY_ID3(line) trace_memory_unique_##line | 154 #define INTERNAL_TRACE_MEMORY_ID3(line) trace_memory_unique_##line |
155 #define INTERNAL_TRACE_MEMORY_ID2(line) INTERNAL_TRACE_MEMORY_ID3(line) | 155 #define INTERNAL_TRACE_MEMORY_ID2(line) INTERNAL_TRACE_MEMORY_ID3(line) |
156 #define INTERNAL_TRACE_MEMORY_ID INTERNAL_TRACE_MEMORY_ID2(__LINE__) | 156 #define INTERNAL_TRACE_MEMORY_ID INTERNAL_TRACE_MEMORY_ID2(__LINE__) |
157 | 157 |
158 // This is the core macro that adds a scope to each TRACE_EVENT location. | 158 // This is the core macro that adds a scope to each TRACE_EVENT location. |
159 // It generates a unique local variable name using the macros above. | 159 // It generates a unique local variable name using the macros above. |
160 #if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED) | 160 #if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED) |
161 #define INTERNAL_TRACE_MEMORY(category, name) \ | 161 #define INTERNAL_TRACE_MEMORY(category, name) \ |
162 base::debug::ScopedTraceMemory INTERNAL_TRACE_MEMORY_ID(category, name); | 162 base::trace_event::ScopedTraceMemory INTERNAL_TRACE_MEMORY_ID(category, name); |
163 #else | 163 #else |
164 #define INTERNAL_TRACE_MEMORY(category, name) | 164 #define INTERNAL_TRACE_MEMORY(category, name) |
165 #endif // defined(TRACE_MEMORY_SUPPORTED) | 165 #endif // defined(TRACE_MEMORY_SUPPORTED) |
166 | 166 |
167 // A special trace name that allows us to ignore memory allocations inside | 167 // A special trace name that allows us to ignore memory allocations inside |
168 // the memory dump system itself. The allocations are recorded, but the | 168 // the memory dump system itself. The allocations are recorded, but the |
169 // visualizer skips them. Must match the value in heap.js. | 169 // visualizer skips them. Must match the value in heap.js. |
170 #define TRACE_MEMORY_IGNORE "trace-memory-ignore" | 170 #define TRACE_MEMORY_IGNORE "trace-memory-ignore" |
171 | 171 |
172 #endif // BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_H_ | 172 #endif // BASE_TRACE_EVENT_TRACE_EVENT_MEMORY_H_ |
OLD | NEW |