Path: blob/master/src/hotspot/share/jfr/leakprofiler/chains/edgeStore.cpp
41153 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "jfr/leakprofiler/chains/edgeStore.hpp"26#include "jfr/leakprofiler/chains/edgeUtils.hpp"27#include "jfr/leakprofiler/utilities/unifiedOopRef.inline.hpp"28#include "oops/oop.inline.hpp"2930StoredEdge::StoredEdge(const Edge* parent, UnifiedOopRef reference) : Edge(parent, reference), _gc_root_id(0), _skip_length(0) {}3132StoredEdge::StoredEdge(const Edge& edge) : Edge(edge), _gc_root_id(0), _skip_length(0) {}3334StoredEdge::StoredEdge(const StoredEdge& edge) : Edge(edge), _gc_root_id(edge._gc_root_id), _skip_length(edge._skip_length) {}3536traceid EdgeStore::_edge_id_counter = 0;3738EdgeStore::EdgeStore() : _edges(NULL) {39_edges = new EdgeHashTable(this);40}4142EdgeStore::~EdgeStore() {43assert(_edges != NULL, "invariant");44delete _edges;45}4647bool EdgeStore::is_empty() const {48return !_edges->has_entries();49}5051void EdgeStore::on_link(EdgeEntry* entry) {52assert(entry != NULL, "invariant");53assert(entry->id() == 0, "invariant");54entry->set_id(++_edge_id_counter);55}5657bool EdgeStore::on_equals(uintptr_t hash, const EdgeEntry* entry) {58assert(entry != NULL, "invariant");59assert(entry->hash() == hash, "invariant");60return true;61}6263void EdgeStore::on_unlink(EdgeEntry* entry) {64assert(entry != NULL, "invariant");65// nothing66}6768#ifdef ASSERT69bool EdgeStore::contains(UnifiedOopRef reference) const {70return get(reference) != NULL;71}72#endif7374StoredEdge* EdgeStore::get(UnifiedOopRef reference) const {75assert(!reference.is_null(), "invariant");76EdgeEntry* const entry = _edges->lookup_only(reference.addr<uintptr_t>());77return entry != NULL ? entry->literal_addr() : NULL;78}7980StoredEdge* EdgeStore::put(UnifiedOopRef reference) {81assert(!reference.is_null(), "invariant");82const StoredEdge e(NULL, reference);83assert(NULL == _edges->lookup_only(reference.addr<uintptr_t>()), "invariant");84EdgeEntry& entry = _edges->put(reference.addr<uintptr_t>(), e);85return entry.literal_addr();86}8788traceid EdgeStore::get_id(const Edge* edge) const {89assert(edge != NULL, "invariant");90EdgeEntry* const entry = _edges->lookup_only(edge->reference().addr<uintptr_t>());91assert(entry != NULL, "invariant");92return entry->id();93}9495traceid EdgeStore::gc_root_id(const Edge* edge) const {96assert(edge != NULL, "invariant");97const traceid gc_root_id = static_cast<const StoredEdge*>(edge)->gc_root_id();98if (gc_root_id != 0) {99return gc_root_id;100}101// not cached102assert(edge != NULL, "invariant");103const Edge* const root = EdgeUtils::root(*edge);104assert(root != NULL, "invariant");105assert(root->parent() == NULL, "invariant");106return get_id(root);107}108109static const Edge* get_skip_ancestor(const Edge** current, size_t distance_to_root, size_t* skip_length) {110assert(distance_to_root >= EdgeUtils::root_context, "invariant");111assert(*skip_length == 0, "invariant");112*skip_length = distance_to_root - (EdgeUtils::root_context - 1);113const Edge* const target = EdgeUtils::ancestor(**current, *skip_length);114assert(target != NULL, "invariant");115assert(target->distance_to_root() + 1 == EdgeUtils::root_context, "invariant");116return target;117}118119bool EdgeStore::put_skip_edge(StoredEdge** previous, const Edge** current, size_t distance_to_root) {120assert(*previous != NULL, "invariant");121assert((*previous)->parent() == NULL, "invariant");122assert(*current != NULL, "invariant");123assert((*current)->distance_to_root() == distance_to_root, "invariant");124125if (distance_to_root < EdgeUtils::root_context) {126// nothing to skip127return false;128}129130size_t skip_length = 0;131const Edge* const skip_ancestor = get_skip_ancestor(current, distance_to_root, &skip_length);132assert(skip_ancestor != NULL, "invariant");133(*previous)->set_skip_length(skip_length);134135// lookup target136StoredEdge* stored_target = get(skip_ancestor->reference());137if (stored_target != NULL) {138(*previous)->set_parent(stored_target);139// linked to existing, complete140return true;141}142143assert(stored_target == NULL, "invariant");144stored_target = put(skip_ancestor->reference());145assert(stored_target != NULL, "invariant");146(*previous)->set_parent(stored_target);147*previous = stored_target;148*current = skip_ancestor->parent();149return false;150}151152static void link_edge(const StoredEdge* current_stored, StoredEdge** previous) {153assert(current_stored != NULL, "invariant");154assert(*previous != NULL, "invariant");155assert((*previous)->parent() == NULL, "invariant");156(*previous)->set_parent(current_stored);157}158159static const StoredEdge* find_closest_skip_edge(const StoredEdge* edge, size_t* distance) {160assert(edge != NULL, "invariant");161assert(distance != NULL, "invariant");162const StoredEdge* current = edge;163*distance = 1;164while (current != NULL && !current->is_skip_edge()) {165++(*distance);166current = current->parent();167}168return current;169}170171void EdgeStore::link_with_existing_chain(const StoredEdge* current_stored, StoredEdge** previous, size_t previous_length) {172assert(current_stored != NULL, "invariant");173assert((*previous)->parent() == NULL, "invariant");174size_t distance_to_skip_edge; // including the skip edge itself175const StoredEdge* const closest_skip_edge = find_closest_skip_edge(current_stored, &distance_to_skip_edge);176if (closest_skip_edge == NULL) {177// no found skip edge implies root178if (distance_to_skip_edge + previous_length <= EdgeUtils::max_ref_chain_depth) {179link_edge(current_stored, previous);180return;181}182assert(current_stored->distance_to_root() == distance_to_skip_edge - 2, "invariant");183put_skip_edge(previous, reinterpret_cast<const Edge**>(¤t_stored), distance_to_skip_edge - 2);184return;185}186assert(closest_skip_edge->is_skip_edge(), "invariant");187if (distance_to_skip_edge + previous_length <= EdgeUtils::leak_context) {188link_edge(current_stored, previous);189return;190}191// create a new skip edge with derived information from closest skip edge192(*previous)->set_skip_length(distance_to_skip_edge + closest_skip_edge->skip_length());193(*previous)->set_parent(closest_skip_edge->parent());194}195196StoredEdge* EdgeStore::link_new_edge(StoredEdge** previous, const Edge** current) {197assert(*previous != NULL, "invariant");198assert((*previous)->parent() == NULL, "invariant");199assert(*current != NULL, "invariant");200assert(!contains((*current)->reference()), "invariant");201StoredEdge* const stored_edge = put((*current)->reference());202assert(stored_edge != NULL, "invariant");203link_edge(stored_edge, previous);204return stored_edge;205}206207bool EdgeStore::put_edges(StoredEdge** previous, const Edge** current, size_t limit) {208assert(*previous != NULL, "invariant");209assert(*current != NULL, "invariant");210size_t depth = 1;211while (*current != NULL && depth < limit) {212StoredEdge* stored_edge = get((*current)->reference());213if (stored_edge != NULL) {214link_with_existing_chain(stored_edge, previous, depth);215return true;216}217stored_edge = link_new_edge(previous, current);218assert((*previous)->parent() != NULL, "invariant");219*previous = stored_edge;220*current = (*current)->parent();221++depth;222}223return NULL == *current;224}225226// Install the immediate edge into the mark word of the leak candidate object227StoredEdge* EdgeStore::associate_leak_context_with_candidate(const Edge* edge) {228assert(edge != NULL, "invariant");229assert(!contains(edge->reference()), "invariant");230StoredEdge* const leak_context_edge = put(edge->reference());231oop sample_object = edge->pointee();232assert(sample_object != NULL, "invariant");233assert(sample_object->mark().is_marked(), "invariant");234sample_object->set_mark(markWord::from_pointer(leak_context_edge));235return leak_context_edge;236}237238/*239* The purpose of put_chain() is to reify the edge sequence240* discovered during heap traversal with a normalized logical copy.241* This copy consist of two sub-sequences and a connecting link (skip edge).242*243* "current" can be thought of as the cursor (search) edge, it is not in the edge store.244* "previous" is always an edge in the edge store.245* The leak context edge is the edge adjacent to the leak candidate object, always an edge in the edge store.246*/247void EdgeStore::put_chain(const Edge* chain, size_t length) {248assert(chain != NULL, "invariant");249assert(chain->distance_to_root() + 1 == length, "invariant");250StoredEdge* const leak_context_edge = associate_leak_context_with_candidate(chain);251assert(leak_context_edge != NULL, "invariant");252assert(leak_context_edge->parent() == NULL, "invariant");253254if (1 == length) {255store_gc_root_id_in_leak_context_edge(leak_context_edge, leak_context_edge);256return;257}258259const Edge* current = chain->parent();260assert(current != NULL, "invariant");261StoredEdge* previous = leak_context_edge;262263// a leak context is the sequence of (limited) edges reachable from the leak candidate264if (put_edges(&previous, ¤t, EdgeUtils::leak_context)) {265// complete266assert(previous != NULL, "invariant");267put_chain_epilogue(leak_context_edge, EdgeUtils::root(*previous));268return;269}270271const size_t distance_to_root = length > EdgeUtils::leak_context ? length - 1 - EdgeUtils::leak_context : length - 1;272assert(current->distance_to_root() == distance_to_root, "invariant");273274// a skip edge is the logical link275// connecting the leak context sequence with the root context sequence276if (put_skip_edge(&previous, ¤t, distance_to_root)) {277// complete278assert(previous != NULL, "invariant");279assert(previous->is_skip_edge(), "invariant");280assert(previous->parent() != NULL, "invariant");281put_chain_epilogue(leak_context_edge, EdgeUtils::root(*previous->parent()));282return;283}284285assert(current->distance_to_root() < EdgeUtils::root_context, "invariant");286287// a root context is the sequence of (limited) edges reachable from the root288put_edges(&previous, ¤t, EdgeUtils::root_context);289assert(previous != NULL, "invariant");290put_chain_epilogue(leak_context_edge, EdgeUtils::root(*previous));291}292293void EdgeStore::put_chain_epilogue(StoredEdge* leak_context_edge, const Edge* root) const {294assert(leak_context_edge != NULL, "invariant");295assert(root != NULL, "invariant");296store_gc_root_id_in_leak_context_edge(leak_context_edge, root);297assert(leak_context_edge->distance_to_root() + 1 <= EdgeUtils::max_ref_chain_depth, "invariant");298}299300// To avoid another traversal to resolve the root edge id later,301// cache it in the immediate leak context edge for fast retrieval.302void EdgeStore::store_gc_root_id_in_leak_context_edge(StoredEdge* leak_context_edge, const Edge* root) const {303assert(leak_context_edge != NULL, "invariant");304assert(leak_context_edge->gc_root_id() == 0, "invariant");305assert(root != NULL, "invariant");306assert(root->parent() == NULL, "invariant");307assert(root->distance_to_root() == 0, "invariant");308const StoredEdge* const stored_root = static_cast<const StoredEdge*>(root);309traceid root_id = stored_root->gc_root_id();310if (root_id == 0) {311root_id = get_id(root);312stored_root->set_gc_root_id(root_id);313}314assert(root_id != 0, "invariant");315leak_context_edge->set_gc_root_id(root_id);316assert(leak_context_edge->gc_root_id() == stored_root->gc_root_id(), "invariant");317}318319320