1 /*
   2  * Copyright (c) 2018, 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 #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 ClassLoaderDataGraphMetaspaceIterator;
  38   friend class ClassLoaderDataGraphKlassIteratorAtomic;
  39   friend class ClassLoaderDataGraphKlassIteratorStatic;
  40   friend class ClassLoaderDataGraphIterator;
  41   friend class VMStructs;
  42  private:
  43   // All CLDs (except the null CLD) can be reached by walking _head->_next->...
  44   static ClassLoaderData* volatile _head;
  45   static ClassLoaderData* _unloading;
  46 
  47   // Set if there's anything to purge in the deallocate lists or previous versions
  48   // during a safepoint after class unloading in a full GC.
  49   static bool _should_clean_deallocate_lists;
  50   static bool _safepoint_cleanup_needed;
  51 
  52   // OOM has been seen in metaspace allocation. Used to prevent some
  53   // allocations until class unloading
  54   static bool _metaspace_oom;
  55 
  56   static volatile size_t  _num_instance_classes;
  57   static volatile size_t  _num_array_classes;
  58 
  59   static ClassLoaderData* add_to_graph(Handle class_loader, bool has_class_mirror_holder);
  60 
  61  public:
  62   static ClassLoaderData* find_or_create(Handle class_loader);
  63   static ClassLoaderData* add(Handle class_loader, bool has_class_mirror_holder);
  64   static void clean_module_and_package_info();
  65   static void purge();
  66   static void clear_claimed_marks();
  67   static void clear_claimed_marks(int claim);
  68   // Iteration through CLDG inside a safepoint; GC support
  69   static void cld_do(CLDClosure* cl);
  70   static void cld_unloading_do(CLDClosure* cl);
  71   static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
  72   static void always_strong_cld_do(CLDClosure* cl);
  73   // Iteration through CLDG not by GC.
  74   static void loaded_cld_do(CLDClosure* cl);
  75   // klass do
  76   // Walking classes through the ClassLoaderDataGraph include array classes.  It also includes
  77   // classes that are allocated but not loaded, classes that have errors, and scratch classes
  78   // for redefinition.  These classes are removed during the next class unloading.
  79   // Walking the ClassLoaderDataGraph also includes hidden and unsafe anonymous classes.
  80   static void classes_do(KlassClosure* klass_closure);
  81   static void classes_do(void f(Klass* const));
  82   static void methods_do(void f(Method*));
  83   static void modules_do(void f(ModuleEntry*));
  84   static void modules_unloading_do(void f(ModuleEntry*));
  85   static void packages_do(void f(PackageEntry*));
  86   static void packages_unloading_do(void f(PackageEntry*));
  87   static void loaded_classes_do(KlassClosure* klass_closure);
  88   static void unlocked_loaded_classes_do(KlassClosure* klass_closure);
  89   static void classes_unloading_do(void f(Klass* const));
  90   static bool do_unloading();
  91 
  92   // Expose state to avoid logging overhead in safepoint cleanup tasks.
  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   static void walk_metadata_and_clean_metaspaces();
  97 
  98   // dictionary do
  99   // Iterate over all klasses in dictionary, but
 100   // just the classes from defining class loaders.
 101   static void dictionary_classes_do(void f(InstanceKlass*));
 102   // Added for initialize_itable_for_klass to handle exceptions.
 103   static void dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS);
 104 
 105   // VM_CounterDecay iteration support
 106   static InstanceKlass* try_get_next_class();
 107   static void adjust_saved_class(ClassLoaderData* cld);
 108   static void adjust_saved_class(Klass* klass);
 109 
 110   static void verify_dictionary();
 111   static void print_dictionary(outputStream* st);
 112   static void print_table_statistics(outputStream* st);
 113 
 114   static int resize_dictionaries();
 115 
 116   static bool has_metaspace_oom()           { return _metaspace_oom; }
 117   static void set_metaspace_oom(bool value) { _metaspace_oom = value; }
 118 
 119   static void print_on(outputStream * const out) PRODUCT_RETURN;
 120   static void print();
 121   static void verify();
 122 
 123   // instance and array class counters
 124   static inline size_t num_instance_classes();
 125   static inline size_t num_array_classes();
 126   static inline void inc_instance_classes(size_t count);
 127   static inline void dec_instance_classes(size_t count);
 128   static inline void inc_array_classes(size_t count);
 129   static inline void dec_array_classes(size_t count);
 130 
 131 #ifndef PRODUCT
 132   static bool contains_loader_data(ClassLoaderData* loader_data);
 133 #endif
 134 
 135   // Check if ClassLoaderData is part of the ClassLoaderDataGraph (not unloaded)
 136   // Usage without lock only allowed during error reporting.
 137   static bool is_valid(ClassLoaderData* loader_data);
 138 };
 139 
 140 class LockedClassesDo : public KlassClosure {
 141   typedef void (*classes_do_func_t)(Klass*);
 142   classes_do_func_t _function;
 143   bool _do_lock;
 144 public:
 145   LockedClassesDo();  // For callers who provide their own do_klass
 146   LockedClassesDo(classes_do_func_t function);
 147   ~LockedClassesDo();
 148 
 149   void do_klass(Klass* k) {
 150     (*_function)(k);
 151   }
 152 };
 153 
 154 // An iterator that distributes Klasses to parallel worker threads.
 155 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
 156  Klass* volatile _next_klass;
 157  public:
 158   ClassLoaderDataGraphKlassIteratorAtomic();
 159   Klass* next_klass();
 160  private:
 161   static Klass* next_klass_in_cldg(Klass* klass);
 162 };
 163 
 164 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
 165   ClassLoaderData* _data;
 166  public:
 167   ClassLoaderDataGraphMetaspaceIterator();
 168   ~ClassLoaderDataGraphMetaspaceIterator();
 169   bool repeat() { return _data != NULL; }
 170   ClassLoaderMetaspace* get_next();
 171 };
 172 #endif // SHARE_CLASSFILE_CLASSLOADERDATAGRAPH_HPP