Home | History | Annotate | Download | only in trace_event
      1 // Copyright 2015 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_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_
      6 #define BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_
      7 
      8 #include <stddef.h>
      9 
     10 #include <unordered_map>
     11 #include <vector>
     12 
     13 #include "base/base_export.h"
     14 #include "base/macros.h"
     15 #include "base/memory/ref_counted.h"
     16 #include "base/memory/scoped_vector.h"
     17 #include "base/trace_event/memory_allocator_dump.h"
     18 #include "base/trace_event/memory_allocator_dump_guid.h"
     19 #include "base/trace_event/memory_dump_request_args.h"
     20 #include "base/trace_event/memory_dump_session_state.h"
     21 #include "base/trace_event/process_memory_maps.h"
     22 #include "base/trace_event/process_memory_totals.h"
     23 #include "build/build_config.h"
     24 
     25 // Define COUNT_RESIDENT_BYTES_SUPPORTED if platform supports counting of the
     26 // resident memory.
     27 #if (defined(OS_POSIX) && !defined(OS_NACL)) || defined(OS_WIN)
     28 #define COUNT_RESIDENT_BYTES_SUPPORTED
     29 #endif
     30 
     31 namespace base {
     32 namespace trace_event {
     33 
     34 class MemoryDumpManager;
     35 class MemoryDumpSessionState;
     36 class TracedValue;
     37 
     38 // ProcessMemoryDump is as a strongly typed container which holds the dumps
     39 // produced by the MemoryDumpProvider(s) for a specific process.
     40 class BASE_EXPORT ProcessMemoryDump {
     41  public:
     42   struct MemoryAllocatorDumpEdge {
     43     MemoryAllocatorDumpGuid source;
     44     MemoryAllocatorDumpGuid target;
     45     int importance;
     46     const char* type;
     47   };
     48 
     49   // Maps allocator dumps absolute names (allocator_name/heap/subheap) to
     50   // MemoryAllocatorDump instances.
     51   using AllocatorDumpsMap =
     52       std::unordered_map<std::string, std::unique_ptr<MemoryAllocatorDump>>;
     53 
     54   using HeapDumpsMap =
     55       std::unordered_map<std::string, std::unique_ptr<TracedValue>>;
     56 
     57 #if defined(COUNT_RESIDENT_BYTES_SUPPORTED)
     58   // Returns the number of bytes in a kernel memory page. Some platforms may
     59   // have a different value for kernel page sizes from user page sizes. It is
     60   // important to use kernel memory page sizes for resident bytes calculation.
     61   // In most cases, the two are the same.
     62   static size_t GetSystemPageSize();
     63 
     64   // Returns the total bytes resident for a virtual address range, with given
     65   // |start_address| and |mapped_size|. |mapped_size| is specified in bytes. The
     66   // value returned is valid only if the given range is currently mmapped by the
     67   // process. The |start_address| must be page-aligned.
     68   static size_t CountResidentBytes(void* start_address, size_t mapped_size);
     69 #endif
     70 
     71   ProcessMemoryDump(scoped_refptr<MemoryDumpSessionState> session_state,
     72                     const MemoryDumpArgs& dump_args);
     73   ~ProcessMemoryDump();
     74 
     75   // Creates a new MemoryAllocatorDump with the given name and returns the
     76   // empty object back to the caller.
     77   // Arguments:
     78   //   absolute_name: a name that uniquely identifies allocator dumps produced
     79   //       by this provider. It is possible to specify nesting by using a
     80   //       path-like string (e.g., v8/isolate1/heap1, v8/isolate1/heap2).
     81   //       Leading or trailing slashes are not allowed.
     82   //   guid: an optional identifier, unique among all processes within the
     83   //       scope of a global dump. This is only relevant when using
     84   //       AddOwnershipEdge() to express memory sharing. If omitted,
     85   //       it will be automatically generated.
     86   // ProcessMemoryDump handles the memory ownership of its MemoryAllocatorDumps.
     87   MemoryAllocatorDump* CreateAllocatorDump(const std::string& absolute_name);
     88   MemoryAllocatorDump* CreateAllocatorDump(const std::string& absolute_name,
     89                                            const MemoryAllocatorDumpGuid& guid);
     90 
     91   // Looks up a MemoryAllocatorDump given its allocator and heap names, or
     92   // nullptr if not found.
     93   MemoryAllocatorDump* GetAllocatorDump(const std::string& absolute_name) const;
     94 
     95   MemoryAllocatorDump* GetOrCreateAllocatorDump(
     96       const std::string& absolute_name);
     97 
     98   // Creates a shared MemoryAllocatorDump, to express cross-process sharing.
     99   // Shared allocator dumps are allowed to have duplicate guids within the
    100   // global scope, in order to reference the same dump from multiple processes.
    101   // See the design doc goo.gl/keU6Bf for reference usage patterns.
    102   MemoryAllocatorDump* CreateSharedGlobalAllocatorDump(
    103       const MemoryAllocatorDumpGuid& guid);
    104 
    105   // Creates a shared MemoryAllocatorDump as CreateSharedGlobalAllocatorDump,
    106   // but with a WEAK flag. A weak dump will be discarded unless a non-weak dump
    107   // is created using CreateSharedGlobalAllocatorDump by at least one process.
    108   // The WEAK flag does not apply if a non-weak dump with the same GUID already
    109   // exists or is created later. All owners and children of the discarded dump
    110   // will also be discarded transitively.
    111   MemoryAllocatorDump* CreateWeakSharedGlobalAllocatorDump(
    112       const MemoryAllocatorDumpGuid& guid);
    113 
    114   // Looks up a shared MemoryAllocatorDump given its guid.
    115   MemoryAllocatorDump* GetSharedGlobalAllocatorDump(
    116       const MemoryAllocatorDumpGuid& guid) const;
    117 
    118   // Returns the map of the MemoryAllocatorDumps added to this dump.
    119   const AllocatorDumpsMap& allocator_dumps() const { return allocator_dumps_; }
    120 
    121   // Dumps heap usage with |allocator_name|.
    122   void DumpHeapUsage(const base::hash_map<base::trace_event::AllocationContext,
    123                                           base::trace_event::AllocationMetrics>&
    124                          metrics_by_context,
    125                      base::trace_event::TraceEventMemoryOverhead& overhead,
    126                      const char* allocator_name);
    127 
    128   // Adds an ownership relationship between two MemoryAllocatorDump(s) with the
    129   // semantics: |source| owns |target|, and has the effect of attributing
    130   // the memory usage of |target| to |source|. |importance| is optional and
    131   // relevant only for the cases of co-ownership, where it acts as a z-index:
    132   // the owner with the highest importance will be attributed |target|'s memory.
    133   void AddOwnershipEdge(const MemoryAllocatorDumpGuid& source,
    134                         const MemoryAllocatorDumpGuid& target,
    135                         int importance);
    136   void AddOwnershipEdge(const MemoryAllocatorDumpGuid& source,
    137                         const MemoryAllocatorDumpGuid& target);
    138 
    139   const std::vector<MemoryAllocatorDumpEdge>& allocator_dumps_edges() const {
    140     return allocator_dumps_edges_;
    141   }
    142 
    143   // Utility method to add a suballocation relationship with the following
    144   // semantics: |source| is suballocated from |target_node_name|.
    145   // This creates a child node of |target_node_name| and adds an ownership edge
    146   // between |source| and the new child node. As a result, the UI will not
    147   // account the memory of |source| in the target node.
    148   void AddSuballocation(const MemoryAllocatorDumpGuid& source,
    149                         const std::string& target_node_name);
    150 
    151   const scoped_refptr<MemoryDumpSessionState>& session_state() const {
    152     return session_state_;
    153   }
    154 
    155   // Removes all the MemoryAllocatorDump(s) contained in this instance. This
    156   // ProcessMemoryDump can be safely reused as if it was new once this returns.
    157   void Clear();
    158 
    159   // Merges all MemoryAllocatorDump(s) contained in |other| inside this
    160   // ProcessMemoryDump, transferring their ownership to this instance.
    161   // |other| will be an empty ProcessMemoryDump after this method returns.
    162   // This is to allow dump providers to pre-populate ProcessMemoryDump instances
    163   // and later move their contents into the ProcessMemoryDump passed as argument
    164   // of the MemoryDumpProvider::OnMemoryDump(ProcessMemoryDump*) callback.
    165   void TakeAllDumpsFrom(ProcessMemoryDump* other);
    166 
    167   // Called at trace generation time to populate the TracedValue.
    168   void AsValueInto(TracedValue* value) const;
    169 
    170   ProcessMemoryTotals* process_totals() { return &process_totals_; }
    171   bool has_process_totals() const { return has_process_totals_; }
    172   void set_has_process_totals() { has_process_totals_ = true; }
    173 
    174   ProcessMemoryMaps* process_mmaps() { return &process_mmaps_; }
    175   bool has_process_mmaps() const { return has_process_mmaps_; }
    176   void set_has_process_mmaps() { has_process_mmaps_ = true; }
    177 
    178   const HeapDumpsMap& heap_dumps() const { return heap_dumps_; }
    179 
    180   const MemoryDumpArgs& dump_args() const { return dump_args_; }
    181 
    182  private:
    183   FRIEND_TEST_ALL_PREFIXES(ProcessMemoryDumpTest, BackgroundModeTest);
    184 
    185   MemoryAllocatorDump* AddAllocatorDumpInternal(
    186       std::unique_ptr<MemoryAllocatorDump> mad);
    187 
    188   MemoryAllocatorDump* GetBlackHoleMad();
    189 
    190   ProcessMemoryTotals process_totals_;
    191   bool has_process_totals_;
    192 
    193   ProcessMemoryMaps process_mmaps_;
    194   bool has_process_mmaps_;
    195 
    196   AllocatorDumpsMap allocator_dumps_;
    197   HeapDumpsMap heap_dumps_;
    198 
    199   // State shared among all PMDs instances created in a given trace session.
    200   scoped_refptr<MemoryDumpSessionState> session_state_;
    201 
    202   // Keeps track of relationships between MemoryAllocatorDump(s).
    203   std::vector<MemoryAllocatorDumpEdge> allocator_dumps_edges_;
    204 
    205   // Level of detail of the current dump.
    206   const MemoryDumpArgs dump_args_;
    207 
    208   // This allocator dump is returned when an invalid dump is created in
    209   // background mode. The attributes of the dump are ignored and not added to
    210   // the trace.
    211   std::unique_ptr<MemoryAllocatorDump> black_hole_mad_;
    212 
    213   // When set to true, the DCHECK(s) for invalid dump creations on the
    214   // background mode are disabled for testing.
    215   static bool is_black_hole_non_fatal_for_testing_;
    216 
    217   DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDump);
    218 };
    219 
    220 }  // namespace trace_event
    221 }  // namespace base
    222 
    223 #endif  // BASE_TRACE_EVENT_PROCESS_MEMORY_DUMP_H_
    224