44 Arena* SymbolTable::_arena = NULL; |
44 Arena* SymbolTable::_arena = NULL; |
45 bool SymbolTable::_needs_rehashing = false; |
45 bool SymbolTable::_needs_rehashing = false; |
46 jint SymbolTable::_seed = 0; |
46 jint SymbolTable::_seed = 0; |
47 |
47 |
48 Symbol* SymbolTable::allocate_symbol(const u1* name, int len, bool c_heap, TRAPS) { |
48 Symbol* SymbolTable::allocate_symbol(const u1* name, int len, bool c_heap, TRAPS) { |
49 // Don't allow symbols to be created which cannot fit in a Symbol*. |
49 assert (len <= Symbol::max_length(), "should be checked by caller"); |
50 if (len > Symbol::max_length()) { |
50 |
51 THROW_MSG_0(vmSymbols::java_lang_InternalError(), |
|
52 "name is too long to represent"); |
|
53 } |
|
54 Symbol* sym; |
51 Symbol* sym; |
55 // Allocate symbols in the C heap when dumping shared spaces in case there |
52 // Allocate symbols in the C heap when dumping shared spaces in case there |
56 // are temporary symbols we can remove. |
53 // are temporary symbols we can remove. |
57 if (c_heap || DumpSharedSpaces) { |
54 if (c_heap || DumpSharedSpaces) { |
58 // refcount starts as 1 |
55 // refcount starts as 1 |
93 void SymbolTable::unlink() { |
90 void SymbolTable::unlink() { |
94 int removed = 0; |
91 int removed = 0; |
95 int total = 0; |
92 int total = 0; |
96 size_t memory_total = 0; |
93 size_t memory_total = 0; |
97 for (int i = 0; i < the_table()->table_size(); ++i) { |
94 for (int i = 0; i < the_table()->table_size(); ++i) { |
98 for (HashtableEntry<Symbol*>** p = the_table()->bucket_addr(i); *p != NULL; ) { |
95 HashtableEntry<Symbol*>** p = the_table()->bucket_addr(i); |
99 HashtableEntry<Symbol*>* entry = *p; |
96 HashtableEntry<Symbol*>* entry = the_table()->bucket(i); |
100 if (entry->is_shared()) { |
97 while (entry != NULL) { |
|
98 // Shared entries are normally at the end of the bucket and if we run into |
|
99 // a shared entry, then there is nothing more to remove. However, if we |
|
100 // have rehashed the table, then the shared entries are no longer at the |
|
101 // end of the bucket. |
|
102 if (entry->is_shared() && !use_alternate_hashcode()) { |
101 break; |
103 break; |
102 } |
104 } |
103 Symbol* s = entry->literal(); |
105 Symbol* s = entry->literal(); |
104 memory_total += s->object_size(); |
106 memory_total += s->object_size(); |
105 total++; |
107 total++; |
106 assert(s != NULL, "just checking"); |
108 assert(s != NULL, "just checking"); |
107 // If reference count is zero, remove. |
109 // If reference count is zero, remove. |
108 if (s->refcount() == 0) { |
110 if (s->refcount() == 0) { |
|
111 assert(!entry->is_shared(), "shared entries should be kept live"); |
109 delete s; |
112 delete s; |
110 removed++; |
113 removed++; |
111 *p = entry->next(); |
114 *p = entry->next(); |
112 the_table()->free_entry(entry); |
115 the_table()->free_entry(entry); |
113 } else { |
116 } else { |
114 p = entry->next_addr(); |
117 p = entry->next_addr(); |
115 } |
118 } |
|
119 // get next entry |
|
120 entry = (HashtableEntry<Symbol*>*)HashtableEntry<Symbol*>::make_ptr(*p); |
116 } |
121 } |
117 } |
122 } |
118 symbols_removed += removed; |
123 symbols_removed += removed; |
119 symbols_counted += total; |
124 symbols_counted += total; |
120 // Exclude printing for normal PrintGCDetails because people parse |
125 // Exclude printing for normal PrintGCDetails because people parse |
133 |
138 |
134 // Create a new table and using alternate hash code, populate the new table |
139 // Create a new table and using alternate hash code, populate the new table |
135 // with the existing strings. Set flag to use the alternate hash code afterwards. |
140 // with the existing strings. Set flag to use the alternate hash code afterwards. |
136 void SymbolTable::rehash_table() { |
141 void SymbolTable::rehash_table() { |
137 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
142 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
138 assert(!DumpSharedSpaces, "this should never happen with -Xshare:dump"); |
143 // This should never happen with -Xshare:dump but it might in testing mode. |
|
144 if (DumpSharedSpaces) return; |
139 // Create a new symbol table |
145 // Create a new symbol table |
140 SymbolTable* new_table = new SymbolTable(); |
146 SymbolTable* new_table = new SymbolTable(); |
141 |
147 |
142 // Initialize the global seed for hashing. |
148 // Initialize the global seed for hashing. |
143 _seed = AltHashing::compute_seed(); |
149 _seed = AltHashing::compute_seed(); |
174 _needs_rehashing = check_rehash_table(count); |
180 _needs_rehashing = check_rehash_table(count); |
175 } |
181 } |
176 return NULL; |
182 return NULL; |
177 } |
183 } |
178 |
184 |
179 // Pick hashing algorithm, but return value already given if not using a new |
185 // Pick hashing algorithm. |
180 // hash algorithm. |
186 unsigned int SymbolTable::hash_symbol(const char* s, int len) { |
181 unsigned int SymbolTable::hash_symbol(const char* s, int len, unsigned int hashValue) { |
|
182 return use_alternate_hashcode() ? |
187 return use_alternate_hashcode() ? |
183 AltHashing::murmur3_32(seed(), (const jbyte*)s, len) : |
188 AltHashing::murmur3_32(seed(), (const jbyte*)s, len) : |
184 (hashValue != 0 ? hashValue : java_lang_String::to_hash(s, len)); |
189 java_lang_String::to_hash(s, len); |
185 } |
190 } |
186 |
191 |
187 |
192 |
188 // We take care not to be blocking while holding the |
193 // We take care not to be blocking while holding the |
189 // SymbolTable_lock. Otherwise, the system might deadlock, since the |
194 // SymbolTable_lock. Otherwise, the system might deadlock, since the |
198 |
203 |
199 Symbol* s = the_table()->lookup(index, name, len, hashValue); |
204 Symbol* s = the_table()->lookup(index, name, len, hashValue); |
200 |
205 |
201 // Found |
206 // Found |
202 if (s != NULL) return s; |
207 if (s != NULL) return s; |
|
208 |
|
209 // Grab SymbolTable_lock first. |
|
210 MutexLocker ml(SymbolTable_lock, THREAD); |
203 |
211 |
204 // Otherwise, add to symbol to table |
212 // Otherwise, add to symbol to table |
205 return the_table()->basic_add(index, (u1*)name, len, hashValue, true, CHECK_NULL); |
213 return the_table()->basic_add(index, (u1*)name, len, hashValue, true, CHECK_NULL); |
206 } |
214 } |
207 |
215 |
235 buffer[i] = name[i]; |
243 buffer[i] = name[i]; |
236 } |
244 } |
237 // Make sure there is no safepoint in the code above since name can't move. |
245 // Make sure there is no safepoint in the code above since name can't move. |
238 // We can't include the code in No_Safepoint_Verifier because of the |
246 // We can't include the code in No_Safepoint_Verifier because of the |
239 // ResourceMark. |
247 // ResourceMark. |
|
248 |
|
249 // Grab SymbolTable_lock first. |
|
250 MutexLocker ml(SymbolTable_lock, THREAD); |
240 |
251 |
241 return the_table()->basic_add(index, (u1*)buffer, len, hashValue, true, CHECK_NULL); |
252 return the_table()->basic_add(index, (u1*)buffer, len, hashValue, true, CHECK_NULL); |
242 } |
253 } |
243 |
254 |
244 Symbol* SymbolTable::lookup_only(const char* name, int len, |
255 Symbol* SymbolTable::lookup_only(const char* name, int len, |
304 |
315 |
305 void SymbolTable::add(Handle class_loader, constantPoolHandle cp, |
316 void SymbolTable::add(Handle class_loader, constantPoolHandle cp, |
306 int names_count, |
317 int names_count, |
307 const char** names, int* lengths, int* cp_indices, |
318 const char** names, int* lengths, int* cp_indices, |
308 unsigned int* hashValues, TRAPS) { |
319 unsigned int* hashValues, TRAPS) { |
|
320 // Grab SymbolTable_lock first. |
|
321 MutexLocker ml(SymbolTable_lock, THREAD); |
|
322 |
309 SymbolTable* table = the_table(); |
323 SymbolTable* table = the_table(); |
310 bool added = table->basic_add(class_loader, cp, names_count, names, lengths, |
324 bool added = table->basic_add(class_loader, cp, names_count, names, lengths, |
311 cp_indices, hashValues, CHECK); |
325 cp_indices, hashValues, CHECK); |
312 if (!added) { |
326 if (!added) { |
313 // do it the hard way |
327 // do it the hard way |
324 unsigned int hash; |
338 unsigned int hash; |
325 Symbol* result = SymbolTable::lookup_only((char*)name, (int)strlen(name), hash); |
339 Symbol* result = SymbolTable::lookup_only((char*)name, (int)strlen(name), hash); |
326 if (result != NULL) { |
340 if (result != NULL) { |
327 return result; |
341 return result; |
328 } |
342 } |
|
343 // Grab SymbolTable_lock first. |
|
344 MutexLocker ml(SymbolTable_lock, THREAD); |
|
345 |
329 SymbolTable* table = the_table(); |
346 SymbolTable* table = the_table(); |
330 int index = table->hash_to_index(hash); |
347 int index = table->hash_to_index(hash); |
331 return table->basic_add(index, (u1*)name, (int)strlen(name), hash, false, THREAD); |
348 return table->basic_add(index, (u1*)name, (int)strlen(name), hash, false, THREAD); |
332 } |
349 } |
333 |
350 |
334 Symbol* SymbolTable::basic_add(int index, u1 *name, int len, |
351 Symbol* SymbolTable::basic_add(int index_arg, u1 *name, int len, |
335 unsigned int hashValue_arg, bool c_heap, TRAPS) { |
352 unsigned int hashValue_arg, bool c_heap, TRAPS) { |
336 assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(), |
353 assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(), |
337 "proposed name of symbol must be stable"); |
354 "proposed name of symbol must be stable"); |
338 |
355 |
339 // Grab SymbolTable_lock first. |
356 // Don't allow symbols to be created which cannot fit in a Symbol*. |
340 MutexLocker ml(SymbolTable_lock, THREAD); |
357 if (len > Symbol::max_length()) { |
|
358 THROW_MSG_0(vmSymbols::java_lang_InternalError(), |
|
359 "name is too long to represent"); |
|
360 } |
|
361 |
|
362 // Cannot hit a safepoint in this function because the "this" pointer can move. |
|
363 No_Safepoint_Verifier nsv; |
341 |
364 |
342 // Check if the symbol table has been rehashed, if so, need to recalculate |
365 // Check if the symbol table has been rehashed, if so, need to recalculate |
343 // the hash value. |
366 // the hash value and index. |
344 unsigned int hashValue = hash_symbol((const char*)name, len, hashValue_arg); |
367 unsigned int hashValue; |
|
368 int index; |
|
369 if (use_alternate_hashcode()) { |
|
370 hashValue = hash_symbol((const char*)name, len); |
|
371 index = hash_to_index(hashValue); |
|
372 } else { |
|
373 hashValue = hashValue_arg; |
|
374 index = index_arg; |
|
375 } |
345 |
376 |
346 // Since look-up was done lock-free, we need to check if another |
377 // Since look-up was done lock-free, we need to check if another |
347 // thread beat us in the race to insert the symbol. |
378 // thread beat us in the race to insert the symbol. |
348 Symbol* test = lookup(index, (char*)name, len, hashValue); |
379 Symbol* test = lookup(index, (char*)name, len, hashValue); |
349 if (test != NULL) { |
380 if (test != NULL) { |
375 THROW_MSG_0(vmSymbols::java_lang_InternalError(), |
406 THROW_MSG_0(vmSymbols::java_lang_InternalError(), |
376 "name is too long to represent"); |
407 "name is too long to represent"); |
377 } |
408 } |
378 } |
409 } |
379 |
410 |
380 // Hold SymbolTable_lock through the symbol creation |
411 // Cannot hit a safepoint in this function because the "this" pointer can move. |
381 MutexLocker ml(SymbolTable_lock, THREAD); |
412 No_Safepoint_Verifier nsv; |
382 |
413 |
383 for (int i=0; i<names_count; i++) { |
414 for (int i=0; i<names_count; i++) { |
384 // Check if the symbol table has been rehashed, if so, need to recalculate |
415 // Check if the symbol table has been rehashed, if so, need to recalculate |
385 // the hash value. |
416 // the hash value. |
386 unsigned int hashValue = hash_symbol(names[i], lengths[i], hashValues[i]); |
417 unsigned int hashValue; |
|
418 if (use_alternate_hashcode()) { |
|
419 hashValue = hash_symbol(names[i], lengths[i]); |
|
420 } else { |
|
421 hashValue = hashValues[i]; |
|
422 } |
387 // Since look-up was done lock-free, we need to check if another |
423 // Since look-up was done lock-free, we need to check if another |
388 // thread beat us in the race to insert the symbol. |
424 // thread beat us in the race to insert the symbol. |
389 int index = hash_to_index(hashValue); |
425 int index = hash_to_index(hashValue); |
390 Symbol* test = lookup(index, names[i], lengths[i], hashValue); |
426 Symbol* test = lookup(index, names[i], lengths[i], hashValue); |
391 if (test != NULL) { |
427 if (test != NULL) { |
585 |
621 |
586 bool StringTable::_needs_rehashing = false; |
622 bool StringTable::_needs_rehashing = false; |
587 jint StringTable::_seed = 0; |
623 jint StringTable::_seed = 0; |
588 |
624 |
589 // Pick hashing algorithm |
625 // Pick hashing algorithm |
590 unsigned int StringTable::hash_string(const jchar* s, int len, unsigned int hashValue) { |
626 unsigned int StringTable::hash_string(const jchar* s, int len) { |
591 return use_alternate_hashcode() ? AltHashing::murmur3_32(seed(), s, len) : |
627 return use_alternate_hashcode() ? AltHashing::murmur3_32(seed(), s, len) : |
592 (hashValue != 0 ? hashValue : java_lang_String::to_hash(s, len)); |
628 java_lang_String::to_hash(s, len); |
593 } |
629 } |
594 |
630 |
595 oop StringTable::lookup(int index, jchar* name, |
631 oop StringTable::lookup(int index, jchar* name, |
596 int len, unsigned int hash) { |
632 int len, unsigned int hash) { |
597 int count = 0; |
633 int count = 0; |
609 } |
645 } |
610 return NULL; |
646 return NULL; |
611 } |
647 } |
612 |
648 |
613 |
649 |
614 oop StringTable::basic_add(int index, Handle string_or_null, jchar* name, |
650 oop StringTable::basic_add(int index_arg, Handle string, jchar* name, |
615 int len, unsigned int hashValue_arg, TRAPS) { |
651 int len, unsigned int hashValue_arg, TRAPS) { |
616 debug_only(StableMemoryChecker smc(name, len * sizeof(name[0]))); |
|
617 assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(), |
|
618 "proposed name of symbol must be stable"); |
|
619 |
|
620 Handle string; |
|
621 // try to reuse the string if possible |
|
622 if (!string_or_null.is_null() && (!JavaObjectsInPerm || string_or_null()->is_perm())) { |
|
623 string = string_or_null; |
|
624 } else { |
|
625 string = java_lang_String::create_tenured_from_unicode(name, len, CHECK_NULL); |
|
626 } |
|
627 |
|
628 // Allocation must be done before grapping the SymbolTable_lock lock |
|
629 MutexLocker ml(StringTable_lock, THREAD); |
|
630 |
652 |
631 assert(java_lang_String::equals(string(), name, len), |
653 assert(java_lang_String::equals(string(), name, len), |
632 "string must be properly initialized"); |
654 "string must be properly initialized"); |
|
655 // Cannot hit a safepoint in this function because the "this" pointer can move. |
|
656 No_Safepoint_Verifier nsv; |
633 |
657 |
634 // Check if the symbol table has been rehashed, if so, need to recalculate |
658 // Check if the symbol table has been rehashed, if so, need to recalculate |
635 // the hash value before second lookup. |
659 // the hash value and index before second lookup. |
636 unsigned int hashValue = hash_string(name, len, hashValue_arg); |
660 unsigned int hashValue; |
|
661 int index; |
|
662 if (use_alternate_hashcode()) { |
|
663 hashValue = hash_string(name, len); |
|
664 index = hash_to_index(hashValue); |
|
665 } else { |
|
666 hashValue = hashValue_arg; |
|
667 index = index_arg; |
|
668 } |
637 |
669 |
638 // Since look-up was done lock-free, we need to check if another |
670 // Since look-up was done lock-free, we need to check if another |
639 // thread beat us in the race to insert the symbol. |
671 // thread beat us in the race to insert the symbol. |
640 |
672 |
641 oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int) |
673 oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int) |
662 |
694 |
663 oop StringTable::intern(Handle string_or_null, jchar* name, |
695 oop StringTable::intern(Handle string_or_null, jchar* name, |
664 int len, TRAPS) { |
696 int len, TRAPS) { |
665 unsigned int hashValue = hash_string(name, len); |
697 unsigned int hashValue = hash_string(name, len); |
666 int index = the_table()->hash_to_index(hashValue); |
698 int index = the_table()->hash_to_index(hashValue); |
667 oop string = the_table()->lookup(index, name, len, hashValue); |
699 oop found_string = the_table()->lookup(index, name, len, hashValue); |
668 |
700 |
669 // Found |
701 // Found |
670 if (string != NULL) return string; |
702 if (found_string != NULL) return found_string; |
|
703 |
|
704 debug_only(StableMemoryChecker smc(name, len * sizeof(name[0]))); |
|
705 assert(!Universe::heap()->is_in_reserved(name) || GC_locker::is_active(), |
|
706 "proposed name of symbol must be stable"); |
|
707 |
|
708 Handle string; |
|
709 // try to reuse the string if possible |
|
710 if (!string_or_null.is_null() && (!JavaObjectsInPerm || string_or_null()->is_perm())) { |
|
711 string = string_or_null; |
|
712 } else { |
|
713 string = java_lang_String::create_tenured_from_unicode(name, len, CHECK_NULL); |
|
714 } |
|
715 |
|
716 // Grab the StringTable_lock before getting the_table() because it could |
|
717 // change at safepoint. |
|
718 MutexLocker ml(StringTable_lock, THREAD); |
671 |
719 |
672 // Otherwise, add to symbol to table |
720 // Otherwise, add to symbol to table |
673 return the_table()->basic_add(index, string_or_null, name, len, |
721 return the_table()->basic_add(index, string, name, len, |
674 hashValue, CHECK_NULL); |
722 hashValue, CHECK_NULL); |
675 } |
723 } |
676 |
724 |
677 oop StringTable::intern(Symbol* symbol, TRAPS) { |
725 oop StringTable::intern(Symbol* symbol, TRAPS) { |
678 if (symbol == NULL) return NULL; |
726 if (symbol == NULL) return NULL; |
711 void StringTable::unlink(BoolObjectClosure* is_alive) { |
759 void StringTable::unlink(BoolObjectClosure* is_alive) { |
712 // Readers of the table are unlocked, so we should only be removing |
760 // Readers of the table are unlocked, so we should only be removing |
713 // entries at a safepoint. |
761 // entries at a safepoint. |
714 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
762 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
715 for (int i = 0; i < the_table()->table_size(); ++i) { |
763 for (int i = 0; i < the_table()->table_size(); ++i) { |
716 for (HashtableEntry<oop>** p = the_table()->bucket_addr(i); *p != NULL; ) { |
764 HashtableEntry<oop>** p = the_table()->bucket_addr(i); |
717 HashtableEntry<oop>* entry = *p; |
765 HashtableEntry<oop>* entry = the_table()->bucket(i); |
718 if (entry->is_shared()) { |
766 while (entry != NULL) { |
|
767 // Shared entries are normally at the end of the bucket and if we run into |
|
768 // a shared entry, then there is nothing more to remove. However, if we |
|
769 // have rehashed the table, then the shared entries are no longer at the |
|
770 // end of the bucket. |
|
771 if (entry->is_shared() && !use_alternate_hashcode()) { |
719 break; |
772 break; |
720 } |
773 } |
721 assert(entry->literal() != NULL, "just checking"); |
774 assert(entry->literal() != NULL, "just checking"); |
722 if (is_alive->do_object_b(entry->literal())) { |
775 if (entry->is_shared() || is_alive->do_object_b(entry->literal())) { |
723 p = entry->next_addr(); |
776 p = entry->next_addr(); |
724 } else { |
777 } else { |
725 *p = entry->next(); |
778 *p = entry->next(); |
726 the_table()->free_entry(entry); |
779 the_table()->free_entry(entry); |
727 } |
780 } |
|
781 entry = (HashtableEntry<oop>*)HashtableEntry<oop>::make_ptr(*p); |
728 } |
782 } |
729 } |
783 } |
730 } |
784 } |
731 |
785 |
732 void StringTable::oops_do(OopClosure* f) { |
786 void StringTable::oops_do(OopClosure* f) { |
793 |
847 |
794 // Create a new table and using alternate hash code, populate the new table |
848 // Create a new table and using alternate hash code, populate the new table |
795 // with the existing strings. Set flag to use the alternate hash code afterwards. |
849 // with the existing strings. Set flag to use the alternate hash code afterwards. |
796 void StringTable::rehash_table() { |
850 void StringTable::rehash_table() { |
797 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
851 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
798 assert(!DumpSharedSpaces, "this should never happen with -Xshare:dump"); |
852 // This should never happen with -Xshare:dump but it might in testing mode. |
|
853 if (DumpSharedSpaces) return; |
799 StringTable* new_table = new StringTable(); |
854 StringTable* new_table = new StringTable(); |
800 |
855 |
801 // Initialize new global seed for hashing. |
856 // Initialize new global seed for hashing. |
802 _seed = AltHashing::compute_seed(); |
857 _seed = AltHashing::compute_seed(); |
803 assert(seed() != 0, "shouldn't be zero"); |
858 assert(seed() != 0, "shouldn't be zero"); |