1.1 --- a/src/share/vm/jfr/leakprofiler/emitEventOperation.cpp Fri Sep 27 13:23:32 2019 +0800 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,235 +0,0 @@ 1.4 -/* 1.5 - * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved. 1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 - * 1.8 - * This code is free software; you can redistribute it and/or modify it 1.9 - * under the terms of the GNU General Public License version 2 only, as 1.10 - * published by the Free Software Foundation. 1.11 - * 1.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 - * version 2 for more details (a copy is included in the LICENSE file that 1.16 - * accompanied this code). 1.17 - * 1.18 - * You should have received a copy of the GNU General Public License version 1.19 - * 2 along with this work; if not, write to the Free Software Foundation, 1.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 - * 1.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 - * or visit www.oracle.com if you need additional information or have any 1.24 - * questions. 1.25 - * 1.26 - */ 1.27 -#include "precompiled.hpp" 1.28 -#include "gc_interface/collectedHeap.hpp" 1.29 -#include "jfr/jfrEvents.hpp" 1.30 -#include "jfr/leakprofiler/utilities/granularTimer.hpp" 1.31 -#include "jfr/leakprofiler/chains/rootSetClosure.hpp" 1.32 -#include "jfr/leakprofiler/chains/edge.hpp" 1.33 -#include "jfr/leakprofiler/chains/edgeQueue.hpp" 1.34 -#include "jfr/leakprofiler/chains/edgeStore.hpp" 1.35 -#include "jfr/leakprofiler/chains/bitset.hpp" 1.36 -#include "jfr/leakprofiler/sampling/objectSample.hpp" 1.37 -#include "jfr/leakprofiler/leakProfiler.hpp" 1.38 -#include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp" 1.39 -#include "jfr/leakprofiler/sampling/objectSampler.hpp" 1.40 -#include "jfr/leakprofiler/emitEventOperation.hpp" 1.41 -#include "jfr/leakprofiler/chains/bfsClosure.hpp" 1.42 -#include "jfr/leakprofiler/chains/dfsClosure.hpp" 1.43 -#include "jfr/leakprofiler/chains/objectSampleMarker.hpp" 1.44 -#include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp" 1.45 -#include "jfr/support/jfrThreadId.hpp" 1.46 -#include "memory/resourceArea.hpp" 1.47 -#include "memory/universe.hpp" 1.48 -#include "oops/markOop.hpp" 1.49 -#include "oops/oop.inline.hpp" 1.50 -#include "runtime/safepoint.hpp" 1.51 -#include "runtime/vmThread.hpp" 1.52 -#include "utilities/globalDefinitions.hpp" 1.53 - 1.54 -/* The EdgeQueue is backed by directly managed virtual memory. 1.55 - * We will attempt to dimension an initial reservation 1.56 - * in proportion to the size of the heap (represented by heap_region). 1.57 - * Initial memory reservation: 5% of the heap OR at least 32 Mb 1.58 - * Commit ratio: 1 : 10 (subject to allocation granularties) 1.59 - */ 1.60 -static size_t edge_queue_memory_reservation(const MemRegion& heap_region) { 1.61 - const size_t memory_reservation_bytes = MAX2(heap_region.byte_size() / 20, 32*M); 1.62 - assert(memory_reservation_bytes >= (size_t)32*M, "invariant"); 1.63 - return memory_reservation_bytes; 1.64 -} 1.65 - 1.66 -static size_t edge_queue_memory_commit_size(size_t memory_reservation_bytes) { 1.67 - const size_t memory_commit_block_size_bytes = memory_reservation_bytes / 10; 1.68 - assert(memory_commit_block_size_bytes >= (size_t)3*M, "invariant"); 1.69 - return memory_commit_block_size_bytes; 1.70 -} 1.71 - 1.72 -static void log_edge_queue_summary(const EdgeQueue& edge_queue) { 1.73 - if (LogJFR && Verbose) tty->print_cr("EdgeQueue reserved size total: " SIZE_FORMAT " [KB]", edge_queue.reserved_size() / K); 1.74 - if (LogJFR && Verbose) tty->print_cr("EdgeQueue edges total: " SIZE_FORMAT, edge_queue.top()); 1.75 - if (LogJFR && Verbose) tty->print_cr("EdgeQueue liveset total: " SIZE_FORMAT " [KB]", edge_queue.live_set() / K); 1.76 - if (edge_queue.reserved_size() > 0) { 1.77 - if (LogJFR && Verbose) tty->print_cr("EdgeQueue commit reserve ratio: %f\n", 1.78 - ((double)edge_queue.live_set() / (double)edge_queue.reserved_size())); 1.79 - } 1.80 -} 1.81 - 1.82 -void EmitEventOperation::doit() { 1.83 - assert(LeakProfiler::is_running(), "invariant"); 1.84 - _object_sampler = LeakProfiler::object_sampler(); 1.85 - assert(_object_sampler != NULL, "invariant"); 1.86 - 1.87 - _vm_thread = VMThread::vm_thread(); 1.88 - assert(_vm_thread == Thread::current(), "invariant"); 1.89 - _vm_thread_local = _vm_thread->jfr_thread_local(); 1.90 - assert(_vm_thread_local != NULL, "invariant"); 1.91 - assert(_vm_thread->jfr_thread_local()->thread_id() == JFR_THREAD_ID(_vm_thread), "invariant"); 1.92 - 1.93 - // The VM_Operation::evaluate() which invoked doit() 1.94 - // contains a top level ResourceMark 1.95 - 1.96 - // save the original markWord for the potential leak objects 1.97 - // to be restored on function exit 1.98 - ObjectSampleMarker marker; 1.99 - if (ObjectSampleCheckpoint::mark(marker, _emit_all) == 0) { 1.100 - return; 1.101 - } 1.102 - 1.103 - EdgeStore edge_store; 1.104 - 1.105 - GranularTimer::start(_cutoff_ticks, 1000000); 1.106 - if (_cutoff_ticks <= 0) { 1.107 - // no chains 1.108 - write_events(&edge_store); 1.109 - return; 1.110 - } 1.111 - 1.112 - assert(_cutoff_ticks > 0, "invariant"); 1.113 - 1.114 - // The bitset used for marking is dimensioned as a function of the heap size 1.115 - const MemRegion heap_region = Universe::heap()->reserved_region(); 1.116 - BitSet mark_bits(heap_region); 1.117 - 1.118 - // The edge queue is dimensioned as a fraction of the heap size 1.119 - const size_t edge_queue_reservation_size = edge_queue_memory_reservation(heap_region); 1.120 - EdgeQueue edge_queue(edge_queue_reservation_size, edge_queue_memory_commit_size(edge_queue_reservation_size)); 1.121 - 1.122 - // The initialize() routines will attempt to reserve and allocate backing storage memory. 1.123 - // Failure to accommodate will render root chain processing impossible. 1.124 - // As a fallback on failure, just write out the existing samples, flat, without chains. 1.125 - if (!(mark_bits.initialize() && edge_queue.initialize())) { 1.126 - if (LogJFR) tty->print_cr("Unable to allocate memory for root chain processing"); 1.127 - write_events(&edge_store); 1.128 - return; 1.129 - } 1.130 - 1.131 - // necessary condition for attempting a root set iteration 1.132 - Universe::heap()->ensure_parsability(false); 1.133 - 1.134 - RootSetClosure::add_to_queue(&edge_queue); 1.135 - if (edge_queue.is_full()) { 1.136 - // Pathological case where roots don't fit in queue 1.137 - // Do a depth-first search, but mark roots first 1.138 - // to avoid walking sideways over roots 1.139 - DFSClosure::find_leaks_from_root_set(&edge_store, &mark_bits); 1.140 - } else { 1.141 - BFSClosure bfs(&edge_queue, &edge_store, &mark_bits); 1.142 - bfs.process(); 1.143 - } 1.144 - GranularTimer::stop(); 1.145 - write_events(&edge_store); 1.146 - log_edge_queue_summary(edge_queue); 1.147 -} 1.148 - 1.149 -int EmitEventOperation::write_events(EdgeStore* edge_store) { 1.150 - assert(_object_sampler != NULL, "invariant"); 1.151 - assert(edge_store != NULL, "invariant"); 1.152 - assert(_vm_thread != NULL, "invariant"); 1.153 - assert(_vm_thread_local != NULL, "invariant"); 1.154 - assert(SafepointSynchronize::is_at_safepoint(), "invariant"); 1.155 - 1.156 - // save thread id in preparation for thread local trace data manipulations 1.157 - const traceid vmthread_id = _vm_thread_local->thread_id(); 1.158 - assert(_vm_thread_local->thread_id() == JFR_THREAD_ID(_vm_thread), "invariant"); 1.159 - 1.160 - const jlong last_sweep = _emit_all ? max_jlong : _object_sampler->last_sweep().value(); 1.161 - int count = 0; 1.162 - 1.163 - const ObjectSample* current = _object_sampler->first(); 1.164 - while (current != NULL) { 1.165 - ObjectSample* prev = current->prev(); 1.166 - if (current->is_alive_and_older_than(last_sweep)) { 1.167 - write_event(current, edge_store); 1.168 - ++count; 1.169 - } 1.170 - current = prev; 1.171 - } 1.172 - 1.173 - // restore thread local stack trace and thread id 1.174 - _vm_thread_local->set_thread_id(vmthread_id); 1.175 - _vm_thread_local->clear_cached_stack_trace(); 1.176 - assert(_vm_thread_local->thread_id() == JFR_THREAD_ID(_vm_thread), "invariant"); 1.177 - 1.178 - if (count > 0) { 1.179 - // serialize assoicated checkpoints 1.180 - ObjectSampleCheckpoint::write(edge_store, _emit_all, _vm_thread); 1.181 - } 1.182 - return count; 1.183 -} 1.184 - 1.185 -static int array_size(const oop object) { 1.186 - assert(object != NULL, "invariant"); 1.187 - if (object->is_array()) { 1.188 - return arrayOop(object)->length(); 1.189 - } 1.190 - return min_jint; 1.191 -} 1.192 - 1.193 -void EmitEventOperation::write_event(const ObjectSample* sample, EdgeStore* edge_store) { 1.194 - assert(sample != NULL, "invariant"); 1.195 - assert(!sample->is_dead(), "invariant"); 1.196 - assert(edge_store != NULL, "invariant"); 1.197 - assert(_vm_thread_local != NULL, "invariant"); 1.198 - const oop* object_addr = sample->object_addr(); 1.199 - assert(*object_addr != NULL, "invariant"); 1.200 - 1.201 - const Edge* edge = (const Edge*)(*object_addr)->mark(); 1.202 - traceid gc_root_id = 0; 1.203 - if (edge == NULL) { 1.204 - // In order to dump out a representation of the event 1.205 - // even though it was not reachable / too long to reach, 1.206 - // we need to register a top level edge for this object 1.207 - Edge e(NULL, object_addr); 1.208 - edge_store->add_chain(&e, 1); 1.209 - edge = (const Edge*)(*object_addr)->mark(); 1.210 - } else { 1.211 - gc_root_id = edge_store->get_root_id(edge); 1.212 - } 1.213 - 1.214 - assert(edge != NULL, "invariant"); 1.215 - assert(edge->pointee() == *object_addr, "invariant"); 1.216 - const traceid object_id = edge_store->get_id(edge); 1.217 - assert(object_id != 0, "invariant"); 1.218 - 1.219 - EventOldObjectSample e(UNTIMED); 1.220 - e.set_starttime(GranularTimer::start_time()); 1.221 - e.set_endtime(GranularTimer::end_time()); 1.222 - e.set_allocationTime(sample->allocation_time()); 1.223 - e.set_lastKnownHeapUsage(sample->heap_used_at_last_gc()); 1.224 - e.set_object(object_id); 1.225 - e.set_arrayElements(array_size(*object_addr)); 1.226 - e.set_root(gc_root_id); 1.227 - 1.228 - // Temporarily assigning both the stack trace id and thread id 1.229 - // onto the thread local data structure of the VMThread (for the duration 1.230 - // of the commit() call). This trick provides a means to override 1.231 - // the event generation mechanism by injecting externally provided id's. 1.232 - // Here, in particular, this allows us to emit an old object event 1.233 - // supplying information from where the actual sampling occurred. 1.234 - _vm_thread_local->set_cached_stack_trace_id(sample->stack_trace_id()); 1.235 - assert(sample->has_thread(), "invariant"); 1.236 - _vm_thread_local->set_thread_id(sample->thread_id()); 1.237 - e.commit(); 1.238 -}