1 /* 2 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "jfr/leakprofiler/leakProfiler.hpp" 27 #include "jfr/leakprofiler/startOperation.hpp" 28 #include "jfr/leakprofiler/stopOperation.hpp" 29 #include "jfr/leakprofiler/checkpoint/eventEmitter.hpp" 30 #include "jfr/leakprofiler/sampling/objectSampler.hpp" 31 #include "jfr/recorder/service/jfrOptionSet.hpp" 32 #include "logging/log.hpp" 33 #include "memory/iterator.hpp" 34 #include "runtime/mutexLocker.hpp" 35 #include "runtime/thread.inline.hpp" 36 #include "runtime/vmThread.hpp" 37 38 bool LeakProfiler::is_running() { 39 return ObjectSampler::is_created(); 40 } 41 42 bool LeakProfiler::start(int sample_count) { 43 if (is_running()) { 44 return true; 45 } 46 47 // Allows user to disable leak profiler on command line by setting queue size to zero. 48 if (sample_count == 0) { 49 return false; 50 } 51 52 assert(!is_running(), "invariant"); 53 assert(sample_count > 0, "invariant"); 54 55 // schedule the safepoint operation for installing the object sampler 56 StartOperation op(sample_count); 57 VMThread::execute(&op); 58 59 if (!is_running()) { 60 log_trace(jfr, system)("Object sampling could not be started because the sampler could not be allocated"); 61 return false; 62 } 63 assert(is_running(), "invariant"); 64 log_trace(jfr, system)("Object sampling started"); 65 return true; 66 } 67 68 bool LeakProfiler::stop() { 69 if (!is_running()) { 70 return false; 71 } 72 73 // schedule the safepoint operation for uninstalling and destroying the object sampler 74 StopOperation op; 75 VMThread::execute(&op); 76 77 assert(!is_running(), "invariant"); 78 log_trace(jfr, system)("Object sampling stopped"); 79 return true; 80 } 81 82 void LeakProfiler::emit_events(int64_t cutoff_ticks, bool emit_all) { 83 if (!is_running()) { 84 return; 85 } 86 MutexLocker lock(JfrStream_lock); 87 // exclusive access to object sampler instance 88 ObjectSampler* const sampler = ObjectSampler::acquire(); 89 assert(sampler != NULL, "invariant"); 90 EventEmitter::emit(sampler, cutoff_ticks, emit_all); 91 ObjectSampler::release(); 92 } 93 94 void LeakProfiler::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { 95 assert(SafepointSynchronize::is_at_safepoint(), 96 "Leak Profiler::oops_do(...) may only be called during safepoint"); 97 if (is_running()) { 98 ObjectSampler::weak_oops_do(is_alive, f); 99 } 100 } 101 102 void LeakProfiler::sample(HeapWord* object, size_t size, JavaThread* thread) { 103 assert(is_running(), "invariant"); 104 assert(thread != NULL, "invariant"); 105 assert(thread->thread_state() == _thread_in_vm, "invariant"); 106 107 // exclude compiler threads and code sweeper thread 108 if (thread->is_hidden_from_external_view()) { 109 return; 110 } 111 112 ObjectSampler::sample(object, size, thread); 113 }