1 /*
  2  * Copyright (c) 2014, 2020, 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/chains/jfrbitset.hpp"
 30 #include "jfr/leakprofiler/chains/rootSetClosure.hpp"
 31 #include "jfr/leakprofiler/utilities/granularTimer.hpp"
 32 #include "jfr/leakprofiler/utilities/rootType.hpp"
 33 #include "jfr/leakprofiler/utilities/unifiedOopRef.inline.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 UnifiedOopRef DFSClosure::_reference_stack[max_dfs_depth];
 41 
 42 void DFSClosure::find_leaks_from_edge(EdgeStore* edge_store,
 43                                       JFRBitSet* mark_bits,
 44                                       const Edge* start_edge) {
 45   assert(edge_store != NULL, "invariant");
 46   assert(mark_bits != NULL," invariant");
 47   assert(start_edge != NULL, "invariant");
 48 
 49   // Depth-first search, starting from a BFS egde
 50   DFSClosure dfs(edge_store, mark_bits, start_edge);
 51   start_edge->pointee()->oop_iterate(&dfs);
 52 }
 53 
 54 void DFSClosure::find_leaks_from_root_set(EdgeStore* edge_store,
 55                                           JFRBitSet* mark_bits) {
 56   assert(edge_store != NULL, "invariant");
 57   assert(mark_bits != NULL, "invariant");
 58 
 59   // Mark root set, to avoid going sideways
 60   DFSClosure dfs(edge_store, mark_bits, NULL);
 61   dfs._max_depth = 1;
 62   RootSetClosure<DFSClosure> rs(&dfs);
 63   rs.process();
 64 
 65   // Depth-first search
 66   dfs._max_depth = max_dfs_depth;
 67   dfs._ignore_root_set = true;
 68   rs.process();
 69 }
 70 
 71 DFSClosure::DFSClosure(EdgeStore* edge_store, JFRBitSet* mark_bits, const Edge* start_edge)
 72   :_edge_store(edge_store), _mark_bits(mark_bits), _start_edge(start_edge),
 73   _max_depth(max_dfs_depth), _depth(0), _ignore_root_set(false) {
 74 }
 75 
 76 void DFSClosure::closure_impl(UnifiedOopRef reference, const oop pointee) {
 77   assert(pointee != NULL, "invariant");
 78   assert(!reference.is_null(), "invariant");
 79 
 80   if (GranularTimer::is_finished()) {
 81     return;
 82   }
 83 
 84   if (_depth == 0 && _ignore_root_set) {
 85     // Root set is already marked, but we want
 86     // to continue, so skip is_marked check.
 87     assert(_mark_bits->is_marked(pointee), "invariant");
 88     _reference_stack[_depth] = reference;
 89   } else {
 90     if (_mark_bits->is_marked(pointee)) {
 91       return;
 92     }
 93     _mark_bits->mark_obj(pointee);
 94     _reference_stack[_depth] = reference;
 95     // is the pointee a sample object?
 96     if (pointee->mark().is_marked()) {
 97       add_chain();
 98     }
 99   }
100   assert(_max_depth >= 1, "invariant");
101   if (_depth < _max_depth - 1) {
102     _depth++;
103     pointee->oop_iterate(this);
104     assert(_depth > 0, "invariant");
105     _depth--;
106   }
107 }
108 
109 void DFSClosure::add_chain() {
110   const size_t array_length = _depth + 2;
111 
112   ResourceMark rm;
113   Edge* const chain = NEW_RESOURCE_ARRAY(Edge, array_length);
114   size_t idx = 0;
115 
116   // aggregate from depth-first search
117   for (size_t i = 0; i <= _depth; i++) {
118     const size_t next = idx + 1;
119     const size_t depth = _depth - i;
120     chain[idx++] = Edge(&chain[next], _reference_stack[depth]);
121   }
122   assert(_depth + 1 == idx, "invariant");
123   assert(array_length == idx + 1, "invariant");
124 
125   // aggregate from breadth-first search
126   if (_start_edge != NULL) {
127     chain[idx++] = *_start_edge;
128   } else {
129     chain[idx - 1] = Edge(NULL, chain[idx - 1].reference());
130   }
131   _edge_store->put_chain(chain, idx + (_start_edge != NULL ? _start_edge->distance_to_root() : 0));
132 }
133 
134 void DFSClosure::do_oop(oop* ref) {
135   assert(ref != NULL, "invariant");
136   assert(is_aligned(ref, HeapWordSize), "invariant");
137   const oop pointee = HeapAccess<AS_NO_KEEPALIVE>::oop_load(ref);
138   if (pointee != NULL) {
139     closure_impl(UnifiedOopRef::encode_in_heap(ref), pointee);
140   }
141 }
142 
143 void DFSClosure::do_oop(narrowOop* ref) {
144   assert(ref != NULL, "invariant");
145   assert(is_aligned(ref, sizeof(narrowOop)), "invariant");
146   const oop pointee = HeapAccess<AS_NO_KEEPALIVE>::oop_load(ref);
147   if (pointee != NULL) {
148     closure_impl(UnifiedOopRef::encode_in_heap(ref), pointee);
149   }
150 }
151 
152 void DFSClosure::do_root(UnifiedOopRef ref) {
153   assert(!ref.is_null(), "invariant");
154   const oop pointee = ref.dereference();
155   assert(pointee != NULL, "invariant");
156   closure_impl(ref, pointee);
157 }