1 /*
   2  * Copyright (c) 2014, 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 #include "precompiled.hpp"
  26 #include "jfr/leakprofiler/chains/dfsClosure.hpp"
  27 #include "jfr/leakprofiler/chains/edge.hpp"
  28 #include "jfr/leakprofiler/chains/edgeStore.hpp"
  29 #include "jfr/leakprofiler/utilities/granularTimer.hpp"
  30 #include "jfr/leakprofiler/chains/bitset.hpp"
  31 #include "jfr/leakprofiler/utilities/unifiedOop.hpp"
  32 #include "jfr/leakprofiler/utilities/rootType.hpp"
  33 #include "jfr/leakprofiler/chains/rootSetClosure.hpp"
  34 #include "memory/iterator.inline.hpp"
  35 #include "memory/resourceArea.hpp"
  36 #include "oops/access.inline.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "utilities/align.hpp"
  39 
  40 // max dfs depth should not exceed size of stack
  41 static const size_t max_dfs_depth = 5000;
  42 
  43 EdgeStore* DFSClosure::_edge_store = NULL;
  44 BitSet* DFSClosure::_mark_bits = NULL;
  45 const Edge* DFSClosure::_start_edge = NULL;
  46 size_t DFSClosure::_max_depth = max_dfs_depth;
  47 bool DFSClosure::_ignore_root_set = false;
  48 
  49 DFSClosure::DFSClosure() :
  50   _parent(NULL),
  51   _reference(NULL),
  52   _depth(0) {
  53 }
  54 
  55 DFSClosure::DFSClosure(DFSClosure* parent, size_t depth) :
  56   _parent(parent),
  57   _reference(NULL),
  58   _depth(depth) {
  59 }
  60 
  61 void DFSClosure::find_leaks_from_edge(EdgeStore* edge_store,
  62                                       BitSet* mark_bits,
  63                                       const Edge* start_edge) {
  64   assert(edge_store != NULL, "invariant");
  65   assert(mark_bits != NULL," invariant");
  66   assert(start_edge != NULL, "invariant");
  67 
  68   _edge_store = edge_store;
  69   _mark_bits = mark_bits;
  70   _start_edge = start_edge;
  71   _ignore_root_set = false;
  72   assert(_max_depth == max_dfs_depth, "invariant");
  73 
  74   // Depth-first search, starting from a BFS egde
  75   DFSClosure dfs;
  76   start_edge->pointee()->oop_iterate(&dfs);
  77 }
  78 
  79 void DFSClosure::find_leaks_from_root_set(EdgeStore* edge_store,
  80                                           BitSet* mark_bits) {
  81   assert(edge_store != NULL, "invariant");
  82   assert(mark_bits != NULL, "invariant");
  83 
  84   _edge_store = edge_store;
  85   _mark_bits = mark_bits;
  86   _start_edge = NULL;
  87 
  88   // Mark root set, to avoid going sideways
  89   _max_depth = 1;
  90   _ignore_root_set = false;
  91   DFSClosure dfs1;
  92   RootSetClosure::process_roots(&dfs1);
  93 
  94   // Depth-first search
  95   _max_depth = max_dfs_depth;
  96   _ignore_root_set = true;
  97   assert(_start_edge == NULL, "invariant");
  98   DFSClosure dfs2;
  99   RootSetClosure::process_roots(&dfs2);
 100 }
 101 
 102 void DFSClosure::closure_impl(const oop* reference, const oop pointee) {
 103   assert(pointee != NULL, "invariant");
 104   assert(reference != NULL, "invariant");
 105 
 106   if (GranularTimer::is_finished()) {
 107      return;
 108   }
 109   if (_depth == 0 && _ignore_root_set) {
 110     // Root set is already marked, but we want
 111     // to continue, so skip is_marked check.
 112     assert(_mark_bits->is_marked(pointee), "invariant");
 113   } else {
 114     if (_mark_bits->is_marked(pointee)) {
 115       return;
 116     }
 117   }
 118 
 119   _reference = reference;
 120   _mark_bits->mark_obj(pointee);
 121   assert(_mark_bits->is_marked(pointee), "invariant");
 122 
 123   // is the pointee a sample object?
 124   if (NULL == pointee->mark()) {
 125     add_chain();
 126   }
 127 
 128   assert(_max_depth >= 1, "invariant");
 129   if (_depth < _max_depth - 1) {
 130     DFSClosure next_level(this, _depth + 1);
 131     pointee->oop_iterate(&next_level);
 132   }
 133 }
 134 
 135 void DFSClosure::add_chain() {
 136   const size_t length = _start_edge == NULL ? _depth + 1 :
 137                         _start_edge->distance_to_root() + 1 + _depth + 1;
 138 
 139   ResourceMark rm;
 140   Edge* const chain = NEW_RESOURCE_ARRAY(Edge, length);
 141   size_t idx = 0;
 142 
 143   // aggregate from depth-first search
 144   const DFSClosure* c = this;
 145   while (c != NULL) {
 146     chain[idx++] = Edge(NULL, c->reference());
 147     c = c->parent();
 148   }
 149 
 150   assert(idx == _depth + 1, "invariant");
 151 
 152   // aggregate from breadth-first search
 153   const Edge* current = _start_edge;
 154   while (current != NULL) {
 155     chain[idx++] = Edge(NULL, current->reference());
 156     current = current->parent();
 157   }
 158   assert(idx == length, "invariant");
 159   _edge_store->add_chain(chain, length);
 160 }
 161 
 162 void DFSClosure::do_oop(oop* ref) {
 163   assert(ref != NULL, "invariant");
 164   assert(is_aligned(ref, HeapWordSize), "invariant");
 165   const oop pointee = *ref;
 166   if (pointee != NULL) {
 167     closure_impl(ref, pointee);
 168   }
 169 }
 170 
 171 void DFSClosure::do_oop(narrowOop* ref) {
 172   assert(ref != NULL, "invariant");
 173   assert(is_aligned(ref, sizeof(narrowOop)), "invariant");
 174   const oop pointee = RawAccess<>::oop_load(ref);
 175   if (pointee != NULL) {
 176     closure_impl(UnifiedOop::encode(ref), pointee);
 177   }
 178 }