1 /*
   2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_SERVICES_MEM_BASELINE_HPP
  26 #define SHARE_VM_SERVICES_MEM_BASELINE_HPP
  27 
  28 #if INCLUDE_NMT
  29 
  30 #include "memory/allocation.hpp"
  31 #include "runtime/mutex.hpp"
  32 #include "services/mallocSiteTable.hpp"
  33 #include "services/mallocTracker.hpp"
  34 #include "services/nmtCommon.hpp"
  35 #include "services/virtualMemoryTracker.hpp"
  36 #include "utilities/linkedlist.hpp"
  37 
  38 typedef LinkedListIterator<MallocSite>                   MallocSiteIterator;
  39 typedef LinkedListIterator<VirtualMemoryAllocationSite>  VirtualMemorySiteIterator;
  40 typedef LinkedListIterator<ReservedMemoryRegion>         VirtualMemoryAllocationIterator;
  41 
  42 /*
  43  * Baseline a memory snapshot
  44  */
  45 class MemBaseline VALUE_OBJ_CLASS_SPEC {
  46  public:
  47   enum BaselineThreshold {
  48     SIZE_THRESHOLD = K        // Only allocation size over this threshold will be baselined.
  49   };
  50 
  51   enum BaselineType {
  52     Not_baselined,
  53     Summary_baselined,
  54     Detail_baselined
  55   };
  56 
  57   enum SortingOrder {
  58     by_address,      // by memory address
  59     by_size,         // by memory size
  60     by_site,         // by call site where the memory is allocated from
  61     by_site_and_type // by call site and memory type
  62   };
  63 
  64  private:
  65   // Summary information
  66   MallocMemorySnapshot   _malloc_memory_snapshot;
  67   VirtualMemorySnapshot  _virtual_memory_snapshot;
  68   MetaspaceSnapshot      _metaspace_snapshot;
  69 
  70   size_t                 _instance_class_count;
  71   size_t                 _array_class_count;
  72 
  73   // Allocation sites information
  74   // Malloc allocation sites
  75   LinkedListImpl<MallocSite>                  _malloc_sites;
  76 
  77   // All virtual memory allocations
  78   LinkedListImpl<ReservedMemoryRegion>        _virtual_memory_allocations;
  79 
  80   // Virtual memory allocations by allocation sites, always in by_address
  81   // order
  82   LinkedListImpl<VirtualMemoryAllocationSite> _virtual_memory_sites;
  83 
  84   SortingOrder         _malloc_sites_order;
  85   SortingOrder         _virtual_memory_sites_order;
  86 
  87   BaselineType         _baseline_type;
  88 
  89  public:
  90   // create a memory baseline
  91   MemBaseline():
  92     _baseline_type(Not_baselined),
  93     _instance_class_count(0), _array_class_count(0) {
  94   }
  95 
  96   bool baseline(bool summaryOnly = true);
  97 
  98   BaselineType baseline_type() const { return _baseline_type; }
  99 
 100   MallocMemorySnapshot* malloc_memory_snapshot() {
 101     return &_malloc_memory_snapshot;
 102   }
 103 
 104   VirtualMemorySnapshot* virtual_memory_snapshot() {
 105     return &_virtual_memory_snapshot;
 106   }
 107 
 108   MetaspaceSnapshot* metaspace_snapshot() {
 109     return &_metaspace_snapshot;
 110   }
 111 
 112   MallocSiteIterator malloc_sites(SortingOrder order);
 113   VirtualMemorySiteIterator virtual_memory_sites(SortingOrder order);
 114 
 115   // Virtual memory allocation iterator always returns in virtual memory
 116   // base address order.
 117   VirtualMemoryAllocationIterator virtual_memory_allocations() {
 118     assert(!_virtual_memory_allocations.is_empty(), "Not detail baseline");
 119     return VirtualMemoryAllocationIterator(_virtual_memory_allocations.head());
 120   }
 121 
 122   // Total reserved memory = total malloc'd memory + total reserved virtual
 123   // memory
 124   size_t total_reserved_memory() const {
 125     assert(baseline_type() != Not_baselined, "Not yet baselined");
 126     size_t amount = _malloc_memory_snapshot.total() +
 127            _virtual_memory_snapshot.total_reserved();
 128     return amount;
 129   }
 130 
 131   // Total committed memory = total malloc'd memory + total committed
 132   // virtual memory
 133   size_t total_committed_memory() const {
 134     assert(baseline_type() != Not_baselined, "Not yet baselined");
 135     size_t amount = _malloc_memory_snapshot.total() +
 136            _virtual_memory_snapshot.total_committed();
 137     return amount;
 138   }
 139 
 140   size_t total_arena_memory() const {
 141     assert(baseline_type() != Not_baselined, "Not yet baselined");
 142     return _malloc_memory_snapshot.total_arena();
 143   }
 144 
 145   size_t malloc_tracking_overhead() const {
 146     assert(baseline_type() != Not_baselined, "Not yet baselined");
 147     MemBaseline* bl = const_cast<MemBaseline*>(this);
 148     return bl->_malloc_memory_snapshot.malloc_overhead()->size();
 149   }
 150 
 151   MallocMemory* malloc_memory(MEMFLAGS flag) {
 152     assert(baseline_type() != Not_baselined, "Not yet baselined");
 153     return _malloc_memory_snapshot.by_type(flag);
 154   }
 155 
 156   VirtualMemory* virtual_memory(MEMFLAGS flag) {
 157     assert(baseline_type() != Not_baselined, "Not yet baselined");
 158     return _virtual_memory_snapshot.by_type(flag);
 159   }
 160 
 161 
 162   size_t class_count() const {
 163     assert(baseline_type() != Not_baselined, "Not yet baselined");
 164     return _instance_class_count + _array_class_count;
 165   }
 166 
 167   size_t instance_class_count() const {
 168     assert(baseline_type() != Not_baselined, "Not yet baselined");
 169     return _instance_class_count;
 170   }
 171 
 172   size_t array_class_count() const {
 173     assert(baseline_type() != Not_baselined, "Not yet baselined");
 174     return _array_class_count;
 175   }
 176 
 177   size_t thread_count() const {
 178     assert(baseline_type() != Not_baselined, "Not yet baselined");
 179     return _malloc_memory_snapshot.thread_count();
 180   }
 181 
 182   // reset the baseline for reuse
 183   void reset() {
 184     _baseline_type = Not_baselined;
 185     // _malloc_memory_snapshot and _virtual_memory_snapshot are copied over.
 186     _instance_class_count  = 0;
 187     _array_class_count = 0;
 188 
 189     _malloc_sites.clear();
 190     _virtual_memory_sites.clear();
 191     _virtual_memory_allocations.clear();
 192   }
 193 
 194  private:
 195   // Baseline summary information
 196   bool baseline_summary();
 197 
 198   // Baseline allocation sites (detail tracking only)
 199   bool baseline_allocation_sites();
 200 
 201   // Aggregate virtual memory allocation by allocation sites
 202   bool aggregate_virtual_memory_allocation_sites();
 203 
 204   // Sorting allocation sites in different orders
 205   // Sort allocation sites in size order
 206   void malloc_sites_to_size_order();
 207   // Sort allocation sites in call site address order
 208   void malloc_sites_to_allocation_site_order();
 209   // Sort allocation sites in call site address and memory type order
 210   void malloc_sites_to_allocation_site_and_type_order();
 211 
 212   // Sort allocation sites in reserved size order
 213   void virtual_memory_sites_to_size_order();
 214   // Sort allocation sites in call site address order
 215   void virtual_memory_sites_to_reservation_site_order();
 216 };
 217 
 218 #endif // INCLUDE_NMT
 219 
 220 #endif // SHARE_VM_SERVICES_MEM_BASELINE_HPP