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 "memory/iterator.hpp"
  33 #include "runtime/thread.inline.hpp"
  34 #include "runtime/vmThread.hpp"
  35 
  36 bool LeakProfiler::is_running() {
  37   return ObjectSampler::is_created();
  38 }
  39 
  40 bool LeakProfiler::start(int sample_count) {
  41   if (is_running()) {
  42     return true;
  43   }
  44 
  45   // Allows user to disable leak profiler on command line by setting queue size to zero.
  46   if (sample_count == 0) {
  47     return false;
  48   }
  49 
  50   assert(!is_running(), "invariant");
  51   assert(sample_count > 0, "invariant");
  52 
  53   // schedule the safepoint operation for installing the object sampler
  54   StartOperation op(sample_count);
  55   VMThread::execute(&op);
  56 
  57   if (!is_running()) {
  58     if (LogJFR && Verbose) tty->print_cr("Object sampling could not be started because the sampler could not be allocated");
  59     return false;
  60   }
  61   assert(is_running(), "invariant");
  62   if (LogJFR && Verbose) tty->print_cr("Object sampling started");
  63   return true;
  64 }
  65 
  66 bool LeakProfiler::stop() {
  67   if (!is_running()) {
  68     return false;
  69   }
  70 
  71   // schedule the safepoint operation for uninstalling and destroying the object sampler
  72   StopOperation op;
  73   VMThread::execute(&op);
  74 
  75   assert(!is_running(), "invariant");
  76   if (LogJFR && Verbose) tty->print_cr("Object sampling stopped");
  77   return true;
  78 }
  79 
  80 void LeakProfiler::emit_events(int64_t cutoff_ticks, bool emit_all) {
  81   if (!is_running()) {
  82     return;
  83   }
  84   // exclusive access to object sampler instance
  85   ObjectSampler* const sampler = ObjectSampler::acquire();
  86   assert(sampler != NULL, "invariant");
  87   EventEmitter::emit(sampler, cutoff_ticks, emit_all);
  88   ObjectSampler::release();
  89 }
  90 
  91 void LeakProfiler::oops_do(BoolObjectClosure* is_alive, OopClosure* f) {
  92   assert(SafepointSynchronize::is_at_safepoint(),
  93     "Leak Profiler::oops_do(...) may only be called during safepoint");
  94   if (is_running()) {
  95     ObjectSampler::oops_do(is_alive, f);
  96   }
  97 }
  98 
  99 void LeakProfiler::sample(HeapWord* object, size_t size, JavaThread* thread) {
 100   assert(is_running(), "invariant");
 101   assert(thread != NULL, "invariant");
 102   assert(thread->thread_state() == _thread_in_vm, "invariant");
 103 
 104   // exclude compiler threads and code sweeper thread
 105   if (thread->is_hidden_from_external_view()) {
 106     return;
 107   }
 108 
 109   ObjectSampler::sample(object, size, thread);
 110 }