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