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 #include "precompiled.hpp" 25 #include "jfr/leakprofiler/chains/bitset.hpp" 26 #include "jfr/leakprofiler/chains/bfsClosure.hpp" 27 #include "jfr/leakprofiler/chains/dfsClosure.hpp" 28 #include "jfr/leakprofiler/chains/edge.hpp" 29 #include "jfr/leakprofiler/chains/edgeStore.hpp" 30 #include "jfr/leakprofiler/chains/edgeQueue.hpp" 31 #include "jfr/leakprofiler/utilities/granularTimer.hpp" 32 #include "jfr/leakprofiler/utilities/unifiedOop.hpp" 33 #include "logging/log.hpp" 34 #include "memory/resourceArea.hpp" 35 #include "oops/access.inline.hpp" 36 #include "utilities/align.hpp" 37 38 BFSClosure::BFSClosure(EdgeQueue* edge_queue, EdgeStore* edge_store, BitSet* mark_bits) : 39 _edge_queue(edge_queue), 40 _edge_store(edge_store), 41 _mark_bits(mark_bits), 42 _current_parent(NULL), 43 _current_frontier_level(0), 44 _next_frontier_idx(0), 45 _prev_frontier_idx(0), 46 _dfs_fallback_idx(0), 47 _use_dfs(false) { 48 } 49 50 static void log_frontier_level_summary(size_t level, 51 size_t high_idx, 52 size_t low_idx, 53 size_t edge_size) { 54 const size_t nof_edges_in_frontier = high_idx - low_idx; 55 log_trace(jfr, system)( 56 "BFS front: " SIZE_FORMAT " edges: " SIZE_FORMAT " size: " SIZE_FORMAT " [KB]", 57 level, 58 nof_edges_in_frontier, 59 (nof_edges_in_frontier * edge_size) / K 60 ); 61 } 62 63 void BFSClosure::log_completed_frontier() const { 64 log_frontier_level_summary(_current_frontier_level, 65 _next_frontier_idx, 66 _prev_frontier_idx, 67 _edge_queue->sizeof_edge()); 68 } 69 70 void BFSClosure::log_dfs_fallback() const { 71 const size_t edge_size = _edge_queue->sizeof_edge(); 72 // first complete summary for frontier in progress 73 log_frontier_level_summary(_current_frontier_level, 74 _next_frontier_idx, 75 _prev_frontier_idx, 76 edge_size); 77 78 // and then also complete the last frontier 79 log_frontier_level_summary(_current_frontier_level + 1, 80 _edge_queue->bottom(), 81 _next_frontier_idx, 82 edge_size); 83 84 // additional information about DFS fallover 85 log_trace(jfr, system)( 86 "BFS front: " SIZE_FORMAT " filled edge queue at edge: " SIZE_FORMAT, 87 _current_frontier_level, 88 _dfs_fallback_idx 89 ); 90 91 const size_t nof_dfs_completed_edges = _edge_queue->bottom() - _dfs_fallback_idx; 92 log_trace(jfr, system)( 93 "DFS to complete " SIZE_FORMAT " edges size: " SIZE_FORMAT " [KB]", 94 nof_dfs_completed_edges, 95 (nof_dfs_completed_edges * edge_size) / K 96 ); 97 } 98 99 void BFSClosure::process() { 100 101 process_root_set(); 102 process_queue(); 103 } 104 105 void BFSClosure::process_root_set() { 106 for (size_t idx = _edge_queue->bottom(); idx < _edge_queue->top(); ++idx) { 107 const Edge* edge = _edge_queue->element_at(idx); 108 assert(edge->parent() == NULL, "invariant"); 109 process(edge->reference(), edge->pointee()); 110 } 111 } 112 113 void BFSClosure::process(const oop* reference, const oop pointee) { 114 closure_impl(reference, pointee); 115 } 116 void BFSClosure::closure_impl(const oop* reference, const oop pointee) { 117 assert(reference != NULL, "invariant"); 118 assert(UnifiedOop::dereference(reference) == pointee, "invariant"); 119 120 if (GranularTimer::is_finished()) { 121 return; 122 } 123 124 if (_use_dfs) { 125 assert(_current_parent != NULL, "invariant"); 126 DFSClosure::find_leaks_from_edge(_edge_store, _mark_bits, _current_parent); 127 return; 128 } 129 130 if (!_mark_bits->is_marked(pointee)) { 131 _mark_bits->mark_obj(pointee); 132 // is the pointee a sample object? 133 if (NULL == pointee->mark()) { 134 add_chain(reference, pointee); 135 } 136 137 // if we are processinig initial root set, don't add to queue 138 if (_current_parent != NULL) { 139 assert(_current_parent->distance_to_root() == _current_frontier_level, "invariant"); 140 _edge_queue->add(_current_parent, reference); 141 } 142 143 if (_edge_queue->is_full()) { 144 dfs_fallback(); 145 } 146 } 147 } 148 149 void BFSClosure::add_chain(const oop* reference, const oop pointee) { 150 assert(pointee != NULL, "invariant"); 151 assert(NULL == pointee->mark(), "invariant"); 152 153 const size_t length = _current_parent == NULL ? 1 : _current_parent->distance_to_root() + 2; 154 ResourceMark rm; 155 Edge* const chain = NEW_RESOURCE_ARRAY(Edge, length); 156 size_t idx = 0; 157 chain[idx++] = Edge(NULL, reference); 158 // aggregate from breadth-first search 159 const Edge* current = _current_parent; 160 while (current != NULL) { 161 chain[idx++] = Edge(NULL, current->reference()); 162 current = current->parent(); 163 } 164 assert(length == idx, "invariant"); 165 _edge_store->add_chain(chain, length); 166 } 167 168 void BFSClosure::dfs_fallback() { 169 assert(_edge_queue->is_full(), "invariant"); 170 _use_dfs = true; 171 _dfs_fallback_idx = _edge_queue->bottom(); 172 while (!_edge_queue->is_empty()) { 173 const Edge* edge = _edge_queue->remove(); 174 if (edge->pointee() != NULL) { 175 DFSClosure::find_leaks_from_edge(_edge_store, _mark_bits, edge); 176 } 177 } 178 } 179 180 void BFSClosure::process_queue() { 181 assert(_current_frontier_level == 0, "invariant"); 182 assert(_next_frontier_idx == 0, "invariant"); 183 assert(_prev_frontier_idx == 0, "invariant"); 184 185 _next_frontier_idx = _edge_queue->top(); 186 while (!is_complete()) { 187 iterate(_edge_queue->remove()); // edge_queue.remove() increments bottom 188 } 189 } 190 191 void BFSClosure::step_frontier() const { 192 log_completed_frontier(); 193 ++_current_frontier_level; 194 _prev_frontier_idx = _next_frontier_idx; 195 _next_frontier_idx = _edge_queue->top(); 196 } 197 198 bool BFSClosure::is_complete() const { 199 if (_edge_queue->bottom() < _next_frontier_idx) { 200 return false; 201 } 202 if (_edge_queue->bottom() > _next_frontier_idx) { 203 // fallback onto DFS as part of processing the frontier 204 assert(_dfs_fallback_idx >= _prev_frontier_idx, "invariant"); 205 assert(_dfs_fallback_idx < _next_frontier_idx, "invariant"); 206 log_dfs_fallback(); 207 return true; 208 } 209 assert(_edge_queue->bottom() == _next_frontier_idx, "invariant"); 210 if (_edge_queue->is_empty()) { 211 return true; 212 } 213 step_frontier(); 214 return false; 215 } 216 217 void BFSClosure::iterate(const Edge* parent) { 218 assert(parent != NULL, "invariant"); 219 const oop pointee = parent->pointee(); 220 assert(pointee != NULL, "invariant"); 221 _current_parent = parent; 222 pointee->oop_iterate(this); 223 } 224 225 void BFSClosure::do_oop(oop* ref) { 226 assert(ref != NULL, "invariant"); 227 assert(is_aligned(ref, HeapWordSize), "invariant"); 228 const oop pointee = *ref; 229 if (pointee != NULL) { 230 closure_impl(ref, pointee); 231 } 232 } 233 234 void BFSClosure::do_oop(narrowOop* ref) { 235 assert(ref != NULL, "invariant"); 236 assert(is_aligned(ref, sizeof(narrowOop)), "invariant"); 237 const oop pointee = RawAccess<>::oop_load(ref); 238 if (pointee != NULL) { 239 closure_impl(UnifiedOop::encode(ref), pointee); 240 } 241 }