Index: base/debug/trace_memory.h |
diff --git a/base/debug/trace_memory.h b/base/debug/trace_memory.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f6949be6e70c44eb6f6b2f8a936bfb8f68711e31 |
--- /dev/null |
+++ b/base/debug/trace_memory.h |
@@ -0,0 +1,89 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef BASE_DEBUG_TRACE_MEMORY_H_ |
+#define BASE_DEBUG_TRACE_MEMORY_H_ |
+ |
+#include "base/base_export.h" |
+#include "base/debug/trace_event_impl.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/timer.h" |
+ |
+namespace base { |
+ |
+class MessageLoopProxy; |
+ |
+namespace debug { |
+ |
+// Watches for chrome://tracing to be enabled or disabled. When tracing is |
+// enabled, also enables tcmalloc heap profiling. This class is the preferred |
+// way to turn trace-base heap memory profiling on and off. |
+class BASE_EXPORT TraceMemoryTraceLogObserver |
+ : public TraceLog::EnabledStateObserver { |
+ public: |
+ // |message_loop_proxy| must be a proxy to the primary thread for the client |
+ // process, e.g. the UI thread in a browser. |
+ TraceMemoryTraceLogObserver( |
+ scoped_refptr<MessageLoopProxy> message_loop_proxy); |
+ virtual ~TraceMemoryTraceLogObserver(); |
+ |
+ // base::debug::TraceLog::EnabledStateChangedObserver overrides: |
+ virtual void OnTraceLogEnabled() OVERRIDE; |
+ virtual void OnTraceLogDisabled() OVERRIDE; |
+ |
+ // Starts heap memory profiling. |
+ void StartProfiling(); |
+ |
+ // Ends heap memory profiling. |
+ void StopProfiling(); |
+ |
+ bool IsTimerRunningForTest() const; |
dsinclair
2013/06/18 15:30:15
If this is only for test, can we make it private a
James Cook
2013/06/29 00:02:42
Done.
|
+ |
+ private: |
+ // Ensures the observer starts and stops tracing on the primary thread. |
+ scoped_refptr<MessageLoopProxy> message_loop_proxy_; |
+ |
+ // Timer to schedule memory profile dumps. |
+ RepeatingTimer<TraceMemoryTraceLogObserver> dump_timer_; |
+ |
+ WeakPtrFactory<TraceMemoryTraceLogObserver> weak_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TraceMemoryTraceLogObserver); |
+}; |
+ |
+////////////////////////////////////////////////////////////////////////////// |
+ |
+// A scoped context for memory tracing. Pushes the category onto a stack for |
+// recording by tcmalloc heap profiling. |
+class BASE_EXPORT ScopedTraceMemory { |
+ public: |
+ ScopedTraceMemory(const char* category); |
dsinclair
2013/06/18 15:30:15
The memory for category, does that need to be stat
James Cook
2013/06/29 00:02:42
Added clarifying comment and changed to |name|, si
|
+ ~ScopedTraceMemory(); |
+ |
+ // Testing interface: |
+ static int GetStackIndexForTest(); |
+ static const char* GetItemForTest(int index); |
+}; |
+ |
+} // namespace debug |
+} // namespace base |
+ |
+// Make local variables with unique names based on the line number. Note that |
+// the extra level of redirection is needed. |
+#define INTERNAL_TRACE_MEMORY_ID3(line) trace_memory_unique_##line |
+#define INTERNAL_TRACE_MEMORY_ID2(line) INTERNAL_TRACE_MEMORY_ID3(line) |
+#define INTERNAL_TRACE_MEMORY_ID INTERNAL_TRACE_MEMORY_ID2(__LINE__) |
+ |
+// Generates a unique local variable name. |
+// TODO(jamescook): Make it record both category and name. |
dsinclair
2013/06/18 15:30:15
You'll need to be careful when you add category in
James Cook
2013/06/29 00:02:42
OK. For now I'm only recording the name, but we'l
|
+#define TRACE_MEMORY(category, name) \ |
+ base::debug::ScopedTraceMemory INTERNAL_TRACE_MEMORY_ID(name); |
+ |
+// A special trace name that allows us to ignore memory allocations inside |
+// the memory dump system itself. The allocations are recorded, but the |
+// visualizer skips them. Must match the value in heap.js. |
+#define TRACE_MEMORY_IGNORE "trace-memory-ignore" |
+ |
+#endif // BASE_DEBUG_TRACE_MEMORY_H_ |