Path: blob/master/src/hotspot/share/jfr/leakprofiler/checkpoint/eventEmitter.cpp
41155 views
/*1* Copyright (c) 2019, 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/jfrEvents.hpp"26#include "jfr/leakprofiler/chains/edgeStore.hpp"27#include "jfr/leakprofiler/chains/pathToGcRootsOperation.hpp"28#include "jfr/leakprofiler/checkpoint/eventEmitter.hpp"29#include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp"30#include "jfr/leakprofiler/sampling/objectSample.hpp"31#include "jfr/leakprofiler/sampling/objectSampler.hpp"32#include "jfr/leakprofiler/utilities/unifiedOopRef.inline.hpp"33#include "logging/log.hpp"34#include "memory/resourceArea.hpp"35#include "oops/markWord.hpp"36#include "oops/oop.inline.hpp"37#include "runtime/mutexLocker.hpp"38#include "runtime/thread.inline.hpp"39#include "runtime/vmThread.hpp"4041EventEmitter::EventEmitter(const JfrTicks& start_time, const JfrTicks& end_time) :42_start_time(start_time),43_end_time(end_time),44_thread(Thread::current()),45_jfr_thread_local(_thread->jfr_thread_local()),46_thread_id(_thread->jfr_thread_local()->thread_id()) {}4748EventEmitter::~EventEmitter() {49// restore / reset thread local stack trace and thread id50_jfr_thread_local->set_thread_id(_thread_id);51_jfr_thread_local->clear_cached_stack_trace();52}5354void EventEmitter::emit(ObjectSampler* sampler, int64_t cutoff_ticks, bool emit_all, bool skip_bfs) {55assert(sampler != NULL, "invariant");56ResourceMark rm;57EdgeStore edge_store;58if (cutoff_ticks <= 0) {59// no reference chains60JfrTicks time_stamp = JfrTicks::now();61EventEmitter emitter(time_stamp, time_stamp);62emitter.write_events(sampler, &edge_store, emit_all);63return;64}65// events emitted with reference chains require a safepoint operation66PathToGcRootsOperation op(sampler, &edge_store, cutoff_ticks, emit_all, skip_bfs);67VMThread::execute(&op);68}6970size_t EventEmitter::write_events(ObjectSampler* object_sampler, EdgeStore* edge_store, bool emit_all) {71assert(_thread == Thread::current(), "invariant");72assert(_thread->jfr_thread_local() == _jfr_thread_local, "invariant");73assert(object_sampler != NULL, "invariant");74assert(edge_store != NULL, "invariant");7576const jlong last_sweep = emit_all ? max_jlong : ObjectSampler::last_sweep();77size_t count = 0;7879// First pass associates a live sample with its immediate edge80// in preparation for writing checkpoint information.81const ObjectSample* current = object_sampler->first();82while (current != NULL) {83ObjectSample* prev = current->prev();84if (current->is_alive_and_older_than(last_sweep)) {85link_sample_with_edge(current, edge_store);86++count;87}88current = prev;89}90if (count > 0) {91// We need to serialize the associated checkpoints and potential chains92// before writing the events to ensure constants are available for resolution93// at the time old object sample events appear in the stream.94ObjectSampleCheckpoint::write(object_sampler, edge_store, emit_all, _thread);9596// Now we are ready to write the events97const ObjectSample* current = object_sampler->first();98while (current != NULL) {99ObjectSample* prev = current->prev();100if (current->is_alive_and_older_than(last_sweep)) {101write_event(current, edge_store);102}103current = prev;104}105}106return count;107}108109static int array_size(const oop object) {110assert(object != NULL, "invariant");111if (object->is_array()) {112return arrayOop(object)->length();113}114return min_jint;115}116117void EventEmitter::link_sample_with_edge(const ObjectSample* sample, EdgeStore* edge_store) {118assert(sample != NULL, "invariant");119assert(!sample->is_dead(), "invariant");120assert(edge_store != NULL, "invariant");121if (SafepointSynchronize::is_at_safepoint()) {122if (!sample->object()->mark().is_marked()) {123// Associated with an edge (chain) already during heap traversal.124return;125}126}127// In order to dump out a representation of the event128// even though the sample object was found not reachable / too long to reach,129// we need to register a top level edge.130edge_store->put(UnifiedOopRef::encode_in_native(sample->object_addr()));131}132133void EventEmitter::write_event(const ObjectSample* sample, EdgeStore* edge_store) {134assert(sample != NULL, "invariant");135assert(!sample->is_dead(), "invariant");136assert(edge_store != NULL, "invariant");137assert(_jfr_thread_local != NULL, "invariant");138139traceid gc_root_id = 0;140const Edge* edge = NULL;141if (SafepointSynchronize::is_at_safepoint()) {142if (!sample->object()->mark().is_marked()) {143edge = (const Edge*)(sample->object())->mark().to_pointer();144}145}146if (edge == NULL) {147edge = edge_store->get(UnifiedOopRef::encode_in_native(sample->object_addr()));148} else {149gc_root_id = edge_store->gc_root_id(edge);150}151assert(edge != NULL, "invariant");152const traceid object_id = edge_store->get_id(edge);153assert(object_id != 0, "invariant");154155Tickspan object_age = Ticks(_start_time.value()) - sample->allocation_time();156157EventOldObjectSample e(UNTIMED);158e.set_starttime(_start_time);159e.set_endtime(_end_time);160e.set_allocationTime(sample->allocation_time());161e.set_objectAge(object_age);162e.set_lastKnownHeapUsage(sample->heap_used_at_last_gc());163e.set_object(object_id);164e.set_arrayElements(array_size(edge->pointee()));165e.set_root(gc_root_id);166167// Temporarily assigning both the stack trace id and thread id168// onto the thread local data structure of the emitter thread (for the duration169// of the commit() call). This trick provides a means to override170// the event generation mechanism by injecting externally provided id's.171// At this particular location, it allows us to emit an old object event172// supplying information from where the actual sampling occurred.173_jfr_thread_local->set_cached_stack_trace_id(sample->stack_trace_id());174assert(sample->has_thread(), "invariant");175_jfr_thread_local->set_thread_id(sample->thread_id());176e.commit();177}178179180