|
1 /* |
|
2 * Copyright (c) 1997, 2014, 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 #include "precompiled.hpp" |
|
26 #include "classfile/altHashing.hpp" |
|
27 #include "classfile/javaClasses.hpp" |
|
28 #include "classfile/stringTable.hpp" |
|
29 #include "classfile/systemDictionary.hpp" |
|
30 #include "gc_interface/collectedHeap.inline.hpp" |
|
31 #include "memory/allocation.inline.hpp" |
|
32 #include "memory/filemap.hpp" |
|
33 #include "memory/gcLocker.inline.hpp" |
|
34 #include "oops/oop.inline.hpp" |
|
35 #include "oops/oop.inline2.hpp" |
|
36 #include "runtime/mutexLocker.hpp" |
|
37 #include "utilities/hashtable.inline.hpp" |
|
38 #if INCLUDE_ALL_GCS |
|
39 #include "gc_implementation/g1/g1StringDedup.hpp" |
|
40 #endif |
|
41 |
|
42 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC |
|
43 |
|
44 // the number of buckets a thread claims |
|
45 const int ClaimChunkSize = 32; |
|
46 |
|
47 #ifdef ASSERT |
|
48 class StableMemoryChecker : public StackObj { |
|
49 enum { _bufsize = wordSize*4 }; |
|
50 |
|
51 address _region; |
|
52 jint _size; |
|
53 u1 _save_buf[_bufsize]; |
|
54 |
|
55 int sample(u1* save_buf) { |
|
56 if (_size <= _bufsize) { |
|
57 memcpy(save_buf, _region, _size); |
|
58 return _size; |
|
59 } else { |
|
60 // copy head and tail |
|
61 memcpy(&save_buf[0], _region, _bufsize/2); |
|
62 memcpy(&save_buf[_bufsize/2], _region + _size - _bufsize/2, _bufsize/2); |
|
63 return (_bufsize/2)*2; |
|
64 } |
|
65 } |
|
66 |
|
67 public: |
|
68 StableMemoryChecker(const void* region, jint size) { |
|
69 _region = (address) region; |
|
70 _size = size; |
|
71 sample(_save_buf); |
|
72 } |
|
73 |
|
74 bool verify() { |
|
75 u1 check_buf[sizeof(_save_buf)]; |
|
76 int check_size = sample(check_buf); |
|
77 return (0 == memcmp(_save_buf, check_buf, check_size)); |
|
78 } |
|
79 |
|
80 void set_region(const void* region) { _region = (address) region; } |
|
81 }; |
|
82 #endif |
|
83 |
|
84 |
|
85 // -------------------------------------------------------------------------- |
|
86 StringTable* StringTable::_the_table = NULL; |
|
87 |
|
88 bool StringTable::_needs_rehashing = false; |
|
89 |
|
90 volatile int StringTable::_parallel_claimed_idx = 0; |
|
91 |
|
92 // Pick hashing algorithm |
|
93 unsigned int StringTable::hash_string(const jchar* s, int len) { |
|
94 return use_alternate_hashcode() ? AltHashing::murmur3_32(seed(), s, len) : |
|
95 java_lang_String::hash_code(s, len); |
|
96 } |
|
97 |
|
98 oop StringTable::lookup(int index, jchar* name, |
|
99 int len, unsigned int hash) { |
|
100 int count = 0; |
|
101 for (HashtableEntry<oop, mtSymbol>* l = bucket(index); l != NULL; l = l->next()) { |
|
102 count++; |
|
103 if (l->hash() == hash) { |
|
104 if (java_lang_String::equals(l->literal(), name, len)) { |
|
105 return l->literal(); |
|
106 } |
|
107 } |
|
108 } |
|
109 // If the bucket size is too deep check if this hash code is insufficient. |
|
110 if (count >= BasicHashtable<mtSymbol>::rehash_count && !needs_rehashing()) { |
|
111 _needs_rehashing = check_rehash_table(count); |
|
112 } |
|
113 return NULL; |
|
114 } |
|
115 |
|
116 |
|
117 oop StringTable::basic_add(int index_arg, Handle string, jchar* name, |
|
118 int len, unsigned int hashValue_arg, TRAPS) { |
|
119 |
|
120 assert(java_lang_String::equals(string(), name, len), |
|
121 "string must be properly initialized"); |
|
122 // Cannot hit a safepoint in this function because the "this" pointer can move. |
|
123 No_Safepoint_Verifier nsv; |
|
124 |
|
125 // Check if the symbol table has been rehashed, if so, need to recalculate |
|
126 // the hash value and index before second lookup. |
|
127 unsigned int hashValue; |
|
128 int index; |
|
129 if (use_alternate_hashcode()) { |
|
130 hashValue = hash_string(name, len); |
|
131 index = hash_to_index(hashValue); |
|
132 } else { |
|
133 hashValue = hashValue_arg; |
|
134 index = index_arg; |
|
135 } |
|
136 |
|
137 // Since look-up was done lock-free, we need to check if another |
|
138 // thread beat us in the race to insert the symbol. |
|
139 |
|
140 oop test = lookup(index, name, len, hashValue); // calls lookup(u1*, int) |
|
141 if (test != NULL) { |
|
142 // Entry already added |
|
143 return test; |
|
144 } |
|
145 |
|
146 HashtableEntry<oop, mtSymbol>* entry = new_entry(hashValue, string()); |
|
147 add_entry(index, entry); |
|
148 return string(); |
|
149 } |
|
150 |
|
151 |
|
152 oop StringTable::lookup(Symbol* symbol) { |
|
153 ResourceMark rm; |
|
154 int length; |
|
155 jchar* chars = symbol->as_unicode(length); |
|
156 return lookup(chars, length); |
|
157 } |
|
158 |
|
159 |
|
160 oop StringTable::lookup(jchar* name, int len) { |
|
161 unsigned int hash = hash_string(name, len); |
|
162 int index = the_table()->hash_to_index(hash); |
|
163 return the_table()->lookup(index, name, len, hash); |
|
164 } |
|
165 |
|
166 |
|
167 oop StringTable::intern(Handle string_or_null, jchar* name, |
|
168 int len, TRAPS) { |
|
169 unsigned int hashValue = hash_string(name, len); |
|
170 int index = the_table()->hash_to_index(hashValue); |
|
171 oop found_string = the_table()->lookup(index, name, len, hashValue); |
|
172 |
|
173 // Found |
|
174 if (found_string != NULL) return found_string; |
|
175 |
|
176 debug_only(StableMemoryChecker smc(name, len * sizeof(name[0]))); |
|
177 assert(!Universe::heap()->is_in_reserved(name), |
|
178 "proposed name of symbol must be stable"); |
|
179 |
|
180 Handle string; |
|
181 // try to reuse the string if possible |
|
182 if (!string_or_null.is_null()) { |
|
183 string = string_or_null; |
|
184 } else { |
|
185 string = java_lang_String::create_from_unicode(name, len, CHECK_NULL); |
|
186 } |
|
187 |
|
188 #if INCLUDE_ALL_GCS |
|
189 if (G1StringDedup::is_enabled()) { |
|
190 // Deduplicate the string before it is interned. Note that we should never |
|
191 // deduplicate a string after it has been interned. Doing so will counteract |
|
192 // compiler optimizations done on e.g. interned string literals. |
|
193 G1StringDedup::deduplicate(string()); |
|
194 } |
|
195 #endif |
|
196 |
|
197 // Grab the StringTable_lock before getting the_table() because it could |
|
198 // change at safepoint. |
|
199 MutexLocker ml(StringTable_lock, THREAD); |
|
200 |
|
201 // Otherwise, add to symbol to table |
|
202 return the_table()->basic_add(index, string, name, len, |
|
203 hashValue, CHECK_NULL); |
|
204 } |
|
205 |
|
206 oop StringTable::intern(Symbol* symbol, TRAPS) { |
|
207 if (symbol == NULL) return NULL; |
|
208 ResourceMark rm(THREAD); |
|
209 int length; |
|
210 jchar* chars = symbol->as_unicode(length); |
|
211 Handle string; |
|
212 oop result = intern(string, chars, length, CHECK_NULL); |
|
213 return result; |
|
214 } |
|
215 |
|
216 |
|
217 oop StringTable::intern(oop string, TRAPS) |
|
218 { |
|
219 if (string == NULL) return NULL; |
|
220 ResourceMark rm(THREAD); |
|
221 int length; |
|
222 Handle h_string (THREAD, string); |
|
223 jchar* chars = java_lang_String::as_unicode_string(string, length, CHECK_NULL); |
|
224 oop result = intern(h_string, chars, length, CHECK_NULL); |
|
225 return result; |
|
226 } |
|
227 |
|
228 |
|
229 oop StringTable::intern(const char* utf8_string, TRAPS) { |
|
230 if (utf8_string == NULL) return NULL; |
|
231 ResourceMark rm(THREAD); |
|
232 int length = UTF8::unicode_length(utf8_string); |
|
233 jchar* chars = NEW_RESOURCE_ARRAY(jchar, length); |
|
234 UTF8::convert_to_unicode(utf8_string, chars, length); |
|
235 Handle string; |
|
236 oop result = intern(string, chars, length, CHECK_NULL); |
|
237 return result; |
|
238 } |
|
239 |
|
240 void StringTable::unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int* processed, int* removed) { |
|
241 buckets_unlink_or_oops_do(is_alive, f, 0, the_table()->table_size(), processed, removed); |
|
242 } |
|
243 |
|
244 void StringTable::possibly_parallel_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int* processed, int* removed) { |
|
245 // Readers of the table are unlocked, so we should only be removing |
|
246 // entries at a safepoint. |
|
247 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
|
248 const int limit = the_table()->table_size(); |
|
249 |
|
250 for (;;) { |
|
251 // Grab next set of buckets to scan |
|
252 int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize; |
|
253 if (start_idx >= limit) { |
|
254 // End of table |
|
255 break; |
|
256 } |
|
257 |
|
258 int end_idx = MIN2(limit, start_idx + ClaimChunkSize); |
|
259 buckets_unlink_or_oops_do(is_alive, f, start_idx, end_idx, processed, removed); |
|
260 } |
|
261 } |
|
262 |
|
263 void StringTable::buckets_oops_do(OopClosure* f, int start_idx, int end_idx) { |
|
264 const int limit = the_table()->table_size(); |
|
265 |
|
266 assert(0 <= start_idx && start_idx <= limit, |
|
267 err_msg("start_idx (%d) is out of bounds", start_idx)); |
|
268 assert(0 <= end_idx && end_idx <= limit, |
|
269 err_msg("end_idx (%d) is out of bounds", end_idx)); |
|
270 assert(start_idx <= end_idx, |
|
271 err_msg("Index ordering: start_idx=%d, end_idx=%d", |
|
272 start_idx, end_idx)); |
|
273 |
|
274 for (int i = start_idx; i < end_idx; i += 1) { |
|
275 HashtableEntry<oop, mtSymbol>* entry = the_table()->bucket(i); |
|
276 while (entry != NULL) { |
|
277 assert(!entry->is_shared(), "CDS not used for the StringTable"); |
|
278 |
|
279 f->do_oop((oop*)entry->literal_addr()); |
|
280 |
|
281 entry = entry->next(); |
|
282 } |
|
283 } |
|
284 } |
|
285 |
|
286 void StringTable::buckets_unlink_or_oops_do(BoolObjectClosure* is_alive, OopClosure* f, int start_idx, int end_idx, int* processed, int* removed) { |
|
287 const int limit = the_table()->table_size(); |
|
288 |
|
289 assert(0 <= start_idx && start_idx <= limit, |
|
290 err_msg("start_idx (%d) is out of bounds", start_idx)); |
|
291 assert(0 <= end_idx && end_idx <= limit, |
|
292 err_msg("end_idx (%d) is out of bounds", end_idx)); |
|
293 assert(start_idx <= end_idx, |
|
294 err_msg("Index ordering: start_idx=%d, end_idx=%d", |
|
295 start_idx, end_idx)); |
|
296 |
|
297 for (int i = start_idx; i < end_idx; ++i) { |
|
298 HashtableEntry<oop, mtSymbol>** p = the_table()->bucket_addr(i); |
|
299 HashtableEntry<oop, mtSymbol>* entry = the_table()->bucket(i); |
|
300 while (entry != NULL) { |
|
301 assert(!entry->is_shared(), "CDS not used for the StringTable"); |
|
302 |
|
303 if (is_alive->do_object_b(entry->literal())) { |
|
304 if (f != NULL) { |
|
305 f->do_oop((oop*)entry->literal_addr()); |
|
306 } |
|
307 p = entry->next_addr(); |
|
308 } else { |
|
309 *p = entry->next(); |
|
310 the_table()->free_entry(entry); |
|
311 (*removed)++; |
|
312 } |
|
313 (*processed)++; |
|
314 entry = *p; |
|
315 } |
|
316 } |
|
317 } |
|
318 |
|
319 void StringTable::oops_do(OopClosure* f) { |
|
320 buckets_oops_do(f, 0, the_table()->table_size()); |
|
321 } |
|
322 |
|
323 void StringTable::possibly_parallel_oops_do(OopClosure* f) { |
|
324 const int limit = the_table()->table_size(); |
|
325 |
|
326 for (;;) { |
|
327 // Grab next set of buckets to scan |
|
328 int start_idx = Atomic::add(ClaimChunkSize, &_parallel_claimed_idx) - ClaimChunkSize; |
|
329 if (start_idx >= limit) { |
|
330 // End of table |
|
331 break; |
|
332 } |
|
333 |
|
334 int end_idx = MIN2(limit, start_idx + ClaimChunkSize); |
|
335 buckets_oops_do(f, start_idx, end_idx); |
|
336 } |
|
337 } |
|
338 |
|
339 // This verification is part of Universe::verify() and needs to be quick. |
|
340 // See StringTable::verify_and_compare() below for exhaustive verification. |
|
341 void StringTable::verify() { |
|
342 for (int i = 0; i < the_table()->table_size(); ++i) { |
|
343 HashtableEntry<oop, mtSymbol>* p = the_table()->bucket(i); |
|
344 for ( ; p != NULL; p = p->next()) { |
|
345 oop s = p->literal(); |
|
346 guarantee(s != NULL, "interned string is NULL"); |
|
347 unsigned int h = java_lang_String::hash_string(s); |
|
348 guarantee(p->hash() == h, "broken hash in string table entry"); |
|
349 guarantee(the_table()->hash_to_index(h) == i, |
|
350 "wrong index in string table"); |
|
351 } |
|
352 } |
|
353 } |
|
354 |
|
355 void StringTable::dump(outputStream* st) { |
|
356 the_table()->dump_table(st, "StringTable"); |
|
357 } |
|
358 |
|
359 StringTable::VerifyRetTypes StringTable::compare_entries( |
|
360 int bkt1, int e_cnt1, |
|
361 HashtableEntry<oop, mtSymbol>* e_ptr1, |
|
362 int bkt2, int e_cnt2, |
|
363 HashtableEntry<oop, mtSymbol>* e_ptr2) { |
|
364 // These entries are sanity checked by verify_and_compare_entries() |
|
365 // before this function is called. |
|
366 oop str1 = e_ptr1->literal(); |
|
367 oop str2 = e_ptr2->literal(); |
|
368 |
|
369 if (str1 == str2) { |
|
370 tty->print_cr("ERROR: identical oop values (0x" PTR_FORMAT ") " |
|
371 "in entry @ bucket[%d][%d] and entry @ bucket[%d][%d]", |
|
372 (void *)str1, bkt1, e_cnt1, bkt2, e_cnt2); |
|
373 return _verify_fail_continue; |
|
374 } |
|
375 |
|
376 if (java_lang_String::equals(str1, str2)) { |
|
377 tty->print_cr("ERROR: identical String values in entry @ " |
|
378 "bucket[%d][%d] and entry @ bucket[%d][%d]", |
|
379 bkt1, e_cnt1, bkt2, e_cnt2); |
|
380 return _verify_fail_continue; |
|
381 } |
|
382 |
|
383 return _verify_pass; |
|
384 } |
|
385 |
|
386 StringTable::VerifyRetTypes StringTable::verify_entry(int bkt, int e_cnt, |
|
387 HashtableEntry<oop, mtSymbol>* e_ptr, |
|
388 StringTable::VerifyMesgModes mesg_mode) { |
|
389 |
|
390 VerifyRetTypes ret = _verify_pass; // be optimistic |
|
391 |
|
392 oop str = e_ptr->literal(); |
|
393 if (str == NULL) { |
|
394 if (mesg_mode == _verify_with_mesgs) { |
|
395 tty->print_cr("ERROR: NULL oop value in entry @ bucket[%d][%d]", bkt, |
|
396 e_cnt); |
|
397 } |
|
398 // NULL oop means no more verifications are possible |
|
399 return _verify_fail_done; |
|
400 } |
|
401 |
|
402 if (str->klass() != SystemDictionary::String_klass()) { |
|
403 if (mesg_mode == _verify_with_mesgs) { |
|
404 tty->print_cr("ERROR: oop is not a String in entry @ bucket[%d][%d]", |
|
405 bkt, e_cnt); |
|
406 } |
|
407 // not a String means no more verifications are possible |
|
408 return _verify_fail_done; |
|
409 } |
|
410 |
|
411 unsigned int h = java_lang_String::hash_string(str); |
|
412 if (e_ptr->hash() != h) { |
|
413 if (mesg_mode == _verify_with_mesgs) { |
|
414 tty->print_cr("ERROR: broken hash value in entry @ bucket[%d][%d], " |
|
415 "bkt_hash=%d, str_hash=%d", bkt, e_cnt, e_ptr->hash(), h); |
|
416 } |
|
417 ret = _verify_fail_continue; |
|
418 } |
|
419 |
|
420 if (the_table()->hash_to_index(h) != bkt) { |
|
421 if (mesg_mode == _verify_with_mesgs) { |
|
422 tty->print_cr("ERROR: wrong index value for entry @ bucket[%d][%d], " |
|
423 "str_hash=%d, hash_to_index=%d", bkt, e_cnt, h, |
|
424 the_table()->hash_to_index(h)); |
|
425 } |
|
426 ret = _verify_fail_continue; |
|
427 } |
|
428 |
|
429 return ret; |
|
430 } |
|
431 |
|
432 // See StringTable::verify() above for the quick verification that is |
|
433 // part of Universe::verify(). This verification is exhaustive and |
|
434 // reports on every issue that is found. StringTable::verify() only |
|
435 // reports on the first issue that is found. |
|
436 // |
|
437 // StringTable::verify_entry() checks: |
|
438 // - oop value != NULL (same as verify()) |
|
439 // - oop value is a String |
|
440 // - hash(String) == hash in entry (same as verify()) |
|
441 // - index for hash == index of entry (same as verify()) |
|
442 // |
|
443 // StringTable::compare_entries() checks: |
|
444 // - oops are unique across all entries |
|
445 // - String values are unique across all entries |
|
446 // |
|
447 int StringTable::verify_and_compare_entries() { |
|
448 assert(StringTable_lock->is_locked(), "sanity check"); |
|
449 |
|
450 int fail_cnt = 0; |
|
451 |
|
452 // first, verify all the entries individually: |
|
453 for (int bkt = 0; bkt < the_table()->table_size(); bkt++) { |
|
454 HashtableEntry<oop, mtSymbol>* e_ptr = the_table()->bucket(bkt); |
|
455 for (int e_cnt = 0; e_ptr != NULL; e_ptr = e_ptr->next(), e_cnt++) { |
|
456 VerifyRetTypes ret = verify_entry(bkt, e_cnt, e_ptr, _verify_with_mesgs); |
|
457 if (ret != _verify_pass) { |
|
458 fail_cnt++; |
|
459 } |
|
460 } |
|
461 } |
|
462 |
|
463 // Optimization: if the above check did not find any failures, then |
|
464 // the comparison loop below does not need to call verify_entry() |
|
465 // before calling compare_entries(). If there were failures, then we |
|
466 // have to call verify_entry() to see if the entry can be passed to |
|
467 // compare_entries() safely. When we call verify_entry() in the loop |
|
468 // below, we do so quietly to void duplicate messages and we don't |
|
469 // increment fail_cnt because the failures have already been counted. |
|
470 bool need_entry_verify = (fail_cnt != 0); |
|
471 |
|
472 // second, verify all entries relative to each other: |
|
473 for (int bkt1 = 0; bkt1 < the_table()->table_size(); bkt1++) { |
|
474 HashtableEntry<oop, mtSymbol>* e_ptr1 = the_table()->bucket(bkt1); |
|
475 for (int e_cnt1 = 0; e_ptr1 != NULL; e_ptr1 = e_ptr1->next(), e_cnt1++) { |
|
476 if (need_entry_verify) { |
|
477 VerifyRetTypes ret = verify_entry(bkt1, e_cnt1, e_ptr1, |
|
478 _verify_quietly); |
|
479 if (ret == _verify_fail_done) { |
|
480 // cannot use the current entry to compare against other entries |
|
481 continue; |
|
482 } |
|
483 } |
|
484 |
|
485 for (int bkt2 = bkt1; bkt2 < the_table()->table_size(); bkt2++) { |
|
486 HashtableEntry<oop, mtSymbol>* e_ptr2 = the_table()->bucket(bkt2); |
|
487 int e_cnt2; |
|
488 for (e_cnt2 = 0; e_ptr2 != NULL; e_ptr2 = e_ptr2->next(), e_cnt2++) { |
|
489 if (bkt1 == bkt2 && e_cnt2 <= e_cnt1) { |
|
490 // skip the entries up to and including the one that |
|
491 // we're comparing against |
|
492 continue; |
|
493 } |
|
494 |
|
495 if (need_entry_verify) { |
|
496 VerifyRetTypes ret = verify_entry(bkt2, e_cnt2, e_ptr2, |
|
497 _verify_quietly); |
|
498 if (ret == _verify_fail_done) { |
|
499 // cannot compare against this entry |
|
500 continue; |
|
501 } |
|
502 } |
|
503 |
|
504 // compare two entries, report and count any failures: |
|
505 if (compare_entries(bkt1, e_cnt1, e_ptr1, bkt2, e_cnt2, e_ptr2) |
|
506 != _verify_pass) { |
|
507 fail_cnt++; |
|
508 } |
|
509 } |
|
510 } |
|
511 } |
|
512 } |
|
513 return fail_cnt; |
|
514 } |
|
515 |
|
516 // Create a new table and using alternate hash code, populate the new table |
|
517 // with the existing strings. Set flag to use the alternate hash code afterwards. |
|
518 void StringTable::rehash_table() { |
|
519 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
|
520 // This should never happen with -Xshare:dump but it might in testing mode. |
|
521 if (DumpSharedSpaces) return; |
|
522 StringTable* new_table = new StringTable(); |
|
523 |
|
524 // Rehash the table |
|
525 the_table()->move_to(new_table); |
|
526 |
|
527 // Delete the table and buckets (entries are reused in new table). |
|
528 delete _the_table; |
|
529 // Don't check if we need rehashing until the table gets unbalanced again. |
|
530 // Then rehash with a new global seed. |
|
531 _needs_rehashing = false; |
|
532 _the_table = new_table; |
|
533 } |