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