Fri, 03 May 2013 05:05:31 -0700
8004095: Add support for JMX interface to Diagnostic Framework and Commands
Reviewed-by: acorn, sla
1 /*
2 * Copyright (c) 2003, 2012, 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 */
25 #include "precompiled.hpp"
26 #include "classfile/dictionary.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "oops/oop.inline.hpp"
29 #include "prims/jvmtiRedefineClassesTrace.hpp"
30 #include "utilities/hashtable.inline.hpp"
33 DictionaryEntry* Dictionary::_current_class_entry = NULL;
34 int Dictionary::_current_class_index = 0;
37 Dictionary::Dictionary(int table_size)
38 : TwoOopHashtable<Klass*, mtClass>(table_size, sizeof(DictionaryEntry)) {
39 _current_class_index = 0;
40 _current_class_entry = NULL;
41 };
45 Dictionary::Dictionary(int table_size, HashtableBucket<mtClass>* t,
46 int number_of_entries)
47 : TwoOopHashtable<Klass*, mtClass>(table_size, sizeof(DictionaryEntry), t, number_of_entries) {
48 _current_class_index = 0;
49 _current_class_entry = NULL;
50 };
53 DictionaryEntry* Dictionary::new_entry(unsigned int hash, Klass* klass,
54 ClassLoaderData* loader_data) {
55 DictionaryEntry* entry = (DictionaryEntry*)Hashtable<Klass*, mtClass>::new_entry(hash, klass);
56 entry->set_loader_data(loader_data);
57 entry->set_pd_set(NULL);
58 assert(klass->oop_is_instance(), "Must be");
59 return entry;
60 }
63 void Dictionary::free_entry(DictionaryEntry* entry) {
64 // avoid recursion when deleting linked list
65 while (entry->pd_set() != NULL) {
66 ProtectionDomainEntry* to_delete = entry->pd_set();
67 entry->set_pd_set(to_delete->next());
68 delete to_delete;
69 }
70 Hashtable<Klass*, mtClass>::free_entry(entry);
71 }
74 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const {
75 #ifdef ASSERT
76 if (protection_domain == InstanceKlass::cast(klass())->protection_domain()) {
77 // Ensure this doesn't show up in the pd_set (invariant)
78 bool in_pd_set = false;
79 for (ProtectionDomainEntry* current = _pd_set;
80 current != NULL;
81 current = current->next()) {
82 if (current->protection_domain() == protection_domain) {
83 in_pd_set = true;
84 break;
85 }
86 }
87 if (in_pd_set) {
88 assert(false, "A klass's protection domain should not show up "
89 "in its sys. dict. PD set");
90 }
91 }
92 #endif /* ASSERT */
94 if (protection_domain == InstanceKlass::cast(klass())->protection_domain()) {
95 // Succeeds trivially
96 return true;
97 }
99 for (ProtectionDomainEntry* current = _pd_set;
100 current != NULL;
101 current = current->next()) {
102 if (current->protection_domain() == protection_domain) return true;
103 }
104 return false;
105 }
108 void DictionaryEntry::add_protection_domain(oop protection_domain) {
109 assert_locked_or_safepoint(SystemDictionary_lock);
110 if (!contains_protection_domain(protection_domain)) {
111 ProtectionDomainEntry* new_head =
112 new ProtectionDomainEntry(protection_domain, _pd_set);
113 // Warning: Preserve store ordering. The SystemDictionary is read
114 // without locks. The new ProtectionDomainEntry must be
115 // complete before other threads can be allowed to see it
116 // via a store to _pd_set.
117 OrderAccess::release_store_ptr(&_pd_set, new_head);
118 }
119 if (TraceProtectionDomainVerification && WizardMode) {
120 print();
121 }
122 }
125 bool Dictionary::do_unloading() {
126 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
127 bool class_was_unloaded = false;
128 int index = 0; // Defined here for portability! Do not move
130 // Remove unloadable entries and classes from system dictionary
131 // The placeholder array has been handled in always_strong_oops_do.
132 DictionaryEntry* probe = NULL;
133 for (index = 0; index < table_size(); index++) {
134 for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
135 probe = *p;
136 Klass* e = probe->klass();
137 ClassLoaderData* loader_data = probe->loader_data();
139 InstanceKlass* ik = InstanceKlass::cast(e);
141 // Non-unloadable classes were handled in always_strong_oops_do
142 if (!is_strongly_reachable(loader_data, e)) {
143 // Entry was not visited in phase1 (negated test from phase1)
144 assert(!loader_data->is_the_null_class_loader_data(), "unloading entry with null class loader");
145 ClassLoaderData* k_def_class_loader_data = ik->class_loader_data();
147 // Do we need to delete this system dictionary entry?
148 bool purge_entry = false;
150 // Do we need to delete this system dictionary entry?
151 if (loader_data->is_unloading()) {
152 // If the loader is not live this entry should always be
153 // removed (will never be looked up again). Note that this is
154 // not the same as unloading the referred class.
155 if (k_def_class_loader_data == loader_data) {
156 // This is the defining entry, so the referred class is about
157 // to be unloaded.
158 class_was_unloaded = true;
159 }
160 // Also remove this system dictionary entry.
161 purge_entry = true;
163 } else {
164 // The loader in this entry is alive. If the klass is dead,
165 // (determined by checking the defining class loader)
166 // the loader must be an initiating loader (rather than the
167 // defining loader). Remove this entry.
168 if (k_def_class_loader_data->is_unloading()) {
169 // If we get here, the class_loader_data must not be the defining
170 // loader, it must be an initiating one.
171 assert(k_def_class_loader_data != loader_data,
172 "cannot have live defining loader and unreachable klass");
173 // Loader is live, but class and its defining loader are dead.
174 // Remove the entry. The class is going away.
175 purge_entry = true;
176 }
177 }
179 if (purge_entry) {
180 *p = probe->next();
181 if (probe == _current_class_entry) {
182 _current_class_entry = NULL;
183 }
184 free_entry(probe);
185 continue;
186 }
187 }
188 p = probe->next_addr();
189 }
190 }
191 return class_was_unloaded;
192 }
195 void Dictionary::always_strong_oops_do(OopClosure* blk) {
196 // Follow all system classes and temporary placeholders in dictionary
197 for (int index = 0; index < table_size(); index++) {
198 for (DictionaryEntry *probe = bucket(index);
199 probe != NULL;
200 probe = probe->next()) {
201 Klass* e = probe->klass();
202 ClassLoaderData* loader_data = probe->loader_data();
203 if (is_strongly_reachable(loader_data, e)) {
204 probe->protection_domain_set_oops_do(blk);
205 }
206 }
207 }
208 }
211 void Dictionary::always_strong_classes_do(KlassClosure* closure) {
212 // Follow all system classes and temporary placeholders in dictionary
213 for (int index = 0; index < table_size(); index++) {
214 for (DictionaryEntry* probe = bucket(index);
215 probe != NULL;
216 probe = probe->next()) {
217 Klass* e = probe->klass();
218 ClassLoaderData* loader_data = probe->loader_data();
219 if (is_strongly_reachable(loader_data, e)) {
220 closure->do_klass(e);
221 }
222 }
223 }
224 }
227 // Just the classes from defining class loaders
228 void Dictionary::classes_do(void f(Klass*)) {
229 for (int index = 0; index < table_size(); index++) {
230 for (DictionaryEntry* probe = bucket(index);
231 probe != NULL;
232 probe = probe->next()) {
233 Klass* k = probe->klass();
234 if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) {
235 f(k);
236 }
237 }
238 }
239 }
241 // Added for initialize_itable_for_klass to handle exceptions
242 // Just the classes from defining class loaders
243 void Dictionary::classes_do(void f(Klass*, TRAPS), TRAPS) {
244 for (int index = 0; index < table_size(); index++) {
245 for (DictionaryEntry* probe = bucket(index);
246 probe != NULL;
247 probe = probe->next()) {
248 Klass* k = probe->klass();
249 if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) {
250 f(k, CHECK);
251 }
252 }
253 }
254 }
257 // All classes, and their class loaders
258 // (added for helpers that use HandleMarks and ResourceMarks)
259 // Don't iterate over placeholders
260 void Dictionary::classes_do(void f(Klass*, ClassLoaderData*, TRAPS), TRAPS) {
261 for (int index = 0; index < table_size(); index++) {
262 for (DictionaryEntry* probe = bucket(index);
263 probe != NULL;
264 probe = probe->next()) {
265 Klass* k = probe->klass();
266 f(k, probe->loader_data(), CHECK);
267 }
268 }
269 }
272 // All classes, and their class loaders
273 // Don't iterate over placeholders
274 void Dictionary::classes_do(void f(Klass*, ClassLoaderData*)) {
275 for (int index = 0; index < table_size(); index++) {
276 for (DictionaryEntry* probe = bucket(index);
277 probe != NULL;
278 probe = probe->next()) {
279 Klass* k = probe->klass();
280 f(k, probe->loader_data());
281 }
282 }
283 }
286 void Dictionary::oops_do(OopClosure* f) {
287 for (int index = 0; index < table_size(); index++) {
288 for (DictionaryEntry* probe = bucket(index);
289 probe != NULL;
290 probe = probe->next()) {
291 probe->protection_domain_set_oops_do(f);
292 }
293 }
294 }
297 void Dictionary::methods_do(void f(Method*)) {
298 for (int index = 0; index < table_size(); index++) {
299 for (DictionaryEntry* probe = bucket(index);
300 probe != NULL;
301 probe = probe->next()) {
302 Klass* k = probe->klass();
303 if (probe->loader_data() == InstanceKlass::cast(k)->class_loader_data()) {
304 // only take klass is we have the entry with the defining class loader
305 InstanceKlass::cast(k)->methods_do(f);
306 }
307 }
308 }
309 }
312 Klass* Dictionary::try_get_next_class() {
313 while (true) {
314 if (_current_class_entry != NULL) {
315 Klass* k = _current_class_entry->klass();
316 _current_class_entry = _current_class_entry->next();
317 return k;
318 }
319 _current_class_index = (_current_class_index + 1) % table_size();
320 _current_class_entry = bucket(_current_class_index);
321 }
322 // never reached
323 }
326 // Add a loaded class to the system dictionary.
327 // Readers of the SystemDictionary aren't always locked, so _buckets
328 // is volatile. The store of the next field in the constructor is
329 // also cast to volatile; we do this to ensure store order is maintained
330 // by the compilers.
332 void Dictionary::add_klass(Symbol* class_name, ClassLoaderData* loader_data,
333 KlassHandle obj) {
334 assert_locked_or_safepoint(SystemDictionary_lock);
335 assert(obj() != NULL, "adding NULL obj");
336 assert(obj()->name() == class_name, "sanity check on name");
337 assert(loader_data != NULL, "Must be non-NULL");
339 unsigned int hash = compute_hash(class_name, loader_data);
340 int index = hash_to_index(hash);
341 DictionaryEntry* entry = new_entry(hash, obj(), loader_data);
342 add_entry(index, entry);
343 }
346 // This routine does not lock the system dictionary.
347 //
348 // Since readers don't hold a lock, we must make sure that system
349 // dictionary entries are only removed at a safepoint (when only one
350 // thread is running), and are added to in a safe way (all links must
351 // be updated in an MT-safe manner).
352 //
353 // Callers should be aware that an entry could be added just after
354 // _buckets[index] is read here, so the caller will not see the new entry.
355 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash,
356 Symbol* class_name,
357 ClassLoaderData* loader_data) {
358 debug_only(_lookup_count++);
359 for (DictionaryEntry* entry = bucket(index);
360 entry != NULL;
361 entry = entry->next()) {
362 if (entry->hash() == hash && entry->equals(class_name, loader_data)) {
363 return entry;
364 }
365 debug_only(_lookup_length++);
366 }
367 return NULL;
368 }
371 Klass* Dictionary::find(int index, unsigned int hash, Symbol* name,
372 ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
373 DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
374 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) {
375 return entry->klass();
376 } else {
377 return NULL;
378 }
379 }
382 Klass* Dictionary::find_class(int index, unsigned int hash,
383 Symbol* name, ClassLoaderData* loader_data) {
384 assert_locked_or_safepoint(SystemDictionary_lock);
385 assert (index == index_for(name, loader_data), "incorrect index?");
387 DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
388 return (entry != NULL) ? entry->klass() : (Klass*)NULL;
389 }
392 // Variant of find_class for shared classes. No locking required, as
393 // that table is static.
395 Klass* Dictionary::find_shared_class(int index, unsigned int hash,
396 Symbol* name) {
397 assert (index == index_for(name, NULL), "incorrect index?");
399 DictionaryEntry* entry = get_entry(index, hash, name, NULL);
400 return (entry != NULL) ? entry->klass() : (Klass*)NULL;
401 }
404 void Dictionary::add_protection_domain(int index, unsigned int hash,
405 instanceKlassHandle klass,
406 ClassLoaderData* loader_data, Handle protection_domain,
407 TRAPS) {
408 Symbol* klass_name = klass->name();
409 DictionaryEntry* entry = get_entry(index, hash, klass_name, loader_data);
411 assert(entry != NULL,"entry must be present, we just created it");
412 assert(protection_domain() != NULL,
413 "real protection domain should be present");
415 entry->add_protection_domain(protection_domain());
417 assert(entry->contains_protection_domain(protection_domain()),
418 "now protection domain should be present");
419 }
422 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash,
423 Symbol* name,
424 ClassLoaderData* loader_data,
425 Handle protection_domain) {
426 DictionaryEntry* entry = get_entry(index, hash, name, loader_data);
427 return entry->is_valid_protection_domain(protection_domain);
428 }
431 void Dictionary::reorder_dictionary() {
433 // Copy all the dictionary entries into a single master list.
435 DictionaryEntry* master_list = NULL;
436 for (int i = 0; i < table_size(); ++i) {
437 DictionaryEntry* p = bucket(i);
438 while (p != NULL) {
439 DictionaryEntry* tmp;
440 tmp = p->next();
441 p->set_next(master_list);
442 master_list = p;
443 p = tmp;
444 }
445 set_entry(i, NULL);
446 }
448 // Add the dictionary entries back to the list in the correct buckets.
449 while (master_list != NULL) {
450 DictionaryEntry* p = master_list;
451 master_list = master_list->next();
452 p->set_next(NULL);
453 Symbol* class_name = InstanceKlass::cast((Klass*)(p->klass()))->name();
454 // Since the null class loader data isn't copied to the CDS archive,
455 // compute the hash with NULL for loader data.
456 unsigned int hash = compute_hash(class_name, NULL);
457 int index = hash_to_index(hash);
458 p->set_hash(hash);
459 p->set_loader_data(NULL); // loader_data isn't copied to CDS
460 p->set_next(bucket(index));
461 set_entry(index, p);
462 }
463 }
465 SymbolPropertyTable::SymbolPropertyTable(int table_size)
466 : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry))
467 {
468 }
469 SymbolPropertyTable::SymbolPropertyTable(int table_size, HashtableBucket<mtSymbol>* t,
470 int number_of_entries)
471 : Hashtable<Symbol*, mtSymbol>(table_size, sizeof(SymbolPropertyEntry), t, number_of_entries)
472 {
473 }
476 SymbolPropertyEntry* SymbolPropertyTable::find_entry(int index, unsigned int hash,
477 Symbol* sym,
478 intptr_t sym_mode) {
479 assert(index == index_for(sym, sym_mode), "incorrect index?");
480 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
481 if (p->hash() == hash && p->symbol() == sym && p->symbol_mode() == sym_mode) {
482 return p;
483 }
484 }
485 return NULL;
486 }
489 SymbolPropertyEntry* SymbolPropertyTable::add_entry(int index, unsigned int hash,
490 Symbol* sym, intptr_t sym_mode) {
491 assert_locked_or_safepoint(SystemDictionary_lock);
492 assert(index == index_for(sym, sym_mode), "incorrect index?");
493 assert(find_entry(index, hash, sym, sym_mode) == NULL, "no double entry");
495 SymbolPropertyEntry* p = new_entry(hash, sym, sym_mode);
496 Hashtable<Symbol*, mtSymbol>::add_entry(index, p);
497 return p;
498 }
500 void SymbolPropertyTable::oops_do(OopClosure* f) {
501 for (int index = 0; index < table_size(); index++) {
502 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
503 if (p->method_type() != NULL) {
504 f->do_oop(p->method_type_addr());
505 }
506 }
507 }
508 }
510 void SymbolPropertyTable::methods_do(void f(Method*)) {
511 for (int index = 0; index < table_size(); index++) {
512 for (SymbolPropertyEntry* p = bucket(index); p != NULL; p = p->next()) {
513 Method* prop = p->method();
514 if (prop != NULL) {
515 f((Method*)prop);
516 }
517 }
518 }
519 }
522 // ----------------------------------------------------------------------------
523 #ifndef PRODUCT
525 void Dictionary::print() {
526 ResourceMark rm;
527 HandleMark hm;
529 tty->print_cr("Java system dictionary (table_size=%d, classes=%d)",
530 table_size(), number_of_entries());
531 tty->print_cr("^ indicates that initiating loader is different from "
532 "defining loader");
534 for (int index = 0; index < table_size(); index++) {
535 for (DictionaryEntry* probe = bucket(index);
536 probe != NULL;
537 probe = probe->next()) {
538 if (Verbose) tty->print("%4d: ", index);
539 Klass* e = probe->klass();
540 ClassLoaderData* loader_data = probe->loader_data();
541 bool is_defining_class =
542 (loader_data == InstanceKlass::cast(e)->class_loader_data());
543 tty->print("%s%s", is_defining_class ? " " : "^",
544 e->external_name());
546 tty->print(", loader ");
547 loader_data->print_value();
548 tty->cr();
549 }
550 }
551 }
553 #endif
556 void Dictionary::verify() {
557 guarantee(number_of_entries() >= 0, "Verify of system dictionary failed");
559 int element_count = 0;
560 for (int index = 0; index < table_size(); index++) {
561 for (DictionaryEntry* probe = bucket(index);
562 probe != NULL;
563 probe = probe->next()) {
564 Klass* e = probe->klass();
565 ClassLoaderData* loader_data = probe->loader_data();
566 guarantee(e->oop_is_instance(),
567 "Verify of system dictionary failed");
568 // class loader must be present; a null class loader is the
569 // boostrap loader
570 guarantee(loader_data != NULL || DumpSharedSpaces ||
571 loader_data->class_loader() == NULL ||
572 loader_data->class_loader()->is_instance(),
573 "checking type of class_loader");
574 e->verify();
575 probe->verify_protection_domain_set();
576 element_count++;
577 }
578 }
579 guarantee(number_of_entries() == element_count,
580 "Verify of system dictionary failed");
581 debug_only(verify_lookup_length((double)number_of_entries() / table_size()));
582 }