1 /*
   2  * Copyright (c) 2014, 2018, 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 "classfile/classLoaderData.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "jfr/leakprofiler/chains/bfsClosure.hpp"
  29 #include "jfr/leakprofiler/chains/dfsClosure.hpp"
  30 #include "jfr/leakprofiler/chains/edgeQueue.hpp"
  31 #include "jfr/leakprofiler/chains/rootSetClosure.hpp"
  32 #include "jfr/leakprofiler/utilities/saveRestore.hpp"
  33 #include "jfr/leakprofiler/utilities/unifiedOop.hpp"
  34 #include "memory/universe.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "prims/jvmtiExport.hpp"
  37 #include "runtime/jniHandles.hpp"
  38 #include "runtime/synchronizer.hpp"
  39 #include "runtime/thread.hpp"
  40 #include "services/management.hpp"
  41 #include "utilities/align.hpp"
  42 
  43 template <typename Delegate>
  44 RootSetClosure<Delegate>::RootSetClosure(Delegate* delegate) : _delegate(delegate) {}
  45 
  46 template <typename Delegate>
  47 void RootSetClosure<Delegate>::do_oop(oop* ref) {
  48   assert(ref != NULL, "invariant");
  49   // We discard unaligned root references because
  50   // our reference tagging scheme will use
  51   // the lowest bit in a represented reference
  52   // to indicate the reference is narrow.
  53   // It is mainly roots delivered via nmethods::do_oops()
  54   // that come in unaligned. It should be ok to duck these
  55   // since they are supposedly weak.
  56   if (!is_aligned(ref, HeapWordSize)) {
  57     return;
  58   }
  59 
  60   assert(is_aligned(ref, HeapWordSize), "invariant");
  61   if (*ref != NULL) {
  62     _delegate->do_root(ref);
  63   }
  64 }
  65 
  66 template <typename Delegate>
  67 void RootSetClosure<Delegate>::do_oop(narrowOop* ref) {
  68   assert(ref != NULL, "invariant");
  69   assert(is_aligned(ref, sizeof(narrowOop)), "invariant");
  70   const oop pointee = oopDesc::load_decode_heap_oop(ref);
  71   if (pointee != NULL) {
  72     _delegate->do_root(UnifiedOop::encode(ref));
  73   }
  74 }
  75 
  76 class RootSetClosureMarkScope : public MarkingCodeBlobClosure::MarkScope {};
  77 
  78 template <typename Delegate>
  79 void RootSetClosure<Delegate>::process() {
  80   RootSetClosureMarkScope mark_scope;
  81   CLDToOopClosure cldt_closure(this);
  82   ClassLoaderDataGraph::always_strong_cld_do(&cldt_closure);
  83   CodeBlobToOopClosure blobs(this, false);
  84   Threads::oops_do(this, NULL, &blobs); // XXX set CLDClosure to NULL
  85   ObjectSynchronizer::oops_do(this);
  86   Universe::oops_do(this);
  87   JNIHandles::oops_do(this);
  88   JvmtiExport::oops_do(this);
  89   SystemDictionary::oops_do(this);
  90   Management::oops_do(this);
  91   StringTable::oops_do(this);
  92 }
  93 
  94 template class RootSetClosure<BFSClosure>;
  95 template class RootSetClosure<DFSClosure>;