memBaseline.hpp 6.3 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
  bool baseline(bool summaryOnly = true);
Z
zgu 已提交
94

95
  BaselineType baseline_type() const { return _baseline_type; }
Z
zgu 已提交
96

Z
zgu 已提交
97 98
  MallocMemorySnapshot* malloc_memory_snapshot() {
    return &_malloc_memory_snapshot;
Z
zgu 已提交
99 100
  }

Z
zgu 已提交
101 102
  VirtualMemorySnapshot* virtual_memory_snapshot() {
    return &_virtual_memory_snapshot;
Z
zgu 已提交
103 104
  }

105 106
  MallocSiteIterator malloc_sites(SortingOrder order);
  VirtualMemorySiteIterator virtual_memory_sites(SortingOrder order);
Z
zgu 已提交
107

108 109 110 111 112
  // 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 已提交
113 114
  }

115 116 117 118
  // 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 已提交
119 120
    size_t amount = _malloc_memory_snapshot.total() +
           _virtual_memory_snapshot.total_reserved();
121
    return amount;
Z
zgu 已提交
122 123
  }

124 125 126 127
  // 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 已提交
128 129
    size_t amount = _malloc_memory_snapshot.total() +
           _virtual_memory_snapshot.total_committed();
130
    return amount;
Z
zgu 已提交
131 132
  }

133 134
  size_t total_arena_memory() const {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
Z
zgu 已提交
135
    return _malloc_memory_snapshot.total_arena();
Z
zgu 已提交
136 137
  }

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

Z
zgu 已提交
144 145 146
  MallocMemory* malloc_memory(MEMFLAGS flag) {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
    return _malloc_memory_snapshot.by_type(flag);
Z
zgu 已提交
147 148
  }

Z
zgu 已提交
149 150 151
  VirtualMemory* virtual_memory(MEMFLAGS flag) {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
    return _virtual_memory_snapshot.by_type(flag);
Z
zgu 已提交
152 153 154
  }


155 156 157
  size_t class_count() const {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
    return _class_count;
Z
zgu 已提交
158 159
  }

160 161
  size_t thread_count() const {
    assert(baseline_type() != Not_baselined, "Not yet baselined");
Z
zgu 已提交
162
    return _malloc_memory_snapshot.thread_count();
Z
zgu 已提交
163 164 165
  }

  // reset the baseline for reuse
166 167
  void reset() {
    _baseline_type = Not_baselined;
168
    // _malloc_memory_snapshot and _virtual_memory_snapshot are copied over.
169
    _class_count  = 0;
Z
zgu 已提交
170

Z
zgu 已提交
171 172 173
    _malloc_sites.clear();
    _virtual_memory_sites.clear();
    _virtual_memory_allocations.clear();
Z
zgu 已提交
174 175 176
  }

 private:
177 178
  // Baseline summary information
  bool baseline_summary();
Z
zgu 已提交
179

180 181
  // Baseline allocation sites (detail tracking only)
  bool baseline_allocation_sites();
Z
zgu 已提交
182

183 184
  // Aggregate virtual memory allocation by allocation sites
  bool aggregate_virtual_memory_allocation_sites();
Z
zgu 已提交
185

186 187 188 189 190
  // 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 已提交
191

192 193 194 195
  // 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 已提交
196 197
};

198
#endif // INCLUDE_NMT
Z
zgu 已提交
199 200

#endif // SHARE_VM_SERVICES_MEM_BASELINE_HPP