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