27 #include "classfile/sharedClassUtil.hpp" |
27 #include "classfile/sharedClassUtil.hpp" |
28 #include "classfile/dictionary.hpp" |
28 #include "classfile/dictionary.hpp" |
29 #include "classfile/protectionDomainCache.hpp" |
29 #include "classfile/protectionDomainCache.hpp" |
30 #include "classfile/systemDictionary.hpp" |
30 #include "classfile/systemDictionary.hpp" |
31 #include "classfile/systemDictionaryShared.hpp" |
31 #include "classfile/systemDictionaryShared.hpp" |
|
32 #include "gc/shared/gcLocker.hpp" |
32 #include "logging/log.hpp" |
33 #include "logging/log.hpp" |
33 #include "logging/logStream.hpp" |
34 #include "logging/logStream.hpp" |
34 #include "memory/iterator.hpp" |
35 #include "memory/iterator.hpp" |
35 #include "memory/metaspaceClosure.hpp" |
36 #include "memory/metaspaceClosure.hpp" |
36 #include "memory/resourceArea.hpp" |
37 #include "memory/resourceArea.hpp" |
37 #include "oops/oop.inline.hpp" |
38 #include "oops/oop.inline.hpp" |
38 #include "runtime/atomic.hpp" |
39 #include "runtime/atomic.hpp" |
39 #include "runtime/orderAccess.inline.hpp" |
40 #include "runtime/orderAccess.inline.hpp" |
40 #include "utilities/hashtable.inline.hpp" |
41 #include "utilities/hashtable.inline.hpp" |
41 |
42 |
|
43 // Optimization: if any dictionary needs resizing, we set this flag, |
|
44 // so that we dont't have to walk all dictionaries to check if any actually |
|
45 // needs resizing, which is costly to do at Safepoint. |
|
46 bool Dictionary::_some_dictionary_needs_resizing = false; |
|
47 |
42 size_t Dictionary::entry_size() { |
48 size_t Dictionary::entry_size() { |
43 if (DumpSharedSpaces) { |
49 if (DumpSharedSpaces) { |
44 return SystemDictionaryShared::dictionary_entry_size(); |
50 return SystemDictionaryShared::dictionary_entry_size(); |
45 } else { |
51 } else { |
46 return sizeof(DictionaryEntry); |
52 return sizeof(DictionaryEntry); |
47 } |
53 } |
48 } |
54 } |
49 |
55 |
50 Dictionary::Dictionary(ClassLoaderData* loader_data, int table_size) |
56 Dictionary::Dictionary(ClassLoaderData* loader_data, int table_size, bool resizable) |
51 : _loader_data(loader_data), Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size()) { |
57 : _loader_data(loader_data), _resizable(resizable), _needs_resizing(false), |
|
58 Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size()) { |
52 }; |
59 }; |
53 |
60 |
54 |
61 |
55 Dictionary::Dictionary(ClassLoaderData* loader_data, |
62 Dictionary::Dictionary(ClassLoaderData* loader_data, |
56 int table_size, HashtableBucket<mtClass>* t, |
63 int table_size, HashtableBucket<mtClass>* t, |
57 int number_of_entries) |
64 int number_of_entries, bool resizable) |
58 : _loader_data(loader_data), Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size(), t, number_of_entries) { |
65 : _loader_data(loader_data), _resizable(resizable), _needs_resizing(false), |
|
66 Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size(), t, number_of_entries) { |
59 }; |
67 }; |
60 |
68 |
61 Dictionary::~Dictionary() { |
69 Dictionary::~Dictionary() { |
62 DictionaryEntry* probe = NULL; |
70 DictionaryEntry* probe = NULL; |
63 for (int index = 0; index < table_size(); index++) { |
71 for (int index = 0; index < table_size(); index++) { |
94 // Unlink from the Hashtable prior to freeing |
102 // Unlink from the Hashtable prior to freeing |
95 unlink_entry(entry); |
103 unlink_entry(entry); |
96 FREE_C_HEAP_ARRAY(char, entry); |
104 FREE_C_HEAP_ARRAY(char, entry); |
97 } |
105 } |
98 |
106 |
|
107 const int _resize_load_trigger = 5; // load factor that will trigger the resize |
|
108 const double _resize_factor = 2.0; // by how much we will resize using current number of entries |
|
109 const int _resize_max_size = 40423; // the max dictionary size allowed |
|
110 const int _primelist[] = {107, 1009, 2017, 4049, 5051, 10103, 20201, _resize_max_size}; |
|
111 const int _prime_array_size = sizeof(_primelist)/sizeof(int); |
|
112 |
|
113 // Calculate next "good" dictionary size based on requested count |
|
114 static int calculate_dictionary_size(int requested) { |
|
115 int newsize = _primelist[0]; |
|
116 int index = 0; |
|
117 for (newsize = _primelist[index]; index < (_prime_array_size - 1); |
|
118 newsize = _primelist[++index]) { |
|
119 if (requested <= newsize) { |
|
120 break; |
|
121 } |
|
122 } |
|
123 return newsize; |
|
124 } |
|
125 |
|
126 bool Dictionary::does_any_dictionary_needs_resizing() { |
|
127 return Dictionary::_some_dictionary_needs_resizing; |
|
128 } |
|
129 |
|
130 void Dictionary::check_if_needs_resize() { |
|
131 if (_resizable == true) { |
|
132 if (number_of_entries() > (_resize_load_trigger*table_size())) { |
|
133 _needs_resizing = true; |
|
134 Dictionary::_some_dictionary_needs_resizing = true; |
|
135 } |
|
136 } |
|
137 } |
|
138 |
|
139 bool Dictionary::resize_if_needed() { |
|
140 int desired_size = 0; |
|
141 if (_needs_resizing == true) { |
|
142 desired_size = calculate_dictionary_size((int)(_resize_factor*number_of_entries())); |
|
143 if (desired_size >= _resize_max_size) { |
|
144 desired_size = _resize_max_size; |
|
145 // We have reached the limit, turn resizing off |
|
146 _resizable = false; |
|
147 } |
|
148 if ((desired_size != 0) && (desired_size != table_size())) { |
|
149 if (!resize(desired_size)) { |
|
150 // Something went wrong, turn resizing off |
|
151 _resizable = false; |
|
152 } |
|
153 } |
|
154 } |
|
155 |
|
156 _needs_resizing = false; |
|
157 Dictionary::_some_dictionary_needs_resizing = false; |
|
158 |
|
159 return (desired_size != 0); |
|
160 } |
99 |
161 |
100 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const { |
162 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const { |
101 #ifdef ASSERT |
163 #ifdef ASSERT |
102 if (protection_domain == instance_klass()->protection_domain()) { |
164 if (protection_domain == instance_klass()->protection_domain()) { |
103 // Ensure this doesn't show up in the pd_set (invariant) |
165 // Ensure this doesn't show up in the pd_set (invariant) |
262 // Readers of the SystemDictionary aren't always locked, so _buckets |
324 // Readers of the SystemDictionary aren't always locked, so _buckets |
263 // is volatile. The store of the next field in the constructor is |
325 // is volatile. The store of the next field in the constructor is |
264 // also cast to volatile; we do this to ensure store order is maintained |
326 // also cast to volatile; we do this to ensure store order is maintained |
265 // by the compilers. |
327 // by the compilers. |
266 |
328 |
267 void Dictionary::add_klass(int index, unsigned int hash, Symbol* class_name, |
329 void Dictionary::add_klass(unsigned int hash, Symbol* class_name, |
268 InstanceKlass* obj) { |
330 InstanceKlass* obj) { |
269 assert_locked_or_safepoint(SystemDictionary_lock); |
331 assert_locked_or_safepoint(SystemDictionary_lock); |
270 assert(obj != NULL, "adding NULL obj"); |
332 assert(obj != NULL, "adding NULL obj"); |
271 assert(obj->name() == class_name, "sanity check on name"); |
333 assert(obj->name() == class_name, "sanity check on name"); |
272 |
334 |
273 DictionaryEntry* entry = new_entry(hash, obj); |
335 DictionaryEntry* entry = new_entry(hash, obj); |
|
336 int index = hash_to_index(hash); |
274 add_entry(index, entry); |
337 add_entry(index, entry); |
|
338 check_if_needs_resize(); |
275 } |
339 } |
276 |
340 |
277 |
341 |
278 // This routine does not lock the dictionary. |
342 // This routine does not lock the dictionary. |
279 // |
343 // |
297 } |
361 } |
298 return NULL; |
362 return NULL; |
299 } |
363 } |
300 |
364 |
301 |
365 |
302 InstanceKlass* Dictionary::find(int index, unsigned int hash, Symbol* name, |
366 InstanceKlass* Dictionary::find(unsigned int hash, Symbol* name, |
303 Handle protection_domain) { |
367 Handle protection_domain) { |
|
368 NoSafepointVerifier nsv; |
|
369 |
|
370 int index = hash_to_index(hash); |
304 DictionaryEntry* entry = get_entry(index, hash, name); |
371 DictionaryEntry* entry = get_entry(index, hash, name); |
305 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) { |
372 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) { |
306 return entry->instance_klass(); |
373 return entry->instance_klass(); |
307 } else { |
374 } else { |
308 return NULL; |
375 return NULL; |
348 assert(entry->contains_protection_domain(protection_domain()), |
415 assert(entry->contains_protection_domain(protection_domain()), |
349 "now protection domain should be present"); |
416 "now protection domain should be present"); |
350 } |
417 } |
351 |
418 |
352 |
419 |
353 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash, |
420 bool Dictionary::is_valid_protection_domain(unsigned int hash, |
354 Symbol* name, |
421 Symbol* name, |
355 Handle protection_domain) { |
422 Handle protection_domain) { |
|
423 int index = hash_to_index(hash); |
356 DictionaryEntry* entry = get_entry(index, hash, name); |
424 DictionaryEntry* entry = get_entry(index, hash, name); |
357 return entry->is_valid_protection_domain(protection_domain); |
425 return entry->is_valid_protection_domain(protection_domain); |
358 } |
426 } |
359 |
427 |
360 #if INCLUDE_CDS |
428 #if INCLUDE_CDS |