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