1 /*
 2  * Copyright (c) 2014, 2020, 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/classLoaderDataGraph.hpp"
28 #include "classfile/stringTable.hpp"
29 #include "gc/shared/oopStorage.inline.hpp"
30 #include "gc/shared/oopStorageSet.hpp"
31 #include "gc/shared/strongRootsScope.hpp"
32 #include "jfr/leakprofiler/chains/bfsClosure.hpp"
33 #include "jfr/leakprofiler/chains/dfsClosure.hpp"
34 #include "jfr/leakprofiler/chains/edgeQueue.hpp"
35 #include "jfr/leakprofiler/chains/rootSetClosure.hpp"
36 #include "jfr/leakprofiler/utilities/unifiedOopRef.inline.hpp"
37 #include "memory/universe.hpp"
38 #include "oops/access.inline.hpp"
39 #include "oops/oop.inline.hpp"
40 #include "prims/jvmtiExport.hpp"
41 #include "runtime/jniHandles.inline.hpp"
42 #include "runtime/synchronizer.hpp"
43 #include "runtime/thread.hpp"
44 #include "services/management.hpp"
45 #include "utilities/align.hpp"
46 
47 template <typename Delegate>
48 RootSetClosure<Delegate>::RootSetClosure(Delegate* delegate) : _delegate(delegate) {}
49 
50 template <typename Delegate>
51 void RootSetClosure<Delegate>::do_oop(oop* ref) {
52   assert(ref != NULL, "invariant");
53   assert(is_aligned(ref, HeapWordSize), "invariant");
54   if (*ref != NULL) {
55     _delegate->do_root(UnifiedOopRef::encode_in_native(ref));
56   }
57 }
58 
59 template <typename Delegate>
60 void RootSetClosure<Delegate>::do_oop(narrowOop* ref) {
61   assert(ref != NULL, "invariant");
62   assert(is_aligned(ref, sizeof(narrowOop)), "invariant");
63   if (*ref != 0) {
64     _delegate->do_root(UnifiedOopRef::encode_in_native(ref));
65   }
66 }
67 
68 class RootSetClosureMarkScope : public MarkScope {};
69 
70 template <typename Delegate>
71 void RootSetClosure<Delegate>::process() {
72   RootSetClosureMarkScope mark_scope;
73   CLDToOopClosure cldt_closure(this, ClassLoaderData::_claim_none);
74   ClassLoaderDataGraph::always_strong_cld_do(&cldt_closure);
75   // We don't follow code blob oops, because they have misaligned oops.
76   Threads::oops_do(this, NULL);
77   ObjectSynchronizer::oops_do(this);
78   Universe::oops_do(this);
79   JNIHandles::oops_do(this);
80   JvmtiExport::oops_do(this);
81   OopStorageSet::vm_global()->oops_do(this);
82   Management::oops_do(this);
83   AOTLoader::oops_do(this);
84 }
85 
86 template class RootSetClosure<BFSClosure>;
87 template class RootSetClosure<DFSClosure>;