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