memBaseline.hpp 6.4 KB
Newer Older
Z
zgu 已提交
1
/*
2
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
Z
zgu 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#ifndef SHARE_VM_SERVICES_MEM_BASELINE_HPP
#define SHARE_VM_SERVICES_MEM_BASELINE_HPP

28 29
#if INCLUDE_NMT

Z
zgu 已提交
30 31
#include "memory/allocation.hpp"
#include "runtime/mutex.hpp"
32 33 34 35 36
#include "services/mallocSiteTable.hpp"
#include "services/mallocTracker.hpp"
#include "services/nmtCommon.hpp"
#include "services/virtualMemoryTracker.hpp"
#include "utilities/linkedlist.hpp"
Z
zgu 已提交
37

38 39 40
typedef LinkedListIterator<MallocSite>                   MallocSiteIterator;
typedef LinkedListIterator<VirtualMemoryAllocationSite>  VirtualMemorySiteIterator;
typedef LinkedListIterator<ReservedMemoryRegion>         VirtualMemoryAllocationIterator;
Z
zgu 已提交
41 42

/*
43
 * Baseline a memory snapshot
Z
zgu 已提交
44
 */
45
class MemBaseline VALUE_OBJ_CLASS_SPEC {
Z
zgu 已提交
46
 public:
47 48
  enum BaselineThreshold {
    SIZE_THRESHOLD = K        // Only allocation size over this threshold will be baselined.
Z
zgu 已提交
49 50
  };

51 52 53 54 55
  enum BaselineType {
    Not_baselined,
    Summary_baselined,
    Detail_baselined
  };
Z
zgu 已提交
56

57 58 59 60 61
  enum SortingOrder {
    by_address,   // by memory address
    by_size,      // by memory size
    by_site       // by call site where the memory is allocated from
  };
Z
zgu 已提交
62 63

 private:
64
  // Summary information
Z
zgu 已提交
65 66
  MallocMemorySnapshot   _malloc_memory_snapshot;
  VirtualMemorySnapshot  _virtual_memory_snapshot;
Z
zgu 已提交
67

68
  size_t               _class_count;
Z
zgu 已提交
69

70 71
  // Allocation sites information
  // Malloc allocation sites
Z
zgu 已提交
72
  LinkedListImpl<MallocSite>                  _malloc_sites;
Z
zgu 已提交
73

74
  // All virtual memory allocations
Z
zgu 已提交
75
  LinkedListImpl<ReservedMemoryRegion>        _virtual_memory_allocations;
Z
zgu 已提交
76

77 78
  // Virtual memory allocations by allocation sites, always in by_address
  // order
Z
zgu 已提交
79
  LinkedListImpl<VirtualMemoryAllocationSite> _virtual_memory_sites;
Z
zgu 已提交
80

81 82
  SortingOrder         _malloc_sites_order;
  SortingOrder         _virtual_memory_sites_order;
Z
zgu 已提交
83

84
  BaselineType         _baseline_type;
Z
zgu 已提交
85 86

 public:
87 88 89
  // create a memory baseline
  MemBaseline():
    _baseline_type(Not_baselined),
Z
zgu 已提交
90
    _class_count(0) {
Z
zgu 已提交
91 92
  }

93 94
  ~MemBaseline() {
    reset();
Z
zgu 已提交
95 96
  }

97
  bool baseline(bool summaryOnly = true);
Z
zgu 已提交
98

99
  BaselineType baseline_type() const { return _baseline_type; }
Z
zgu 已提交
100

Z
zgu 已提交
101 102
  MallocMemorySnapshot* malloc_memory_snapshot() {
    return &_malloc_memory_snapshot;
Z
zgu 已提交
103 104
  }

Z
zgu 已提交
105 106
  VirtualMemorySnapshot* virtual_memory_snapshot() {
    return &_virtual_memory_snapshot;
Z
zgu 已提交
107 108
  }

109 110
  MallocSiteIterator malloc_sites(SortingOrder order);
  VirtualMemorySiteIterator virtual_memory_sites(SortingOrder order);
Z
zgu 已提交
111

112 113 114 115 116
  // Virtual memory allocation iterator always returns in virtual memory
  // base address order.
  VirtualMemoryAllocationIterator virtual_memory_allocations() {
    assert(!_virtual_memory_allocations.is_empty(), "Not detail baseline");
    return VirtualMemoryAllocationIterator(_virtual_memory_allocations.head());
Z
zgu 已提交
117 118
  }

119 120 121 122
  // Total reserved memory = total malloc'd memory + total reserved virtual
  // memory
  size_t total_reserved_memory() const {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
Z
zgu 已提交
123 124
    size_t amount = _malloc_memory_snapshot.total() +
           _virtual_memory_snapshot.total_reserved();
125
    return amount;
Z
zgu 已提交
126 127
  }

128 129 130 131
  // Total committed memory = total malloc'd memory + total committed
  // virtual memory
  size_t total_committed_memory() const {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
Z
zgu 已提交
132 133
    size_t amount = _malloc_memory_snapshot.total() +
           _virtual_memory_snapshot.total_committed();
134
    return amount;
Z
zgu 已提交
135 136
  }

137 138
  size_t total_arena_memory() const {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
Z
zgu 已提交
139
    return _malloc_memory_snapshot.total_arena();
Z
zgu 已提交
140 141
  }

142 143
  size_t malloc_tracking_overhead() const {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
Z
zgu 已提交
144 145
    MemBaseline* bl = const_cast<MemBaseline*>(this);
    return bl->_malloc_memory_snapshot.malloc_overhead()->size();
Z
zgu 已提交
146 147
  }

Z
zgu 已提交
148 149 150
  MallocMemory* malloc_memory(MEMFLAGS flag) {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
    return _malloc_memory_snapshot.by_type(flag);
Z
zgu 已提交
151 152
  }

Z
zgu 已提交
153 154 155
  VirtualMemory* virtual_memory(MEMFLAGS flag) {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
    return _virtual_memory_snapshot.by_type(flag);
Z
zgu 已提交
156 157 158
  }


159 160 161
  size_t class_count() const {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
    return _class_count;
Z
zgu 已提交
162 163
  }

164 165
  size_t thread_count() const {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
Z
zgu 已提交
166
    return _malloc_memory_snapshot.thread_count();
Z
zgu 已提交
167 168 169
  }

  // reset the baseline for reuse
170 171
  void reset() {
    _baseline_type = Not_baselined;
Z
zgu 已提交
172 173
    _malloc_memory_snapshot.reset();
    _virtual_memory_snapshot.reset();
174
    _class_count  = 0;
Z
zgu 已提交
175

Z
zgu 已提交
176 177 178
    _malloc_sites.clear();
    _virtual_memory_sites.clear();
    _virtual_memory_allocations.clear();
Z
zgu 已提交
179 180 181
  }

 private:
182 183
  // Baseline summary information
  bool baseline_summary();
Z
zgu 已提交
184

185 186
  // Baseline allocation sites (detail tracking only)
  bool baseline_allocation_sites();
Z
zgu 已提交
187

188 189
  // Aggregate virtual memory allocation by allocation sites
  bool aggregate_virtual_memory_allocation_sites();
Z
zgu 已提交
190

191 192 193 194 195
  // Sorting allocation sites in different orders
  // Sort allocation sites in size order
  void malloc_sites_to_size_order();
  // Sort allocation sites in call site address order
  void malloc_sites_to_allocation_site_order();
Z
zgu 已提交
196

197 198 199 200
  // Sort allocation sites in reserved size order
  void virtual_memory_sites_to_size_order();
  // Sort allocation sites in call site address order
  void virtual_memory_sites_to_reservation_site_order();
Z
zgu 已提交
201 202
};

203
#endif // INCLUDE_NMT
Z
zgu 已提交
204 205

#endif // SHARE_VM_SERVICES_MEM_BASELINE_HPP