Path: blob/master/src/hotspot/share/jfr/leakprofiler/chains/pathToGcRootsOperation.cpp
41153 views
/*1* Copyright (c) 2019, 2021, 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 "gc/shared/collectedHeap.hpp"26#include "gc/shared/gc_globals.hpp"27#include "jfr/leakprofiler/leakProfiler.hpp"28#include "jfr/leakprofiler/chains/bfsClosure.hpp"29#include "jfr/leakprofiler/chains/bitset.inline.hpp"30#include "jfr/leakprofiler/chains/dfsClosure.hpp"31#include "jfr/leakprofiler/chains/edge.hpp"32#include "jfr/leakprofiler/chains/edgeQueue.hpp"33#include "jfr/leakprofiler/chains/edgeStore.hpp"34#include "jfr/leakprofiler/chains/objectSampleMarker.hpp"35#include "jfr/leakprofiler/chains/rootSetClosure.hpp"36#include "jfr/leakprofiler/chains/edgeStore.hpp"37#include "jfr/leakprofiler/chains/objectSampleMarker.hpp"38#include "jfr/leakprofiler/chains/pathToGcRootsOperation.hpp"39#include "jfr/leakprofiler/checkpoint/eventEmitter.hpp"40#include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp"41#include "jfr/leakprofiler/sampling/objectSample.hpp"42#include "jfr/leakprofiler/sampling/objectSampler.hpp"43#include "jfr/leakprofiler/utilities/granularTimer.hpp"44#include "logging/log.hpp"45#include "memory/universe.hpp"46#include "oops/oop.inline.hpp"47#include "runtime/safepoint.hpp"48#include "utilities/globalDefinitions.hpp"4950PathToGcRootsOperation::PathToGcRootsOperation(ObjectSampler* sampler, EdgeStore* edge_store, int64_t cutoff, bool emit_all, bool skip_bfs) :51_sampler(sampler),_edge_store(edge_store), _cutoff_ticks(cutoff), _emit_all(emit_all), _skip_bfs(skip_bfs) {}5253/* The EdgeQueue is backed by directly managed virtual memory.54* We will attempt to dimension an initial reservation55* in proportion to the size of the heap (represented by heap_region).56* Initial memory reservation: 5% of the heap OR at least 32 Mb57* Commit ratio: 1 : 10 (subject to allocation granularties)58*/59static size_t edge_queue_memory_reservation() {60const size_t memory_reservation_bytes = MAX2(MaxHeapSize / 20, 32*M);61assert(memory_reservation_bytes >= (size_t)32*M, "invariant");62return memory_reservation_bytes;63}6465static size_t edge_queue_memory_commit_size(size_t memory_reservation_bytes) {66const size_t memory_commit_block_size_bytes = memory_reservation_bytes / 10;67assert(memory_commit_block_size_bytes >= (size_t)3*M, "invariant");68return memory_commit_block_size_bytes;69}7071static void log_edge_queue_summary(const EdgeQueue& edge_queue) {72log_trace(jfr, system)("EdgeQueue reserved size total: " SIZE_FORMAT " [KB]", edge_queue.reserved_size() / K);73log_trace(jfr, system)("EdgeQueue edges total: " SIZE_FORMAT, edge_queue.top());74log_trace(jfr, system)("EdgeQueue liveset total: " SIZE_FORMAT " [KB]", edge_queue.live_set() / K);75if (edge_queue.reserved_size() > 0) {76log_trace(jfr, system)("EdgeQueue commit reserve ratio: %f\n",77((double)edge_queue.live_set() / (double)edge_queue.reserved_size()));78}79}8081void PathToGcRootsOperation::doit() {82assert(SafepointSynchronize::is_at_safepoint(), "invariant");83assert(_cutoff_ticks > 0, "invariant");8485// The bitset used for marking is dimensioned as a function of the heap size86BitSet mark_bits;8788// The edge queue is dimensioned as a fraction of the heap size89const size_t edge_queue_reservation_size = edge_queue_memory_reservation();90EdgeQueue edge_queue(edge_queue_reservation_size, edge_queue_memory_commit_size(edge_queue_reservation_size));9192// The initialize() routines will attempt to reserve and allocate backing storage memory.93// Failure to accommodate will render root chain processing impossible.94// As a fallback on failure, just write out the existing samples, flat, without chains.95if (!edge_queue.initialize()) {96log_warning(jfr)("Unable to allocate memory for root chain processing");97return;98}99100// Save the original markWord for the potential leak objects,101// to be restored on function exit102ObjectSampleMarker marker;103if (ObjectSampleCheckpoint::save_mark_words(_sampler, marker, _emit_all) == 0) {104// no valid samples to process105return;106}107108// Necessary condition for attempting a root set iteration109Universe::heap()->ensure_parsability(false);110111BFSClosure bfs(&edge_queue, _edge_store, &mark_bits);112RootSetClosure<BFSClosure> roots(&bfs);113114GranularTimer::start(_cutoff_ticks, 1000000);115roots.process();116if (edge_queue.is_full() || _skip_bfs) {117// Pathological case where roots don't fit in queue118// Do a depth-first search, but mark roots first119// to avoid walking sideways over roots120DFSClosure::find_leaks_from_root_set(_edge_store, &mark_bits);121} else {122bfs.process();123}124GranularTimer::stop();125log_edge_queue_summary(edge_queue);126127// Emit old objects including their reference chains as events128EventEmitter emitter(GranularTimer::start_time(), GranularTimer::end_time());129emitter.write_events(_sampler, _edge_store, _emit_all);130}131132133