Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/jfr/leakprofiler/chains/edgeStore.cpp
38922 views
/*1* Copyright (c) 2014, 2019, 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 "oops/oop.inline.hpp"2829StoredEdge::StoredEdge() : Edge() {}30StoredEdge::StoredEdge(const Edge* parent, const oop* 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) {}3536void StoredEdge::operator=(const StoredEdge& edge) {37Edge::operator=(edge);38_gc_root_id = edge._gc_root_id;39_skip_length = edge._skip_length;40}4142traceid EdgeStore::_edge_id_counter = 0;4344EdgeStore::EdgeStore() : _edges(NULL) {45_edges = new EdgeHashTable(this);46}4748EdgeStore::~EdgeStore() {49assert(_edges != NULL, "invariant");50delete _edges;51}5253bool EdgeStore::is_empty() const {54return !_edges->has_entries();55}5657void EdgeStore::assign_id(EdgeEntry* entry) {58assert(entry != NULL, "invariant");59assert(entry->id() == 0, "invariant");60entry->set_id(++_edge_id_counter);61}6263bool EdgeStore::equals(const Edge& query, uintptr_t hash, const EdgeEntry* entry) {64assert(entry != NULL, "invariant");65assert(entry->hash() == hash, "invariant");66return true;67}6869#ifdef ASSERT70bool EdgeStore::contains(const oop* reference) const {71return get(reference) != NULL;72}73#endif7475StoredEdge* EdgeStore::get(const oop* reference) const {76assert(reference != NULL, "invariant");77const StoredEdge e(NULL, reference);78EdgeEntry* const entry = _edges->lookup_only(e, (uintptr_t)reference);79return entry != NULL ? entry->literal_addr() : NULL;80}8182StoredEdge* EdgeStore::put(const oop* reference) {83assert(reference != NULL, "invariant");84const StoredEdge e(NULL, reference);85assert(NULL == _edges->lookup_only(e, (uintptr_t)reference), "invariant");86EdgeEntry& entry = _edges->put(e, (uintptr_t)reference);87return entry.literal_addr();88}8990traceid EdgeStore::get_id(const Edge* edge) const {91assert(edge != NULL, "invariant");92EdgeEntry* const entry = _edges->lookup_only(*edge, (uintptr_t)edge->reference());93assert(entry != NULL, "invariant");94return entry->id();95}9697traceid EdgeStore::gc_root_id(const Edge* edge) const {98assert(edge != NULL, "invariant");99const traceid gc_root_id = static_cast<const StoredEdge*>(edge)->gc_root_id();100if (gc_root_id != 0) {101return gc_root_id;102}103// not cached104assert(edge != NULL, "invariant");105const Edge* const root = EdgeUtils::root(*edge);106assert(root != NULL, "invariant");107assert(root->parent() == NULL, "invariant");108return get_id(root);109}110111static const Edge* get_skip_ancestor(const Edge** current, size_t distance_to_root, size_t* skip_length) {112assert(distance_to_root >= EdgeUtils::root_context, "invariant");113assert(*skip_length == 0, "invariant");114*skip_length = distance_to_root - (EdgeUtils::root_context - 1);115const Edge* const target = EdgeUtils::ancestor(**current, *skip_length);116assert(target != NULL, "invariant");117assert(target->distance_to_root() + 1 == EdgeUtils::root_context, "invariant");118return target;119}120121bool EdgeStore::put_skip_edge(StoredEdge** previous, const Edge** current, size_t distance_to_root) {122assert(*previous != NULL, "invariant");123assert((*previous)->parent() == NULL, "invariant");124assert(*current != NULL, "invariant");125assert((*current)->distance_to_root() == distance_to_root, "invariant");126127if (distance_to_root < EdgeUtils::root_context) {128// nothing to skip129return false;130}131132size_t skip_length = 0;133const Edge* const skip_ancestor = get_skip_ancestor(current, distance_to_root, &skip_length);134assert(skip_ancestor != NULL, "invariant");135(*previous)->set_skip_length(skip_length);136137// lookup target138StoredEdge* stored_target = get(skip_ancestor->reference());139if (stored_target != NULL) {140(*previous)->set_parent(stored_target);141// linked to existing, complete142return true;143}144145assert(stored_target == NULL, "invariant");146stored_target = put(skip_ancestor->reference());147assert(stored_target != NULL, "invariant");148(*previous)->set_parent(stored_target);149*previous = stored_target;150*current = skip_ancestor->parent();151return false;152}153154static void link_edge(const StoredEdge* current_stored, StoredEdge** previous) {155assert(current_stored != NULL, "invariant");156assert(*previous != NULL, "invariant");157assert((*previous)->parent() == NULL, "invariant");158(*previous)->set_parent(current_stored);159}160161static const StoredEdge* find_closest_skip_edge(const StoredEdge* edge, size_t* distance) {162assert(edge != NULL, "invariant");163assert(distance != NULL, "invariant");164const StoredEdge* current = edge;165*distance = 1;166while (current != NULL && !current->is_skip_edge()) {167++(*distance);168current = current->parent();169}170return current;171}172173void EdgeStore::link_with_existing_chain(const StoredEdge* current_stored, StoredEdge** previous, size_t previous_length) {174assert(current_stored != NULL, "invariant");175assert((*previous)->parent() == NULL, "invariant");176size_t distance_to_skip_edge; // including the skip edge itself177const StoredEdge* const closest_skip_edge = find_closest_skip_edge(current_stored, &distance_to_skip_edge);178if (closest_skip_edge == NULL) {179// no found skip edge implies root180if (distance_to_skip_edge + previous_length <= EdgeUtils::max_ref_chain_depth) {181link_edge(current_stored, previous);182return;183}184assert(current_stored->distance_to_root() == distance_to_skip_edge - 2, "invariant");185put_skip_edge(previous, reinterpret_cast<const Edge**>(¤t_stored), distance_to_skip_edge - 2);186return;187}188assert(closest_skip_edge->is_skip_edge(), "invariant");189if (distance_to_skip_edge + previous_length <= EdgeUtils::leak_context) {190link_edge(current_stored, previous);191return;192}193// create a new skip edge with derived information from closest skip edge194(*previous)->set_skip_length(distance_to_skip_edge + closest_skip_edge->skip_length());195(*previous)->set_parent(closest_skip_edge->parent());196}197198StoredEdge* EdgeStore::link_new_edge(StoredEdge** previous, const Edge** current) {199assert(*previous != NULL, "invariant");200assert((*previous)->parent() == NULL, "invariant");201assert(*current != NULL, "invariant");202assert(!contains((*current)->reference()), "invariant");203StoredEdge* const stored_edge = put((*current)->reference());204assert(stored_edge != NULL, "invariant");205link_edge(stored_edge, previous);206return stored_edge;207}208209bool EdgeStore::put_edges(StoredEdge** previous, const Edge** current, size_t limit) {210assert(*previous != NULL, "invariant");211assert(*current != NULL, "invariant");212size_t depth = 1;213while (*current != NULL && depth < limit) {214StoredEdge* stored_edge = get((*current)->reference());215if (stored_edge != NULL) {216link_with_existing_chain(stored_edge, previous, depth);217return true;218}219stored_edge = link_new_edge(previous, current);220assert((*previous)->parent() != NULL, "invariant");221*previous = stored_edge;222*current = (*current)->parent();223++depth;224}225return NULL == *current;226}227228// Install the immediate edge into the mark word of the leak candidate object229StoredEdge* EdgeStore::associate_leak_context_with_candidate(const Edge* edge) {230assert(edge != NULL, "invariant");231assert(!contains(edge->reference()), "invariant");232StoredEdge* const leak_context_edge = put(edge->reference());233oop sample_object = edge->pointee();234assert(sample_object != NULL, "invariant");235assert(NULL == sample_object->mark(), "invariant");236sample_object->set_mark(markOop(leak_context_edge));237return leak_context_edge;238}239240/*241* The purpose of put_chain() is to reify the edge sequence242* discovered during heap traversal with a normalized logical copy.243* This copy consist of two sub-sequences and a connecting link (skip edge).244*245* "current" can be thought of as the cursor (search) edge, it is not in the edge store.246* "previous" is always an edge in the edge store.247* The leak context edge is the edge adjacent to the leak candidate object, always an edge in the edge store.248*/249void EdgeStore::put_chain(const Edge* chain, size_t length) {250assert(chain != NULL, "invariant");251assert(chain->distance_to_root() + 1 == length, "invariant");252StoredEdge* const leak_context_edge = associate_leak_context_with_candidate(chain);253assert(leak_context_edge != NULL, "invariant");254assert(leak_context_edge->parent() == NULL, "invariant");255256if (1 == length) {257return;258}259260const Edge* current = chain->parent();261assert(current != NULL, "invariant");262StoredEdge* previous = leak_context_edge;263264// a leak context is the sequence of (limited) edges reachable from the leak candidate265if (put_edges(&previous, ¤t, EdgeUtils::leak_context)) {266// complete267assert(previous != NULL, "invariant");268put_chain_epilogue(leak_context_edge, EdgeUtils::root(*previous));269return;270}271272const size_t distance_to_root = length > EdgeUtils::leak_context ? length - 1 - EdgeUtils::leak_context : length - 1;273assert(current->distance_to_root() == distance_to_root, "invariant");274275// a skip edge is the logical link276// connecting the leak context sequence with the root context sequence277if (put_skip_edge(&previous, ¤t, distance_to_root)) {278// complete279assert(previous != NULL, "invariant");280assert(previous->is_skip_edge(), "invariant");281assert(previous->parent() != NULL, "invariant");282put_chain_epilogue(leak_context_edge, EdgeUtils::root(*previous->parent()));283return;284}285286assert(current->distance_to_root() < EdgeUtils::root_context, "invariant");287288// a root context is the sequence of (limited) edges reachable from the root289put_edges(&previous, ¤t, EdgeUtils::root_context);290assert(previous != NULL, "invariant");291put_chain_epilogue(leak_context_edge, EdgeUtils::root(*previous));292}293294void EdgeStore::put_chain_epilogue(StoredEdge* leak_context_edge, const Edge* root) const {295assert(leak_context_edge != NULL, "invariant");296assert(root != NULL, "invariant");297store_gc_root_id_in_leak_context_edge(leak_context_edge, root);298assert(leak_context_edge->distance_to_root() + 1 <= EdgeUtils::max_ref_chain_depth, "invariant");299}300301// To avoid another traversal to resolve the root edge id later,302// cache it in the immediate leak context edge for fast retrieval.303void EdgeStore::store_gc_root_id_in_leak_context_edge(StoredEdge* leak_context_edge, const Edge* root) const {304assert(leak_context_edge != NULL, "invariant");305assert(leak_context_edge->gc_root_id() == 0, "invariant");306assert(root != NULL, "invariant");307assert(root->parent() == NULL, "invariant");308assert(root->distance_to_root() == 0, "invariant");309const StoredEdge* const stored_root = static_cast<const StoredEdge*>(root);310traceid root_id = stored_root->gc_root_id();311if (root_id == 0) {312root_id = get_id(root);313stored_root->set_gc_root_id(root_id);314}315assert(root_id != 0, "invariant");316leak_context_edge->set_gc_root_id(root_id);317assert(leak_context_edge->gc_root_id() == stored_root->gc_root_id(), "invariant");318}319320321