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