1 /* 2 * Copyright (c) 2012, 2016, 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_VM_CLASSFILE_CLASSLOADERDATA_HPP 26 #define SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP 27 28 #include "memory/allocation.hpp" 29 #include "memory/memRegion.hpp" 30 #include "memory/metaspace.hpp" 31 #include "memory/metaspaceCounters.hpp" 32 #include "runtime/mutex.hpp" 33 #include "trace/traceMacros.hpp" 34 #include "utilities/growableArray.hpp" 35 #include "utilities/macros.hpp" 36 #if INCLUDE_TRACE 37 #include "utilities/ticks.hpp" 38 #endif 39 40 // 41 // A class loader represents a linkset. Conceptually, a linkset identifies 42 // the complete transitive closure of resolved links that a dynamic linker can 43 // produce. 44 // 45 // A ClassLoaderData also encapsulates the allocation space, called a metaspace, 46 // used by the dynamic linker to allocate the runtime representation of all 47 // the types it defines. 48 // 49 // ClassLoaderData are stored in the runtime representation of classes and the 50 // system dictionary, are roots of garbage collection, and provides iterators 51 // for root tracing and other GC operations. 52 53 class ClassLoaderData; 54 class JNIMethodBlock; 55 class JNIHandleBlock; 56 class Metadebug; 57 class ModuleEntry; 58 class PackageEntry; 59 class ModuleEntryTable; 60 class PackageEntryTable; 61 62 // GC root for walking class loader data created 63 64 class ClassLoaderDataGraph : public AllStatic { 65 friend class ClassLoaderData; 66 friend class ClassLoaderDataGraphMetaspaceIterator; 67 friend class ClassLoaderDataGraphKlassIteratorAtomic; 68 friend class VMStructs; 69 private: 70 // All CLDs (except the null CLD) can be reached by walking _head->_next->... 71 static ClassLoaderData* _head; 72 static ClassLoaderData* _unloading; 73 // CMS support. 74 static ClassLoaderData* _saved_head; 75 static ClassLoaderData* _saved_unloading; 76 static bool _should_purge; 77 // OOM has been seen in metaspace allocation. Used to prevent some 78 // allocations until class unloading 79 static bool _metaspace_oom; 80 81 static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS); 82 static void post_class_unload_events(); 83 public: 84 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS); 85 static void purge(); 86 static void clear_claimed_marks(); 87 // oops do 88 static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim); 89 static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim); 90 static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim); 91 // cld do 92 static void cld_do(CLDClosure* cl); 93 static void cld_unloading_do(CLDClosure* cl); 94 static void roots_cld_do(CLDClosure* strong, CLDClosure* weak); 95 static void keep_alive_cld_do(CLDClosure* cl); 96 static void always_strong_cld_do(CLDClosure* cl); 97 // klass do 98 static void classes_do(KlassClosure* klass_closure); 99 static void classes_do(void f(Klass* const)); 100 static void methods_do(void f(Method*)); 101 static void modules_do(void f(ModuleEntry*)); 102 static void modules_unloading_do(void f(ModuleEntry*)); 103 static void packages_do(void f(PackageEntry*)); 104 static void packages_unloading_do(void f(PackageEntry*)); 105 static void loaded_classes_do(KlassClosure* klass_closure); 106 static void classes_unloading_do(void f(Klass* const)); 107 static bool do_unloading(BoolObjectClosure* is_alive, bool clean_previous_versions); 108 109 // CMS support. 110 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); } 111 static GrowableArray<ClassLoaderData*>* new_clds(); 112 113 static void set_should_purge(bool b) { _should_purge = b; } 114 static void purge_if_needed() { 115 // Only purge the CLDG for CMS if concurrent sweep is complete. 116 if (_should_purge) { 117 purge(); 118 // reset for next time. 119 set_should_purge(false); 120 } 121 } 122 123 static bool has_metaspace_oom() { return _metaspace_oom; } 124 static void set_metaspace_oom(bool value) { _metaspace_oom = value; } 125 126 static void dump_on(outputStream * const out) PRODUCT_RETURN; 127 static void dump() { dump_on(tty); } 128 static void verify(); 129 static void log_creation(Handle loader, ClassLoaderData* cld, TRAPS); 130 131 static bool unload_list_contains(const void* x); 132 #ifndef PRODUCT 133 static bool contains_loader_data(ClassLoaderData* loader_data); 134 #endif 135 136 #if INCLUDE_TRACE 137 private: 138 static Ticks _class_unload_time; 139 static void class_unload_event(Klass* const k); 140 #endif 141 }; 142 143 // ClassLoaderData class 144 145 class ClassLoaderData : public CHeapObj<mtClass> { 146 friend class VMStructs; 147 private: 148 class Dependencies VALUE_OBJ_CLASS_SPEC { 149 objArrayOop _list_head; 150 void locked_add(objArrayHandle last, 151 objArrayHandle new_dependency, 152 Thread* THREAD); 153 public: 154 Dependencies() : _list_head(NULL) {} 155 Dependencies(TRAPS) : _list_head(NULL) { 156 init(CHECK); 157 } 158 void add(Handle dependency, TRAPS); 159 void init(TRAPS); 160 void oops_do(OopClosure* f); 161 }; 162 163 friend class ClassLoaderDataGraph; 164 friend class ClassLoaderDataGraphKlassIteratorAtomic; 165 friend class ClassLoaderDataGraphMetaspaceIterator; 166 friend class MetaDataFactory; 167 friend class Method; 168 169 static ClassLoaderData * _the_null_class_loader_data; 170 171 oop _class_loader; // oop used to uniquely identify a class loader 172 // class loader or a canonical class path 173 Dependencies _dependencies; // holds dependencies from this class loader 174 // data to others. 175 176 Metaspace * volatile _metaspace; // Meta-space where meta-data defined by the 177 // classes in the class loader are allocated. 178 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup. 179 bool _unloading; // true if this class loader goes away 180 bool _is_anonymous; // if this CLD is for an anonymous class 181 s2 _keep_alive; // if this CLD is kept alive without a keep_alive_object(). 182 // Used for anonymous classes and the boot class 183 // loader. _keep_alive does not need to be volatile or 184 // atomic since there is one unique CLD per anonymous class. 185 volatile int _claimed; // true if claimed, for example during GC traces. 186 // To avoid applying oop closure more than once. 187 // Has to be an int because we cas it. 188 JNIHandleBlock* _handles; // Handles to constant pool arrays, Modules, etc, which 189 // have the same life cycle of the corresponding ClassLoader. 190 191 Klass* volatile _klasses; // The classes defined by the class loader. 192 PackageEntryTable* volatile _packages; // The packages defined by the class loader. 193 ModuleEntryTable* volatile _modules; // The modules defined by the class loader. 194 195 // These method IDs are created for the class loader and set to NULL when the 196 // class loader is unloaded. They are rarely freed, only for redefine classes 197 // and if they lose a data race in InstanceKlass. 198 JNIMethodBlock* _jmethod_ids; 199 200 // Metadata to be deallocated when it's safe at class unloading, when 201 // this class loader isn't unloaded itself. 202 GrowableArray<Metadata*>* _deallocate_list; 203 204 // Support for walking class loader data objects 205 ClassLoaderData* _next; /// Next loader_datas created 206 207 // ReadOnly and ReadWrite metaspaces (static because only on the null 208 // class loader for now). 209 static Metaspace* _ro_metaspace; 210 static Metaspace* _rw_metaspace; 211 212 TRACE_DEFINE_TRACE_ID_FIELD; 213 214 void set_next(ClassLoaderData* next) { _next = next; } 215 ClassLoaderData* next() const { return _next; } 216 217 ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies); 218 ~ClassLoaderData(); 219 220 JNIHandleBlock* handles() const; 221 void set_handles(JNIHandleBlock* handles); 222 223 // GC interface. 224 void clear_claimed() { _claimed = 0; } 225 bool claimed() const { return _claimed == 1; } 226 bool claim(); 227 228 void unload(); 229 bool keep_alive() const { return _keep_alive > 0; } 230 void classes_do(void f(Klass*)); 231 void loaded_classes_do(KlassClosure* klass_closure); 232 void classes_do(void f(InstanceKlass*)); 233 void methods_do(void f(Method*)); 234 void modules_do(void f(ModuleEntry*)); 235 void packages_do(void f(PackageEntry*)); 236 237 // Deallocate free list during class unloading. 238 void free_deallocate_list(); 239 240 // Allocate out of this class loader data 241 MetaWord* allocate(size_t size); 242 243 public: 244 245 bool is_alive(BoolObjectClosure* is_alive_closure) const; 246 247 // Accessors 248 Metaspace* metaspace_or_null() const { return _metaspace; } 249 250 static ClassLoaderData* the_null_class_loader_data() { 251 return _the_null_class_loader_data; 252 } 253 254 Mutex* metaspace_lock() const { return _metaspace_lock; } 255 256 bool is_anonymous() const { return _is_anonymous; } 257 258 static void init_null_class_loader_data() { 259 assert(_the_null_class_loader_data == NULL, "cannot initialize twice"); 260 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice"); 261 262 // We explicitly initialize the Dependencies object at a later phase in the initialization 263 _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies()); 264 ClassLoaderDataGraph::_head = _the_null_class_loader_data; 265 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be"); 266 if (DumpSharedSpaces) { 267 _the_null_class_loader_data->initialize_shared_metaspaces(); 268 } 269 } 270 271 bool is_the_null_class_loader_data() const { 272 return this == _the_null_class_loader_data; 273 } 274 bool is_system_class_loader_data() const; 275 bool is_platform_class_loader_data() const; 276 bool is_builtin_class_loader_data() const; 277 278 // The Metaspace is created lazily so may be NULL. This 279 // method will allocate a Metaspace if needed. 280 Metaspace* metaspace_non_null(); 281 282 oop class_loader() const { return _class_loader; } 283 284 // The object the GC is using to keep this ClassLoaderData alive. 285 oop keep_alive_object() const; 286 287 // Returns true if this class loader data is for a loader going away. 288 bool is_unloading() const { 289 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded"); 290 return _unloading; 291 } 292 293 // Used to refcount an anonymous class's CLD in order to 294 // indicate their aliveness without a keep_alive_object(). 295 void inc_keep_alive(); 296 void dec_keep_alive(); 297 298 inline unsigned int identity_hash() const; 299 300 // Used when tracing from klasses. 301 void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim); 302 303 void classes_do(KlassClosure* klass_closure); 304 305 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; } 306 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; } 307 308 void print_value() { print_value_on(tty); } 309 void print_value_on(outputStream* out) const; 310 void dump(outputStream * const out) PRODUCT_RETURN; 311 void verify(); 312 const char* loader_name(); 313 314 jobject add_handle(Handle h); 315 void remove_handle(jobject h); 316 void add_class(Klass* k, bool publicize = true); 317 void remove_class(Klass* k); 318 bool contains_klass(Klass* k); 319 void record_dependency(const Klass* to, TRAPS); 320 void init_dependencies(TRAPS); 321 PackageEntryTable* packages(); 322 bool packages_defined() { return (_packages != NULL); } 323 ModuleEntryTable* modules(); 324 bool modules_defined() { return (_modules != NULL); } 325 326 void add_to_deallocate_list(Metadata* m); 327 328 static ClassLoaderData* class_loader_data(oop loader); 329 static ClassLoaderData* class_loader_data_or_null(oop loader); 330 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS); 331 static void print_loader(ClassLoaderData *loader_data, outputStream *out); 332 333 // CDS support 334 Metaspace* ro_metaspace(); 335 Metaspace* rw_metaspace(); 336 void initialize_shared_metaspaces(); 337 338 TRACE_DEFINE_TRACE_ID_METHODS; 339 }; 340 341 // An iterator that distributes Klasses to parallel worker threads. 342 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj { 343 Klass* volatile _next_klass; 344 public: 345 ClassLoaderDataGraphKlassIteratorAtomic(); 346 Klass* next_klass(); 347 private: 348 static Klass* next_klass_in_cldg(Klass* klass); 349 }; 350 351 class ClassLoaderDataGraphMetaspaceIterator : public StackObj { 352 ClassLoaderData* _data; 353 public: 354 ClassLoaderDataGraphMetaspaceIterator(); 355 ~ClassLoaderDataGraphMetaspaceIterator(); 356 bool repeat() { return _data != NULL; } 357 Metaspace* get_next() { 358 assert(_data != NULL, "Should not be NULL in call to the iterator"); 359 Metaspace* result = _data->metaspace_or_null(); 360 _data = _data->next(); 361 // This result might be NULL for class loaders without metaspace 362 // yet. It would be nice to return only non-null results but 363 // there is no guarantee that there will be a non-null result 364 // down the list so the caller is going to have to check. 365 return result; 366 } 367 }; 368 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP