memBaseline.cpp 9.3 KB
Newer Older
Z
zgu 已提交
1
/*
2
 * Copyright (c) 2012, 2019, 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
 * 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.
 *
 */
#include "precompiled.hpp"
25

Z
zgu 已提交
26
#include "memory/allocation.hpp"
27 28
#include "runtime/safepoint.hpp"
#include "runtime/thread.inline.hpp"
Z
zgu 已提交
29 30 31
#include "services/memBaseline.hpp"
#include "services/memTracker.hpp"

32 33 34 35 36 37 38 39 40 41 42 43
/*
 * Sizes are sorted in descenting order for reporting
 */
int compare_malloc_size(const MallocSite& s1, const MallocSite& s2) {
  if (s1.size() == s2.size()) {
    return 0;
  } else if (s1.size() > s2.size()) {
    return -1;
  } else {
    return 1;
  }
}
44

Z
zgu 已提交
45

46 47 48 49 50 51 52 53
int compare_virtual_memory_size(const VirtualMemoryAllocationSite& s1,
  const VirtualMemoryAllocationSite& s2) {
  if (s1.reserved() == s2.reserved()) {
    return 0;
  } else if (s1.reserved() > s2.reserved()) {
    return -1;
  } else {
    return 1;
Z
zgu 已提交
54
  }
55
}
Z
zgu 已提交
56

57 58 59
// Sort into allocation site addresses order for baseline comparison
int compare_malloc_site(const MallocSite& s1, const MallocSite& s2) {
  return s1.call_stack()->compare(*s2.call_stack());
Z
zgu 已提交
60 61
}

62 63 64 65
// Sort into allocation site addresses and memory type order for baseline comparison
int compare_malloc_site_and_type(const MallocSite& s1, const MallocSite& s2) {
  int res = compare_malloc_site(s1, s2);
  if (res == 0) {
66
    res = (int)(s1.flag() - s2.flag());
67 68 69 70
  }

  return res;
}
Z
zgu 已提交
71

72 73 74 75 76 77 78 79 80 81
int compare_virtual_memory_site(const VirtualMemoryAllocationSite& s1,
  const VirtualMemoryAllocationSite& s2) {
  return s1.call_stack()->compare(*s2.call_stack());
}

/*
 * Walker to walk malloc allocation site table
 */
class MallocAllocationSiteWalker : public MallocSiteWalker {
 private:
Z
zgu 已提交
82
  SortedLinkedList<MallocSite, compare_malloc_size> _malloc_sites;
83 84 85 86 87
  size_t         _count;

  // Entries in MallocSiteTable with size = 0 and count = 0,
  // when the malloc site is not longer there.
 public:
Z
zgu 已提交
88
  MallocAllocationSiteWalker() : _count(0) { }
Z
zgu 已提交
89

90 91 92 93
  inline size_t count() const { return _count; }

  LinkedList<MallocSite>* malloc_sites() {
    return &_malloc_sites;
Z
zgu 已提交
94 95
  }

96 97 98 99 100 101 102 103 104 105 106 107
  bool do_malloc_site(const MallocSite* site) {
    if (site->size() >= MemBaseline::SIZE_THRESHOLD) {
      if (_malloc_sites.add(*site) != NULL) {
        _count++;
        return true;
      } else {
        return false;  // OOM
      }
    } else {
      // malloc site does not meet threshold, ignore and continue
      return true;
    }
108
  }
109
};
110

111 112 113
// Compare virtual memory region's base address
int compare_virtual_memory_base(const ReservedMemoryRegion& r1, const ReservedMemoryRegion& r2) {
  return r1.compare(r2);
Z
zgu 已提交
114 115
}

116 117 118
// Walk all virtual memory regions for baselining
class VirtualMemoryAllocationWalker : public VirtualMemoryWalker {
 private:
Z
zgu 已提交
119
  SortedLinkedList<ReservedMemoryRegion, compare_virtual_memory_base>
120 121
                _virtual_memory_regions;
  size_t        _count;
Z
zgu 已提交
122

123
 public:
Z
zgu 已提交
124
  VirtualMemoryAllocationWalker() : _count(0) { }
Z
zgu 已提交
125

126 127 128 129 130 131 132
  bool do_allocation_site(const ReservedMemoryRegion* rgn)  {
    if (rgn->size() >= MemBaseline::SIZE_THRESHOLD) {
      if (_virtual_memory_regions.add(*rgn) != NULL) {
        _count ++;
        return true;
      } else {
        return false;
Z
zgu 已提交
133 134
      }
    }
135
    return true;
Z
zgu 已提交
136 137
  }

138 139 140 141
  LinkedList<ReservedMemoryRegion>* virtual_memory_allocations() {
    return &_virtual_memory_regions;
  }
};
Z
zgu 已提交
142 143


144
bool MemBaseline::baseline_summary() {
Z
zgu 已提交
145 146
  MallocMemorySummary::snapshot(&_malloc_memory_snapshot);
  VirtualMemorySummary::snapshot(&_virtual_memory_snapshot);
Z
zgu 已提交
147 148 149
  return true;
}

150 151
bool MemBaseline::baseline_allocation_sites() {
  // Malloc allocation sites
Z
zgu 已提交
152
  MallocAllocationSiteWalker malloc_walker;
153 154
  if (!MallocSiteTable::walk_malloc_site(&malloc_walker)) {
    return false;
Z
zgu 已提交
155 156
  }

Z
zgu 已提交
157
  _malloc_sites.move(malloc_walker.malloc_sites());
158 159 160 161
  // The malloc sites are collected in size order
  _malloc_sites_order = by_size;

  // Virtual memory allocation sites
Z
zgu 已提交
162
  VirtualMemoryAllocationWalker virtual_memory_walker;
163 164
  if (!VirtualMemoryTracker::walk_virtual_memory(&virtual_memory_walker)) {
    return false;
Z
zgu 已提交
165 166
  }

167
  // Virtual memory allocations are collected in call stack order
Z
zgu 已提交
168
  _virtual_memory_allocations.move(virtual_memory_walker.virtual_memory_allocations());
169 170 171

  if (!aggregate_virtual_memory_allocation_sites()) {
    return false;
Z
zgu 已提交
172
  }
173 174 175
  // Virtual memory allocation sites are aggregrated in call stack order
  _virtual_memory_sites_order = by_address;

Z
zgu 已提交
176 177 178
  return true;
}

179 180
bool MemBaseline::baseline(bool summaryOnly) {
  reset();
Z
zgu 已提交
181

182
  _class_count = InstanceKlass::number_of_instance_classes();
183

184
  if (!baseline_summary()) {
185 186 187
    return false;
  }

188 189 190 191 192 193 194
  _baseline_type = Summary_baselined;

  // baseline details
  if (!summaryOnly &&
      MemTracker::tracking_level() == NMT_detail) {
    baseline_allocation_sites();
    _baseline_type = Detail_baselined;
195 196
  }

Z
zgu 已提交
197 198 199
  return true;
}

200 201 202
int compare_allocation_site(const VirtualMemoryAllocationSite& s1,
  const VirtualMemoryAllocationSite& s2) {
  return s1.call_stack()->compare(*s2.call_stack());
Z
zgu 已提交
203 204
}

205
bool MemBaseline::aggregate_virtual_memory_allocation_sites() {
Z
zgu 已提交
206
  SortedLinkedList<VirtualMemoryAllocationSite, compare_allocation_site> allocation_sites;
207 208 209 210 211

  VirtualMemoryAllocationIterator itr = virtual_memory_allocations();
  const ReservedMemoryRegion* rgn;
  VirtualMemoryAllocationSite* site;
  while ((rgn = itr.next()) != NULL) {
212
    VirtualMemoryAllocationSite tmp(*rgn->call_stack(), rgn->flag());
213 214 215 216 217 218
    site = allocation_sites.find(tmp);
    if (site == NULL) {
      LinkedListNode<VirtualMemoryAllocationSite>* node =
        allocation_sites.add(tmp);
      if (node == NULL) return false;
      site = node->data();
Z
zgu 已提交
219
    }
220 221
    site->reserve_memory(rgn->size());
    site->commit_memory(rgn->committed_size());
Z
zgu 已提交
222
  }
223

Z
zgu 已提交
224
  _virtual_memory_sites.move(&allocation_sites);
225
  return true;
Z
zgu 已提交
226 227
}

228
MallocSiteIterator MemBaseline::malloc_sites(SortingOrder order) {
Z
zgu 已提交
229
  assert(!_malloc_sites.is_empty(), "Not detail baseline");
230 231 232 233 234 235 236
  switch(order) {
    case by_size:
      malloc_sites_to_size_order();
      break;
    case by_site:
      malloc_sites_to_allocation_site_order();
      break;
237 238 239
    case by_site_and_type:
      malloc_sites_to_allocation_site_and_type_order();
      break;
240 241 242
    case by_address:
    default:
      ShouldNotReachHere();
Z
zgu 已提交
243
  }
244
  return MallocSiteIterator(_malloc_sites.head());
Z
zgu 已提交
245 246
}

247
VirtualMemorySiteIterator MemBaseline::virtual_memory_sites(SortingOrder order) {
Z
zgu 已提交
248
  assert(!_virtual_memory_sites.is_empty(), "Not detail baseline");
249 250 251 252 253 254 255 256 257 258 259 260 261
  switch(order) {
    case by_size:
      virtual_memory_sites_to_size_order();
      break;
    case by_site:
      virtual_memory_sites_to_reservation_site_order();
      break;
    case by_address:
    default:
      ShouldNotReachHere();
  }
  return VirtualMemorySiteIterator(_virtual_memory_sites.head());
}
Z
zgu 已提交
262 263


264 265 266
// Sorting allocations sites in different orders
void MemBaseline::malloc_sites_to_size_order() {
  if (_malloc_sites_order != by_size) {
Z
zgu 已提交
267
    SortedLinkedList<MallocSite, compare_malloc_size> tmp;
Z
zgu 已提交
268

269 270 271 272 273
    // Add malloc sites to sorted linked list to sort into size order
    tmp.move(&_malloc_sites);
    _malloc_sites.set_head(tmp.head());
    tmp.set_head(NULL);
    _malloc_sites_order = by_size;
Z
zgu 已提交
274
  }
275
}
Z
zgu 已提交
276

277
void MemBaseline::malloc_sites_to_allocation_site_order() {
278
  if (_malloc_sites_order != by_site && _malloc_sites_order != by_site_and_type) {
Z
zgu 已提交
279
    SortedLinkedList<MallocSite, compare_malloc_site> tmp;
280 281 282 283 284
    // Add malloc sites to sorted linked list to sort into site (address) order
    tmp.move(&_malloc_sites);
    _malloc_sites.set_head(tmp.head());
    tmp.set_head(NULL);
    _malloc_sites_order = by_site;
Z
zgu 已提交
285 286 287
  }
}

288 289 290 291 292 293 294 295 296 297 298
void MemBaseline::malloc_sites_to_allocation_site_and_type_order() {
  if (_malloc_sites_order != by_site_and_type) {
    SortedLinkedList<MallocSite, compare_malloc_site_and_type> tmp;
    // Add malloc sites to sorted linked list to sort into site (address) order
    tmp.move(&_malloc_sites);
    _malloc_sites.set_head(tmp.head());
    tmp.set_head(NULL);
    _malloc_sites_order = by_site_and_type;
  }
}

299 300
void MemBaseline::virtual_memory_sites_to_size_order() {
  if (_virtual_memory_sites_order != by_size) {
Z
zgu 已提交
301
    SortedLinkedList<VirtualMemoryAllocationSite, compare_virtual_memory_size> tmp;
Z
zgu 已提交
302

303
    tmp.move(&_virtual_memory_sites);
Z
zgu 已提交
304

305 306 307 308
    _virtual_memory_sites.set_head(tmp.head());
    tmp.set_head(NULL);
    _virtual_memory_sites_order = by_size;
  }
Z
zgu 已提交
309 310
}

311 312
void MemBaseline::virtual_memory_sites_to_reservation_site_order() {
  if (_virtual_memory_sites_order != by_size) {
Z
zgu 已提交
313
    SortedLinkedList<VirtualMemoryAllocationSite, compare_virtual_memory_site> tmp;
Z
zgu 已提交
314

Z
zgu 已提交
315
    tmp.move(&_virtual_memory_sites);
Z
zgu 已提交
316

317 318
    _virtual_memory_sites.set_head(tmp.head());
    tmp.set_head(NULL);
Z
zgu 已提交
319

320 321
    _virtual_memory_sites_order = by_size;
  }
Z
zgu 已提交
322 323
}