1 /*
  2  * Copyright (c) 2018, 2024, 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 #ifndef SHARE_CLASSFILE_CLASSLOADERDATAGRAPH_HPP
 26 #define SHARE_CLASSFILE_CLASSLOADERDATAGRAPH_HPP
 27 
 28 #include "classfile/classLoaderData.hpp"
 29 #include "memory/allocation.hpp"
 30 #include "utilities/growableArray.hpp"
 31 #include "utilities/macros.hpp"
 32 
 33 // GC root for walking class loader data created
 34 
 35 class ClassLoaderDataGraph : public AllStatic {
 36   friend class ClassLoaderData;
 37   friend class ClassLoaderDataGraphIteratorAtomic;
 38   friend class VMStructs;
 39  private:
 40   class ClassLoaderDataGraphIterator;
 41 
 42   // All CLDs (except unlinked CLDs) can be reached by walking _head->_next->...
 43   static ClassLoaderData* volatile _head;
 44 
 45   // Set if there's anything to purge in the deallocate lists or previous versions
 46   // during a safepoint after class unloading in a full GC.
 47   static bool _should_clean_deallocate_lists;
 48   static bool _safepoint_cleanup_needed;
 49 
 50   // OOM has been seen in metaspace allocation. Used to prevent some
 51   // allocations until class unloading
 52   static bool _metaspace_oom;
 53 
 54   static volatile size_t  _num_instance_classes;
 55   static volatile size_t  _num_array_classes;
 56 
 57   static ClassLoaderData* add_to_graph(Handle class_loader, bool has_class_mirror_holder);
 58 
 59  public:
 60   static ClassLoaderData* find_or_create(Handle class_loader, bool& created);
 61   static ClassLoaderData* add(Handle class_loader, bool has_class_mirror_holder);
 62   static void clean_module_and_package_info();
 63   static void purge(bool at_safepoint);
 64   static void clear_claimed_marks();
 65   static void clear_claimed_marks(int claim);
 66   static void verify_claimed_marks_cleared(int claim);
 67   // Iteration through CLDG; GC support
 68   static void cld_do(CLDClosure* cl);
 69   static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
 70   static void always_strong_cld_do(CLDClosure* cl);
 71   // Iteration through CLDG not by GC.
 72   // All the do suffixed functions do not keep the CLD alive. Any CLD OopHandles
 73   // (modules, mirrors, resolved refs) resolved must be treated as no keepalive.
 74   // And requires that its CLD's holder is kept alive if they escape the
 75   // caller's safepoint or ClassLoaderDataGraph_lock critical section.
 76   // The do_keepalive suffixed functions will keep all CLDs alive.
 77   static void loaded_cld_do(CLDClosure* cl);
 78   // klass do
 79   // Walking classes through the ClassLoaderDataGraph include array classes.  It also includes
 80   // classes that are allocated but not loaded, classes that have errors, and scratch classes
 81   // for redefinition.  These classes are removed during the next class unloading.
 82   // Walking the ClassLoaderDataGraph also includes hidden classes.
 83   static void classes_do(KlassClosure* klass_closure);
 84   static void classes_do(void f(Klass* const));
 85   static void methods_do(void f(Method*));
 86   static void modules_do_keepalive(void f(ModuleEntry*));
 87   static void modules_do(void f(ModuleEntry*));
 88   static void packages_do(void f(PackageEntry*));
 89   static void loaded_classes_do_keepalive(KlassClosure* klass_closure);
 90   static void classes_unloading_do(void f(Klass* const));
 91   static bool do_unloading();
 92 
 93   static inline bool should_clean_metaspaces_and_reset();
 94   static void set_should_clean_deallocate_lists() { _should_clean_deallocate_lists = true; }
 95   static void clean_deallocate_lists(bool purge_previous_versions);
 96   // Called from ServiceThread
 97   static void safepoint_and_clean_metaspaces();
 98   // Called from VMOperation
 99   static void walk_metadata_and_clean_metaspaces();
100 
101   static void verify_dictionary();
102   static void print_dictionary(outputStream* st);
103   static void print_table_statistics(outputStream* st);
104 
105   static bool has_metaspace_oom()           { return _metaspace_oom; }
106   static void set_metaspace_oom(bool value) { _metaspace_oom = value; }
107 
108   static void print_on(outputStream * const out) PRODUCT_RETURN;
109   static void print();
110   static void verify();
111 
112   // instance and array class counters
113   static inline size_t num_instance_classes();
114   static inline size_t num_array_classes();
115   static inline void inc_instance_classes(size_t count);
116   static inline void dec_instance_classes(size_t count);
117   static inline void inc_array_classes(size_t count);
118   static inline void dec_array_classes(size_t count);
119 
120 #ifndef PRODUCT
121   static bool contains_loader_data(ClassLoaderData* loader_data);
122 #endif
123 
124   // Check if ClassLoaderData is part of the ClassLoaderDataGraph (not unloaded)
125   // Usage without lock only allowed during error reporting.
126   static bool is_valid(ClassLoaderData* loader_data);
127 };
128 
129 class LockedClassesDo : public KlassClosure {
130   typedef void (*classes_do_func_t)(Klass*);
131   classes_do_func_t _function;
132   bool _do_lock;
133 public:
134   LockedClassesDo();  // For callers who provide their own do_klass
135   LockedClassesDo(classes_do_func_t function);
136   ~LockedClassesDo();
137 
138   void do_klass(Klass* k) {
139     (*_function)(k);
140   }
141 };
142 
143 // An iterator that distributes Klasses to parallel worker threads based on CLDs.
144 class ClassLoaderDataGraphIteratorAtomic : public StackObj {
145   ClassLoaderData* volatile _cld;
146 
147 public:
148   ClassLoaderDataGraphIteratorAtomic();
149 
150   ClassLoaderData* next();
151 };
152 
153 #endif // SHARE_CLASSFILE_CLASSLOADERDATAGRAPH_HPP