dfsClosure.cpp 5.5 KB
Newer Older
A
apetushkov 已提交
1
/*
2
 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
A
apetushkov 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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"
26
#include "jfr/leakprofiler/chains/bitset.hpp"
A
apetushkov 已提交
27 28 29
#include "jfr/leakprofiler/chains/dfsClosure.hpp"
#include "jfr/leakprofiler/chains/edge.hpp"
#include "jfr/leakprofiler/chains/edgeStore.hpp"
30
#include "jfr/leakprofiler/chains/rootSetClosure.hpp"
A
apetushkov 已提交
31 32
#include "jfr/leakprofiler/utilities/granularTimer.hpp"
#include "jfr/leakprofiler/utilities/rootType.hpp"
33
#include "jfr/leakprofiler/utilities/unifiedOop.hpp"
A
apetushkov 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#include "memory/iterator.inline.hpp"
#include "memory/resourceArea.hpp"
#include "oops/oop.inline.hpp"
#include "utilities/align.hpp"

// max dfs depth should not exceed size of stack
static const size_t max_dfs_depth = 5000;

EdgeStore* DFSClosure::_edge_store = NULL;
BitSet* DFSClosure::_mark_bits = NULL;
const Edge* DFSClosure::_start_edge = NULL;
size_t DFSClosure::_max_depth = max_dfs_depth;
bool DFSClosure::_ignore_root_set = false;

DFSClosure::DFSClosure() :
  _parent(NULL),
  _reference(NULL),
  _depth(0) {
}

DFSClosure::DFSClosure(DFSClosure* parent, size_t depth) :
  _parent(parent),
  _reference(NULL),
  _depth(depth) {
}

void DFSClosure::find_leaks_from_edge(EdgeStore* edge_store,
                                      BitSet* mark_bits,
                                      const Edge* start_edge) {
  assert(edge_store != NULL, "invariant");
  assert(mark_bits != NULL," invariant");
  assert(start_edge != NULL, "invariant");

  _edge_store = edge_store;
  _mark_bits = mark_bits;
  _start_edge = start_edge;
  _ignore_root_set = false;
  assert(_max_depth == max_dfs_depth, "invariant");

  // Depth-first search, starting from a BFS egde
  DFSClosure dfs;
  start_edge->pointee()->oop_iterate(&dfs);
}

void DFSClosure::find_leaks_from_root_set(EdgeStore* edge_store,
                                          BitSet* mark_bits) {
  assert(edge_store != NULL, "invariant");
  assert(mark_bits != NULL, "invariant");

  _edge_store = edge_store;
  _mark_bits = mark_bits;
  _start_edge = NULL;

  // Mark root set, to avoid going sideways
  _max_depth = 1;
  _ignore_root_set = false;
90 91 92
  DFSClosure dfs;
  RootSetClosure<DFSClosure> rs(&dfs);
  rs.process();
A
apetushkov 已提交
93 94 95 96 97

  // Depth-first search
  _max_depth = max_dfs_depth;
  _ignore_root_set = true;
  assert(_start_edge == NULL, "invariant");
98
  rs.process();
A
apetushkov 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

void DFSClosure::closure_impl(const oop* reference, const oop pointee) {
  assert(pointee != NULL, "invariant");
  assert(reference != NULL, "invariant");

  if (GranularTimer::is_finished()) {
     return;
  }
  if (_depth == 0 && _ignore_root_set) {
    // Root set is already marked, but we want
    // to continue, so skip is_marked check.
    assert(_mark_bits->is_marked(pointee), "invariant");
  } else {
    if (_mark_bits->is_marked(pointee)) {
      return;
    }
  }

  _reference = reference;
  _mark_bits->mark_obj(pointee);
  assert(_mark_bits->is_marked(pointee), "invariant");

  // is the pointee a sample object?
  if (NULL == pointee->mark()) {
    add_chain();
  }

  assert(_max_depth >= 1, "invariant");
  if (_depth < _max_depth - 1) {
    DFSClosure next_level(this, _depth + 1);
    pointee->oop_iterate(&next_level);
  }
}

void DFSClosure::add_chain() {
135
  const size_t array_length = _depth + 2;
A
apetushkov 已提交
136 137

  ResourceMark rm;
138
  Edge* const chain = NEW_RESOURCE_ARRAY(Edge, array_length);
A
apetushkov 已提交
139 140 141 142 143
  size_t idx = 0;

  // aggregate from depth-first search
  const DFSClosure* c = this;
  while (c != NULL) {
144 145
    const size_t next = idx + 1;
    chain[idx++] = Edge(&chain[next], c->reference());
A
apetushkov 已提交
146 147
    c = c->parent();
  }
148 149
  assert(_depth + 1 == idx, "invariant");
  assert(array_length == idx + 1, "invariant");
A
apetushkov 已提交
150 151

  // aggregate from breadth-first search
152 153 154 155
  if (_start_edge != NULL) {
    chain[idx++] = *_start_edge;
  } else {
    chain[idx - 1] = Edge(NULL, chain[idx - 1].reference());
A
apetushkov 已提交
156
  }
157
  _edge_store->put_chain(chain, idx + (_start_edge != NULL ? _start_edge->distance_to_root() : 0));
A
apetushkov 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
}

void DFSClosure::do_oop(oop* ref) {
  assert(ref != NULL, "invariant");
  assert(is_aligned(ref, HeapWordSize), "invariant");
  const oop pointee = *ref;
  if (pointee != NULL) {
    closure_impl(ref, pointee);
  }
}

void DFSClosure::do_oop(narrowOop* ref) {
  assert(ref != NULL, "invariant");
  assert(is_aligned(ref, sizeof(narrowOop)), "invariant");
  const oop pointee = oopDesc::load_decode_heap_oop(ref);
  if (pointee != NULL) {
    closure_impl(UnifiedOop::encode(ref), pointee);
  }
}
177 178 179 180 181 182 183

void DFSClosure::do_root(const oop* ref) {
  assert(ref != NULL, "invariant");
  const oop pointee = UnifiedOop::dereference(ref);
  assert(pointee != NULL, "invariant");
  closure_impl(ref, pointee);
}