Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/classfile/classLoaderData.cpp
32285 views
1
/*
2
* Copyright (c) 2012, 2018, 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
// A ClassLoaderData identifies the full set of class types that a class
26
// loader's name resolution strategy produces for a given configuration of the
27
// class loader.
28
// Class types in the ClassLoaderData may be defined by from class file binaries
29
// provided by the class loader, or from other class loader it interacts with
30
// according to its name resolution strategy.
31
//
32
// Class loaders that implement a deterministic name resolution strategy
33
// (including with respect to their delegation behavior), such as the boot, the
34
// extension, and the system loaders of the JDK's built-in class loader
35
// hierarchy, always produce the same linkset for a given configuration.
36
//
37
// ClassLoaderData carries information related to a linkset (e.g.,
38
// metaspace holding its klass definitions).
39
// The System Dictionary and related data structures (e.g., placeholder table,
40
// loader constraints table) as well as the runtime representation of classes
41
// only reference ClassLoaderData.
42
//
43
// Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
44
// that represent the loader's "linking domain" in the JVM.
45
//
46
// The bootstrap loader (represented by NULL) also has a ClassLoaderData,
47
// the singleton class the_null_class_loader_data().
48
49
#include "precompiled.hpp"
50
#include "classfile/classLoaderData.hpp"
51
#include "classfile/classLoaderData.inline.hpp"
52
#include "classfile/javaClasses.hpp"
53
#include "classfile/metadataOnStackMark.hpp"
54
#include "classfile/systemDictionary.hpp"
55
#include "code/codeCache.hpp"
56
#include "memory/gcLocker.hpp"
57
#include "memory/metadataFactory.hpp"
58
#include "memory/metaspaceShared.hpp"
59
#include "memory/oopFactory.hpp"
60
#include "runtime/jniHandles.hpp"
61
#include "runtime/mutex.hpp"
62
#include "runtime/safepoint.hpp"
63
#include "runtime/synchronizer.hpp"
64
#include "utilities/growableArray.hpp"
65
#include "utilities/macros.hpp"
66
#include "utilities/ostream.hpp"
67
68
ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
69
70
ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
71
_class_loader(h_class_loader()),
72
_is_anonymous(is_anonymous),
73
// An anonymous class loader data doesn't have anything to keep
74
// it from being unloaded during parsing of the anonymous class.
75
// The null-class-loader should always be kept alive.
76
_keep_alive(is_anonymous || h_class_loader.is_null()),
77
_metaspace(NULL), _unloading(false), _klasses(NULL),
78
_claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
79
_next(NULL), _dependencies(dependencies),
80
_metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
81
82
JFR_ONLY(INIT_ID(this);)
83
}
84
85
void ClassLoaderData::init_dependencies(TRAPS) {
86
assert(!Universe::is_fully_initialized(), "should only be called when initializing");
87
assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
88
_dependencies.init(CHECK);
89
}
90
91
void ClassLoaderData::Dependencies::init(TRAPS) {
92
// Create empty dependencies array to add to. CMS requires this to be
93
// an oop so that it can track additions via card marks. We think.
94
_list_head = oopFactory::new_objectArray(2, CHECK);
95
}
96
97
ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
98
Chunk* c = _head;
99
while (c != NULL) {
100
Chunk* next = c->_next;
101
delete c;
102
c = next;
103
}
104
}
105
106
oop* ClassLoaderData::ChunkedHandleList::add(oop o) {
107
if (_head == NULL || _head->_size == Chunk::CAPACITY) {
108
Chunk* next = new Chunk(_head);
109
OrderAccess::release_store_ptr(&_head, next);
110
}
111
oop* handle = &_head->_data[_head->_size];
112
*handle = o;
113
OrderAccess::release_store(&_head->_size, _head->_size + 1);
114
return handle;
115
}
116
117
inline void ClassLoaderData::ChunkedHandleList::oops_do_chunk(OopClosure* f, Chunk* c, const juint size) {
118
for (juint i = 0; i < size; i++) {
119
if (c->_data[i] != NULL) {
120
f->do_oop(&c->_data[i]);
121
}
122
}
123
}
124
125
void ClassLoaderData::ChunkedHandleList::oops_do(OopClosure* f) {
126
Chunk* head = (Chunk*) OrderAccess::load_ptr_acquire(&_head);
127
if (head != NULL) {
128
// Must be careful when reading size of head
129
oops_do_chunk(f, head, OrderAccess::load_acquire(&head->_size));
130
for (Chunk* c = head->_next; c != NULL; c = c->_next) {
131
oops_do_chunk(f, c, c->_size);
132
}
133
}
134
}
135
136
bool ClassLoaderData::claim() {
137
if (_claimed == 1) {
138
return false;
139
}
140
141
return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
142
}
143
144
void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
145
if (must_claim && !claim()) {
146
return;
147
}
148
149
f->do_oop(&_class_loader);
150
_dependencies.oops_do(f);
151
_handles.oops_do(f);
152
if (klass_closure != NULL) {
153
classes_do(klass_closure);
154
}
155
}
156
157
void ClassLoaderData::Dependencies::oops_do(OopClosure* f) {
158
f->do_oop((oop*)&_list_head);
159
}
160
161
void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
162
for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
163
klass_closure->do_klass(k);
164
assert(k != k->next_link(), "no loops!");
165
}
166
}
167
168
void ClassLoaderData::classes_do(void f(Klass * const)) {
169
for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
170
f(k);
171
}
172
}
173
174
void ClassLoaderData::loaded_classes_do(KlassClosure* klass_closure) {
175
// Lock to avoid classes being modified/added/removed during iteration
176
MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
177
for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
178
// Do not filter ArrayKlass oops here...
179
if (k->oop_is_array() || (k->oop_is_instance() && InstanceKlass::cast(k)->is_loaded())) {
180
klass_closure->do_klass(k);
181
}
182
}
183
}
184
185
void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
186
for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
187
if (k->oop_is_instance()) {
188
f(InstanceKlass::cast(k));
189
}
190
assert(k != k->next_link(), "no loops!");
191
}
192
}
193
194
void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
195
ClassLoaderData * const from_cld = this;
196
ClassLoaderData * const to_cld = k->class_loader_data();
197
198
// Dependency to the null class loader data doesn't need to be recorded
199
// because the null class loader data never goes away.
200
if (to_cld->is_the_null_class_loader_data()) {
201
return;
202
}
203
204
oop to;
205
if (to_cld->is_anonymous()) {
206
// Anonymous class dependencies are through the mirror.
207
to = k->java_mirror();
208
} else {
209
to = to_cld->class_loader();
210
211
// If from_cld is anonymous, even if it's class_loader is a parent of 'to'
212
// we still have to add it. The class_loader won't keep from_cld alive.
213
if (!from_cld->is_anonymous()) {
214
// Check that this dependency isn't from the same or parent class_loader
215
oop from = from_cld->class_loader();
216
217
oop curr = from;
218
while (curr != NULL) {
219
if (curr == to) {
220
return; // this class loader is in the parent list, no need to add it.
221
}
222
curr = java_lang_ClassLoader::parent(curr);
223
}
224
}
225
}
226
227
// It's a dependency we won't find through GC, add it. This is relatively rare
228
// Must handle over GC point.
229
Handle dependency(THREAD, to);
230
from_cld->_dependencies.add(dependency, CHECK);
231
}
232
233
234
void ClassLoaderData::Dependencies::add(Handle dependency, TRAPS) {
235
// Check first if this dependency is already in the list.
236
// Save a pointer to the last to add to under the lock.
237
objArrayOop ok = _list_head;
238
objArrayOop last = NULL;
239
while (ok != NULL) {
240
last = ok;
241
if (ok->obj_at(0) == dependency()) {
242
// Don't need to add it
243
return;
244
}
245
ok = (objArrayOop)ok->obj_at(1);
246
}
247
248
// Must handle over GC points
249
assert (last != NULL, "dependencies should be initialized");
250
objArrayHandle last_handle(THREAD, last);
251
252
// Create a new dependency node with fields for (class_loader or mirror, next)
253
objArrayOop deps = oopFactory::new_objectArray(2, CHECK);
254
deps->obj_at_put(0, dependency());
255
256
// Must handle over GC points
257
objArrayHandle new_dependency(THREAD, deps);
258
259
// Add the dependency under lock
260
locked_add(last_handle, new_dependency, THREAD);
261
}
262
263
void ClassLoaderData::Dependencies::locked_add(objArrayHandle last_handle,
264
objArrayHandle new_dependency,
265
Thread* THREAD) {
266
267
// Have to lock and put the new dependency on the end of the dependency
268
// array so the card mark for CMS sees that this dependency is new.
269
// Can probably do this lock free with some effort.
270
ObjectLocker ol(Handle(THREAD, _list_head), THREAD);
271
272
oop loader_or_mirror = new_dependency->obj_at(0);
273
274
// Since the dependencies are only added, add to the end.
275
objArrayOop end = last_handle();
276
objArrayOop last = NULL;
277
while (end != NULL) {
278
last = end;
279
// check again if another thread added it to the end.
280
if (end->obj_at(0) == loader_or_mirror) {
281
// Don't need to add it
282
return;
283
}
284
end = (objArrayOop)end->obj_at(1);
285
}
286
assert (last != NULL, "dependencies should be initialized");
287
// fill in the first element with the oop in new_dependency.
288
if (last->obj_at(0) == NULL) {
289
last->obj_at_put(0, new_dependency->obj_at(0));
290
} else {
291
last->obj_at_put(1, new_dependency());
292
}
293
}
294
295
void ClassLoaderDataGraph::clear_claimed_marks() {
296
for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
297
cld->clear_claimed();
298
}
299
}
300
301
void ClassLoaderData::add_class(Klass* k) {
302
MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
303
Klass* old_value = _klasses;
304
k->set_next_link(old_value);
305
// link the new item into the list
306
_klasses = k;
307
308
if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) {
309
ResourceMark rm;
310
tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "
311
PTR_FORMAT " loader: " PTR_FORMAT " %s",
312
p2i(k),
313
k->external_name(),
314
p2i(k->class_loader_data()),
315
p2i((void *)k->class_loader()),
316
loader_name());
317
}
318
}
319
320
// This is called by InstanceKlass::deallocate_contents() to remove the
321
// scratch_class for redefine classes. We need a lock because there it may not
322
// be called at a safepoint if there's an error.
323
void ClassLoaderData::remove_class(Klass* scratch_class) {
324
MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
325
Klass* prev = NULL;
326
for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
327
if (k == scratch_class) {
328
if (prev == NULL) {
329
_klasses = k->next_link();
330
} else {
331
Klass* next = k->next_link();
332
prev->set_next_link(next);
333
}
334
return;
335
}
336
prev = k;
337
assert(k != k->next_link(), "no loops!");
338
}
339
ShouldNotReachHere(); // should have found this class!!
340
}
341
342
void ClassLoaderData::unload() {
343
_unloading = true;
344
345
// Tell serviceability tools these classes are unloading
346
classes_do(InstanceKlass::notify_unload_class);
347
348
if (TraceClassLoaderData) {
349
ResourceMark rm;
350
tty->print("[ClassLoaderData: unload loader data " INTPTR_FORMAT, p2i(this));
351
tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
352
loader_name());
353
if (is_anonymous()) {
354
tty->print(" for anonymous class " INTPTR_FORMAT " ", p2i(_klasses));
355
}
356
tty->print_cr("]");
357
}
358
}
359
360
oop ClassLoaderData::keep_alive_object() const {
361
assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive");
362
return is_anonymous() ? _klasses->java_mirror() : class_loader();
363
}
364
365
bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
366
bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
367
|| is_alive_closure->do_object_b(keep_alive_object());
368
369
return alive;
370
}
371
372
373
ClassLoaderData::~ClassLoaderData() {
374
// Release C heap structures for all the classes.
375
classes_do(InstanceKlass::release_C_heap_structures);
376
377
Metaspace *m = _metaspace;
378
if (m != NULL) {
379
_metaspace = NULL;
380
// release the metaspace
381
delete m;
382
}
383
384
// Clear all the JNI handles for methods
385
// These aren't deallocated and are going to look like a leak, but that's
386
// needed because we can't really get rid of jmethodIDs because we don't
387
// know when native code is going to stop using them. The spec says that
388
// they're "invalid" but existing programs likely rely on their being
389
// NULL after class unloading.
390
if (_jmethod_ids != NULL) {
391
Method::clear_jmethod_ids(this);
392
}
393
// Delete lock
394
delete _metaspace_lock;
395
396
// Delete free list
397
if (_deallocate_list != NULL) {
398
delete _deallocate_list;
399
}
400
}
401
402
/**
403
* Returns true if this class loader data is for the extension class loader.
404
*/
405
bool ClassLoaderData::is_ext_class_loader_data() const {
406
return SystemDictionary::is_ext_class_loader(class_loader());
407
}
408
409
Metaspace* ClassLoaderData::metaspace_non_null() {
410
assert(!DumpSharedSpaces, "wrong metaspace!");
411
// If the metaspace has not been allocated, create a new one. Might want
412
// to create smaller arena for Reflection class loaders also.
413
// The reason for the delayed allocation is because some class loaders are
414
// simply for delegating with no metadata of their own.
415
if (_metaspace == NULL) {
416
MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
417
// Check again if metaspace has been allocated while we were getting this lock.
418
if (_metaspace != NULL) {
419
return _metaspace;
420
}
421
if (this == the_null_class_loader_data()) {
422
assert (class_loader() == NULL, "Must be");
423
set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
424
} else if (is_anonymous()) {
425
if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
426
tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
427
}
428
set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
429
} else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
430
if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
431
tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
432
}
433
set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
434
} else {
435
set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
436
}
437
}
438
return _metaspace;
439
}
440
441
jobject ClassLoaderData::add_handle(Handle h) {
442
MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
443
return (jobject) _handles.add(h());
444
}
445
446
// Add this metadata pointer to be freed when it's safe. This is only during
447
// class unloading because Handles might point to this metadata field.
448
void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
449
// Metadata in shared region isn't deleted.
450
if (!m->is_shared()) {
451
MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
452
if (_deallocate_list == NULL) {
453
_deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
454
}
455
_deallocate_list->append_if_missing(m);
456
}
457
}
458
459
// Deallocate free metadata on the free list. How useful the PermGen was!
460
void ClassLoaderData::free_deallocate_list() {
461
// Don't need lock, at safepoint
462
assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
463
if (_deallocate_list == NULL) {
464
return;
465
}
466
// Go backwards because this removes entries that are freed.
467
for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
468
Metadata* m = _deallocate_list->at(i);
469
if (!m->on_stack()) {
470
_deallocate_list->remove_at(i);
471
// There are only three types of metadata that we deallocate directly.
472
// Cast them so they can be used by the template function.
473
if (m->is_method()) {
474
MetadataFactory::free_metadata(this, (Method*)m);
475
} else if (m->is_constantPool()) {
476
MetadataFactory::free_metadata(this, (ConstantPool*)m);
477
} else if (m->is_klass()) {
478
MetadataFactory::free_metadata(this, (InstanceKlass*)m);
479
} else {
480
ShouldNotReachHere();
481
}
482
}
483
}
484
}
485
486
// These anonymous class loaders are to contain classes used for JSR292
487
ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
488
// Add a new class loader data to the graph.
489
return ClassLoaderDataGraph::add(loader, true, THREAD);
490
}
491
492
const char* ClassLoaderData::loader_name() {
493
// Handles null class loader
494
return SystemDictionary::loader_name(class_loader());
495
}
496
497
#ifndef PRODUCT
498
// Define to dump klasses
499
#undef CLD_DUMP_KLASSES
500
501
void ClassLoaderData::dump(outputStream * const out) {
502
ResourceMark rm;
503
out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: " PTR_FORMAT " %s {",
504
p2i(this), p2i((void *)class_loader()),
505
p2i(class_loader() != NULL ? class_loader()->klass() : NULL), loader_name());
506
if (claimed()) out->print(" claimed ");
507
if (is_unloading()) out->print(" unloading ");
508
out->cr();
509
if (metaspace_or_null() != NULL) {
510
out->print_cr("metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
511
metaspace_or_null()->dump(out);
512
} else {
513
out->print_cr("metaspace: NULL");
514
}
515
516
#ifdef CLD_DUMP_KLASSES
517
if (Verbose) {
518
ResourceMark rm;
519
Klass* k = _klasses;
520
while (k != NULL) {
521
out->print_cr("klass " PTR_FORMAT ", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
522
k->has_modified_oops(), k->has_accumulated_modified_oops());
523
assert(k != k->next_link(), "no loops!");
524
k = k->next_link();
525
}
526
}
527
#endif // CLD_DUMP_KLASSES
528
#undef CLD_DUMP_KLASSES
529
if (_jmethod_ids != NULL) {
530
Method::print_jmethod_ids(this, out);
531
}
532
out->print_cr("}");
533
}
534
#endif // PRODUCT
535
536
void ClassLoaderData::verify() {
537
oop cl = class_loader();
538
539
guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
540
guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
541
542
// Verify the integrity of the allocated space.
543
if (metaspace_or_null() != NULL) {
544
metaspace_or_null()->verify();
545
}
546
547
for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
548
guarantee(k->class_loader_data() == this, "Must be the same");
549
k->verify();
550
assert(k != k->next_link(), "no loops!");
551
}
552
}
553
554
bool ClassLoaderData::contains_klass(Klass* klass) {
555
for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
556
if (k == klass) return true;
557
}
558
return false;
559
}
560
561
562
// GC root of class loader data created.
563
ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
564
ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
565
ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
566
ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
567
568
bool ClassLoaderDataGraph::_should_purge = false;
569
570
// Add a new class loader data node to the list. Assign the newly created
571
// ClassLoaderData into the java/lang/ClassLoader object as a hidden field
572
ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) {
573
// We need to allocate all the oops for the ClassLoaderData before allocating the
574
// actual ClassLoaderData object.
575
ClassLoaderData::Dependencies dependencies(CHECK_NULL);
576
577
No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the
578
// ClassLoaderData in the graph since the CLD
579
// contains unhandled oops
580
581
ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous, dependencies);
582
583
584
if (!is_anonymous) {
585
ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());
586
// First, Atomically set it
587
ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
588
if (old != NULL) {
589
delete cld;
590
// Returns the data.
591
return old;
592
}
593
}
594
595
// We won the race, and therefore the task of adding the data to the list of
596
// class loader data
597
ClassLoaderData** list_head = &_head;
598
ClassLoaderData* next = _head;
599
600
do {
601
cld->set_next(next);
602
ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
603
if (exchanged == next) {
604
if (TraceClassLoaderData) {
605
ResourceMark rm;
606
tty->print("[ClassLoaderData: ");
607
tty->print("create class loader data " INTPTR_FORMAT, p2i(cld));
608
tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()),
609
cld->loader_name());
610
tty->print_cr("]");
611
}
612
return cld;
613
}
614
next = exchanged;
615
} while (true);
616
617
}
618
619
void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
620
for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
621
cld->oops_do(f, klass_closure, must_claim);
622
}
623
}
624
625
void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
626
for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
627
if (cld->keep_alive()) {
628
cld->oops_do(f, klass_closure, must_claim);
629
}
630
}
631
}
632
633
void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
634
if (ClassUnloading) {
635
keep_alive_oops_do(f, klass_closure, must_claim);
636
} else {
637
oops_do(f, klass_closure, must_claim);
638
}
639
}
640
641
void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
642
for (ClassLoaderData* cld = _head; cl != NULL && cld != NULL; cld = cld->next()) {
643
cl->do_cld(cld);
644
}
645
}
646
647
void ClassLoaderDataGraph::cld_unloading_do(CLDClosure* cl) {
648
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
649
// Only walk the head until any clds not purged from prior unloading
650
// (CMS doesn't purge right away).
651
for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
652
assert(cld->is_unloading(), "invariant");
653
cl->do_cld(cld);
654
}
655
}
656
657
void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) {
658
for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->_next) {
659
CLDClosure* closure = cld->keep_alive() ? strong : weak;
660
if (closure != NULL) {
661
closure->do_cld(cld);
662
}
663
}
664
}
665
666
void ClassLoaderDataGraph::keep_alive_cld_do(CLDClosure* cl) {
667
roots_cld_do(cl, NULL);
668
}
669
670
void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) {
671
if (ClassUnloading) {
672
keep_alive_cld_do(cl);
673
} else {
674
cld_do(cl);
675
}
676
}
677
678
void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
679
for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
680
cld->classes_do(klass_closure);
681
}
682
}
683
684
void ClassLoaderDataGraph::classes_do(void f(Klass* const)) {
685
for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
686
cld->classes_do(f);
687
}
688
}
689
690
void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {
691
for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
692
cld->loaded_classes_do(klass_closure);
693
}
694
}
695
696
void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
697
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
698
// Only walk the head until any clds not purged from prior unloading
699
// (CMS doesn't purge right away).
700
for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
701
cld->classes_do(f);
702
}
703
}
704
705
GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
706
assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
707
708
GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
709
710
// The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
711
ClassLoaderData* curr = _head;
712
while (curr != _saved_head) {
713
if (!curr->claimed()) {
714
array->push(curr);
715
716
if (TraceClassLoaderData) {
717
tty->print("[ClassLoaderData] found new CLD: ");
718
curr->print_value_on(tty);
719
tty->cr();
720
}
721
}
722
723
curr = curr->_next;
724
}
725
726
return array;
727
}
728
729
bool ClassLoaderDataGraph::unload_list_contains(const void* x) {
730
assert(SafepointSynchronize::is_at_safepoint(), "only safe to call at safepoint");
731
for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
732
if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
733
return true;
734
}
735
}
736
return false;
737
}
738
739
#ifndef PRODUCT
740
bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
741
for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
742
if (loader_data == data) {
743
return true;
744
}
745
}
746
747
return false;
748
}
749
#endif // PRODUCT
750
751
// Move class loader data from main list to the unloaded list for unloading
752
// and deallocation later.
753
bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure, bool clean_alive) {
754
ClassLoaderData* data = _head;
755
ClassLoaderData* prev = NULL;
756
bool seen_dead_loader = false;
757
758
// Save previous _unloading pointer for CMS which may add to unloading list before
759
// purging and we don't want to rewalk the previously unloaded class loader data.
760
_saved_unloading = _unloading;
761
762
while (data != NULL) {
763
if (data->is_alive(is_alive_closure)) {
764
prev = data;
765
data = data->next();
766
continue;
767
}
768
seen_dead_loader = true;
769
ClassLoaderData* dead = data;
770
dead->unload();
771
data = data->next();
772
// Remove from loader list.
773
// This class loader data will no longer be found
774
// in the ClassLoaderDataGraph.
775
if (prev != NULL) {
776
prev->set_next(data);
777
} else {
778
assert(dead == _head, "sanity check");
779
_head = data;
780
}
781
dead->set_next(_unloading);
782
_unloading = dead;
783
}
784
785
if (clean_alive) {
786
// Clean previous versions and the deallocate list.
787
ClassLoaderDataGraph::clean_metaspaces();
788
}
789
790
return seen_dead_loader;
791
}
792
793
void ClassLoaderDataGraph::clean_metaspaces() {
794
// mark metadata seen on the stack and code cache so we can delete unneeded entries.
795
bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
796
MetadataOnStackMark md_on_stack(has_redefined_a_class);
797
798
if (has_redefined_a_class) {
799
// purge_previous_versions also cleans weak method links. Because
800
// one method's MDO can reference another method from another
801
// class loader, we need to first clean weak method links for all
802
// class loaders here. Below, we can then free redefined methods
803
// for all class loaders.
804
for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
805
data->classes_do(InstanceKlass::purge_previous_versions);
806
}
807
}
808
809
// Need to purge the previous version before deallocating.
810
free_deallocate_lists();
811
}
812
813
void ClassLoaderDataGraph::purge() {
814
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
815
ClassLoaderData* list = _unloading;
816
_unloading = NULL;
817
ClassLoaderData* next = list;
818
while (next != NULL) {
819
ClassLoaderData* purge_me = next;
820
next = purge_me->next();
821
delete purge_me;
822
}
823
Metaspace::purge();
824
}
825
826
void ClassLoaderDataGraph::free_deallocate_lists() {
827
for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
828
// We need to keep this data until InstanceKlass::purge_previous_version has been
829
// called on all alive classes. See the comment in ClassLoaderDataGraph::clean_metaspaces.
830
cld->free_deallocate_list();
831
}
832
833
// In some rare cases items added to the unloading list will not be freed elsewhere.
834
// To keep it simple, walk the _unloading list also.
835
for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
836
cld->free_deallocate_list();
837
}
838
}
839
840
// CDS support
841
842
// Global metaspaces for writing information to the shared archive. When
843
// application CDS is supported, we may need one per metaspace, so this
844
// sort of looks like it.
845
Metaspace* ClassLoaderData::_ro_metaspace = NULL;
846
Metaspace* ClassLoaderData::_rw_metaspace = NULL;
847
static bool _shared_metaspaces_initialized = false;
848
849
// Initialize shared metaspaces (change to call from somewhere not lazily)
850
void ClassLoaderData::initialize_shared_metaspaces() {
851
assert(DumpSharedSpaces, "only use this for dumping shared spaces");
852
assert(this == ClassLoaderData::the_null_class_loader_data(),
853
"only supported for null loader data for now");
854
assert (!_shared_metaspaces_initialized, "only initialize once");
855
MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
856
_ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType);
857
_rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType);
858
_shared_metaspaces_initialized = true;
859
}
860
861
Metaspace* ClassLoaderData::ro_metaspace() {
862
assert(_ro_metaspace != NULL, "should already be initialized");
863
return _ro_metaspace;
864
}
865
866
Metaspace* ClassLoaderData::rw_metaspace() {
867
assert(_rw_metaspace != NULL, "should already be initialized");
868
return _rw_metaspace;
869
}
870
871
ClassLoaderDataGraphKlassIteratorAtomic::ClassLoaderDataGraphKlassIteratorAtomic()
872
: _next_klass(NULL) {
873
ClassLoaderData* cld = ClassLoaderDataGraph::_head;
874
Klass* klass = NULL;
875
876
// Find the first klass in the CLDG.
877
while (cld != NULL) {
878
klass = cld->_klasses;
879
if (klass != NULL) {
880
_next_klass = klass;
881
return;
882
}
883
cld = cld->next();
884
}
885
}
886
887
Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass_in_cldg(Klass* klass) {
888
Klass* next = klass->next_link();
889
if (next != NULL) {
890
return next;
891
}
892
893
// No more klasses in the current CLD. Time to find a new CLD.
894
ClassLoaderData* cld = klass->class_loader_data();
895
while (next == NULL) {
896
cld = cld->next();
897
if (cld == NULL) {
898
break;
899
}
900
next = cld->_klasses;
901
}
902
903
return next;
904
}
905
906
Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass() {
907
Klass* head = _next_klass;
908
909
while (head != NULL) {
910
Klass* next = next_klass_in_cldg(head);
911
912
Klass* old_head = (Klass*)Atomic::cmpxchg_ptr(next, &_next_klass, head);
913
914
if (old_head == head) {
915
return head; // Won the CAS.
916
}
917
918
head = old_head;
919
}
920
921
// Nothing more for the iterator to hand out.
922
assert(head == NULL, err_msg("head is " PTR_FORMAT ", expected not null:", p2i(head)));
923
return NULL;
924
}
925
926
ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
927
_data = ClassLoaderDataGraph::_head;
928
}
929
930
ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
931
932
#ifndef PRODUCT
933
// callable from debugger
934
extern "C" int print_loader_data_graph() {
935
ClassLoaderDataGraph::dump_on(tty);
936
return 0;
937
}
938
939
void ClassLoaderDataGraph::verify() {
940
for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
941
data->verify();
942
}
943
}
944
945
void ClassLoaderDataGraph::dump_on(outputStream * const out) {
946
for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
947
data->dump(out);
948
}
949
MetaspaceAux::dump(out);
950
}
951
#endif // PRODUCT
952
953
void ClassLoaderData::print_value_on(outputStream* out) const {
954
if (class_loader() == NULL) {
955
out->print("NULL class_loader");
956
} else {
957
out->print("class loader " INTPTR_FORMAT, p2i(this));
958
class_loader()->print_value_on(out);
959
}
960
}
961
962