Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/jfr/leakprofiler/sampling/objectSampler.cpp
38922 views
/*1* Copyright (c) 2017, 2018, 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*/23#include "precompiled.hpp"24#include "jfr/jfrEvents.hpp"25#include "jfr/leakprofiler/sampling/objectSample.hpp"26#include "jfr/leakprofiler/sampling/objectSampler.hpp"27#include "jfr/leakprofiler/sampling/sampleList.hpp"28#include "jfr/leakprofiler/sampling/samplePriorityQueue.hpp"29#include "jfr/recorder/jfrEventSetting.inline.hpp"30#include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"31#include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"32#include "jfr/support/jfrThreadLocal.hpp"33#include "jfr/utilities/jfrTryLock.hpp"34#include "memory/universe.hpp"35#include "oops/oop.inline.hpp"36#include "runtime/atomic.hpp"37#include "runtime/orderAccess.hpp"38#include "runtime/safepoint.hpp"39#include "runtime/thread.hpp"4041static ObjectSampler* _instance = NULL;4243static ObjectSampler& instance() {44assert(_instance != NULL, "invariant");45return *_instance;46}4748ObjectSampler::ObjectSampler(size_t size) :49_priority_queue(new SamplePriorityQueue(size)),50_list(new SampleList(size)),51_last_sweep(JfrTicks::now()),52_total_allocated(0),53_threshold(0),54_size(size),55_dead_samples(false) {}5657ObjectSampler::~ObjectSampler() {58delete _priority_queue;59_priority_queue = NULL;60delete _list;61_list = NULL;62}6364bool ObjectSampler::create(size_t size) {65assert(SafepointSynchronize::is_at_safepoint(), "invariant");66assert(_instance == NULL, "invariant");67_instance = new ObjectSampler(size);68return _instance != NULL;69}7071bool ObjectSampler::is_created() {72return _instance != NULL;73}7475ObjectSampler* ObjectSampler::sampler() {76assert(is_created(), "invariant");77return _instance;78}7980void ObjectSampler::destroy() {81assert(SafepointSynchronize::is_at_safepoint(), "invariant");82if (_instance != NULL) {83ObjectSampler* const sampler = _instance;84_instance = NULL;85delete sampler;86}87}8889static volatile int _lock = 0;9091ObjectSampler* ObjectSampler::acquire() {92assert(is_created(), "invariant");93while (Atomic::cmpxchg(1, &_lock, 0) == 1) {}94return _instance;95}9697void ObjectSampler::release() {98assert(is_created(), "invariant");99OrderAccess::fence();100_lock = 0;101}102103static traceid get_thread_id(JavaThread* thread) {104assert(thread != NULL, "invariant");105if (thread->threadObj() == NULL) {106return 0;107}108const JfrThreadLocal* const tl = thread->jfr_thread_local();109assert(tl != NULL, "invariant");110if (!tl->has_thread_checkpoint()) {111JfrCheckpointManager::create_thread_checkpoint(thread);112}113assert(tl->has_thread_checkpoint(), "invariant");114return tl->thread_id();115}116117// Populates the thread local stack frames, but does not add them118// to the stacktrace repository (...yet, see stacktrace_id() below)119//120void ObjectSampler::fill_stacktrace(JfrStackTrace* stacktrace, JavaThread* thread) {121assert(stacktrace != NULL, "invariant");122assert(thread != NULL, "invariant");123if (JfrEventSetting::has_stacktrace(EventOldObjectSample::eventId)) {124JfrStackTraceRepository::fill_stacktrace_for(thread, stacktrace, 0);125}126}127128// We were successful in acquiring the try lock and have been selected for adding a sample.129// Go ahead with installing our previously taken stacktrace into the stacktrace repository.130//131traceid ObjectSampler::stacktrace_id(const JfrStackTrace* stacktrace, JavaThread* thread) {132assert(stacktrace != NULL, "invariant");133assert(stacktrace->hash() != 0, "invariant");134const traceid stacktrace_id = JfrStackTraceRepository::add(stacktrace, thread);135thread->jfr_thread_local()->set_cached_stack_trace_id(stacktrace_id, stacktrace->hash());136return stacktrace_id;137}138139void ObjectSampler::sample(HeapWord* obj, size_t allocated, JavaThread* thread) {140assert(thread != NULL, "invariant");141assert(is_created(), "invariant");142143const traceid thread_id = get_thread_id(thread);144if (thread_id == 0) {145return;146}147const JfrThreadLocal* const tl = thread->jfr_thread_local();148JfrStackTrace stacktrace(tl->stackframes(), tl->stackdepth());149fill_stacktrace(&stacktrace, thread);150151// try enter critical section152JfrTryLock tryLock(&_lock);153if (!tryLock.has_lock()) {154if (LogJFR && Verbose) tty->print_cr("Skipping old object sample due to lock contention");155return;156}157158instance().add(obj, allocated, thread_id, &stacktrace, thread);159}160161void ObjectSampler::add(HeapWord* obj, size_t allocated, traceid thread_id, JfrStackTrace* stacktrace, JavaThread* thread) {162assert(stacktrace != NULL, "invariant");163assert(thread_id != 0, "invariant");164assert(thread != NULL, "invariant");165assert(thread->jfr_thread_local()->has_thread_checkpoint(), "invariant");166167if (_dead_samples) {168scavenge();169assert(!_dead_samples, "invariant");170}171172_total_allocated += allocated;173const size_t span = _total_allocated - _priority_queue->total();174ObjectSample* sample;175if ((size_t)_priority_queue->count() == _size) {176assert(_list->count() == _size, "invariant");177const ObjectSample* peek = _priority_queue->peek();178if (peek->span() > span) {179// quick reject, will not fit180return;181}182sample = _list->reuse(_priority_queue->pop());183} else {184sample = _list->get();185}186187assert(sample != NULL, "invariant");188sample->set_thread_id(thread_id);189sample->set_thread_checkpoint(thread->jfr_thread_local()->thread_checkpoint());190191const unsigned int stacktrace_hash = stacktrace->hash();192if (stacktrace_hash != 0) {193sample->set_stack_trace_id(stacktrace_id(stacktrace, thread));194sample->set_stack_trace_hash(stacktrace_hash);195}196197sample->set_span(allocated);198sample->set_object((oop)obj);199sample->set_allocated(allocated);200sample->set_allocation_time(JfrTicks::now());201sample->set_heap_used_at_last_gc(Universe::get_heap_used_at_last_gc());202_priority_queue->push(sample);203}204205void ObjectSampler::scavenge() {206ObjectSample* current = _list->last();207while (current != NULL) {208ObjectSample* next = current->next();209if (current->is_dead()) {210remove_dead(current);211}212current = next;213}214_dead_samples = false;215}216217void ObjectSampler::remove_dead(ObjectSample* sample) {218assert(sample != NULL, "invariant");219assert(sample->is_dead(), "invariant");220ObjectSample* const previous = sample->prev();221// push span on to previous222if (previous != NULL) {223_priority_queue->remove(previous);224previous->add_span(sample->span());225_priority_queue->push(previous);226}227_priority_queue->remove(sample);228_list->release(sample);229}230231void ObjectSampler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) {232assert(is_created(), "invariant");233assert(SafepointSynchronize::is_at_safepoint(), "invariant");234ObjectSampler& sampler = instance();235ObjectSample* current = sampler._list->last();236while (current != NULL) {237ObjectSample* next = current->next();238if (!current->is_dead()) {239if (is_alive->do_object_b(current->object())) {240// The weakly referenced object is alive, update pointer241f->do_oop(const_cast<oop*>(current->object_addr()));242} else {243current->set_dead();244sampler._dead_samples = true;245}246}247current = next;248}249sampler._last_sweep = JfrTicks::now();250}251252const ObjectSample* ObjectSampler::last() const {253return _list->last();254}255256const ObjectSample* ObjectSampler::first() const {257return _list->first();258}259260const ObjectSample* ObjectSampler::last_resolved() const {261return _list->last_resolved();262}263264void ObjectSampler::set_last_resolved(const ObjectSample* sample) {265_list->set_last_resolved(sample);266}267268int ObjectSampler::item_count() const {269return _priority_queue->count();270}271272const ObjectSample* ObjectSampler::item_at(int index) const {273return _priority_queue->item_at(index);274}275276ObjectSample* ObjectSampler::item_at(int index) {277return const_cast<ObjectSample*>(278const_cast<const ObjectSampler*>(this)->item_at(index)279);280}281282const JfrTicks& ObjectSampler::last_sweep() const {283return _last_sweep;284}285286287