src/share/vm/jfr/leakprofiler/sampling/objectSampler.cpp

Mon, 12 Aug 2019 18:30:40 +0300

author
apetushkov
date
Mon, 12 Aug 2019 18:30:40 +0300
changeset 9858
b985cbb00e68
child 9867
150ab470bf7f
permissions
-rw-r--r--

8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens

apetushkov@9858 1 /*
apetushkov@9858 2 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
apetushkov@9858 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
apetushkov@9858 4 *
apetushkov@9858 5 * This code is free software; you can redistribute it and/or modify it
apetushkov@9858 6 * under the terms of the GNU General Public License version 2 only, as
apetushkov@9858 7 * published by the Free Software Foundation.
apetushkov@9858 8 *
apetushkov@9858 9 * This code is distributed in the hope that it will be useful, but WITHOUT
apetushkov@9858 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
apetushkov@9858 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
apetushkov@9858 12 * version 2 for more details (a copy is included in the LICENSE file that
apetushkov@9858 13 * accompanied this code).
apetushkov@9858 14 *
apetushkov@9858 15 * You should have received a copy of the GNU General Public License version
apetushkov@9858 16 * 2 along with this work; if not, write to the Free Software Foundation,
apetushkov@9858 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
apetushkov@9858 18 *
apetushkov@9858 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
apetushkov@9858 20 * or visit www.oracle.com if you need additional information or have any
apetushkov@9858 21 * questions.
apetushkov@9858 22 *
apetushkov@9858 23 */
apetushkov@9858 24 #include "precompiled.hpp"
apetushkov@9858 25 #include "jfr/jfrEvents.hpp"
apetushkov@9858 26 #include "jfr/leakprofiler/sampling/objectSample.hpp"
apetushkov@9858 27 #include "jfr/leakprofiler/sampling/objectSampler.hpp"
apetushkov@9858 28 #include "jfr/leakprofiler/sampling/sampleList.hpp"
apetushkov@9858 29 #include "jfr/leakprofiler/sampling/samplePriorityQueue.hpp"
apetushkov@9858 30 #include "jfr/recorder/jfrEventSetting.inline.hpp"
apetushkov@9858 31 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
apetushkov@9858 32 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
apetushkov@9858 33 #include "jfr/support/jfrThreadLocal.hpp"
apetushkov@9858 34 #include "jfr/utilities/jfrTryLock.hpp"
apetushkov@9858 35 #include "memory/universe.hpp"
apetushkov@9858 36 #include "oops/oop.inline.hpp"
apetushkov@9858 37 #include "runtime/thread.hpp"
apetushkov@9858 38
apetushkov@9858 39 ObjectSampler::ObjectSampler(size_t size) :
apetushkov@9858 40 _priority_queue(new SamplePriorityQueue(size)),
apetushkov@9858 41 _list(new SampleList(size)),
apetushkov@9858 42 _last_sweep(JfrTicks::now()),
apetushkov@9858 43 _total_allocated(0),
apetushkov@9858 44 _threshold(0),
apetushkov@9858 45 _size(size),
apetushkov@9858 46 _tryLock(0),
apetushkov@9858 47 _dead_samples(false) {}
apetushkov@9858 48
apetushkov@9858 49 ObjectSampler::~ObjectSampler() {
apetushkov@9858 50 delete _priority_queue;
apetushkov@9858 51 _priority_queue = NULL;
apetushkov@9858 52 delete _list;
apetushkov@9858 53 _list = NULL;
apetushkov@9858 54 }
apetushkov@9858 55
apetushkov@9858 56 void ObjectSampler::add(HeapWord* obj, size_t allocated, JavaThread* thread) {
apetushkov@9858 57 assert(thread != NULL, "invariant");
apetushkov@9858 58 const traceid thread_id = thread->threadObj() != NULL ? thread->jfr_thread_local()->thread_id() : 0;
apetushkov@9858 59 if (thread_id == 0) {
apetushkov@9858 60 return;
apetushkov@9858 61 }
apetushkov@9858 62 assert(thread_id != 0, "invariant");
apetushkov@9858 63
apetushkov@9858 64 if (!thread->jfr_thread_local()->has_thread_checkpoint()) {
apetushkov@9858 65 JfrCheckpointManager::create_thread_checkpoint(thread);
apetushkov@9858 66 assert(thread->jfr_thread_local()->has_thread_checkpoint(), "invariant");
apetushkov@9858 67 }
apetushkov@9858 68
apetushkov@9858 69 traceid stack_trace_id = 0;
apetushkov@9858 70 unsigned int stack_trace_hash = 0;
apetushkov@9858 71 if (JfrEventSetting::has_stacktrace(EventOldObjectSample::eventId)) {
apetushkov@9858 72 stack_trace_id = JfrStackTraceRepository::record(thread, 0, &stack_trace_hash);
apetushkov@9858 73 thread->jfr_thread_local()->set_cached_stack_trace_id(stack_trace_id, stack_trace_hash);
apetushkov@9858 74 }
apetushkov@9858 75
apetushkov@9858 76 JfrTryLock tryLock(&_tryLock);
apetushkov@9858 77 if (!tryLock.has_lock()) {
apetushkov@9858 78 if (LogJFR && Verbose) tty->print_cr("Skipping old object sample due to lock contention");
apetushkov@9858 79 return;
apetushkov@9858 80 }
apetushkov@9858 81
apetushkov@9858 82 if (_dead_samples) {
apetushkov@9858 83 scavenge();
apetushkov@9858 84 assert(!_dead_samples, "invariant");
apetushkov@9858 85 }
apetushkov@9858 86
apetushkov@9858 87 _total_allocated += allocated;
apetushkov@9858 88 const size_t span = _total_allocated - _priority_queue->total();
apetushkov@9858 89 ObjectSample* sample;
apetushkov@9858 90 if ((size_t)_priority_queue->count() == _size) {
apetushkov@9858 91 assert(_list->count() == _size, "invariant");
apetushkov@9858 92 const ObjectSample* peek = _priority_queue->peek();
apetushkov@9858 93 if (peek->span() > span) {
apetushkov@9858 94 // quick reject, will not fit
apetushkov@9858 95 return;
apetushkov@9858 96 }
apetushkov@9858 97 sample = _list->reuse(_priority_queue->pop());
apetushkov@9858 98 } else {
apetushkov@9858 99 sample = _list->get();
apetushkov@9858 100 }
apetushkov@9858 101
apetushkov@9858 102 assert(sample != NULL, "invariant");
apetushkov@9858 103 assert(thread_id != 0, "invariant");
apetushkov@9858 104 sample->set_thread_id(thread_id);
apetushkov@9858 105 sample->set_thread_checkpoint(thread->jfr_thread_local()->thread_checkpoint());
apetushkov@9858 106
apetushkov@9858 107 if (stack_trace_id != 0) {
apetushkov@9858 108 sample->set_stack_trace_id(stack_trace_id);
apetushkov@9858 109 sample->set_stack_trace_hash(stack_trace_hash);
apetushkov@9858 110 }
apetushkov@9858 111
apetushkov@9858 112 sample->set_span(allocated);
apetushkov@9858 113 sample->set_object((oop)obj);
apetushkov@9858 114 sample->set_allocated(allocated);
apetushkov@9858 115 sample->set_allocation_time(JfrTicks::now());
apetushkov@9858 116 sample->set_heap_used_at_last_gc(Universe::get_heap_used_at_last_gc());
apetushkov@9858 117 _priority_queue->push(sample);
apetushkov@9858 118 }
apetushkov@9858 119
apetushkov@9858 120 const ObjectSample* ObjectSampler::last() const {
apetushkov@9858 121 return _list->last();
apetushkov@9858 122 }
apetushkov@9858 123
apetushkov@9858 124 const ObjectSample* ObjectSampler::last_resolved() const {
apetushkov@9858 125 return _list->last_resolved();
apetushkov@9858 126 }
apetushkov@9858 127
apetushkov@9858 128 void ObjectSampler::set_last_resolved(const ObjectSample* sample) {
apetushkov@9858 129 _list->set_last_resolved(sample);
apetushkov@9858 130 }
apetushkov@9858 131
apetushkov@9858 132 void ObjectSampler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
apetushkov@9858 133 ObjectSample* current = _list->last();
apetushkov@9858 134 while (current != NULL) {
apetushkov@9858 135 ObjectSample* next = current->next();
apetushkov@9858 136 if (!current->is_dead()) {
apetushkov@9858 137 if (is_alive->do_object_b(current->object())) {
apetushkov@9858 138 // The weakly referenced object is alive, update pointer
apetushkov@9858 139 f->do_oop(const_cast<oop*>(current->object_addr()));
apetushkov@9858 140 } else {
apetushkov@9858 141 current->set_dead();
apetushkov@9858 142 _dead_samples = true;
apetushkov@9858 143 }
apetushkov@9858 144 }
apetushkov@9858 145 current = next;
apetushkov@9858 146 }
apetushkov@9858 147 _last_sweep = JfrTicks::now();
apetushkov@9858 148 }
apetushkov@9858 149
apetushkov@9858 150 void ObjectSampler::remove_dead(ObjectSample* sample) {
apetushkov@9858 151 assert(sample != NULL, "invariant");
apetushkov@9858 152 assert(sample->is_dead(), "invariant");
apetushkov@9858 153 ObjectSample* const previous = sample->prev();
apetushkov@9858 154 // push span on to previous
apetushkov@9858 155 if (previous != NULL) {
apetushkov@9858 156 _priority_queue->remove(previous);
apetushkov@9858 157 previous->add_span(sample->span());
apetushkov@9858 158 _priority_queue->push(previous);
apetushkov@9858 159 }
apetushkov@9858 160 _priority_queue->remove(sample);
apetushkov@9858 161 _list->release(sample);
apetushkov@9858 162 }
apetushkov@9858 163
apetushkov@9858 164 void ObjectSampler::scavenge() {
apetushkov@9858 165 ObjectSample* current = _list->last();
apetushkov@9858 166 while (current != NULL) {
apetushkov@9858 167 ObjectSample* next = current->next();
apetushkov@9858 168 if (current->is_dead()) {
apetushkov@9858 169 remove_dead(current);
apetushkov@9858 170 }
apetushkov@9858 171 current = next;
apetushkov@9858 172 }
apetushkov@9858 173 _dead_samples = false;
apetushkov@9858 174 }
apetushkov@9858 175
apetushkov@9858 176 int ObjectSampler::item_count() const {
apetushkov@9858 177 return _priority_queue->count();
apetushkov@9858 178 }
apetushkov@9858 179
apetushkov@9858 180 const ObjectSample* ObjectSampler::item_at(int index) const {
apetushkov@9858 181 return _priority_queue->item_at(index);
apetushkov@9858 182 }
apetushkov@9858 183
apetushkov@9858 184 ObjectSample* ObjectSampler::item_at(int index) {
apetushkov@9858 185 return const_cast<ObjectSample*>(
apetushkov@9858 186 const_cast<const ObjectSampler*>(this)->item_at(index)
apetushkov@9858 187 );
apetushkov@9858 188 }
apetushkov@9858 189
apetushkov@9858 190 const JfrTicks& ObjectSampler::last_sweep() const {
apetushkov@9858 191 return _last_sweep;
apetushkov@9858 192 }

mercurial