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