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 #include "precompiled.hpp"
26 #include "classfile/classLoaderData.inline.hpp"
27 #include "classfile/sharedClassUtil.hpp"
28 #include "classfile/dictionary.hpp"
29 #include "classfile/protectionDomainCache.hpp"
30 #include "classfile/systemDictionary.hpp"
31 #include "classfile/systemDictionaryShared.hpp"
32 #include "logging/log.hpp"
33 #include "logging/logStream.hpp"
34 #include "memory/iterator.hpp"
35 #include "memory/metaspaceClosure.hpp"
36 #include "memory/resourceArea.hpp"
37 #include "oops/oop.inline.hpp"
38 #include "runtime/atomic.hpp"
39 #include "runtime/orderAccess.inline.hpp"
40 #include "utilities/hashtable.inline.hpp"
41
42 size_t Dictionary::entry_size() {
43 if (DumpSharedSpaces) {
44 return SystemDictionaryShared::dictionary_entry_size();
45 } else {
46 return sizeof(DictionaryEntry);
47 }
48 }
49
50 Dictionary::Dictionary(ClassLoaderData* loader_data, int table_size)
51 : _loader_data(loader_data), Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size()) {
52 };
53
54
55 Dictionary::Dictionary(ClassLoaderData* loader_data,
56 int table_size, HashtableBucket<mtClass>* t,
57 int number_of_entries)
58 : _loader_data(loader_data), Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size(), t, number_of_entries) {
59 };
60
61 Dictionary::~Dictionary() {
62 DictionaryEntry* probe = NULL;
63 for (int index = 0; index < table_size(); index++) {
64 for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
65 probe = *p;
66 *p = probe->next();
67 free_entry(probe);
68 }
69 }
70 assert(number_of_entries() == 0, "should have removed all entries");
71 assert(new_entry_free_list() == NULL, "entry present on Dictionary's free list");
72 free_buckets();
73 }
74
75 DictionaryEntry* Dictionary::new_entry(unsigned int hash, InstanceKlass* klass) {
76 DictionaryEntry* entry = (DictionaryEntry*)Hashtable<InstanceKlass*, mtClass>::allocate_new_entry(hash, klass);
77 entry->set_pd_set(NULL);
78 assert(klass->is_instance_klass(), "Must be");
79 if (DumpSharedSpaces) {
80 SystemDictionaryShared::init_shared_dictionary_entry(klass, entry);
81 }
82 return entry;
83 }
84
85
86 void Dictionary::free_entry(DictionaryEntry* entry) {
87 // avoid recursion when deleting linked list
88 // pd_set is accessed during a safepoint.
89 while (entry->pd_set() != NULL) {
90 ProtectionDomainEntry* to_delete = entry->pd_set();
91 entry->set_pd_set(to_delete->next());
92 delete to_delete;
93 }
94 // Unlink from the Hashtable prior to freeing
95 unlink_entry(entry);
96 FREE_C_HEAP_ARRAY(char, entry);
97 }
98
99
100 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const {
101 #ifdef ASSERT
102 if (protection_domain == instance_klass()->protection_domain()) {
103 // Ensure this doesn't show up in the pd_set (invariant)
104 bool in_pd_set = false;
105 for (ProtectionDomainEntry* current = pd_set_acquire();
106 current != NULL;
107 current = current->next()) {
108 if (current->protection_domain() == protection_domain) {
109 in_pd_set = true;
110 break;
111 }
112 }
113 if (in_pd_set) {
114 assert(false, "A klass's protection domain should not show up "
115 "in its sys. dict. PD set");
116 }
117 }
118 #endif /* ASSERT */
247 void Dictionary::classes_do(MetaspaceClosure* it) {
248 assert(DumpSharedSpaces, "dump-time only");
249 for (int index = 0; index < table_size(); index++) {
250 for (DictionaryEntry* probe = bucket(index);
251 probe != NULL;
252 probe = probe->next()) {
253 it->push(probe->klass_addr());
254 ((SharedDictionaryEntry*)probe)->metaspace_pointers_do(it);
255 }
256 }
257 }
258
259
260
261 // Add a loaded class to the dictionary.
262 // Readers of the SystemDictionary aren't always locked, so _buckets
263 // 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
265 // by the compilers.
266
267 void Dictionary::add_klass(int index, unsigned int hash, Symbol* class_name,
268 InstanceKlass* obj) {
269 assert_locked_or_safepoint(SystemDictionary_lock);
270 assert(obj != NULL, "adding NULL obj");
271 assert(obj->name() == class_name, "sanity check on name");
272
273 DictionaryEntry* entry = new_entry(hash, obj);
274 add_entry(index, entry);
275 }
276
277
278 // This routine does not lock the dictionary.
279 //
280 // Since readers don't hold a lock, we must make sure that system
281 // dictionary entries are only removed at a safepoint (when only one
282 // thread is running), and are added to in a safe way (all links must
283 // be updated in an MT-safe manner).
284 //
285 // Callers should be aware that an entry could be added just after
286 // _buckets[index] is read here, so the caller will not see the new entry.
287 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash,
288 Symbol* class_name) {
289 for (DictionaryEntry* entry = bucket(index);
290 entry != NULL;
291 entry = entry->next()) {
292 if (entry->hash() == hash && entry->equals(class_name)) {
293 if (!DumpSharedSpaces || SystemDictionaryShared::is_builtin(entry)) {
294 return entry;
295 }
296 }
297 }
298 return NULL;
299 }
300
301
302 InstanceKlass* Dictionary::find(int index, unsigned int hash, Symbol* name,
303 Handle protection_domain) {
304 DictionaryEntry* entry = get_entry(index, hash, name);
305 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) {
306 return entry->instance_klass();
307 } else {
308 return NULL;
309 }
310 }
311
312
313 InstanceKlass* Dictionary::find_class(int index, unsigned int hash,
314 Symbol* name) {
315 assert_locked_or_safepoint(SystemDictionary_lock);
316 assert (index == index_for(name), "incorrect index?");
317
318 DictionaryEntry* entry = get_entry(index, hash, name);
319 return (entry != NULL) ? entry->instance_klass() : NULL;
320 }
321
322
323 // Variant of find_class for shared classes. No locking required, as
333
334
335 void Dictionary::add_protection_domain(int index, unsigned int hash,
336 InstanceKlass* klass,
337 Handle protection_domain,
338 TRAPS) {
339 Symbol* klass_name = klass->name();
340 DictionaryEntry* entry = get_entry(index, hash, klass_name);
341
342 assert(entry != NULL,"entry must be present, we just created it");
343 assert(protection_domain() != NULL,
344 "real protection domain should be present");
345
346 entry->add_protection_domain(this, protection_domain);
347
348 assert(entry->contains_protection_domain(protection_domain()),
349 "now protection domain should be present");
350 }
351
352
353 bool Dictionary::is_valid_protection_domain(int index, unsigned int hash,
354 Symbol* name,
355 Handle protection_domain) {
356 DictionaryEntry* entry = get_entry(index, hash, name);
357 return entry->is_valid_protection_domain(protection_domain);
358 }
359
360
361 void Dictionary::reorder_dictionary_for_sharing() {
362
363 // Copy all the dictionary entries into a single master list.
364
365 DictionaryEntry* master_list = NULL;
366 for (int i = 0; i < table_size(); ++i) {
367 DictionaryEntry* p = bucket(i);
368 while (p != NULL) {
369 DictionaryEntry* next = p->next();
370 InstanceKlass*ik = p->instance_klass();
371 // we cannot include signed classes in the archive because the certificates
372 // used during dump time may be different than those used during
373 // runtime (due to expiration, etc).
374 if (ik->signers() != NULL) {
375 ResourceMark rm;
|
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 #include "precompiled.hpp"
26 #include "classfile/classLoaderData.inline.hpp"
27 #include "classfile/sharedClassUtil.hpp"
28 #include "classfile/dictionary.hpp"
29 #include "classfile/protectionDomainCache.hpp"
30 #include "classfile/systemDictionary.hpp"
31 #include "classfile/systemDictionaryShared.hpp"
32 #include "gc/shared/gcLocker.hpp"
33 #include "logging/log.hpp"
34 #include "logging/logStream.hpp"
35 #include "memory/iterator.hpp"
36 #include "memory/metaspaceClosure.hpp"
37 #include "memory/resourceArea.hpp"
38 #include "oops/oop.inline.hpp"
39 #include "runtime/atomic.hpp"
40 #include "runtime/orderAccess.inline.hpp"
41 #include "utilities/hashtable.inline.hpp"
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
48 size_t Dictionary::entry_size() {
49 if (DumpSharedSpaces) {
50 return SystemDictionaryShared::dictionary_entry_size();
51 } else {
52 return sizeof(DictionaryEntry);
53 }
54 }
55
56 Dictionary::Dictionary(ClassLoaderData* loader_data, int table_size, bool resizable)
57 : _loader_data(loader_data), _resizable(resizable), _needs_resizing(false),
58 Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size()) {
59 };
60
61
62 Dictionary::Dictionary(ClassLoaderData* loader_data,
63 int table_size, HashtableBucket<mtClass>* t,
64 int number_of_entries, bool resizable)
65 : _loader_data(loader_data), _resizable(resizable), _needs_resizing(false),
66 Hashtable<InstanceKlass*, mtClass>(table_size, (int)entry_size(), t, number_of_entries) {
67 };
68
69 Dictionary::~Dictionary() {
70 DictionaryEntry* probe = NULL;
71 for (int index = 0; index < table_size(); index++) {
72 for (DictionaryEntry** p = bucket_addr(index); *p != NULL; ) {
73 probe = *p;
74 *p = probe->next();
75 free_entry(probe);
76 }
77 }
78 assert(number_of_entries() == 0, "should have removed all entries");
79 assert(new_entry_free_list() == NULL, "entry present on Dictionary's free list");
80 free_buckets();
81 }
82
83 DictionaryEntry* Dictionary::new_entry(unsigned int hash, InstanceKlass* klass) {
84 DictionaryEntry* entry = (DictionaryEntry*)Hashtable<InstanceKlass*, mtClass>::allocate_new_entry(hash, klass);
85 entry->set_pd_set(NULL);
86 assert(klass->is_instance_klass(), "Must be");
87 if (DumpSharedSpaces) {
88 SystemDictionaryShared::init_shared_dictionary_entry(klass, entry);
89 }
90 return entry;
91 }
92
93
94 void Dictionary::free_entry(DictionaryEntry* entry) {
95 // avoid recursion when deleting linked list
96 // pd_set is accessed during a safepoint.
97 while (entry->pd_set() != NULL) {
98 ProtectionDomainEntry* to_delete = entry->pd_set();
99 entry->set_pd_set(to_delete->next());
100 delete to_delete;
101 }
102 // Unlink from the Hashtable prior to freeing
103 unlink_entry(entry);
104 FREE_C_HEAP_ARRAY(char, entry);
105 }
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 }
161
162 bool DictionaryEntry::contains_protection_domain(oop protection_domain) const {
163 #ifdef ASSERT
164 if (protection_domain == instance_klass()->protection_domain()) {
165 // Ensure this doesn't show up in the pd_set (invariant)
166 bool in_pd_set = false;
167 for (ProtectionDomainEntry* current = pd_set_acquire();
168 current != NULL;
169 current = current->next()) {
170 if (current->protection_domain() == protection_domain) {
171 in_pd_set = true;
172 break;
173 }
174 }
175 if (in_pd_set) {
176 assert(false, "A klass's protection domain should not show up "
177 "in its sys. dict. PD set");
178 }
179 }
180 #endif /* ASSERT */
309 void Dictionary::classes_do(MetaspaceClosure* it) {
310 assert(DumpSharedSpaces, "dump-time only");
311 for (int index = 0; index < table_size(); index++) {
312 for (DictionaryEntry* probe = bucket(index);
313 probe != NULL;
314 probe = probe->next()) {
315 it->push(probe->klass_addr());
316 ((SharedDictionaryEntry*)probe)->metaspace_pointers_do(it);
317 }
318 }
319 }
320
321
322
323 // Add a loaded class to the dictionary.
324 // Readers of the SystemDictionary aren't always locked, so _buckets
325 // is volatile. The store of the next field in the constructor is
326 // also cast to volatile; we do this to ensure store order is maintained
327 // by the compilers.
328
329 void Dictionary::add_klass(unsigned int hash, Symbol* class_name,
330 InstanceKlass* obj) {
331 assert_locked_or_safepoint(SystemDictionary_lock);
332 assert(obj != NULL, "adding NULL obj");
333 assert(obj->name() == class_name, "sanity check on name");
334
335 DictionaryEntry* entry = new_entry(hash, obj);
336 int index = hash_to_index(hash);
337 add_entry(index, entry);
338 check_if_needs_resize();
339 }
340
341
342 // This routine does not lock the dictionary.
343 //
344 // Since readers don't hold a lock, we must make sure that system
345 // dictionary entries are only removed at a safepoint (when only one
346 // thread is running), and are added to in a safe way (all links must
347 // be updated in an MT-safe manner).
348 //
349 // Callers should be aware that an entry could be added just after
350 // _buckets[index] is read here, so the caller will not see the new entry.
351 DictionaryEntry* Dictionary::get_entry(int index, unsigned int hash,
352 Symbol* class_name) {
353 for (DictionaryEntry* entry = bucket(index);
354 entry != NULL;
355 entry = entry->next()) {
356 if (entry->hash() == hash && entry->equals(class_name)) {
357 if (!DumpSharedSpaces || SystemDictionaryShared::is_builtin(entry)) {
358 return entry;
359 }
360 }
361 }
362 return NULL;
363 }
364
365
366 InstanceKlass* Dictionary::find(unsigned int hash, Symbol* name,
367 Handle protection_domain) {
368 NoSafepointVerifier nsv;
369
370 int index = hash_to_index(hash);
371 DictionaryEntry* entry = get_entry(index, hash, name);
372 if (entry != NULL && entry->is_valid_protection_domain(protection_domain)) {
373 return entry->instance_klass();
374 } else {
375 return NULL;
376 }
377 }
378
379
380 InstanceKlass* Dictionary::find_class(int index, unsigned int hash,
381 Symbol* name) {
382 assert_locked_or_safepoint(SystemDictionary_lock);
383 assert (index == index_for(name), "incorrect index?");
384
385 DictionaryEntry* entry = get_entry(index, hash, name);
386 return (entry != NULL) ? entry->instance_klass() : NULL;
387 }
388
389
390 // Variant of find_class for shared classes. No locking required, as
400
401
402 void Dictionary::add_protection_domain(int index, unsigned int hash,
403 InstanceKlass* klass,
404 Handle protection_domain,
405 TRAPS) {
406 Symbol* klass_name = klass->name();
407 DictionaryEntry* entry = get_entry(index, hash, klass_name);
408
409 assert(entry != NULL,"entry must be present, we just created it");
410 assert(protection_domain() != NULL,
411 "real protection domain should be present");
412
413 entry->add_protection_domain(this, protection_domain);
414
415 assert(entry->contains_protection_domain(protection_domain()),
416 "now protection domain should be present");
417 }
418
419
420 bool Dictionary::is_valid_protection_domain(unsigned int hash,
421 Symbol* name,
422 Handle protection_domain) {
423 int index = hash_to_index(hash);
424 DictionaryEntry* entry = get_entry(index, hash, name);
425 return entry->is_valid_protection_domain(protection_domain);
426 }
427
428
429 void Dictionary::reorder_dictionary_for_sharing() {
430
431 // Copy all the dictionary entries into a single master list.
432
433 DictionaryEntry* master_list = NULL;
434 for (int i = 0; i < table_size(); ++i) {
435 DictionaryEntry* p = bucket(i);
436 while (p != NULL) {
437 DictionaryEntry* next = p->next();
438 InstanceKlass*ik = p->instance_klass();
439 // we cannot include signed classes in the archive because the certificates
440 // used during dump time may be different than those used during
441 // runtime (due to expiration, etc).
442 if (ik->signers() != NULL) {
443 ResourceMark rm;
|