Home | History | Annotate | Download | only in debug
      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 // 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
     20 
     21 namespace base {
     22 
     23 class MessageLoopProxy;
     24 
     25 namespace debug {
     26 
     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)();
     37 
     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
     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(
     43       scoped_refptr<MessageLoopProxy> message_loop_proxy,
     44       HeapProfilerStartFunction heap_profiler_start_function,
     45       HeapProfilerStopFunction heap_profiler_stop_function,
     46       GetHeapProfileFunction get_heap_profile_function);
     47   virtual ~TraceMemoryController();
     48 
     49   // base::debug::TraceLog::EnabledStateChangedObserver overrides:
     50   virtual void OnTraceLogEnabled() OVERRIDE;
     51   virtual void OnTraceLogDisabled() OVERRIDE;
     52 
     53   // Starts heap memory profiling.
     54   void StartProfiling();
     55 
     56   // Captures a heap profile.
     57   void DumpMemoryProfile();
     58 
     59   // If memory tracing is enabled, dumps a memory profile to the tracing system.
     60   void StopProfiling();
     61 
     62  private:
     63   FRIEND_TEST_ALL_PREFIXES(TraceMemoryTest, TraceMemoryController);
     64 
     65   bool IsTimerRunningForTest() const;
     66 
     67   // Ensures the observer starts and stops tracing on the primary thread.
     68   scoped_refptr<MessageLoopProxy> message_loop_proxy_;
     69 
     70   // Pointers to tcmalloc heap profiling functions. Allows this class to use
     71   // tcmalloc functions without introducing a dependency from base to tcmalloc.
     72   HeapProfilerStartFunction heap_profiler_start_function_;
     73   HeapProfilerStopFunction heap_profiler_stop_function_;
     74   GetHeapProfileFunction get_heap_profile_function_;
     75 
     76   // Timer to schedule memory profile dumps.
     77   RepeatingTimer<TraceMemoryController> dump_timer_;
     78 
     79   WeakPtrFactory<TraceMemoryController> weak_factory_;
     80 
     81   DISALLOW_COPY_AND_ASSIGN(TraceMemoryController);
     82 };
     83 
     84 //////////////////////////////////////////////////////////////////////////////
     85 
     86 // A scoped context for memory tracing. Pushes the name onto a stack for
     87 // recording by tcmalloc heap profiling.
     88 class BASE_EXPORT ScopedTraceMemory {
     89  public:
     90   // Memory for |name| must be static, for example, a literal string in
     91   // a TRACE_EVENT macro.
     92   explicit ScopedTraceMemory(const char* name);
     93   ~ScopedTraceMemory();
     94 
     95   // Enables the storing of trace names on a per-thread stack.
     96   static void set_enabled(bool enabled) { enabled_ = enabled; }
     97 
     98   // Testing interface:
     99   static void InitForTest();
    100   static void CleanupForTest();
    101   static int GetStackIndexForTest();
    102   static const char* GetItemForTest(int index);
    103 
    104  private:
    105   static bool enabled_;
    106   DISALLOW_COPY_AND_ASSIGN(ScopedTraceMemory);
    107 };
    108 
    109 //////////////////////////////////////////////////////////////////////////////
    110 
    111 // Converts tcmalloc's heap profiler data with pseudo-stacks in |input| to
    112 // trace event compatible JSON and appends to |output|. Visible for testing.
    113 BASE_EXPORT void AppendHeapProfileAsTraceFormat(const char* input,
    114                                                 std::string* output);
    115 
    116 // Converts the first |line| of heap profiler data, which contains totals for
    117 // all allocations in a special format, into trace event compatible JSON and
    118 // appends to |output|. Visible for testing.
    119 BASE_EXPORT void AppendHeapProfileTotalsAsTraceFormat(const std::string& line,
    120                                                       std::string* output);
    121 
    122 // Converts a single |line| of heap profiler data into trace event compatible
    123 // JSON and appends to |output|. Returns true if the line was valid and has a
    124 // non-zero number of current allocations. Visible for testing.
    125 BASE_EXPORT bool AppendHeapProfileLineAsTraceFormat(const std::string& line,
    126                                                     std::string* output);
    127 
    128 }  // namespace debug
    129 }  // namespace base
    130 
    131 // Make local variables with unique names based on the line number. Note that
    132 // the extra level of redirection is needed.
    133 #define INTERNAL_TRACE_MEMORY_ID3(line) trace_memory_unique_##line
    134 #define INTERNAL_TRACE_MEMORY_ID2(line) INTERNAL_TRACE_MEMORY_ID3(line)
    135 #define INTERNAL_TRACE_MEMORY_ID INTERNAL_TRACE_MEMORY_ID2(__LINE__)
    136 
    137 // This is the core macro that adds a scope to each TRACE_EVENT location.
    138 // It generates a unique local variable name using the macros above.
    139 // TODO(jamescook): Make it record both category and name.
    140 #if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED)
    141 #define INTERNAL_TRACE_MEMORY(category, name) \
    142   base::debug::ScopedTraceMemory INTERNAL_TRACE_MEMORY_ID(name);
    143 #else
    144 #define INTERNAL_TRACE_MEMORY(category, name)
    145 #endif  // defined(TRACE_MEMORY_SUPPORTED)
    146 
    147 // A special trace name that allows us to ignore memory allocations inside
    148 // the memory dump system itself. The allocations are recorded, but the
    149 // visualizer skips them. Must match the value in heap.js.
    150 #define TRACE_MEMORY_IGNORE "trace-memory-ignore"
    151 
    152 #endif  // BASE_DEBUG_TRACE_EVENT_MEMORY_H_
    153